blob: 591bf2b1ab668ba9f2de27df107c32efb85b7632 [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
457 entries = kmalloc(MAX_STACK_TRACE_DEPTH * sizeof(*entries), GFP_KERNEL);
458 if (!entries)
459 return -ENOMEM;
460
461 trace.nr_entries = 0;
462 trace.max_entries = MAX_STACK_TRACE_DEPTH;
463 trace.entries = entries;
464 trace.skip = 0;
Ken Chen2ec220e2008-11-10 11:26:08 +0300465
Al Viroa9712bc2011-03-23 15:52:50 -0400466 err = lock_trace(task);
467 if (!err) {
468 save_stack_trace_tsk(task, &trace);
469
470 for (i = 0; i < trace.nr_entries; i++) {
Josh Poimboeuf8b927d72016-08-18 10:59:07 -0500471 seq_printf(m, "[<%pK>] %pB\n",
Al Viroa9712bc2011-03-23 15:52:50 -0400472 (void *)entries[i], (void *)entries[i]);
473 }
474 unlock_trace(task);
Ken Chen2ec220e2008-11-10 11:26:08 +0300475 }
476 kfree(entries);
477
Al Viroa9712bc2011-03-23 15:52:50 -0400478 return err;
Ken Chen2ec220e2008-11-10 11:26:08 +0300479}
480#endif
481
Naveen N. Rao5968cec2015-06-30 14:36:03 +0530482#ifdef CONFIG_SCHED_INFO
Linus Torvalds1da177e2005-04-16 15:20:36 -0700483/*
484 * Provides /proc/PID/schedstat
485 */
Alexey Dobriyanf6e826c2014-08-08 14:21:46 -0700486static int proc_pid_schedstat(struct seq_file *m, struct pid_namespace *ns,
487 struct pid *pid, struct task_struct *task)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700488{
Naveen N. Rao5968cec2015-06-30 14:36:03 +0530489 if (unlikely(!sched_info_on()))
490 seq_printf(m, "0 0 0\n");
491 else
492 seq_printf(m, "%llu %llu %lu\n",
Joe Perches25ce3192015-04-15 16:18:17 -0700493 (unsigned long long)task->se.sum_exec_runtime,
494 (unsigned long long)task->sched_info.run_delay,
495 task->sched_info.pcount);
496
497 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700498}
499#endif
500
Arjan van de Ven97455122008-01-25 21:08:34 +0100501#ifdef CONFIG_LATENCYTOP
502static int lstats_show_proc(struct seq_file *m, void *v)
503{
504 int i;
Hiroshi Shimamoto13d77c32008-02-20 16:53:29 -0800505 struct inode *inode = m->private;
506 struct task_struct *task = get_proc_task(inode);
Arjan van de Ven97455122008-01-25 21:08:34 +0100507
Hiroshi Shimamoto13d77c32008-02-20 16:53:29 -0800508 if (!task)
509 return -ESRCH;
510 seq_puts(m, "Latency Top version : v0.1\n");
Arjan van de Ven97455122008-01-25 21:08:34 +0100511 for (i = 0; i < 32; i++) {
Joe Perches34e49d42011-01-12 17:00:30 -0800512 struct latency_record *lr = &task->latency_record[i];
513 if (lr->backtrace[0]) {
Arjan van de Ven97455122008-01-25 21:08:34 +0100514 int q;
Joe Perches34e49d42011-01-12 17:00:30 -0800515 seq_printf(m, "%i %li %li",
516 lr->count, lr->time, lr->max);
Arjan van de Ven97455122008-01-25 21:08:34 +0100517 for (q = 0; q < LT_BACKTRACEDEPTH; q++) {
Joe Perches34e49d42011-01-12 17:00:30 -0800518 unsigned long bt = lr->backtrace[q];
519 if (!bt)
Arjan van de Ven97455122008-01-25 21:08:34 +0100520 break;
Joe Perches34e49d42011-01-12 17:00:30 -0800521 if (bt == ULONG_MAX)
Arjan van de Ven97455122008-01-25 21:08:34 +0100522 break;
Joe Perches34e49d42011-01-12 17:00:30 -0800523 seq_printf(m, " %ps", (void *)bt);
Arjan van de Ven97455122008-01-25 21:08:34 +0100524 }
Alexey Dobriyan9d6de122011-01-12 17:00:32 -0800525 seq_putc(m, '\n');
Arjan van de Ven97455122008-01-25 21:08:34 +0100526 }
527
528 }
Hiroshi Shimamoto13d77c32008-02-20 16:53:29 -0800529 put_task_struct(task);
Arjan van de Ven97455122008-01-25 21:08:34 +0100530 return 0;
531}
532
533static int lstats_open(struct inode *inode, struct file *file)
534{
Hiroshi Shimamoto13d77c32008-02-20 16:53:29 -0800535 return single_open(file, lstats_show_proc, inode);
Hiroshi Shimamotod6643d12008-02-14 10:27:00 -0800536}
537
Arjan van de Ven97455122008-01-25 21:08:34 +0100538static ssize_t lstats_write(struct file *file, const char __user *buf,
539 size_t count, loff_t *offs)
540{
Al Viro496ad9a2013-01-23 17:07:38 -0500541 struct task_struct *task = get_proc_task(file_inode(file));
Arjan van de Ven97455122008-01-25 21:08:34 +0100542
Hiroshi Shimamoto13d77c32008-02-20 16:53:29 -0800543 if (!task)
544 return -ESRCH;
Arjan van de Ven97455122008-01-25 21:08:34 +0100545 clear_all_latency_tracing(task);
Hiroshi Shimamoto13d77c32008-02-20 16:53:29 -0800546 put_task_struct(task);
Arjan van de Ven97455122008-01-25 21:08:34 +0100547
548 return count;
549}
550
551static const struct file_operations proc_lstats_operations = {
552 .open = lstats_open,
553 .read = seq_read,
554 .write = lstats_write,
555 .llseek = seq_lseek,
Hiroshi Shimamoto13d77c32008-02-20 16:53:29 -0800556 .release = single_release,
Arjan van de Ven97455122008-01-25 21:08:34 +0100557};
558
559#endif
560
Alexey Dobriyan6ba51e32014-08-08 14:21:48 -0700561static int proc_oom_score(struct seq_file *m, struct pid_namespace *ns,
562 struct pid *pid, struct task_struct *task)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700563{
David Rientjesa7f638f2012-05-29 15:06:47 -0700564 unsigned long totalpages = totalram_pages + total_swap_pages;
Oleg Nesterovb95c35e2010-04-01 15:13:57 +0200565 unsigned long points = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700566
Oleg Nesterovef419392016-08-02 14:03:19 -0700567 points = oom_badness(task, NULL, NULL, totalpages) *
568 1000 / totalpages;
Joe Perches25ce3192015-04-15 16:18:17 -0700569 seq_printf(m, "%lu\n", points);
570
571 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700572}
573
Neil Hormand85f50d2007-10-18 23:40:37 -0700574struct limit_names {
Alexey Dobriyancedbcca2014-08-08 14:21:33 -0700575 const char *name;
576 const char *unit;
Neil Hormand85f50d2007-10-18 23:40:37 -0700577};
578
579static const struct limit_names lnames[RLIM_NLIMITS] = {
Kees Cookcff4edb2009-09-22 16:45:32 -0700580 [RLIMIT_CPU] = {"Max cpu time", "seconds"},
Neil Hormand85f50d2007-10-18 23:40:37 -0700581 [RLIMIT_FSIZE] = {"Max file size", "bytes"},
582 [RLIMIT_DATA] = {"Max data size", "bytes"},
583 [RLIMIT_STACK] = {"Max stack size", "bytes"},
584 [RLIMIT_CORE] = {"Max core file size", "bytes"},
585 [RLIMIT_RSS] = {"Max resident set", "bytes"},
586 [RLIMIT_NPROC] = {"Max processes", "processes"},
587 [RLIMIT_NOFILE] = {"Max open files", "files"},
588 [RLIMIT_MEMLOCK] = {"Max locked memory", "bytes"},
589 [RLIMIT_AS] = {"Max address space", "bytes"},
590 [RLIMIT_LOCKS] = {"Max file locks", "locks"},
591 [RLIMIT_SIGPENDING] = {"Max pending signals", "signals"},
592 [RLIMIT_MSGQUEUE] = {"Max msgqueue size", "bytes"},
593 [RLIMIT_NICE] = {"Max nice priority", NULL},
594 [RLIMIT_RTPRIO] = {"Max realtime priority", NULL},
Eugene Teo88081172008-02-23 15:23:52 -0800595 [RLIMIT_RTTIME] = {"Max realtime timeout", "us"},
Neil Hormand85f50d2007-10-18 23:40:37 -0700596};
597
598/* Display limits for a process */
Alexey Dobriyan1c963eb2014-08-08 14:21:37 -0700599static int proc_pid_limits(struct seq_file *m, struct pid_namespace *ns,
600 struct pid *pid, struct task_struct *task)
Neil Hormand85f50d2007-10-18 23:40:37 -0700601{
602 unsigned int i;
Neil Hormand85f50d2007-10-18 23:40:37 -0700603 unsigned long flags;
Neil Hormand85f50d2007-10-18 23:40:37 -0700604
605 struct rlimit rlim[RLIM_NLIMITS];
606
Lai Jiangshana6bebbc2008-10-05 00:51:15 +0400607 if (!lock_task_sighand(task, &flags))
Neil Hormand85f50d2007-10-18 23:40:37 -0700608 return 0;
Neil Hormand85f50d2007-10-18 23:40:37 -0700609 memcpy(rlim, task->signal->rlim, sizeof(struct rlimit) * RLIM_NLIMITS);
610 unlock_task_sighand(task, &flags);
Neil Hormand85f50d2007-10-18 23:40:37 -0700611
612 /*
613 * print the file header
614 */
Alexey Dobriyan1c963eb2014-08-08 14:21:37 -0700615 seq_printf(m, "%-25s %-20s %-20s %-10s\n",
Joe Perches25ce3192015-04-15 16:18:17 -0700616 "Limit", "Soft Limit", "Hard Limit", "Units");
Neil Hormand85f50d2007-10-18 23:40:37 -0700617
618 for (i = 0; i < RLIM_NLIMITS; i++) {
619 if (rlim[i].rlim_cur == RLIM_INFINITY)
Alexey Dobriyan1c963eb2014-08-08 14:21:37 -0700620 seq_printf(m, "%-25s %-20s ",
Joe Perches25ce3192015-04-15 16:18:17 -0700621 lnames[i].name, "unlimited");
Neil Hormand85f50d2007-10-18 23:40:37 -0700622 else
Alexey Dobriyan1c963eb2014-08-08 14:21:37 -0700623 seq_printf(m, "%-25s %-20lu ",
Joe Perches25ce3192015-04-15 16:18:17 -0700624 lnames[i].name, rlim[i].rlim_cur);
Neil Hormand85f50d2007-10-18 23:40:37 -0700625
626 if (rlim[i].rlim_max == RLIM_INFINITY)
Alexey Dobriyan1c963eb2014-08-08 14:21:37 -0700627 seq_printf(m, "%-20s ", "unlimited");
Neil Hormand85f50d2007-10-18 23:40:37 -0700628 else
Alexey Dobriyan1c963eb2014-08-08 14:21:37 -0700629 seq_printf(m, "%-20lu ", rlim[i].rlim_max);
Neil Hormand85f50d2007-10-18 23:40:37 -0700630
631 if (lnames[i].unit)
Alexey Dobriyan1c963eb2014-08-08 14:21:37 -0700632 seq_printf(m, "%-10s\n", lnames[i].unit);
Neil Hormand85f50d2007-10-18 23:40:37 -0700633 else
Alexey Dobriyan1c963eb2014-08-08 14:21:37 -0700634 seq_putc(m, '\n');
Neil Hormand85f50d2007-10-18 23:40:37 -0700635 }
636
Alexey Dobriyan1c963eb2014-08-08 14:21:37 -0700637 return 0;
Neil Hormand85f50d2007-10-18 23:40:37 -0700638}
639
Roland McGrathebcb6732008-07-25 19:46:00 -0700640#ifdef CONFIG_HAVE_ARCH_TRACEHOOK
Alexey Dobriyan09d93bd2014-08-08 14:21:39 -0700641static int proc_pid_syscall(struct seq_file *m, struct pid_namespace *ns,
642 struct pid *pid, struct task_struct *task)
Roland McGrathebcb6732008-07-25 19:46:00 -0700643{
644 long nr;
645 unsigned long args[6], sp, pc;
Joe Perches25ce3192015-04-15 16:18:17 -0700646 int res;
647
648 res = lock_trace(task);
Al Viroa9712bc2011-03-23 15:52:50 -0400649 if (res)
650 return res;
Roland McGrathebcb6732008-07-25 19:46:00 -0700651
652 if (task_current_syscall(task, &nr, args, 6, &sp, &pc))
Alexey Dobriyan09d93bd2014-08-08 14:21:39 -0700653 seq_puts(m, "running\n");
Al Viroa9712bc2011-03-23 15:52:50 -0400654 else if (nr < 0)
Alexey Dobriyan09d93bd2014-08-08 14:21:39 -0700655 seq_printf(m, "%ld 0x%lx 0x%lx\n", nr, sp, pc);
Al Viroa9712bc2011-03-23 15:52:50 -0400656 else
Alexey Dobriyan09d93bd2014-08-08 14:21:39 -0700657 seq_printf(m,
Roland McGrathebcb6732008-07-25 19:46:00 -0700658 "%ld 0x%lx 0x%lx 0x%lx 0x%lx 0x%lx 0x%lx 0x%lx 0x%lx\n",
659 nr,
660 args[0], args[1], args[2], args[3], args[4], args[5],
661 sp, pc);
Al Viroa9712bc2011-03-23 15:52:50 -0400662 unlock_trace(task);
Joe Perches25ce3192015-04-15 16:18:17 -0700663
664 return 0;
Roland McGrathebcb6732008-07-25 19:46:00 -0700665}
666#endif /* CONFIG_HAVE_ARCH_TRACEHOOK */
667
Linus Torvalds1da177e2005-04-16 15:20:36 -0700668/************************************************************************/
669/* Here the fs part begins */
670/************************************************************************/
671
672/* permission checks */
Eric W. Biederman778c1142006-06-26 00:25:58 -0700673static int proc_fd_access_allowed(struct inode *inode)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700674{
Eric W. Biederman778c1142006-06-26 00:25:58 -0700675 struct task_struct *task;
676 int allowed = 0;
Eric W. Biedermandf26c402006-06-26 00:25:59 -0700677 /* Allow access to a task's file descriptors if it is us or we
678 * may use ptrace attach to the process and find out that
679 * information.
Eric W. Biederman778c1142006-06-26 00:25:58 -0700680 */
681 task = get_proc_task(inode);
Eric W. Biedermandf26c402006-06-26 00:25:59 -0700682 if (task) {
Jann Horncaaee622016-01-20 15:00:04 -0800683 allowed = ptrace_may_access(task, PTRACE_MODE_READ_FSCREDS);
Eric W. Biederman778c1142006-06-26 00:25:58 -0700684 put_task_struct(task);
Eric W. Biedermandf26c402006-06-26 00:25:59 -0700685 }
Eric W. Biederman778c1142006-06-26 00:25:58 -0700686 return allowed;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700687}
688
Eric W. Biederman6b4e3062010-03-07 16:41:34 -0800689int proc_setattr(struct dentry *dentry, struct iattr *attr)
Linus Torvalds6d76fa52006-07-15 12:26:45 -0700690{
691 int error;
David Howells2b0143b2015-03-17 22:25:59 +0000692 struct inode *inode = d_inode(dentry);
Linus Torvalds6d76fa52006-07-15 12:26:45 -0700693
694 if (attr->ia_valid & ATTR_MODE)
695 return -EPERM;
696
Jan Kara31051c82016-05-26 16:55:18 +0200697 error = setattr_prepare(dentry, attr);
Christoph Hellwig10257742010-06-04 11:30:02 +0200698 if (error)
699 return error;
700
Christoph Hellwig10257742010-06-04 11:30:02 +0200701 setattr_copy(inode, attr);
702 mark_inode_dirty(inode);
703 return 0;
Linus Torvalds6d76fa52006-07-15 12:26:45 -0700704}
705
Vasiliy Kulikov04996802012-01-10 15:11:31 -0800706/*
707 * May current process learn task's sched/cmdline info (for hide_pid_min=1)
708 * or euid/egid (for hide_pid_min=2)?
709 */
710static bool has_pid_permissions(struct pid_namespace *pid,
711 struct task_struct *task,
712 int hide_pid_min)
713{
714 if (pid->hide_pid < hide_pid_min)
715 return true;
716 if (in_group_p(pid->pid_gid))
717 return true;
Jann Horncaaee622016-01-20 15:00:04 -0800718 return ptrace_may_access(task, PTRACE_MODE_READ_FSCREDS);
Vasiliy Kulikov04996802012-01-10 15:11:31 -0800719}
720
721
722static int proc_pid_permission(struct inode *inode, int mask)
723{
724 struct pid_namespace *pid = inode->i_sb->s_fs_info;
725 struct task_struct *task;
726 bool has_perms;
727
728 task = get_proc_task(inode);
Xiaotian Fenga2ef9902012-01-12 17:17:08 -0800729 if (!task)
730 return -ESRCH;
Vasiliy Kulikov04996802012-01-10 15:11:31 -0800731 has_perms = has_pid_permissions(pid, task, 1);
732 put_task_struct(task);
733
734 if (!has_perms) {
735 if (pid->hide_pid == 2) {
736 /*
737 * Let's make getdents(), stat(), and open()
738 * consistent with each other. If a process
739 * may not stat() a file, it shouldn't be seen
740 * in procfs at all.
741 */
742 return -ENOENT;
743 }
744
745 return -EPERM;
746 }
747 return generic_permission(inode, mask);
748}
749
750
751
Arjan van de Venc5ef1c42007-02-12 00:55:40 -0800752static const struct inode_operations proc_def_inode_operations = {
Linus Torvalds6d76fa52006-07-15 12:26:45 -0700753 .setattr = proc_setattr,
754};
755
Eric W. Biedermanbe614082008-02-08 04:18:30 -0800756static int proc_single_show(struct seq_file *m, void *v)
757{
758 struct inode *inode = m->private;
759 struct pid_namespace *ns;
760 struct pid *pid;
761 struct task_struct *task;
762 int ret;
763
764 ns = inode->i_sb->s_fs_info;
765 pid = proc_pid(inode);
766 task = get_pid_task(pid, PIDTYPE_PID);
767 if (!task)
768 return -ESRCH;
769
770 ret = PROC_I(inode)->op.proc_show(m, ns, pid, task);
771
772 put_task_struct(task);
773 return ret;
774}
775
776static int proc_single_open(struct inode *inode, struct file *filp)
777{
Jovi Zhangc6a34052011-01-12 17:00:34 -0800778 return single_open(filp, proc_single_show, inode);
Eric W. Biedermanbe614082008-02-08 04:18:30 -0800779}
780
781static const struct file_operations proc_single_file_operations = {
782 .open = proc_single_open,
783 .read = seq_read,
784 .llseek = seq_lseek,
785 .release = single_release,
786};
787
Oleg Nesterov5381e162014-10-09 15:25:24 -0700788
789struct mm_struct *proc_mem_open(struct inode *inode, unsigned int mode)
790{
791 struct task_struct *task = get_proc_task(inode);
792 struct mm_struct *mm = ERR_PTR(-ESRCH);
793
794 if (task) {
Jann Horncaaee622016-01-20 15:00:04 -0800795 mm = mm_access(task, mode | PTRACE_MODE_FSCREDS);
Oleg Nesterov5381e162014-10-09 15:25:24 -0700796 put_task_struct(task);
797
798 if (!IS_ERR_OR_NULL(mm)) {
799 /* ensure this mm_struct can't be freed */
800 atomic_inc(&mm->mm_count);
801 /* but do not pin its memory */
802 mmput(mm);
803 }
804 }
805
806 return mm;
807}
808
Cong Wangb409e572012-05-31 16:26:17 -0700809static int __mem_open(struct inode *inode, struct file *file, unsigned int mode)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700810{
Oleg Nesterov5381e162014-10-09 15:25:24 -0700811 struct mm_struct *mm = proc_mem_open(inode, mode);
Linus Torvaldse2683372012-01-17 15:21:19 -0800812
813 if (IS_ERR(mm))
814 return PTR_ERR(mm);
815
Linus Torvaldse2683372012-01-17 15:21:19 -0800816 file->private_data = mm;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700817 return 0;
818}
819
Cong Wangb409e572012-05-31 16:26:17 -0700820static int mem_open(struct inode *inode, struct file *file)
821{
Djalal Harounibc452b42012-07-30 14:42:28 -0700822 int ret = __mem_open(inode, file, PTRACE_MODE_ATTACH);
823
824 /* OK to pass negative loff_t, we can catch out-of-range */
825 file->f_mode |= FMODE_UNSIGNED_OFFSET;
826
827 return ret;
Cong Wangb409e572012-05-31 16:26:17 -0700828}
829
Oleg Nesterov572d34b2012-01-31 17:14:54 +0100830static ssize_t mem_rw(struct file *file, char __user *buf,
831 size_t count, loff_t *ppos, int write)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700832{
Linus Torvaldse2683372012-01-17 15:21:19 -0800833 struct mm_struct *mm = file->private_data;
Oleg Nesterov572d34b2012-01-31 17:14:54 +0100834 unsigned long addr = *ppos;
835 ssize_t copied;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700836 char *page;
Linus Torvalds272ddc82016-10-24 19:00:44 -0700837 unsigned int flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700838
Linus Torvaldse2683372012-01-17 15:21:19 -0800839 if (!mm)
840 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700841
Mel Gormane12ba742007-10-16 01:25:52 -0700842 page = (char *)__get_free_page(GFP_TEMPORARY);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700843 if (!page)
Linus Torvaldse2683372012-01-17 15:21:19 -0800844 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700845
Frederik Deweerdtf7ca54f2006-09-29 02:01:02 -0700846 copied = 0;
Oleg Nesterov6d08f2c2012-01-31 17:15:11 +0100847 if (!atomic_inc_not_zero(&mm->mm_users))
848 goto free;
849
Linus Torvalds272ddc82016-10-24 19:00:44 -0700850 /* Maybe we should limit FOLL_FORCE to actual ptrace users? */
851 flags = FOLL_FORCE;
Lorenzo Stoakes6347e8d2016-10-13 01:20:19 +0100852 if (write)
853 flags |= FOLL_WRITE;
854
Linus Torvalds1da177e2005-04-16 15:20:36 -0700855 while (count > 0) {
Oleg Nesterov572d34b2012-01-31 17:14:54 +0100856 int this_len = min_t(int, count, PAGE_SIZE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700857
Oleg Nesterov572d34b2012-01-31 17:14:54 +0100858 if (write && copy_from_user(page, buf, this_len)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700859 copied = -EFAULT;
860 break;
861 }
Oleg Nesterov572d34b2012-01-31 17:14:54 +0100862
Lorenzo Stoakes6347e8d2016-10-13 01:20:19 +0100863 this_len = access_remote_vm(mm, addr, page, this_len, flags);
Oleg Nesterov572d34b2012-01-31 17:14:54 +0100864 if (!this_len) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700865 if (!copied)
866 copied = -EIO;
867 break;
868 }
Oleg Nesterov572d34b2012-01-31 17:14:54 +0100869
870 if (!write && copy_to_user(buf, page, this_len)) {
871 copied = -EFAULT;
872 break;
873 }
874
875 buf += this_len;
876 addr += this_len;
877 copied += this_len;
878 count -= this_len;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700879 }
Oleg Nesterov572d34b2012-01-31 17:14:54 +0100880 *ppos = addr;
KOSAKI Motohiro30cd8902011-05-26 16:25:52 -0700881
Oleg Nesterov6d08f2c2012-01-31 17:15:11 +0100882 mmput(mm);
883free:
KOSAKI Motohiro30cd8902011-05-26 16:25:52 -0700884 free_page((unsigned long) page);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700885 return copied;
886}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700887
Oleg Nesterov572d34b2012-01-31 17:14:54 +0100888static ssize_t mem_read(struct file *file, char __user *buf,
889 size_t count, loff_t *ppos)
890{
891 return mem_rw(file, buf, count, ppos, 0);
892}
893
894static ssize_t mem_write(struct file *file, const char __user *buf,
895 size_t count, loff_t *ppos)
896{
897 return mem_rw(file, (char __user*)buf, count, ppos, 1);
898}
899
Matt Mackall85863e42008-02-04 22:29:04 -0800900loff_t mem_lseek(struct file *file, loff_t offset, int orig)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700901{
902 switch (orig) {
903 case 0:
904 file->f_pos = offset;
905 break;
906 case 1:
907 file->f_pos += offset;
908 break;
909 default:
910 return -EINVAL;
911 }
912 force_successful_syscall_return();
913 return file->f_pos;
914}
915
Linus Torvaldse2683372012-01-17 15:21:19 -0800916static int mem_release(struct inode *inode, struct file *file)
917{
918 struct mm_struct *mm = file->private_data;
Oleg Nesterov71879d32012-01-31 17:14:38 +0100919 if (mm)
Oleg Nesterov6d08f2c2012-01-31 17:15:11 +0100920 mmdrop(mm);
Linus Torvaldse2683372012-01-17 15:21:19 -0800921 return 0;
922}
923
Arjan van de Ven00977a52007-02-12 00:55:34 -0800924static const struct file_operations proc_mem_operations = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700925 .llseek = mem_lseek,
926 .read = mem_read,
927 .write = mem_write,
928 .open = mem_open,
Linus Torvaldse2683372012-01-17 15:21:19 -0800929 .release = mem_release,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700930};
931
Cong Wangb409e572012-05-31 16:26:17 -0700932static int environ_open(struct inode *inode, struct file *file)
933{
934 return __mem_open(inode, file, PTRACE_MODE_READ);
935}
936
James Pearson315e28c2007-10-16 23:30:17 -0700937static ssize_t environ_read(struct file *file, char __user *buf,
938 size_t count, loff_t *ppos)
939{
James Pearson315e28c2007-10-16 23:30:17 -0700940 char *page;
941 unsigned long src = *ppos;
Cong Wangb409e572012-05-31 16:26:17 -0700942 int ret = 0;
943 struct mm_struct *mm = file->private_data;
Mateusz Guzika3b609e2016-01-20 15:01:05 -0800944 unsigned long env_start, env_end;
James Pearson315e28c2007-10-16 23:30:17 -0700945
Mathias Krause8148a732016-05-05 16:22:26 -0700946 /* Ensure the process spawned far enough to have an environment. */
947 if (!mm || !mm->env_end)
Cong Wangb409e572012-05-31 16:26:17 -0700948 return 0;
James Pearson315e28c2007-10-16 23:30:17 -0700949
James Pearson315e28c2007-10-16 23:30:17 -0700950 page = (char *)__get_free_page(GFP_TEMPORARY);
951 if (!page)
Cong Wangb409e572012-05-31 16:26:17 -0700952 return -ENOMEM;
James Pearson315e28c2007-10-16 23:30:17 -0700953
Al Virod6f64b82011-02-15 22:26:01 -0500954 ret = 0;
Cong Wangb409e572012-05-31 16:26:17 -0700955 if (!atomic_inc_not_zero(&mm->mm_users))
956 goto free;
Mateusz Guzika3b609e2016-01-20 15:01:05 -0800957
958 down_read(&mm->mmap_sem);
959 env_start = mm->env_start;
960 env_end = mm->env_end;
961 up_read(&mm->mmap_sem);
962
James Pearson315e28c2007-10-16 23:30:17 -0700963 while (count > 0) {
Djalal Harounie8905ec2012-07-30 14:42:26 -0700964 size_t this_len, max_len;
965 int retval;
966
Mateusz Guzika3b609e2016-01-20 15:01:05 -0800967 if (src >= (env_end - env_start))
Djalal Harounie8905ec2012-07-30 14:42:26 -0700968 break;
James Pearson315e28c2007-10-16 23:30:17 -0700969
Mateusz Guzika3b609e2016-01-20 15:01:05 -0800970 this_len = env_end - (env_start + src);
James Pearson315e28c2007-10-16 23:30:17 -0700971
Djalal Harounie8905ec2012-07-30 14:42:26 -0700972 max_len = min_t(size_t, PAGE_SIZE, count);
973 this_len = min(max_len, this_len);
James Pearson315e28c2007-10-16 23:30:17 -0700974
Willy Tarreau6f1abf82018-05-11 08:11:44 +0200975 retval = access_remote_vm(mm, (env_start + src), page, this_len, FOLL_ANON);
James Pearson315e28c2007-10-16 23:30:17 -0700976
977 if (retval <= 0) {
978 ret = retval;
979 break;
980 }
981
982 if (copy_to_user(buf, page, retval)) {
983 ret = -EFAULT;
984 break;
985 }
986
987 ret += retval;
988 src += retval;
989 buf += retval;
990 count -= retval;
991 }
992 *ppos = src;
James Pearson315e28c2007-10-16 23:30:17 -0700993 mmput(mm);
Cong Wangb409e572012-05-31 16:26:17 -0700994
995free:
James Pearson315e28c2007-10-16 23:30:17 -0700996 free_page((unsigned long) page);
James Pearson315e28c2007-10-16 23:30:17 -0700997 return ret;
998}
999
1000static const struct file_operations proc_environ_operations = {
Cong Wangb409e572012-05-31 16:26:17 -07001001 .open = environ_open,
James Pearson315e28c2007-10-16 23:30:17 -07001002 .read = environ_read,
Arnd Bergmann87df8422010-03-17 23:06:02 +01001003 .llseek = generic_file_llseek,
Cong Wangb409e572012-05-31 16:26:17 -07001004 .release = mem_release,
James Pearson315e28c2007-10-16 23:30:17 -07001005};
1006
Al Viroc5317162016-10-05 18:43:43 -04001007static int auxv_open(struct inode *inode, struct file *file)
1008{
1009 return __mem_open(inode, file, PTRACE_MODE_READ_FSCREDS);
1010}
1011
1012static ssize_t auxv_read(struct file *file, char __user *buf,
1013 size_t count, loff_t *ppos)
1014{
1015 struct mm_struct *mm = file->private_data;
1016 unsigned int nwords = 0;
Leon Yu06b28492016-10-27 17:46:50 -07001017
1018 if (!mm)
1019 return 0;
Al Viroc5317162016-10-05 18:43:43 -04001020 do {
1021 nwords += 2;
1022 } while (mm->saved_auxv[nwords - 2] != 0); /* AT_NULL */
1023 return simple_read_from_buffer(buf, count, ppos, mm->saved_auxv,
1024 nwords * sizeof(mm->saved_auxv[0]));
1025}
1026
1027static const struct file_operations proc_auxv_operations = {
1028 .open = auxv_open,
1029 .read = auxv_read,
1030 .llseek = generic_file_llseek,
1031 .release = mem_release,
1032};
1033
David Rientjesfa0cbbf2012-11-12 17:53:04 -08001034static ssize_t oom_adj_read(struct file *file, char __user *buf, size_t count,
1035 loff_t *ppos)
1036{
Al Viro496ad9a2013-01-23 17:07:38 -05001037 struct task_struct *task = get_proc_task(file_inode(file));
David Rientjesfa0cbbf2012-11-12 17:53:04 -08001038 char buffer[PROC_NUMBUF];
1039 int oom_adj = OOM_ADJUST_MIN;
1040 size_t len;
David Rientjesfa0cbbf2012-11-12 17:53:04 -08001041
1042 if (!task)
1043 return -ESRCH;
Michal Hockof913da52016-07-28 15:44:37 -07001044 if (task->signal->oom_score_adj == OOM_SCORE_ADJ_MAX)
1045 oom_adj = OOM_ADJUST_MAX;
1046 else
1047 oom_adj = (task->signal->oom_score_adj * -OOM_DISABLE) /
1048 OOM_SCORE_ADJ_MAX;
David Rientjesfa0cbbf2012-11-12 17:53:04 -08001049 put_task_struct(task);
1050 len = snprintf(buffer, sizeof(buffer), "%d\n", oom_adj);
1051 return simple_read_from_buffer(buf, count, ppos, buffer, len);
1052}
1053
Michal Hocko1d5f0ac2016-07-28 15:44:40 -07001054static int __set_oom_adj(struct file *file, int oom_adj, bool legacy)
1055{
1056 static DEFINE_MUTEX(oom_adj_mutex);
Michal Hocko44a70ade2016-07-28 15:44:43 -07001057 struct mm_struct *mm = NULL;
Michal Hocko1d5f0ac2016-07-28 15:44:40 -07001058 struct task_struct *task;
1059 int err = 0;
1060
1061 task = get_proc_task(file_inode(file));
1062 if (!task)
1063 return -ESRCH;
1064
1065 mutex_lock(&oom_adj_mutex);
1066 if (legacy) {
1067 if (oom_adj < task->signal->oom_score_adj &&
1068 !capable(CAP_SYS_RESOURCE)) {
1069 err = -EACCES;
1070 goto err_unlock;
1071 }
1072 /*
1073 * /proc/pid/oom_adj is provided for legacy purposes, ask users to use
1074 * /proc/pid/oom_score_adj instead.
1075 */
1076 pr_warn_once("%s (%d): /proc/%d/oom_adj is deprecated, please use /proc/%d/oom_score_adj instead.\n",
1077 current->comm, task_pid_nr(current), task_pid_nr(task),
1078 task_pid_nr(task));
1079 } else {
1080 if ((short)oom_adj < task->signal->oom_score_adj_min &&
1081 !capable(CAP_SYS_RESOURCE)) {
1082 err = -EACCES;
1083 goto err_unlock;
1084 }
1085 }
1086
Michal Hocko44a70ade2016-07-28 15:44:43 -07001087 /*
1088 * Make sure we will check other processes sharing the mm if this is
1089 * not vfrok which wants its own oom_score_adj.
1090 * pin the mm so it doesn't go away and get reused after task_unlock
1091 */
1092 if (!task->vfork_done) {
1093 struct task_struct *p = find_lock_task_mm(task);
1094
1095 if (p) {
1096 if (atomic_read(&p->mm->mm_users) > 1) {
1097 mm = p->mm;
1098 atomic_inc(&mm->mm_count);
1099 }
1100 task_unlock(p);
1101 }
1102 }
1103
Michal Hocko1d5f0ac2016-07-28 15:44:40 -07001104 task->signal->oom_score_adj = oom_adj;
1105 if (!legacy && has_capability_noaudit(current, CAP_SYS_RESOURCE))
1106 task->signal->oom_score_adj_min = (short)oom_adj;
1107 trace_oom_score_adj_update(task);
Michal Hocko44a70ade2016-07-28 15:44:43 -07001108
1109 if (mm) {
1110 struct task_struct *p;
1111
1112 rcu_read_lock();
1113 for_each_process(p) {
1114 if (same_thread_group(task, p))
1115 continue;
1116
1117 /* do not touch kernel threads or the global init */
1118 if (p->flags & PF_KTHREAD || is_global_init(p))
1119 continue;
1120
1121 task_lock(p);
1122 if (!p->vfork_done && process_shares_mm(p, mm)) {
1123 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",
1124 task_pid_nr(p), p->comm,
1125 p->signal->oom_score_adj, oom_adj,
1126 task_pid_nr(task), task->comm);
1127 p->signal->oom_score_adj = oom_adj;
1128 if (!legacy && has_capability_noaudit(current, CAP_SYS_RESOURCE))
1129 p->signal->oom_score_adj_min = (short)oom_adj;
1130 }
1131 task_unlock(p);
1132 }
1133 rcu_read_unlock();
1134 mmdrop(mm);
1135 }
Michal Hocko1d5f0ac2016-07-28 15:44:40 -07001136err_unlock:
1137 mutex_unlock(&oom_adj_mutex);
1138 put_task_struct(task);
1139 return err;
1140}
Michal Hockof913da52016-07-28 15:44:37 -07001141
David Rientjesb72bdfa2015-11-05 18:50:32 -08001142/*
1143 * /proc/pid/oom_adj exists solely for backwards compatibility with previous
1144 * kernels. The effective policy is defined by oom_score_adj, which has a
1145 * different scale: oom_adj grew exponentially and oom_score_adj grows linearly.
1146 * Values written to oom_adj are simply mapped linearly to oom_score_adj.
1147 * Processes that become oom disabled via oom_adj will still be oom disabled
1148 * with this implementation.
1149 *
1150 * oom_adj cannot be removed since existing userspace binaries use it.
1151 */
David Rientjesfa0cbbf2012-11-12 17:53:04 -08001152static ssize_t oom_adj_write(struct file *file, const char __user *buf,
1153 size_t count, loff_t *ppos)
1154{
David Rientjesfa0cbbf2012-11-12 17:53:04 -08001155 char buffer[PROC_NUMBUF];
1156 int oom_adj;
David Rientjesfa0cbbf2012-11-12 17:53:04 -08001157 int err;
1158
1159 memset(buffer, 0, sizeof(buffer));
1160 if (count > sizeof(buffer) - 1)
1161 count = sizeof(buffer) - 1;
1162 if (copy_from_user(buffer, buf, count)) {
1163 err = -EFAULT;
1164 goto out;
1165 }
1166
1167 err = kstrtoint(strstrip(buffer), 0, &oom_adj);
1168 if (err)
1169 goto out;
1170 if ((oom_adj < OOM_ADJUST_MIN || oom_adj > OOM_ADJUST_MAX) &&
1171 oom_adj != OOM_DISABLE) {
1172 err = -EINVAL;
1173 goto out;
1174 }
1175
David Rientjesfa0cbbf2012-11-12 17:53:04 -08001176 /*
1177 * Scale /proc/pid/oom_score_adj appropriately ensuring that a maximum
1178 * value is always attainable.
1179 */
1180 if (oom_adj == OOM_ADJUST_MAX)
1181 oom_adj = OOM_SCORE_ADJ_MAX;
1182 else
1183 oom_adj = (oom_adj * OOM_SCORE_ADJ_MAX) / -OOM_DISABLE;
1184
Michal Hocko1d5f0ac2016-07-28 15:44:40 -07001185 err = __set_oom_adj(file, oom_adj, true);
David Rientjesfa0cbbf2012-11-12 17:53:04 -08001186out:
1187 return err < 0 ? err : count;
1188}
1189
1190static const struct file_operations proc_oom_adj_operations = {
1191 .read = oom_adj_read,
1192 .write = oom_adj_write,
1193 .llseek = generic_file_llseek,
1194};
1195
David Rientjesa63d83f2010-08-09 17:19:46 -07001196static ssize_t oom_score_adj_read(struct file *file, char __user *buf,
1197 size_t count, loff_t *ppos)
1198{
Al Viro496ad9a2013-01-23 17:07:38 -05001199 struct task_struct *task = get_proc_task(file_inode(file));
David Rientjesa63d83f2010-08-09 17:19:46 -07001200 char buffer[PROC_NUMBUF];
David Rientjesa9c58b902012-12-11 16:02:54 -08001201 short oom_score_adj = OOM_SCORE_ADJ_MIN;
David Rientjesa63d83f2010-08-09 17:19:46 -07001202 size_t len;
1203
1204 if (!task)
1205 return -ESRCH;
Michal Hockof913da52016-07-28 15:44:37 -07001206 oom_score_adj = task->signal->oom_score_adj;
David Rientjesa63d83f2010-08-09 17:19:46 -07001207 put_task_struct(task);
David Rientjesa9c58b902012-12-11 16:02:54 -08001208 len = snprintf(buffer, sizeof(buffer), "%hd\n", oom_score_adj);
David Rientjesa63d83f2010-08-09 17:19:46 -07001209 return simple_read_from_buffer(buf, count, ppos, buffer, len);
1210}
1211
1212static ssize_t oom_score_adj_write(struct file *file, const char __user *buf,
1213 size_t count, loff_t *ppos)
1214{
David Rientjesa63d83f2010-08-09 17:19:46 -07001215 char buffer[PROC_NUMBUF];
Alexey Dobriyan0a8cb8e2011-05-26 16:25:50 -07001216 int oom_score_adj;
David Rientjesa63d83f2010-08-09 17:19:46 -07001217 int err;
1218
1219 memset(buffer, 0, sizeof(buffer));
1220 if (count > sizeof(buffer) - 1)
1221 count = sizeof(buffer) - 1;
David Rientjes723548b2010-10-26 14:21:25 -07001222 if (copy_from_user(buffer, buf, count)) {
1223 err = -EFAULT;
1224 goto out;
1225 }
David Rientjesa63d83f2010-08-09 17:19:46 -07001226
Alexey Dobriyan0a8cb8e2011-05-26 16:25:50 -07001227 err = kstrtoint(strstrip(buffer), 0, &oom_score_adj);
David Rientjesa63d83f2010-08-09 17:19:46 -07001228 if (err)
David Rientjes723548b2010-10-26 14:21:25 -07001229 goto out;
David Rientjesa63d83f2010-08-09 17:19:46 -07001230 if (oom_score_adj < OOM_SCORE_ADJ_MIN ||
David Rientjes723548b2010-10-26 14:21:25 -07001231 oom_score_adj > OOM_SCORE_ADJ_MAX) {
1232 err = -EINVAL;
1233 goto out;
1234 }
David Rientjesa63d83f2010-08-09 17:19:46 -07001235
Michal Hocko1d5f0ac2016-07-28 15:44:40 -07001236 err = __set_oom_adj(file, oom_score_adj, false);
David Rientjes723548b2010-10-26 14:21:25 -07001237out:
1238 return err < 0 ? err : count;
David Rientjesa63d83f2010-08-09 17:19:46 -07001239}
1240
1241static const struct file_operations proc_oom_score_adj_operations = {
1242 .read = oom_score_adj_read,
1243 .write = oom_score_adj_write,
Arnd Bergmann6038f372010-08-15 18:52:59 +02001244 .llseek = default_llseek,
David Rientjesa63d83f2010-08-09 17:19:46 -07001245};
1246
Linus Torvalds1da177e2005-04-16 15:20:36 -07001247#ifdef CONFIG_AUDITSYSCALL
1248#define TMPBUFLEN 21
1249static ssize_t proc_loginuid_read(struct file * file, char __user * buf,
1250 size_t count, loff_t *ppos)
1251{
Al Viro496ad9a2013-01-23 17:07:38 -05001252 struct inode * inode = file_inode(file);
Eric W. Biederman99f89552006-06-26 00:25:55 -07001253 struct task_struct *task = get_proc_task(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001254 ssize_t length;
1255 char tmpbuf[TMPBUFLEN];
1256
Eric W. Biederman99f89552006-06-26 00:25:55 -07001257 if (!task)
1258 return -ESRCH;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001259 length = scnprintf(tmpbuf, TMPBUFLEN, "%u",
Eric W. Biedermane1760bd2012-09-10 22:39:43 -07001260 from_kuid(file->f_cred->user_ns,
1261 audit_get_loginuid(task)));
Eric W. Biederman99f89552006-06-26 00:25:55 -07001262 put_task_struct(task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001263 return simple_read_from_buffer(buf, count, ppos, tmpbuf, length);
1264}
1265
1266static ssize_t proc_loginuid_write(struct file * file, const char __user * buf,
1267 size_t count, loff_t *ppos)
1268{
Al Viro496ad9a2013-01-23 17:07:38 -05001269 struct inode * inode = file_inode(file);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001270 uid_t loginuid;
Eric W. Biedermane1760bd2012-09-10 22:39:43 -07001271 kuid_t kloginuid;
Alexey Dobriyan774636e2015-09-09 15:36:59 -07001272 int rv;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001273
Paul E. McKenney7dc52152010-02-22 17:04:52 -08001274 rcu_read_lock();
1275 if (current != pid_task(proc_pid(inode), PIDTYPE_PID)) {
1276 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001277 return -EPERM;
Paul E. McKenney7dc52152010-02-22 17:04:52 -08001278 }
1279 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001280
Linus Torvalds1da177e2005-04-16 15:20:36 -07001281 if (*ppos != 0) {
1282 /* No partial writes. */
1283 return -EINVAL;
1284 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001285
Alexey Dobriyan774636e2015-09-09 15:36:59 -07001286 rv = kstrtou32_from_user(buf, count, 10, &loginuid);
1287 if (rv < 0)
1288 return rv;
Eric Paris81407c82013-05-24 09:49:14 -04001289
1290 /* is userspace tring to explicitly UNSET the loginuid? */
1291 if (loginuid == AUDIT_UID_UNSET) {
1292 kloginuid = INVALID_UID;
1293 } else {
1294 kloginuid = make_kuid(file->f_cred->user_ns, loginuid);
Alexey Dobriyan774636e2015-09-09 15:36:59 -07001295 if (!uid_valid(kloginuid))
1296 return -EINVAL;
Eric W. Biedermane1760bd2012-09-10 22:39:43 -07001297 }
1298
Alexey Dobriyan774636e2015-09-09 15:36:59 -07001299 rv = audit_set_loginuid(kloginuid);
1300 if (rv < 0)
1301 return rv;
1302 return count;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001303}
1304
Arjan van de Ven00977a52007-02-12 00:55:34 -08001305static const struct file_operations proc_loginuid_operations = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001306 .read = proc_loginuid_read,
1307 .write = proc_loginuid_write,
Arnd Bergmann87df8422010-03-17 23:06:02 +01001308 .llseek = generic_file_llseek,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001309};
Eric Paris1e0bd752008-03-13 08:15:31 -04001310
1311static ssize_t proc_sessionid_read(struct file * file, char __user * buf,
1312 size_t count, loff_t *ppos)
1313{
Al Viro496ad9a2013-01-23 17:07:38 -05001314 struct inode * inode = file_inode(file);
Eric Paris1e0bd752008-03-13 08:15:31 -04001315 struct task_struct *task = get_proc_task(inode);
1316 ssize_t length;
1317 char tmpbuf[TMPBUFLEN];
1318
1319 if (!task)
1320 return -ESRCH;
1321 length = scnprintf(tmpbuf, TMPBUFLEN, "%u",
1322 audit_get_sessionid(task));
1323 put_task_struct(task);
1324 return simple_read_from_buffer(buf, count, ppos, tmpbuf, length);
1325}
1326
1327static const struct file_operations proc_sessionid_operations = {
1328 .read = proc_sessionid_read,
Arnd Bergmann87df8422010-03-17 23:06:02 +01001329 .llseek = generic_file_llseek,
Eric Paris1e0bd752008-03-13 08:15:31 -04001330};
Linus Torvalds1da177e2005-04-16 15:20:36 -07001331#endif
1332
Akinobu Mitaf4f154f2006-12-08 02:39:47 -08001333#ifdef CONFIG_FAULT_INJECTION
1334static ssize_t proc_fault_inject_read(struct file * file, char __user * buf,
1335 size_t count, loff_t *ppos)
1336{
Al Viro496ad9a2013-01-23 17:07:38 -05001337 struct task_struct *task = get_proc_task(file_inode(file));
Akinobu Mitaf4f154f2006-12-08 02:39:47 -08001338 char buffer[PROC_NUMBUF];
1339 size_t len;
1340 int make_it_fail;
Akinobu Mitaf4f154f2006-12-08 02:39:47 -08001341
1342 if (!task)
1343 return -ESRCH;
1344 make_it_fail = task->make_it_fail;
1345 put_task_struct(task);
1346
1347 len = snprintf(buffer, sizeof(buffer), "%i\n", make_it_fail);
Akinobu Mita0c28f282007-05-08 00:31:41 -07001348
1349 return simple_read_from_buffer(buf, count, ppos, buffer, len);
Akinobu Mitaf4f154f2006-12-08 02:39:47 -08001350}
1351
1352static ssize_t proc_fault_inject_write(struct file * file,
1353 const char __user * buf, size_t count, loff_t *ppos)
1354{
1355 struct task_struct *task;
Alexey Dobriyan774636e2015-09-09 15:36:59 -07001356 char buffer[PROC_NUMBUF];
Akinobu Mitaf4f154f2006-12-08 02:39:47 -08001357 int make_it_fail;
Alexey Dobriyan774636e2015-09-09 15:36:59 -07001358 int rv;
Akinobu Mitaf4f154f2006-12-08 02:39:47 -08001359
1360 if (!capable(CAP_SYS_RESOURCE))
1361 return -EPERM;
1362 memset(buffer, 0, sizeof(buffer));
1363 if (count > sizeof(buffer) - 1)
1364 count = sizeof(buffer) - 1;
1365 if (copy_from_user(buffer, buf, count))
1366 return -EFAULT;
Alexey Dobriyan774636e2015-09-09 15:36:59 -07001367 rv = kstrtoint(strstrip(buffer), 0, &make_it_fail);
1368 if (rv < 0)
1369 return rv;
Dave Jones16caed32014-04-07 15:39:15 -07001370 if (make_it_fail < 0 || make_it_fail > 1)
1371 return -EINVAL;
1372
Al Viro496ad9a2013-01-23 17:07:38 -05001373 task = get_proc_task(file_inode(file));
Akinobu Mitaf4f154f2006-12-08 02:39:47 -08001374 if (!task)
1375 return -ESRCH;
1376 task->make_it_fail = make_it_fail;
1377 put_task_struct(task);
Vincent Licba8aaf2009-09-22 16:45:38 -07001378
1379 return count;
Akinobu Mitaf4f154f2006-12-08 02:39:47 -08001380}
1381
Arjan van de Ven00977a52007-02-12 00:55:34 -08001382static const struct file_operations proc_fault_inject_operations = {
Akinobu Mitaf4f154f2006-12-08 02:39:47 -08001383 .read = proc_fault_inject_read,
1384 .write = proc_fault_inject_write,
Arnd Bergmann87df8422010-03-17 23:06:02 +01001385 .llseek = generic_file_llseek,
Akinobu Mitaf4f154f2006-12-08 02:39:47 -08001386};
1387#endif
1388
Arjan van de Ven97455122008-01-25 21:08:34 +01001389
Ingo Molnar43ae34c2007-07-09 18:52:00 +02001390#ifdef CONFIG_SCHED_DEBUG
1391/*
1392 * Print out various scheduling related per-task fields:
1393 */
1394static int sched_show(struct seq_file *m, void *v)
1395{
1396 struct inode *inode = m->private;
1397 struct task_struct *p;
1398
Ingo Molnar43ae34c2007-07-09 18:52:00 +02001399 p = get_proc_task(inode);
1400 if (!p)
1401 return -ESRCH;
1402 proc_sched_show_task(p, m);
1403
1404 put_task_struct(p);
1405
1406 return 0;
1407}
1408
1409static ssize_t
1410sched_write(struct file *file, const char __user *buf,
1411 size_t count, loff_t *offset)
1412{
Al Viro496ad9a2013-01-23 17:07:38 -05001413 struct inode *inode = file_inode(file);
Ingo Molnar43ae34c2007-07-09 18:52:00 +02001414 struct task_struct *p;
1415
Ingo Molnar43ae34c2007-07-09 18:52:00 +02001416 p = get_proc_task(inode);
1417 if (!p)
1418 return -ESRCH;
1419 proc_sched_set_task(p);
1420
1421 put_task_struct(p);
1422
1423 return count;
1424}
1425
1426static int sched_open(struct inode *inode, struct file *filp)
1427{
Jovi Zhangc6a34052011-01-12 17:00:34 -08001428 return single_open(filp, sched_show, inode);
Ingo Molnar43ae34c2007-07-09 18:52:00 +02001429}
1430
1431static const struct file_operations proc_pid_sched_operations = {
1432 .open = sched_open,
1433 .read = seq_read,
1434 .write = sched_write,
1435 .llseek = seq_lseek,
Alexey Dobriyan5ea473a2007-07-31 00:38:50 -07001436 .release = single_release,
Ingo Molnar43ae34c2007-07-09 18:52:00 +02001437};
1438
1439#endif
1440
Mike Galbraith5091faa2010-11-30 14:18:03 +01001441#ifdef CONFIG_SCHED_AUTOGROUP
1442/*
1443 * Print out autogroup related information:
1444 */
1445static int sched_autogroup_show(struct seq_file *m, void *v)
1446{
1447 struct inode *inode = m->private;
1448 struct task_struct *p;
1449
1450 p = get_proc_task(inode);
1451 if (!p)
1452 return -ESRCH;
1453 proc_sched_autogroup_show_task(p, m);
1454
1455 put_task_struct(p);
1456
1457 return 0;
1458}
1459
1460static ssize_t
1461sched_autogroup_write(struct file *file, const char __user *buf,
1462 size_t count, loff_t *offset)
1463{
Al Viro496ad9a2013-01-23 17:07:38 -05001464 struct inode *inode = file_inode(file);
Mike Galbraith5091faa2010-11-30 14:18:03 +01001465 struct task_struct *p;
1466 char buffer[PROC_NUMBUF];
Alexey Dobriyan0a8cb8e2011-05-26 16:25:50 -07001467 int nice;
Mike Galbraith5091faa2010-11-30 14:18:03 +01001468 int err;
1469
1470 memset(buffer, 0, sizeof(buffer));
1471 if (count > sizeof(buffer) - 1)
1472 count = sizeof(buffer) - 1;
1473 if (copy_from_user(buffer, buf, count))
1474 return -EFAULT;
1475
Alexey Dobriyan0a8cb8e2011-05-26 16:25:50 -07001476 err = kstrtoint(strstrip(buffer), 0, &nice);
1477 if (err < 0)
1478 return err;
Mike Galbraith5091faa2010-11-30 14:18:03 +01001479
1480 p = get_proc_task(inode);
1481 if (!p)
1482 return -ESRCH;
1483
Hiroshi Shimamoto2e5b5b32012-02-23 17:41:27 +09001484 err = proc_sched_autogroup_set_nice(p, nice);
Mike Galbraith5091faa2010-11-30 14:18:03 +01001485 if (err)
1486 count = err;
1487
1488 put_task_struct(p);
1489
1490 return count;
1491}
1492
1493static int sched_autogroup_open(struct inode *inode, struct file *filp)
1494{
1495 int ret;
1496
1497 ret = single_open(filp, sched_autogroup_show, NULL);
1498 if (!ret) {
1499 struct seq_file *m = filp->private_data;
1500
1501 m->private = inode;
1502 }
1503 return ret;
1504}
1505
1506static const struct file_operations proc_pid_sched_autogroup_operations = {
1507 .open = sched_autogroup_open,
1508 .read = seq_read,
1509 .write = sched_autogroup_write,
1510 .llseek = seq_lseek,
1511 .release = single_release,
1512};
1513
1514#endif /* CONFIG_SCHED_AUTOGROUP */
1515
john stultz4614a696b2009-12-14 18:00:05 -08001516static ssize_t comm_write(struct file *file, const char __user *buf,
1517 size_t count, loff_t *offset)
1518{
Al Viro496ad9a2013-01-23 17:07:38 -05001519 struct inode *inode = file_inode(file);
john stultz4614a696b2009-12-14 18:00:05 -08001520 struct task_struct *p;
1521 char buffer[TASK_COMM_LEN];
David Rientjes830e0fc2013-04-30 15:28:18 -07001522 const size_t maxlen = sizeof(buffer) - 1;
john stultz4614a696b2009-12-14 18:00:05 -08001523
1524 memset(buffer, 0, sizeof(buffer));
David Rientjes830e0fc2013-04-30 15:28:18 -07001525 if (copy_from_user(buffer, buf, count > maxlen ? maxlen : count))
john stultz4614a696b2009-12-14 18:00:05 -08001526 return -EFAULT;
1527
1528 p = get_proc_task(inode);
1529 if (!p)
1530 return -ESRCH;
1531
1532 if (same_thread_group(current, p))
1533 set_task_comm(p, buffer);
1534 else
1535 count = -EINVAL;
1536
1537 put_task_struct(p);
1538
1539 return count;
1540}
1541
1542static int comm_show(struct seq_file *m, void *v)
1543{
1544 struct inode *inode = m->private;
1545 struct task_struct *p;
1546
1547 p = get_proc_task(inode);
1548 if (!p)
1549 return -ESRCH;
1550
1551 task_lock(p);
1552 seq_printf(m, "%s\n", p->comm);
1553 task_unlock(p);
1554
1555 put_task_struct(p);
1556
1557 return 0;
1558}
1559
1560static int comm_open(struct inode *inode, struct file *filp)
1561{
Jovi Zhangc6a34052011-01-12 17:00:34 -08001562 return single_open(filp, comm_show, inode);
john stultz4614a696b2009-12-14 18:00:05 -08001563}
1564
1565static const struct file_operations proc_pid_set_comm_operations = {
1566 .open = comm_open,
1567 .read = seq_read,
1568 .write = comm_write,
1569 .llseek = seq_lseek,
1570 .release = single_release,
1571};
1572
Cyrill Gorcunov7773fbc2012-01-10 15:11:20 -08001573static int proc_exe_link(struct dentry *dentry, struct path *exe_path)
Matt Helsley925d1c42008-04-29 01:01:36 -07001574{
1575 struct task_struct *task;
Matt Helsley925d1c42008-04-29 01:01:36 -07001576 struct file *exe_file;
1577
David Howells2b0143b2015-03-17 22:25:59 +00001578 task = get_proc_task(d_inode(dentry));
Matt Helsley925d1c42008-04-29 01:01:36 -07001579 if (!task)
1580 return -ENOENT;
Mateusz Guzikcd81a912016-08-23 16:20:38 +02001581 exe_file = get_task_exe_file(task);
Matt Helsley925d1c42008-04-29 01:01:36 -07001582 put_task_struct(task);
Matt Helsley925d1c42008-04-29 01:01:36 -07001583 if (exe_file) {
1584 *exe_path = exe_file->f_path;
1585 path_get(&exe_file->f_path);
1586 fput(exe_file);
1587 return 0;
1588 } else
1589 return -ENOENT;
1590}
1591
Al Viro6b255392015-11-17 10:20:54 -05001592static const char *proc_pid_get_link(struct dentry *dentry,
Al Virofceef392015-12-29 15:58:39 -05001593 struct inode *inode,
1594 struct delayed_call *done)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001595{
Christoph Hellwig408ef012012-06-18 10:47:03 -04001596 struct path path;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001597 int error = -EACCES;
1598
Al Viro6b255392015-11-17 10:20:54 -05001599 if (!dentry)
1600 return ERR_PTR(-ECHILD);
1601
Eric W. Biederman778c1142006-06-26 00:25:58 -07001602 /* Are we allowed to snoop on the tasks file descriptors? */
1603 if (!proc_fd_access_allowed(inode))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001604 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001605
Christoph Hellwig408ef012012-06-18 10:47:03 -04001606 error = PROC_I(inode)->op.proc_get_link(dentry, &path);
1607 if (error)
1608 goto out;
1609
Al Viro6e771372015-05-02 13:37:52 -04001610 nd_jump_link(&path);
Christoph Hellwig408ef012012-06-18 10:47:03 -04001611 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001612out:
Al Viro008b1502005-08-20 00:17:39 +01001613 return ERR_PTR(error);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001614}
1615
Jan Blunck3dcd25f2008-02-14 19:38:35 -08001616static int do_proc_readlink(struct path *path, char __user *buffer, int buflen)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001617{
Mel Gormane12ba742007-10-16 01:25:52 -07001618 char *tmp = (char*)__get_free_page(GFP_TEMPORARY);
Jan Blunck3dcd25f2008-02-14 19:38:35 -08001619 char *pathname;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001620 int len;
1621
1622 if (!tmp)
1623 return -ENOMEM;
Akinobu Mita0c28f282007-05-08 00:31:41 -07001624
Eric W. Biederman7b2a69b2010-12-05 15:51:21 -08001625 pathname = d_path(path, tmp, PAGE_SIZE);
Jan Blunck3dcd25f2008-02-14 19:38:35 -08001626 len = PTR_ERR(pathname);
1627 if (IS_ERR(pathname))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001628 goto out;
Jan Blunck3dcd25f2008-02-14 19:38:35 -08001629 len = tmp + PAGE_SIZE - 1 - pathname;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001630
1631 if (len > buflen)
1632 len = buflen;
Jan Blunck3dcd25f2008-02-14 19:38:35 -08001633 if (copy_to_user(buffer, pathname, len))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001634 len = -EFAULT;
1635 out:
1636 free_page((unsigned long)tmp);
1637 return len;
1638}
1639
1640static int proc_pid_readlink(struct dentry * dentry, char __user * buffer, int buflen)
1641{
1642 int error = -EACCES;
David Howells2b0143b2015-03-17 22:25:59 +00001643 struct inode *inode = d_inode(dentry);
Jan Blunck3dcd25f2008-02-14 19:38:35 -08001644 struct path path;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001645
Eric W. Biederman778c1142006-06-26 00:25:58 -07001646 /* Are we allowed to snoop on the tasks file descriptors? */
1647 if (!proc_fd_access_allowed(inode))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001648 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001649
Cyrill Gorcunov7773fbc2012-01-10 15:11:20 -08001650 error = PROC_I(inode)->op.proc_get_link(dentry, &path);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001651 if (error)
1652 goto out;
1653
Jan Blunck3dcd25f2008-02-14 19:38:35 -08001654 error = do_proc_readlink(&path, buffer, buflen);
1655 path_put(&path);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001656out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001657 return error;
1658}
1659
Cyrill Gorcunovfaf60af2012-08-23 14:43:24 +04001660const struct inode_operations proc_pid_link_inode_operations = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001661 .readlink = proc_pid_readlink,
Al Viro6b255392015-11-17 10:20:54 -05001662 .get_link = proc_pid_get_link,
Linus Torvalds6d76fa52006-07-15 12:26:45 -07001663 .setattr = proc_setattr,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001664};
1665
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001666
1667/* building an inode */
1668
Eric W. Biederman6b4e3062010-03-07 16:41:34 -08001669struct inode *proc_pid_make_inode(struct super_block * sb, struct task_struct *task)
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001670{
1671 struct inode * inode;
1672 struct proc_inode *ei;
David Howellsc69e8d92008-11-14 10:39:19 +11001673 const struct cred *cred;
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001674
1675 /* We need a new inode */
1676
1677 inode = new_inode(sb);
1678 if (!inode)
1679 goto out;
1680
1681 /* Common stuff */
1682 ei = PROC_I(inode);
Christoph Hellwig85fe4022010-10-23 11:19:54 -04001683 inode->i_ino = get_next_ino();
Deepa Dinamani078cd822016-09-14 07:48:04 -07001684 inode->i_mtime = inode->i_atime = inode->i_ctime = current_time(inode);
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001685 inode->i_op = &proc_def_inode_operations;
1686
1687 /*
1688 * grab the reference to task.
1689 */
Oleg Nesterov1a657f782006-10-02 02:18:59 -07001690 ei->pid = get_task_pid(task, PIDTYPE_PID);
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001691 if (!ei->pid)
1692 goto out_unlock;
1693
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001694 if (task_dumpable(task)) {
David Howellsc69e8d92008-11-14 10:39:19 +11001695 rcu_read_lock();
1696 cred = __task_cred(task);
1697 inode->i_uid = cred->euid;
1698 inode->i_gid = cred->egid;
1699 rcu_read_unlock();
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001700 }
1701 security_task_to_inode(task, inode);
1702
1703out:
1704 return inode;
1705
1706out_unlock:
1707 iput(inode);
1708 return NULL;
1709}
1710
Eric W. Biederman6b4e3062010-03-07 16:41:34 -08001711int pid_getattr(struct vfsmount *mnt, struct dentry *dentry, struct kstat *stat)
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001712{
David Howells2b0143b2015-03-17 22:25:59 +00001713 struct inode *inode = d_inode(dentry);
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001714 struct task_struct *task;
David Howellsc69e8d92008-11-14 10:39:19 +11001715 const struct cred *cred;
Vasiliy Kulikov04996802012-01-10 15:11:31 -08001716 struct pid_namespace *pid = dentry->d_sb->s_fs_info;
David Howellsc69e8d92008-11-14 10:39:19 +11001717
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001718 generic_fillattr(inode, stat);
1719
1720 rcu_read_lock();
Eric W. Biedermandcb0f222012-02-09 08:48:21 -08001721 stat->uid = GLOBAL_ROOT_UID;
1722 stat->gid = GLOBAL_ROOT_GID;
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001723 task = pid_task(proc_pid(inode), PIDTYPE_PID);
1724 if (task) {
Vasiliy Kulikov04996802012-01-10 15:11:31 -08001725 if (!has_pid_permissions(pid, task, 2)) {
1726 rcu_read_unlock();
1727 /*
1728 * This doesn't prevent learning whether PID exists,
1729 * it only makes getattr() consistent with readdir().
1730 */
1731 return -ENOENT;
1732 }
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001733 if ((inode->i_mode == (S_IFDIR|S_IRUGO|S_IXUGO)) ||
1734 task_dumpable(task)) {
David Howellsc69e8d92008-11-14 10:39:19 +11001735 cred = __task_cred(task);
1736 stat->uid = cred->euid;
1737 stat->gid = cred->egid;
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001738 }
1739 }
1740 rcu_read_unlock();
1741 return 0;
1742}
1743
1744/* dentry stuff */
1745
1746/*
1747 * Exceptional case: normally we are not allowed to unhash a busy
1748 * directory. In this case, however, we can do it - no aliasing problems
1749 * due to the way we treat inodes.
1750 *
1751 * Rewrite the inode's ownerships here because the owning task may have
1752 * performed a setuid(), etc.
1753 *
1754 * Before the /proc/pid/status file was created the only way to read
1755 * the effective uid of a /process was to stat /proc/pid. Reading
1756 * /proc/pid/status is slow enough that procps and other packages
1757 * kept stating /proc/pid. To keep the rules in /proc simple I have
1758 * made this apply to all per process world readable and executable
1759 * directories.
1760 */
Al Viro0b728e12012-06-10 16:03:43 -04001761int pid_revalidate(struct dentry *dentry, unsigned int flags)
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001762{
Nick Piggin34286d62011-01-07 17:49:57 +11001763 struct inode *inode;
1764 struct task_struct *task;
David Howellsc69e8d92008-11-14 10:39:19 +11001765 const struct cred *cred;
1766
Al Viro0b728e12012-06-10 16:03:43 -04001767 if (flags & LOOKUP_RCU)
Nick Piggin34286d62011-01-07 17:49:57 +11001768 return -ECHILD;
1769
David Howells2b0143b2015-03-17 22:25:59 +00001770 inode = d_inode(dentry);
Nick Piggin34286d62011-01-07 17:49:57 +11001771 task = get_proc_task(inode);
1772
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001773 if (task) {
1774 if ((inode->i_mode == (S_IFDIR|S_IRUGO|S_IXUGO)) ||
1775 task_dumpable(task)) {
David Howellsc69e8d92008-11-14 10:39:19 +11001776 rcu_read_lock();
1777 cred = __task_cred(task);
1778 inode->i_uid = cred->euid;
1779 inode->i_gid = cred->egid;
1780 rcu_read_unlock();
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001781 } else {
Eric W. Biedermandcb0f222012-02-09 08:48:21 -08001782 inode->i_uid = GLOBAL_ROOT_UID;
1783 inode->i_gid = GLOBAL_ROOT_GID;
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001784 }
1785 inode->i_mode &= ~(S_ISUID | S_ISGID);
1786 security_task_to_inode(task, inode);
1787 put_task_struct(task);
1788 return 1;
1789 }
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001790 return 0;
1791}
1792
Oleg Nesterovd855a4b2014-01-23 15:55:39 -08001793static inline bool proc_inode_is_dead(struct inode *inode)
1794{
1795 return !proc_pid(inode)->tasks[PIDTYPE_PID].first;
1796}
1797
David Howells1dd704b2013-04-12 01:08:50 +01001798int pid_delete_dentry(const struct dentry *dentry)
1799{
1800 /* Is the task we represent dead?
1801 * If so, then don't put the dentry on the lru list,
1802 * kill it immediately.
1803 */
David Howells2b0143b2015-03-17 22:25:59 +00001804 return proc_inode_is_dead(d_inode(dentry));
David Howells1dd704b2013-04-12 01:08:50 +01001805}
1806
Eric W. Biederman6b4e3062010-03-07 16:41:34 -08001807const struct dentry_operations pid_dentry_operations =
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001808{
1809 .d_revalidate = pid_revalidate,
1810 .d_delete = pid_delete_dentry,
1811};
1812
1813/* Lookups */
1814
Eric W. Biederman1c0d04c2006-10-02 02:18:57 -07001815/*
1816 * Fill a directory entry.
1817 *
1818 * If possible create the dcache entry and derive our inode number and
1819 * file type from dcache entry.
1820 *
1821 * Since all of the proc inode numbers are dynamically generated, the inode
1822 * numbers do not exist until the inode is cache. This means creating the
1823 * the dcache entry in readdir is necessary to keep the inode numbers
1824 * reported by readdir in sync with the inode numbers reported
1825 * by stat.
1826 */
Al Virof0c3b502013-05-16 12:07:31 -04001827bool proc_fill_cache(struct file *file, struct dir_context *ctx,
Eric W. Biederman6b4e3062010-03-07 16:41:34 -08001828 const char *name, int len,
Eric Dumazetc5141e62007-05-08 00:26:15 -07001829 instantiate_t instantiate, struct task_struct *task, const void *ptr)
Eric W. Biederman61a28782006-10-02 02:18:49 -07001830{
Al Virof0c3b502013-05-16 12:07:31 -04001831 struct dentry *child, *dir = file->f_path.dentry;
Al Viro1df98b82013-06-15 11:33:10 +04001832 struct qstr qname = QSTR_INIT(name, len);
Eric W. Biederman61a28782006-10-02 02:18:49 -07001833 struct inode *inode;
Al Viro1df98b82013-06-15 11:33:10 +04001834 unsigned type;
1835 ino_t ino;
Eric W. Biederman61a28782006-10-02 02:18:49 -07001836
Al Viro1df98b82013-06-15 11:33:10 +04001837 child = d_hash_and_lookup(dir, &qname);
Eric W. Biederman61a28782006-10-02 02:18:49 -07001838 if (!child) {
Al Viro37817642016-04-20 16:31:31 -04001839 DECLARE_WAIT_QUEUE_HEAD_ONSTACK(wq);
1840 child = d_alloc_parallel(dir, &qname, &wq);
1841 if (IS_ERR(child))
Al Viro1df98b82013-06-15 11:33:10 +04001842 goto end_instantiate;
Al Viro37817642016-04-20 16:31:31 -04001843 if (d_in_lookup(child)) {
1844 int err = instantiate(d_inode(dir), child, task, ptr);
1845 d_lookup_done(child);
1846 if (err < 0) {
1847 dput(child);
1848 goto end_instantiate;
1849 }
Eric W. Biederman61a28782006-10-02 02:18:49 -07001850 }
1851 }
David Howells2b0143b2015-03-17 22:25:59 +00001852 inode = d_inode(child);
Al Viro147ce692013-06-15 10:26:35 +04001853 ino = inode->i_ino;
1854 type = inode->i_mode >> 12;
Eric W. Biederman61a28782006-10-02 02:18:49 -07001855 dput(child);
Al Virof0c3b502013-05-16 12:07:31 -04001856 return dir_emit(ctx, name, len, ino, type);
Al Viro1df98b82013-06-15 11:33:10 +04001857
1858end_instantiate:
1859 return dir_emit(ctx, name, len, 1, DT_UNKNOWN);
Eric W. Biederman61a28782006-10-02 02:18:49 -07001860}
1861
Pavel Emelyanov640708a2012-01-10 15:11:23 -08001862/*
1863 * dname_to_vma_addr - maps a dentry name into two unsigned longs
1864 * which represent vma start and end addresses.
1865 */
1866static int dname_to_vma_addr(struct dentry *dentry,
1867 unsigned long *start, unsigned long *end)
1868{
Alexey Dobriyane0a1a012018-02-06 15:36:59 -08001869 const char *str = dentry->d_name.name;
1870 unsigned long long sval, eval;
1871 unsigned int len;
1872
1873 len = _parse_integer(str, 16, &sval);
1874 if (len & KSTRTOX_OVERFLOW)
Pavel Emelyanov640708a2012-01-10 15:11:23 -08001875 return -EINVAL;
Alexey Dobriyane0a1a012018-02-06 15:36:59 -08001876 if (sval != (unsigned long)sval)
1877 return -EINVAL;
1878 str += len;
1879
1880 if (*str != '-')
1881 return -EINVAL;
1882 str++;
1883
1884 len = _parse_integer(str, 16, &eval);
1885 if (len & KSTRTOX_OVERFLOW)
1886 return -EINVAL;
1887 if (eval != (unsigned long)eval)
1888 return -EINVAL;
1889 str += len;
1890
1891 if (*str != '\0')
1892 return -EINVAL;
1893
1894 *start = sval;
1895 *end = eval;
Pavel Emelyanov640708a2012-01-10 15:11:23 -08001896
1897 return 0;
1898}
1899
Al Viro0b728e12012-06-10 16:03:43 -04001900static int map_files_d_revalidate(struct dentry *dentry, unsigned int flags)
Pavel Emelyanov640708a2012-01-10 15:11:23 -08001901{
1902 unsigned long vm_start, vm_end;
1903 bool exact_vma_exists = false;
1904 struct mm_struct *mm = NULL;
1905 struct task_struct *task;
1906 const struct cred *cred;
1907 struct inode *inode;
1908 int status = 0;
1909
Al Viro0b728e12012-06-10 16:03:43 -04001910 if (flags & LOOKUP_RCU)
Pavel Emelyanov640708a2012-01-10 15:11:23 -08001911 return -ECHILD;
1912
David Howells2b0143b2015-03-17 22:25:59 +00001913 inode = d_inode(dentry);
Pavel Emelyanov640708a2012-01-10 15:11:23 -08001914 task = get_proc_task(inode);
1915 if (!task)
1916 goto out_notask;
1917
Jann Horncaaee622016-01-20 15:00:04 -08001918 mm = mm_access(task, PTRACE_MODE_READ_FSCREDS);
Cong Wang2344bec2012-05-31 16:26:18 -07001919 if (IS_ERR_OR_NULL(mm))
Pavel Emelyanov640708a2012-01-10 15:11:23 -08001920 goto out;
1921
1922 if (!dname_to_vma_addr(dentry, &vm_start, &vm_end)) {
1923 down_read(&mm->mmap_sem);
1924 exact_vma_exists = !!find_exact_vma(mm, vm_start, vm_end);
1925 up_read(&mm->mmap_sem);
1926 }
1927
1928 mmput(mm);
1929
1930 if (exact_vma_exists) {
1931 if (task_dumpable(task)) {
1932 rcu_read_lock();
1933 cred = __task_cred(task);
1934 inode->i_uid = cred->euid;
1935 inode->i_gid = cred->egid;
1936 rcu_read_unlock();
1937 } else {
Eric W. Biedermandcb0f222012-02-09 08:48:21 -08001938 inode->i_uid = GLOBAL_ROOT_UID;
1939 inode->i_gid = GLOBAL_ROOT_GID;
Pavel Emelyanov640708a2012-01-10 15:11:23 -08001940 }
1941 security_task_to_inode(task, inode);
1942 status = 1;
1943 }
1944
1945out:
1946 put_task_struct(task);
1947
1948out_notask:
Pavel Emelyanov640708a2012-01-10 15:11:23 -08001949 return status;
1950}
1951
1952static const struct dentry_operations tid_map_files_dentry_operations = {
1953 .d_revalidate = map_files_d_revalidate,
1954 .d_delete = pid_delete_dentry,
1955};
1956
Al Viro6b255392015-11-17 10:20:54 -05001957static int map_files_get_link(struct dentry *dentry, struct path *path)
Pavel Emelyanov640708a2012-01-10 15:11:23 -08001958{
1959 unsigned long vm_start, vm_end;
1960 struct vm_area_struct *vma;
1961 struct task_struct *task;
1962 struct mm_struct *mm;
1963 int rc;
1964
1965 rc = -ENOENT;
David Howells2b0143b2015-03-17 22:25:59 +00001966 task = get_proc_task(d_inode(dentry));
Pavel Emelyanov640708a2012-01-10 15:11:23 -08001967 if (!task)
1968 goto out;
1969
1970 mm = get_task_mm(task);
1971 put_task_struct(task);
1972 if (!mm)
1973 goto out;
1974
1975 rc = dname_to_vma_addr(dentry, &vm_start, &vm_end);
1976 if (rc)
1977 goto out_mmput;
1978
Artem Fetishev70335ab2014-03-10 15:49:45 -07001979 rc = -ENOENT;
Pavel Emelyanov640708a2012-01-10 15:11:23 -08001980 down_read(&mm->mmap_sem);
1981 vma = find_exact_vma(mm, vm_start, vm_end);
1982 if (vma && vma->vm_file) {
1983 *path = vma->vm_file->f_path;
1984 path_get(path);
1985 rc = 0;
1986 }
1987 up_read(&mm->mmap_sem);
1988
1989out_mmput:
1990 mmput(mm);
1991out:
1992 return rc;
1993}
1994
1995struct map_files_info {
Al Viro7b540d02012-08-27 14:55:26 -04001996 fmode_t mode;
Pavel Emelyanov640708a2012-01-10 15:11:23 -08001997 unsigned long len;
1998 unsigned char name[4*sizeof(long)+2]; /* max: %lx-%lx\0 */
1999};
2000
Calvin Owensbdb4d102015-09-09 15:35:54 -07002001/*
2002 * Only allow CAP_SYS_ADMIN to follow the links, due to concerns about how the
2003 * symlinks may be used to bypass permissions on ancestor directories in the
2004 * path to the file in question.
2005 */
2006static const char *
Al Viro6b255392015-11-17 10:20:54 -05002007proc_map_files_get_link(struct dentry *dentry,
Al Virofceef392015-12-29 15:58:39 -05002008 struct inode *inode,
2009 struct delayed_call *done)
Calvin Owensbdb4d102015-09-09 15:35:54 -07002010{
2011 if (!capable(CAP_SYS_ADMIN))
2012 return ERR_PTR(-EPERM);
2013
Al Virofceef392015-12-29 15:58:39 -05002014 return proc_pid_get_link(dentry, inode, done);
Calvin Owensbdb4d102015-09-09 15:35:54 -07002015}
2016
2017/*
Al Viro6b255392015-11-17 10:20:54 -05002018 * Identical to proc_pid_link_inode_operations except for get_link()
Calvin Owensbdb4d102015-09-09 15:35:54 -07002019 */
2020static const struct inode_operations proc_map_files_link_inode_operations = {
2021 .readlink = proc_pid_readlink,
Al Viro6b255392015-11-17 10:20:54 -05002022 .get_link = proc_map_files_get_link,
Calvin Owensbdb4d102015-09-09 15:35:54 -07002023 .setattr = proc_setattr,
2024};
2025
Al Viroc52a47a2013-06-15 11:15:20 +04002026static int
Pavel Emelyanov640708a2012-01-10 15:11:23 -08002027proc_map_files_instantiate(struct inode *dir, struct dentry *dentry,
2028 struct task_struct *task, const void *ptr)
2029{
Al Viro7b540d02012-08-27 14:55:26 -04002030 fmode_t mode = (fmode_t)(unsigned long)ptr;
Pavel Emelyanov640708a2012-01-10 15:11:23 -08002031 struct proc_inode *ei;
2032 struct inode *inode;
2033
Pavel Emelyanov640708a2012-01-10 15:11:23 -08002034 inode = proc_pid_make_inode(dir->i_sb, task);
2035 if (!inode)
Al Viroc52a47a2013-06-15 11:15:20 +04002036 return -ENOENT;
Pavel Emelyanov640708a2012-01-10 15:11:23 -08002037
2038 ei = PROC_I(inode);
Al Viro6b255392015-11-17 10:20:54 -05002039 ei->op.proc_get_link = map_files_get_link;
Pavel Emelyanov640708a2012-01-10 15:11:23 -08002040
Calvin Owensbdb4d102015-09-09 15:35:54 -07002041 inode->i_op = &proc_map_files_link_inode_operations;
Pavel Emelyanov640708a2012-01-10 15:11:23 -08002042 inode->i_size = 64;
2043 inode->i_mode = S_IFLNK;
2044
Al Viro7b540d02012-08-27 14:55:26 -04002045 if (mode & FMODE_READ)
Pavel Emelyanov640708a2012-01-10 15:11:23 -08002046 inode->i_mode |= S_IRUSR;
Al Viro7b540d02012-08-27 14:55:26 -04002047 if (mode & FMODE_WRITE)
Pavel Emelyanov640708a2012-01-10 15:11:23 -08002048 inode->i_mode |= S_IWUSR;
2049
2050 d_set_d_op(dentry, &tid_map_files_dentry_operations);
2051 d_add(dentry, inode);
2052
Al Viroc52a47a2013-06-15 11:15:20 +04002053 return 0;
Pavel Emelyanov640708a2012-01-10 15:11:23 -08002054}
2055
2056static struct dentry *proc_map_files_lookup(struct inode *dir,
Al Viro00cd8dd2012-06-10 17:13:09 -04002057 struct dentry *dentry, unsigned int flags)
Pavel Emelyanov640708a2012-01-10 15:11:23 -08002058{
2059 unsigned long vm_start, vm_end;
2060 struct vm_area_struct *vma;
2061 struct task_struct *task;
Al Viroc52a47a2013-06-15 11:15:20 +04002062 int result;
Pavel Emelyanov640708a2012-01-10 15:11:23 -08002063 struct mm_struct *mm;
2064
Al Viroc52a47a2013-06-15 11:15:20 +04002065 result = -ENOENT;
Pavel Emelyanov640708a2012-01-10 15:11:23 -08002066 task = get_proc_task(dir);
2067 if (!task)
2068 goto out;
2069
Al Viroc52a47a2013-06-15 11:15:20 +04002070 result = -EACCES;
Jann Horncaaee622016-01-20 15:00:04 -08002071 if (!ptrace_may_access(task, PTRACE_MODE_READ_FSCREDS))
Pavel Emelyanov640708a2012-01-10 15:11:23 -08002072 goto out_put_task;
2073
Al Viroc52a47a2013-06-15 11:15:20 +04002074 result = -ENOENT;
Pavel Emelyanov640708a2012-01-10 15:11:23 -08002075 if (dname_to_vma_addr(dentry, &vm_start, &vm_end))
Cyrill Gorcunoveb94cd92012-05-17 17:03:25 -07002076 goto out_put_task;
Pavel Emelyanov640708a2012-01-10 15:11:23 -08002077
2078 mm = get_task_mm(task);
2079 if (!mm)
Cyrill Gorcunoveb94cd92012-05-17 17:03:25 -07002080 goto out_put_task;
Pavel Emelyanov640708a2012-01-10 15:11:23 -08002081
2082 down_read(&mm->mmap_sem);
2083 vma = find_exact_vma(mm, vm_start, vm_end);
2084 if (!vma)
2085 goto out_no_vma;
2086
Stanislav Kinsbursky05f56482012-11-26 16:29:42 -08002087 if (vma->vm_file)
2088 result = proc_map_files_instantiate(dir, dentry, task,
2089 (void *)(unsigned long)vma->vm_file->f_mode);
Pavel Emelyanov640708a2012-01-10 15:11:23 -08002090
2091out_no_vma:
2092 up_read(&mm->mmap_sem);
2093 mmput(mm);
Pavel Emelyanov640708a2012-01-10 15:11:23 -08002094out_put_task:
2095 put_task_struct(task);
2096out:
Al Viroc52a47a2013-06-15 11:15:20 +04002097 return ERR_PTR(result);
Pavel Emelyanov640708a2012-01-10 15:11:23 -08002098}
2099
2100static const struct inode_operations proc_map_files_inode_operations = {
2101 .lookup = proc_map_files_lookup,
2102 .permission = proc_fd_permission,
2103 .setattr = proc_setattr,
2104};
2105
2106static int
Al Virof0c3b502013-05-16 12:07:31 -04002107proc_map_files_readdir(struct file *file, struct dir_context *ctx)
Pavel Emelyanov640708a2012-01-10 15:11:23 -08002108{
Pavel Emelyanov640708a2012-01-10 15:11:23 -08002109 struct vm_area_struct *vma;
2110 struct task_struct *task;
2111 struct mm_struct *mm;
Al Virof0c3b502013-05-16 12:07:31 -04002112 unsigned long nr_files, pos, i;
2113 struct flex_array *fa = NULL;
2114 struct map_files_info info;
2115 struct map_files_info *p;
Pavel Emelyanov640708a2012-01-10 15:11:23 -08002116 int ret;
2117
Pavel Emelyanov640708a2012-01-10 15:11:23 -08002118 ret = -ENOENT;
Al Virof0c3b502013-05-16 12:07:31 -04002119 task = get_proc_task(file_inode(file));
Pavel Emelyanov640708a2012-01-10 15:11:23 -08002120 if (!task)
2121 goto out;
2122
2123 ret = -EACCES;
Jann Horncaaee622016-01-20 15:00:04 -08002124 if (!ptrace_may_access(task, PTRACE_MODE_READ_FSCREDS))
Pavel Emelyanov640708a2012-01-10 15:11:23 -08002125 goto out_put_task;
2126
2127 ret = 0;
Al Virof0c3b502013-05-16 12:07:31 -04002128 if (!dir_emit_dots(file, ctx))
2129 goto out_put_task;
Pavel Emelyanov640708a2012-01-10 15:11:23 -08002130
Al Virof0c3b502013-05-16 12:07:31 -04002131 mm = get_task_mm(task);
2132 if (!mm)
2133 goto out_put_task;
2134 down_read(&mm->mmap_sem);
Pavel Emelyanov640708a2012-01-10 15:11:23 -08002135
Al Virof0c3b502013-05-16 12:07:31 -04002136 nr_files = 0;
Pavel Emelyanov640708a2012-01-10 15:11:23 -08002137
Al Virof0c3b502013-05-16 12:07:31 -04002138 /*
2139 * We need two passes here:
2140 *
2141 * 1) Collect vmas of mapped files with mmap_sem taken
2142 * 2) Release mmap_sem and instantiate entries
2143 *
2144 * otherwise we get lockdep complained, since filldir()
2145 * routine might require mmap_sem taken in might_fault().
2146 */
Pavel Emelyanov640708a2012-01-10 15:11:23 -08002147
Al Virof0c3b502013-05-16 12:07:31 -04002148 for (vma = mm->mmap, pos = 2; vma; vma = vma->vm_next) {
2149 if (vma->vm_file && ++pos > ctx->pos)
2150 nr_files++;
Pavel Emelyanov640708a2012-01-10 15:11:23 -08002151 }
Al Virof0c3b502013-05-16 12:07:31 -04002152
2153 if (nr_files) {
2154 fa = flex_array_alloc(sizeof(info), nr_files,
2155 GFP_KERNEL);
2156 if (!fa || flex_array_prealloc(fa, 0, nr_files,
2157 GFP_KERNEL)) {
2158 ret = -ENOMEM;
2159 if (fa)
2160 flex_array_free(fa);
2161 up_read(&mm->mmap_sem);
2162 mmput(mm);
2163 goto out_put_task;
2164 }
2165 for (i = 0, vma = mm->mmap, pos = 2; vma;
2166 vma = vma->vm_next) {
2167 if (!vma->vm_file)
2168 continue;
2169 if (++pos <= ctx->pos)
2170 continue;
2171
2172 info.mode = vma->vm_file->f_mode;
2173 info.len = snprintf(info.name,
2174 sizeof(info.name), "%lx-%lx",
2175 vma->vm_start, vma->vm_end);
2176 if (flex_array_put(fa, i++, &info, GFP_KERNEL))
2177 BUG();
2178 }
Pavel Emelyanov640708a2012-01-10 15:11:23 -08002179 }
Al Virof0c3b502013-05-16 12:07:31 -04002180 up_read(&mm->mmap_sem);
2181
2182 for (i = 0; i < nr_files; i++) {
2183 p = flex_array_get(fa, i);
2184 if (!proc_fill_cache(file, ctx,
2185 p->name, p->len,
2186 proc_map_files_instantiate,
2187 task,
2188 (void *)(unsigned long)p->mode))
2189 break;
2190 ctx->pos++;
2191 }
2192 if (fa)
2193 flex_array_free(fa);
2194 mmput(mm);
Pavel Emelyanov640708a2012-01-10 15:11:23 -08002195
Pavel Emelyanov640708a2012-01-10 15:11:23 -08002196out_put_task:
2197 put_task_struct(task);
2198out:
2199 return ret;
2200}
2201
2202static const struct file_operations proc_map_files_operations = {
2203 .read = generic_read_dir,
Al Virof50752e2016-04-20 17:13:54 -04002204 .iterate_shared = proc_map_files_readdir,
2205 .llseek = generic_file_llseek,
Pavel Emelyanov640708a2012-01-10 15:11:23 -08002206};
2207
Eric Engestromb5946be2016-03-17 14:20:57 -07002208#ifdef CONFIG_CHECKPOINT_RESTORE
Pavel Emelyanov48f6a7a2013-03-11 13:12:45 +04002209struct timers_private {
2210 struct pid *pid;
2211 struct task_struct *task;
2212 struct sighand_struct *sighand;
Pavel Emelyanov57b80152013-03-11 13:13:08 +04002213 struct pid_namespace *ns;
Pavel Emelyanov48f6a7a2013-03-11 13:12:45 +04002214 unsigned long flags;
2215};
2216
2217static void *timers_start(struct seq_file *m, loff_t *pos)
2218{
2219 struct timers_private *tp = m->private;
2220
2221 tp->task = get_pid_task(tp->pid, PIDTYPE_PID);
2222 if (!tp->task)
2223 return ERR_PTR(-ESRCH);
2224
2225 tp->sighand = lock_task_sighand(tp->task, &tp->flags);
2226 if (!tp->sighand)
2227 return ERR_PTR(-ESRCH);
2228
2229 return seq_list_start(&tp->task->signal->posix_timers, *pos);
2230}
2231
2232static void *timers_next(struct seq_file *m, void *v, loff_t *pos)
2233{
2234 struct timers_private *tp = m->private;
2235 return seq_list_next(v, &tp->task->signal->posix_timers, pos);
2236}
2237
2238static void timers_stop(struct seq_file *m, void *v)
2239{
2240 struct timers_private *tp = m->private;
2241
2242 if (tp->sighand) {
2243 unlock_task_sighand(tp->task, &tp->flags);
2244 tp->sighand = NULL;
2245 }
2246
2247 if (tp->task) {
2248 put_task_struct(tp->task);
2249 tp->task = NULL;
2250 }
2251}
2252
2253static int show_timer(struct seq_file *m, void *v)
2254{
2255 struct k_itimer *timer;
Pavel Emelyanov57b80152013-03-11 13:13:08 +04002256 struct timers_private *tp = m->private;
2257 int notify;
Alexey Dobriyancedbcca2014-08-08 14:21:33 -07002258 static const char * const nstr[] = {
Pavel Emelyanov57b80152013-03-11 13:13:08 +04002259 [SIGEV_SIGNAL] = "signal",
2260 [SIGEV_NONE] = "none",
2261 [SIGEV_THREAD] = "thread",
2262 };
Pavel Emelyanov48f6a7a2013-03-11 13:12:45 +04002263
2264 timer = list_entry((struct list_head *)v, struct k_itimer, list);
Pavel Emelyanov57b80152013-03-11 13:13:08 +04002265 notify = timer->it_sigev_notify;
2266
Pavel Emelyanov48f6a7a2013-03-11 13:12:45 +04002267 seq_printf(m, "ID: %d\n", timer->it_id);
Joe Perches25ce3192015-04-15 16:18:17 -07002268 seq_printf(m, "signal: %d/%p\n",
2269 timer->sigq->info.si_signo,
2270 timer->sigq->info.si_value.sival_ptr);
Pavel Emelyanov57b80152013-03-11 13:13:08 +04002271 seq_printf(m, "notify: %s/%s.%d\n",
Joe Perches25ce3192015-04-15 16:18:17 -07002272 nstr[notify & ~SIGEV_THREAD_ID],
2273 (notify & SIGEV_THREAD_ID) ? "tid" : "pid",
2274 pid_nr_ns(timer->it_pid, tp->ns));
Pavel Tikhomirov15ef0292013-05-17 02:12:03 +04002275 seq_printf(m, "ClockID: %d\n", timer->it_clock);
Pavel Emelyanov48f6a7a2013-03-11 13:12:45 +04002276
2277 return 0;
2278}
2279
2280static const struct seq_operations proc_timers_seq_ops = {
2281 .start = timers_start,
2282 .next = timers_next,
2283 .stop = timers_stop,
2284 .show = show_timer,
2285};
2286
2287static int proc_timers_open(struct inode *inode, struct file *file)
2288{
2289 struct timers_private *tp;
2290
2291 tp = __seq_open_private(file, &proc_timers_seq_ops,
2292 sizeof(struct timers_private));
2293 if (!tp)
2294 return -ENOMEM;
2295
2296 tp->pid = proc_pid(inode);
Pavel Emelyanov57b80152013-03-11 13:13:08 +04002297 tp->ns = inode->i_sb->s_fs_info;
Pavel Emelyanov48f6a7a2013-03-11 13:12:45 +04002298 return 0;
2299}
2300
2301static const struct file_operations proc_timers_operations = {
2302 .open = proc_timers_open,
2303 .read = seq_read,
2304 .llseek = seq_lseek,
2305 .release = seq_release_private,
2306};
Eric Engestromb5946be2016-03-17 14:20:57 -07002307#endif
Pavel Emelyanov640708a2012-01-10 15:11:23 -08002308
John Stultz5de23d42016-03-17 14:20:54 -07002309static ssize_t timerslack_ns_write(struct file *file, const char __user *buf,
2310 size_t count, loff_t *offset)
2311{
2312 struct inode *inode = file_inode(file);
2313 struct task_struct *p;
2314 u64 slack_ns;
2315 int err;
2316
2317 err = kstrtoull_from_user(buf, count, 10, &slack_ns);
2318 if (err < 0)
2319 return err;
2320
2321 p = get_proc_task(inode);
2322 if (!p)
2323 return -ESRCH;
2324
John Stultz4b2bd5f2016-10-07 17:02:33 -07002325 if (p != current) {
2326 if (!capable(CAP_SYS_NICE)) {
2327 count = -EPERM;
2328 goto out;
2329 }
John Stultz5de23d42016-03-17 14:20:54 -07002330
John Stultz4b2bd5f2016-10-07 17:02:33 -07002331 err = security_task_setscheduler(p);
2332 if (err) {
2333 count = err;
2334 goto out;
2335 }
John Stultz904763e2016-10-07 17:02:29 -07002336 }
2337
John Stultz7abbaf92016-10-07 17:02:26 -07002338 task_lock(p);
2339 if (slack_ns == 0)
2340 p->timer_slack_ns = p->default_timer_slack_ns;
2341 else
2342 p->timer_slack_ns = slack_ns;
2343 task_unlock(p);
2344
2345out:
John Stultz5de23d42016-03-17 14:20:54 -07002346 put_task_struct(p);
2347
2348 return count;
2349}
2350
2351static int timerslack_ns_show(struct seq_file *m, void *v)
2352{
2353 struct inode *inode = m->private;
2354 struct task_struct *p;
John Stultz7abbaf92016-10-07 17:02:26 -07002355 int err = 0;
John Stultz5de23d42016-03-17 14:20:54 -07002356
2357 p = get_proc_task(inode);
2358 if (!p)
2359 return -ESRCH;
2360
John Stultz4b2bd5f2016-10-07 17:02:33 -07002361 if (p != current) {
John Stultz5de23d42016-03-17 14:20:54 -07002362
John Stultz4b2bd5f2016-10-07 17:02:33 -07002363 if (!capable(CAP_SYS_NICE)) {
2364 err = -EPERM;
2365 goto out;
2366 }
2367 err = security_task_getscheduler(p);
2368 if (err)
2369 goto out;
2370 }
John Stultz904763e2016-10-07 17:02:29 -07002371
John Stultz7abbaf92016-10-07 17:02:26 -07002372 task_lock(p);
2373 seq_printf(m, "%llu\n", p->timer_slack_ns);
2374 task_unlock(p);
2375
2376out:
John Stultz5de23d42016-03-17 14:20:54 -07002377 put_task_struct(p);
2378
2379 return err;
2380}
2381
2382static int timerslack_ns_open(struct inode *inode, struct file *filp)
2383{
2384 return single_open(filp, timerslack_ns_show, inode);
2385}
2386
2387static const struct file_operations proc_pid_set_timerslack_ns_operations = {
2388 .open = timerslack_ns_open,
2389 .read = seq_read,
2390 .write = timerslack_ns_write,
2391 .llseek = seq_lseek,
2392 .release = single_release,
2393};
2394
Al Viroc52a47a2013-06-15 11:15:20 +04002395static int proc_pident_instantiate(struct inode *dir,
Eric Dumazetc5141e62007-05-08 00:26:15 -07002396 struct dentry *dentry, struct task_struct *task, const void *ptr)
Eric W. Biederman444ceed2006-10-02 02:18:49 -07002397{
Eric Dumazetc5141e62007-05-08 00:26:15 -07002398 const struct pid_entry *p = ptr;
Eric W. Biederman444ceed2006-10-02 02:18:49 -07002399 struct inode *inode;
2400 struct proc_inode *ei;
Eric W. Biederman444ceed2006-10-02 02:18:49 -07002401
Eric W. Biederman61a28782006-10-02 02:18:49 -07002402 inode = proc_pid_make_inode(dir->i_sb, task);
Eric W. Biederman444ceed2006-10-02 02:18:49 -07002403 if (!inode)
2404 goto out;
2405
2406 ei = PROC_I(inode);
2407 inode->i_mode = p->mode;
2408 if (S_ISDIR(inode->i_mode))
Miklos Szeredibfe86842011-10-28 14:13:29 +02002409 set_nlink(inode, 2); /* Use getattr to fix if necessary */
Eric W. Biederman444ceed2006-10-02 02:18:49 -07002410 if (p->iop)
2411 inode->i_op = p->iop;
2412 if (p->fop)
2413 inode->i_fop = p->fop;
2414 ei->op = p->op;
Nick Pigginfb045ad2011-01-07 17:49:55 +11002415 d_set_d_op(dentry, &pid_dentry_operations);
Eric W. Biederman444ceed2006-10-02 02:18:49 -07002416 d_add(dentry, inode);
2417 /* Close the race of the process dying before we return the dentry */
Al Viro0b728e12012-06-10 16:03:43 -04002418 if (pid_revalidate(dentry, 0))
Al Viroc52a47a2013-06-15 11:15:20 +04002419 return 0;
Eric W. Biederman444ceed2006-10-02 02:18:49 -07002420out:
Al Viroc52a47a2013-06-15 11:15:20 +04002421 return -ENOENT;
Eric W. Biederman444ceed2006-10-02 02:18:49 -07002422}
2423
Linus Torvalds1da177e2005-04-16 15:20:36 -07002424static struct dentry *proc_pident_lookup(struct inode *dir,
2425 struct dentry *dentry,
Eric Dumazetc5141e62007-05-08 00:26:15 -07002426 const struct pid_entry *ents,
Eric W. Biederman7bcd6b02006-10-02 02:18:56 -07002427 unsigned int nents)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002428{
Al Viroc52a47a2013-06-15 11:15:20 +04002429 int error;
Eric W. Biederman99f89552006-06-26 00:25:55 -07002430 struct task_struct *task = get_proc_task(dir);
Eric Dumazetc5141e62007-05-08 00:26:15 -07002431 const struct pid_entry *p, *last;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002432
Al Viroc52a47a2013-06-15 11:15:20 +04002433 error = -ENOENT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002434
Eric W. Biederman99f89552006-06-26 00:25:55 -07002435 if (!task)
2436 goto out_no_task;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002437
Eric W. Biederman20cdc892006-10-02 02:17:07 -07002438 /*
2439 * Yes, it does not scale. And it should not. Don't add
2440 * new entries into /proc/<tgid>/ without very good reasons.
2441 */
Eric W. Biederman7bcd6b02006-10-02 02:18:56 -07002442 last = &ents[nents - 1];
2443 for (p = ents; p <= last; p++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002444 if (p->len != dentry->d_name.len)
2445 continue;
2446 if (!memcmp(dentry->d_name.name, p->name, p->len))
2447 break;
2448 }
Eric W. Biederman7bcd6b02006-10-02 02:18:56 -07002449 if (p > last)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002450 goto out;
2451
Eric W. Biederman444ceed2006-10-02 02:18:49 -07002452 error = proc_pident_instantiate(dir, dentry, task, p);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002453out:
Eric W. Biederman99f89552006-06-26 00:25:55 -07002454 put_task_struct(task);
2455out_no_task:
Al Viroc52a47a2013-06-15 11:15:20 +04002456 return ERR_PTR(error);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002457}
2458
Al Virof0c3b502013-05-16 12:07:31 -04002459static int proc_pident_readdir(struct file *file, struct dir_context *ctx,
Eric Dumazetc5141e62007-05-08 00:26:15 -07002460 const struct pid_entry *ents, unsigned int nents)
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002461{
Al Virof0c3b502013-05-16 12:07:31 -04002462 struct task_struct *task = get_proc_task(file_inode(file));
2463 const struct pid_entry *p;
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002464
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002465 if (!task)
Al Virof0c3b502013-05-16 12:07:31 -04002466 return -ENOENT;
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002467
Al Virof0c3b502013-05-16 12:07:31 -04002468 if (!dir_emit_dots(file, ctx))
2469 goto out;
2470
2471 if (ctx->pos >= nents + 2)
2472 goto out;
2473
2474 for (p = ents + (ctx->pos - 2); p <= ents + nents - 1; p++) {
2475 if (!proc_fill_cache(file, ctx, p->name, p->len,
2476 proc_pident_instantiate, task, p))
2477 break;
2478 ctx->pos++;
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002479 }
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002480out:
Eric W. Biederman61a28782006-10-02 02:18:49 -07002481 put_task_struct(task);
Al Virof0c3b502013-05-16 12:07:31 -04002482 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002483}
2484
Linus Torvalds1da177e2005-04-16 15:20:36 -07002485#ifdef CONFIG_SECURITY
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002486static ssize_t proc_pid_attr_read(struct file * file, char __user * buf,
2487 size_t count, loff_t *ppos)
2488{
Al Viro496ad9a2013-01-23 17:07:38 -05002489 struct inode * inode = file_inode(file);
Al Viro04ff9702007-03-12 16:17:58 +00002490 char *p = NULL;
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002491 ssize_t length;
2492 struct task_struct *task = get_proc_task(inode);
2493
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002494 if (!task)
Al Viro04ff9702007-03-12 16:17:58 +00002495 return -ESRCH;
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002496
2497 length = security_getprocattr(task,
Josef "Jeff" Sipek2fddfee2006-12-08 02:36:36 -08002498 (char*)file->f_path.dentry->d_name.name,
Al Viro04ff9702007-03-12 16:17:58 +00002499 &p);
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002500 put_task_struct(task);
Al Viro04ff9702007-03-12 16:17:58 +00002501 if (length > 0)
2502 length = simple_read_from_buffer(buf, count, ppos, p, length);
2503 kfree(p);
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002504 return length;
2505}
2506
2507static ssize_t proc_pid_attr_write(struct file * file, const char __user * buf,
2508 size_t count, loff_t *ppos)
2509{
Al Viro496ad9a2013-01-23 17:07:38 -05002510 struct inode * inode = file_inode(file);
Al Virobb646cd2015-12-24 00:16:30 -05002511 void *page;
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002512 ssize_t length;
2513 struct task_struct *task = get_proc_task(inode);
2514
2515 length = -ESRCH;
2516 if (!task)
2517 goto out_no_task;
2518 if (count > PAGE_SIZE)
2519 count = PAGE_SIZE;
2520
2521 /* No partial writes. */
2522 length = -EINVAL;
2523 if (*ppos != 0)
2524 goto out;
2525
Al Virobb646cd2015-12-24 00:16:30 -05002526 page = memdup_user(buf, count);
2527 if (IS_ERR(page)) {
2528 length = PTR_ERR(page);
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002529 goto out;
Al Virobb646cd2015-12-24 00:16:30 -05002530 }
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002531
David Howells107db7c2009-05-08 13:55:27 +01002532 /* Guard against adverse ptrace interaction */
KOSAKI Motohiro9b1bf122010-10-27 15:34:08 -07002533 length = mutex_lock_interruptible(&task->signal->cred_guard_mutex);
David Howells107db7c2009-05-08 13:55:27 +01002534 if (length < 0)
2535 goto out_free;
2536
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002537 length = security_setprocattr(task,
Josef "Jeff" Sipek2fddfee2006-12-08 02:36:36 -08002538 (char*)file->f_path.dentry->d_name.name,
Al Virobb646cd2015-12-24 00:16:30 -05002539 page, count);
KOSAKI Motohiro9b1bf122010-10-27 15:34:08 -07002540 mutex_unlock(&task->signal->cred_guard_mutex);
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002541out_free:
Al Virobb646cd2015-12-24 00:16:30 -05002542 kfree(page);
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002543out:
2544 put_task_struct(task);
2545out_no_task:
2546 return length;
2547}
2548
Arjan van de Ven00977a52007-02-12 00:55:34 -08002549static const struct file_operations proc_pid_attr_operations = {
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002550 .read = proc_pid_attr_read,
2551 .write = proc_pid_attr_write,
Arnd Bergmann87df8422010-03-17 23:06:02 +01002552 .llseek = generic_file_llseek,
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002553};
2554
Eric Dumazetc5141e62007-05-08 00:26:15 -07002555static const struct pid_entry attr_dir_stuff[] = {
Alexey Dobriyan631f9c12008-11-10 01:32:52 +03002556 REG("current", S_IRUGO|S_IWUGO, proc_pid_attr_operations),
2557 REG("prev", S_IRUGO, proc_pid_attr_operations),
2558 REG("exec", S_IRUGO|S_IWUGO, proc_pid_attr_operations),
2559 REG("fscreate", S_IRUGO|S_IWUGO, proc_pid_attr_operations),
2560 REG("keycreate", S_IRUGO|S_IWUGO, proc_pid_attr_operations),
2561 REG("sockcreate", S_IRUGO|S_IWUGO, proc_pid_attr_operations),
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002562};
2563
Al Virof0c3b502013-05-16 12:07:31 -04002564static int proc_attr_dir_readdir(struct file *file, struct dir_context *ctx)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002565{
Al Virof0c3b502013-05-16 12:07:31 -04002566 return proc_pident_readdir(file, ctx,
2567 attr_dir_stuff, ARRAY_SIZE(attr_dir_stuff));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002568}
2569
Arjan van de Ven00977a52007-02-12 00:55:34 -08002570static const struct file_operations proc_attr_dir_operations = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002571 .read = generic_read_dir,
Al Virof50752e2016-04-20 17:13:54 -04002572 .iterate_shared = proc_attr_dir_readdir,
2573 .llseek = generic_file_llseek,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002574};
2575
Eric W. Biederman72d9dcf2006-10-02 02:18:50 -07002576static struct dentry *proc_attr_dir_lookup(struct inode *dir,
Al Viro00cd8dd2012-06-10 17:13:09 -04002577 struct dentry *dentry, unsigned int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002578{
Eric W. Biederman7bcd6b02006-10-02 02:18:56 -07002579 return proc_pident_lookup(dir, dentry,
2580 attr_dir_stuff, ARRAY_SIZE(attr_dir_stuff));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002581}
2582
Arjan van de Venc5ef1c42007-02-12 00:55:40 -08002583static const struct inode_operations proc_attr_dir_inode_operations = {
Eric W. Biederman72d9dcf2006-10-02 02:18:50 -07002584 .lookup = proc_attr_dir_lookup,
Eric W. Biederman99f89552006-06-26 00:25:55 -07002585 .getattr = pid_getattr,
Linus Torvalds6d76fa52006-07-15 12:26:45 -07002586 .setattr = proc_setattr,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002587};
2588
Linus Torvalds1da177e2005-04-16 15:20:36 -07002589#endif
2590
Christoph Hellwig698ba7b2009-12-15 16:47:37 -08002591#ifdef CONFIG_ELF_CORE
Kawai, Hidehiro3cb4a0b2007-07-19 01:48:28 -07002592static ssize_t proc_coredump_filter_read(struct file *file, char __user *buf,
2593 size_t count, loff_t *ppos)
2594{
Al Viro496ad9a2013-01-23 17:07:38 -05002595 struct task_struct *task = get_proc_task(file_inode(file));
Kawai, Hidehiro3cb4a0b2007-07-19 01:48:28 -07002596 struct mm_struct *mm;
2597 char buffer[PROC_NUMBUF];
2598 size_t len;
2599 int ret;
2600
2601 if (!task)
2602 return -ESRCH;
2603
2604 ret = 0;
2605 mm = get_task_mm(task);
2606 if (mm) {
2607 len = snprintf(buffer, sizeof(buffer), "%08lx\n",
2608 ((mm->flags & MMF_DUMP_FILTER_MASK) >>
2609 MMF_DUMP_FILTER_SHIFT));
2610 mmput(mm);
2611 ret = simple_read_from_buffer(buf, count, ppos, buffer, len);
2612 }
2613
2614 put_task_struct(task);
2615
2616 return ret;
2617}
2618
2619static ssize_t proc_coredump_filter_write(struct file *file,
2620 const char __user *buf,
2621 size_t count,
2622 loff_t *ppos)
2623{
2624 struct task_struct *task;
2625 struct mm_struct *mm;
Kawai, Hidehiro3cb4a0b2007-07-19 01:48:28 -07002626 unsigned int val;
2627 int ret;
2628 int i;
2629 unsigned long mask;
2630
Alexey Dobriyan774636e2015-09-09 15:36:59 -07002631 ret = kstrtouint_from_user(buf, count, 0, &val);
2632 if (ret < 0)
2633 return ret;
Kawai, Hidehiro3cb4a0b2007-07-19 01:48:28 -07002634
2635 ret = -ESRCH;
Al Viro496ad9a2013-01-23 17:07:38 -05002636 task = get_proc_task(file_inode(file));
Kawai, Hidehiro3cb4a0b2007-07-19 01:48:28 -07002637 if (!task)
2638 goto out_no_task;
2639
Kawai, Hidehiro3cb4a0b2007-07-19 01:48:28 -07002640 mm = get_task_mm(task);
2641 if (!mm)
2642 goto out_no_mm;
Colin Ian King41a0c2492015-12-18 14:22:01 -08002643 ret = 0;
Kawai, Hidehiro3cb4a0b2007-07-19 01:48:28 -07002644
2645 for (i = 0, mask = 1; i < MMF_DUMP_FILTER_BITS; i++, mask <<= 1) {
2646 if (val & mask)
2647 set_bit(i + MMF_DUMP_FILTER_SHIFT, &mm->flags);
2648 else
2649 clear_bit(i + MMF_DUMP_FILTER_SHIFT, &mm->flags);
2650 }
2651
2652 mmput(mm);
2653 out_no_mm:
2654 put_task_struct(task);
2655 out_no_task:
Alexey Dobriyan774636e2015-09-09 15:36:59 -07002656 if (ret < 0)
2657 return ret;
2658 return count;
Kawai, Hidehiro3cb4a0b2007-07-19 01:48:28 -07002659}
2660
2661static const struct file_operations proc_coredump_filter_operations = {
2662 .read = proc_coredump_filter_read,
2663 .write = proc_coredump_filter_write,
Arnd Bergmann87df8422010-03-17 23:06:02 +01002664 .llseek = generic_file_llseek,
Kawai, Hidehiro3cb4a0b2007-07-19 01:48:28 -07002665};
2666#endif
2667
Andrew Mortonaba76fd2006-12-10 02:19:48 -08002668#ifdef CONFIG_TASK_IO_ACCOUNTING
Alexey Dobriyan19aadc92014-08-08 14:21:50 -07002669static int do_io_accounting(struct task_struct *task, struct seq_file *m, int whole)
Andrew Mortonaba76fd2006-12-10 02:19:48 -08002670{
Andrea Righi940389b2008-07-28 00:48:12 +02002671 struct task_io_accounting acct = task->ioac;
Andrea Righi59954772008-07-27 17:29:15 +02002672 unsigned long flags;
Vasiliy Kulikov293eb1e2011-07-26 16:08:38 -07002673 int result;
Andrea Righi297c5d92008-07-25 01:48:49 -07002674
Vasiliy Kulikov293eb1e2011-07-26 16:08:38 -07002675 result = mutex_lock_killable(&task->signal->cred_guard_mutex);
2676 if (result)
2677 return result;
2678
Jann Horncaaee622016-01-20 15:00:04 -08002679 if (!ptrace_may_access(task, PTRACE_MODE_READ_FSCREDS)) {
Vasiliy Kulikov293eb1e2011-07-26 16:08:38 -07002680 result = -EACCES;
2681 goto out_unlock;
2682 }
Vasiliy Kulikov1d1221f2011-06-24 16:08:38 +04002683
Andrea Righi59954772008-07-27 17:29:15 +02002684 if (whole && lock_task_sighand(task, &flags)) {
2685 struct task_struct *t = task;
Andrea Righib2d002d2008-07-26 15:22:27 -07002686
Andrea Righi59954772008-07-27 17:29:15 +02002687 task_io_accounting_add(&acct, &task->signal->ioac);
2688 while_each_thread(task, t)
2689 task_io_accounting_add(&acct, &t->ioac);
Andrea Righi297c5d92008-07-25 01:48:49 -07002690
Andrea Righi59954772008-07-27 17:29:15 +02002691 unlock_task_sighand(task, &flags);
Andrea Righi297c5d92008-07-25 01:48:49 -07002692 }
Joe Perches25ce3192015-04-15 16:18:17 -07002693 seq_printf(m,
2694 "rchar: %llu\n"
2695 "wchar: %llu\n"
2696 "syscr: %llu\n"
2697 "syscw: %llu\n"
2698 "read_bytes: %llu\n"
2699 "write_bytes: %llu\n"
2700 "cancelled_write_bytes: %llu\n",
2701 (unsigned long long)acct.rchar,
2702 (unsigned long long)acct.wchar,
2703 (unsigned long long)acct.syscr,
2704 (unsigned long long)acct.syscw,
2705 (unsigned long long)acct.read_bytes,
2706 (unsigned long long)acct.write_bytes,
2707 (unsigned long long)acct.cancelled_write_bytes);
2708 result = 0;
2709
Vasiliy Kulikov293eb1e2011-07-26 16:08:38 -07002710out_unlock:
2711 mutex_unlock(&task->signal->cred_guard_mutex);
2712 return result;
Andrew Mortonaba76fd2006-12-10 02:19:48 -08002713}
Andrea Righi297c5d92008-07-25 01:48:49 -07002714
Alexey Dobriyan19aadc92014-08-08 14:21:50 -07002715static int proc_tid_io_accounting(struct seq_file *m, struct pid_namespace *ns,
2716 struct pid *pid, struct task_struct *task)
Andrea Righi297c5d92008-07-25 01:48:49 -07002717{
Alexey Dobriyan19aadc92014-08-08 14:21:50 -07002718 return do_io_accounting(task, m, 0);
Andrea Righi297c5d92008-07-25 01:48:49 -07002719}
2720
Alexey Dobriyan19aadc92014-08-08 14:21:50 -07002721static int proc_tgid_io_accounting(struct seq_file *m, struct pid_namespace *ns,
2722 struct pid *pid, struct task_struct *task)
Andrea Righi297c5d92008-07-25 01:48:49 -07002723{
Alexey Dobriyan19aadc92014-08-08 14:21:50 -07002724 return do_io_accounting(task, m, 1);
Andrea Righi297c5d92008-07-25 01:48:49 -07002725}
2726#endif /* CONFIG_TASK_IO_ACCOUNTING */
Andrew Mortonaba76fd2006-12-10 02:19:48 -08002727
Eric W. Biederman22d917d2011-11-17 00:11:58 -08002728#ifdef CONFIG_USER_NS
2729static int proc_id_map_open(struct inode *inode, struct file *file,
Fabian Frederickccf94f12014-08-08 14:21:22 -07002730 const struct seq_operations *seq_ops)
Eric W. Biederman22d917d2011-11-17 00:11:58 -08002731{
2732 struct user_namespace *ns = NULL;
2733 struct task_struct *task;
2734 struct seq_file *seq;
2735 int ret = -EINVAL;
2736
2737 task = get_proc_task(inode);
2738 if (task) {
2739 rcu_read_lock();
2740 ns = get_user_ns(task_cred_xxx(task, user_ns));
2741 rcu_read_unlock();
2742 put_task_struct(task);
2743 }
2744 if (!ns)
2745 goto err;
2746
2747 ret = seq_open(file, seq_ops);
2748 if (ret)
2749 goto err_put_ns;
2750
2751 seq = file->private_data;
2752 seq->private = ns;
2753
2754 return 0;
2755err_put_ns:
2756 put_user_ns(ns);
2757err:
2758 return ret;
2759}
2760
2761static int proc_id_map_release(struct inode *inode, struct file *file)
2762{
2763 struct seq_file *seq = file->private_data;
2764 struct user_namespace *ns = seq->private;
2765 put_user_ns(ns);
2766 return seq_release(inode, file);
2767}
2768
2769static int proc_uid_map_open(struct inode *inode, struct file *file)
2770{
2771 return proc_id_map_open(inode, file, &proc_uid_seq_operations);
2772}
2773
2774static int proc_gid_map_open(struct inode *inode, struct file *file)
2775{
2776 return proc_id_map_open(inode, file, &proc_gid_seq_operations);
2777}
2778
Eric W. Biedermanf76d2072012-08-30 01:24:05 -07002779static int proc_projid_map_open(struct inode *inode, struct file *file)
2780{
2781 return proc_id_map_open(inode, file, &proc_projid_seq_operations);
2782}
2783
Eric W. Biederman22d917d2011-11-17 00:11:58 -08002784static const struct file_operations proc_uid_map_operations = {
2785 .open = proc_uid_map_open,
2786 .write = proc_uid_map_write,
2787 .read = seq_read,
2788 .llseek = seq_lseek,
2789 .release = proc_id_map_release,
2790};
2791
2792static const struct file_operations proc_gid_map_operations = {
2793 .open = proc_gid_map_open,
2794 .write = proc_gid_map_write,
2795 .read = seq_read,
2796 .llseek = seq_lseek,
2797 .release = proc_id_map_release,
2798};
Eric W. Biedermanf76d2072012-08-30 01:24:05 -07002799
2800static const struct file_operations proc_projid_map_operations = {
2801 .open = proc_projid_map_open,
2802 .write = proc_projid_map_write,
2803 .read = seq_read,
2804 .llseek = seq_lseek,
2805 .release = proc_id_map_release,
2806};
Eric W. Biederman9cc46512014-12-02 12:27:26 -06002807
2808static int proc_setgroups_open(struct inode *inode, struct file *file)
2809{
2810 struct user_namespace *ns = NULL;
2811 struct task_struct *task;
2812 int ret;
2813
2814 ret = -ESRCH;
2815 task = get_proc_task(inode);
2816 if (task) {
2817 rcu_read_lock();
2818 ns = get_user_ns(task_cred_xxx(task, user_ns));
2819 rcu_read_unlock();
2820 put_task_struct(task);
2821 }
2822 if (!ns)
2823 goto err;
2824
2825 if (file->f_mode & FMODE_WRITE) {
2826 ret = -EACCES;
2827 if (!ns_capable(ns, CAP_SYS_ADMIN))
2828 goto err_put_ns;
2829 }
2830
2831 ret = single_open(file, &proc_setgroups_show, ns);
2832 if (ret)
2833 goto err_put_ns;
2834
2835 return 0;
2836err_put_ns:
2837 put_user_ns(ns);
2838err:
2839 return ret;
2840}
2841
2842static int proc_setgroups_release(struct inode *inode, struct file *file)
2843{
2844 struct seq_file *seq = file->private_data;
2845 struct user_namespace *ns = seq->private;
2846 int ret = single_release(inode, file);
2847 put_user_ns(ns);
2848 return ret;
2849}
2850
2851static const struct file_operations proc_setgroups_operations = {
2852 .open = proc_setgroups_open,
2853 .write = proc_setgroups_write,
2854 .read = seq_read,
2855 .llseek = seq_lseek,
2856 .release = proc_setgroups_release,
2857};
Eric W. Biederman22d917d2011-11-17 00:11:58 -08002858#endif /* CONFIG_USER_NS */
2859
Kees Cook47830722008-10-06 03:11:58 +04002860static int proc_pid_personality(struct seq_file *m, struct pid_namespace *ns,
2861 struct pid *pid, struct task_struct *task)
2862{
Al Viroa9712bc2011-03-23 15:52:50 -04002863 int err = lock_trace(task);
2864 if (!err) {
2865 seq_printf(m, "%08x\n", task->personality);
2866 unlock_trace(task);
2867 }
2868 return err;
Kees Cook47830722008-10-06 03:11:58 +04002869}
2870
Eric W. Biederman801199c2006-10-02 02:18:48 -07002871/*
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002872 * Thread groups
2873 */
Arjan van de Ven00977a52007-02-12 00:55:34 -08002874static const struct file_operations proc_task_operations;
Arjan van de Venc5ef1c42007-02-12 00:55:40 -08002875static const struct inode_operations proc_task_inode_operations;
Eric W. Biederman20cdc892006-10-02 02:17:07 -07002876
Eric Dumazetc5141e62007-05-08 00:26:15 -07002877static const struct pid_entry tgid_base_stuff[] = {
Alexey Dobriyan631f9c12008-11-10 01:32:52 +03002878 DIR("task", S_IRUGO|S_IXUGO, proc_task_inode_operations, proc_task_operations),
2879 DIR("fd", S_IRUSR|S_IXUSR, proc_fd_inode_operations, proc_fd_operations),
Pavel Emelyanov640708a2012-01-10 15:11:23 -08002880 DIR("map_files", S_IRUSR|S_IXUSR, proc_map_files_inode_operations, proc_map_files_operations),
Alexey Dobriyan631f9c12008-11-10 01:32:52 +03002881 DIR("fdinfo", S_IRUSR|S_IXUSR, proc_fdinfo_inode_operations, proc_fdinfo_operations),
Eric W. Biederman6b4e3062010-03-07 16:41:34 -08002882 DIR("ns", S_IRUSR|S_IXUGO, proc_ns_dir_inode_operations, proc_ns_dir_operations),
Andrew Mortonb2211a32008-03-11 18:03:35 -07002883#ifdef CONFIG_NET
Alexey Dobriyan631f9c12008-11-10 01:32:52 +03002884 DIR("net", S_IRUGO|S_IXUGO, proc_net_inode_operations, proc_net_operations),
Andrew Mortonb2211a32008-03-11 18:03:35 -07002885#endif
Alexey Dobriyan631f9c12008-11-10 01:32:52 +03002886 REG("environ", S_IRUSR, proc_environ_operations),
Al Viroc5317162016-10-05 18:43:43 -04002887 REG("auxv", S_IRUSR, proc_auxv_operations),
Alexey Dobriyan631f9c12008-11-10 01:32:52 +03002888 ONE("status", S_IRUGO, proc_pid_status),
Djalal Harouni35a35042014-04-07 15:38:36 -07002889 ONE("personality", S_IRUSR, proc_pid_personality),
Alexey Dobriyan1c963eb2014-08-08 14:21:37 -07002890 ONE("limits", S_IRUGO, proc_pid_limits),
Ingo Molnar43ae34c2007-07-09 18:52:00 +02002891#ifdef CONFIG_SCHED_DEBUG
Alexey Dobriyan631f9c12008-11-10 01:32:52 +03002892 REG("sched", S_IRUGO|S_IWUSR, proc_pid_sched_operations),
Ingo Molnar43ae34c2007-07-09 18:52:00 +02002893#endif
Mike Galbraith5091faa2010-11-30 14:18:03 +01002894#ifdef CONFIG_SCHED_AUTOGROUP
2895 REG("autogroup", S_IRUGO|S_IWUSR, proc_pid_sched_autogroup_operations),
2896#endif
john stultz4614a696b2009-12-14 18:00:05 -08002897 REG("comm", S_IRUGO|S_IWUSR, proc_pid_set_comm_operations),
Roland McGrathebcb6732008-07-25 19:46:00 -07002898#ifdef CONFIG_HAVE_ARCH_TRACEHOOK
Alexey Dobriyan09d93bd2014-08-08 14:21:39 -07002899 ONE("syscall", S_IRUSR, proc_pid_syscall),
Roland McGrathebcb6732008-07-25 19:46:00 -07002900#endif
Alexey Dobriyanc2c0bb42015-06-25 15:00:54 -07002901 REG("cmdline", S_IRUGO, proc_pid_cmdline_ops),
Alexey Dobriyan631f9c12008-11-10 01:32:52 +03002902 ONE("stat", S_IRUGO, proc_tgid_stat),
2903 ONE("statm", S_IRUGO, proc_pid_statm),
Siddhesh Poyarekarb7643752012-03-21 16:34:04 -07002904 REG("maps", S_IRUGO, proc_pid_maps_operations),
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002905#ifdef CONFIG_NUMA
Siddhesh Poyarekarb7643752012-03-21 16:34:04 -07002906 REG("numa_maps", S_IRUGO, proc_pid_numa_maps_operations),
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002907#endif
Alexey Dobriyan631f9c12008-11-10 01:32:52 +03002908 REG("mem", S_IRUSR|S_IWUSR, proc_mem_operations),
2909 LNK("cwd", proc_cwd_link),
2910 LNK("root", proc_root_link),
2911 LNK("exe", proc_exe_link),
2912 REG("mounts", S_IRUGO, proc_mounts_operations),
2913 REG("mountinfo", S_IRUGO, proc_mountinfo_operations),
2914 REG("mountstats", S_IRUSR, proc_mountstats_operations),
Matt Mackall1e883282008-02-04 22:29:07 -08002915#ifdef CONFIG_PROC_PAGE_MONITOR
Alexey Dobriyan631f9c12008-11-10 01:32:52 +03002916 REG("clear_refs", S_IWUSR, proc_clear_refs_operations),
Siddhesh Poyarekarb7643752012-03-21 16:34:04 -07002917 REG("smaps", S_IRUGO, proc_pid_smaps_operations),
Djalal Harouni32ed74a2014-04-07 15:38:38 -07002918 REG("pagemap", S_IRUSR, proc_pagemap_operations),
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002919#endif
2920#ifdef CONFIG_SECURITY
Alexey Dobriyan631f9c12008-11-10 01:32:52 +03002921 DIR("attr", S_IRUGO|S_IXUGO, proc_attr_dir_inode_operations, proc_attr_dir_operations),
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002922#endif
2923#ifdef CONFIG_KALLSYMS
Alexey Dobriyanedfcd602014-08-08 14:21:44 -07002924 ONE("wchan", S_IRUGO, proc_pid_wchan),
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002925#endif
Ken Chen2ec220e2008-11-10 11:26:08 +03002926#ifdef CONFIG_STACKTRACE
Djalal Harouni35a35042014-04-07 15:38:36 -07002927 ONE("stack", S_IRUSR, proc_pid_stack),
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002928#endif
Naveen N. Rao5968cec2015-06-30 14:36:03 +05302929#ifdef CONFIG_SCHED_INFO
Alexey Dobriyanf6e826c2014-08-08 14:21:46 -07002930 ONE("schedstat", S_IRUGO, proc_pid_schedstat),
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002931#endif
Arjan van de Ven97455122008-01-25 21:08:34 +01002932#ifdef CONFIG_LATENCYTOP
Alexey Dobriyan631f9c12008-11-10 01:32:52 +03002933 REG("latency", S_IRUGO, proc_lstats_operations),
Arjan van de Ven97455122008-01-25 21:08:34 +01002934#endif
Paul Menage8793d852007-10-18 23:39:39 -07002935#ifdef CONFIG_PROC_PID_CPUSET
Zefan Li52de4772014-09-18 16:03:36 +08002936 ONE("cpuset", S_IRUGO, proc_cpuset_show),
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002937#endif
Paul Menagea4243162007-10-18 23:39:35 -07002938#ifdef CONFIG_CGROUPS
Zefan Li006f4ac2014-09-18 16:03:15 +08002939 ONE("cgroup", S_IRUGO, proc_cgroup_show),
Paul Menagea4243162007-10-18 23:39:35 -07002940#endif
Alexey Dobriyan6ba51e32014-08-08 14:21:48 -07002941 ONE("oom_score", S_IRUGO, proc_oom_score),
David Rientjesfa0cbbf2012-11-12 17:53:04 -08002942 REG("oom_adj", S_IRUGO|S_IWUSR, proc_oom_adj_operations),
David Rientjesa63d83f2010-08-09 17:19:46 -07002943 REG("oom_score_adj", S_IRUGO|S_IWUSR, proc_oom_score_adj_operations),
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002944#ifdef CONFIG_AUDITSYSCALL
Alexey Dobriyan631f9c12008-11-10 01:32:52 +03002945 REG("loginuid", S_IWUSR|S_IRUGO, proc_loginuid_operations),
2946 REG("sessionid", S_IRUGO, proc_sessionid_operations),
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002947#endif
Akinobu Mitaf4f154f2006-12-08 02:39:47 -08002948#ifdef CONFIG_FAULT_INJECTION
Alexey Dobriyan631f9c12008-11-10 01:32:52 +03002949 REG("make-it-fail", S_IRUGO|S_IWUSR, proc_fault_inject_operations),
Akinobu Mitaf4f154f2006-12-08 02:39:47 -08002950#endif
Christoph Hellwig698ba7b2009-12-15 16:47:37 -08002951#ifdef CONFIG_ELF_CORE
Alexey Dobriyan631f9c12008-11-10 01:32:52 +03002952 REG("coredump_filter", S_IRUGO|S_IWUSR, proc_coredump_filter_operations),
Kawai, Hidehiro3cb4a0b2007-07-19 01:48:28 -07002953#endif
Andrew Mortonaba76fd2006-12-10 02:19:48 -08002954#ifdef CONFIG_TASK_IO_ACCOUNTING
Alexey Dobriyan19aadc92014-08-08 14:21:50 -07002955 ONE("io", S_IRUSR, proc_tgid_io_accounting),
Andrew Mortonaba76fd2006-12-10 02:19:48 -08002956#endif
Chris Metcalff133ecc2011-05-26 12:40:09 -04002957#ifdef CONFIG_HARDWALL
Alexey Dobriyand962c142014-08-08 14:21:52 -07002958 ONE("hardwall", S_IRUGO, proc_pid_hardwall),
Chris Metcalff133ecc2011-05-26 12:40:09 -04002959#endif
Eric W. Biederman22d917d2011-11-17 00:11:58 -08002960#ifdef CONFIG_USER_NS
2961 REG("uid_map", S_IRUGO|S_IWUSR, proc_uid_map_operations),
2962 REG("gid_map", S_IRUGO|S_IWUSR, proc_gid_map_operations),
Eric W. Biedermanf76d2072012-08-30 01:24:05 -07002963 REG("projid_map", S_IRUGO|S_IWUSR, proc_projid_map_operations),
Eric W. Biederman9cc46512014-12-02 12:27:26 -06002964 REG("setgroups", S_IRUGO|S_IWUSR, proc_setgroups_operations),
Eric W. Biederman22d917d2011-11-17 00:11:58 -08002965#endif
Pavel Emelyanov48f6a7a2013-03-11 13:12:45 +04002966#ifdef CONFIG_CHECKPOINT_RESTORE
2967 REG("timers", S_IRUGO, proc_timers_operations),
2968#endif
John Stultz5de23d42016-03-17 14:20:54 -07002969 REG("timerslack_ns", S_IRUGO|S_IWUGO, proc_pid_set_timerslack_ns_operations),
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002970};
2971
Al Virof0c3b502013-05-16 12:07:31 -04002972static int proc_tgid_base_readdir(struct file *file, struct dir_context *ctx)
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002973{
Al Virof0c3b502013-05-16 12:07:31 -04002974 return proc_pident_readdir(file, ctx,
2975 tgid_base_stuff, ARRAY_SIZE(tgid_base_stuff));
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002976}
2977
Arjan van de Ven00977a52007-02-12 00:55:34 -08002978static const struct file_operations proc_tgid_base_operations = {
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002979 .read = generic_read_dir,
Al Virof50752e2016-04-20 17:13:54 -04002980 .iterate_shared = proc_tgid_base_readdir,
2981 .llseek = generic_file_llseek,
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002982};
2983
Al Viro00cd8dd2012-06-10 17:13:09 -04002984static struct dentry *proc_tgid_base_lookup(struct inode *dir, struct dentry *dentry, unsigned int flags)
2985{
Eric W. Biederman7bcd6b02006-10-02 02:18:56 -07002986 return proc_pident_lookup(dir, dentry,
2987 tgid_base_stuff, ARRAY_SIZE(tgid_base_stuff));
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002988}
2989
Arjan van de Venc5ef1c42007-02-12 00:55:40 -08002990static const struct inode_operations proc_tgid_base_inode_operations = {
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002991 .lookup = proc_tgid_base_lookup,
2992 .getattr = pid_getattr,
2993 .setattr = proc_setattr,
Vasiliy Kulikov04996802012-01-10 15:11:31 -08002994 .permission = proc_pid_permission,
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002995};
2996
Pavel Emelyanov60347f62007-10-18 23:40:03 -07002997static void proc_flush_task_mnt(struct vfsmount *mnt, pid_t pid, pid_t tgid)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002998{
Eric W. Biederman48e64842006-06-26 00:25:48 -07002999 struct dentry *dentry, *leader, *dir;
Eric W. Biederman8578cea2006-06-26 00:25:54 -07003000 char buf[PROC_NUMBUF];
Eric W. Biederman48e64842006-06-26 00:25:48 -07003001 struct qstr name;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003002
Eric W. Biederman48e64842006-06-26 00:25:48 -07003003 name.name = buf;
Pavel Emelyanov60347f62007-10-18 23:40:03 -07003004 name.len = snprintf(buf, sizeof(buf), "%d", pid);
Al Viro4f522a22013-02-11 23:20:37 -05003005 /* no ->d_hash() rejects on procfs */
Pavel Emelyanov60347f62007-10-18 23:40:03 -07003006 dentry = d_hash_and_lookup(mnt->mnt_root, &name);
Eric W. Biederman48e64842006-06-26 00:25:48 -07003007 if (dentry) {
Eric W. Biedermanbbd51922014-02-13 10:24:23 -08003008 d_invalidate(dentry);
Eric W. Biederman48e64842006-06-26 00:25:48 -07003009 dput(dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003010 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003011
Oleg Nesterovc35a7f12014-12-10 15:54:56 -08003012 if (pid == tgid)
3013 return;
3014
Eric W. Biederman48e64842006-06-26 00:25:48 -07003015 name.name = buf;
Pavel Emelyanov60347f62007-10-18 23:40:03 -07003016 name.len = snprintf(buf, sizeof(buf), "%d", tgid);
3017 leader = d_hash_and_lookup(mnt->mnt_root, &name);
Eric W. Biederman48e64842006-06-26 00:25:48 -07003018 if (!leader)
3019 goto out;
3020
3021 name.name = "task";
3022 name.len = strlen(name.name);
3023 dir = d_hash_and_lookup(leader, &name);
3024 if (!dir)
3025 goto out_put_leader;
3026
3027 name.name = buf;
Pavel Emelyanov60347f62007-10-18 23:40:03 -07003028 name.len = snprintf(buf, sizeof(buf), "%d", pid);
Eric W. Biederman48e64842006-06-26 00:25:48 -07003029 dentry = d_hash_and_lookup(dir, &name);
3030 if (dentry) {
Eric W. Biedermanbbd51922014-02-13 10:24:23 -08003031 d_invalidate(dentry);
Eric W. Biederman48e64842006-06-26 00:25:48 -07003032 dput(dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003033 }
Eric W. Biederman48e64842006-06-26 00:25:48 -07003034
3035 dput(dir);
3036out_put_leader:
3037 dput(leader);
3038out:
3039 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003040}
3041
Randy Dunlap0895e912007-10-21 21:00:10 -07003042/**
3043 * proc_flush_task - Remove dcache entries for @task from the /proc dcache.
3044 * @task: task that should be flushed.
3045 *
3046 * When flushing dentries from proc, one needs to flush them from global
Pavel Emelyanov60347f62007-10-18 23:40:03 -07003047 * proc (proc_mnt) and from all the namespaces' procs this task was seen
Randy Dunlap0895e912007-10-21 21:00:10 -07003048 * in. This call is supposed to do all of this job.
3049 *
3050 * Looks in the dcache for
3051 * /proc/@pid
3052 * /proc/@tgid/task/@pid
3053 * if either directory is present flushes it and all of it'ts children
3054 * from the dcache.
3055 *
3056 * It is safe and reasonable to cache /proc entries for a task until
3057 * that task exits. After that they just clog up the dcache with
3058 * useless entries, possibly causing useful dcache entries to be
3059 * flushed instead. This routine is proved to flush those useless
3060 * dcache entries at process exit time.
3061 *
3062 * NOTE: This routine is just an optimization so it does not guarantee
3063 * that no dcache entries will exist at process exit time it
3064 * just makes it very unlikely that any will persist.
Pavel Emelyanov60347f62007-10-18 23:40:03 -07003065 */
3066
3067void proc_flush_task(struct task_struct *task)
3068{
Eric W. Biederman9fcc2d12007-11-14 17:00:07 -08003069 int i;
Oleg Nesterov9b4d1cbe2009-09-22 16:45:34 -07003070 struct pid *pid, *tgid;
Pavel Emelyanov130f77e2007-10-18 23:40:11 -07003071 struct upid *upid;
3072
Pavel Emelyanov130f77e2007-10-18 23:40:11 -07003073 pid = task_pid(task);
Oleg Nesterov9b4d1cbe2009-09-22 16:45:34 -07003074 tgid = task_tgid(task);
Pavel Emelyanov130f77e2007-10-18 23:40:11 -07003075
Eric W. Biederman9fcc2d12007-11-14 17:00:07 -08003076 for (i = 0; i <= pid->level; i++) {
Pavel Emelyanov130f77e2007-10-18 23:40:11 -07003077 upid = &pid->numbers[i];
3078 proc_flush_task_mnt(upid->ns->proc_mnt, upid->nr,
Oleg Nesterov9b4d1cbe2009-09-22 16:45:34 -07003079 tgid->numbers[i].nr);
Pavel Emelyanov130f77e2007-10-18 23:40:11 -07003080 }
Pavel Emelyanov60347f62007-10-18 23:40:03 -07003081}
3082
Al Viroc52a47a2013-06-15 11:15:20 +04003083static int proc_pid_instantiate(struct inode *dir,
3084 struct dentry * dentry,
3085 struct task_struct *task, const void *ptr)
Eric W. Biederman444ceed2006-10-02 02:18:49 -07003086{
Eric W. Biederman444ceed2006-10-02 02:18:49 -07003087 struct inode *inode;
3088
Eric W. Biederman61a28782006-10-02 02:18:49 -07003089 inode = proc_pid_make_inode(dir->i_sb, task);
Eric W. Biederman444ceed2006-10-02 02:18:49 -07003090 if (!inode)
3091 goto out;
3092
3093 inode->i_mode = S_IFDIR|S_IRUGO|S_IXUGO;
3094 inode->i_op = &proc_tgid_base_inode_operations;
3095 inode->i_fop = &proc_tgid_base_operations;
3096 inode->i_flags|=S_IMMUTABLE;
Vegard Nossumaed54172008-06-05 22:46:53 -07003097
Miklos Szeredibfe86842011-10-28 14:13:29 +02003098 set_nlink(inode, 2 + pid_entry_count_dirs(tgid_base_stuff,
3099 ARRAY_SIZE(tgid_base_stuff)));
Eric W. Biederman444ceed2006-10-02 02:18:49 -07003100
Nick Pigginfb045ad2011-01-07 17:49:55 +11003101 d_set_d_op(dentry, &pid_dentry_operations);
Eric W. Biederman444ceed2006-10-02 02:18:49 -07003102
3103 d_add(dentry, inode);
3104 /* Close the race of the process dying before we return the dentry */
Al Viro0b728e12012-06-10 16:03:43 -04003105 if (pid_revalidate(dentry, 0))
Al Viroc52a47a2013-06-15 11:15:20 +04003106 return 0;
Eric W. Biederman444ceed2006-10-02 02:18:49 -07003107out:
Al Viroc52a47a2013-06-15 11:15:20 +04003108 return -ENOENT;
Eric W. Biederman444ceed2006-10-02 02:18:49 -07003109}
3110
Al Viro00cd8dd2012-06-10 17:13:09 -04003111struct dentry *proc_pid_lookup(struct inode *dir, struct dentry * dentry, unsigned int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003112{
Alexey Dobriyan335eb532014-08-08 14:21:27 -07003113 int result = -ENOENT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003114 struct task_struct *task;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003115 unsigned tgid;
Pavel Emelyanovb4888932007-10-18 23:40:14 -07003116 struct pid_namespace *ns;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003117
Alexey Dobriyandbcdb502014-08-08 14:21:25 -07003118 tgid = name_to_int(&dentry->d_name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003119 if (tgid == ~0U)
3120 goto out;
3121
Pavel Emelyanovb4888932007-10-18 23:40:14 -07003122 ns = dentry->d_sb->s_fs_info;
Eric W. Biedermande758732006-06-26 00:25:51 -07003123 rcu_read_lock();
Pavel Emelyanovb4888932007-10-18 23:40:14 -07003124 task = find_task_by_pid_ns(tgid, ns);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003125 if (task)
3126 get_task_struct(task);
Eric W. Biedermande758732006-06-26 00:25:51 -07003127 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07003128 if (!task)
3129 goto out;
3130
Eric W. Biederman444ceed2006-10-02 02:18:49 -07003131 result = proc_pid_instantiate(dir, dentry, task, NULL);
Eric W. Biederman48e64842006-06-26 00:25:48 -07003132 put_task_struct(task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003133out:
Al Viroc52a47a2013-06-15 11:15:20 +04003134 return ERR_PTR(result);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003135}
3136
Linus Torvalds1da177e2005-04-16 15:20:36 -07003137/*
Eric W. Biederman0804ef42006-10-02 02:17:04 -07003138 * Find the first task with tgid >= tgid
Eric W. Biederman0bc58a92006-06-26 00:25:50 -07003139 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07003140 */
Eric W. Biederman19fd4bb2007-11-28 16:21:26 -08003141struct tgid_iter {
3142 unsigned int tgid;
Eric W. Biederman0804ef42006-10-02 02:17:04 -07003143 struct task_struct *task;
Eric W. Biederman19fd4bb2007-11-28 16:21:26 -08003144};
3145static struct tgid_iter next_tgid(struct pid_namespace *ns, struct tgid_iter iter)
3146{
Eric W. Biederman0804ef42006-10-02 02:17:04 -07003147 struct pid *pid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003148
Eric W. Biederman19fd4bb2007-11-28 16:21:26 -08003149 if (iter.task)
3150 put_task_struct(iter.task);
Eric W. Biederman0804ef42006-10-02 02:17:04 -07003151 rcu_read_lock();
3152retry:
Eric W. Biederman19fd4bb2007-11-28 16:21:26 -08003153 iter.task = NULL;
3154 pid = find_ge_pid(iter.tgid, ns);
Eric W. Biederman0804ef42006-10-02 02:17:04 -07003155 if (pid) {
Eric W. Biederman19fd4bb2007-11-28 16:21:26 -08003156 iter.tgid = pid_nr_ns(pid, ns);
3157 iter.task = pid_task(pid, PIDTYPE_PID);
Eric W. Biederman0804ef42006-10-02 02:17:04 -07003158 /* What we to know is if the pid we have find is the
3159 * pid of a thread_group_leader. Testing for task
3160 * being a thread_group_leader is the obvious thing
3161 * todo but there is a window when it fails, due to
3162 * the pid transfer logic in de_thread.
3163 *
3164 * So we perform the straight forward test of seeing
3165 * if the pid we have found is the pid of a thread
3166 * group leader, and don't worry if the task we have
3167 * found doesn't happen to be a thread group leader.
3168 * As we don't care in the case of readdir.
3169 */
Eric W. Biederman19fd4bb2007-11-28 16:21:26 -08003170 if (!iter.task || !has_group_leader_pid(iter.task)) {
3171 iter.tgid += 1;
Eric W. Biederman0804ef42006-10-02 02:17:04 -07003172 goto retry;
Eric W. Biederman19fd4bb2007-11-28 16:21:26 -08003173 }
3174 get_task_struct(iter.task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003175 }
Eric W. Biederman454cc102006-06-26 00:25:51 -07003176 rcu_read_unlock();
Eric W. Biederman19fd4bb2007-11-28 16:21:26 -08003177 return iter;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003178}
3179
Eric W. Biederman00978752014-07-31 03:10:50 -07003180#define TGID_OFFSET (FIRST_PROCESS_ENTRY + 2)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003181
3182/* for the /proc/ directory itself, after non-process stuff has been done */
Al Virof0c3b502013-05-16 12:07:31 -04003183int proc_pid_readdir(struct file *file, struct dir_context *ctx)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003184{
Eric W. Biederman19fd4bb2007-11-28 16:21:26 -08003185 struct tgid_iter iter;
Al Viro3aa33772014-10-31 00:42:35 -04003186 struct pid_namespace *ns = file_inode(file)->i_sb->s_fs_info;
Al Virof0c3b502013-05-16 12:07:31 -04003187 loff_t pos = ctx->pos;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003188
Al Viro021ada72013-03-29 19:27:05 -04003189 if (pos >= PID_MAX_LIMIT + TGID_OFFSET)
Al Virof0c3b502013-05-16 12:07:31 -04003190 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003191
Eric W. Biederman00978752014-07-31 03:10:50 -07003192 if (pos == TGID_OFFSET - 2) {
David Howells2b0143b2015-03-17 22:25:59 +00003193 struct inode *inode = d_inode(ns->proc_self);
Al Virodb963162013-06-15 10:45:10 +04003194 if (!dir_emit(ctx, "self", 4, inode->i_ino, DT_LNK))
Al Virof0c3b502013-05-16 12:07:31 -04003195 return 0;
Eric W. Biederman00978752014-07-31 03:10:50 -07003196 ctx->pos = pos = pos + 1;
Al Viro021ada72013-03-29 19:27:05 -04003197 }
Eric W. Biederman00978752014-07-31 03:10:50 -07003198 if (pos == TGID_OFFSET - 1) {
David Howells2b0143b2015-03-17 22:25:59 +00003199 struct inode *inode = d_inode(ns->proc_thread_self);
Eric W. Biederman00978752014-07-31 03:10:50 -07003200 if (!dir_emit(ctx, "thread-self", 11, inode->i_ino, DT_LNK))
3201 return 0;
3202 ctx->pos = pos = pos + 1;
3203 }
3204 iter.tgid = pos - TGID_OFFSET;
Eric W. Biederman19fd4bb2007-11-28 16:21:26 -08003205 iter.task = NULL;
Eric W. Biederman19fd4bb2007-11-28 16:21:26 -08003206 for (iter = next_tgid(ns, iter);
3207 iter.task;
3208 iter.tgid += 1, iter = next_tgid(ns, iter)) {
Al Virof0c3b502013-05-16 12:07:31 -04003209 char name[PROC_NUMBUF];
3210 int len;
Eric Dumazet9618fba2017-01-24 15:18:07 -08003211
3212 cond_resched();
Al Virof0c3b502013-05-16 12:07:31 -04003213 if (!has_pid_permissions(ns, iter.task, 2))
3214 continue;
Vasiliy Kulikov04996802012-01-10 15:11:31 -08003215
Al Virof0c3b502013-05-16 12:07:31 -04003216 len = snprintf(name, sizeof(name), "%d", iter.tgid);
3217 ctx->pos = iter.tgid + TGID_OFFSET;
3218 if (!proc_fill_cache(file, ctx, name, len,
3219 proc_pid_instantiate, iter.task, NULL)) {
Eric W. Biederman19fd4bb2007-11-28 16:21:26 -08003220 put_task_struct(iter.task);
Al Virof0c3b502013-05-16 12:07:31 -04003221 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003222 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003223 }
Al Virof0c3b502013-05-16 12:07:31 -04003224 ctx->pos = PID_MAX_LIMIT + TGID_OFFSET;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003225 return 0;
3226}
3227
Eric W. Biederman0bc58a92006-06-26 00:25:50 -07003228/*
Janis Danisevskis1b3044e2016-05-20 17:00:08 -07003229 * proc_tid_comm_permission is a special permission function exclusively
3230 * used for the node /proc/<pid>/task/<tid>/comm.
3231 * It bypasses generic permission checks in the case where a task of the same
3232 * task group attempts to access the node.
3233 * The rationale behind this is that glibc and bionic access this node for
3234 * cross thread naming (pthread_set/getname_np(!self)). However, if
3235 * PR_SET_DUMPABLE gets set to 0 this node among others becomes uid=0 gid=0,
3236 * which locks out the cross thread naming implementation.
3237 * This function makes sure that the node is always accessible for members of
3238 * same thread group.
3239 */
3240static int proc_tid_comm_permission(struct inode *inode, int mask)
3241{
3242 bool is_same_tgroup;
3243 struct task_struct *task;
3244
3245 task = get_proc_task(inode);
3246 if (!task)
3247 return -ESRCH;
3248 is_same_tgroup = same_thread_group(current, task);
3249 put_task_struct(task);
3250
3251 if (likely(is_same_tgroup && !(mask & MAY_EXEC))) {
3252 /* This file (/proc/<pid>/task/<tid>/comm) can always be
3253 * read or written by the members of the corresponding
3254 * thread group.
3255 */
3256 return 0;
3257 }
3258
3259 return generic_permission(inode, mask);
3260}
3261
3262static const struct inode_operations proc_tid_comm_inode_operations = {
3263 .permission = proc_tid_comm_permission,
3264};
3265
3266/*
Eric W. Biederman28a6d672006-10-02 02:17:05 -07003267 * Tasks
3268 */
Eric Dumazetc5141e62007-05-08 00:26:15 -07003269static const struct pid_entry tid_base_stuff[] = {
Alexey Dobriyan631f9c12008-11-10 01:32:52 +03003270 DIR("fd", S_IRUSR|S_IXUSR, proc_fd_inode_operations, proc_fd_operations),
Jerome Marchand38355412010-04-27 13:13:06 -07003271 DIR("fdinfo", S_IRUSR|S_IXUSR, proc_fdinfo_inode_operations, proc_fdinfo_operations),
Eric W. Biederman6b4e3062010-03-07 16:41:34 -08003272 DIR("ns", S_IRUSR|S_IXUGO, proc_ns_dir_inode_operations, proc_ns_dir_operations),
Eric W. Biederman6ba8ed72014-07-31 16:27:08 -07003273#ifdef CONFIG_NET
3274 DIR("net", S_IRUGO|S_IXUGO, proc_net_inode_operations, proc_net_operations),
3275#endif
Alexey Dobriyan631f9c12008-11-10 01:32:52 +03003276 REG("environ", S_IRUSR, proc_environ_operations),
Al Viroc5317162016-10-05 18:43:43 -04003277 REG("auxv", S_IRUSR, proc_auxv_operations),
Alexey Dobriyan631f9c12008-11-10 01:32:52 +03003278 ONE("status", S_IRUGO, proc_pid_status),
Djalal Harouni35a35042014-04-07 15:38:36 -07003279 ONE("personality", S_IRUSR, proc_pid_personality),
Alexey Dobriyan1c963eb2014-08-08 14:21:37 -07003280 ONE("limits", S_IRUGO, proc_pid_limits),
Ingo Molnar43ae34c2007-07-09 18:52:00 +02003281#ifdef CONFIG_SCHED_DEBUG
Alexey Dobriyan631f9c12008-11-10 01:32:52 +03003282 REG("sched", S_IRUGO|S_IWUSR, proc_pid_sched_operations),
Ingo Molnar43ae34c2007-07-09 18:52:00 +02003283#endif
Janis Danisevskis1b3044e2016-05-20 17:00:08 -07003284 NOD("comm", S_IFREG|S_IRUGO|S_IWUSR,
3285 &proc_tid_comm_inode_operations,
3286 &proc_pid_set_comm_operations, {}),
Roland McGrathebcb6732008-07-25 19:46:00 -07003287#ifdef CONFIG_HAVE_ARCH_TRACEHOOK
Alexey Dobriyan09d93bd2014-08-08 14:21:39 -07003288 ONE("syscall", S_IRUSR, proc_pid_syscall),
Roland McGrathebcb6732008-07-25 19:46:00 -07003289#endif
Alexey Dobriyanc2c0bb42015-06-25 15:00:54 -07003290 REG("cmdline", S_IRUGO, proc_pid_cmdline_ops),
Alexey Dobriyan631f9c12008-11-10 01:32:52 +03003291 ONE("stat", S_IRUGO, proc_tid_stat),
3292 ONE("statm", S_IRUGO, proc_pid_statm),
Siddhesh Poyarekarb7643752012-03-21 16:34:04 -07003293 REG("maps", S_IRUGO, proc_tid_maps_operations),
Iago López Galeiras2e13ba52015-06-25 15:00:57 -07003294#ifdef CONFIG_PROC_CHILDREN
Cyrill Gorcunov818411612012-05-31 16:26:43 -07003295 REG("children", S_IRUGO, proc_tid_children_operations),
3296#endif
Eric W. Biederman28a6d672006-10-02 02:17:05 -07003297#ifdef CONFIG_NUMA
Siddhesh Poyarekarb7643752012-03-21 16:34:04 -07003298 REG("numa_maps", S_IRUGO, proc_tid_numa_maps_operations),
Eric W. Biederman28a6d672006-10-02 02:17:05 -07003299#endif
Alexey Dobriyan631f9c12008-11-10 01:32:52 +03003300 REG("mem", S_IRUSR|S_IWUSR, proc_mem_operations),
3301 LNK("cwd", proc_cwd_link),
3302 LNK("root", proc_root_link),
3303 LNK("exe", proc_exe_link),
3304 REG("mounts", S_IRUGO, proc_mounts_operations),
3305 REG("mountinfo", S_IRUGO, proc_mountinfo_operations),
Matt Mackall1e883282008-02-04 22:29:07 -08003306#ifdef CONFIG_PROC_PAGE_MONITOR
Alexey Dobriyan631f9c12008-11-10 01:32:52 +03003307 REG("clear_refs", S_IWUSR, proc_clear_refs_operations),
Siddhesh Poyarekarb7643752012-03-21 16:34:04 -07003308 REG("smaps", S_IRUGO, proc_tid_smaps_operations),
Djalal Harouni32ed74a2014-04-07 15:38:38 -07003309 REG("pagemap", S_IRUSR, proc_pagemap_operations),
Eric W. Biederman28a6d672006-10-02 02:17:05 -07003310#endif
3311#ifdef CONFIG_SECURITY
Alexey Dobriyan631f9c12008-11-10 01:32:52 +03003312 DIR("attr", S_IRUGO|S_IXUGO, proc_attr_dir_inode_operations, proc_attr_dir_operations),
Eric W. Biederman28a6d672006-10-02 02:17:05 -07003313#endif
3314#ifdef CONFIG_KALLSYMS
Alexey Dobriyanedfcd602014-08-08 14:21:44 -07003315 ONE("wchan", S_IRUGO, proc_pid_wchan),
Eric W. Biederman28a6d672006-10-02 02:17:05 -07003316#endif
Ken Chen2ec220e2008-11-10 11:26:08 +03003317#ifdef CONFIG_STACKTRACE
Djalal Harouni35a35042014-04-07 15:38:36 -07003318 ONE("stack", S_IRUSR, proc_pid_stack),
Eric W. Biederman28a6d672006-10-02 02:17:05 -07003319#endif
Naveen N. Rao5968cec2015-06-30 14:36:03 +05303320#ifdef CONFIG_SCHED_INFO
Alexey Dobriyanf6e826c2014-08-08 14:21:46 -07003321 ONE("schedstat", S_IRUGO, proc_pid_schedstat),
Eric W. Biederman28a6d672006-10-02 02:17:05 -07003322#endif
Arjan van de Ven97455122008-01-25 21:08:34 +01003323#ifdef CONFIG_LATENCYTOP
Alexey Dobriyan631f9c12008-11-10 01:32:52 +03003324 REG("latency", S_IRUGO, proc_lstats_operations),
Arjan van de Ven97455122008-01-25 21:08:34 +01003325#endif
Paul Menage8793d852007-10-18 23:39:39 -07003326#ifdef CONFIG_PROC_PID_CPUSET
Zefan Li52de4772014-09-18 16:03:36 +08003327 ONE("cpuset", S_IRUGO, proc_cpuset_show),
Eric W. Biederman28a6d672006-10-02 02:17:05 -07003328#endif
Paul Menagea4243162007-10-18 23:39:35 -07003329#ifdef CONFIG_CGROUPS
Zefan Li006f4ac2014-09-18 16:03:15 +08003330 ONE("cgroup", S_IRUGO, proc_cgroup_show),
Paul Menagea4243162007-10-18 23:39:35 -07003331#endif
Alexey Dobriyan6ba51e32014-08-08 14:21:48 -07003332 ONE("oom_score", S_IRUGO, proc_oom_score),
David Rientjesfa0cbbf2012-11-12 17:53:04 -08003333 REG("oom_adj", S_IRUGO|S_IWUSR, proc_oom_adj_operations),
David Rientjesa63d83f2010-08-09 17:19:46 -07003334 REG("oom_score_adj", S_IRUGO|S_IWUSR, proc_oom_score_adj_operations),
Eric W. Biederman28a6d672006-10-02 02:17:05 -07003335#ifdef CONFIG_AUDITSYSCALL
Alexey Dobriyan631f9c12008-11-10 01:32:52 +03003336 REG("loginuid", S_IWUSR|S_IRUGO, proc_loginuid_operations),
Al Viro26ec3c62011-02-15 21:24:05 -05003337 REG("sessionid", S_IRUGO, proc_sessionid_operations),
Eric W. Biederman28a6d672006-10-02 02:17:05 -07003338#endif
Akinobu Mitaf4f154f2006-12-08 02:39:47 -08003339#ifdef CONFIG_FAULT_INJECTION
Alexey Dobriyan631f9c12008-11-10 01:32:52 +03003340 REG("make-it-fail", S_IRUGO|S_IWUSR, proc_fault_inject_operations),
Akinobu Mitaf4f154f2006-12-08 02:39:47 -08003341#endif
Andrea Righi297c5d92008-07-25 01:48:49 -07003342#ifdef CONFIG_TASK_IO_ACCOUNTING
Alexey Dobriyan19aadc92014-08-08 14:21:50 -07003343 ONE("io", S_IRUSR, proc_tid_io_accounting),
Andrea Righi297c5d92008-07-25 01:48:49 -07003344#endif
Chris Metcalff133ecc2011-05-26 12:40:09 -04003345#ifdef CONFIG_HARDWALL
Alexey Dobriyand962c142014-08-08 14:21:52 -07003346 ONE("hardwall", S_IRUGO, proc_pid_hardwall),
Chris Metcalff133ecc2011-05-26 12:40:09 -04003347#endif
Eric W. Biederman22d917d2011-11-17 00:11:58 -08003348#ifdef CONFIG_USER_NS
3349 REG("uid_map", S_IRUGO|S_IWUSR, proc_uid_map_operations),
3350 REG("gid_map", S_IRUGO|S_IWUSR, proc_gid_map_operations),
Eric W. Biedermanf76d2072012-08-30 01:24:05 -07003351 REG("projid_map", S_IRUGO|S_IWUSR, proc_projid_map_operations),
Eric W. Biederman9cc46512014-12-02 12:27:26 -06003352 REG("setgroups", S_IRUGO|S_IWUSR, proc_setgroups_operations),
Eric W. Biederman22d917d2011-11-17 00:11:58 -08003353#endif
Eric W. Biederman28a6d672006-10-02 02:17:05 -07003354};
3355
Al Virof0c3b502013-05-16 12:07:31 -04003356static int proc_tid_base_readdir(struct file *file, struct dir_context *ctx)
Eric W. Biederman28a6d672006-10-02 02:17:05 -07003357{
Al Virof0c3b502013-05-16 12:07:31 -04003358 return proc_pident_readdir(file, ctx,
3359 tid_base_stuff, ARRAY_SIZE(tid_base_stuff));
Eric W. Biederman28a6d672006-10-02 02:17:05 -07003360}
3361
Al Viro00cd8dd2012-06-10 17:13:09 -04003362static struct dentry *proc_tid_base_lookup(struct inode *dir, struct dentry *dentry, unsigned int flags)
3363{
Eric W. Biederman7bcd6b02006-10-02 02:18:56 -07003364 return proc_pident_lookup(dir, dentry,
3365 tid_base_stuff, ARRAY_SIZE(tid_base_stuff));
Eric W. Biederman28a6d672006-10-02 02:17:05 -07003366}
3367
Arjan van de Ven00977a52007-02-12 00:55:34 -08003368static const struct file_operations proc_tid_base_operations = {
Eric W. Biederman28a6d672006-10-02 02:17:05 -07003369 .read = generic_read_dir,
Al Virof50752e2016-04-20 17:13:54 -04003370 .iterate_shared = proc_tid_base_readdir,
3371 .llseek = generic_file_llseek,
Eric W. Biederman28a6d672006-10-02 02:17:05 -07003372};
3373
Arjan van de Venc5ef1c42007-02-12 00:55:40 -08003374static const struct inode_operations proc_tid_base_inode_operations = {
Eric W. Biederman28a6d672006-10-02 02:17:05 -07003375 .lookup = proc_tid_base_lookup,
3376 .getattr = pid_getattr,
3377 .setattr = proc_setattr,
3378};
3379
Al Viroc52a47a2013-06-15 11:15:20 +04003380static int proc_task_instantiate(struct inode *dir,
Eric Dumazetc5141e62007-05-08 00:26:15 -07003381 struct dentry *dentry, struct task_struct *task, const void *ptr)
Eric W. Biederman444ceed2006-10-02 02:18:49 -07003382{
Eric W. Biederman444ceed2006-10-02 02:18:49 -07003383 struct inode *inode;
Eric W. Biederman61a28782006-10-02 02:18:49 -07003384 inode = proc_pid_make_inode(dir->i_sb, task);
Eric W. Biederman444ceed2006-10-02 02:18:49 -07003385
3386 if (!inode)
3387 goto out;
3388 inode->i_mode = S_IFDIR|S_IRUGO|S_IXUGO;
3389 inode->i_op = &proc_tid_base_inode_operations;
3390 inode->i_fop = &proc_tid_base_operations;
3391 inode->i_flags|=S_IMMUTABLE;
Vegard Nossumaed54172008-06-05 22:46:53 -07003392
Miklos Szeredibfe86842011-10-28 14:13:29 +02003393 set_nlink(inode, 2 + pid_entry_count_dirs(tid_base_stuff,
3394 ARRAY_SIZE(tid_base_stuff)));
Eric W. Biederman444ceed2006-10-02 02:18:49 -07003395
Nick Pigginfb045ad2011-01-07 17:49:55 +11003396 d_set_d_op(dentry, &pid_dentry_operations);
Eric W. Biederman444ceed2006-10-02 02:18:49 -07003397
3398 d_add(dentry, inode);
3399 /* Close the race of the process dying before we return the dentry */
Al Viro0b728e12012-06-10 16:03:43 -04003400 if (pid_revalidate(dentry, 0))
Al Viroc52a47a2013-06-15 11:15:20 +04003401 return 0;
Eric W. Biederman444ceed2006-10-02 02:18:49 -07003402out:
Al Viroc52a47a2013-06-15 11:15:20 +04003403 return -ENOENT;
Eric W. Biederman444ceed2006-10-02 02:18:49 -07003404}
3405
Al Viro00cd8dd2012-06-10 17:13:09 -04003406static struct dentry *proc_task_lookup(struct inode *dir, struct dentry * dentry, unsigned int flags)
Eric W. Biederman28a6d672006-10-02 02:17:05 -07003407{
Al Viroc52a47a2013-06-15 11:15:20 +04003408 int result = -ENOENT;
Eric W. Biederman28a6d672006-10-02 02:17:05 -07003409 struct task_struct *task;
3410 struct task_struct *leader = get_proc_task(dir);
Eric W. Biederman28a6d672006-10-02 02:17:05 -07003411 unsigned tid;
Pavel Emelyanovb4888932007-10-18 23:40:14 -07003412 struct pid_namespace *ns;
Eric W. Biederman28a6d672006-10-02 02:17:05 -07003413
3414 if (!leader)
3415 goto out_no_task;
3416
Alexey Dobriyandbcdb502014-08-08 14:21:25 -07003417 tid = name_to_int(&dentry->d_name);
Eric W. Biederman28a6d672006-10-02 02:17:05 -07003418 if (tid == ~0U)
3419 goto out;
3420
Pavel Emelyanovb4888932007-10-18 23:40:14 -07003421 ns = dentry->d_sb->s_fs_info;
Eric W. Biederman28a6d672006-10-02 02:17:05 -07003422 rcu_read_lock();
Pavel Emelyanovb4888932007-10-18 23:40:14 -07003423 task = find_task_by_pid_ns(tid, ns);
Eric W. Biederman28a6d672006-10-02 02:17:05 -07003424 if (task)
3425 get_task_struct(task);
3426 rcu_read_unlock();
3427 if (!task)
3428 goto out;
Pavel Emelyanovbac0abd2007-10-18 23:40:18 -07003429 if (!same_thread_group(leader, task))
Eric W. Biederman28a6d672006-10-02 02:17:05 -07003430 goto out_drop_task;
3431
Eric W. Biederman444ceed2006-10-02 02:18:49 -07003432 result = proc_task_instantiate(dir, dentry, task, NULL);
Eric W. Biederman28a6d672006-10-02 02:17:05 -07003433out_drop_task:
3434 put_task_struct(task);
3435out:
3436 put_task_struct(leader);
3437out_no_task:
Al Viroc52a47a2013-06-15 11:15:20 +04003438 return ERR_PTR(result);
Eric W. Biederman28a6d672006-10-02 02:17:05 -07003439}
3440
3441/*
Eric W. Biederman0bc58a92006-06-26 00:25:50 -07003442 * Find the first tid of a thread group to return to user space.
3443 *
3444 * Usually this is just the thread group leader, but if the users
3445 * buffer was too small or there was a seek into the middle of the
3446 * directory we have more work todo.
3447 *
3448 * In the case of a short read we start with find_task_by_pid.
3449 *
3450 * In the case of a seek we start with the leader and walk nr
3451 * threads past it.
3452 */
Oleg Nesterov9f6e9632014-01-23 15:55:40 -08003453static struct task_struct *first_tid(struct pid *pid, int tid, loff_t f_pos,
3454 struct pid_namespace *ns)
Eric W. Biederman0bc58a92006-06-26 00:25:50 -07003455{
Oleg Nesterovd855a4b2014-01-23 15:55:39 -08003456 struct task_struct *pos, *task;
Oleg Nesterov9f6e9632014-01-23 15:55:40 -08003457 unsigned long nr = f_pos;
3458
3459 if (nr != f_pos) /* 32bit overflow? */
3460 return NULL;
Eric W. Biederman0bc58a92006-06-26 00:25:50 -07003461
Eric W. Biedermancc288732006-06-26 00:26:01 -07003462 rcu_read_lock();
Oleg Nesterovd855a4b2014-01-23 15:55:39 -08003463 task = pid_task(pid, PIDTYPE_PID);
3464 if (!task)
3465 goto fail;
3466
3467 /* Attempt to start with the tid of a thread */
Oleg Nesterov9f6e9632014-01-23 15:55:40 -08003468 if (tid && nr) {
Pavel Emelyanovb4888932007-10-18 23:40:14 -07003469 pos = find_task_by_pid_ns(tid, ns);
Oleg Nesterovd855a4b2014-01-23 15:55:39 -08003470 if (pos && same_thread_group(pos, task))
Oleg Nesterova872ff02006-06-26 00:26:01 -07003471 goto found;
Eric W. Biederman0bc58a92006-06-26 00:25:50 -07003472 }
3473
3474 /* If nr exceeds the number of threads there is nothing todo */
Oleg Nesterov9f6e9632014-01-23 15:55:40 -08003475 if (nr >= get_nr_threads(task))
Oleg Nesterovc986c142014-01-23 15:55:38 -08003476 goto fail;
Oleg Nesterova872ff02006-06-26 00:26:01 -07003477
3478 /* If we haven't found our starting place yet start
3479 * with the leader and walk nr threads forward.
3480 */
Oleg Nesterovd855a4b2014-01-23 15:55:39 -08003481 pos = task = task->group_leader;
Oleg Nesterovc986c142014-01-23 15:55:38 -08003482 do {
Oleg Nesterov9f6e9632014-01-23 15:55:40 -08003483 if (!nr--)
Oleg Nesterovc986c142014-01-23 15:55:38 -08003484 goto found;
Oleg Nesterovd855a4b2014-01-23 15:55:39 -08003485 } while_each_thread(task, pos);
Oleg Nesterovc986c142014-01-23 15:55:38 -08003486fail:
3487 pos = NULL;
3488 goto out;
Oleg Nesterova872ff02006-06-26 00:26:01 -07003489found:
3490 get_task_struct(pos);
3491out:
Eric W. Biedermancc288732006-06-26 00:26:01 -07003492 rcu_read_unlock();
Eric W. Biederman0bc58a92006-06-26 00:25:50 -07003493 return pos;
3494}
3495
3496/*
3497 * Find the next thread in the thread list.
3498 * Return NULL if there is an error or no next thread.
3499 *
3500 * The reference to the input task_struct is released.
3501 */
3502static struct task_struct *next_tid(struct task_struct *start)
3503{
Oleg Nesterovc1df7fb2006-06-26 00:26:02 -07003504 struct task_struct *pos = NULL;
Eric W. Biedermancc288732006-06-26 00:26:01 -07003505 rcu_read_lock();
Oleg Nesterovc1df7fb2006-06-26 00:26:02 -07003506 if (pid_alive(start)) {
Eric W. Biederman0bc58a92006-06-26 00:25:50 -07003507 pos = next_thread(start);
Oleg Nesterovc1df7fb2006-06-26 00:26:02 -07003508 if (thread_group_leader(pos))
3509 pos = NULL;
3510 else
3511 get_task_struct(pos);
3512 }
Eric W. Biedermancc288732006-06-26 00:26:01 -07003513 rcu_read_unlock();
Eric W. Biederman0bc58a92006-06-26 00:25:50 -07003514 put_task_struct(start);
3515 return pos;
3516}
3517
Linus Torvalds1da177e2005-04-16 15:20:36 -07003518/* for the /proc/TGID/task/ directories */
Al Virof0c3b502013-05-16 12:07:31 -04003519static int proc_task_readdir(struct file *file, struct dir_context *ctx)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003520{
Oleg Nesterovd855a4b2014-01-23 15:55:39 -08003521 struct inode *inode = file_inode(file);
3522 struct task_struct *task;
Pavel Emelyanovb4888932007-10-18 23:40:14 -07003523 struct pid_namespace *ns;
Al Virof0c3b502013-05-16 12:07:31 -04003524 int tid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003525
Oleg Nesterovd855a4b2014-01-23 15:55:39 -08003526 if (proc_inode_is_dead(inode))
Al Virof0c3b502013-05-16 12:07:31 -04003527 return -ENOENT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003528
Al Virof0c3b502013-05-16 12:07:31 -04003529 if (!dir_emit_dots(file, ctx))
Oleg Nesterovd855a4b2014-01-23 15:55:39 -08003530 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003531
Eric W. Biederman0bc58a92006-06-26 00:25:50 -07003532 /* f_version caches the tgid value that the last readdir call couldn't
3533 * return. lseek aka telldir automagically resets f_version to 0.
3534 */
Al Viro3aa33772014-10-31 00:42:35 -04003535 ns = inode->i_sb->s_fs_info;
Al Virof0c3b502013-05-16 12:07:31 -04003536 tid = (int)file->f_version;
3537 file->f_version = 0;
Oleg Nesterovd855a4b2014-01-23 15:55:39 -08003538 for (task = first_tid(proc_pid(inode), tid, ctx->pos - 2, ns);
Eric W. Biederman0bc58a92006-06-26 00:25:50 -07003539 task;
Al Virof0c3b502013-05-16 12:07:31 -04003540 task = next_tid(task), ctx->pos++) {
3541 char name[PROC_NUMBUF];
3542 int len;
Pavel Emelyanovb4888932007-10-18 23:40:14 -07003543 tid = task_pid_nr_ns(task, ns);
Al Virof0c3b502013-05-16 12:07:31 -04003544 len = snprintf(name, sizeof(name), "%d", tid);
3545 if (!proc_fill_cache(file, ctx, name, len,
3546 proc_task_instantiate, task, NULL)) {
Eric W. Biederman0bc58a92006-06-26 00:25:50 -07003547 /* returning this tgid failed, save it as the first
3548 * pid for the next readir call */
Al Virof0c3b502013-05-16 12:07:31 -04003549 file->f_version = (u64)tid;
Eric W. Biederman0bc58a92006-06-26 00:25:50 -07003550 put_task_struct(task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003551 break;
Eric W. Biederman0bc58a92006-06-26 00:25:50 -07003552 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003553 }
Oleg Nesterovd855a4b2014-01-23 15:55:39 -08003554
Al Virof0c3b502013-05-16 12:07:31 -04003555 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003556}
Eric W. Biederman6e66b522006-06-26 00:25:47 -07003557
3558static int proc_task_getattr(struct vfsmount *mnt, struct dentry *dentry, struct kstat *stat)
3559{
David Howells2b0143b2015-03-17 22:25:59 +00003560 struct inode *inode = d_inode(dentry);
Eric W. Biederman99f89552006-06-26 00:25:55 -07003561 struct task_struct *p = get_proc_task(inode);
Eric W. Biederman6e66b522006-06-26 00:25:47 -07003562 generic_fillattr(inode, stat);
3563
Eric W. Biederman99f89552006-06-26 00:25:55 -07003564 if (p) {
Eric W. Biederman99f89552006-06-26 00:25:55 -07003565 stat->nlink += get_nr_threads(p);
Eric W. Biederman99f89552006-06-26 00:25:55 -07003566 put_task_struct(p);
Eric W. Biederman6e66b522006-06-26 00:25:47 -07003567 }
3568
3569 return 0;
3570}
Eric W. Biederman28a6d672006-10-02 02:17:05 -07003571
Arjan van de Venc5ef1c42007-02-12 00:55:40 -08003572static const struct inode_operations proc_task_inode_operations = {
Eric W. Biederman28a6d672006-10-02 02:17:05 -07003573 .lookup = proc_task_lookup,
3574 .getattr = proc_task_getattr,
3575 .setattr = proc_setattr,
Vasiliy Kulikov04996802012-01-10 15:11:31 -08003576 .permission = proc_pid_permission,
Eric W. Biederman28a6d672006-10-02 02:17:05 -07003577};
3578
Arjan van de Ven00977a52007-02-12 00:55:34 -08003579static const struct file_operations proc_task_operations = {
Eric W. Biederman28a6d672006-10-02 02:17:05 -07003580 .read = generic_read_dir,
Al Virof50752e2016-04-20 17:13:54 -04003581 .iterate_shared = proc_task_readdir,
3582 .llseek = generic_file_llseek,
Eric W. Biederman28a6d672006-10-02 02:17:05 -07003583};