blob: 79702d405ba726b5abf1a9bb89278ca95b9dd25f [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)) {
1137 pr_info("updating oom_score_adj for %d (%s) from %d to %d because it shares mm with %d (%s). Report if this is unexpected.\n",
1138 task_pid_nr(p), p->comm,
1139 p->signal->oom_score_adj, oom_adj,
1140 task_pid_nr(task), task->comm);
1141 p->signal->oom_score_adj = oom_adj;
1142 if (!legacy && has_capability_noaudit(current, CAP_SYS_RESOURCE))
1143 p->signal->oom_score_adj_min = (short)oom_adj;
1144 }
1145 task_unlock(p);
1146 }
1147 rcu_read_unlock();
1148 mmdrop(mm);
1149 }
Michal Hocko1d5f0ac2016-07-28 15:44:40 -07001150err_unlock:
1151 mutex_unlock(&oom_adj_mutex);
1152 put_task_struct(task);
1153 return err;
1154}
Michal Hockof913da52016-07-28 15:44:37 -07001155
David Rientjesb72bdfa2015-11-05 18:50:32 -08001156/*
1157 * /proc/pid/oom_adj exists solely for backwards compatibility with previous
1158 * kernels. The effective policy is defined by oom_score_adj, which has a
1159 * different scale: oom_adj grew exponentially and oom_score_adj grows linearly.
1160 * Values written to oom_adj are simply mapped linearly to oom_score_adj.
1161 * Processes that become oom disabled via oom_adj will still be oom disabled
1162 * with this implementation.
1163 *
1164 * oom_adj cannot be removed since existing userspace binaries use it.
1165 */
David Rientjesfa0cbbf2012-11-12 17:53:04 -08001166static ssize_t oom_adj_write(struct file *file, const char __user *buf,
1167 size_t count, loff_t *ppos)
1168{
David Rientjesfa0cbbf2012-11-12 17:53:04 -08001169 char buffer[PROC_NUMBUF];
1170 int oom_adj;
David Rientjesfa0cbbf2012-11-12 17:53:04 -08001171 int err;
1172
1173 memset(buffer, 0, sizeof(buffer));
1174 if (count > sizeof(buffer) - 1)
1175 count = sizeof(buffer) - 1;
1176 if (copy_from_user(buffer, buf, count)) {
1177 err = -EFAULT;
1178 goto out;
1179 }
1180
1181 err = kstrtoint(strstrip(buffer), 0, &oom_adj);
1182 if (err)
1183 goto out;
1184 if ((oom_adj < OOM_ADJUST_MIN || oom_adj > OOM_ADJUST_MAX) &&
1185 oom_adj != OOM_DISABLE) {
1186 err = -EINVAL;
1187 goto out;
1188 }
1189
David Rientjesfa0cbbf2012-11-12 17:53:04 -08001190 /*
1191 * Scale /proc/pid/oom_score_adj appropriately ensuring that a maximum
1192 * value is always attainable.
1193 */
1194 if (oom_adj == OOM_ADJUST_MAX)
1195 oom_adj = OOM_SCORE_ADJ_MAX;
1196 else
1197 oom_adj = (oom_adj * OOM_SCORE_ADJ_MAX) / -OOM_DISABLE;
1198
Michal Hocko1d5f0ac2016-07-28 15:44:40 -07001199 err = __set_oom_adj(file, oom_adj, true);
David Rientjesfa0cbbf2012-11-12 17:53:04 -08001200out:
1201 return err < 0 ? err : count;
1202}
1203
1204static const struct file_operations proc_oom_adj_operations = {
1205 .read = oom_adj_read,
1206 .write = oom_adj_write,
1207 .llseek = generic_file_llseek,
1208};
1209
David Rientjesa63d83f2010-08-09 17:19:46 -07001210static ssize_t oom_score_adj_read(struct file *file, char __user *buf,
1211 size_t count, loff_t *ppos)
1212{
Al Viro496ad9a2013-01-23 17:07:38 -05001213 struct task_struct *task = get_proc_task(file_inode(file));
David Rientjesa63d83f2010-08-09 17:19:46 -07001214 char buffer[PROC_NUMBUF];
David Rientjesa9c58b902012-12-11 16:02:54 -08001215 short oom_score_adj = OOM_SCORE_ADJ_MIN;
David Rientjesa63d83f2010-08-09 17:19:46 -07001216 size_t len;
1217
1218 if (!task)
1219 return -ESRCH;
Michal Hockof913da52016-07-28 15:44:37 -07001220 oom_score_adj = task->signal->oom_score_adj;
David Rientjesa63d83f2010-08-09 17:19:46 -07001221 put_task_struct(task);
David Rientjesa9c58b902012-12-11 16:02:54 -08001222 len = snprintf(buffer, sizeof(buffer), "%hd\n", oom_score_adj);
David Rientjesa63d83f2010-08-09 17:19:46 -07001223 return simple_read_from_buffer(buf, count, ppos, buffer, len);
1224}
1225
1226static ssize_t oom_score_adj_write(struct file *file, const char __user *buf,
1227 size_t count, loff_t *ppos)
1228{
David Rientjesa63d83f2010-08-09 17:19:46 -07001229 char buffer[PROC_NUMBUF];
Alexey Dobriyan0a8cb8e2011-05-26 16:25:50 -07001230 int oom_score_adj;
David Rientjesa63d83f2010-08-09 17:19:46 -07001231 int err;
1232
1233 memset(buffer, 0, sizeof(buffer));
1234 if (count > sizeof(buffer) - 1)
1235 count = sizeof(buffer) - 1;
David Rientjes723548b2010-10-26 14:21:25 -07001236 if (copy_from_user(buffer, buf, count)) {
1237 err = -EFAULT;
1238 goto out;
1239 }
David Rientjesa63d83f2010-08-09 17:19:46 -07001240
Alexey Dobriyan0a8cb8e2011-05-26 16:25:50 -07001241 err = kstrtoint(strstrip(buffer), 0, &oom_score_adj);
David Rientjesa63d83f2010-08-09 17:19:46 -07001242 if (err)
David Rientjes723548b2010-10-26 14:21:25 -07001243 goto out;
David Rientjesa63d83f2010-08-09 17:19:46 -07001244 if (oom_score_adj < OOM_SCORE_ADJ_MIN ||
David Rientjes723548b2010-10-26 14:21:25 -07001245 oom_score_adj > OOM_SCORE_ADJ_MAX) {
1246 err = -EINVAL;
1247 goto out;
1248 }
David Rientjesa63d83f2010-08-09 17:19:46 -07001249
Michal Hocko1d5f0ac2016-07-28 15:44:40 -07001250 err = __set_oom_adj(file, oom_score_adj, false);
David Rientjes723548b2010-10-26 14:21:25 -07001251out:
1252 return err < 0 ? err : count;
David Rientjesa63d83f2010-08-09 17:19:46 -07001253}
1254
1255static const struct file_operations proc_oom_score_adj_operations = {
1256 .read = oom_score_adj_read,
1257 .write = oom_score_adj_write,
Arnd Bergmann6038f372010-08-15 18:52:59 +02001258 .llseek = default_llseek,
David Rientjesa63d83f2010-08-09 17:19:46 -07001259};
1260
Linus Torvalds1da177e2005-04-16 15:20:36 -07001261#ifdef CONFIG_AUDITSYSCALL
1262#define TMPBUFLEN 21
1263static ssize_t proc_loginuid_read(struct file * file, char __user * buf,
1264 size_t count, loff_t *ppos)
1265{
Al Viro496ad9a2013-01-23 17:07:38 -05001266 struct inode * inode = file_inode(file);
Eric W. Biederman99f89552006-06-26 00:25:55 -07001267 struct task_struct *task = get_proc_task(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001268 ssize_t length;
1269 char tmpbuf[TMPBUFLEN];
1270
Eric W. Biederman99f89552006-06-26 00:25:55 -07001271 if (!task)
1272 return -ESRCH;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001273 length = scnprintf(tmpbuf, TMPBUFLEN, "%u",
Eric W. Biedermane1760bd2012-09-10 22:39:43 -07001274 from_kuid(file->f_cred->user_ns,
1275 audit_get_loginuid(task)));
Eric W. Biederman99f89552006-06-26 00:25:55 -07001276 put_task_struct(task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001277 return simple_read_from_buffer(buf, count, ppos, tmpbuf, length);
1278}
1279
1280static ssize_t proc_loginuid_write(struct file * file, const char __user * buf,
1281 size_t count, loff_t *ppos)
1282{
Al Viro496ad9a2013-01-23 17:07:38 -05001283 struct inode * inode = file_inode(file);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001284 uid_t loginuid;
Eric W. Biedermane1760bd2012-09-10 22:39:43 -07001285 kuid_t kloginuid;
Alexey Dobriyan774636e2015-09-09 15:36:59 -07001286 int rv;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001287
Paul E. McKenney7dc52152010-02-22 17:04:52 -08001288 rcu_read_lock();
1289 if (current != pid_task(proc_pid(inode), PIDTYPE_PID)) {
1290 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001291 return -EPERM;
Paul E. McKenney7dc52152010-02-22 17:04:52 -08001292 }
1293 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001294
Linus Torvalds1da177e2005-04-16 15:20:36 -07001295 if (*ppos != 0) {
1296 /* No partial writes. */
1297 return -EINVAL;
1298 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001299
Alexey Dobriyan774636e2015-09-09 15:36:59 -07001300 rv = kstrtou32_from_user(buf, count, 10, &loginuid);
1301 if (rv < 0)
1302 return rv;
Eric Paris81407c82013-05-24 09:49:14 -04001303
1304 /* is userspace tring to explicitly UNSET the loginuid? */
1305 if (loginuid == AUDIT_UID_UNSET) {
1306 kloginuid = INVALID_UID;
1307 } else {
1308 kloginuid = make_kuid(file->f_cred->user_ns, loginuid);
Alexey Dobriyan774636e2015-09-09 15:36:59 -07001309 if (!uid_valid(kloginuid))
1310 return -EINVAL;
Eric W. Biedermane1760bd2012-09-10 22:39:43 -07001311 }
1312
Alexey Dobriyan774636e2015-09-09 15:36:59 -07001313 rv = audit_set_loginuid(kloginuid);
1314 if (rv < 0)
1315 return rv;
1316 return count;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001317}
1318
Arjan van de Ven00977a52007-02-12 00:55:34 -08001319static const struct file_operations proc_loginuid_operations = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001320 .read = proc_loginuid_read,
1321 .write = proc_loginuid_write,
Arnd Bergmann87df8422010-03-17 23:06:02 +01001322 .llseek = generic_file_llseek,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001323};
Eric Paris1e0bd752008-03-13 08:15:31 -04001324
1325static ssize_t proc_sessionid_read(struct file * file, char __user * buf,
1326 size_t count, loff_t *ppos)
1327{
Al Viro496ad9a2013-01-23 17:07:38 -05001328 struct inode * inode = file_inode(file);
Eric Paris1e0bd752008-03-13 08:15:31 -04001329 struct task_struct *task = get_proc_task(inode);
1330 ssize_t length;
1331 char tmpbuf[TMPBUFLEN];
1332
1333 if (!task)
1334 return -ESRCH;
1335 length = scnprintf(tmpbuf, TMPBUFLEN, "%u",
1336 audit_get_sessionid(task));
1337 put_task_struct(task);
1338 return simple_read_from_buffer(buf, count, ppos, tmpbuf, length);
1339}
1340
1341static const struct file_operations proc_sessionid_operations = {
1342 .read = proc_sessionid_read,
Arnd Bergmann87df8422010-03-17 23:06:02 +01001343 .llseek = generic_file_llseek,
Eric Paris1e0bd752008-03-13 08:15:31 -04001344};
Linus Torvalds1da177e2005-04-16 15:20:36 -07001345#endif
1346
Akinobu Mitaf4f154f2006-12-08 02:39:47 -08001347#ifdef CONFIG_FAULT_INJECTION
1348static ssize_t proc_fault_inject_read(struct file * file, char __user * buf,
1349 size_t count, loff_t *ppos)
1350{
Al Viro496ad9a2013-01-23 17:07:38 -05001351 struct task_struct *task = get_proc_task(file_inode(file));
Akinobu Mitaf4f154f2006-12-08 02:39:47 -08001352 char buffer[PROC_NUMBUF];
1353 size_t len;
1354 int make_it_fail;
Akinobu Mitaf4f154f2006-12-08 02:39:47 -08001355
1356 if (!task)
1357 return -ESRCH;
1358 make_it_fail = task->make_it_fail;
1359 put_task_struct(task);
1360
1361 len = snprintf(buffer, sizeof(buffer), "%i\n", make_it_fail);
Akinobu Mita0c28f282007-05-08 00:31:41 -07001362
1363 return simple_read_from_buffer(buf, count, ppos, buffer, len);
Akinobu Mitaf4f154f2006-12-08 02:39:47 -08001364}
1365
1366static ssize_t proc_fault_inject_write(struct file * file,
1367 const char __user * buf, size_t count, loff_t *ppos)
1368{
1369 struct task_struct *task;
Alexey Dobriyan774636e2015-09-09 15:36:59 -07001370 char buffer[PROC_NUMBUF];
Akinobu Mitaf4f154f2006-12-08 02:39:47 -08001371 int make_it_fail;
Alexey Dobriyan774636e2015-09-09 15:36:59 -07001372 int rv;
Akinobu Mitaf4f154f2006-12-08 02:39:47 -08001373
1374 if (!capable(CAP_SYS_RESOURCE))
1375 return -EPERM;
1376 memset(buffer, 0, sizeof(buffer));
1377 if (count > sizeof(buffer) - 1)
1378 count = sizeof(buffer) - 1;
1379 if (copy_from_user(buffer, buf, count))
1380 return -EFAULT;
Alexey Dobriyan774636e2015-09-09 15:36:59 -07001381 rv = kstrtoint(strstrip(buffer), 0, &make_it_fail);
1382 if (rv < 0)
1383 return rv;
Dave Jones16caed32014-04-07 15:39:15 -07001384 if (make_it_fail < 0 || make_it_fail > 1)
1385 return -EINVAL;
1386
Al Viro496ad9a2013-01-23 17:07:38 -05001387 task = get_proc_task(file_inode(file));
Akinobu Mitaf4f154f2006-12-08 02:39:47 -08001388 if (!task)
1389 return -ESRCH;
1390 task->make_it_fail = make_it_fail;
1391 put_task_struct(task);
Vincent Licba8aaf2009-09-22 16:45:38 -07001392
1393 return count;
Akinobu Mitaf4f154f2006-12-08 02:39:47 -08001394}
1395
Arjan van de Ven00977a52007-02-12 00:55:34 -08001396static const struct file_operations proc_fault_inject_operations = {
Akinobu Mitaf4f154f2006-12-08 02:39:47 -08001397 .read = proc_fault_inject_read,
1398 .write = proc_fault_inject_write,
Arnd Bergmann87df8422010-03-17 23:06:02 +01001399 .llseek = generic_file_llseek,
Akinobu Mitaf4f154f2006-12-08 02:39:47 -08001400};
1401#endif
1402
Arjan van de Ven97455122008-01-25 21:08:34 +01001403
Ingo Molnar43ae34c2007-07-09 18:52:00 +02001404#ifdef CONFIG_SCHED_DEBUG
1405/*
1406 * Print out various scheduling related per-task fields:
1407 */
1408static int sched_show(struct seq_file *m, void *v)
1409{
1410 struct inode *inode = m->private;
1411 struct task_struct *p;
1412
Ingo Molnar43ae34c2007-07-09 18:52:00 +02001413 p = get_proc_task(inode);
1414 if (!p)
1415 return -ESRCH;
1416 proc_sched_show_task(p, m);
1417
1418 put_task_struct(p);
1419
1420 return 0;
1421}
1422
1423static ssize_t
1424sched_write(struct file *file, const char __user *buf,
1425 size_t count, loff_t *offset)
1426{
Al Viro496ad9a2013-01-23 17:07:38 -05001427 struct inode *inode = file_inode(file);
Ingo Molnar43ae34c2007-07-09 18:52:00 +02001428 struct task_struct *p;
1429
Ingo Molnar43ae34c2007-07-09 18:52:00 +02001430 p = get_proc_task(inode);
1431 if (!p)
1432 return -ESRCH;
1433 proc_sched_set_task(p);
1434
1435 put_task_struct(p);
1436
1437 return count;
1438}
1439
1440static int sched_open(struct inode *inode, struct file *filp)
1441{
Jovi Zhangc6a34052011-01-12 17:00:34 -08001442 return single_open(filp, sched_show, inode);
Ingo Molnar43ae34c2007-07-09 18:52:00 +02001443}
1444
1445static const struct file_operations proc_pid_sched_operations = {
1446 .open = sched_open,
1447 .read = seq_read,
1448 .write = sched_write,
1449 .llseek = seq_lseek,
Alexey Dobriyan5ea473a2007-07-31 00:38:50 -07001450 .release = single_release,
Ingo Molnar43ae34c2007-07-09 18:52:00 +02001451};
1452
1453#endif
1454
Mike Galbraith5091faa2010-11-30 14:18:03 +01001455#ifdef CONFIG_SCHED_AUTOGROUP
1456/*
1457 * Print out autogroup related information:
1458 */
1459static int sched_autogroup_show(struct seq_file *m, void *v)
1460{
1461 struct inode *inode = m->private;
1462 struct task_struct *p;
1463
1464 p = get_proc_task(inode);
1465 if (!p)
1466 return -ESRCH;
1467 proc_sched_autogroup_show_task(p, m);
1468
1469 put_task_struct(p);
1470
1471 return 0;
1472}
1473
1474static ssize_t
1475sched_autogroup_write(struct file *file, const char __user *buf,
1476 size_t count, loff_t *offset)
1477{
Al Viro496ad9a2013-01-23 17:07:38 -05001478 struct inode *inode = file_inode(file);
Mike Galbraith5091faa2010-11-30 14:18:03 +01001479 struct task_struct *p;
1480 char buffer[PROC_NUMBUF];
Alexey Dobriyan0a8cb8e2011-05-26 16:25:50 -07001481 int nice;
Mike Galbraith5091faa2010-11-30 14:18:03 +01001482 int err;
1483
1484 memset(buffer, 0, sizeof(buffer));
1485 if (count > sizeof(buffer) - 1)
1486 count = sizeof(buffer) - 1;
1487 if (copy_from_user(buffer, buf, count))
1488 return -EFAULT;
1489
Alexey Dobriyan0a8cb8e2011-05-26 16:25:50 -07001490 err = kstrtoint(strstrip(buffer), 0, &nice);
1491 if (err < 0)
1492 return err;
Mike Galbraith5091faa2010-11-30 14:18:03 +01001493
1494 p = get_proc_task(inode);
1495 if (!p)
1496 return -ESRCH;
1497
Hiroshi Shimamoto2e5b5b32012-02-23 17:41:27 +09001498 err = proc_sched_autogroup_set_nice(p, nice);
Mike Galbraith5091faa2010-11-30 14:18:03 +01001499 if (err)
1500 count = err;
1501
1502 put_task_struct(p);
1503
1504 return count;
1505}
1506
1507static int sched_autogroup_open(struct inode *inode, struct file *filp)
1508{
1509 int ret;
1510
1511 ret = single_open(filp, sched_autogroup_show, NULL);
1512 if (!ret) {
1513 struct seq_file *m = filp->private_data;
1514
1515 m->private = inode;
1516 }
1517 return ret;
1518}
1519
1520static const struct file_operations proc_pid_sched_autogroup_operations = {
1521 .open = sched_autogroup_open,
1522 .read = seq_read,
1523 .write = sched_autogroup_write,
1524 .llseek = seq_lseek,
1525 .release = single_release,
1526};
1527
1528#endif /* CONFIG_SCHED_AUTOGROUP */
1529
john stultz4614a696b2009-12-14 18:00:05 -08001530static ssize_t comm_write(struct file *file, const char __user *buf,
1531 size_t count, loff_t *offset)
1532{
Al Viro496ad9a2013-01-23 17:07:38 -05001533 struct inode *inode = file_inode(file);
john stultz4614a696b2009-12-14 18:00:05 -08001534 struct task_struct *p;
1535 char buffer[TASK_COMM_LEN];
David Rientjes830e0fc2013-04-30 15:28:18 -07001536 const size_t maxlen = sizeof(buffer) - 1;
john stultz4614a696b2009-12-14 18:00:05 -08001537
1538 memset(buffer, 0, sizeof(buffer));
David Rientjes830e0fc2013-04-30 15:28:18 -07001539 if (copy_from_user(buffer, buf, count > maxlen ? maxlen : count))
john stultz4614a696b2009-12-14 18:00:05 -08001540 return -EFAULT;
1541
1542 p = get_proc_task(inode);
1543 if (!p)
1544 return -ESRCH;
1545
1546 if (same_thread_group(current, p))
1547 set_task_comm(p, buffer);
1548 else
1549 count = -EINVAL;
1550
1551 put_task_struct(p);
1552
1553 return count;
1554}
1555
1556static int comm_show(struct seq_file *m, void *v)
1557{
1558 struct inode *inode = m->private;
1559 struct task_struct *p;
1560
1561 p = get_proc_task(inode);
1562 if (!p)
1563 return -ESRCH;
1564
1565 task_lock(p);
1566 seq_printf(m, "%s\n", p->comm);
1567 task_unlock(p);
1568
1569 put_task_struct(p);
1570
1571 return 0;
1572}
1573
1574static int comm_open(struct inode *inode, struct file *filp)
1575{
Jovi Zhangc6a34052011-01-12 17:00:34 -08001576 return single_open(filp, comm_show, inode);
john stultz4614a696b2009-12-14 18:00:05 -08001577}
1578
1579static const struct file_operations proc_pid_set_comm_operations = {
1580 .open = comm_open,
1581 .read = seq_read,
1582 .write = comm_write,
1583 .llseek = seq_lseek,
1584 .release = single_release,
1585};
1586
Cyrill Gorcunov7773fbc2012-01-10 15:11:20 -08001587static int proc_exe_link(struct dentry *dentry, struct path *exe_path)
Matt Helsley925d1c42008-04-29 01:01:36 -07001588{
1589 struct task_struct *task;
Matt Helsley925d1c42008-04-29 01:01:36 -07001590 struct file *exe_file;
1591
David Howells2b0143b2015-03-17 22:25:59 +00001592 task = get_proc_task(d_inode(dentry));
Matt Helsley925d1c42008-04-29 01:01:36 -07001593 if (!task)
1594 return -ENOENT;
Mateusz Guzikcd81a912016-08-23 16:20:38 +02001595 exe_file = get_task_exe_file(task);
Matt Helsley925d1c42008-04-29 01:01:36 -07001596 put_task_struct(task);
Matt Helsley925d1c42008-04-29 01:01:36 -07001597 if (exe_file) {
1598 *exe_path = exe_file->f_path;
1599 path_get(&exe_file->f_path);
1600 fput(exe_file);
1601 return 0;
1602 } else
1603 return -ENOENT;
1604}
1605
Al Viro6b255392015-11-17 10:20:54 -05001606static const char *proc_pid_get_link(struct dentry *dentry,
Al Virofceef392015-12-29 15:58:39 -05001607 struct inode *inode,
1608 struct delayed_call *done)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001609{
Christoph Hellwig408ef012012-06-18 10:47:03 -04001610 struct path path;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001611 int error = -EACCES;
1612
Al Viro6b255392015-11-17 10:20:54 -05001613 if (!dentry)
1614 return ERR_PTR(-ECHILD);
1615
Eric W. Biederman778c1142006-06-26 00:25:58 -07001616 /* Are we allowed to snoop on the tasks file descriptors? */
1617 if (!proc_fd_access_allowed(inode))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001618 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001619
Christoph Hellwig408ef012012-06-18 10:47:03 -04001620 error = PROC_I(inode)->op.proc_get_link(dentry, &path);
1621 if (error)
1622 goto out;
1623
Al Viro6e771372015-05-02 13:37:52 -04001624 nd_jump_link(&path);
Christoph Hellwig408ef012012-06-18 10:47:03 -04001625 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001626out:
Al Viro008b1502005-08-20 00:17:39 +01001627 return ERR_PTR(error);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001628}
1629
Jan Blunck3dcd25f2008-02-14 19:38:35 -08001630static int do_proc_readlink(struct path *path, char __user *buffer, int buflen)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001631{
Mel Gormane12ba742007-10-16 01:25:52 -07001632 char *tmp = (char*)__get_free_page(GFP_TEMPORARY);
Jan Blunck3dcd25f2008-02-14 19:38:35 -08001633 char *pathname;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001634 int len;
1635
1636 if (!tmp)
1637 return -ENOMEM;
Akinobu Mita0c28f282007-05-08 00:31:41 -07001638
Eric W. Biederman7b2a69b2010-12-05 15:51:21 -08001639 pathname = d_path(path, tmp, PAGE_SIZE);
Jan Blunck3dcd25f2008-02-14 19:38:35 -08001640 len = PTR_ERR(pathname);
1641 if (IS_ERR(pathname))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001642 goto out;
Jan Blunck3dcd25f2008-02-14 19:38:35 -08001643 len = tmp + PAGE_SIZE - 1 - pathname;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001644
1645 if (len > buflen)
1646 len = buflen;
Jan Blunck3dcd25f2008-02-14 19:38:35 -08001647 if (copy_to_user(buffer, pathname, len))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001648 len = -EFAULT;
1649 out:
1650 free_page((unsigned long)tmp);
1651 return len;
1652}
1653
1654static int proc_pid_readlink(struct dentry * dentry, char __user * buffer, int buflen)
1655{
1656 int error = -EACCES;
David Howells2b0143b2015-03-17 22:25:59 +00001657 struct inode *inode = d_inode(dentry);
Jan Blunck3dcd25f2008-02-14 19:38:35 -08001658 struct path path;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001659
Eric W. Biederman778c1142006-06-26 00:25:58 -07001660 /* Are we allowed to snoop on the tasks file descriptors? */
1661 if (!proc_fd_access_allowed(inode))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001662 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001663
Cyrill Gorcunov7773fbc2012-01-10 15:11:20 -08001664 error = PROC_I(inode)->op.proc_get_link(dentry, &path);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001665 if (error)
1666 goto out;
1667
Jan Blunck3dcd25f2008-02-14 19:38:35 -08001668 error = do_proc_readlink(&path, buffer, buflen);
1669 path_put(&path);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001670out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001671 return error;
1672}
1673
Cyrill Gorcunovfaf60af2012-08-23 14:43:24 +04001674const struct inode_operations proc_pid_link_inode_operations = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001675 .readlink = proc_pid_readlink,
Al Viro6b255392015-11-17 10:20:54 -05001676 .get_link = proc_pid_get_link,
Linus Torvalds6d76fa52006-07-15 12:26:45 -07001677 .setattr = proc_setattr,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001678};
1679
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001680
1681/* building an inode */
1682
Eric W. Biederman6b4e3062010-03-07 16:41:34 -08001683struct inode *proc_pid_make_inode(struct super_block * sb, struct task_struct *task)
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001684{
1685 struct inode * inode;
1686 struct proc_inode *ei;
David Howellsc69e8d92008-11-14 10:39:19 +11001687 const struct cred *cred;
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001688
1689 /* We need a new inode */
1690
1691 inode = new_inode(sb);
1692 if (!inode)
1693 goto out;
1694
1695 /* Common stuff */
1696 ei = PROC_I(inode);
Christoph Hellwig85fe4022010-10-23 11:19:54 -04001697 inode->i_ino = get_next_ino();
Deepa Dinamani078cd822016-09-14 07:48:04 -07001698 inode->i_mtime = inode->i_atime = inode->i_ctime = current_time(inode);
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001699 inode->i_op = &proc_def_inode_operations;
1700
1701 /*
1702 * grab the reference to task.
1703 */
Oleg Nesterov1a657f782006-10-02 02:18:59 -07001704 ei->pid = get_task_pid(task, PIDTYPE_PID);
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001705 if (!ei->pid)
1706 goto out_unlock;
1707
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001708 if (task_dumpable(task)) {
David Howellsc69e8d92008-11-14 10:39:19 +11001709 rcu_read_lock();
1710 cred = __task_cred(task);
1711 inode->i_uid = cred->euid;
1712 inode->i_gid = cred->egid;
1713 rcu_read_unlock();
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001714 }
1715 security_task_to_inode(task, inode);
1716
1717out:
1718 return inode;
1719
1720out_unlock:
1721 iput(inode);
1722 return NULL;
1723}
1724
Eric W. Biederman6b4e3062010-03-07 16:41:34 -08001725int pid_getattr(struct vfsmount *mnt, struct dentry *dentry, struct kstat *stat)
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001726{
David Howells2b0143b2015-03-17 22:25:59 +00001727 struct inode *inode = d_inode(dentry);
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001728 struct task_struct *task;
David Howellsc69e8d92008-11-14 10:39:19 +11001729 const struct cred *cred;
Vasiliy Kulikov04996802012-01-10 15:11:31 -08001730 struct pid_namespace *pid = dentry->d_sb->s_fs_info;
David Howellsc69e8d92008-11-14 10:39:19 +11001731
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001732 generic_fillattr(inode, stat);
1733
1734 rcu_read_lock();
Eric W. Biedermandcb0f222012-02-09 08:48:21 -08001735 stat->uid = GLOBAL_ROOT_UID;
1736 stat->gid = GLOBAL_ROOT_GID;
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001737 task = pid_task(proc_pid(inode), PIDTYPE_PID);
1738 if (task) {
Vasiliy Kulikov04996802012-01-10 15:11:31 -08001739 if (!has_pid_permissions(pid, task, 2)) {
1740 rcu_read_unlock();
1741 /*
1742 * This doesn't prevent learning whether PID exists,
1743 * it only makes getattr() consistent with readdir().
1744 */
1745 return -ENOENT;
1746 }
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001747 if ((inode->i_mode == (S_IFDIR|S_IRUGO|S_IXUGO)) ||
1748 task_dumpable(task)) {
David Howellsc69e8d92008-11-14 10:39:19 +11001749 cred = __task_cred(task);
1750 stat->uid = cred->euid;
1751 stat->gid = cred->egid;
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001752 }
1753 }
1754 rcu_read_unlock();
1755 return 0;
1756}
1757
1758/* dentry stuff */
1759
1760/*
1761 * Exceptional case: normally we are not allowed to unhash a busy
1762 * directory. In this case, however, we can do it - no aliasing problems
1763 * due to the way we treat inodes.
1764 *
1765 * Rewrite the inode's ownerships here because the owning task may have
1766 * performed a setuid(), etc.
1767 *
1768 * Before the /proc/pid/status file was created the only way to read
1769 * the effective uid of a /process was to stat /proc/pid. Reading
1770 * /proc/pid/status is slow enough that procps and other packages
1771 * kept stating /proc/pid. To keep the rules in /proc simple I have
1772 * made this apply to all per process world readable and executable
1773 * directories.
1774 */
Al Viro0b728e12012-06-10 16:03:43 -04001775int pid_revalidate(struct dentry *dentry, unsigned int flags)
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001776{
Nick Piggin34286d62011-01-07 17:49:57 +11001777 struct inode *inode;
1778 struct task_struct *task;
David Howellsc69e8d92008-11-14 10:39:19 +11001779 const struct cred *cred;
1780
Al Viro0b728e12012-06-10 16:03:43 -04001781 if (flags & LOOKUP_RCU)
Nick Piggin34286d62011-01-07 17:49:57 +11001782 return -ECHILD;
1783
David Howells2b0143b2015-03-17 22:25:59 +00001784 inode = d_inode(dentry);
Nick Piggin34286d62011-01-07 17:49:57 +11001785 task = get_proc_task(inode);
1786
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001787 if (task) {
1788 if ((inode->i_mode == (S_IFDIR|S_IRUGO|S_IXUGO)) ||
1789 task_dumpable(task)) {
David Howellsc69e8d92008-11-14 10:39:19 +11001790 rcu_read_lock();
1791 cred = __task_cred(task);
1792 inode->i_uid = cred->euid;
1793 inode->i_gid = cred->egid;
1794 rcu_read_unlock();
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001795 } else {
Eric W. Biedermandcb0f222012-02-09 08:48:21 -08001796 inode->i_uid = GLOBAL_ROOT_UID;
1797 inode->i_gid = GLOBAL_ROOT_GID;
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001798 }
1799 inode->i_mode &= ~(S_ISUID | S_ISGID);
1800 security_task_to_inode(task, inode);
1801 put_task_struct(task);
1802 return 1;
1803 }
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001804 return 0;
1805}
1806
Oleg Nesterovd855a4b2014-01-23 15:55:39 -08001807static inline bool proc_inode_is_dead(struct inode *inode)
1808{
1809 return !proc_pid(inode)->tasks[PIDTYPE_PID].first;
1810}
1811
David Howells1dd704b2013-04-12 01:08:50 +01001812int pid_delete_dentry(const struct dentry *dentry)
1813{
1814 /* Is the task we represent dead?
1815 * If so, then don't put the dentry on the lru list,
1816 * kill it immediately.
1817 */
David Howells2b0143b2015-03-17 22:25:59 +00001818 return proc_inode_is_dead(d_inode(dentry));
David Howells1dd704b2013-04-12 01:08:50 +01001819}
1820
Eric W. Biederman6b4e3062010-03-07 16:41:34 -08001821const struct dentry_operations pid_dentry_operations =
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001822{
1823 .d_revalidate = pid_revalidate,
1824 .d_delete = pid_delete_dentry,
1825};
1826
1827/* Lookups */
1828
Eric W. Biederman1c0d04c2006-10-02 02:18:57 -07001829/*
1830 * Fill a directory entry.
1831 *
1832 * If possible create the dcache entry and derive our inode number and
1833 * file type from dcache entry.
1834 *
1835 * Since all of the proc inode numbers are dynamically generated, the inode
1836 * numbers do not exist until the inode is cache. This means creating the
1837 * the dcache entry in readdir is necessary to keep the inode numbers
1838 * reported by readdir in sync with the inode numbers reported
1839 * by stat.
1840 */
Al Virof0c3b502013-05-16 12:07:31 -04001841bool proc_fill_cache(struct file *file, struct dir_context *ctx,
Eric W. Biederman6b4e3062010-03-07 16:41:34 -08001842 const char *name, int len,
Eric Dumazetc5141e62007-05-08 00:26:15 -07001843 instantiate_t instantiate, struct task_struct *task, const void *ptr)
Eric W. Biederman61a28782006-10-02 02:18:49 -07001844{
Al Virof0c3b502013-05-16 12:07:31 -04001845 struct dentry *child, *dir = file->f_path.dentry;
Al Viro1df98b82013-06-15 11:33:10 +04001846 struct qstr qname = QSTR_INIT(name, len);
Eric W. Biederman61a28782006-10-02 02:18:49 -07001847 struct inode *inode;
Al Viro1df98b82013-06-15 11:33:10 +04001848 unsigned type;
1849 ino_t ino;
Eric W. Biederman61a28782006-10-02 02:18:49 -07001850
Al Viro1df98b82013-06-15 11:33:10 +04001851 child = d_hash_and_lookup(dir, &qname);
Eric W. Biederman61a28782006-10-02 02:18:49 -07001852 if (!child) {
Al Viro37817642016-04-20 16:31:31 -04001853 DECLARE_WAIT_QUEUE_HEAD_ONSTACK(wq);
1854 child = d_alloc_parallel(dir, &qname, &wq);
1855 if (IS_ERR(child))
Al Viro1df98b82013-06-15 11:33:10 +04001856 goto end_instantiate;
Al Viro37817642016-04-20 16:31:31 -04001857 if (d_in_lookup(child)) {
1858 int err = instantiate(d_inode(dir), child, task, ptr);
1859 d_lookup_done(child);
1860 if (err < 0) {
1861 dput(child);
1862 goto end_instantiate;
1863 }
Eric W. Biederman61a28782006-10-02 02:18:49 -07001864 }
1865 }
David Howells2b0143b2015-03-17 22:25:59 +00001866 inode = d_inode(child);
Al Viro147ce692013-06-15 10:26:35 +04001867 ino = inode->i_ino;
1868 type = inode->i_mode >> 12;
Eric W. Biederman61a28782006-10-02 02:18:49 -07001869 dput(child);
Al Virof0c3b502013-05-16 12:07:31 -04001870 return dir_emit(ctx, name, len, ino, type);
Al Viro1df98b82013-06-15 11:33:10 +04001871
1872end_instantiate:
1873 return dir_emit(ctx, name, len, 1, DT_UNKNOWN);
Eric W. Biederman61a28782006-10-02 02:18:49 -07001874}
1875
Pavel Emelyanov640708a2012-01-10 15:11:23 -08001876/*
1877 * dname_to_vma_addr - maps a dentry name into two unsigned longs
1878 * which represent vma start and end addresses.
1879 */
1880static int dname_to_vma_addr(struct dentry *dentry,
1881 unsigned long *start, unsigned long *end)
1882{
Alexey Dobriyane0a1a012018-02-06 15:36:59 -08001883 const char *str = dentry->d_name.name;
1884 unsigned long long sval, eval;
1885 unsigned int len;
1886
1887 len = _parse_integer(str, 16, &sval);
1888 if (len & KSTRTOX_OVERFLOW)
Pavel Emelyanov640708a2012-01-10 15:11:23 -08001889 return -EINVAL;
Alexey Dobriyane0a1a012018-02-06 15:36:59 -08001890 if (sval != (unsigned long)sval)
1891 return -EINVAL;
1892 str += len;
1893
1894 if (*str != '-')
1895 return -EINVAL;
1896 str++;
1897
1898 len = _parse_integer(str, 16, &eval);
1899 if (len & KSTRTOX_OVERFLOW)
1900 return -EINVAL;
1901 if (eval != (unsigned long)eval)
1902 return -EINVAL;
1903 str += len;
1904
1905 if (*str != '\0')
1906 return -EINVAL;
1907
1908 *start = sval;
1909 *end = eval;
Pavel Emelyanov640708a2012-01-10 15:11:23 -08001910
1911 return 0;
1912}
1913
Al Viro0b728e12012-06-10 16:03:43 -04001914static int map_files_d_revalidate(struct dentry *dentry, unsigned int flags)
Pavel Emelyanov640708a2012-01-10 15:11:23 -08001915{
1916 unsigned long vm_start, vm_end;
1917 bool exact_vma_exists = false;
1918 struct mm_struct *mm = NULL;
1919 struct task_struct *task;
1920 const struct cred *cred;
1921 struct inode *inode;
1922 int status = 0;
1923
Al Viro0b728e12012-06-10 16:03:43 -04001924 if (flags & LOOKUP_RCU)
Pavel Emelyanov640708a2012-01-10 15:11:23 -08001925 return -ECHILD;
1926
David Howells2b0143b2015-03-17 22:25:59 +00001927 inode = d_inode(dentry);
Pavel Emelyanov640708a2012-01-10 15:11:23 -08001928 task = get_proc_task(inode);
1929 if (!task)
1930 goto out_notask;
1931
Jann Horncaaee622016-01-20 15:00:04 -08001932 mm = mm_access(task, PTRACE_MODE_READ_FSCREDS);
Cong Wang2344bec2012-05-31 16:26:18 -07001933 if (IS_ERR_OR_NULL(mm))
Pavel Emelyanov640708a2012-01-10 15:11:23 -08001934 goto out;
1935
1936 if (!dname_to_vma_addr(dentry, &vm_start, &vm_end)) {
1937 down_read(&mm->mmap_sem);
1938 exact_vma_exists = !!find_exact_vma(mm, vm_start, vm_end);
1939 up_read(&mm->mmap_sem);
1940 }
1941
1942 mmput(mm);
1943
1944 if (exact_vma_exists) {
1945 if (task_dumpable(task)) {
1946 rcu_read_lock();
1947 cred = __task_cred(task);
1948 inode->i_uid = cred->euid;
1949 inode->i_gid = cred->egid;
1950 rcu_read_unlock();
1951 } else {
Eric W. Biedermandcb0f222012-02-09 08:48:21 -08001952 inode->i_uid = GLOBAL_ROOT_UID;
1953 inode->i_gid = GLOBAL_ROOT_GID;
Pavel Emelyanov640708a2012-01-10 15:11:23 -08001954 }
1955 security_task_to_inode(task, inode);
1956 status = 1;
1957 }
1958
1959out:
1960 put_task_struct(task);
1961
1962out_notask:
Pavel Emelyanov640708a2012-01-10 15:11:23 -08001963 return status;
1964}
1965
1966static const struct dentry_operations tid_map_files_dentry_operations = {
1967 .d_revalidate = map_files_d_revalidate,
1968 .d_delete = pid_delete_dentry,
1969};
1970
Al Viro6b255392015-11-17 10:20:54 -05001971static int map_files_get_link(struct dentry *dentry, struct path *path)
Pavel Emelyanov640708a2012-01-10 15:11:23 -08001972{
1973 unsigned long vm_start, vm_end;
1974 struct vm_area_struct *vma;
1975 struct task_struct *task;
1976 struct mm_struct *mm;
1977 int rc;
1978
1979 rc = -ENOENT;
David Howells2b0143b2015-03-17 22:25:59 +00001980 task = get_proc_task(d_inode(dentry));
Pavel Emelyanov640708a2012-01-10 15:11:23 -08001981 if (!task)
1982 goto out;
1983
1984 mm = get_task_mm(task);
1985 put_task_struct(task);
1986 if (!mm)
1987 goto out;
1988
1989 rc = dname_to_vma_addr(dentry, &vm_start, &vm_end);
1990 if (rc)
1991 goto out_mmput;
1992
Artem Fetishev70335ab2014-03-10 15:49:45 -07001993 rc = -ENOENT;
Pavel Emelyanov640708a2012-01-10 15:11:23 -08001994 down_read(&mm->mmap_sem);
1995 vma = find_exact_vma(mm, vm_start, vm_end);
1996 if (vma && vma->vm_file) {
1997 *path = vma->vm_file->f_path;
1998 path_get(path);
1999 rc = 0;
2000 }
2001 up_read(&mm->mmap_sem);
2002
2003out_mmput:
2004 mmput(mm);
2005out:
2006 return rc;
2007}
2008
2009struct map_files_info {
Al Viro7b540d02012-08-27 14:55:26 -04002010 fmode_t mode;
Pavel Emelyanov640708a2012-01-10 15:11:23 -08002011 unsigned long len;
2012 unsigned char name[4*sizeof(long)+2]; /* max: %lx-%lx\0 */
2013};
2014
Calvin Owensbdb4d102015-09-09 15:35:54 -07002015/*
2016 * Only allow CAP_SYS_ADMIN to follow the links, due to concerns about how the
2017 * symlinks may be used to bypass permissions on ancestor directories in the
2018 * path to the file in question.
2019 */
2020static const char *
Al Viro6b255392015-11-17 10:20:54 -05002021proc_map_files_get_link(struct dentry *dentry,
Al Virofceef392015-12-29 15:58:39 -05002022 struct inode *inode,
2023 struct delayed_call *done)
Calvin Owensbdb4d102015-09-09 15:35:54 -07002024{
2025 if (!capable(CAP_SYS_ADMIN))
2026 return ERR_PTR(-EPERM);
2027
Al Virofceef392015-12-29 15:58:39 -05002028 return proc_pid_get_link(dentry, inode, done);
Calvin Owensbdb4d102015-09-09 15:35:54 -07002029}
2030
2031/*
Al Viro6b255392015-11-17 10:20:54 -05002032 * Identical to proc_pid_link_inode_operations except for get_link()
Calvin Owensbdb4d102015-09-09 15:35:54 -07002033 */
2034static const struct inode_operations proc_map_files_link_inode_operations = {
2035 .readlink = proc_pid_readlink,
Al Viro6b255392015-11-17 10:20:54 -05002036 .get_link = proc_map_files_get_link,
Calvin Owensbdb4d102015-09-09 15:35:54 -07002037 .setattr = proc_setattr,
2038};
2039
Al Viroc52a47a2013-06-15 11:15:20 +04002040static int
Pavel Emelyanov640708a2012-01-10 15:11:23 -08002041proc_map_files_instantiate(struct inode *dir, struct dentry *dentry,
2042 struct task_struct *task, const void *ptr)
2043{
Al Viro7b540d02012-08-27 14:55:26 -04002044 fmode_t mode = (fmode_t)(unsigned long)ptr;
Pavel Emelyanov640708a2012-01-10 15:11:23 -08002045 struct proc_inode *ei;
2046 struct inode *inode;
2047
Pavel Emelyanov640708a2012-01-10 15:11:23 -08002048 inode = proc_pid_make_inode(dir->i_sb, task);
2049 if (!inode)
Al Viroc52a47a2013-06-15 11:15:20 +04002050 return -ENOENT;
Pavel Emelyanov640708a2012-01-10 15:11:23 -08002051
2052 ei = PROC_I(inode);
Al Viro6b255392015-11-17 10:20:54 -05002053 ei->op.proc_get_link = map_files_get_link;
Pavel Emelyanov640708a2012-01-10 15:11:23 -08002054
Calvin Owensbdb4d102015-09-09 15:35:54 -07002055 inode->i_op = &proc_map_files_link_inode_operations;
Pavel Emelyanov640708a2012-01-10 15:11:23 -08002056 inode->i_size = 64;
2057 inode->i_mode = S_IFLNK;
2058
Al Viro7b540d02012-08-27 14:55:26 -04002059 if (mode & FMODE_READ)
Pavel Emelyanov640708a2012-01-10 15:11:23 -08002060 inode->i_mode |= S_IRUSR;
Al Viro7b540d02012-08-27 14:55:26 -04002061 if (mode & FMODE_WRITE)
Pavel Emelyanov640708a2012-01-10 15:11:23 -08002062 inode->i_mode |= S_IWUSR;
2063
2064 d_set_d_op(dentry, &tid_map_files_dentry_operations);
2065 d_add(dentry, inode);
2066
Al Viroc52a47a2013-06-15 11:15:20 +04002067 return 0;
Pavel Emelyanov640708a2012-01-10 15:11:23 -08002068}
2069
2070static struct dentry *proc_map_files_lookup(struct inode *dir,
Al Viro00cd8dd2012-06-10 17:13:09 -04002071 struct dentry *dentry, unsigned int flags)
Pavel Emelyanov640708a2012-01-10 15:11:23 -08002072{
2073 unsigned long vm_start, vm_end;
2074 struct vm_area_struct *vma;
2075 struct task_struct *task;
Al Viroc52a47a2013-06-15 11:15:20 +04002076 int result;
Pavel Emelyanov640708a2012-01-10 15:11:23 -08002077 struct mm_struct *mm;
2078
Al Viroc52a47a2013-06-15 11:15:20 +04002079 result = -ENOENT;
Pavel Emelyanov640708a2012-01-10 15:11:23 -08002080 task = get_proc_task(dir);
2081 if (!task)
2082 goto out;
2083
Al Viroc52a47a2013-06-15 11:15:20 +04002084 result = -EACCES;
Jann Horncaaee622016-01-20 15:00:04 -08002085 if (!ptrace_may_access(task, PTRACE_MODE_READ_FSCREDS))
Pavel Emelyanov640708a2012-01-10 15:11:23 -08002086 goto out_put_task;
2087
Al Viroc52a47a2013-06-15 11:15:20 +04002088 result = -ENOENT;
Pavel Emelyanov640708a2012-01-10 15:11:23 -08002089 if (dname_to_vma_addr(dentry, &vm_start, &vm_end))
Cyrill Gorcunoveb94cd92012-05-17 17:03:25 -07002090 goto out_put_task;
Pavel Emelyanov640708a2012-01-10 15:11:23 -08002091
2092 mm = get_task_mm(task);
2093 if (!mm)
Cyrill Gorcunoveb94cd92012-05-17 17:03:25 -07002094 goto out_put_task;
Pavel Emelyanov640708a2012-01-10 15:11:23 -08002095
2096 down_read(&mm->mmap_sem);
2097 vma = find_exact_vma(mm, vm_start, vm_end);
2098 if (!vma)
2099 goto out_no_vma;
2100
Stanislav Kinsbursky05f56482012-11-26 16:29:42 -08002101 if (vma->vm_file)
2102 result = proc_map_files_instantiate(dir, dentry, task,
2103 (void *)(unsigned long)vma->vm_file->f_mode);
Pavel Emelyanov640708a2012-01-10 15:11:23 -08002104
2105out_no_vma:
2106 up_read(&mm->mmap_sem);
2107 mmput(mm);
Pavel Emelyanov640708a2012-01-10 15:11:23 -08002108out_put_task:
2109 put_task_struct(task);
2110out:
Al Viroc52a47a2013-06-15 11:15:20 +04002111 return ERR_PTR(result);
Pavel Emelyanov640708a2012-01-10 15:11:23 -08002112}
2113
2114static const struct inode_operations proc_map_files_inode_operations = {
2115 .lookup = proc_map_files_lookup,
2116 .permission = proc_fd_permission,
2117 .setattr = proc_setattr,
2118};
2119
2120static int
Al Virof0c3b502013-05-16 12:07:31 -04002121proc_map_files_readdir(struct file *file, struct dir_context *ctx)
Pavel Emelyanov640708a2012-01-10 15:11:23 -08002122{
Pavel Emelyanov640708a2012-01-10 15:11:23 -08002123 struct vm_area_struct *vma;
2124 struct task_struct *task;
2125 struct mm_struct *mm;
Al Virof0c3b502013-05-16 12:07:31 -04002126 unsigned long nr_files, pos, i;
2127 struct flex_array *fa = NULL;
2128 struct map_files_info info;
2129 struct map_files_info *p;
Pavel Emelyanov640708a2012-01-10 15:11:23 -08002130 int ret;
2131
Pavel Emelyanov640708a2012-01-10 15:11:23 -08002132 ret = -ENOENT;
Al Virof0c3b502013-05-16 12:07:31 -04002133 task = get_proc_task(file_inode(file));
Pavel Emelyanov640708a2012-01-10 15:11:23 -08002134 if (!task)
2135 goto out;
2136
2137 ret = -EACCES;
Jann Horncaaee622016-01-20 15:00:04 -08002138 if (!ptrace_may_access(task, PTRACE_MODE_READ_FSCREDS))
Pavel Emelyanov640708a2012-01-10 15:11:23 -08002139 goto out_put_task;
2140
2141 ret = 0;
Al Virof0c3b502013-05-16 12:07:31 -04002142 if (!dir_emit_dots(file, ctx))
2143 goto out_put_task;
Pavel Emelyanov640708a2012-01-10 15:11:23 -08002144
Al Virof0c3b502013-05-16 12:07:31 -04002145 mm = get_task_mm(task);
2146 if (!mm)
2147 goto out_put_task;
2148 down_read(&mm->mmap_sem);
Pavel Emelyanov640708a2012-01-10 15:11:23 -08002149
Al Virof0c3b502013-05-16 12:07:31 -04002150 nr_files = 0;
Pavel Emelyanov640708a2012-01-10 15:11:23 -08002151
Al Virof0c3b502013-05-16 12:07:31 -04002152 /*
2153 * We need two passes here:
2154 *
2155 * 1) Collect vmas of mapped files with mmap_sem taken
2156 * 2) Release mmap_sem and instantiate entries
2157 *
2158 * otherwise we get lockdep complained, since filldir()
2159 * routine might require mmap_sem taken in might_fault().
2160 */
Pavel Emelyanov640708a2012-01-10 15:11:23 -08002161
Al Virof0c3b502013-05-16 12:07:31 -04002162 for (vma = mm->mmap, pos = 2; vma; vma = vma->vm_next) {
2163 if (vma->vm_file && ++pos > ctx->pos)
2164 nr_files++;
Pavel Emelyanov640708a2012-01-10 15:11:23 -08002165 }
Al Virof0c3b502013-05-16 12:07:31 -04002166
2167 if (nr_files) {
2168 fa = flex_array_alloc(sizeof(info), nr_files,
2169 GFP_KERNEL);
2170 if (!fa || flex_array_prealloc(fa, 0, nr_files,
2171 GFP_KERNEL)) {
2172 ret = -ENOMEM;
2173 if (fa)
2174 flex_array_free(fa);
2175 up_read(&mm->mmap_sem);
2176 mmput(mm);
2177 goto out_put_task;
2178 }
2179 for (i = 0, vma = mm->mmap, pos = 2; vma;
2180 vma = vma->vm_next) {
2181 if (!vma->vm_file)
2182 continue;
2183 if (++pos <= ctx->pos)
2184 continue;
2185
2186 info.mode = vma->vm_file->f_mode;
2187 info.len = snprintf(info.name,
2188 sizeof(info.name), "%lx-%lx",
2189 vma->vm_start, vma->vm_end);
2190 if (flex_array_put(fa, i++, &info, GFP_KERNEL))
2191 BUG();
2192 }
Pavel Emelyanov640708a2012-01-10 15:11:23 -08002193 }
Al Virof0c3b502013-05-16 12:07:31 -04002194 up_read(&mm->mmap_sem);
2195
2196 for (i = 0; i < nr_files; i++) {
2197 p = flex_array_get(fa, i);
2198 if (!proc_fill_cache(file, ctx,
2199 p->name, p->len,
2200 proc_map_files_instantiate,
2201 task,
2202 (void *)(unsigned long)p->mode))
2203 break;
2204 ctx->pos++;
2205 }
2206 if (fa)
2207 flex_array_free(fa);
2208 mmput(mm);
Pavel Emelyanov640708a2012-01-10 15:11:23 -08002209
Pavel Emelyanov640708a2012-01-10 15:11:23 -08002210out_put_task:
2211 put_task_struct(task);
2212out:
2213 return ret;
2214}
2215
2216static const struct file_operations proc_map_files_operations = {
2217 .read = generic_read_dir,
Al Virof50752e2016-04-20 17:13:54 -04002218 .iterate_shared = proc_map_files_readdir,
2219 .llseek = generic_file_llseek,
Pavel Emelyanov640708a2012-01-10 15:11:23 -08002220};
2221
Eric Engestromb5946be2016-03-17 14:20:57 -07002222#ifdef CONFIG_CHECKPOINT_RESTORE
Pavel Emelyanov48f6a7a2013-03-11 13:12:45 +04002223struct timers_private {
2224 struct pid *pid;
2225 struct task_struct *task;
2226 struct sighand_struct *sighand;
Pavel Emelyanov57b80152013-03-11 13:13:08 +04002227 struct pid_namespace *ns;
Pavel Emelyanov48f6a7a2013-03-11 13:12:45 +04002228 unsigned long flags;
2229};
2230
2231static void *timers_start(struct seq_file *m, loff_t *pos)
2232{
2233 struct timers_private *tp = m->private;
2234
2235 tp->task = get_pid_task(tp->pid, PIDTYPE_PID);
2236 if (!tp->task)
2237 return ERR_PTR(-ESRCH);
2238
2239 tp->sighand = lock_task_sighand(tp->task, &tp->flags);
2240 if (!tp->sighand)
2241 return ERR_PTR(-ESRCH);
2242
2243 return seq_list_start(&tp->task->signal->posix_timers, *pos);
2244}
2245
2246static void *timers_next(struct seq_file *m, void *v, loff_t *pos)
2247{
2248 struct timers_private *tp = m->private;
2249 return seq_list_next(v, &tp->task->signal->posix_timers, pos);
2250}
2251
2252static void timers_stop(struct seq_file *m, void *v)
2253{
2254 struct timers_private *tp = m->private;
2255
2256 if (tp->sighand) {
2257 unlock_task_sighand(tp->task, &tp->flags);
2258 tp->sighand = NULL;
2259 }
2260
2261 if (tp->task) {
2262 put_task_struct(tp->task);
2263 tp->task = NULL;
2264 }
2265}
2266
2267static int show_timer(struct seq_file *m, void *v)
2268{
2269 struct k_itimer *timer;
Pavel Emelyanov57b80152013-03-11 13:13:08 +04002270 struct timers_private *tp = m->private;
2271 int notify;
Alexey Dobriyancedbcca2014-08-08 14:21:33 -07002272 static const char * const nstr[] = {
Pavel Emelyanov57b80152013-03-11 13:13:08 +04002273 [SIGEV_SIGNAL] = "signal",
2274 [SIGEV_NONE] = "none",
2275 [SIGEV_THREAD] = "thread",
2276 };
Pavel Emelyanov48f6a7a2013-03-11 13:12:45 +04002277
2278 timer = list_entry((struct list_head *)v, struct k_itimer, list);
Pavel Emelyanov57b80152013-03-11 13:13:08 +04002279 notify = timer->it_sigev_notify;
2280
Pavel Emelyanov48f6a7a2013-03-11 13:12:45 +04002281 seq_printf(m, "ID: %d\n", timer->it_id);
Joe Perches25ce3192015-04-15 16:18:17 -07002282 seq_printf(m, "signal: %d/%p\n",
2283 timer->sigq->info.si_signo,
2284 timer->sigq->info.si_value.sival_ptr);
Pavel Emelyanov57b80152013-03-11 13:13:08 +04002285 seq_printf(m, "notify: %s/%s.%d\n",
Joe Perches25ce3192015-04-15 16:18:17 -07002286 nstr[notify & ~SIGEV_THREAD_ID],
2287 (notify & SIGEV_THREAD_ID) ? "tid" : "pid",
2288 pid_nr_ns(timer->it_pid, tp->ns));
Pavel Tikhomirov15ef0292013-05-17 02:12:03 +04002289 seq_printf(m, "ClockID: %d\n", timer->it_clock);
Pavel Emelyanov48f6a7a2013-03-11 13:12:45 +04002290
2291 return 0;
2292}
2293
2294static const struct seq_operations proc_timers_seq_ops = {
2295 .start = timers_start,
2296 .next = timers_next,
2297 .stop = timers_stop,
2298 .show = show_timer,
2299};
2300
2301static int proc_timers_open(struct inode *inode, struct file *file)
2302{
2303 struct timers_private *tp;
2304
2305 tp = __seq_open_private(file, &proc_timers_seq_ops,
2306 sizeof(struct timers_private));
2307 if (!tp)
2308 return -ENOMEM;
2309
2310 tp->pid = proc_pid(inode);
Pavel Emelyanov57b80152013-03-11 13:13:08 +04002311 tp->ns = inode->i_sb->s_fs_info;
Pavel Emelyanov48f6a7a2013-03-11 13:12:45 +04002312 return 0;
2313}
2314
2315static const struct file_operations proc_timers_operations = {
2316 .open = proc_timers_open,
2317 .read = seq_read,
2318 .llseek = seq_lseek,
2319 .release = seq_release_private,
2320};
Eric Engestromb5946be2016-03-17 14:20:57 -07002321#endif
Pavel Emelyanov640708a2012-01-10 15:11:23 -08002322
John Stultz5de23d42016-03-17 14:20:54 -07002323static ssize_t timerslack_ns_write(struct file *file, const char __user *buf,
2324 size_t count, loff_t *offset)
2325{
2326 struct inode *inode = file_inode(file);
2327 struct task_struct *p;
2328 u64 slack_ns;
2329 int err;
2330
2331 err = kstrtoull_from_user(buf, count, 10, &slack_ns);
2332 if (err < 0)
2333 return err;
2334
2335 p = get_proc_task(inode);
2336 if (!p)
2337 return -ESRCH;
2338
John Stultz4b2bd5f2016-10-07 17:02:33 -07002339 if (p != current) {
2340 if (!capable(CAP_SYS_NICE)) {
2341 count = -EPERM;
2342 goto out;
2343 }
John Stultz5de23d42016-03-17 14:20:54 -07002344
John Stultz4b2bd5f2016-10-07 17:02:33 -07002345 err = security_task_setscheduler(p);
2346 if (err) {
2347 count = err;
2348 goto out;
2349 }
John Stultz904763e2016-10-07 17:02:29 -07002350 }
2351
John Stultz7abbaf92016-10-07 17:02:26 -07002352 task_lock(p);
2353 if (slack_ns == 0)
2354 p->timer_slack_ns = p->default_timer_slack_ns;
2355 else
2356 p->timer_slack_ns = slack_ns;
2357 task_unlock(p);
2358
2359out:
John Stultz5de23d42016-03-17 14:20:54 -07002360 put_task_struct(p);
2361
2362 return count;
2363}
2364
2365static int timerslack_ns_show(struct seq_file *m, void *v)
2366{
2367 struct inode *inode = m->private;
2368 struct task_struct *p;
John Stultz7abbaf92016-10-07 17:02:26 -07002369 int err = 0;
John Stultz5de23d42016-03-17 14:20:54 -07002370
2371 p = get_proc_task(inode);
2372 if (!p)
2373 return -ESRCH;
2374
John Stultz4b2bd5f2016-10-07 17:02:33 -07002375 if (p != current) {
John Stultz5de23d42016-03-17 14:20:54 -07002376
John Stultz4b2bd5f2016-10-07 17:02:33 -07002377 if (!capable(CAP_SYS_NICE)) {
2378 err = -EPERM;
2379 goto out;
2380 }
2381 err = security_task_getscheduler(p);
2382 if (err)
2383 goto out;
2384 }
John Stultz904763e2016-10-07 17:02:29 -07002385
John Stultz7abbaf92016-10-07 17:02:26 -07002386 task_lock(p);
2387 seq_printf(m, "%llu\n", p->timer_slack_ns);
2388 task_unlock(p);
2389
2390out:
John Stultz5de23d42016-03-17 14:20:54 -07002391 put_task_struct(p);
2392
2393 return err;
2394}
2395
2396static int timerslack_ns_open(struct inode *inode, struct file *filp)
2397{
2398 return single_open(filp, timerslack_ns_show, inode);
2399}
2400
2401static const struct file_operations proc_pid_set_timerslack_ns_operations = {
2402 .open = timerslack_ns_open,
2403 .read = seq_read,
2404 .write = timerslack_ns_write,
2405 .llseek = seq_lseek,
2406 .release = single_release,
2407};
2408
Al Viroc52a47a2013-06-15 11:15:20 +04002409static int proc_pident_instantiate(struct inode *dir,
Eric Dumazetc5141e62007-05-08 00:26:15 -07002410 struct dentry *dentry, struct task_struct *task, const void *ptr)
Eric W. Biederman444ceed2006-10-02 02:18:49 -07002411{
Eric Dumazetc5141e62007-05-08 00:26:15 -07002412 const struct pid_entry *p = ptr;
Eric W. Biederman444ceed2006-10-02 02:18:49 -07002413 struct inode *inode;
2414 struct proc_inode *ei;
Eric W. Biederman444ceed2006-10-02 02:18:49 -07002415
Eric W. Biederman61a28782006-10-02 02:18:49 -07002416 inode = proc_pid_make_inode(dir->i_sb, task);
Eric W. Biederman444ceed2006-10-02 02:18:49 -07002417 if (!inode)
2418 goto out;
2419
2420 ei = PROC_I(inode);
2421 inode->i_mode = p->mode;
2422 if (S_ISDIR(inode->i_mode))
Miklos Szeredibfe86842011-10-28 14:13:29 +02002423 set_nlink(inode, 2); /* Use getattr to fix if necessary */
Eric W. Biederman444ceed2006-10-02 02:18:49 -07002424 if (p->iop)
2425 inode->i_op = p->iop;
2426 if (p->fop)
2427 inode->i_fop = p->fop;
2428 ei->op = p->op;
Nick Pigginfb045ad2011-01-07 17:49:55 +11002429 d_set_d_op(dentry, &pid_dentry_operations);
Eric W. Biederman444ceed2006-10-02 02:18:49 -07002430 d_add(dentry, inode);
2431 /* Close the race of the process dying before we return the dentry */
Al Viro0b728e12012-06-10 16:03:43 -04002432 if (pid_revalidate(dentry, 0))
Al Viroc52a47a2013-06-15 11:15:20 +04002433 return 0;
Eric W. Biederman444ceed2006-10-02 02:18:49 -07002434out:
Al Viroc52a47a2013-06-15 11:15:20 +04002435 return -ENOENT;
Eric W. Biederman444ceed2006-10-02 02:18:49 -07002436}
2437
Linus Torvalds1da177e2005-04-16 15:20:36 -07002438static struct dentry *proc_pident_lookup(struct inode *dir,
2439 struct dentry *dentry,
Eric Dumazetc5141e62007-05-08 00:26:15 -07002440 const struct pid_entry *ents,
Eric W. Biederman7bcd6b02006-10-02 02:18:56 -07002441 unsigned int nents)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002442{
Al Viroc52a47a2013-06-15 11:15:20 +04002443 int error;
Eric W. Biederman99f89552006-06-26 00:25:55 -07002444 struct task_struct *task = get_proc_task(dir);
Eric Dumazetc5141e62007-05-08 00:26:15 -07002445 const struct pid_entry *p, *last;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002446
Al Viroc52a47a2013-06-15 11:15:20 +04002447 error = -ENOENT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002448
Eric W. Biederman99f89552006-06-26 00:25:55 -07002449 if (!task)
2450 goto out_no_task;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002451
Eric W. Biederman20cdc892006-10-02 02:17:07 -07002452 /*
2453 * Yes, it does not scale. And it should not. Don't add
2454 * new entries into /proc/<tgid>/ without very good reasons.
2455 */
Eric W. Biederman7bcd6b02006-10-02 02:18:56 -07002456 last = &ents[nents - 1];
2457 for (p = ents; p <= last; p++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002458 if (p->len != dentry->d_name.len)
2459 continue;
2460 if (!memcmp(dentry->d_name.name, p->name, p->len))
2461 break;
2462 }
Eric W. Biederman7bcd6b02006-10-02 02:18:56 -07002463 if (p > last)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002464 goto out;
2465
Eric W. Biederman444ceed2006-10-02 02:18:49 -07002466 error = proc_pident_instantiate(dir, dentry, task, p);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002467out:
Eric W. Biederman99f89552006-06-26 00:25:55 -07002468 put_task_struct(task);
2469out_no_task:
Al Viroc52a47a2013-06-15 11:15:20 +04002470 return ERR_PTR(error);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002471}
2472
Al Virof0c3b502013-05-16 12:07:31 -04002473static int proc_pident_readdir(struct file *file, struct dir_context *ctx,
Eric Dumazetc5141e62007-05-08 00:26:15 -07002474 const struct pid_entry *ents, unsigned int nents)
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002475{
Al Virof0c3b502013-05-16 12:07:31 -04002476 struct task_struct *task = get_proc_task(file_inode(file));
2477 const struct pid_entry *p;
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002478
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002479 if (!task)
Al Virof0c3b502013-05-16 12:07:31 -04002480 return -ENOENT;
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002481
Al Virof0c3b502013-05-16 12:07:31 -04002482 if (!dir_emit_dots(file, ctx))
2483 goto out;
2484
2485 if (ctx->pos >= nents + 2)
2486 goto out;
2487
2488 for (p = ents + (ctx->pos - 2); p <= ents + nents - 1; p++) {
2489 if (!proc_fill_cache(file, ctx, p->name, p->len,
2490 proc_pident_instantiate, task, p))
2491 break;
2492 ctx->pos++;
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002493 }
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002494out:
Eric W. Biederman61a28782006-10-02 02:18:49 -07002495 put_task_struct(task);
Al Virof0c3b502013-05-16 12:07:31 -04002496 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002497}
2498
Linus Torvalds1da177e2005-04-16 15:20:36 -07002499#ifdef CONFIG_SECURITY
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002500static ssize_t proc_pid_attr_read(struct file * file, char __user * buf,
2501 size_t count, loff_t *ppos)
2502{
Al Viro496ad9a2013-01-23 17:07:38 -05002503 struct inode * inode = file_inode(file);
Al Viro04ff9702007-03-12 16:17:58 +00002504 char *p = NULL;
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002505 ssize_t length;
2506 struct task_struct *task = get_proc_task(inode);
2507
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002508 if (!task)
Al Viro04ff9702007-03-12 16:17:58 +00002509 return -ESRCH;
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002510
2511 length = security_getprocattr(task,
Josef "Jeff" Sipek2fddfee2006-12-08 02:36:36 -08002512 (char*)file->f_path.dentry->d_name.name,
Al Viro04ff9702007-03-12 16:17:58 +00002513 &p);
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002514 put_task_struct(task);
Al Viro04ff9702007-03-12 16:17:58 +00002515 if (length > 0)
2516 length = simple_read_from_buffer(buf, count, ppos, p, length);
2517 kfree(p);
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002518 return length;
2519}
2520
2521static ssize_t proc_pid_attr_write(struct file * file, const char __user * buf,
2522 size_t count, loff_t *ppos)
2523{
Al Viro496ad9a2013-01-23 17:07:38 -05002524 struct inode * inode = file_inode(file);
Al Virobb646cd2015-12-24 00:16:30 -05002525 void *page;
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002526 ssize_t length;
2527 struct task_struct *task = get_proc_task(inode);
2528
2529 length = -ESRCH;
2530 if (!task)
2531 goto out_no_task;
2532 if (count > PAGE_SIZE)
2533 count = PAGE_SIZE;
2534
2535 /* No partial writes. */
2536 length = -EINVAL;
2537 if (*ppos != 0)
2538 goto out;
2539
Al Virobb646cd2015-12-24 00:16:30 -05002540 page = memdup_user(buf, count);
2541 if (IS_ERR(page)) {
2542 length = PTR_ERR(page);
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002543 goto out;
Al Virobb646cd2015-12-24 00:16:30 -05002544 }
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002545
David Howells107db7c2009-05-08 13:55:27 +01002546 /* Guard against adverse ptrace interaction */
KOSAKI Motohiro9b1bf122010-10-27 15:34:08 -07002547 length = mutex_lock_interruptible(&task->signal->cred_guard_mutex);
David Howells107db7c2009-05-08 13:55:27 +01002548 if (length < 0)
2549 goto out_free;
2550
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002551 length = security_setprocattr(task,
Josef "Jeff" Sipek2fddfee2006-12-08 02:36:36 -08002552 (char*)file->f_path.dentry->d_name.name,
Al Virobb646cd2015-12-24 00:16:30 -05002553 page, count);
KOSAKI Motohiro9b1bf122010-10-27 15:34:08 -07002554 mutex_unlock(&task->signal->cred_guard_mutex);
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002555out_free:
Al Virobb646cd2015-12-24 00:16:30 -05002556 kfree(page);
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002557out:
2558 put_task_struct(task);
2559out_no_task:
2560 return length;
2561}
2562
Arjan van de Ven00977a52007-02-12 00:55:34 -08002563static const struct file_operations proc_pid_attr_operations = {
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002564 .read = proc_pid_attr_read,
2565 .write = proc_pid_attr_write,
Arnd Bergmann87df8422010-03-17 23:06:02 +01002566 .llseek = generic_file_llseek,
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002567};
2568
Eric Dumazetc5141e62007-05-08 00:26:15 -07002569static const struct pid_entry attr_dir_stuff[] = {
Alexey Dobriyan631f9c12008-11-10 01:32:52 +03002570 REG("current", S_IRUGO|S_IWUGO, proc_pid_attr_operations),
2571 REG("prev", S_IRUGO, proc_pid_attr_operations),
2572 REG("exec", S_IRUGO|S_IWUGO, proc_pid_attr_operations),
2573 REG("fscreate", S_IRUGO|S_IWUGO, proc_pid_attr_operations),
2574 REG("keycreate", S_IRUGO|S_IWUGO, proc_pid_attr_operations),
2575 REG("sockcreate", S_IRUGO|S_IWUGO, proc_pid_attr_operations),
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002576};
2577
Al Virof0c3b502013-05-16 12:07:31 -04002578static int proc_attr_dir_readdir(struct file *file, struct dir_context *ctx)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002579{
Al Virof0c3b502013-05-16 12:07:31 -04002580 return proc_pident_readdir(file, ctx,
2581 attr_dir_stuff, ARRAY_SIZE(attr_dir_stuff));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002582}
2583
Arjan van de Ven00977a52007-02-12 00:55:34 -08002584static const struct file_operations proc_attr_dir_operations = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002585 .read = generic_read_dir,
Al Virof50752e2016-04-20 17:13:54 -04002586 .iterate_shared = proc_attr_dir_readdir,
2587 .llseek = generic_file_llseek,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002588};
2589
Eric W. Biederman72d9dcf2006-10-02 02:18:50 -07002590static struct dentry *proc_attr_dir_lookup(struct inode *dir,
Al Viro00cd8dd2012-06-10 17:13:09 -04002591 struct dentry *dentry, unsigned int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002592{
Eric W. Biederman7bcd6b02006-10-02 02:18:56 -07002593 return proc_pident_lookup(dir, dentry,
2594 attr_dir_stuff, ARRAY_SIZE(attr_dir_stuff));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002595}
2596
Arjan van de Venc5ef1c42007-02-12 00:55:40 -08002597static const struct inode_operations proc_attr_dir_inode_operations = {
Eric W. Biederman72d9dcf2006-10-02 02:18:50 -07002598 .lookup = proc_attr_dir_lookup,
Eric W. Biederman99f89552006-06-26 00:25:55 -07002599 .getattr = pid_getattr,
Linus Torvalds6d76fa52006-07-15 12:26:45 -07002600 .setattr = proc_setattr,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002601};
2602
Linus Torvalds1da177e2005-04-16 15:20:36 -07002603#endif
2604
Christoph Hellwig698ba7b2009-12-15 16:47:37 -08002605#ifdef CONFIG_ELF_CORE
Kawai, Hidehiro3cb4a0b2007-07-19 01:48:28 -07002606static ssize_t proc_coredump_filter_read(struct file *file, char __user *buf,
2607 size_t count, loff_t *ppos)
2608{
Al Viro496ad9a2013-01-23 17:07:38 -05002609 struct task_struct *task = get_proc_task(file_inode(file));
Kawai, Hidehiro3cb4a0b2007-07-19 01:48:28 -07002610 struct mm_struct *mm;
2611 char buffer[PROC_NUMBUF];
2612 size_t len;
2613 int ret;
2614
2615 if (!task)
2616 return -ESRCH;
2617
2618 ret = 0;
2619 mm = get_task_mm(task);
2620 if (mm) {
2621 len = snprintf(buffer, sizeof(buffer), "%08lx\n",
2622 ((mm->flags & MMF_DUMP_FILTER_MASK) >>
2623 MMF_DUMP_FILTER_SHIFT));
2624 mmput(mm);
2625 ret = simple_read_from_buffer(buf, count, ppos, buffer, len);
2626 }
2627
2628 put_task_struct(task);
2629
2630 return ret;
2631}
2632
2633static ssize_t proc_coredump_filter_write(struct file *file,
2634 const char __user *buf,
2635 size_t count,
2636 loff_t *ppos)
2637{
2638 struct task_struct *task;
2639 struct mm_struct *mm;
Kawai, Hidehiro3cb4a0b2007-07-19 01:48:28 -07002640 unsigned int val;
2641 int ret;
2642 int i;
2643 unsigned long mask;
2644
Alexey Dobriyan774636e2015-09-09 15:36:59 -07002645 ret = kstrtouint_from_user(buf, count, 0, &val);
2646 if (ret < 0)
2647 return ret;
Kawai, Hidehiro3cb4a0b2007-07-19 01:48:28 -07002648
2649 ret = -ESRCH;
Al Viro496ad9a2013-01-23 17:07:38 -05002650 task = get_proc_task(file_inode(file));
Kawai, Hidehiro3cb4a0b2007-07-19 01:48:28 -07002651 if (!task)
2652 goto out_no_task;
2653
Kawai, Hidehiro3cb4a0b2007-07-19 01:48:28 -07002654 mm = get_task_mm(task);
2655 if (!mm)
2656 goto out_no_mm;
Colin Ian King41a0c2492015-12-18 14:22:01 -08002657 ret = 0;
Kawai, Hidehiro3cb4a0b2007-07-19 01:48:28 -07002658
2659 for (i = 0, mask = 1; i < MMF_DUMP_FILTER_BITS; i++, mask <<= 1) {
2660 if (val & mask)
2661 set_bit(i + MMF_DUMP_FILTER_SHIFT, &mm->flags);
2662 else
2663 clear_bit(i + MMF_DUMP_FILTER_SHIFT, &mm->flags);
2664 }
2665
2666 mmput(mm);
2667 out_no_mm:
2668 put_task_struct(task);
2669 out_no_task:
Alexey Dobriyan774636e2015-09-09 15:36:59 -07002670 if (ret < 0)
2671 return ret;
2672 return count;
Kawai, Hidehiro3cb4a0b2007-07-19 01:48:28 -07002673}
2674
2675static const struct file_operations proc_coredump_filter_operations = {
2676 .read = proc_coredump_filter_read,
2677 .write = proc_coredump_filter_write,
Arnd Bergmann87df8422010-03-17 23:06:02 +01002678 .llseek = generic_file_llseek,
Kawai, Hidehiro3cb4a0b2007-07-19 01:48:28 -07002679};
2680#endif
2681
Andrew Mortonaba76fd2006-12-10 02:19:48 -08002682#ifdef CONFIG_TASK_IO_ACCOUNTING
Alexey Dobriyan19aadc92014-08-08 14:21:50 -07002683static int do_io_accounting(struct task_struct *task, struct seq_file *m, int whole)
Andrew Mortonaba76fd2006-12-10 02:19:48 -08002684{
Andrea Righi940389b2008-07-28 00:48:12 +02002685 struct task_io_accounting acct = task->ioac;
Andrea Righi59954772008-07-27 17:29:15 +02002686 unsigned long flags;
Vasiliy Kulikov293eb1e2011-07-26 16:08:38 -07002687 int result;
Andrea Righi297c5d92008-07-25 01:48:49 -07002688
Vasiliy Kulikov293eb1e2011-07-26 16:08:38 -07002689 result = mutex_lock_killable(&task->signal->cred_guard_mutex);
2690 if (result)
2691 return result;
2692
Jann Horncaaee622016-01-20 15:00:04 -08002693 if (!ptrace_may_access(task, PTRACE_MODE_READ_FSCREDS)) {
Vasiliy Kulikov293eb1e2011-07-26 16:08:38 -07002694 result = -EACCES;
2695 goto out_unlock;
2696 }
Vasiliy Kulikov1d1221f2011-06-24 16:08:38 +04002697
Andrea Righi59954772008-07-27 17:29:15 +02002698 if (whole && lock_task_sighand(task, &flags)) {
2699 struct task_struct *t = task;
Andrea Righib2d002d2008-07-26 15:22:27 -07002700
Andrea Righi59954772008-07-27 17:29:15 +02002701 task_io_accounting_add(&acct, &task->signal->ioac);
2702 while_each_thread(task, t)
2703 task_io_accounting_add(&acct, &t->ioac);
Andrea Righi297c5d92008-07-25 01:48:49 -07002704
Andrea Righi59954772008-07-27 17:29:15 +02002705 unlock_task_sighand(task, &flags);
Andrea Righi297c5d92008-07-25 01:48:49 -07002706 }
Joe Perches25ce3192015-04-15 16:18:17 -07002707 seq_printf(m,
2708 "rchar: %llu\n"
2709 "wchar: %llu\n"
2710 "syscr: %llu\n"
2711 "syscw: %llu\n"
2712 "read_bytes: %llu\n"
2713 "write_bytes: %llu\n"
2714 "cancelled_write_bytes: %llu\n",
2715 (unsigned long long)acct.rchar,
2716 (unsigned long long)acct.wchar,
2717 (unsigned long long)acct.syscr,
2718 (unsigned long long)acct.syscw,
2719 (unsigned long long)acct.read_bytes,
2720 (unsigned long long)acct.write_bytes,
2721 (unsigned long long)acct.cancelled_write_bytes);
2722 result = 0;
2723
Vasiliy Kulikov293eb1e2011-07-26 16:08:38 -07002724out_unlock:
2725 mutex_unlock(&task->signal->cred_guard_mutex);
2726 return result;
Andrew Mortonaba76fd2006-12-10 02:19:48 -08002727}
Andrea Righi297c5d92008-07-25 01:48:49 -07002728
Alexey Dobriyan19aadc92014-08-08 14:21:50 -07002729static int proc_tid_io_accounting(struct seq_file *m, struct pid_namespace *ns,
2730 struct pid *pid, struct task_struct *task)
Andrea Righi297c5d92008-07-25 01:48:49 -07002731{
Alexey Dobriyan19aadc92014-08-08 14:21:50 -07002732 return do_io_accounting(task, m, 0);
Andrea Righi297c5d92008-07-25 01:48:49 -07002733}
2734
Alexey Dobriyan19aadc92014-08-08 14:21:50 -07002735static int proc_tgid_io_accounting(struct seq_file *m, struct pid_namespace *ns,
2736 struct pid *pid, struct task_struct *task)
Andrea Righi297c5d92008-07-25 01:48:49 -07002737{
Alexey Dobriyan19aadc92014-08-08 14:21:50 -07002738 return do_io_accounting(task, m, 1);
Andrea Righi297c5d92008-07-25 01:48:49 -07002739}
2740#endif /* CONFIG_TASK_IO_ACCOUNTING */
Andrew Mortonaba76fd2006-12-10 02:19:48 -08002741
Eric W. Biederman22d917d2011-11-17 00:11:58 -08002742#ifdef CONFIG_USER_NS
2743static int proc_id_map_open(struct inode *inode, struct file *file,
Fabian Frederickccf94f12014-08-08 14:21:22 -07002744 const struct seq_operations *seq_ops)
Eric W. Biederman22d917d2011-11-17 00:11:58 -08002745{
2746 struct user_namespace *ns = NULL;
2747 struct task_struct *task;
2748 struct seq_file *seq;
2749 int ret = -EINVAL;
2750
2751 task = get_proc_task(inode);
2752 if (task) {
2753 rcu_read_lock();
2754 ns = get_user_ns(task_cred_xxx(task, user_ns));
2755 rcu_read_unlock();
2756 put_task_struct(task);
2757 }
2758 if (!ns)
2759 goto err;
2760
2761 ret = seq_open(file, seq_ops);
2762 if (ret)
2763 goto err_put_ns;
2764
2765 seq = file->private_data;
2766 seq->private = ns;
2767
2768 return 0;
2769err_put_ns:
2770 put_user_ns(ns);
2771err:
2772 return ret;
2773}
2774
2775static int proc_id_map_release(struct inode *inode, struct file *file)
2776{
2777 struct seq_file *seq = file->private_data;
2778 struct user_namespace *ns = seq->private;
2779 put_user_ns(ns);
2780 return seq_release(inode, file);
2781}
2782
2783static int proc_uid_map_open(struct inode *inode, struct file *file)
2784{
2785 return proc_id_map_open(inode, file, &proc_uid_seq_operations);
2786}
2787
2788static int proc_gid_map_open(struct inode *inode, struct file *file)
2789{
2790 return proc_id_map_open(inode, file, &proc_gid_seq_operations);
2791}
2792
Eric W. Biedermanf76d2072012-08-30 01:24:05 -07002793static int proc_projid_map_open(struct inode *inode, struct file *file)
2794{
2795 return proc_id_map_open(inode, file, &proc_projid_seq_operations);
2796}
2797
Eric W. Biederman22d917d2011-11-17 00:11:58 -08002798static const struct file_operations proc_uid_map_operations = {
2799 .open = proc_uid_map_open,
2800 .write = proc_uid_map_write,
2801 .read = seq_read,
2802 .llseek = seq_lseek,
2803 .release = proc_id_map_release,
2804};
2805
2806static const struct file_operations proc_gid_map_operations = {
2807 .open = proc_gid_map_open,
2808 .write = proc_gid_map_write,
2809 .read = seq_read,
2810 .llseek = seq_lseek,
2811 .release = proc_id_map_release,
2812};
Eric W. Biedermanf76d2072012-08-30 01:24:05 -07002813
2814static const struct file_operations proc_projid_map_operations = {
2815 .open = proc_projid_map_open,
2816 .write = proc_projid_map_write,
2817 .read = seq_read,
2818 .llseek = seq_lseek,
2819 .release = proc_id_map_release,
2820};
Eric W. Biederman9cc46512014-12-02 12:27:26 -06002821
2822static int proc_setgroups_open(struct inode *inode, struct file *file)
2823{
2824 struct user_namespace *ns = NULL;
2825 struct task_struct *task;
2826 int ret;
2827
2828 ret = -ESRCH;
2829 task = get_proc_task(inode);
2830 if (task) {
2831 rcu_read_lock();
2832 ns = get_user_ns(task_cred_xxx(task, user_ns));
2833 rcu_read_unlock();
2834 put_task_struct(task);
2835 }
2836 if (!ns)
2837 goto err;
2838
2839 if (file->f_mode & FMODE_WRITE) {
2840 ret = -EACCES;
2841 if (!ns_capable(ns, CAP_SYS_ADMIN))
2842 goto err_put_ns;
2843 }
2844
2845 ret = single_open(file, &proc_setgroups_show, ns);
2846 if (ret)
2847 goto err_put_ns;
2848
2849 return 0;
2850err_put_ns:
2851 put_user_ns(ns);
2852err:
2853 return ret;
2854}
2855
2856static int proc_setgroups_release(struct inode *inode, struct file *file)
2857{
2858 struct seq_file *seq = file->private_data;
2859 struct user_namespace *ns = seq->private;
2860 int ret = single_release(inode, file);
2861 put_user_ns(ns);
2862 return ret;
2863}
2864
2865static const struct file_operations proc_setgroups_operations = {
2866 .open = proc_setgroups_open,
2867 .write = proc_setgroups_write,
2868 .read = seq_read,
2869 .llseek = seq_lseek,
2870 .release = proc_setgroups_release,
2871};
Eric W. Biederman22d917d2011-11-17 00:11:58 -08002872#endif /* CONFIG_USER_NS */
2873
Kees Cook47830722008-10-06 03:11:58 +04002874static int proc_pid_personality(struct seq_file *m, struct pid_namespace *ns,
2875 struct pid *pid, struct task_struct *task)
2876{
Al Viroa9712bc2011-03-23 15:52:50 -04002877 int err = lock_trace(task);
2878 if (!err) {
2879 seq_printf(m, "%08x\n", task->personality);
2880 unlock_trace(task);
2881 }
2882 return err;
Kees Cook47830722008-10-06 03:11:58 +04002883}
2884
Eric W. Biederman801199c2006-10-02 02:18:48 -07002885/*
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002886 * Thread groups
2887 */
Arjan van de Ven00977a52007-02-12 00:55:34 -08002888static const struct file_operations proc_task_operations;
Arjan van de Venc5ef1c42007-02-12 00:55:40 -08002889static const struct inode_operations proc_task_inode_operations;
Eric W. Biederman20cdc892006-10-02 02:17:07 -07002890
Eric Dumazetc5141e62007-05-08 00:26:15 -07002891static const struct pid_entry tgid_base_stuff[] = {
Alexey Dobriyan631f9c12008-11-10 01:32:52 +03002892 DIR("task", S_IRUGO|S_IXUGO, proc_task_inode_operations, proc_task_operations),
2893 DIR("fd", S_IRUSR|S_IXUSR, proc_fd_inode_operations, proc_fd_operations),
Pavel Emelyanov640708a2012-01-10 15:11:23 -08002894 DIR("map_files", S_IRUSR|S_IXUSR, proc_map_files_inode_operations, proc_map_files_operations),
Alexey Dobriyan631f9c12008-11-10 01:32:52 +03002895 DIR("fdinfo", S_IRUSR|S_IXUSR, proc_fdinfo_inode_operations, proc_fdinfo_operations),
Eric W. Biederman6b4e3062010-03-07 16:41:34 -08002896 DIR("ns", S_IRUSR|S_IXUGO, proc_ns_dir_inode_operations, proc_ns_dir_operations),
Andrew Mortonb2211a32008-03-11 18:03:35 -07002897#ifdef CONFIG_NET
Alexey Dobriyan631f9c12008-11-10 01:32:52 +03002898 DIR("net", S_IRUGO|S_IXUGO, proc_net_inode_operations, proc_net_operations),
Andrew Mortonb2211a32008-03-11 18:03:35 -07002899#endif
Alexey Dobriyan631f9c12008-11-10 01:32:52 +03002900 REG("environ", S_IRUSR, proc_environ_operations),
Al Viroc5317162016-10-05 18:43:43 -04002901 REG("auxv", S_IRUSR, proc_auxv_operations),
Alexey Dobriyan631f9c12008-11-10 01:32:52 +03002902 ONE("status", S_IRUGO, proc_pid_status),
Djalal Harouni35a35042014-04-07 15:38:36 -07002903 ONE("personality", S_IRUSR, proc_pid_personality),
Alexey Dobriyan1c963eb2014-08-08 14:21:37 -07002904 ONE("limits", S_IRUGO, proc_pid_limits),
Ingo Molnar43ae34c2007-07-09 18:52:00 +02002905#ifdef CONFIG_SCHED_DEBUG
Alexey Dobriyan631f9c12008-11-10 01:32:52 +03002906 REG("sched", S_IRUGO|S_IWUSR, proc_pid_sched_operations),
Ingo Molnar43ae34c2007-07-09 18:52:00 +02002907#endif
Mike Galbraith5091faa2010-11-30 14:18:03 +01002908#ifdef CONFIG_SCHED_AUTOGROUP
2909 REG("autogroup", S_IRUGO|S_IWUSR, proc_pid_sched_autogroup_operations),
2910#endif
john stultz4614a696b2009-12-14 18:00:05 -08002911 REG("comm", S_IRUGO|S_IWUSR, proc_pid_set_comm_operations),
Roland McGrathebcb6732008-07-25 19:46:00 -07002912#ifdef CONFIG_HAVE_ARCH_TRACEHOOK
Alexey Dobriyan09d93bd2014-08-08 14:21:39 -07002913 ONE("syscall", S_IRUSR, proc_pid_syscall),
Roland McGrathebcb6732008-07-25 19:46:00 -07002914#endif
Alexey Dobriyanc2c0bb42015-06-25 15:00:54 -07002915 REG("cmdline", S_IRUGO, proc_pid_cmdline_ops),
Alexey Dobriyan631f9c12008-11-10 01:32:52 +03002916 ONE("stat", S_IRUGO, proc_tgid_stat),
2917 ONE("statm", S_IRUGO, proc_pid_statm),
Siddhesh Poyarekarb7643752012-03-21 16:34:04 -07002918 REG("maps", S_IRUGO, proc_pid_maps_operations),
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002919#ifdef CONFIG_NUMA
Siddhesh Poyarekarb7643752012-03-21 16:34:04 -07002920 REG("numa_maps", S_IRUGO, proc_pid_numa_maps_operations),
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002921#endif
Alexey Dobriyan631f9c12008-11-10 01:32:52 +03002922 REG("mem", S_IRUSR|S_IWUSR, proc_mem_operations),
2923 LNK("cwd", proc_cwd_link),
2924 LNK("root", proc_root_link),
2925 LNK("exe", proc_exe_link),
2926 REG("mounts", S_IRUGO, proc_mounts_operations),
2927 REG("mountinfo", S_IRUGO, proc_mountinfo_operations),
2928 REG("mountstats", S_IRUSR, proc_mountstats_operations),
Matt Mackall1e883282008-02-04 22:29:07 -08002929#ifdef CONFIG_PROC_PAGE_MONITOR
Alexey Dobriyan631f9c12008-11-10 01:32:52 +03002930 REG("clear_refs", S_IWUSR, proc_clear_refs_operations),
Siddhesh Poyarekarb7643752012-03-21 16:34:04 -07002931 REG("smaps", S_IRUGO, proc_pid_smaps_operations),
Djalal Harouni32ed74a2014-04-07 15:38:38 -07002932 REG("pagemap", S_IRUSR, proc_pagemap_operations),
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002933#endif
2934#ifdef CONFIG_SECURITY
Alexey Dobriyan631f9c12008-11-10 01:32:52 +03002935 DIR("attr", S_IRUGO|S_IXUGO, proc_attr_dir_inode_operations, proc_attr_dir_operations),
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002936#endif
2937#ifdef CONFIG_KALLSYMS
Alexey Dobriyanedfcd602014-08-08 14:21:44 -07002938 ONE("wchan", S_IRUGO, proc_pid_wchan),
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002939#endif
Ken Chen2ec220e2008-11-10 11:26:08 +03002940#ifdef CONFIG_STACKTRACE
Djalal Harouni35a35042014-04-07 15:38:36 -07002941 ONE("stack", S_IRUSR, proc_pid_stack),
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002942#endif
Naveen N. Rao5968cec2015-06-30 14:36:03 +05302943#ifdef CONFIG_SCHED_INFO
Alexey Dobriyanf6e826c2014-08-08 14:21:46 -07002944 ONE("schedstat", S_IRUGO, proc_pid_schedstat),
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002945#endif
Arjan van de Ven97455122008-01-25 21:08:34 +01002946#ifdef CONFIG_LATENCYTOP
Alexey Dobriyan631f9c12008-11-10 01:32:52 +03002947 REG("latency", S_IRUGO, proc_lstats_operations),
Arjan van de Ven97455122008-01-25 21:08:34 +01002948#endif
Paul Menage8793d852007-10-18 23:39:39 -07002949#ifdef CONFIG_PROC_PID_CPUSET
Zefan Li52de4772014-09-18 16:03:36 +08002950 ONE("cpuset", S_IRUGO, proc_cpuset_show),
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002951#endif
Paul Menagea4243162007-10-18 23:39:35 -07002952#ifdef CONFIG_CGROUPS
Zefan Li006f4ac2014-09-18 16:03:15 +08002953 ONE("cgroup", S_IRUGO, proc_cgroup_show),
Paul Menagea4243162007-10-18 23:39:35 -07002954#endif
Alexey Dobriyan6ba51e32014-08-08 14:21:48 -07002955 ONE("oom_score", S_IRUGO, proc_oom_score),
David Rientjesfa0cbbf2012-11-12 17:53:04 -08002956 REG("oom_adj", S_IRUGO|S_IWUSR, proc_oom_adj_operations),
David Rientjesa63d83f2010-08-09 17:19:46 -07002957 REG("oom_score_adj", S_IRUGO|S_IWUSR, proc_oom_score_adj_operations),
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002958#ifdef CONFIG_AUDITSYSCALL
Alexey Dobriyan631f9c12008-11-10 01:32:52 +03002959 REG("loginuid", S_IWUSR|S_IRUGO, proc_loginuid_operations),
2960 REG("sessionid", S_IRUGO, proc_sessionid_operations),
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002961#endif
Akinobu Mitaf4f154f2006-12-08 02:39:47 -08002962#ifdef CONFIG_FAULT_INJECTION
Alexey Dobriyan631f9c12008-11-10 01:32:52 +03002963 REG("make-it-fail", S_IRUGO|S_IWUSR, proc_fault_inject_operations),
Akinobu Mitaf4f154f2006-12-08 02:39:47 -08002964#endif
Christoph Hellwig698ba7b2009-12-15 16:47:37 -08002965#ifdef CONFIG_ELF_CORE
Alexey Dobriyan631f9c12008-11-10 01:32:52 +03002966 REG("coredump_filter", S_IRUGO|S_IWUSR, proc_coredump_filter_operations),
Kawai, Hidehiro3cb4a0b2007-07-19 01:48:28 -07002967#endif
Andrew Mortonaba76fd2006-12-10 02:19:48 -08002968#ifdef CONFIG_TASK_IO_ACCOUNTING
Alexey Dobriyan19aadc92014-08-08 14:21:50 -07002969 ONE("io", S_IRUSR, proc_tgid_io_accounting),
Andrew Mortonaba76fd2006-12-10 02:19:48 -08002970#endif
Chris Metcalff133ecc2011-05-26 12:40:09 -04002971#ifdef CONFIG_HARDWALL
Alexey Dobriyand962c142014-08-08 14:21:52 -07002972 ONE("hardwall", S_IRUGO, proc_pid_hardwall),
Chris Metcalff133ecc2011-05-26 12:40:09 -04002973#endif
Eric W. Biederman22d917d2011-11-17 00:11:58 -08002974#ifdef CONFIG_USER_NS
2975 REG("uid_map", S_IRUGO|S_IWUSR, proc_uid_map_operations),
2976 REG("gid_map", S_IRUGO|S_IWUSR, proc_gid_map_operations),
Eric W. Biedermanf76d2072012-08-30 01:24:05 -07002977 REG("projid_map", S_IRUGO|S_IWUSR, proc_projid_map_operations),
Eric W. Biederman9cc46512014-12-02 12:27:26 -06002978 REG("setgroups", S_IRUGO|S_IWUSR, proc_setgroups_operations),
Eric W. Biederman22d917d2011-11-17 00:11:58 -08002979#endif
Pavel Emelyanov48f6a7a2013-03-11 13:12:45 +04002980#ifdef CONFIG_CHECKPOINT_RESTORE
2981 REG("timers", S_IRUGO, proc_timers_operations),
2982#endif
John Stultz5de23d42016-03-17 14:20:54 -07002983 REG("timerslack_ns", S_IRUGO|S_IWUGO, proc_pid_set_timerslack_ns_operations),
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002984};
2985
Al Virof0c3b502013-05-16 12:07:31 -04002986static int proc_tgid_base_readdir(struct file *file, struct dir_context *ctx)
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002987{
Al Virof0c3b502013-05-16 12:07:31 -04002988 return proc_pident_readdir(file, ctx,
2989 tgid_base_stuff, ARRAY_SIZE(tgid_base_stuff));
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002990}
2991
Arjan van de Ven00977a52007-02-12 00:55:34 -08002992static const struct file_operations proc_tgid_base_operations = {
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002993 .read = generic_read_dir,
Al Virof50752e2016-04-20 17:13:54 -04002994 .iterate_shared = proc_tgid_base_readdir,
2995 .llseek = generic_file_llseek,
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002996};
2997
Al Viro00cd8dd2012-06-10 17:13:09 -04002998static struct dentry *proc_tgid_base_lookup(struct inode *dir, struct dentry *dentry, unsigned int flags)
2999{
Eric W. Biederman7bcd6b02006-10-02 02:18:56 -07003000 return proc_pident_lookup(dir, dentry,
3001 tgid_base_stuff, ARRAY_SIZE(tgid_base_stuff));
Eric W. Biederman28a6d672006-10-02 02:17:05 -07003002}
3003
Arjan van de Venc5ef1c42007-02-12 00:55:40 -08003004static const struct inode_operations proc_tgid_base_inode_operations = {
Eric W. Biederman28a6d672006-10-02 02:17:05 -07003005 .lookup = proc_tgid_base_lookup,
3006 .getattr = pid_getattr,
3007 .setattr = proc_setattr,
Vasiliy Kulikov04996802012-01-10 15:11:31 -08003008 .permission = proc_pid_permission,
Eric W. Biederman28a6d672006-10-02 02:17:05 -07003009};
3010
Pavel Emelyanov60347f62007-10-18 23:40:03 -07003011static void proc_flush_task_mnt(struct vfsmount *mnt, pid_t pid, pid_t tgid)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003012{
Eric W. Biederman48e64842006-06-26 00:25:48 -07003013 struct dentry *dentry, *leader, *dir;
Eric W. Biederman8578cea2006-06-26 00:25:54 -07003014 char buf[PROC_NUMBUF];
Eric W. Biederman48e64842006-06-26 00:25:48 -07003015 struct qstr name;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003016
Eric W. Biederman48e64842006-06-26 00:25:48 -07003017 name.name = buf;
Pavel Emelyanov60347f62007-10-18 23:40:03 -07003018 name.len = snprintf(buf, sizeof(buf), "%d", pid);
Al Viro4f522a22013-02-11 23:20:37 -05003019 /* no ->d_hash() rejects on procfs */
Pavel Emelyanov60347f62007-10-18 23:40:03 -07003020 dentry = d_hash_and_lookup(mnt->mnt_root, &name);
Eric W. Biederman48e64842006-06-26 00:25:48 -07003021 if (dentry) {
Eric W. Biedermanbbd51922014-02-13 10:24:23 -08003022 d_invalidate(dentry);
Eric W. Biederman48e64842006-06-26 00:25:48 -07003023 dput(dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003024 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003025
Oleg Nesterovc35a7f12014-12-10 15:54:56 -08003026 if (pid == tgid)
3027 return;
3028
Eric W. Biederman48e64842006-06-26 00:25:48 -07003029 name.name = buf;
Pavel Emelyanov60347f62007-10-18 23:40:03 -07003030 name.len = snprintf(buf, sizeof(buf), "%d", tgid);
3031 leader = d_hash_and_lookup(mnt->mnt_root, &name);
Eric W. Biederman48e64842006-06-26 00:25:48 -07003032 if (!leader)
3033 goto out;
3034
3035 name.name = "task";
3036 name.len = strlen(name.name);
3037 dir = d_hash_and_lookup(leader, &name);
3038 if (!dir)
3039 goto out_put_leader;
3040
3041 name.name = buf;
Pavel Emelyanov60347f62007-10-18 23:40:03 -07003042 name.len = snprintf(buf, sizeof(buf), "%d", pid);
Eric W. Biederman48e64842006-06-26 00:25:48 -07003043 dentry = d_hash_and_lookup(dir, &name);
3044 if (dentry) {
Eric W. Biedermanbbd51922014-02-13 10:24:23 -08003045 d_invalidate(dentry);
Eric W. Biederman48e64842006-06-26 00:25:48 -07003046 dput(dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003047 }
Eric W. Biederman48e64842006-06-26 00:25:48 -07003048
3049 dput(dir);
3050out_put_leader:
3051 dput(leader);
3052out:
3053 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003054}
3055
Randy Dunlap0895e912007-10-21 21:00:10 -07003056/**
3057 * proc_flush_task - Remove dcache entries for @task from the /proc dcache.
3058 * @task: task that should be flushed.
3059 *
3060 * When flushing dentries from proc, one needs to flush them from global
Pavel Emelyanov60347f62007-10-18 23:40:03 -07003061 * proc (proc_mnt) and from all the namespaces' procs this task was seen
Randy Dunlap0895e912007-10-21 21:00:10 -07003062 * in. This call is supposed to do all of this job.
3063 *
3064 * Looks in the dcache for
3065 * /proc/@pid
3066 * /proc/@tgid/task/@pid
3067 * if either directory is present flushes it and all of it'ts children
3068 * from the dcache.
3069 *
3070 * It is safe and reasonable to cache /proc entries for a task until
3071 * that task exits. After that they just clog up the dcache with
3072 * useless entries, possibly causing useful dcache entries to be
3073 * flushed instead. This routine is proved to flush those useless
3074 * dcache entries at process exit time.
3075 *
3076 * NOTE: This routine is just an optimization so it does not guarantee
3077 * that no dcache entries will exist at process exit time it
3078 * just makes it very unlikely that any will persist.
Pavel Emelyanov60347f62007-10-18 23:40:03 -07003079 */
3080
3081void proc_flush_task(struct task_struct *task)
3082{
Eric W. Biederman9fcc2d12007-11-14 17:00:07 -08003083 int i;
Oleg Nesterov9b4d1cbe2009-09-22 16:45:34 -07003084 struct pid *pid, *tgid;
Pavel Emelyanov130f77e2007-10-18 23:40:11 -07003085 struct upid *upid;
3086
Pavel Emelyanov130f77e2007-10-18 23:40:11 -07003087 pid = task_pid(task);
Oleg Nesterov9b4d1cbe2009-09-22 16:45:34 -07003088 tgid = task_tgid(task);
Pavel Emelyanov130f77e2007-10-18 23:40:11 -07003089
Eric W. Biederman9fcc2d12007-11-14 17:00:07 -08003090 for (i = 0; i <= pid->level; i++) {
Pavel Emelyanov130f77e2007-10-18 23:40:11 -07003091 upid = &pid->numbers[i];
3092 proc_flush_task_mnt(upid->ns->proc_mnt, upid->nr,
Oleg Nesterov9b4d1cbe2009-09-22 16:45:34 -07003093 tgid->numbers[i].nr);
Pavel Emelyanov130f77e2007-10-18 23:40:11 -07003094 }
Pavel Emelyanov60347f62007-10-18 23:40:03 -07003095}
3096
Al Viroc52a47a2013-06-15 11:15:20 +04003097static int proc_pid_instantiate(struct inode *dir,
3098 struct dentry * dentry,
3099 struct task_struct *task, const void *ptr)
Eric W. Biederman444ceed2006-10-02 02:18:49 -07003100{
Eric W. Biederman444ceed2006-10-02 02:18:49 -07003101 struct inode *inode;
3102
Eric W. Biederman61a28782006-10-02 02:18:49 -07003103 inode = proc_pid_make_inode(dir->i_sb, task);
Eric W. Biederman444ceed2006-10-02 02:18:49 -07003104 if (!inode)
3105 goto out;
3106
3107 inode->i_mode = S_IFDIR|S_IRUGO|S_IXUGO;
3108 inode->i_op = &proc_tgid_base_inode_operations;
3109 inode->i_fop = &proc_tgid_base_operations;
3110 inode->i_flags|=S_IMMUTABLE;
Vegard Nossumaed54172008-06-05 22:46:53 -07003111
Miklos Szeredibfe86842011-10-28 14:13:29 +02003112 set_nlink(inode, 2 + pid_entry_count_dirs(tgid_base_stuff,
3113 ARRAY_SIZE(tgid_base_stuff)));
Eric W. Biederman444ceed2006-10-02 02:18:49 -07003114
Nick Pigginfb045ad2011-01-07 17:49:55 +11003115 d_set_d_op(dentry, &pid_dentry_operations);
Eric W. Biederman444ceed2006-10-02 02:18:49 -07003116
3117 d_add(dentry, inode);
3118 /* Close the race of the process dying before we return the dentry */
Al Viro0b728e12012-06-10 16:03:43 -04003119 if (pid_revalidate(dentry, 0))
Al Viroc52a47a2013-06-15 11:15:20 +04003120 return 0;
Eric W. Biederman444ceed2006-10-02 02:18:49 -07003121out:
Al Viroc52a47a2013-06-15 11:15:20 +04003122 return -ENOENT;
Eric W. Biederman444ceed2006-10-02 02:18:49 -07003123}
3124
Al Viro00cd8dd2012-06-10 17:13:09 -04003125struct dentry *proc_pid_lookup(struct inode *dir, struct dentry * dentry, unsigned int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003126{
Alexey Dobriyan335eb532014-08-08 14:21:27 -07003127 int result = -ENOENT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003128 struct task_struct *task;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003129 unsigned tgid;
Pavel Emelyanovb4888932007-10-18 23:40:14 -07003130 struct pid_namespace *ns;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003131
Alexey Dobriyandbcdb502014-08-08 14:21:25 -07003132 tgid = name_to_int(&dentry->d_name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003133 if (tgid == ~0U)
3134 goto out;
3135
Pavel Emelyanovb4888932007-10-18 23:40:14 -07003136 ns = dentry->d_sb->s_fs_info;
Eric W. Biedermande758732006-06-26 00:25:51 -07003137 rcu_read_lock();
Pavel Emelyanovb4888932007-10-18 23:40:14 -07003138 task = find_task_by_pid_ns(tgid, ns);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003139 if (task)
3140 get_task_struct(task);
Eric W. Biedermande758732006-06-26 00:25:51 -07003141 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07003142 if (!task)
3143 goto out;
3144
Eric W. Biederman444ceed2006-10-02 02:18:49 -07003145 result = proc_pid_instantiate(dir, dentry, task, NULL);
Eric W. Biederman48e64842006-06-26 00:25:48 -07003146 put_task_struct(task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003147out:
Al Viroc52a47a2013-06-15 11:15:20 +04003148 return ERR_PTR(result);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003149}
3150
Linus Torvalds1da177e2005-04-16 15:20:36 -07003151/*
Eric W. Biederman0804ef42006-10-02 02:17:04 -07003152 * Find the first task with tgid >= tgid
Eric W. Biederman0bc58a92006-06-26 00:25:50 -07003153 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07003154 */
Eric W. Biederman19fd4bb2007-11-28 16:21:26 -08003155struct tgid_iter {
3156 unsigned int tgid;
Eric W. Biederman0804ef42006-10-02 02:17:04 -07003157 struct task_struct *task;
Eric W. Biederman19fd4bb2007-11-28 16:21:26 -08003158};
3159static struct tgid_iter next_tgid(struct pid_namespace *ns, struct tgid_iter iter)
3160{
Eric W. Biederman0804ef42006-10-02 02:17:04 -07003161 struct pid *pid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003162
Eric W. Biederman19fd4bb2007-11-28 16:21:26 -08003163 if (iter.task)
3164 put_task_struct(iter.task);
Eric W. Biederman0804ef42006-10-02 02:17:04 -07003165 rcu_read_lock();
3166retry:
Eric W. Biederman19fd4bb2007-11-28 16:21:26 -08003167 iter.task = NULL;
3168 pid = find_ge_pid(iter.tgid, ns);
Eric W. Biederman0804ef42006-10-02 02:17:04 -07003169 if (pid) {
Eric W. Biederman19fd4bb2007-11-28 16:21:26 -08003170 iter.tgid = pid_nr_ns(pid, ns);
3171 iter.task = pid_task(pid, PIDTYPE_PID);
Eric W. Biederman0804ef42006-10-02 02:17:04 -07003172 /* What we to know is if the pid we have find is the
3173 * pid of a thread_group_leader. Testing for task
3174 * being a thread_group_leader is the obvious thing
3175 * todo but there is a window when it fails, due to
3176 * the pid transfer logic in de_thread.
3177 *
3178 * So we perform the straight forward test of seeing
3179 * if the pid we have found is the pid of a thread
3180 * group leader, and don't worry if the task we have
3181 * found doesn't happen to be a thread group leader.
3182 * As we don't care in the case of readdir.
3183 */
Eric W. Biederman19fd4bb2007-11-28 16:21:26 -08003184 if (!iter.task || !has_group_leader_pid(iter.task)) {
3185 iter.tgid += 1;
Eric W. Biederman0804ef42006-10-02 02:17:04 -07003186 goto retry;
Eric W. Biederman19fd4bb2007-11-28 16:21:26 -08003187 }
3188 get_task_struct(iter.task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003189 }
Eric W. Biederman454cc102006-06-26 00:25:51 -07003190 rcu_read_unlock();
Eric W. Biederman19fd4bb2007-11-28 16:21:26 -08003191 return iter;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003192}
3193
Eric W. Biederman00978752014-07-31 03:10:50 -07003194#define TGID_OFFSET (FIRST_PROCESS_ENTRY + 2)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003195
3196/* for the /proc/ directory itself, after non-process stuff has been done */
Al Virof0c3b502013-05-16 12:07:31 -04003197int proc_pid_readdir(struct file *file, struct dir_context *ctx)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003198{
Eric W. Biederman19fd4bb2007-11-28 16:21:26 -08003199 struct tgid_iter iter;
Al Viro3aa33772014-10-31 00:42:35 -04003200 struct pid_namespace *ns = file_inode(file)->i_sb->s_fs_info;
Al Virof0c3b502013-05-16 12:07:31 -04003201 loff_t pos = ctx->pos;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003202
Al Viro021ada72013-03-29 19:27:05 -04003203 if (pos >= PID_MAX_LIMIT + TGID_OFFSET)
Al Virof0c3b502013-05-16 12:07:31 -04003204 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003205
Eric W. Biederman00978752014-07-31 03:10:50 -07003206 if (pos == TGID_OFFSET - 2) {
David Howells2b0143b2015-03-17 22:25:59 +00003207 struct inode *inode = d_inode(ns->proc_self);
Al Virodb963162013-06-15 10:45:10 +04003208 if (!dir_emit(ctx, "self", 4, inode->i_ino, DT_LNK))
Al Virof0c3b502013-05-16 12:07:31 -04003209 return 0;
Eric W. Biederman00978752014-07-31 03:10:50 -07003210 ctx->pos = pos = pos + 1;
Al Viro021ada72013-03-29 19:27:05 -04003211 }
Eric W. Biederman00978752014-07-31 03:10:50 -07003212 if (pos == TGID_OFFSET - 1) {
David Howells2b0143b2015-03-17 22:25:59 +00003213 struct inode *inode = d_inode(ns->proc_thread_self);
Eric W. Biederman00978752014-07-31 03:10:50 -07003214 if (!dir_emit(ctx, "thread-self", 11, inode->i_ino, DT_LNK))
3215 return 0;
3216 ctx->pos = pos = pos + 1;
3217 }
3218 iter.tgid = pos - TGID_OFFSET;
Eric W. Biederman19fd4bb2007-11-28 16:21:26 -08003219 iter.task = NULL;
Eric W. Biederman19fd4bb2007-11-28 16:21:26 -08003220 for (iter = next_tgid(ns, iter);
3221 iter.task;
3222 iter.tgid += 1, iter = next_tgid(ns, iter)) {
Al Virof0c3b502013-05-16 12:07:31 -04003223 char name[PROC_NUMBUF];
3224 int len;
Eric Dumazet9618fba2017-01-24 15:18:07 -08003225
3226 cond_resched();
Al Virof0c3b502013-05-16 12:07:31 -04003227 if (!has_pid_permissions(ns, iter.task, 2))
3228 continue;
Vasiliy Kulikov04996802012-01-10 15:11:31 -08003229
Al Virof0c3b502013-05-16 12:07:31 -04003230 len = snprintf(name, sizeof(name), "%d", iter.tgid);
3231 ctx->pos = iter.tgid + TGID_OFFSET;
3232 if (!proc_fill_cache(file, ctx, name, len,
3233 proc_pid_instantiate, iter.task, NULL)) {
Eric W. Biederman19fd4bb2007-11-28 16:21:26 -08003234 put_task_struct(iter.task);
Al Virof0c3b502013-05-16 12:07:31 -04003235 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003236 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003237 }
Al Virof0c3b502013-05-16 12:07:31 -04003238 ctx->pos = PID_MAX_LIMIT + TGID_OFFSET;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003239 return 0;
3240}
3241
Eric W. Biederman0bc58a92006-06-26 00:25:50 -07003242/*
Janis Danisevskis1b3044e2016-05-20 17:00:08 -07003243 * proc_tid_comm_permission is a special permission function exclusively
3244 * used for the node /proc/<pid>/task/<tid>/comm.
3245 * It bypasses generic permission checks in the case where a task of the same
3246 * task group attempts to access the node.
3247 * The rationale behind this is that glibc and bionic access this node for
3248 * cross thread naming (pthread_set/getname_np(!self)). However, if
3249 * PR_SET_DUMPABLE gets set to 0 this node among others becomes uid=0 gid=0,
3250 * which locks out the cross thread naming implementation.
3251 * This function makes sure that the node is always accessible for members of
3252 * same thread group.
3253 */
3254static int proc_tid_comm_permission(struct inode *inode, int mask)
3255{
3256 bool is_same_tgroup;
3257 struct task_struct *task;
3258
3259 task = get_proc_task(inode);
3260 if (!task)
3261 return -ESRCH;
3262 is_same_tgroup = same_thread_group(current, task);
3263 put_task_struct(task);
3264
3265 if (likely(is_same_tgroup && !(mask & MAY_EXEC))) {
3266 /* This file (/proc/<pid>/task/<tid>/comm) can always be
3267 * read or written by the members of the corresponding
3268 * thread group.
3269 */
3270 return 0;
3271 }
3272
3273 return generic_permission(inode, mask);
3274}
3275
3276static const struct inode_operations proc_tid_comm_inode_operations = {
3277 .permission = proc_tid_comm_permission,
3278};
3279
3280/*
Eric W. Biederman28a6d672006-10-02 02:17:05 -07003281 * Tasks
3282 */
Eric Dumazetc5141e62007-05-08 00:26:15 -07003283static const struct pid_entry tid_base_stuff[] = {
Alexey Dobriyan631f9c12008-11-10 01:32:52 +03003284 DIR("fd", S_IRUSR|S_IXUSR, proc_fd_inode_operations, proc_fd_operations),
Jerome Marchand38355412010-04-27 13:13:06 -07003285 DIR("fdinfo", S_IRUSR|S_IXUSR, proc_fdinfo_inode_operations, proc_fdinfo_operations),
Eric W. Biederman6b4e3062010-03-07 16:41:34 -08003286 DIR("ns", S_IRUSR|S_IXUGO, proc_ns_dir_inode_operations, proc_ns_dir_operations),
Eric W. Biederman6ba8ed72014-07-31 16:27:08 -07003287#ifdef CONFIG_NET
3288 DIR("net", S_IRUGO|S_IXUGO, proc_net_inode_operations, proc_net_operations),
3289#endif
Alexey Dobriyan631f9c12008-11-10 01:32:52 +03003290 REG("environ", S_IRUSR, proc_environ_operations),
Al Viroc5317162016-10-05 18:43:43 -04003291 REG("auxv", S_IRUSR, proc_auxv_operations),
Alexey Dobriyan631f9c12008-11-10 01:32:52 +03003292 ONE("status", S_IRUGO, proc_pid_status),
Djalal Harouni35a35042014-04-07 15:38:36 -07003293 ONE("personality", S_IRUSR, proc_pid_personality),
Alexey Dobriyan1c963eb2014-08-08 14:21:37 -07003294 ONE("limits", S_IRUGO, proc_pid_limits),
Ingo Molnar43ae34c2007-07-09 18:52:00 +02003295#ifdef CONFIG_SCHED_DEBUG
Alexey Dobriyan631f9c12008-11-10 01:32:52 +03003296 REG("sched", S_IRUGO|S_IWUSR, proc_pid_sched_operations),
Ingo Molnar43ae34c2007-07-09 18:52:00 +02003297#endif
Janis Danisevskis1b3044e2016-05-20 17:00:08 -07003298 NOD("comm", S_IFREG|S_IRUGO|S_IWUSR,
3299 &proc_tid_comm_inode_operations,
3300 &proc_pid_set_comm_operations, {}),
Roland McGrathebcb6732008-07-25 19:46:00 -07003301#ifdef CONFIG_HAVE_ARCH_TRACEHOOK
Alexey Dobriyan09d93bd2014-08-08 14:21:39 -07003302 ONE("syscall", S_IRUSR, proc_pid_syscall),
Roland McGrathebcb6732008-07-25 19:46:00 -07003303#endif
Alexey Dobriyanc2c0bb42015-06-25 15:00:54 -07003304 REG("cmdline", S_IRUGO, proc_pid_cmdline_ops),
Alexey Dobriyan631f9c12008-11-10 01:32:52 +03003305 ONE("stat", S_IRUGO, proc_tid_stat),
3306 ONE("statm", S_IRUGO, proc_pid_statm),
Siddhesh Poyarekarb7643752012-03-21 16:34:04 -07003307 REG("maps", S_IRUGO, proc_tid_maps_operations),
Iago López Galeiras2e13ba52015-06-25 15:00:57 -07003308#ifdef CONFIG_PROC_CHILDREN
Cyrill Gorcunov818411612012-05-31 16:26:43 -07003309 REG("children", S_IRUGO, proc_tid_children_operations),
3310#endif
Eric W. Biederman28a6d672006-10-02 02:17:05 -07003311#ifdef CONFIG_NUMA
Siddhesh Poyarekarb7643752012-03-21 16:34:04 -07003312 REG("numa_maps", S_IRUGO, proc_tid_numa_maps_operations),
Eric W. Biederman28a6d672006-10-02 02:17:05 -07003313#endif
Alexey Dobriyan631f9c12008-11-10 01:32:52 +03003314 REG("mem", S_IRUSR|S_IWUSR, proc_mem_operations),
3315 LNK("cwd", proc_cwd_link),
3316 LNK("root", proc_root_link),
3317 LNK("exe", proc_exe_link),
3318 REG("mounts", S_IRUGO, proc_mounts_operations),
3319 REG("mountinfo", S_IRUGO, proc_mountinfo_operations),
Matt Mackall1e883282008-02-04 22:29:07 -08003320#ifdef CONFIG_PROC_PAGE_MONITOR
Alexey Dobriyan631f9c12008-11-10 01:32:52 +03003321 REG("clear_refs", S_IWUSR, proc_clear_refs_operations),
Siddhesh Poyarekarb7643752012-03-21 16:34:04 -07003322 REG("smaps", S_IRUGO, proc_tid_smaps_operations),
Djalal Harouni32ed74a2014-04-07 15:38:38 -07003323 REG("pagemap", S_IRUSR, proc_pagemap_operations),
Eric W. Biederman28a6d672006-10-02 02:17:05 -07003324#endif
3325#ifdef CONFIG_SECURITY
Alexey Dobriyan631f9c12008-11-10 01:32:52 +03003326 DIR("attr", S_IRUGO|S_IXUGO, proc_attr_dir_inode_operations, proc_attr_dir_operations),
Eric W. Biederman28a6d672006-10-02 02:17:05 -07003327#endif
3328#ifdef CONFIG_KALLSYMS
Alexey Dobriyanedfcd602014-08-08 14:21:44 -07003329 ONE("wchan", S_IRUGO, proc_pid_wchan),
Eric W. Biederman28a6d672006-10-02 02:17:05 -07003330#endif
Ken Chen2ec220e2008-11-10 11:26:08 +03003331#ifdef CONFIG_STACKTRACE
Djalal Harouni35a35042014-04-07 15:38:36 -07003332 ONE("stack", S_IRUSR, proc_pid_stack),
Eric W. Biederman28a6d672006-10-02 02:17:05 -07003333#endif
Naveen N. Rao5968cec2015-06-30 14:36:03 +05303334#ifdef CONFIG_SCHED_INFO
Alexey Dobriyanf6e826c2014-08-08 14:21:46 -07003335 ONE("schedstat", S_IRUGO, proc_pid_schedstat),
Eric W. Biederman28a6d672006-10-02 02:17:05 -07003336#endif
Arjan van de Ven97455122008-01-25 21:08:34 +01003337#ifdef CONFIG_LATENCYTOP
Alexey Dobriyan631f9c12008-11-10 01:32:52 +03003338 REG("latency", S_IRUGO, proc_lstats_operations),
Arjan van de Ven97455122008-01-25 21:08:34 +01003339#endif
Paul Menage8793d852007-10-18 23:39:39 -07003340#ifdef CONFIG_PROC_PID_CPUSET
Zefan Li52de4772014-09-18 16:03:36 +08003341 ONE("cpuset", S_IRUGO, proc_cpuset_show),
Eric W. Biederman28a6d672006-10-02 02:17:05 -07003342#endif
Paul Menagea4243162007-10-18 23:39:35 -07003343#ifdef CONFIG_CGROUPS
Zefan Li006f4ac2014-09-18 16:03:15 +08003344 ONE("cgroup", S_IRUGO, proc_cgroup_show),
Paul Menagea4243162007-10-18 23:39:35 -07003345#endif
Alexey Dobriyan6ba51e32014-08-08 14:21:48 -07003346 ONE("oom_score", S_IRUGO, proc_oom_score),
David Rientjesfa0cbbf2012-11-12 17:53:04 -08003347 REG("oom_adj", S_IRUGO|S_IWUSR, proc_oom_adj_operations),
David Rientjesa63d83f2010-08-09 17:19:46 -07003348 REG("oom_score_adj", S_IRUGO|S_IWUSR, proc_oom_score_adj_operations),
Eric W. Biederman28a6d672006-10-02 02:17:05 -07003349#ifdef CONFIG_AUDITSYSCALL
Alexey Dobriyan631f9c12008-11-10 01:32:52 +03003350 REG("loginuid", S_IWUSR|S_IRUGO, proc_loginuid_operations),
Al Viro26ec3c62011-02-15 21:24:05 -05003351 REG("sessionid", S_IRUGO, proc_sessionid_operations),
Eric W. Biederman28a6d672006-10-02 02:17:05 -07003352#endif
Akinobu Mitaf4f154f2006-12-08 02:39:47 -08003353#ifdef CONFIG_FAULT_INJECTION
Alexey Dobriyan631f9c12008-11-10 01:32:52 +03003354 REG("make-it-fail", S_IRUGO|S_IWUSR, proc_fault_inject_operations),
Akinobu Mitaf4f154f2006-12-08 02:39:47 -08003355#endif
Andrea Righi297c5d92008-07-25 01:48:49 -07003356#ifdef CONFIG_TASK_IO_ACCOUNTING
Alexey Dobriyan19aadc92014-08-08 14:21:50 -07003357 ONE("io", S_IRUSR, proc_tid_io_accounting),
Andrea Righi297c5d92008-07-25 01:48:49 -07003358#endif
Chris Metcalff133ecc2011-05-26 12:40:09 -04003359#ifdef CONFIG_HARDWALL
Alexey Dobriyand962c142014-08-08 14:21:52 -07003360 ONE("hardwall", S_IRUGO, proc_pid_hardwall),
Chris Metcalff133ecc2011-05-26 12:40:09 -04003361#endif
Eric W. Biederman22d917d2011-11-17 00:11:58 -08003362#ifdef CONFIG_USER_NS
3363 REG("uid_map", S_IRUGO|S_IWUSR, proc_uid_map_operations),
3364 REG("gid_map", S_IRUGO|S_IWUSR, proc_gid_map_operations),
Eric W. Biedermanf76d2072012-08-30 01:24:05 -07003365 REG("projid_map", S_IRUGO|S_IWUSR, proc_projid_map_operations),
Eric W. Biederman9cc46512014-12-02 12:27:26 -06003366 REG("setgroups", S_IRUGO|S_IWUSR, proc_setgroups_operations),
Eric W. Biederman22d917d2011-11-17 00:11:58 -08003367#endif
Eric W. Biederman28a6d672006-10-02 02:17:05 -07003368};
3369
Al Virof0c3b502013-05-16 12:07:31 -04003370static int proc_tid_base_readdir(struct file *file, struct dir_context *ctx)
Eric W. Biederman28a6d672006-10-02 02:17:05 -07003371{
Al Virof0c3b502013-05-16 12:07:31 -04003372 return proc_pident_readdir(file, ctx,
3373 tid_base_stuff, ARRAY_SIZE(tid_base_stuff));
Eric W. Biederman28a6d672006-10-02 02:17:05 -07003374}
3375
Al Viro00cd8dd2012-06-10 17:13:09 -04003376static struct dentry *proc_tid_base_lookup(struct inode *dir, struct dentry *dentry, unsigned int flags)
3377{
Eric W. Biederman7bcd6b02006-10-02 02:18:56 -07003378 return proc_pident_lookup(dir, dentry,
3379 tid_base_stuff, ARRAY_SIZE(tid_base_stuff));
Eric W. Biederman28a6d672006-10-02 02:17:05 -07003380}
3381
Arjan van de Ven00977a52007-02-12 00:55:34 -08003382static const struct file_operations proc_tid_base_operations = {
Eric W. Biederman28a6d672006-10-02 02:17:05 -07003383 .read = generic_read_dir,
Al Virof50752e2016-04-20 17:13:54 -04003384 .iterate_shared = proc_tid_base_readdir,
3385 .llseek = generic_file_llseek,
Eric W. Biederman28a6d672006-10-02 02:17:05 -07003386};
3387
Arjan van de Venc5ef1c42007-02-12 00:55:40 -08003388static const struct inode_operations proc_tid_base_inode_operations = {
Eric W. Biederman28a6d672006-10-02 02:17:05 -07003389 .lookup = proc_tid_base_lookup,
3390 .getattr = pid_getattr,
3391 .setattr = proc_setattr,
3392};
3393
Al Viroc52a47a2013-06-15 11:15:20 +04003394static int proc_task_instantiate(struct inode *dir,
Eric Dumazetc5141e62007-05-08 00:26:15 -07003395 struct dentry *dentry, struct task_struct *task, const void *ptr)
Eric W. Biederman444ceed2006-10-02 02:18:49 -07003396{
Eric W. Biederman444ceed2006-10-02 02:18:49 -07003397 struct inode *inode;
Eric W. Biederman61a28782006-10-02 02:18:49 -07003398 inode = proc_pid_make_inode(dir->i_sb, task);
Eric W. Biederman444ceed2006-10-02 02:18:49 -07003399
3400 if (!inode)
3401 goto out;
3402 inode->i_mode = S_IFDIR|S_IRUGO|S_IXUGO;
3403 inode->i_op = &proc_tid_base_inode_operations;
3404 inode->i_fop = &proc_tid_base_operations;
3405 inode->i_flags|=S_IMMUTABLE;
Vegard Nossumaed54172008-06-05 22:46:53 -07003406
Miklos Szeredibfe86842011-10-28 14:13:29 +02003407 set_nlink(inode, 2 + pid_entry_count_dirs(tid_base_stuff,
3408 ARRAY_SIZE(tid_base_stuff)));
Eric W. Biederman444ceed2006-10-02 02:18:49 -07003409
Nick Pigginfb045ad2011-01-07 17:49:55 +11003410 d_set_d_op(dentry, &pid_dentry_operations);
Eric W. Biederman444ceed2006-10-02 02:18:49 -07003411
3412 d_add(dentry, inode);
3413 /* Close the race of the process dying before we return the dentry */
Al Viro0b728e12012-06-10 16:03:43 -04003414 if (pid_revalidate(dentry, 0))
Al Viroc52a47a2013-06-15 11:15:20 +04003415 return 0;
Eric W. Biederman444ceed2006-10-02 02:18:49 -07003416out:
Al Viroc52a47a2013-06-15 11:15:20 +04003417 return -ENOENT;
Eric W. Biederman444ceed2006-10-02 02:18:49 -07003418}
3419
Al Viro00cd8dd2012-06-10 17:13:09 -04003420static struct dentry *proc_task_lookup(struct inode *dir, struct dentry * dentry, unsigned int flags)
Eric W. Biederman28a6d672006-10-02 02:17:05 -07003421{
Al Viroc52a47a2013-06-15 11:15:20 +04003422 int result = -ENOENT;
Eric W. Biederman28a6d672006-10-02 02:17:05 -07003423 struct task_struct *task;
3424 struct task_struct *leader = get_proc_task(dir);
Eric W. Biederman28a6d672006-10-02 02:17:05 -07003425 unsigned tid;
Pavel Emelyanovb4888932007-10-18 23:40:14 -07003426 struct pid_namespace *ns;
Eric W. Biederman28a6d672006-10-02 02:17:05 -07003427
3428 if (!leader)
3429 goto out_no_task;
3430
Alexey Dobriyandbcdb502014-08-08 14:21:25 -07003431 tid = name_to_int(&dentry->d_name);
Eric W. Biederman28a6d672006-10-02 02:17:05 -07003432 if (tid == ~0U)
3433 goto out;
3434
Pavel Emelyanovb4888932007-10-18 23:40:14 -07003435 ns = dentry->d_sb->s_fs_info;
Eric W. Biederman28a6d672006-10-02 02:17:05 -07003436 rcu_read_lock();
Pavel Emelyanovb4888932007-10-18 23:40:14 -07003437 task = find_task_by_pid_ns(tid, ns);
Eric W. Biederman28a6d672006-10-02 02:17:05 -07003438 if (task)
3439 get_task_struct(task);
3440 rcu_read_unlock();
3441 if (!task)
3442 goto out;
Pavel Emelyanovbac0abd2007-10-18 23:40:18 -07003443 if (!same_thread_group(leader, task))
Eric W. Biederman28a6d672006-10-02 02:17:05 -07003444 goto out_drop_task;
3445
Eric W. Biederman444ceed2006-10-02 02:18:49 -07003446 result = proc_task_instantiate(dir, dentry, task, NULL);
Eric W. Biederman28a6d672006-10-02 02:17:05 -07003447out_drop_task:
3448 put_task_struct(task);
3449out:
3450 put_task_struct(leader);
3451out_no_task:
Al Viroc52a47a2013-06-15 11:15:20 +04003452 return ERR_PTR(result);
Eric W. Biederman28a6d672006-10-02 02:17:05 -07003453}
3454
3455/*
Eric W. Biederman0bc58a92006-06-26 00:25:50 -07003456 * Find the first tid of a thread group to return to user space.
3457 *
3458 * Usually this is just the thread group leader, but if the users
3459 * buffer was too small or there was a seek into the middle of the
3460 * directory we have more work todo.
3461 *
3462 * In the case of a short read we start with find_task_by_pid.
3463 *
3464 * In the case of a seek we start with the leader and walk nr
3465 * threads past it.
3466 */
Oleg Nesterov9f6e9632014-01-23 15:55:40 -08003467static struct task_struct *first_tid(struct pid *pid, int tid, loff_t f_pos,
3468 struct pid_namespace *ns)
Eric W. Biederman0bc58a92006-06-26 00:25:50 -07003469{
Oleg Nesterovd855a4b2014-01-23 15:55:39 -08003470 struct task_struct *pos, *task;
Oleg Nesterov9f6e9632014-01-23 15:55:40 -08003471 unsigned long nr = f_pos;
3472
3473 if (nr != f_pos) /* 32bit overflow? */
3474 return NULL;
Eric W. Biederman0bc58a92006-06-26 00:25:50 -07003475
Eric W. Biedermancc288732006-06-26 00:26:01 -07003476 rcu_read_lock();
Oleg Nesterovd855a4b2014-01-23 15:55:39 -08003477 task = pid_task(pid, PIDTYPE_PID);
3478 if (!task)
3479 goto fail;
3480
3481 /* Attempt to start with the tid of a thread */
Oleg Nesterov9f6e9632014-01-23 15:55:40 -08003482 if (tid && nr) {
Pavel Emelyanovb4888932007-10-18 23:40:14 -07003483 pos = find_task_by_pid_ns(tid, ns);
Oleg Nesterovd855a4b2014-01-23 15:55:39 -08003484 if (pos && same_thread_group(pos, task))
Oleg Nesterova872ff02006-06-26 00:26:01 -07003485 goto found;
Eric W. Biederman0bc58a92006-06-26 00:25:50 -07003486 }
3487
3488 /* If nr exceeds the number of threads there is nothing todo */
Oleg Nesterov9f6e9632014-01-23 15:55:40 -08003489 if (nr >= get_nr_threads(task))
Oleg Nesterovc986c142014-01-23 15:55:38 -08003490 goto fail;
Oleg Nesterova872ff02006-06-26 00:26:01 -07003491
3492 /* If we haven't found our starting place yet start
3493 * with the leader and walk nr threads forward.
3494 */
Oleg Nesterovd855a4b2014-01-23 15:55:39 -08003495 pos = task = task->group_leader;
Oleg Nesterovc986c142014-01-23 15:55:38 -08003496 do {
Oleg Nesterov9f6e9632014-01-23 15:55:40 -08003497 if (!nr--)
Oleg Nesterovc986c142014-01-23 15:55:38 -08003498 goto found;
Oleg Nesterovd855a4b2014-01-23 15:55:39 -08003499 } while_each_thread(task, pos);
Oleg Nesterovc986c142014-01-23 15:55:38 -08003500fail:
3501 pos = NULL;
3502 goto out;
Oleg Nesterova872ff02006-06-26 00:26:01 -07003503found:
3504 get_task_struct(pos);
3505out:
Eric W. Biedermancc288732006-06-26 00:26:01 -07003506 rcu_read_unlock();
Eric W. Biederman0bc58a92006-06-26 00:25:50 -07003507 return pos;
3508}
3509
3510/*
3511 * Find the next thread in the thread list.
3512 * Return NULL if there is an error or no next thread.
3513 *
3514 * The reference to the input task_struct is released.
3515 */
3516static struct task_struct *next_tid(struct task_struct *start)
3517{
Oleg Nesterovc1df7fb2006-06-26 00:26:02 -07003518 struct task_struct *pos = NULL;
Eric W. Biedermancc288732006-06-26 00:26:01 -07003519 rcu_read_lock();
Oleg Nesterovc1df7fb2006-06-26 00:26:02 -07003520 if (pid_alive(start)) {
Eric W. Biederman0bc58a92006-06-26 00:25:50 -07003521 pos = next_thread(start);
Oleg Nesterovc1df7fb2006-06-26 00:26:02 -07003522 if (thread_group_leader(pos))
3523 pos = NULL;
3524 else
3525 get_task_struct(pos);
3526 }
Eric W. Biedermancc288732006-06-26 00:26:01 -07003527 rcu_read_unlock();
Eric W. Biederman0bc58a92006-06-26 00:25:50 -07003528 put_task_struct(start);
3529 return pos;
3530}
3531
Linus Torvalds1da177e2005-04-16 15:20:36 -07003532/* for the /proc/TGID/task/ directories */
Al Virof0c3b502013-05-16 12:07:31 -04003533static int proc_task_readdir(struct file *file, struct dir_context *ctx)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003534{
Oleg Nesterovd855a4b2014-01-23 15:55:39 -08003535 struct inode *inode = file_inode(file);
3536 struct task_struct *task;
Pavel Emelyanovb4888932007-10-18 23:40:14 -07003537 struct pid_namespace *ns;
Al Virof0c3b502013-05-16 12:07:31 -04003538 int tid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003539
Oleg Nesterovd855a4b2014-01-23 15:55:39 -08003540 if (proc_inode_is_dead(inode))
Al Virof0c3b502013-05-16 12:07:31 -04003541 return -ENOENT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003542
Al Virof0c3b502013-05-16 12:07:31 -04003543 if (!dir_emit_dots(file, ctx))
Oleg Nesterovd855a4b2014-01-23 15:55:39 -08003544 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003545
Eric W. Biederman0bc58a92006-06-26 00:25:50 -07003546 /* f_version caches the tgid value that the last readdir call couldn't
3547 * return. lseek aka telldir automagically resets f_version to 0.
3548 */
Al Viro3aa33772014-10-31 00:42:35 -04003549 ns = inode->i_sb->s_fs_info;
Al Virof0c3b502013-05-16 12:07:31 -04003550 tid = (int)file->f_version;
3551 file->f_version = 0;
Oleg Nesterovd855a4b2014-01-23 15:55:39 -08003552 for (task = first_tid(proc_pid(inode), tid, ctx->pos - 2, ns);
Eric W. Biederman0bc58a92006-06-26 00:25:50 -07003553 task;
Al Virof0c3b502013-05-16 12:07:31 -04003554 task = next_tid(task), ctx->pos++) {
3555 char name[PROC_NUMBUF];
3556 int len;
Pavel Emelyanovb4888932007-10-18 23:40:14 -07003557 tid = task_pid_nr_ns(task, ns);
Al Virof0c3b502013-05-16 12:07:31 -04003558 len = snprintf(name, sizeof(name), "%d", tid);
3559 if (!proc_fill_cache(file, ctx, name, len,
3560 proc_task_instantiate, task, NULL)) {
Eric W. Biederman0bc58a92006-06-26 00:25:50 -07003561 /* returning this tgid failed, save it as the first
3562 * pid for the next readir call */
Al Virof0c3b502013-05-16 12:07:31 -04003563 file->f_version = (u64)tid;
Eric W. Biederman0bc58a92006-06-26 00:25:50 -07003564 put_task_struct(task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003565 break;
Eric W. Biederman0bc58a92006-06-26 00:25:50 -07003566 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003567 }
Oleg Nesterovd855a4b2014-01-23 15:55:39 -08003568
Al Virof0c3b502013-05-16 12:07:31 -04003569 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003570}
Eric W. Biederman6e66b522006-06-26 00:25:47 -07003571
3572static int proc_task_getattr(struct vfsmount *mnt, struct dentry *dentry, struct kstat *stat)
3573{
David Howells2b0143b2015-03-17 22:25:59 +00003574 struct inode *inode = d_inode(dentry);
Eric W. Biederman99f89552006-06-26 00:25:55 -07003575 struct task_struct *p = get_proc_task(inode);
Eric W. Biederman6e66b522006-06-26 00:25:47 -07003576 generic_fillattr(inode, stat);
3577
Eric W. Biederman99f89552006-06-26 00:25:55 -07003578 if (p) {
Eric W. Biederman99f89552006-06-26 00:25:55 -07003579 stat->nlink += get_nr_threads(p);
Eric W. Biederman99f89552006-06-26 00:25:55 -07003580 put_task_struct(p);
Eric W. Biederman6e66b522006-06-26 00:25:47 -07003581 }
3582
3583 return 0;
3584}
Eric W. Biederman28a6d672006-10-02 02:17:05 -07003585
Arjan van de Venc5ef1c42007-02-12 00:55:40 -08003586static const struct inode_operations proc_task_inode_operations = {
Eric W. Biederman28a6d672006-10-02 02:17:05 -07003587 .lookup = proc_task_lookup,
3588 .getattr = proc_task_getattr,
3589 .setattr = proc_setattr,
Vasiliy Kulikov04996802012-01-10 15:11:31 -08003590 .permission = proc_pid_permission,
Eric W. Biederman28a6d672006-10-02 02:17:05 -07003591};
3592
Arjan van de Ven00977a52007-02-12 00:55:34 -08003593static const struct file_operations proc_task_operations = {
Eric W. Biederman28a6d672006-10-02 02:17:05 -07003594 .read = generic_read_dir,
Al Virof50752e2016-04-20 17:13:54 -04003595 .iterate_shared = proc_task_readdir,
3596 .llseek = generic_file_llseek,
Eric W. Biederman28a6d672006-10-02 02:17:05 -07003597};