blob: 19bd9caae766598282552264bfc26dca7fbc71a4 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * linux/fs/proc/base.c
3 *
4 * Copyright (C) 1991, 1992 Linus Torvalds
5 *
6 * proc base directory handling functions
7 *
8 * 1999, Al Viro. Rewritten. Now it covers the whole per-process part.
9 * Instead of using magical inumbers to determine the kind of object
10 * we allocate and fill in-core inodes upon lookup. They don't even
11 * go into icache. We cache the reference to task_struct upon lookup too.
12 * Eventually it should become a filesystem in its own. We don't use the
13 * rest of procfs anymore.
Mauricio Line070ad42005-09-03 15:55:10 -070014 *
15 *
16 * Changelog:
17 * 17-Jan-2005
18 * Allan Bezerra
19 * Bruna Moreira <bruna.moreira@indt.org.br>
20 * Edjard Mota <edjard.mota@indt.org.br>
21 * Ilias Biris <ilias.biris@indt.org.br>
22 * Mauricio Lin <mauricio.lin@indt.org.br>
23 *
24 * Embedded Linux Lab - 10LE Instituto Nokia de Tecnologia - INdT
25 *
26 * A new process specific entry (smaps) included in /proc. It shows the
27 * size of rss for each memory area. The maps entry lacks information
28 * about physical memory size (rss) for each mapped file, i.e.,
29 * rss information for executables and library files.
30 * This additional information is useful for any tools that need to know
31 * about physical memory consumption for a process specific library.
32 *
33 * Changelog:
34 * 21-Feb-2005
35 * Embedded Linux Lab - 10LE Instituto Nokia de Tecnologia - INdT
36 * Pud inclusion in the page table walking.
37 *
38 * ChangeLog:
39 * 10-Mar-2005
40 * 10LE Instituto Nokia de Tecnologia - INdT:
41 * A better way to walks through the page table as suggested by Hugh Dickins.
42 *
43 * Simo Piiroinen <simo.piiroinen@nokia.com>:
44 * Smaps information related to shared, private, clean and dirty pages.
45 *
46 * Paul Mundt <paul.mundt@nokia.com>:
47 * Overall revision about smaps.
Linus Torvalds1da177e2005-04-16 15:20:36 -070048 */
49
50#include <asm/uaccess.h>
51
Linus Torvalds1da177e2005-04-16 15:20:36 -070052#include <linux/errno.h>
53#include <linux/time.h>
54#include <linux/proc_fs.h>
55#include <linux/stat.h>
Andrea Righi59954772008-07-27 17:29:15 +020056#include <linux/task_io_accounting_ops.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070057#include <linux/init.h>
Randy Dunlap16f7e0f2006-01-11 12:17:46 -080058#include <linux/capability.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070059#include <linux/file.h>
Al Viro9f3acc32008-04-24 07:44:08 -040060#include <linux/fdtable.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070061#include <linux/string.h>
62#include <linux/seq_file.h>
63#include <linux/namei.h>
Kirill Korotaev6b3286e2006-12-08 02:37:56 -080064#include <linux/mnt_namespace.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070065#include <linux/mm.h>
David Rientjesa63d83f2010-08-09 17:19:46 -070066#include <linux/swap.h>
Dipankar Sarmab8359962005-09-09 13:04:14 -070067#include <linux/rcupdate.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070068#include <linux/kallsyms.h>
Ken Chen2ec220e2008-11-10 11:26:08 +030069#include <linux/stacktrace.h>
Neil Hormand85f50d2007-10-18 23:40:37 -070070#include <linux/resource.h>
Kees Cook5096add2007-05-08 00:26:04 -070071#include <linux/module.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070072#include <linux/mount.h>
73#include <linux/security.h>
74#include <linux/ptrace.h>
Roland McGrath0d094ef2008-07-25 19:45:49 -070075#include <linux/tracehook.h>
Andrew Morton87ebdc02013-02-27 17:03:16 -080076#include <linux/printk.h>
Paul Menagea4243162007-10-18 23:39:35 -070077#include <linux/cgroup.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070078#include <linux/cpuset.h>
79#include <linux/audit.h>
Al Viro5addc5d2005-11-07 17:15:49 -050080#include <linux/poll.h>
Serge E. Hallyn1651e142006-10-02 02:18:08 -070081#include <linux/nsproxy.h>
Alexey Dobriyan8ac773b2006-10-19 23:28:32 -070082#include <linux/oom.h>
Kawai, Hidehiro3cb4a0b2007-07-19 01:48:28 -070083#include <linux/elf.h>
Pavel Emelyanov60347f62007-10-18 23:40:03 -070084#include <linux/pid_namespace.h>
Eric W. Biederman22d917d2011-11-17 00:11:58 -080085#include <linux/user_namespace.h>
Al Viro5ad4e532009-03-29 19:50:06 -040086#include <linux/fs_struct.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090087#include <linux/slab.h>
Pavel Emelyanov640708a2012-01-10 15:11:23 -080088#include <linux/flex_array.h>
Pavel Emelyanov48f6a7a2013-03-11 13:12:45 +040089#include <linux/posix-timers.h>
Connor O'Brien6e7b83d2018-01-31 18:11:57 -080090#include <linux/cpufreq_times.h>
Chris Metcalff133ecc2011-05-26 12:40:09 -040091#ifdef CONFIG_HARDWALL
92#include <asm/hardwall.h>
93#endif
KAMEZAWA Hiroyuki43d2b112012-01-10 15:08:09 -080094#include <trace/events/oom.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070095#include "internal.h"
Cyrill Gorcunovfaf60af2012-08-23 14:43:24 +040096#include "fd.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070097
Alexey Dobriyane0a1a012018-02-06 15:36:59 -080098#include "../../lib/kstrtox.h"
99
Eric W. Biederman0f2fe202006-06-26 00:25:46 -0700100/* NOTE:
101 * Implementing inode permission operations in /proc is almost
102 * certainly an error. Permission checks need to happen during
103 * each system call not at open time. The reason is that most of
104 * what we wish to check for permissions in /proc varies at runtime.
105 *
106 * The classic example of a problem is opening file descriptors
107 * in /proc for a task before it execs a suid executable.
108 */
109
Linus Torvalds1da177e2005-04-16 15:20:36 -0700110struct pid_entry {
Alexey Dobriyancedbcca2014-08-08 14:21:33 -0700111 const char *name;
Eric Dumazetc5141e62007-05-08 00:26:15 -0700112 int len;
Al Virod161a132011-07-24 03:36:29 -0400113 umode_t mode;
Arjan van de Venc5ef1c42007-02-12 00:55:40 -0800114 const struct inode_operations *iop;
Arjan van de Ven00977a52007-02-12 00:55:34 -0800115 const struct file_operations *fop;
Eric W. Biederman20cdc892006-10-02 02:17:07 -0700116 union proc_op op;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700117};
118
Eric W. Biederman61a28782006-10-02 02:18:49 -0700119#define NOD(NAME, MODE, IOP, FOP, OP) { \
Eric W. Biederman20cdc892006-10-02 02:17:07 -0700120 .name = (NAME), \
Eric Dumazetc5141e62007-05-08 00:26:15 -0700121 .len = sizeof(NAME) - 1, \
Eric W. Biederman20cdc892006-10-02 02:17:07 -0700122 .mode = MODE, \
123 .iop = IOP, \
124 .fop = FOP, \
125 .op = OP, \
126}
127
Alexey Dobriyan631f9c12008-11-10 01:32:52 +0300128#define DIR(NAME, MODE, iops, fops) \
129 NOD(NAME, (S_IFDIR|(MODE)), &iops, &fops, {} )
130#define LNK(NAME, get_link) \
Eric W. Biederman61a28782006-10-02 02:18:49 -0700131 NOD(NAME, (S_IFLNK|S_IRWXUGO), \
Eric W. Biederman20cdc892006-10-02 02:17:07 -0700132 &proc_pid_link_inode_operations, NULL, \
Alexey Dobriyan631f9c12008-11-10 01:32:52 +0300133 { .proc_get_link = get_link } )
134#define REG(NAME, MODE, fops) \
135 NOD(NAME, (S_IFREG|(MODE)), NULL, &fops, {})
Alexey Dobriyan631f9c12008-11-10 01:32:52 +0300136#define ONE(NAME, MODE, show) \
Eric W. Biedermanbe614082008-02-08 04:18:30 -0800137 NOD(NAME, (S_IFREG|(MODE)), \
138 NULL, &proc_single_file_operations, \
Alexey Dobriyan631f9c12008-11-10 01:32:52 +0300139 { .proc_show = show } )
Linus Torvalds1da177e2005-04-16 15:20:36 -0700140
Vegard Nossumaed54172008-06-05 22:46:53 -0700141/*
142 * Count the number of hardlinks for the pid_entry table, excluding the .
143 * and .. links.
144 */
145static unsigned int pid_entry_count_dirs(const struct pid_entry *entries,
146 unsigned int n)
147{
148 unsigned int i;
149 unsigned int count;
150
151 count = 0;
152 for (i = 0; i < n; ++i) {
153 if (S_ISDIR(entries[i].mode))
154 ++count;
155 }
156
157 return count;
158}
159
Miklos Szeredif7ad3c62010-08-10 11:41:36 +0200160static int get_task_root(struct task_struct *task, struct path *root)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700161{
Hugh Dickins7c2c7d92009-03-28 23:21:27 +0000162 int result = -ENOENT;
163
Miklos Szeredi0494f6e2005-09-06 15:18:22 -0700164 task_lock(task);
Miklos Szeredif7ad3c62010-08-10 11:41:36 +0200165 if (task->fs) {
166 get_fs_root(task->fs, root);
Hugh Dickins7c2c7d92009-03-28 23:21:27 +0000167 result = 0;
168 }
Miklos Szeredi0494f6e2005-09-06 15:18:22 -0700169 task_unlock(task);
Hugh Dickins7c2c7d92009-03-28 23:21:27 +0000170 return result;
Miklos Szeredi0494f6e2005-09-06 15:18:22 -0700171}
172
Cyrill Gorcunov7773fbc2012-01-10 15:11:20 -0800173static int proc_cwd_link(struct dentry *dentry, struct path *path)
Miklos Szeredi0494f6e2005-09-06 15:18:22 -0700174{
David Howells2b0143b2015-03-17 22:25:59 +0000175 struct task_struct *task = get_proc_task(d_inode(dentry));
Miklos Szeredi0494f6e2005-09-06 15:18:22 -0700176 int result = -ENOENT;
Eric W. Biederman99f89552006-06-26 00:25:55 -0700177
178 if (task) {
Miklos Szeredif7ad3c62010-08-10 11:41:36 +0200179 task_lock(task);
180 if (task->fs) {
181 get_fs_pwd(task->fs, path);
182 result = 0;
183 }
184 task_unlock(task);
Eric W. Biederman99f89552006-06-26 00:25:55 -0700185 put_task_struct(task);
186 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700187 return result;
188}
189
Cyrill Gorcunov7773fbc2012-01-10 15:11:20 -0800190static int proc_root_link(struct dentry *dentry, struct path *path)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700191{
David Howells2b0143b2015-03-17 22:25:59 +0000192 struct task_struct *task = get_proc_task(d_inode(dentry));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700193 int result = -ENOENT;
Eric W. Biederman99f89552006-06-26 00:25:55 -0700194
195 if (task) {
Miklos Szeredif7ad3c62010-08-10 11:41:36 +0200196 result = get_task_root(task, path);
Eric W. Biederman99f89552006-06-26 00:25:55 -0700197 put_task_struct(task);
198 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700199 return result;
200}
201
Alexey Dobriyanc2c0bb42015-06-25 15:00:54 -0700202static ssize_t proc_pid_cmdline_read(struct file *file, char __user *buf,
203 size_t _count, loff_t *pos)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700204{
Alexey Dobriyanc2c0bb42015-06-25 15:00:54 -0700205 struct task_struct *tsk;
206 struct mm_struct *mm;
207 char *page;
208 unsigned long count = _count;
209 unsigned long arg_start, arg_end, env_start, env_end;
210 unsigned long len1, len2, len;
211 unsigned long p;
212 char c;
213 ssize_t rv;
214
215 BUG_ON(*pos < 0);
216
217 tsk = get_proc_task(file_inode(file));
218 if (!tsk)
219 return -ESRCH;
220 mm = get_task_mm(tsk);
221 put_task_struct(tsk);
222 if (!mm)
223 return 0;
224 /* Check if process spawned far enough to have cmdline. */
225 if (!mm->env_end) {
226 rv = 0;
227 goto out_mmput;
228 }
229
230 page = (char *)__get_free_page(GFP_TEMPORARY);
231 if (!page) {
232 rv = -ENOMEM;
233 goto out_mmput;
234 }
235
236 down_read(&mm->mmap_sem);
237 arg_start = mm->arg_start;
238 arg_end = mm->arg_end;
239 env_start = mm->env_start;
240 env_end = mm->env_end;
241 up_read(&mm->mmap_sem);
242
243 BUG_ON(arg_start > arg_end);
244 BUG_ON(env_start > env_end);
245
246 len1 = arg_end - arg_start;
247 len2 = env_end - env_start;
248
Alexey Dobriyan3581d452015-07-17 16:24:09 -0700249 /* Empty ARGV. */
250 if (len1 == 0) {
251 rv = 0;
252 goto out_free_page;
253 }
Alexey Dobriyan2ca66ff2014-08-08 14:21:41 -0700254 /*
Alexey Dobriyanc2c0bb42015-06-25 15:00:54 -0700255 * Inherently racy -- command line shares address space
256 * with code and data.
Alexey Dobriyan2ca66ff2014-08-08 14:21:41 -0700257 */
Willy Tarreau6f1abf82018-05-11 08:11:44 +0200258 rv = access_remote_vm(mm, arg_end - 1, &c, 1, FOLL_ANON);
Alexey Dobriyanc2c0bb42015-06-25 15:00:54 -0700259 if (rv <= 0)
260 goto out_free_page;
261
262 rv = 0;
263
264 if (c == '\0') {
265 /* Command line (set of strings) occupies whole ARGV. */
266 if (len1 <= *pos)
267 goto out_free_page;
268
269 p = arg_start + *pos;
270 len = len1 - *pos;
271 while (count > 0 && len > 0) {
272 unsigned int _count;
273 int nr_read;
274
275 _count = min3(count, len, PAGE_SIZE);
Willy Tarreau6f1abf82018-05-11 08:11:44 +0200276 nr_read = access_remote_vm(mm, p, page, _count, FOLL_ANON);
Alexey Dobriyanc2c0bb42015-06-25 15:00:54 -0700277 if (nr_read < 0)
278 rv = nr_read;
279 if (nr_read <= 0)
280 goto out_free_page;
281
282 if (copy_to_user(buf, page, nr_read)) {
283 rv = -EFAULT;
284 goto out_free_page;
285 }
286
287 p += nr_read;
288 len -= nr_read;
289 buf += nr_read;
290 count -= nr_read;
291 rv += nr_read;
292 }
293 } else {
294 /*
295 * Command line (1 string) occupies ARGV and maybe
296 * extends into ENVP.
297 */
298 if (len1 + len2 <= *pos)
299 goto skip_argv_envp;
300 if (len1 <= *pos)
301 goto skip_argv;
302
303 p = arg_start + *pos;
304 len = len1 - *pos;
305 while (count > 0 && len > 0) {
306 unsigned int _count, l;
307 int nr_read;
308 bool final;
309
310 _count = min3(count, len, PAGE_SIZE);
Willy Tarreau6f1abf82018-05-11 08:11:44 +0200311 nr_read = access_remote_vm(mm, p, page, _count, FOLL_ANON);
Alexey Dobriyanc2c0bb42015-06-25 15:00:54 -0700312 if (nr_read < 0)
313 rv = nr_read;
314 if (nr_read <= 0)
315 goto out_free_page;
316
317 /*
318 * Command line can be shorter than whole ARGV
319 * even if last "marker" byte says it is not.
320 */
321 final = false;
322 l = strnlen(page, nr_read);
323 if (l < nr_read) {
324 nr_read = l;
325 final = true;
326 }
327
328 if (copy_to_user(buf, page, nr_read)) {
329 rv = -EFAULT;
330 goto out_free_page;
331 }
332
333 p += nr_read;
334 len -= nr_read;
335 buf += nr_read;
336 count -= nr_read;
337 rv += nr_read;
338
339 if (final)
340 goto out_free_page;
341 }
342skip_argv:
343 /*
344 * Command line (1 string) occupies ARGV and
345 * extends into ENVP.
346 */
347 if (len1 <= *pos) {
348 p = env_start + *pos - len1;
349 len = len1 + len2 - *pos;
350 } else {
351 p = env_start;
352 len = len2;
353 }
354 while (count > 0 && len > 0) {
355 unsigned int _count, l;
356 int nr_read;
357 bool final;
358
359 _count = min3(count, len, PAGE_SIZE);
Willy Tarreau6f1abf82018-05-11 08:11:44 +0200360 nr_read = access_remote_vm(mm, p, page, _count, FOLL_ANON);
Alexey Dobriyanc2c0bb42015-06-25 15:00:54 -0700361 if (nr_read < 0)
362 rv = nr_read;
363 if (nr_read <= 0)
364 goto out_free_page;
365
366 /* Find EOS. */
367 final = false;
368 l = strnlen(page, nr_read);
369 if (l < nr_read) {
370 nr_read = l;
371 final = true;
372 }
373
374 if (copy_to_user(buf, page, nr_read)) {
375 rv = -EFAULT;
376 goto out_free_page;
377 }
378
379 p += nr_read;
380 len -= nr_read;
381 buf += nr_read;
382 count -= nr_read;
383 rv += nr_read;
384
385 if (final)
386 goto out_free_page;
387 }
388skip_argv_envp:
389 ;
390 }
391
392out_free_page:
393 free_page((unsigned long)page);
394out_mmput:
395 mmput(mm);
396 if (rv > 0)
397 *pos += rv;
398 return rv;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700399}
400
Alexey Dobriyanc2c0bb42015-06-25 15:00:54 -0700401static const struct file_operations proc_pid_cmdline_ops = {
402 .read = proc_pid_cmdline_read,
403 .llseek = generic_file_llseek,
404};
405
Linus Torvalds1da177e2005-04-16 15:20:36 -0700406#ifdef CONFIG_KALLSYMS
407/*
408 * Provides a wchan file via kallsyms in a proper one-value-per-file format.
409 * Returns the resolved symbol. If that fails, simply return the address.
410 */
Alexey Dobriyanedfcd602014-08-08 14:21:44 -0700411static int proc_pid_wchan(struct seq_file *m, struct pid_namespace *ns,
412 struct pid *pid, struct task_struct *task)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700413{
Alexey Dobriyanffb45122007-05-08 00:28:41 -0700414 unsigned long wchan;
Tejun Heo9281ace2007-07-17 04:03:51 -0700415 char symname[KSYM_NAME_LEN];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700416
417 wchan = get_wchan(task);
418
Jann Horncaaee622016-01-20 15:00:04 -0800419 if (wchan && ptrace_may_access(task, PTRACE_MODE_READ_FSCREDS)
420 && !lookup_symbol_name(wchan, symname))
Joe Perches25ce3192015-04-15 16:18:17 -0700421 seq_printf(m, "%s", symname);
Ingo Molnarb2f73922015-09-30 15:59:17 +0200422 else
Robin Humble1e92a612016-05-09 16:28:46 -0700423 seq_putc(m, '0');
Joe Perches25ce3192015-04-15 16:18:17 -0700424
425 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700426}
427#endif /* CONFIG_KALLSYMS */
428
Al Viroa9712bc2011-03-23 15:52:50 -0400429static int lock_trace(struct task_struct *task)
430{
431 int err = mutex_lock_killable(&task->signal->cred_guard_mutex);
432 if (err)
433 return err;
Jann Horncaaee622016-01-20 15:00:04 -0800434 if (!ptrace_may_access(task, PTRACE_MODE_ATTACH_FSCREDS)) {
Al Viroa9712bc2011-03-23 15:52:50 -0400435 mutex_unlock(&task->signal->cred_guard_mutex);
436 return -EPERM;
437 }
438 return 0;
439}
440
441static void unlock_trace(struct task_struct *task)
442{
443 mutex_unlock(&task->signal->cred_guard_mutex);
444}
445
Ken Chen2ec220e2008-11-10 11:26:08 +0300446#ifdef CONFIG_STACKTRACE
447
448#define MAX_STACK_TRACE_DEPTH 64
449
450static int proc_pid_stack(struct seq_file *m, struct pid_namespace *ns,
451 struct pid *pid, struct task_struct *task)
452{
453 struct stack_trace trace;
454 unsigned long *entries;
Al Viroa9712bc2011-03-23 15:52:50 -0400455 int err;
Ken Chen2ec220e2008-11-10 11:26:08 +0300456 int i;
457
Jann Horn3c5dc3f2018-10-05 15:51:58 -0700458 /*
459 * The ability to racily run the kernel stack unwinder on a running task
460 * and then observe the unwinder output is scary; while it is useful for
461 * debugging kernel issues, it can also allow an attacker to leak kernel
462 * stack contents.
463 * Doing this in a manner that is at least safe from races would require
464 * some work to ensure that the remote task can not be scheduled; and
465 * even then, this would still expose the unwinder as local attack
466 * surface.
467 * Therefore, this interface is restricted to root.
468 */
469 if (!file_ns_capable(m->file, &init_user_ns, CAP_SYS_ADMIN))
470 return -EACCES;
471
Ken Chen2ec220e2008-11-10 11:26:08 +0300472 entries = kmalloc(MAX_STACK_TRACE_DEPTH * sizeof(*entries), GFP_KERNEL);
473 if (!entries)
474 return -ENOMEM;
475
476 trace.nr_entries = 0;
477 trace.max_entries = MAX_STACK_TRACE_DEPTH;
478 trace.entries = entries;
479 trace.skip = 0;
Ken Chen2ec220e2008-11-10 11:26:08 +0300480
Al Viroa9712bc2011-03-23 15:52:50 -0400481 err = lock_trace(task);
482 if (!err) {
483 save_stack_trace_tsk(task, &trace);
484
485 for (i = 0; i < trace.nr_entries; i++) {
Josh Poimboeuf8b927d72016-08-18 10:59:07 -0500486 seq_printf(m, "[<%pK>] %pB\n",
Al Viroa9712bc2011-03-23 15:52:50 -0400487 (void *)entries[i], (void *)entries[i]);
488 }
489 unlock_trace(task);
Ken Chen2ec220e2008-11-10 11:26:08 +0300490 }
491 kfree(entries);
492
Al Viroa9712bc2011-03-23 15:52:50 -0400493 return err;
Ken Chen2ec220e2008-11-10 11:26:08 +0300494}
495#endif
496
Naveen N. Rao5968cec2015-06-30 14:36:03 +0530497#ifdef CONFIG_SCHED_INFO
Linus Torvalds1da177e2005-04-16 15:20:36 -0700498/*
499 * Provides /proc/PID/schedstat
500 */
Alexey Dobriyanf6e826c2014-08-08 14:21:46 -0700501static int proc_pid_schedstat(struct seq_file *m, struct pid_namespace *ns,
502 struct pid *pid, struct task_struct *task)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700503{
Naveen N. Rao5968cec2015-06-30 14:36:03 +0530504 if (unlikely(!sched_info_on()))
505 seq_printf(m, "0 0 0\n");
506 else
507 seq_printf(m, "%llu %llu %lu\n",
Joe Perches25ce3192015-04-15 16:18:17 -0700508 (unsigned long long)task->se.sum_exec_runtime,
509 (unsigned long long)task->sched_info.run_delay,
510 task->sched_info.pcount);
511
512 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700513}
514#endif
515
Arjan van de Ven97455122008-01-25 21:08:34 +0100516#ifdef CONFIG_LATENCYTOP
517static int lstats_show_proc(struct seq_file *m, void *v)
518{
519 int i;
Hiroshi Shimamoto13d77c32008-02-20 16:53:29 -0800520 struct inode *inode = m->private;
521 struct task_struct *task = get_proc_task(inode);
Arjan van de Ven97455122008-01-25 21:08:34 +0100522
Hiroshi Shimamoto13d77c32008-02-20 16:53:29 -0800523 if (!task)
524 return -ESRCH;
525 seq_puts(m, "Latency Top version : v0.1\n");
Arjan van de Ven97455122008-01-25 21:08:34 +0100526 for (i = 0; i < 32; i++) {
Joe Perches34e49d42011-01-12 17:00:30 -0800527 struct latency_record *lr = &task->latency_record[i];
528 if (lr->backtrace[0]) {
Arjan van de Ven97455122008-01-25 21:08:34 +0100529 int q;
Joe Perches34e49d42011-01-12 17:00:30 -0800530 seq_printf(m, "%i %li %li",
531 lr->count, lr->time, lr->max);
Arjan van de Ven97455122008-01-25 21:08:34 +0100532 for (q = 0; q < LT_BACKTRACEDEPTH; q++) {
Joe Perches34e49d42011-01-12 17:00:30 -0800533 unsigned long bt = lr->backtrace[q];
534 if (!bt)
Arjan van de Ven97455122008-01-25 21:08:34 +0100535 break;
Joe Perches34e49d42011-01-12 17:00:30 -0800536 if (bt == ULONG_MAX)
Arjan van de Ven97455122008-01-25 21:08:34 +0100537 break;
Joe Perches34e49d42011-01-12 17:00:30 -0800538 seq_printf(m, " %ps", (void *)bt);
Arjan van de Ven97455122008-01-25 21:08:34 +0100539 }
Alexey Dobriyan9d6de122011-01-12 17:00:32 -0800540 seq_putc(m, '\n');
Arjan van de Ven97455122008-01-25 21:08:34 +0100541 }
542
543 }
Hiroshi Shimamoto13d77c32008-02-20 16:53:29 -0800544 put_task_struct(task);
Arjan van de Ven97455122008-01-25 21:08:34 +0100545 return 0;
546}
547
548static int lstats_open(struct inode *inode, struct file *file)
549{
Hiroshi Shimamoto13d77c32008-02-20 16:53:29 -0800550 return single_open(file, lstats_show_proc, inode);
Hiroshi Shimamotod6643d12008-02-14 10:27:00 -0800551}
552
Arjan van de Ven97455122008-01-25 21:08:34 +0100553static ssize_t lstats_write(struct file *file, const char __user *buf,
554 size_t count, loff_t *offs)
555{
Al Viro496ad9a2013-01-23 17:07:38 -0500556 struct task_struct *task = get_proc_task(file_inode(file));
Arjan van de Ven97455122008-01-25 21:08:34 +0100557
Hiroshi Shimamoto13d77c32008-02-20 16:53:29 -0800558 if (!task)
559 return -ESRCH;
Arjan van de Ven97455122008-01-25 21:08:34 +0100560 clear_all_latency_tracing(task);
Hiroshi Shimamoto13d77c32008-02-20 16:53:29 -0800561 put_task_struct(task);
Arjan van de Ven97455122008-01-25 21:08:34 +0100562
563 return count;
564}
565
566static const struct file_operations proc_lstats_operations = {
567 .open = lstats_open,
568 .read = seq_read,
569 .write = lstats_write,
570 .llseek = seq_lseek,
Hiroshi Shimamoto13d77c32008-02-20 16:53:29 -0800571 .release = single_release,
Arjan van de Ven97455122008-01-25 21:08:34 +0100572};
573
574#endif
575
Alexey Dobriyan6ba51e32014-08-08 14:21:48 -0700576static int proc_oom_score(struct seq_file *m, struct pid_namespace *ns,
577 struct pid *pid, struct task_struct *task)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700578{
David Rientjesa7f638f2012-05-29 15:06:47 -0700579 unsigned long totalpages = totalram_pages + total_swap_pages;
Oleg Nesterovb95c35e2010-04-01 15:13:57 +0200580 unsigned long points = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700581
Oleg Nesterovef419392016-08-02 14:03:19 -0700582 points = oom_badness(task, NULL, NULL, totalpages) *
583 1000 / totalpages;
Joe Perches25ce3192015-04-15 16:18:17 -0700584 seq_printf(m, "%lu\n", points);
585
586 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700587}
588
Neil Hormand85f50d2007-10-18 23:40:37 -0700589struct limit_names {
Alexey Dobriyancedbcca2014-08-08 14:21:33 -0700590 const char *name;
591 const char *unit;
Neil Hormand85f50d2007-10-18 23:40:37 -0700592};
593
594static const struct limit_names lnames[RLIM_NLIMITS] = {
Kees Cookcff4edb2009-09-22 16:45:32 -0700595 [RLIMIT_CPU] = {"Max cpu time", "seconds"},
Neil Hormand85f50d2007-10-18 23:40:37 -0700596 [RLIMIT_FSIZE] = {"Max file size", "bytes"},
597 [RLIMIT_DATA] = {"Max data size", "bytes"},
598 [RLIMIT_STACK] = {"Max stack size", "bytes"},
599 [RLIMIT_CORE] = {"Max core file size", "bytes"},
600 [RLIMIT_RSS] = {"Max resident set", "bytes"},
601 [RLIMIT_NPROC] = {"Max processes", "processes"},
602 [RLIMIT_NOFILE] = {"Max open files", "files"},
603 [RLIMIT_MEMLOCK] = {"Max locked memory", "bytes"},
604 [RLIMIT_AS] = {"Max address space", "bytes"},
605 [RLIMIT_LOCKS] = {"Max file locks", "locks"},
606 [RLIMIT_SIGPENDING] = {"Max pending signals", "signals"},
607 [RLIMIT_MSGQUEUE] = {"Max msgqueue size", "bytes"},
608 [RLIMIT_NICE] = {"Max nice priority", NULL},
609 [RLIMIT_RTPRIO] = {"Max realtime priority", NULL},
Eugene Teo88081172008-02-23 15:23:52 -0800610 [RLIMIT_RTTIME] = {"Max realtime timeout", "us"},
Neil Hormand85f50d2007-10-18 23:40:37 -0700611};
612
613/* Display limits for a process */
Alexey Dobriyan1c963eb2014-08-08 14:21:37 -0700614static int proc_pid_limits(struct seq_file *m, struct pid_namespace *ns,
615 struct pid *pid, struct task_struct *task)
Neil Hormand85f50d2007-10-18 23:40:37 -0700616{
617 unsigned int i;
Neil Hormand85f50d2007-10-18 23:40:37 -0700618 unsigned long flags;
Neil Hormand85f50d2007-10-18 23:40:37 -0700619
620 struct rlimit rlim[RLIM_NLIMITS];
621
Lai Jiangshana6bebbc2008-10-05 00:51:15 +0400622 if (!lock_task_sighand(task, &flags))
Neil Hormand85f50d2007-10-18 23:40:37 -0700623 return 0;
Neil Hormand85f50d2007-10-18 23:40:37 -0700624 memcpy(rlim, task->signal->rlim, sizeof(struct rlimit) * RLIM_NLIMITS);
625 unlock_task_sighand(task, &flags);
Neil Hormand85f50d2007-10-18 23:40:37 -0700626
627 /*
628 * print the file header
629 */
Alexey Dobriyan1c963eb2014-08-08 14:21:37 -0700630 seq_printf(m, "%-25s %-20s %-20s %-10s\n",
Joe Perches25ce3192015-04-15 16:18:17 -0700631 "Limit", "Soft Limit", "Hard Limit", "Units");
Neil Hormand85f50d2007-10-18 23:40:37 -0700632
633 for (i = 0; i < RLIM_NLIMITS; i++) {
634 if (rlim[i].rlim_cur == RLIM_INFINITY)
Alexey Dobriyan1c963eb2014-08-08 14:21:37 -0700635 seq_printf(m, "%-25s %-20s ",
Joe Perches25ce3192015-04-15 16:18:17 -0700636 lnames[i].name, "unlimited");
Neil Hormand85f50d2007-10-18 23:40:37 -0700637 else
Alexey Dobriyan1c963eb2014-08-08 14:21:37 -0700638 seq_printf(m, "%-25s %-20lu ",
Joe Perches25ce3192015-04-15 16:18:17 -0700639 lnames[i].name, rlim[i].rlim_cur);
Neil Hormand85f50d2007-10-18 23:40:37 -0700640
641 if (rlim[i].rlim_max == RLIM_INFINITY)
Alexey Dobriyan1c963eb2014-08-08 14:21:37 -0700642 seq_printf(m, "%-20s ", "unlimited");
Neil Hormand85f50d2007-10-18 23:40:37 -0700643 else
Alexey Dobriyan1c963eb2014-08-08 14:21:37 -0700644 seq_printf(m, "%-20lu ", rlim[i].rlim_max);
Neil Hormand85f50d2007-10-18 23:40:37 -0700645
646 if (lnames[i].unit)
Alexey Dobriyan1c963eb2014-08-08 14:21:37 -0700647 seq_printf(m, "%-10s\n", lnames[i].unit);
Neil Hormand85f50d2007-10-18 23:40:37 -0700648 else
Alexey Dobriyan1c963eb2014-08-08 14:21:37 -0700649 seq_putc(m, '\n');
Neil Hormand85f50d2007-10-18 23:40:37 -0700650 }
651
Alexey Dobriyan1c963eb2014-08-08 14:21:37 -0700652 return 0;
Neil Hormand85f50d2007-10-18 23:40:37 -0700653}
654
Roland McGrathebcb6732008-07-25 19:46:00 -0700655#ifdef CONFIG_HAVE_ARCH_TRACEHOOK
Alexey Dobriyan09d93bd2014-08-08 14:21:39 -0700656static int proc_pid_syscall(struct seq_file *m, struct pid_namespace *ns,
657 struct pid *pid, struct task_struct *task)
Roland McGrathebcb6732008-07-25 19:46:00 -0700658{
659 long nr;
660 unsigned long args[6], sp, pc;
Joe Perches25ce3192015-04-15 16:18:17 -0700661 int res;
662
663 res = lock_trace(task);
Al Viroa9712bc2011-03-23 15:52:50 -0400664 if (res)
665 return res;
Roland McGrathebcb6732008-07-25 19:46:00 -0700666
667 if (task_current_syscall(task, &nr, args, 6, &sp, &pc))
Alexey Dobriyan09d93bd2014-08-08 14:21:39 -0700668 seq_puts(m, "running\n");
Al Viroa9712bc2011-03-23 15:52:50 -0400669 else if (nr < 0)
Alexey Dobriyan09d93bd2014-08-08 14:21:39 -0700670 seq_printf(m, "%ld 0x%lx 0x%lx\n", nr, sp, pc);
Al Viroa9712bc2011-03-23 15:52:50 -0400671 else
Alexey Dobriyan09d93bd2014-08-08 14:21:39 -0700672 seq_printf(m,
Roland McGrathebcb6732008-07-25 19:46:00 -0700673 "%ld 0x%lx 0x%lx 0x%lx 0x%lx 0x%lx 0x%lx 0x%lx 0x%lx\n",
674 nr,
675 args[0], args[1], args[2], args[3], args[4], args[5],
676 sp, pc);
Al Viroa9712bc2011-03-23 15:52:50 -0400677 unlock_trace(task);
Joe Perches25ce3192015-04-15 16:18:17 -0700678
679 return 0;
Roland McGrathebcb6732008-07-25 19:46:00 -0700680}
681#endif /* CONFIG_HAVE_ARCH_TRACEHOOK */
682
Linus Torvalds1da177e2005-04-16 15:20:36 -0700683/************************************************************************/
684/* Here the fs part begins */
685/************************************************************************/
686
687/* permission checks */
Eric W. Biederman778c1142006-06-26 00:25:58 -0700688static int proc_fd_access_allowed(struct inode *inode)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700689{
Eric W. Biederman778c1142006-06-26 00:25:58 -0700690 struct task_struct *task;
691 int allowed = 0;
Eric W. Biedermandf26c402006-06-26 00:25:59 -0700692 /* Allow access to a task's file descriptors if it is us or we
693 * may use ptrace attach to the process and find out that
694 * information.
Eric W. Biederman778c1142006-06-26 00:25:58 -0700695 */
696 task = get_proc_task(inode);
Eric W. Biedermandf26c402006-06-26 00:25:59 -0700697 if (task) {
Jann Horncaaee622016-01-20 15:00:04 -0800698 allowed = ptrace_may_access(task, PTRACE_MODE_READ_FSCREDS);
Eric W. Biederman778c1142006-06-26 00:25:58 -0700699 put_task_struct(task);
Eric W. Biedermandf26c402006-06-26 00:25:59 -0700700 }
Eric W. Biederman778c1142006-06-26 00:25:58 -0700701 return allowed;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700702}
703
Eric W. Biederman6b4e3062010-03-07 16:41:34 -0800704int proc_setattr(struct dentry *dentry, struct iattr *attr)
Linus Torvalds6d76fa52006-07-15 12:26:45 -0700705{
706 int error;
David Howells2b0143b2015-03-17 22:25:59 +0000707 struct inode *inode = d_inode(dentry);
Linus Torvalds6d76fa52006-07-15 12:26:45 -0700708
709 if (attr->ia_valid & ATTR_MODE)
710 return -EPERM;
711
Jan Kara31051c82016-05-26 16:55:18 +0200712 error = setattr_prepare(dentry, attr);
Christoph Hellwig10257742010-06-04 11:30:02 +0200713 if (error)
714 return error;
715
Christoph Hellwig10257742010-06-04 11:30:02 +0200716 setattr_copy(inode, attr);
717 mark_inode_dirty(inode);
718 return 0;
Linus Torvalds6d76fa52006-07-15 12:26:45 -0700719}
720
Vasiliy Kulikov04996802012-01-10 15:11:31 -0800721/*
722 * May current process learn task's sched/cmdline info (for hide_pid_min=1)
723 * or euid/egid (for hide_pid_min=2)?
724 */
725static bool has_pid_permissions(struct pid_namespace *pid,
726 struct task_struct *task,
727 int hide_pid_min)
728{
729 if (pid->hide_pid < hide_pid_min)
730 return true;
731 if (in_group_p(pid->pid_gid))
732 return true;
Jann Horncaaee622016-01-20 15:00:04 -0800733 return ptrace_may_access(task, PTRACE_MODE_READ_FSCREDS);
Vasiliy Kulikov04996802012-01-10 15:11:31 -0800734}
735
736
737static int proc_pid_permission(struct inode *inode, int mask)
738{
739 struct pid_namespace *pid = inode->i_sb->s_fs_info;
740 struct task_struct *task;
741 bool has_perms;
742
743 task = get_proc_task(inode);
Xiaotian Fenga2ef9902012-01-12 17:17:08 -0800744 if (!task)
745 return -ESRCH;
Vasiliy Kulikov04996802012-01-10 15:11:31 -0800746 has_perms = has_pid_permissions(pid, task, 1);
747 put_task_struct(task);
748
749 if (!has_perms) {
750 if (pid->hide_pid == 2) {
751 /*
752 * Let's make getdents(), stat(), and open()
753 * consistent with each other. If a process
754 * may not stat() a file, it shouldn't be seen
755 * in procfs at all.
756 */
757 return -ENOENT;
758 }
759
760 return -EPERM;
761 }
762 return generic_permission(inode, mask);
763}
764
765
766
Arjan van de Venc5ef1c42007-02-12 00:55:40 -0800767static const struct inode_operations proc_def_inode_operations = {
Linus Torvalds6d76fa52006-07-15 12:26:45 -0700768 .setattr = proc_setattr,
769};
770
Eric W. Biedermanbe614082008-02-08 04:18:30 -0800771static int proc_single_show(struct seq_file *m, void *v)
772{
773 struct inode *inode = m->private;
774 struct pid_namespace *ns;
775 struct pid *pid;
776 struct task_struct *task;
777 int ret;
778
779 ns = inode->i_sb->s_fs_info;
780 pid = proc_pid(inode);
781 task = get_pid_task(pid, PIDTYPE_PID);
782 if (!task)
783 return -ESRCH;
784
785 ret = PROC_I(inode)->op.proc_show(m, ns, pid, task);
786
787 put_task_struct(task);
788 return ret;
789}
790
791static int proc_single_open(struct inode *inode, struct file *filp)
792{
Jovi Zhangc6a34052011-01-12 17:00:34 -0800793 return single_open(filp, proc_single_show, inode);
Eric W. Biedermanbe614082008-02-08 04:18:30 -0800794}
795
796static const struct file_operations proc_single_file_operations = {
797 .open = proc_single_open,
798 .read = seq_read,
799 .llseek = seq_lseek,
800 .release = single_release,
801};
802
Oleg Nesterov5381e162014-10-09 15:25:24 -0700803
804struct mm_struct *proc_mem_open(struct inode *inode, unsigned int mode)
805{
806 struct task_struct *task = get_proc_task(inode);
807 struct mm_struct *mm = ERR_PTR(-ESRCH);
808
809 if (task) {
Jann Horncaaee622016-01-20 15:00:04 -0800810 mm = mm_access(task, mode | PTRACE_MODE_FSCREDS);
Oleg Nesterov5381e162014-10-09 15:25:24 -0700811 put_task_struct(task);
812
813 if (!IS_ERR_OR_NULL(mm)) {
814 /* ensure this mm_struct can't be freed */
815 atomic_inc(&mm->mm_count);
816 /* but do not pin its memory */
817 mmput(mm);
818 }
819 }
820
821 return mm;
822}
823
Cong Wangb409e572012-05-31 16:26:17 -0700824static int __mem_open(struct inode *inode, struct file *file, unsigned int mode)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700825{
Oleg Nesterov5381e162014-10-09 15:25:24 -0700826 struct mm_struct *mm = proc_mem_open(inode, mode);
Linus Torvaldse2683372012-01-17 15:21:19 -0800827
828 if (IS_ERR(mm))
829 return PTR_ERR(mm);
830
Linus Torvaldse2683372012-01-17 15:21:19 -0800831 file->private_data = mm;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700832 return 0;
833}
834
Cong Wangb409e572012-05-31 16:26:17 -0700835static int mem_open(struct inode *inode, struct file *file)
836{
Djalal Harounibc452b42012-07-30 14:42:28 -0700837 int ret = __mem_open(inode, file, PTRACE_MODE_ATTACH);
838
839 /* OK to pass negative loff_t, we can catch out-of-range */
840 file->f_mode |= FMODE_UNSIGNED_OFFSET;
841
842 return ret;
Cong Wangb409e572012-05-31 16:26:17 -0700843}
844
Oleg Nesterov572d34b2012-01-31 17:14:54 +0100845static ssize_t mem_rw(struct file *file, char __user *buf,
846 size_t count, loff_t *ppos, int write)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700847{
Linus Torvaldse2683372012-01-17 15:21:19 -0800848 struct mm_struct *mm = file->private_data;
Oleg Nesterov572d34b2012-01-31 17:14:54 +0100849 unsigned long addr = *ppos;
850 ssize_t copied;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700851 char *page;
Linus Torvalds272ddc82016-10-24 19:00:44 -0700852 unsigned int flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700853
Linus Torvaldse2683372012-01-17 15:21:19 -0800854 if (!mm)
855 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700856
Mel Gormane12ba742007-10-16 01:25:52 -0700857 page = (char *)__get_free_page(GFP_TEMPORARY);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700858 if (!page)
Linus Torvaldse2683372012-01-17 15:21:19 -0800859 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700860
Frederik Deweerdtf7ca54f2006-09-29 02:01:02 -0700861 copied = 0;
Oleg Nesterov6d08f2c2012-01-31 17:15:11 +0100862 if (!atomic_inc_not_zero(&mm->mm_users))
863 goto free;
864
Linus Torvalds272ddc82016-10-24 19:00:44 -0700865 /* Maybe we should limit FOLL_FORCE to actual ptrace users? */
866 flags = FOLL_FORCE;
Lorenzo Stoakes6347e8d2016-10-13 01:20:19 +0100867 if (write)
868 flags |= FOLL_WRITE;
869
Linus Torvalds1da177e2005-04-16 15:20:36 -0700870 while (count > 0) {
Oleg Nesterov572d34b2012-01-31 17:14:54 +0100871 int this_len = min_t(int, count, PAGE_SIZE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700872
Oleg Nesterov572d34b2012-01-31 17:14:54 +0100873 if (write && copy_from_user(page, buf, this_len)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700874 copied = -EFAULT;
875 break;
876 }
Oleg Nesterov572d34b2012-01-31 17:14:54 +0100877
Lorenzo Stoakes6347e8d2016-10-13 01:20:19 +0100878 this_len = access_remote_vm(mm, addr, page, this_len, flags);
Oleg Nesterov572d34b2012-01-31 17:14:54 +0100879 if (!this_len) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700880 if (!copied)
881 copied = -EIO;
882 break;
883 }
Oleg Nesterov572d34b2012-01-31 17:14:54 +0100884
885 if (!write && copy_to_user(buf, page, this_len)) {
886 copied = -EFAULT;
887 break;
888 }
889
890 buf += this_len;
891 addr += this_len;
892 copied += this_len;
893 count -= this_len;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700894 }
Oleg Nesterov572d34b2012-01-31 17:14:54 +0100895 *ppos = addr;
KOSAKI Motohiro30cd8902011-05-26 16:25:52 -0700896
Oleg Nesterov6d08f2c2012-01-31 17:15:11 +0100897 mmput(mm);
898free:
KOSAKI Motohiro30cd8902011-05-26 16:25:52 -0700899 free_page((unsigned long) page);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700900 return copied;
901}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700902
Oleg Nesterov572d34b2012-01-31 17:14:54 +0100903static ssize_t mem_read(struct file *file, char __user *buf,
904 size_t count, loff_t *ppos)
905{
906 return mem_rw(file, buf, count, ppos, 0);
907}
908
909static ssize_t mem_write(struct file *file, const char __user *buf,
910 size_t count, loff_t *ppos)
911{
912 return mem_rw(file, (char __user*)buf, count, ppos, 1);
913}
914
Matt Mackall85863e42008-02-04 22:29:04 -0800915loff_t mem_lseek(struct file *file, loff_t offset, int orig)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700916{
917 switch (orig) {
918 case 0:
919 file->f_pos = offset;
920 break;
921 case 1:
922 file->f_pos += offset;
923 break;
924 default:
925 return -EINVAL;
926 }
927 force_successful_syscall_return();
928 return file->f_pos;
929}
930
Linus Torvaldse2683372012-01-17 15:21:19 -0800931static int mem_release(struct inode *inode, struct file *file)
932{
933 struct mm_struct *mm = file->private_data;
Oleg Nesterov71879d32012-01-31 17:14:38 +0100934 if (mm)
Oleg Nesterov6d08f2c2012-01-31 17:15:11 +0100935 mmdrop(mm);
Linus Torvaldse2683372012-01-17 15:21:19 -0800936 return 0;
937}
938
Arjan van de Ven00977a52007-02-12 00:55:34 -0800939static const struct file_operations proc_mem_operations = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700940 .llseek = mem_lseek,
941 .read = mem_read,
942 .write = mem_write,
943 .open = mem_open,
Linus Torvaldse2683372012-01-17 15:21:19 -0800944 .release = mem_release,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700945};
946
Cong Wangb409e572012-05-31 16:26:17 -0700947static int environ_open(struct inode *inode, struct file *file)
948{
949 return __mem_open(inode, file, PTRACE_MODE_READ);
950}
951
James Pearson315e28c2007-10-16 23:30:17 -0700952static ssize_t environ_read(struct file *file, char __user *buf,
953 size_t count, loff_t *ppos)
954{
James Pearson315e28c2007-10-16 23:30:17 -0700955 char *page;
956 unsigned long src = *ppos;
Cong Wangb409e572012-05-31 16:26:17 -0700957 int ret = 0;
958 struct mm_struct *mm = file->private_data;
Mateusz Guzika3b609e2016-01-20 15:01:05 -0800959 unsigned long env_start, env_end;
James Pearson315e28c2007-10-16 23:30:17 -0700960
Mathias Krause8148a732016-05-05 16:22:26 -0700961 /* Ensure the process spawned far enough to have an environment. */
962 if (!mm || !mm->env_end)
Cong Wangb409e572012-05-31 16:26:17 -0700963 return 0;
James Pearson315e28c2007-10-16 23:30:17 -0700964
James Pearson315e28c2007-10-16 23:30:17 -0700965 page = (char *)__get_free_page(GFP_TEMPORARY);
966 if (!page)
Cong Wangb409e572012-05-31 16:26:17 -0700967 return -ENOMEM;
James Pearson315e28c2007-10-16 23:30:17 -0700968
Al Virod6f64b82011-02-15 22:26:01 -0500969 ret = 0;
Cong Wangb409e572012-05-31 16:26:17 -0700970 if (!atomic_inc_not_zero(&mm->mm_users))
971 goto free;
Mateusz Guzika3b609e2016-01-20 15:01:05 -0800972
973 down_read(&mm->mmap_sem);
974 env_start = mm->env_start;
975 env_end = mm->env_end;
976 up_read(&mm->mmap_sem);
977
James Pearson315e28c2007-10-16 23:30:17 -0700978 while (count > 0) {
Djalal Harounie8905ec2012-07-30 14:42:26 -0700979 size_t this_len, max_len;
980 int retval;
981
Mateusz Guzika3b609e2016-01-20 15:01:05 -0800982 if (src >= (env_end - env_start))
Djalal Harounie8905ec2012-07-30 14:42:26 -0700983 break;
James Pearson315e28c2007-10-16 23:30:17 -0700984
Mateusz Guzika3b609e2016-01-20 15:01:05 -0800985 this_len = env_end - (env_start + src);
James Pearson315e28c2007-10-16 23:30:17 -0700986
Djalal Harounie8905ec2012-07-30 14:42:26 -0700987 max_len = min_t(size_t, PAGE_SIZE, count);
988 this_len = min(max_len, this_len);
James Pearson315e28c2007-10-16 23:30:17 -0700989
Willy Tarreau6f1abf82018-05-11 08:11:44 +0200990 retval = access_remote_vm(mm, (env_start + src), page, this_len, FOLL_ANON);
James Pearson315e28c2007-10-16 23:30:17 -0700991
992 if (retval <= 0) {
993 ret = retval;
994 break;
995 }
996
997 if (copy_to_user(buf, page, retval)) {
998 ret = -EFAULT;
999 break;
1000 }
1001
1002 ret += retval;
1003 src += retval;
1004 buf += retval;
1005 count -= retval;
1006 }
1007 *ppos = src;
James Pearson315e28c2007-10-16 23:30:17 -07001008 mmput(mm);
Cong Wangb409e572012-05-31 16:26:17 -07001009
1010free:
James Pearson315e28c2007-10-16 23:30:17 -07001011 free_page((unsigned long) page);
James Pearson315e28c2007-10-16 23:30:17 -07001012 return ret;
1013}
1014
1015static const struct file_operations proc_environ_operations = {
Cong Wangb409e572012-05-31 16:26:17 -07001016 .open = environ_open,
James Pearson315e28c2007-10-16 23:30:17 -07001017 .read = environ_read,
Arnd Bergmann87df8422010-03-17 23:06:02 +01001018 .llseek = generic_file_llseek,
Cong Wangb409e572012-05-31 16:26:17 -07001019 .release = mem_release,
James Pearson315e28c2007-10-16 23:30:17 -07001020};
1021
Al Viroc5317162016-10-05 18:43:43 -04001022static int auxv_open(struct inode *inode, struct file *file)
1023{
1024 return __mem_open(inode, file, PTRACE_MODE_READ_FSCREDS);
1025}
1026
1027static ssize_t auxv_read(struct file *file, char __user *buf,
1028 size_t count, loff_t *ppos)
1029{
1030 struct mm_struct *mm = file->private_data;
1031 unsigned int nwords = 0;
Leon Yu06b28492016-10-27 17:46:50 -07001032
1033 if (!mm)
1034 return 0;
Al Viroc5317162016-10-05 18:43:43 -04001035 do {
1036 nwords += 2;
1037 } while (mm->saved_auxv[nwords - 2] != 0); /* AT_NULL */
1038 return simple_read_from_buffer(buf, count, ppos, mm->saved_auxv,
1039 nwords * sizeof(mm->saved_auxv[0]));
1040}
1041
1042static const struct file_operations proc_auxv_operations = {
1043 .open = auxv_open,
1044 .read = auxv_read,
1045 .llseek = generic_file_llseek,
1046 .release = mem_release,
1047};
1048
David Rientjesfa0cbbf2012-11-12 17:53:04 -08001049static ssize_t oom_adj_read(struct file *file, char __user *buf, size_t count,
1050 loff_t *ppos)
1051{
Al Viro496ad9a2013-01-23 17:07:38 -05001052 struct task_struct *task = get_proc_task(file_inode(file));
David Rientjesfa0cbbf2012-11-12 17:53:04 -08001053 char buffer[PROC_NUMBUF];
1054 int oom_adj = OOM_ADJUST_MIN;
1055 size_t len;
David Rientjesfa0cbbf2012-11-12 17:53:04 -08001056
1057 if (!task)
1058 return -ESRCH;
Michal Hockof913da52016-07-28 15:44:37 -07001059 if (task->signal->oom_score_adj == OOM_SCORE_ADJ_MAX)
1060 oom_adj = OOM_ADJUST_MAX;
1061 else
1062 oom_adj = (task->signal->oom_score_adj * -OOM_DISABLE) /
1063 OOM_SCORE_ADJ_MAX;
David Rientjesfa0cbbf2012-11-12 17:53:04 -08001064 put_task_struct(task);
1065 len = snprintf(buffer, sizeof(buffer), "%d\n", oom_adj);
1066 return simple_read_from_buffer(buf, count, ppos, buffer, len);
1067}
1068
Michal Hocko1d5f0ac2016-07-28 15:44:40 -07001069static int __set_oom_adj(struct file *file, int oom_adj, bool legacy)
1070{
1071 static DEFINE_MUTEX(oom_adj_mutex);
Michal Hocko44a70ade2016-07-28 15:44:43 -07001072 struct mm_struct *mm = NULL;
Michal Hocko1d5f0ac2016-07-28 15:44:40 -07001073 struct task_struct *task;
1074 int err = 0;
1075
1076 task = get_proc_task(file_inode(file));
1077 if (!task)
1078 return -ESRCH;
1079
1080 mutex_lock(&oom_adj_mutex);
1081 if (legacy) {
1082 if (oom_adj < task->signal->oom_score_adj &&
1083 !capable(CAP_SYS_RESOURCE)) {
1084 err = -EACCES;
1085 goto err_unlock;
1086 }
1087 /*
1088 * /proc/pid/oom_adj is provided for legacy purposes, ask users to use
1089 * /proc/pid/oom_score_adj instead.
1090 */
1091 pr_warn_once("%s (%d): /proc/%d/oom_adj is deprecated, please use /proc/%d/oom_score_adj instead.\n",
1092 current->comm, task_pid_nr(current), task_pid_nr(task),
1093 task_pid_nr(task));
1094 } else {
1095 if ((short)oom_adj < task->signal->oom_score_adj_min &&
1096 !capable(CAP_SYS_RESOURCE)) {
1097 err = -EACCES;
1098 goto err_unlock;
1099 }
1100 }
1101
Michal Hocko44a70ade2016-07-28 15:44:43 -07001102 /*
1103 * Make sure we will check other processes sharing the mm if this is
1104 * not vfrok which wants its own oom_score_adj.
1105 * pin the mm so it doesn't go away and get reused after task_unlock
1106 */
1107 if (!task->vfork_done) {
1108 struct task_struct *p = find_lock_task_mm(task);
1109
1110 if (p) {
1111 if (atomic_read(&p->mm->mm_users) > 1) {
1112 mm = p->mm;
1113 atomic_inc(&mm->mm_count);
1114 }
1115 task_unlock(p);
1116 }
1117 }
1118
Michal Hocko1d5f0ac2016-07-28 15:44:40 -07001119 task->signal->oom_score_adj = oom_adj;
1120 if (!legacy && has_capability_noaudit(current, CAP_SYS_RESOURCE))
1121 task->signal->oom_score_adj_min = (short)oom_adj;
1122 trace_oom_score_adj_update(task);
Michal Hocko44a70ade2016-07-28 15:44:43 -07001123
1124 if (mm) {
1125 struct task_struct *p;
1126
1127 rcu_read_lock();
1128 for_each_process(p) {
1129 if (same_thread_group(task, p))
1130 continue;
1131
1132 /* do not touch kernel threads or the global init */
1133 if (p->flags & PF_KTHREAD || is_global_init(p))
1134 continue;
1135
1136 task_lock(p);
1137 if (!p->vfork_done && process_shares_mm(p, mm)) {
Michal Hocko44a70ade2016-07-28 15:44:43 -07001138 p->signal->oom_score_adj = oom_adj;
1139 if (!legacy && has_capability_noaudit(current, CAP_SYS_RESOURCE))
1140 p->signal->oom_score_adj_min = (short)oom_adj;
1141 }
1142 task_unlock(p);
1143 }
1144 rcu_read_unlock();
1145 mmdrop(mm);
1146 }
Michal Hocko1d5f0ac2016-07-28 15:44:40 -07001147err_unlock:
1148 mutex_unlock(&oom_adj_mutex);
1149 put_task_struct(task);
1150 return err;
1151}
Michal Hockof913da52016-07-28 15:44:37 -07001152
David Rientjesb72bdfa2015-11-05 18:50:32 -08001153/*
1154 * /proc/pid/oom_adj exists solely for backwards compatibility with previous
1155 * kernels. The effective policy is defined by oom_score_adj, which has a
1156 * different scale: oom_adj grew exponentially and oom_score_adj grows linearly.
1157 * Values written to oom_adj are simply mapped linearly to oom_score_adj.
1158 * Processes that become oom disabled via oom_adj will still be oom disabled
1159 * with this implementation.
1160 *
1161 * oom_adj cannot be removed since existing userspace binaries use it.
1162 */
David Rientjesfa0cbbf2012-11-12 17:53:04 -08001163static ssize_t oom_adj_write(struct file *file, const char __user *buf,
1164 size_t count, loff_t *ppos)
1165{
David Rientjesfa0cbbf2012-11-12 17:53:04 -08001166 char buffer[PROC_NUMBUF];
1167 int oom_adj;
David Rientjesfa0cbbf2012-11-12 17:53:04 -08001168 int err;
1169
1170 memset(buffer, 0, sizeof(buffer));
1171 if (count > sizeof(buffer) - 1)
1172 count = sizeof(buffer) - 1;
1173 if (copy_from_user(buffer, buf, count)) {
1174 err = -EFAULT;
1175 goto out;
1176 }
1177
1178 err = kstrtoint(strstrip(buffer), 0, &oom_adj);
1179 if (err)
1180 goto out;
1181 if ((oom_adj < OOM_ADJUST_MIN || oom_adj > OOM_ADJUST_MAX) &&
1182 oom_adj != OOM_DISABLE) {
1183 err = -EINVAL;
1184 goto out;
1185 }
1186
David Rientjesfa0cbbf2012-11-12 17:53:04 -08001187 /*
1188 * Scale /proc/pid/oom_score_adj appropriately ensuring that a maximum
1189 * value is always attainable.
1190 */
1191 if (oom_adj == OOM_ADJUST_MAX)
1192 oom_adj = OOM_SCORE_ADJ_MAX;
1193 else
1194 oom_adj = (oom_adj * OOM_SCORE_ADJ_MAX) / -OOM_DISABLE;
1195
Michal Hocko1d5f0ac2016-07-28 15:44:40 -07001196 err = __set_oom_adj(file, oom_adj, true);
David Rientjesfa0cbbf2012-11-12 17:53:04 -08001197out:
1198 return err < 0 ? err : count;
1199}
1200
1201static const struct file_operations proc_oom_adj_operations = {
1202 .read = oom_adj_read,
1203 .write = oom_adj_write,
1204 .llseek = generic_file_llseek,
1205};
1206
David Rientjesa63d83f2010-08-09 17:19:46 -07001207static ssize_t oom_score_adj_read(struct file *file, char __user *buf,
1208 size_t count, loff_t *ppos)
1209{
Al Viro496ad9a2013-01-23 17:07:38 -05001210 struct task_struct *task = get_proc_task(file_inode(file));
David Rientjesa63d83f2010-08-09 17:19:46 -07001211 char buffer[PROC_NUMBUF];
David Rientjesa9c58b902012-12-11 16:02:54 -08001212 short oom_score_adj = OOM_SCORE_ADJ_MIN;
David Rientjesa63d83f2010-08-09 17:19:46 -07001213 size_t len;
1214
1215 if (!task)
1216 return -ESRCH;
Michal Hockof913da52016-07-28 15:44:37 -07001217 oom_score_adj = task->signal->oom_score_adj;
David Rientjesa63d83f2010-08-09 17:19:46 -07001218 put_task_struct(task);
David Rientjesa9c58b902012-12-11 16:02:54 -08001219 len = snprintf(buffer, sizeof(buffer), "%hd\n", oom_score_adj);
David Rientjesa63d83f2010-08-09 17:19:46 -07001220 return simple_read_from_buffer(buf, count, ppos, buffer, len);
1221}
1222
1223static ssize_t oom_score_adj_write(struct file *file, const char __user *buf,
1224 size_t count, loff_t *ppos)
1225{
David Rientjesa63d83f2010-08-09 17:19:46 -07001226 char buffer[PROC_NUMBUF];
Alexey Dobriyan0a8cb8e2011-05-26 16:25:50 -07001227 int oom_score_adj;
David Rientjesa63d83f2010-08-09 17:19:46 -07001228 int err;
1229
1230 memset(buffer, 0, sizeof(buffer));
1231 if (count > sizeof(buffer) - 1)
1232 count = sizeof(buffer) - 1;
David Rientjes723548b2010-10-26 14:21:25 -07001233 if (copy_from_user(buffer, buf, count)) {
1234 err = -EFAULT;
1235 goto out;
1236 }
David Rientjesa63d83f2010-08-09 17:19:46 -07001237
Alexey Dobriyan0a8cb8e2011-05-26 16:25:50 -07001238 err = kstrtoint(strstrip(buffer), 0, &oom_score_adj);
David Rientjesa63d83f2010-08-09 17:19:46 -07001239 if (err)
David Rientjes723548b2010-10-26 14:21:25 -07001240 goto out;
David Rientjesa63d83f2010-08-09 17:19:46 -07001241 if (oom_score_adj < OOM_SCORE_ADJ_MIN ||
David Rientjes723548b2010-10-26 14:21:25 -07001242 oom_score_adj > OOM_SCORE_ADJ_MAX) {
1243 err = -EINVAL;
1244 goto out;
1245 }
David Rientjesa63d83f2010-08-09 17:19:46 -07001246
Michal Hocko1d5f0ac2016-07-28 15:44:40 -07001247 err = __set_oom_adj(file, oom_score_adj, false);
David Rientjes723548b2010-10-26 14:21:25 -07001248out:
1249 return err < 0 ? err : count;
David Rientjesa63d83f2010-08-09 17:19:46 -07001250}
1251
1252static const struct file_operations proc_oom_score_adj_operations = {
1253 .read = oom_score_adj_read,
1254 .write = oom_score_adj_write,
Arnd Bergmann6038f372010-08-15 18:52:59 +02001255 .llseek = default_llseek,
David Rientjesa63d83f2010-08-09 17:19:46 -07001256};
1257
Linus Torvalds1da177e2005-04-16 15:20:36 -07001258#ifdef CONFIG_AUDITSYSCALL
1259#define TMPBUFLEN 21
1260static ssize_t proc_loginuid_read(struct file * file, char __user * buf,
1261 size_t count, loff_t *ppos)
1262{
Al Viro496ad9a2013-01-23 17:07:38 -05001263 struct inode * inode = file_inode(file);
Eric W. Biederman99f89552006-06-26 00:25:55 -07001264 struct task_struct *task = get_proc_task(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001265 ssize_t length;
1266 char tmpbuf[TMPBUFLEN];
1267
Eric W. Biederman99f89552006-06-26 00:25:55 -07001268 if (!task)
1269 return -ESRCH;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001270 length = scnprintf(tmpbuf, TMPBUFLEN, "%u",
Eric W. Biedermane1760bd2012-09-10 22:39:43 -07001271 from_kuid(file->f_cred->user_ns,
1272 audit_get_loginuid(task)));
Eric W. Biederman99f89552006-06-26 00:25:55 -07001273 put_task_struct(task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001274 return simple_read_from_buffer(buf, count, ppos, tmpbuf, length);
1275}
1276
1277static ssize_t proc_loginuid_write(struct file * file, const char __user * buf,
1278 size_t count, loff_t *ppos)
1279{
Al Viro496ad9a2013-01-23 17:07:38 -05001280 struct inode * inode = file_inode(file);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001281 uid_t loginuid;
Eric W. Biedermane1760bd2012-09-10 22:39:43 -07001282 kuid_t kloginuid;
Alexey Dobriyan774636e2015-09-09 15:36:59 -07001283 int rv;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001284
Paul E. McKenney7dc52152010-02-22 17:04:52 -08001285 rcu_read_lock();
1286 if (current != pid_task(proc_pid(inode), PIDTYPE_PID)) {
1287 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001288 return -EPERM;
Paul E. McKenney7dc52152010-02-22 17:04:52 -08001289 }
1290 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001291
Linus Torvalds1da177e2005-04-16 15:20:36 -07001292 if (*ppos != 0) {
1293 /* No partial writes. */
1294 return -EINVAL;
1295 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001296
Alexey Dobriyan774636e2015-09-09 15:36:59 -07001297 rv = kstrtou32_from_user(buf, count, 10, &loginuid);
1298 if (rv < 0)
1299 return rv;
Eric Paris81407c82013-05-24 09:49:14 -04001300
1301 /* is userspace tring to explicitly UNSET the loginuid? */
1302 if (loginuid == AUDIT_UID_UNSET) {
1303 kloginuid = INVALID_UID;
1304 } else {
1305 kloginuid = make_kuid(file->f_cred->user_ns, loginuid);
Alexey Dobriyan774636e2015-09-09 15:36:59 -07001306 if (!uid_valid(kloginuid))
1307 return -EINVAL;
Eric W. Biedermane1760bd2012-09-10 22:39:43 -07001308 }
1309
Alexey Dobriyan774636e2015-09-09 15:36:59 -07001310 rv = audit_set_loginuid(kloginuid);
1311 if (rv < 0)
1312 return rv;
1313 return count;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001314}
1315
Arjan van de Ven00977a52007-02-12 00:55:34 -08001316static const struct file_operations proc_loginuid_operations = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001317 .read = proc_loginuid_read,
1318 .write = proc_loginuid_write,
Arnd Bergmann87df8422010-03-17 23:06:02 +01001319 .llseek = generic_file_llseek,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001320};
Eric Paris1e0bd752008-03-13 08:15:31 -04001321
1322static ssize_t proc_sessionid_read(struct file * file, char __user * buf,
1323 size_t count, loff_t *ppos)
1324{
Al Viro496ad9a2013-01-23 17:07:38 -05001325 struct inode * inode = file_inode(file);
Eric Paris1e0bd752008-03-13 08:15:31 -04001326 struct task_struct *task = get_proc_task(inode);
1327 ssize_t length;
1328 char tmpbuf[TMPBUFLEN];
1329
1330 if (!task)
1331 return -ESRCH;
1332 length = scnprintf(tmpbuf, TMPBUFLEN, "%u",
1333 audit_get_sessionid(task));
1334 put_task_struct(task);
1335 return simple_read_from_buffer(buf, count, ppos, tmpbuf, length);
1336}
1337
1338static const struct file_operations proc_sessionid_operations = {
1339 .read = proc_sessionid_read,
Arnd Bergmann87df8422010-03-17 23:06:02 +01001340 .llseek = generic_file_llseek,
Eric Paris1e0bd752008-03-13 08:15:31 -04001341};
Linus Torvalds1da177e2005-04-16 15:20:36 -07001342#endif
1343
Akinobu Mitaf4f154f2006-12-08 02:39:47 -08001344#ifdef CONFIG_FAULT_INJECTION
1345static ssize_t proc_fault_inject_read(struct file * file, char __user * buf,
1346 size_t count, loff_t *ppos)
1347{
Al Viro496ad9a2013-01-23 17:07:38 -05001348 struct task_struct *task = get_proc_task(file_inode(file));
Akinobu Mitaf4f154f2006-12-08 02:39:47 -08001349 char buffer[PROC_NUMBUF];
1350 size_t len;
1351 int make_it_fail;
Akinobu Mitaf4f154f2006-12-08 02:39:47 -08001352
1353 if (!task)
1354 return -ESRCH;
1355 make_it_fail = task->make_it_fail;
1356 put_task_struct(task);
1357
1358 len = snprintf(buffer, sizeof(buffer), "%i\n", make_it_fail);
Akinobu Mita0c28f282007-05-08 00:31:41 -07001359
1360 return simple_read_from_buffer(buf, count, ppos, buffer, len);
Akinobu Mitaf4f154f2006-12-08 02:39:47 -08001361}
1362
1363static ssize_t proc_fault_inject_write(struct file * file,
1364 const char __user * buf, size_t count, loff_t *ppos)
1365{
1366 struct task_struct *task;
Alexey Dobriyan774636e2015-09-09 15:36:59 -07001367 char buffer[PROC_NUMBUF];
Akinobu Mitaf4f154f2006-12-08 02:39:47 -08001368 int make_it_fail;
Alexey Dobriyan774636e2015-09-09 15:36:59 -07001369 int rv;
Akinobu Mitaf4f154f2006-12-08 02:39:47 -08001370
1371 if (!capable(CAP_SYS_RESOURCE))
1372 return -EPERM;
1373 memset(buffer, 0, sizeof(buffer));
1374 if (count > sizeof(buffer) - 1)
1375 count = sizeof(buffer) - 1;
1376 if (copy_from_user(buffer, buf, count))
1377 return -EFAULT;
Alexey Dobriyan774636e2015-09-09 15:36:59 -07001378 rv = kstrtoint(strstrip(buffer), 0, &make_it_fail);
1379 if (rv < 0)
1380 return rv;
Dave Jones16caed32014-04-07 15:39:15 -07001381 if (make_it_fail < 0 || make_it_fail > 1)
1382 return -EINVAL;
1383
Al Viro496ad9a2013-01-23 17:07:38 -05001384 task = get_proc_task(file_inode(file));
Akinobu Mitaf4f154f2006-12-08 02:39:47 -08001385 if (!task)
1386 return -ESRCH;
1387 task->make_it_fail = make_it_fail;
1388 put_task_struct(task);
Vincent Licba8aaf2009-09-22 16:45:38 -07001389
1390 return count;
Akinobu Mitaf4f154f2006-12-08 02:39:47 -08001391}
1392
Arjan van de Ven00977a52007-02-12 00:55:34 -08001393static const struct file_operations proc_fault_inject_operations = {
Akinobu Mitaf4f154f2006-12-08 02:39:47 -08001394 .read = proc_fault_inject_read,
1395 .write = proc_fault_inject_write,
Arnd Bergmann87df8422010-03-17 23:06:02 +01001396 .llseek = generic_file_llseek,
Akinobu Mitaf4f154f2006-12-08 02:39:47 -08001397};
1398#endif
1399
Arjan van de Ven97455122008-01-25 21:08:34 +01001400
Ingo Molnar43ae34c2007-07-09 18:52:00 +02001401#ifdef CONFIG_SCHED_DEBUG
1402/*
1403 * Print out various scheduling related per-task fields:
1404 */
1405static int sched_show(struct seq_file *m, void *v)
1406{
1407 struct inode *inode = m->private;
1408 struct task_struct *p;
1409
Ingo Molnar43ae34c2007-07-09 18:52:00 +02001410 p = get_proc_task(inode);
1411 if (!p)
1412 return -ESRCH;
1413 proc_sched_show_task(p, m);
1414
1415 put_task_struct(p);
1416
1417 return 0;
1418}
1419
1420static ssize_t
1421sched_write(struct file *file, const char __user *buf,
1422 size_t count, loff_t *offset)
1423{
Al Viro496ad9a2013-01-23 17:07:38 -05001424 struct inode *inode = file_inode(file);
Ingo Molnar43ae34c2007-07-09 18:52:00 +02001425 struct task_struct *p;
1426
Ingo Molnar43ae34c2007-07-09 18:52:00 +02001427 p = get_proc_task(inode);
1428 if (!p)
1429 return -ESRCH;
1430 proc_sched_set_task(p);
1431
1432 put_task_struct(p);
1433
1434 return count;
1435}
1436
1437static int sched_open(struct inode *inode, struct file *filp)
1438{
Jovi Zhangc6a34052011-01-12 17:00:34 -08001439 return single_open(filp, sched_show, inode);
Ingo Molnar43ae34c2007-07-09 18:52:00 +02001440}
1441
1442static const struct file_operations proc_pid_sched_operations = {
1443 .open = sched_open,
1444 .read = seq_read,
1445 .write = sched_write,
1446 .llseek = seq_lseek,
Alexey Dobriyan5ea473a2007-07-31 00:38:50 -07001447 .release = single_release,
Ingo Molnar43ae34c2007-07-09 18:52:00 +02001448};
1449
1450#endif
1451
Mike Galbraith5091faa2010-11-30 14:18:03 +01001452#ifdef CONFIG_SCHED_AUTOGROUP
1453/*
1454 * Print out autogroup related information:
1455 */
1456static int sched_autogroup_show(struct seq_file *m, void *v)
1457{
1458 struct inode *inode = m->private;
1459 struct task_struct *p;
1460
1461 p = get_proc_task(inode);
1462 if (!p)
1463 return -ESRCH;
1464 proc_sched_autogroup_show_task(p, m);
1465
1466 put_task_struct(p);
1467
1468 return 0;
1469}
1470
1471static ssize_t
1472sched_autogroup_write(struct file *file, const char __user *buf,
1473 size_t count, loff_t *offset)
1474{
Al Viro496ad9a2013-01-23 17:07:38 -05001475 struct inode *inode = file_inode(file);
Mike Galbraith5091faa2010-11-30 14:18:03 +01001476 struct task_struct *p;
1477 char buffer[PROC_NUMBUF];
Alexey Dobriyan0a8cb8e2011-05-26 16:25:50 -07001478 int nice;
Mike Galbraith5091faa2010-11-30 14:18:03 +01001479 int err;
1480
1481 memset(buffer, 0, sizeof(buffer));
1482 if (count > sizeof(buffer) - 1)
1483 count = sizeof(buffer) - 1;
1484 if (copy_from_user(buffer, buf, count))
1485 return -EFAULT;
1486
Alexey Dobriyan0a8cb8e2011-05-26 16:25:50 -07001487 err = kstrtoint(strstrip(buffer), 0, &nice);
1488 if (err < 0)
1489 return err;
Mike Galbraith5091faa2010-11-30 14:18:03 +01001490
1491 p = get_proc_task(inode);
1492 if (!p)
1493 return -ESRCH;
1494
Hiroshi Shimamoto2e5b5b32012-02-23 17:41:27 +09001495 err = proc_sched_autogroup_set_nice(p, nice);
Mike Galbraith5091faa2010-11-30 14:18:03 +01001496 if (err)
1497 count = err;
1498
1499 put_task_struct(p);
1500
1501 return count;
1502}
1503
1504static int sched_autogroup_open(struct inode *inode, struct file *filp)
1505{
1506 int ret;
1507
1508 ret = single_open(filp, sched_autogroup_show, NULL);
1509 if (!ret) {
1510 struct seq_file *m = filp->private_data;
1511
1512 m->private = inode;
1513 }
1514 return ret;
1515}
1516
1517static const struct file_operations proc_pid_sched_autogroup_operations = {
1518 .open = sched_autogroup_open,
1519 .read = seq_read,
1520 .write = sched_autogroup_write,
1521 .llseek = seq_lseek,
1522 .release = single_release,
1523};
1524
1525#endif /* CONFIG_SCHED_AUTOGROUP */
1526
john stultz4614a696b2009-12-14 18:00:05 -08001527static ssize_t comm_write(struct file *file, const char __user *buf,
1528 size_t count, loff_t *offset)
1529{
Al Viro496ad9a2013-01-23 17:07:38 -05001530 struct inode *inode = file_inode(file);
john stultz4614a696b2009-12-14 18:00:05 -08001531 struct task_struct *p;
1532 char buffer[TASK_COMM_LEN];
David Rientjes830e0fc2013-04-30 15:28:18 -07001533 const size_t maxlen = sizeof(buffer) - 1;
john stultz4614a696b2009-12-14 18:00:05 -08001534
1535 memset(buffer, 0, sizeof(buffer));
David Rientjes830e0fc2013-04-30 15:28:18 -07001536 if (copy_from_user(buffer, buf, count > maxlen ? maxlen : count))
john stultz4614a696b2009-12-14 18:00:05 -08001537 return -EFAULT;
1538
1539 p = get_proc_task(inode);
1540 if (!p)
1541 return -ESRCH;
1542
1543 if (same_thread_group(current, p))
1544 set_task_comm(p, buffer);
1545 else
1546 count = -EINVAL;
1547
1548 put_task_struct(p);
1549
1550 return count;
1551}
1552
1553static int comm_show(struct seq_file *m, void *v)
1554{
1555 struct inode *inode = m->private;
1556 struct task_struct *p;
1557
1558 p = get_proc_task(inode);
1559 if (!p)
1560 return -ESRCH;
1561
1562 task_lock(p);
1563 seq_printf(m, "%s\n", p->comm);
1564 task_unlock(p);
1565
1566 put_task_struct(p);
1567
1568 return 0;
1569}
1570
1571static int comm_open(struct inode *inode, struct file *filp)
1572{
Jovi Zhangc6a34052011-01-12 17:00:34 -08001573 return single_open(filp, comm_show, inode);
john stultz4614a696b2009-12-14 18:00:05 -08001574}
1575
1576static const struct file_operations proc_pid_set_comm_operations = {
1577 .open = comm_open,
1578 .read = seq_read,
1579 .write = comm_write,
1580 .llseek = seq_lseek,
1581 .release = single_release,
1582};
1583
Cyrill Gorcunov7773fbc2012-01-10 15:11:20 -08001584static int proc_exe_link(struct dentry *dentry, struct path *exe_path)
Matt Helsley925d1c42008-04-29 01:01:36 -07001585{
1586 struct task_struct *task;
Matt Helsley925d1c42008-04-29 01:01:36 -07001587 struct file *exe_file;
1588
David Howells2b0143b2015-03-17 22:25:59 +00001589 task = get_proc_task(d_inode(dentry));
Matt Helsley925d1c42008-04-29 01:01:36 -07001590 if (!task)
1591 return -ENOENT;
Mateusz Guzikcd81a912016-08-23 16:20:38 +02001592 exe_file = get_task_exe_file(task);
Matt Helsley925d1c42008-04-29 01:01:36 -07001593 put_task_struct(task);
Matt Helsley925d1c42008-04-29 01:01:36 -07001594 if (exe_file) {
1595 *exe_path = exe_file->f_path;
1596 path_get(&exe_file->f_path);
1597 fput(exe_file);
1598 return 0;
1599 } else
1600 return -ENOENT;
1601}
1602
Al Viro6b255392015-11-17 10:20:54 -05001603static const char *proc_pid_get_link(struct dentry *dentry,
Al Virofceef392015-12-29 15:58:39 -05001604 struct inode *inode,
1605 struct delayed_call *done)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001606{
Christoph Hellwig408ef012012-06-18 10:47:03 -04001607 struct path path;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001608 int error = -EACCES;
1609
Al Viro6b255392015-11-17 10:20:54 -05001610 if (!dentry)
1611 return ERR_PTR(-ECHILD);
1612
Eric W. Biederman778c1142006-06-26 00:25:58 -07001613 /* Are we allowed to snoop on the tasks file descriptors? */
1614 if (!proc_fd_access_allowed(inode))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001615 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001616
Christoph Hellwig408ef012012-06-18 10:47:03 -04001617 error = PROC_I(inode)->op.proc_get_link(dentry, &path);
1618 if (error)
1619 goto out;
1620
Al Viro6e771372015-05-02 13:37:52 -04001621 nd_jump_link(&path);
Christoph Hellwig408ef012012-06-18 10:47:03 -04001622 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001623out:
Al Viro008b1502005-08-20 00:17:39 +01001624 return ERR_PTR(error);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001625}
1626
Jan Blunck3dcd25f2008-02-14 19:38:35 -08001627static int do_proc_readlink(struct path *path, char __user *buffer, int buflen)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001628{
Mel Gormane12ba742007-10-16 01:25:52 -07001629 char *tmp = (char*)__get_free_page(GFP_TEMPORARY);
Jan Blunck3dcd25f2008-02-14 19:38:35 -08001630 char *pathname;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001631 int len;
1632
1633 if (!tmp)
1634 return -ENOMEM;
Akinobu Mita0c28f282007-05-08 00:31:41 -07001635
Eric W. Biederman7b2a69b2010-12-05 15:51:21 -08001636 pathname = d_path(path, tmp, PAGE_SIZE);
Jan Blunck3dcd25f2008-02-14 19:38:35 -08001637 len = PTR_ERR(pathname);
1638 if (IS_ERR(pathname))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001639 goto out;
Jan Blunck3dcd25f2008-02-14 19:38:35 -08001640 len = tmp + PAGE_SIZE - 1 - pathname;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001641
1642 if (len > buflen)
1643 len = buflen;
Jan Blunck3dcd25f2008-02-14 19:38:35 -08001644 if (copy_to_user(buffer, pathname, len))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001645 len = -EFAULT;
1646 out:
1647 free_page((unsigned long)tmp);
1648 return len;
1649}
1650
1651static int proc_pid_readlink(struct dentry * dentry, char __user * buffer, int buflen)
1652{
1653 int error = -EACCES;
David Howells2b0143b2015-03-17 22:25:59 +00001654 struct inode *inode = d_inode(dentry);
Jan Blunck3dcd25f2008-02-14 19:38:35 -08001655 struct path path;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001656
Eric W. Biederman778c1142006-06-26 00:25:58 -07001657 /* Are we allowed to snoop on the tasks file descriptors? */
1658 if (!proc_fd_access_allowed(inode))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001659 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001660
Cyrill Gorcunov7773fbc2012-01-10 15:11:20 -08001661 error = PROC_I(inode)->op.proc_get_link(dentry, &path);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001662 if (error)
1663 goto out;
1664
Jan Blunck3dcd25f2008-02-14 19:38:35 -08001665 error = do_proc_readlink(&path, buffer, buflen);
1666 path_put(&path);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001667out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001668 return error;
1669}
1670
Cyrill Gorcunovfaf60af2012-08-23 14:43:24 +04001671const struct inode_operations proc_pid_link_inode_operations = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001672 .readlink = proc_pid_readlink,
Al Viro6b255392015-11-17 10:20:54 -05001673 .get_link = proc_pid_get_link,
Linus Torvalds6d76fa52006-07-15 12:26:45 -07001674 .setattr = proc_setattr,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001675};
1676
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001677
1678/* building an inode */
1679
Eric W. Biederman6b4e3062010-03-07 16:41:34 -08001680struct inode *proc_pid_make_inode(struct super_block * sb, struct task_struct *task)
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001681{
1682 struct inode * inode;
1683 struct proc_inode *ei;
David Howellsc69e8d92008-11-14 10:39:19 +11001684 const struct cred *cred;
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001685
1686 /* We need a new inode */
1687
1688 inode = new_inode(sb);
1689 if (!inode)
1690 goto out;
1691
1692 /* Common stuff */
1693 ei = PROC_I(inode);
Christoph Hellwig85fe4022010-10-23 11:19:54 -04001694 inode->i_ino = get_next_ino();
Deepa Dinamani078cd822016-09-14 07:48:04 -07001695 inode->i_mtime = inode->i_atime = inode->i_ctime = current_time(inode);
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001696 inode->i_op = &proc_def_inode_operations;
1697
1698 /*
1699 * grab the reference to task.
1700 */
Oleg Nesterov1a657f782006-10-02 02:18:59 -07001701 ei->pid = get_task_pid(task, PIDTYPE_PID);
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001702 if (!ei->pid)
1703 goto out_unlock;
1704
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001705 if (task_dumpable(task)) {
David Howellsc69e8d92008-11-14 10:39:19 +11001706 rcu_read_lock();
1707 cred = __task_cred(task);
1708 inode->i_uid = cred->euid;
1709 inode->i_gid = cred->egid;
1710 rcu_read_unlock();
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001711 }
1712 security_task_to_inode(task, inode);
1713
1714out:
1715 return inode;
1716
1717out_unlock:
1718 iput(inode);
1719 return NULL;
1720}
1721
Eric W. Biederman6b4e3062010-03-07 16:41:34 -08001722int pid_getattr(struct vfsmount *mnt, struct dentry *dentry, struct kstat *stat)
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001723{
David Howells2b0143b2015-03-17 22:25:59 +00001724 struct inode *inode = d_inode(dentry);
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001725 struct task_struct *task;
David Howellsc69e8d92008-11-14 10:39:19 +11001726 const struct cred *cred;
Vasiliy Kulikov04996802012-01-10 15:11:31 -08001727 struct pid_namespace *pid = dentry->d_sb->s_fs_info;
David Howellsc69e8d92008-11-14 10:39:19 +11001728
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001729 generic_fillattr(inode, stat);
1730
1731 rcu_read_lock();
Eric W. Biedermandcb0f222012-02-09 08:48:21 -08001732 stat->uid = GLOBAL_ROOT_UID;
1733 stat->gid = GLOBAL_ROOT_GID;
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001734 task = pid_task(proc_pid(inode), PIDTYPE_PID);
1735 if (task) {
Vasiliy Kulikov04996802012-01-10 15:11:31 -08001736 if (!has_pid_permissions(pid, task, 2)) {
1737 rcu_read_unlock();
1738 /*
1739 * This doesn't prevent learning whether PID exists,
1740 * it only makes getattr() consistent with readdir().
1741 */
1742 return -ENOENT;
1743 }
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001744 if ((inode->i_mode == (S_IFDIR|S_IRUGO|S_IXUGO)) ||
1745 task_dumpable(task)) {
David Howellsc69e8d92008-11-14 10:39:19 +11001746 cred = __task_cred(task);
1747 stat->uid = cred->euid;
1748 stat->gid = cred->egid;
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001749 }
1750 }
1751 rcu_read_unlock();
1752 return 0;
1753}
1754
1755/* dentry stuff */
1756
1757/*
1758 * Exceptional case: normally we are not allowed to unhash a busy
1759 * directory. In this case, however, we can do it - no aliasing problems
1760 * due to the way we treat inodes.
1761 *
1762 * Rewrite the inode's ownerships here because the owning task may have
1763 * performed a setuid(), etc.
1764 *
1765 * Before the /proc/pid/status file was created the only way to read
1766 * the effective uid of a /process was to stat /proc/pid. Reading
1767 * /proc/pid/status is slow enough that procps and other packages
1768 * kept stating /proc/pid. To keep the rules in /proc simple I have
1769 * made this apply to all per process world readable and executable
1770 * directories.
1771 */
Al Viro0b728e12012-06-10 16:03:43 -04001772int pid_revalidate(struct dentry *dentry, unsigned int flags)
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001773{
Nick Piggin34286d62011-01-07 17:49:57 +11001774 struct inode *inode;
1775 struct task_struct *task;
David Howellsc69e8d92008-11-14 10:39:19 +11001776 const struct cred *cred;
1777
Al Viro0b728e12012-06-10 16:03:43 -04001778 if (flags & LOOKUP_RCU)
Nick Piggin34286d62011-01-07 17:49:57 +11001779 return -ECHILD;
1780
David Howells2b0143b2015-03-17 22:25:59 +00001781 inode = d_inode(dentry);
Nick Piggin34286d62011-01-07 17:49:57 +11001782 task = get_proc_task(inode);
1783
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001784 if (task) {
1785 if ((inode->i_mode == (S_IFDIR|S_IRUGO|S_IXUGO)) ||
1786 task_dumpable(task)) {
David Howellsc69e8d92008-11-14 10:39:19 +11001787 rcu_read_lock();
1788 cred = __task_cred(task);
1789 inode->i_uid = cred->euid;
1790 inode->i_gid = cred->egid;
1791 rcu_read_unlock();
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001792 } else {
Eric W. Biedermandcb0f222012-02-09 08:48:21 -08001793 inode->i_uid = GLOBAL_ROOT_UID;
1794 inode->i_gid = GLOBAL_ROOT_GID;
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001795 }
1796 inode->i_mode &= ~(S_ISUID | S_ISGID);
1797 security_task_to_inode(task, inode);
1798 put_task_struct(task);
1799 return 1;
1800 }
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001801 return 0;
1802}
1803
Oleg Nesterovd855a4b2014-01-23 15:55:39 -08001804static inline bool proc_inode_is_dead(struct inode *inode)
1805{
1806 return !proc_pid(inode)->tasks[PIDTYPE_PID].first;
1807}
1808
David Howells1dd704b2013-04-12 01:08:50 +01001809int pid_delete_dentry(const struct dentry *dentry)
1810{
1811 /* Is the task we represent dead?
1812 * If so, then don't put the dentry on the lru list,
1813 * kill it immediately.
1814 */
David Howells2b0143b2015-03-17 22:25:59 +00001815 return proc_inode_is_dead(d_inode(dentry));
David Howells1dd704b2013-04-12 01:08:50 +01001816}
1817
Eric W. Biederman6b4e3062010-03-07 16:41:34 -08001818const struct dentry_operations pid_dentry_operations =
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001819{
1820 .d_revalidate = pid_revalidate,
1821 .d_delete = pid_delete_dentry,
1822};
1823
1824/* Lookups */
1825
Eric W. Biederman1c0d04c2006-10-02 02:18:57 -07001826/*
1827 * Fill a directory entry.
1828 *
1829 * If possible create the dcache entry and derive our inode number and
1830 * file type from dcache entry.
1831 *
1832 * Since all of the proc inode numbers are dynamically generated, the inode
1833 * numbers do not exist until the inode is cache. This means creating the
1834 * the dcache entry in readdir is necessary to keep the inode numbers
1835 * reported by readdir in sync with the inode numbers reported
1836 * by stat.
1837 */
Al Virof0c3b502013-05-16 12:07:31 -04001838bool proc_fill_cache(struct file *file, struct dir_context *ctx,
Eric W. Biederman6b4e3062010-03-07 16:41:34 -08001839 const char *name, int len,
Eric Dumazetc5141e62007-05-08 00:26:15 -07001840 instantiate_t instantiate, struct task_struct *task, const void *ptr)
Eric W. Biederman61a28782006-10-02 02:18:49 -07001841{
Al Virof0c3b502013-05-16 12:07:31 -04001842 struct dentry *child, *dir = file->f_path.dentry;
Al Viro1df98b82013-06-15 11:33:10 +04001843 struct qstr qname = QSTR_INIT(name, len);
Eric W. Biederman61a28782006-10-02 02:18:49 -07001844 struct inode *inode;
Al Viro1df98b82013-06-15 11:33:10 +04001845 unsigned type;
1846 ino_t ino;
Eric W. Biederman61a28782006-10-02 02:18:49 -07001847
Al Viro1df98b82013-06-15 11:33:10 +04001848 child = d_hash_and_lookup(dir, &qname);
Eric W. Biederman61a28782006-10-02 02:18:49 -07001849 if (!child) {
Al Viro37817642016-04-20 16:31:31 -04001850 DECLARE_WAIT_QUEUE_HEAD_ONSTACK(wq);
1851 child = d_alloc_parallel(dir, &qname, &wq);
1852 if (IS_ERR(child))
Al Viro1df98b82013-06-15 11:33:10 +04001853 goto end_instantiate;
Al Viro37817642016-04-20 16:31:31 -04001854 if (d_in_lookup(child)) {
1855 int err = instantiate(d_inode(dir), child, task, ptr);
1856 d_lookup_done(child);
1857 if (err < 0) {
1858 dput(child);
1859 goto end_instantiate;
1860 }
Eric W. Biederman61a28782006-10-02 02:18:49 -07001861 }
1862 }
David Howells2b0143b2015-03-17 22:25:59 +00001863 inode = d_inode(child);
Al Viro147ce692013-06-15 10:26:35 +04001864 ino = inode->i_ino;
1865 type = inode->i_mode >> 12;
Eric W. Biederman61a28782006-10-02 02:18:49 -07001866 dput(child);
Al Virof0c3b502013-05-16 12:07:31 -04001867 return dir_emit(ctx, name, len, ino, type);
Al Viro1df98b82013-06-15 11:33:10 +04001868
1869end_instantiate:
1870 return dir_emit(ctx, name, len, 1, DT_UNKNOWN);
Eric W. Biederman61a28782006-10-02 02:18:49 -07001871}
1872
Pavel Emelyanov640708a2012-01-10 15:11:23 -08001873/*
1874 * dname_to_vma_addr - maps a dentry name into two unsigned longs
1875 * which represent vma start and end addresses.
1876 */
1877static int dname_to_vma_addr(struct dentry *dentry,
1878 unsigned long *start, unsigned long *end)
1879{
Alexey Dobriyane0a1a012018-02-06 15:36:59 -08001880 const char *str = dentry->d_name.name;
1881 unsigned long long sval, eval;
1882 unsigned int len;
1883
1884 len = _parse_integer(str, 16, &sval);
1885 if (len & KSTRTOX_OVERFLOW)
Pavel Emelyanov640708a2012-01-10 15:11:23 -08001886 return -EINVAL;
Alexey Dobriyane0a1a012018-02-06 15:36:59 -08001887 if (sval != (unsigned long)sval)
1888 return -EINVAL;
1889 str += len;
1890
1891 if (*str != '-')
1892 return -EINVAL;
1893 str++;
1894
1895 len = _parse_integer(str, 16, &eval);
1896 if (len & KSTRTOX_OVERFLOW)
1897 return -EINVAL;
1898 if (eval != (unsigned long)eval)
1899 return -EINVAL;
1900 str += len;
1901
1902 if (*str != '\0')
1903 return -EINVAL;
1904
1905 *start = sval;
1906 *end = eval;
Pavel Emelyanov640708a2012-01-10 15:11:23 -08001907
1908 return 0;
1909}
1910
Al Viro0b728e12012-06-10 16:03:43 -04001911static int map_files_d_revalidate(struct dentry *dentry, unsigned int flags)
Pavel Emelyanov640708a2012-01-10 15:11:23 -08001912{
1913 unsigned long vm_start, vm_end;
1914 bool exact_vma_exists = false;
1915 struct mm_struct *mm = NULL;
1916 struct task_struct *task;
1917 const struct cred *cred;
1918 struct inode *inode;
1919 int status = 0;
1920
Al Viro0b728e12012-06-10 16:03:43 -04001921 if (flags & LOOKUP_RCU)
Pavel Emelyanov640708a2012-01-10 15:11:23 -08001922 return -ECHILD;
1923
David Howells2b0143b2015-03-17 22:25:59 +00001924 inode = d_inode(dentry);
Pavel Emelyanov640708a2012-01-10 15:11:23 -08001925 task = get_proc_task(inode);
1926 if (!task)
1927 goto out_notask;
1928
Jann Horncaaee622016-01-20 15:00:04 -08001929 mm = mm_access(task, PTRACE_MODE_READ_FSCREDS);
Cong Wang2344bec2012-05-31 16:26:18 -07001930 if (IS_ERR_OR_NULL(mm))
Pavel Emelyanov640708a2012-01-10 15:11:23 -08001931 goto out;
1932
1933 if (!dname_to_vma_addr(dentry, &vm_start, &vm_end)) {
1934 down_read(&mm->mmap_sem);
1935 exact_vma_exists = !!find_exact_vma(mm, vm_start, vm_end);
1936 up_read(&mm->mmap_sem);
1937 }
1938
1939 mmput(mm);
1940
1941 if (exact_vma_exists) {
1942 if (task_dumpable(task)) {
1943 rcu_read_lock();
1944 cred = __task_cred(task);
1945 inode->i_uid = cred->euid;
1946 inode->i_gid = cred->egid;
1947 rcu_read_unlock();
1948 } else {
Eric W. Biedermandcb0f222012-02-09 08:48:21 -08001949 inode->i_uid = GLOBAL_ROOT_UID;
1950 inode->i_gid = GLOBAL_ROOT_GID;
Pavel Emelyanov640708a2012-01-10 15:11:23 -08001951 }
1952 security_task_to_inode(task, inode);
1953 status = 1;
1954 }
1955
1956out:
1957 put_task_struct(task);
1958
1959out_notask:
Pavel Emelyanov640708a2012-01-10 15:11:23 -08001960 return status;
1961}
1962
1963static const struct dentry_operations tid_map_files_dentry_operations = {
1964 .d_revalidate = map_files_d_revalidate,
1965 .d_delete = pid_delete_dentry,
1966};
1967
Al Viro6b255392015-11-17 10:20:54 -05001968static int map_files_get_link(struct dentry *dentry, struct path *path)
Pavel Emelyanov640708a2012-01-10 15:11:23 -08001969{
1970 unsigned long vm_start, vm_end;
1971 struct vm_area_struct *vma;
1972 struct task_struct *task;
1973 struct mm_struct *mm;
1974 int rc;
1975
1976 rc = -ENOENT;
David Howells2b0143b2015-03-17 22:25:59 +00001977 task = get_proc_task(d_inode(dentry));
Pavel Emelyanov640708a2012-01-10 15:11:23 -08001978 if (!task)
1979 goto out;
1980
1981 mm = get_task_mm(task);
1982 put_task_struct(task);
1983 if (!mm)
1984 goto out;
1985
1986 rc = dname_to_vma_addr(dentry, &vm_start, &vm_end);
1987 if (rc)
1988 goto out_mmput;
1989
Artem Fetishev70335ab2014-03-10 15:49:45 -07001990 rc = -ENOENT;
Pavel Emelyanov640708a2012-01-10 15:11:23 -08001991 down_read(&mm->mmap_sem);
1992 vma = find_exact_vma(mm, vm_start, vm_end);
1993 if (vma && vma->vm_file) {
1994 *path = vma->vm_file->f_path;
1995 path_get(path);
1996 rc = 0;
1997 }
1998 up_read(&mm->mmap_sem);
1999
2000out_mmput:
2001 mmput(mm);
2002out:
2003 return rc;
2004}
2005
2006struct map_files_info {
Al Viro7b540d02012-08-27 14:55:26 -04002007 fmode_t mode;
Pavel Emelyanov640708a2012-01-10 15:11:23 -08002008 unsigned long len;
2009 unsigned char name[4*sizeof(long)+2]; /* max: %lx-%lx\0 */
2010};
2011
Calvin Owensbdb4d102015-09-09 15:35:54 -07002012/*
2013 * Only allow CAP_SYS_ADMIN to follow the links, due to concerns about how the
2014 * symlinks may be used to bypass permissions on ancestor directories in the
2015 * path to the file in question.
2016 */
2017static const char *
Al Viro6b255392015-11-17 10:20:54 -05002018proc_map_files_get_link(struct dentry *dentry,
Al Virofceef392015-12-29 15:58:39 -05002019 struct inode *inode,
2020 struct delayed_call *done)
Calvin Owensbdb4d102015-09-09 15:35:54 -07002021{
2022 if (!capable(CAP_SYS_ADMIN))
2023 return ERR_PTR(-EPERM);
2024
Al Virofceef392015-12-29 15:58:39 -05002025 return proc_pid_get_link(dentry, inode, done);
Calvin Owensbdb4d102015-09-09 15:35:54 -07002026}
2027
2028/*
Al Viro6b255392015-11-17 10:20:54 -05002029 * Identical to proc_pid_link_inode_operations except for get_link()
Calvin Owensbdb4d102015-09-09 15:35:54 -07002030 */
2031static const struct inode_operations proc_map_files_link_inode_operations = {
2032 .readlink = proc_pid_readlink,
Al Viro6b255392015-11-17 10:20:54 -05002033 .get_link = proc_map_files_get_link,
Calvin Owensbdb4d102015-09-09 15:35:54 -07002034 .setattr = proc_setattr,
2035};
2036
Al Viroc52a47a2013-06-15 11:15:20 +04002037static int
Pavel Emelyanov640708a2012-01-10 15:11:23 -08002038proc_map_files_instantiate(struct inode *dir, struct dentry *dentry,
2039 struct task_struct *task, const void *ptr)
2040{
Al Viro7b540d02012-08-27 14:55:26 -04002041 fmode_t mode = (fmode_t)(unsigned long)ptr;
Pavel Emelyanov640708a2012-01-10 15:11:23 -08002042 struct proc_inode *ei;
2043 struct inode *inode;
2044
Pavel Emelyanov640708a2012-01-10 15:11:23 -08002045 inode = proc_pid_make_inode(dir->i_sb, task);
2046 if (!inode)
Al Viroc52a47a2013-06-15 11:15:20 +04002047 return -ENOENT;
Pavel Emelyanov640708a2012-01-10 15:11:23 -08002048
2049 ei = PROC_I(inode);
Al Viro6b255392015-11-17 10:20:54 -05002050 ei->op.proc_get_link = map_files_get_link;
Pavel Emelyanov640708a2012-01-10 15:11:23 -08002051
Calvin Owensbdb4d102015-09-09 15:35:54 -07002052 inode->i_op = &proc_map_files_link_inode_operations;
Pavel Emelyanov640708a2012-01-10 15:11:23 -08002053 inode->i_size = 64;
2054 inode->i_mode = S_IFLNK;
2055
Al Viro7b540d02012-08-27 14:55:26 -04002056 if (mode & FMODE_READ)
Pavel Emelyanov640708a2012-01-10 15:11:23 -08002057 inode->i_mode |= S_IRUSR;
Al Viro7b540d02012-08-27 14:55:26 -04002058 if (mode & FMODE_WRITE)
Pavel Emelyanov640708a2012-01-10 15:11:23 -08002059 inode->i_mode |= S_IWUSR;
2060
2061 d_set_d_op(dentry, &tid_map_files_dentry_operations);
2062 d_add(dentry, inode);
2063
Al Viroc52a47a2013-06-15 11:15:20 +04002064 return 0;
Pavel Emelyanov640708a2012-01-10 15:11:23 -08002065}
2066
2067static struct dentry *proc_map_files_lookup(struct inode *dir,
Al Viro00cd8dd2012-06-10 17:13:09 -04002068 struct dentry *dentry, unsigned int flags)
Pavel Emelyanov640708a2012-01-10 15:11:23 -08002069{
2070 unsigned long vm_start, vm_end;
2071 struct vm_area_struct *vma;
2072 struct task_struct *task;
Al Viroc52a47a2013-06-15 11:15:20 +04002073 int result;
Pavel Emelyanov640708a2012-01-10 15:11:23 -08002074 struct mm_struct *mm;
2075
Al Viroc52a47a2013-06-15 11:15:20 +04002076 result = -ENOENT;
Pavel Emelyanov640708a2012-01-10 15:11:23 -08002077 task = get_proc_task(dir);
2078 if (!task)
2079 goto out;
2080
Al Viroc52a47a2013-06-15 11:15:20 +04002081 result = -EACCES;
Jann Horncaaee622016-01-20 15:00:04 -08002082 if (!ptrace_may_access(task, PTRACE_MODE_READ_FSCREDS))
Pavel Emelyanov640708a2012-01-10 15:11:23 -08002083 goto out_put_task;
2084
Al Viroc52a47a2013-06-15 11:15:20 +04002085 result = -ENOENT;
Pavel Emelyanov640708a2012-01-10 15:11:23 -08002086 if (dname_to_vma_addr(dentry, &vm_start, &vm_end))
Cyrill Gorcunoveb94cd92012-05-17 17:03:25 -07002087 goto out_put_task;
Pavel Emelyanov640708a2012-01-10 15:11:23 -08002088
2089 mm = get_task_mm(task);
2090 if (!mm)
Cyrill Gorcunoveb94cd92012-05-17 17:03:25 -07002091 goto out_put_task;
Pavel Emelyanov640708a2012-01-10 15:11:23 -08002092
2093 down_read(&mm->mmap_sem);
2094 vma = find_exact_vma(mm, vm_start, vm_end);
2095 if (!vma)
2096 goto out_no_vma;
2097
Stanislav Kinsbursky05f56482012-11-26 16:29:42 -08002098 if (vma->vm_file)
2099 result = proc_map_files_instantiate(dir, dentry, task,
2100 (void *)(unsigned long)vma->vm_file->f_mode);
Pavel Emelyanov640708a2012-01-10 15:11:23 -08002101
2102out_no_vma:
2103 up_read(&mm->mmap_sem);
2104 mmput(mm);
Pavel Emelyanov640708a2012-01-10 15:11:23 -08002105out_put_task:
2106 put_task_struct(task);
2107out:
Al Viroc52a47a2013-06-15 11:15:20 +04002108 return ERR_PTR(result);
Pavel Emelyanov640708a2012-01-10 15:11:23 -08002109}
2110
2111static const struct inode_operations proc_map_files_inode_operations = {
2112 .lookup = proc_map_files_lookup,
2113 .permission = proc_fd_permission,
2114 .setattr = proc_setattr,
2115};
2116
2117static int
Al Virof0c3b502013-05-16 12:07:31 -04002118proc_map_files_readdir(struct file *file, struct dir_context *ctx)
Pavel Emelyanov640708a2012-01-10 15:11:23 -08002119{
Pavel Emelyanov640708a2012-01-10 15:11:23 -08002120 struct vm_area_struct *vma;
2121 struct task_struct *task;
2122 struct mm_struct *mm;
Al Virof0c3b502013-05-16 12:07:31 -04002123 unsigned long nr_files, pos, i;
2124 struct flex_array *fa = NULL;
2125 struct map_files_info info;
2126 struct map_files_info *p;
Pavel Emelyanov640708a2012-01-10 15:11:23 -08002127 int ret;
2128
Pavel Emelyanov640708a2012-01-10 15:11:23 -08002129 ret = -ENOENT;
Al Virof0c3b502013-05-16 12:07:31 -04002130 task = get_proc_task(file_inode(file));
Pavel Emelyanov640708a2012-01-10 15:11:23 -08002131 if (!task)
2132 goto out;
2133
2134 ret = -EACCES;
Jann Horncaaee622016-01-20 15:00:04 -08002135 if (!ptrace_may_access(task, PTRACE_MODE_READ_FSCREDS))
Pavel Emelyanov640708a2012-01-10 15:11:23 -08002136 goto out_put_task;
2137
2138 ret = 0;
Al Virof0c3b502013-05-16 12:07:31 -04002139 if (!dir_emit_dots(file, ctx))
2140 goto out_put_task;
Pavel Emelyanov640708a2012-01-10 15:11:23 -08002141
Al Virof0c3b502013-05-16 12:07:31 -04002142 mm = get_task_mm(task);
2143 if (!mm)
2144 goto out_put_task;
2145 down_read(&mm->mmap_sem);
Pavel Emelyanov640708a2012-01-10 15:11:23 -08002146
Al Virof0c3b502013-05-16 12:07:31 -04002147 nr_files = 0;
Pavel Emelyanov640708a2012-01-10 15:11:23 -08002148
Al Virof0c3b502013-05-16 12:07:31 -04002149 /*
2150 * We need two passes here:
2151 *
2152 * 1) Collect vmas of mapped files with mmap_sem taken
2153 * 2) Release mmap_sem and instantiate entries
2154 *
2155 * otherwise we get lockdep complained, since filldir()
2156 * routine might require mmap_sem taken in might_fault().
2157 */
Pavel Emelyanov640708a2012-01-10 15:11:23 -08002158
Al Virof0c3b502013-05-16 12:07:31 -04002159 for (vma = mm->mmap, pos = 2; vma; vma = vma->vm_next) {
2160 if (vma->vm_file && ++pos > ctx->pos)
2161 nr_files++;
Pavel Emelyanov640708a2012-01-10 15:11:23 -08002162 }
Al Virof0c3b502013-05-16 12:07:31 -04002163
2164 if (nr_files) {
2165 fa = flex_array_alloc(sizeof(info), nr_files,
2166 GFP_KERNEL);
2167 if (!fa || flex_array_prealloc(fa, 0, nr_files,
2168 GFP_KERNEL)) {
2169 ret = -ENOMEM;
2170 if (fa)
2171 flex_array_free(fa);
2172 up_read(&mm->mmap_sem);
2173 mmput(mm);
2174 goto out_put_task;
2175 }
2176 for (i = 0, vma = mm->mmap, pos = 2; vma;
2177 vma = vma->vm_next) {
2178 if (!vma->vm_file)
2179 continue;
2180 if (++pos <= ctx->pos)
2181 continue;
2182
2183 info.mode = vma->vm_file->f_mode;
2184 info.len = snprintf(info.name,
2185 sizeof(info.name), "%lx-%lx",
2186 vma->vm_start, vma->vm_end);
2187 if (flex_array_put(fa, i++, &info, GFP_KERNEL))
2188 BUG();
2189 }
Pavel Emelyanov640708a2012-01-10 15:11:23 -08002190 }
Al Virof0c3b502013-05-16 12:07:31 -04002191 up_read(&mm->mmap_sem);
2192
2193 for (i = 0; i < nr_files; i++) {
2194 p = flex_array_get(fa, i);
2195 if (!proc_fill_cache(file, ctx,
2196 p->name, p->len,
2197 proc_map_files_instantiate,
2198 task,
2199 (void *)(unsigned long)p->mode))
2200 break;
2201 ctx->pos++;
2202 }
2203 if (fa)
2204 flex_array_free(fa);
2205 mmput(mm);
Pavel Emelyanov640708a2012-01-10 15:11:23 -08002206
Pavel Emelyanov640708a2012-01-10 15:11:23 -08002207out_put_task:
2208 put_task_struct(task);
2209out:
2210 return ret;
2211}
2212
2213static const struct file_operations proc_map_files_operations = {
2214 .read = generic_read_dir,
Al Virof50752e2016-04-20 17:13:54 -04002215 .iterate_shared = proc_map_files_readdir,
2216 .llseek = generic_file_llseek,
Pavel Emelyanov640708a2012-01-10 15:11:23 -08002217};
2218
Eric Engestromb5946be2016-03-17 14:20:57 -07002219#ifdef CONFIG_CHECKPOINT_RESTORE
Pavel Emelyanov48f6a7a2013-03-11 13:12:45 +04002220struct timers_private {
2221 struct pid *pid;
2222 struct task_struct *task;
2223 struct sighand_struct *sighand;
Pavel Emelyanov57b80152013-03-11 13:13:08 +04002224 struct pid_namespace *ns;
Pavel Emelyanov48f6a7a2013-03-11 13:12:45 +04002225 unsigned long flags;
2226};
2227
2228static void *timers_start(struct seq_file *m, loff_t *pos)
2229{
2230 struct timers_private *tp = m->private;
2231
2232 tp->task = get_pid_task(tp->pid, PIDTYPE_PID);
2233 if (!tp->task)
2234 return ERR_PTR(-ESRCH);
2235
2236 tp->sighand = lock_task_sighand(tp->task, &tp->flags);
2237 if (!tp->sighand)
2238 return ERR_PTR(-ESRCH);
2239
2240 return seq_list_start(&tp->task->signal->posix_timers, *pos);
2241}
2242
2243static void *timers_next(struct seq_file *m, void *v, loff_t *pos)
2244{
2245 struct timers_private *tp = m->private;
2246 return seq_list_next(v, &tp->task->signal->posix_timers, pos);
2247}
2248
2249static void timers_stop(struct seq_file *m, void *v)
2250{
2251 struct timers_private *tp = m->private;
2252
2253 if (tp->sighand) {
2254 unlock_task_sighand(tp->task, &tp->flags);
2255 tp->sighand = NULL;
2256 }
2257
2258 if (tp->task) {
2259 put_task_struct(tp->task);
2260 tp->task = NULL;
2261 }
2262}
2263
2264static int show_timer(struct seq_file *m, void *v)
2265{
2266 struct k_itimer *timer;
Pavel Emelyanov57b80152013-03-11 13:13:08 +04002267 struct timers_private *tp = m->private;
2268 int notify;
Alexey Dobriyancedbcca2014-08-08 14:21:33 -07002269 static const char * const nstr[] = {
Pavel Emelyanov57b80152013-03-11 13:13:08 +04002270 [SIGEV_SIGNAL] = "signal",
2271 [SIGEV_NONE] = "none",
2272 [SIGEV_THREAD] = "thread",
2273 };
Pavel Emelyanov48f6a7a2013-03-11 13:12:45 +04002274
2275 timer = list_entry((struct list_head *)v, struct k_itimer, list);
Pavel Emelyanov57b80152013-03-11 13:13:08 +04002276 notify = timer->it_sigev_notify;
2277
Pavel Emelyanov48f6a7a2013-03-11 13:12:45 +04002278 seq_printf(m, "ID: %d\n", timer->it_id);
Joe Perches25ce3192015-04-15 16:18:17 -07002279 seq_printf(m, "signal: %d/%p\n",
2280 timer->sigq->info.si_signo,
2281 timer->sigq->info.si_value.sival_ptr);
Pavel Emelyanov57b80152013-03-11 13:13:08 +04002282 seq_printf(m, "notify: %s/%s.%d\n",
Joe Perches25ce3192015-04-15 16:18:17 -07002283 nstr[notify & ~SIGEV_THREAD_ID],
2284 (notify & SIGEV_THREAD_ID) ? "tid" : "pid",
2285 pid_nr_ns(timer->it_pid, tp->ns));
Pavel Tikhomirov15ef0292013-05-17 02:12:03 +04002286 seq_printf(m, "ClockID: %d\n", timer->it_clock);
Pavel Emelyanov48f6a7a2013-03-11 13:12:45 +04002287
2288 return 0;
2289}
2290
2291static const struct seq_operations proc_timers_seq_ops = {
2292 .start = timers_start,
2293 .next = timers_next,
2294 .stop = timers_stop,
2295 .show = show_timer,
2296};
2297
2298static int proc_timers_open(struct inode *inode, struct file *file)
2299{
2300 struct timers_private *tp;
2301
2302 tp = __seq_open_private(file, &proc_timers_seq_ops,
2303 sizeof(struct timers_private));
2304 if (!tp)
2305 return -ENOMEM;
2306
2307 tp->pid = proc_pid(inode);
Pavel Emelyanov57b80152013-03-11 13:13:08 +04002308 tp->ns = inode->i_sb->s_fs_info;
Pavel Emelyanov48f6a7a2013-03-11 13:12:45 +04002309 return 0;
2310}
2311
2312static const struct file_operations proc_timers_operations = {
2313 .open = proc_timers_open,
2314 .read = seq_read,
2315 .llseek = seq_lseek,
2316 .release = seq_release_private,
2317};
Eric Engestromb5946be2016-03-17 14:20:57 -07002318#endif
Pavel Emelyanov640708a2012-01-10 15:11:23 -08002319
John Stultz5de23d42016-03-17 14:20:54 -07002320static ssize_t timerslack_ns_write(struct file *file, const char __user *buf,
2321 size_t count, loff_t *offset)
2322{
2323 struct inode *inode = file_inode(file);
2324 struct task_struct *p;
2325 u64 slack_ns;
2326 int err;
2327
2328 err = kstrtoull_from_user(buf, count, 10, &slack_ns);
2329 if (err < 0)
2330 return err;
2331
2332 p = get_proc_task(inode);
2333 if (!p)
2334 return -ESRCH;
2335
John Stultz4b2bd5f2016-10-07 17:02:33 -07002336 if (p != current) {
2337 if (!capable(CAP_SYS_NICE)) {
2338 count = -EPERM;
2339 goto out;
2340 }
John Stultz5de23d42016-03-17 14:20:54 -07002341
John Stultz4b2bd5f2016-10-07 17:02:33 -07002342 err = security_task_setscheduler(p);
2343 if (err) {
2344 count = err;
2345 goto out;
2346 }
John Stultz904763e2016-10-07 17:02:29 -07002347 }
2348
John Stultz7abbaf92016-10-07 17:02:26 -07002349 task_lock(p);
2350 if (slack_ns == 0)
2351 p->timer_slack_ns = p->default_timer_slack_ns;
2352 else
2353 p->timer_slack_ns = slack_ns;
2354 task_unlock(p);
2355
2356out:
John Stultz5de23d42016-03-17 14:20:54 -07002357 put_task_struct(p);
2358
2359 return count;
2360}
2361
2362static int timerslack_ns_show(struct seq_file *m, void *v)
2363{
2364 struct inode *inode = m->private;
2365 struct task_struct *p;
John Stultz7abbaf92016-10-07 17:02:26 -07002366 int err = 0;
John Stultz5de23d42016-03-17 14:20:54 -07002367
2368 p = get_proc_task(inode);
2369 if (!p)
2370 return -ESRCH;
2371
John Stultz4b2bd5f2016-10-07 17:02:33 -07002372 if (p != current) {
John Stultz5de23d42016-03-17 14:20:54 -07002373
John Stultz4b2bd5f2016-10-07 17:02:33 -07002374 if (!capable(CAP_SYS_NICE)) {
2375 err = -EPERM;
2376 goto out;
2377 }
2378 err = security_task_getscheduler(p);
2379 if (err)
2380 goto out;
2381 }
John Stultz904763e2016-10-07 17:02:29 -07002382
John Stultz7abbaf92016-10-07 17:02:26 -07002383 task_lock(p);
2384 seq_printf(m, "%llu\n", p->timer_slack_ns);
2385 task_unlock(p);
2386
2387out:
John Stultz5de23d42016-03-17 14:20:54 -07002388 put_task_struct(p);
2389
2390 return err;
2391}
2392
2393static int timerslack_ns_open(struct inode *inode, struct file *filp)
2394{
2395 return single_open(filp, timerslack_ns_show, inode);
2396}
2397
2398static const struct file_operations proc_pid_set_timerslack_ns_operations = {
2399 .open = timerslack_ns_open,
2400 .read = seq_read,
2401 .write = timerslack_ns_write,
2402 .llseek = seq_lseek,
2403 .release = single_release,
2404};
2405
Al Viroc52a47a2013-06-15 11:15:20 +04002406static int proc_pident_instantiate(struct inode *dir,
Eric Dumazetc5141e62007-05-08 00:26:15 -07002407 struct dentry *dentry, struct task_struct *task, const void *ptr)
Eric W. Biederman444ceed2006-10-02 02:18:49 -07002408{
Eric Dumazetc5141e62007-05-08 00:26:15 -07002409 const struct pid_entry *p = ptr;
Eric W. Biederman444ceed2006-10-02 02:18:49 -07002410 struct inode *inode;
2411 struct proc_inode *ei;
Eric W. Biederman444ceed2006-10-02 02:18:49 -07002412
Eric W. Biederman61a28782006-10-02 02:18:49 -07002413 inode = proc_pid_make_inode(dir->i_sb, task);
Eric W. Biederman444ceed2006-10-02 02:18:49 -07002414 if (!inode)
2415 goto out;
2416
2417 ei = PROC_I(inode);
2418 inode->i_mode = p->mode;
2419 if (S_ISDIR(inode->i_mode))
Miklos Szeredibfe86842011-10-28 14:13:29 +02002420 set_nlink(inode, 2); /* Use getattr to fix if necessary */
Eric W. Biederman444ceed2006-10-02 02:18:49 -07002421 if (p->iop)
2422 inode->i_op = p->iop;
2423 if (p->fop)
2424 inode->i_fop = p->fop;
2425 ei->op = p->op;
Nick Pigginfb045ad2011-01-07 17:49:55 +11002426 d_set_d_op(dentry, &pid_dentry_operations);
Eric W. Biederman444ceed2006-10-02 02:18:49 -07002427 d_add(dentry, inode);
2428 /* Close the race of the process dying before we return the dentry */
Al Viro0b728e12012-06-10 16:03:43 -04002429 if (pid_revalidate(dentry, 0))
Al Viroc52a47a2013-06-15 11:15:20 +04002430 return 0;
Eric W. Biederman444ceed2006-10-02 02:18:49 -07002431out:
Al Viroc52a47a2013-06-15 11:15:20 +04002432 return -ENOENT;
Eric W. Biederman444ceed2006-10-02 02:18:49 -07002433}
2434
Linus Torvalds1da177e2005-04-16 15:20:36 -07002435static struct dentry *proc_pident_lookup(struct inode *dir,
2436 struct dentry *dentry,
Eric Dumazetc5141e62007-05-08 00:26:15 -07002437 const struct pid_entry *ents,
Eric W. Biederman7bcd6b02006-10-02 02:18:56 -07002438 unsigned int nents)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002439{
Al Viroc52a47a2013-06-15 11:15:20 +04002440 int error;
Eric W. Biederman99f89552006-06-26 00:25:55 -07002441 struct task_struct *task = get_proc_task(dir);
Eric Dumazetc5141e62007-05-08 00:26:15 -07002442 const struct pid_entry *p, *last;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002443
Al Viroc52a47a2013-06-15 11:15:20 +04002444 error = -ENOENT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002445
Eric W. Biederman99f89552006-06-26 00:25:55 -07002446 if (!task)
2447 goto out_no_task;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002448
Eric W. Biederman20cdc892006-10-02 02:17:07 -07002449 /*
2450 * Yes, it does not scale. And it should not. Don't add
2451 * new entries into /proc/<tgid>/ without very good reasons.
2452 */
Eric W. Biederman7bcd6b02006-10-02 02:18:56 -07002453 last = &ents[nents - 1];
2454 for (p = ents; p <= last; p++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002455 if (p->len != dentry->d_name.len)
2456 continue;
2457 if (!memcmp(dentry->d_name.name, p->name, p->len))
2458 break;
2459 }
Eric W. Biederman7bcd6b02006-10-02 02:18:56 -07002460 if (p > last)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002461 goto out;
2462
Eric W. Biederman444ceed2006-10-02 02:18:49 -07002463 error = proc_pident_instantiate(dir, dentry, task, p);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002464out:
Eric W. Biederman99f89552006-06-26 00:25:55 -07002465 put_task_struct(task);
2466out_no_task:
Al Viroc52a47a2013-06-15 11:15:20 +04002467 return ERR_PTR(error);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002468}
2469
Al Virof0c3b502013-05-16 12:07:31 -04002470static int proc_pident_readdir(struct file *file, struct dir_context *ctx,
Eric Dumazetc5141e62007-05-08 00:26:15 -07002471 const struct pid_entry *ents, unsigned int nents)
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002472{
Al Virof0c3b502013-05-16 12:07:31 -04002473 struct task_struct *task = get_proc_task(file_inode(file));
2474 const struct pid_entry *p;
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002475
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002476 if (!task)
Al Virof0c3b502013-05-16 12:07:31 -04002477 return -ENOENT;
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002478
Al Virof0c3b502013-05-16 12:07:31 -04002479 if (!dir_emit_dots(file, ctx))
2480 goto out;
2481
2482 if (ctx->pos >= nents + 2)
2483 goto out;
2484
2485 for (p = ents + (ctx->pos - 2); p <= ents + nents - 1; p++) {
2486 if (!proc_fill_cache(file, ctx, p->name, p->len,
2487 proc_pident_instantiate, task, p))
2488 break;
2489 ctx->pos++;
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002490 }
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002491out:
Eric W. Biederman61a28782006-10-02 02:18:49 -07002492 put_task_struct(task);
Al Virof0c3b502013-05-16 12:07:31 -04002493 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002494}
2495
Linus Torvalds1da177e2005-04-16 15:20:36 -07002496#ifdef CONFIG_SECURITY
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002497static ssize_t proc_pid_attr_read(struct file * file, char __user * buf,
2498 size_t count, loff_t *ppos)
2499{
Al Viro496ad9a2013-01-23 17:07:38 -05002500 struct inode * inode = file_inode(file);
Al Viro04ff9702007-03-12 16:17:58 +00002501 char *p = NULL;
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002502 ssize_t length;
2503 struct task_struct *task = get_proc_task(inode);
2504
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002505 if (!task)
Al Viro04ff9702007-03-12 16:17:58 +00002506 return -ESRCH;
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002507
2508 length = security_getprocattr(task,
Josef "Jeff" Sipek2fddfee2006-12-08 02:36:36 -08002509 (char*)file->f_path.dentry->d_name.name,
Al Viro04ff9702007-03-12 16:17:58 +00002510 &p);
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002511 put_task_struct(task);
Al Viro04ff9702007-03-12 16:17:58 +00002512 if (length > 0)
2513 length = simple_read_from_buffer(buf, count, ppos, p, length);
2514 kfree(p);
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002515 return length;
2516}
2517
2518static ssize_t proc_pid_attr_write(struct file * file, const char __user * buf,
2519 size_t count, loff_t *ppos)
2520{
Al Viro496ad9a2013-01-23 17:07:38 -05002521 struct inode * inode = file_inode(file);
Al Virobb646cd2015-12-24 00:16:30 -05002522 void *page;
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002523 ssize_t length;
2524 struct task_struct *task = get_proc_task(inode);
2525
2526 length = -ESRCH;
2527 if (!task)
2528 goto out_no_task;
2529 if (count > PAGE_SIZE)
2530 count = PAGE_SIZE;
2531
2532 /* No partial writes. */
2533 length = -EINVAL;
2534 if (*ppos != 0)
2535 goto out;
2536
Al Virobb646cd2015-12-24 00:16:30 -05002537 page = memdup_user(buf, count);
2538 if (IS_ERR(page)) {
2539 length = PTR_ERR(page);
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002540 goto out;
Al Virobb646cd2015-12-24 00:16:30 -05002541 }
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002542
David Howells107db7c2009-05-08 13:55:27 +01002543 /* Guard against adverse ptrace interaction */
KOSAKI Motohiro9b1bf122010-10-27 15:34:08 -07002544 length = mutex_lock_interruptible(&task->signal->cred_guard_mutex);
David Howells107db7c2009-05-08 13:55:27 +01002545 if (length < 0)
2546 goto out_free;
2547
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002548 length = security_setprocattr(task,
Josef "Jeff" Sipek2fddfee2006-12-08 02:36:36 -08002549 (char*)file->f_path.dentry->d_name.name,
Al Virobb646cd2015-12-24 00:16:30 -05002550 page, count);
KOSAKI Motohiro9b1bf122010-10-27 15:34:08 -07002551 mutex_unlock(&task->signal->cred_guard_mutex);
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002552out_free:
Al Virobb646cd2015-12-24 00:16:30 -05002553 kfree(page);
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002554out:
2555 put_task_struct(task);
2556out_no_task:
2557 return length;
2558}
2559
Arjan van de Ven00977a52007-02-12 00:55:34 -08002560static const struct file_operations proc_pid_attr_operations = {
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002561 .read = proc_pid_attr_read,
2562 .write = proc_pid_attr_write,
Arnd Bergmann87df8422010-03-17 23:06:02 +01002563 .llseek = generic_file_llseek,
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002564};
2565
Eric Dumazetc5141e62007-05-08 00:26:15 -07002566static const struct pid_entry attr_dir_stuff[] = {
Alexey Dobriyan631f9c12008-11-10 01:32:52 +03002567 REG("current", S_IRUGO|S_IWUGO, proc_pid_attr_operations),
2568 REG("prev", S_IRUGO, proc_pid_attr_operations),
2569 REG("exec", S_IRUGO|S_IWUGO, proc_pid_attr_operations),
2570 REG("fscreate", S_IRUGO|S_IWUGO, proc_pid_attr_operations),
2571 REG("keycreate", S_IRUGO|S_IWUGO, proc_pid_attr_operations),
2572 REG("sockcreate", S_IRUGO|S_IWUGO, proc_pid_attr_operations),
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002573};
2574
Al Virof0c3b502013-05-16 12:07:31 -04002575static int proc_attr_dir_readdir(struct file *file, struct dir_context *ctx)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002576{
Al Virof0c3b502013-05-16 12:07:31 -04002577 return proc_pident_readdir(file, ctx,
2578 attr_dir_stuff, ARRAY_SIZE(attr_dir_stuff));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002579}
2580
Arjan van de Ven00977a52007-02-12 00:55:34 -08002581static const struct file_operations proc_attr_dir_operations = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002582 .read = generic_read_dir,
Al Virof50752e2016-04-20 17:13:54 -04002583 .iterate_shared = proc_attr_dir_readdir,
2584 .llseek = generic_file_llseek,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002585};
2586
Eric W. Biederman72d9dcf2006-10-02 02:18:50 -07002587static struct dentry *proc_attr_dir_lookup(struct inode *dir,
Al Viro00cd8dd2012-06-10 17:13:09 -04002588 struct dentry *dentry, unsigned int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002589{
Eric W. Biederman7bcd6b02006-10-02 02:18:56 -07002590 return proc_pident_lookup(dir, dentry,
2591 attr_dir_stuff, ARRAY_SIZE(attr_dir_stuff));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002592}
2593
Arjan van de Venc5ef1c42007-02-12 00:55:40 -08002594static const struct inode_operations proc_attr_dir_inode_operations = {
Eric W. Biederman72d9dcf2006-10-02 02:18:50 -07002595 .lookup = proc_attr_dir_lookup,
Eric W. Biederman99f89552006-06-26 00:25:55 -07002596 .getattr = pid_getattr,
Linus Torvalds6d76fa52006-07-15 12:26:45 -07002597 .setattr = proc_setattr,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002598};
2599
Linus Torvalds1da177e2005-04-16 15:20:36 -07002600#endif
2601
Christoph Hellwig698ba7b2009-12-15 16:47:37 -08002602#ifdef CONFIG_ELF_CORE
Kawai, Hidehiro3cb4a0b2007-07-19 01:48:28 -07002603static ssize_t proc_coredump_filter_read(struct file *file, char __user *buf,
2604 size_t count, loff_t *ppos)
2605{
Al Viro496ad9a2013-01-23 17:07:38 -05002606 struct task_struct *task = get_proc_task(file_inode(file));
Kawai, Hidehiro3cb4a0b2007-07-19 01:48:28 -07002607 struct mm_struct *mm;
2608 char buffer[PROC_NUMBUF];
2609 size_t len;
2610 int ret;
2611
2612 if (!task)
2613 return -ESRCH;
2614
2615 ret = 0;
2616 mm = get_task_mm(task);
2617 if (mm) {
2618 len = snprintf(buffer, sizeof(buffer), "%08lx\n",
2619 ((mm->flags & MMF_DUMP_FILTER_MASK) >>
2620 MMF_DUMP_FILTER_SHIFT));
2621 mmput(mm);
2622 ret = simple_read_from_buffer(buf, count, ppos, buffer, len);
2623 }
2624
2625 put_task_struct(task);
2626
2627 return ret;
2628}
2629
2630static ssize_t proc_coredump_filter_write(struct file *file,
2631 const char __user *buf,
2632 size_t count,
2633 loff_t *ppos)
2634{
2635 struct task_struct *task;
2636 struct mm_struct *mm;
Kawai, Hidehiro3cb4a0b2007-07-19 01:48:28 -07002637 unsigned int val;
2638 int ret;
2639 int i;
2640 unsigned long mask;
2641
Alexey Dobriyan774636e2015-09-09 15:36:59 -07002642 ret = kstrtouint_from_user(buf, count, 0, &val);
2643 if (ret < 0)
2644 return ret;
Kawai, Hidehiro3cb4a0b2007-07-19 01:48:28 -07002645
2646 ret = -ESRCH;
Al Viro496ad9a2013-01-23 17:07:38 -05002647 task = get_proc_task(file_inode(file));
Kawai, Hidehiro3cb4a0b2007-07-19 01:48:28 -07002648 if (!task)
2649 goto out_no_task;
2650
Kawai, Hidehiro3cb4a0b2007-07-19 01:48:28 -07002651 mm = get_task_mm(task);
2652 if (!mm)
2653 goto out_no_mm;
Colin Ian King41a0c2492015-12-18 14:22:01 -08002654 ret = 0;
Kawai, Hidehiro3cb4a0b2007-07-19 01:48:28 -07002655
2656 for (i = 0, mask = 1; i < MMF_DUMP_FILTER_BITS; i++, mask <<= 1) {
2657 if (val & mask)
2658 set_bit(i + MMF_DUMP_FILTER_SHIFT, &mm->flags);
2659 else
2660 clear_bit(i + MMF_DUMP_FILTER_SHIFT, &mm->flags);
2661 }
2662
2663 mmput(mm);
2664 out_no_mm:
2665 put_task_struct(task);
2666 out_no_task:
Alexey Dobriyan774636e2015-09-09 15:36:59 -07002667 if (ret < 0)
2668 return ret;
2669 return count;
Kawai, Hidehiro3cb4a0b2007-07-19 01:48:28 -07002670}
2671
2672static const struct file_operations proc_coredump_filter_operations = {
2673 .read = proc_coredump_filter_read,
2674 .write = proc_coredump_filter_write,
Arnd Bergmann87df8422010-03-17 23:06:02 +01002675 .llseek = generic_file_llseek,
Kawai, Hidehiro3cb4a0b2007-07-19 01:48:28 -07002676};
2677#endif
2678
Andrew Mortonaba76fd2006-12-10 02:19:48 -08002679#ifdef CONFIG_TASK_IO_ACCOUNTING
Alexey Dobriyan19aadc92014-08-08 14:21:50 -07002680static int do_io_accounting(struct task_struct *task, struct seq_file *m, int whole)
Andrew Mortonaba76fd2006-12-10 02:19:48 -08002681{
Andrea Righi940389b2008-07-28 00:48:12 +02002682 struct task_io_accounting acct = task->ioac;
Andrea Righi59954772008-07-27 17:29:15 +02002683 unsigned long flags;
Vasiliy Kulikov293eb1e2011-07-26 16:08:38 -07002684 int result;
Andrea Righi297c5d92008-07-25 01:48:49 -07002685
Vasiliy Kulikov293eb1e2011-07-26 16:08:38 -07002686 result = mutex_lock_killable(&task->signal->cred_guard_mutex);
2687 if (result)
2688 return result;
2689
Jann Horncaaee622016-01-20 15:00:04 -08002690 if (!ptrace_may_access(task, PTRACE_MODE_READ_FSCREDS)) {
Vasiliy Kulikov293eb1e2011-07-26 16:08:38 -07002691 result = -EACCES;
2692 goto out_unlock;
2693 }
Vasiliy Kulikov1d1221f2011-06-24 16:08:38 +04002694
Andrea Righi59954772008-07-27 17:29:15 +02002695 if (whole && lock_task_sighand(task, &flags)) {
2696 struct task_struct *t = task;
Andrea Righib2d002d2008-07-26 15:22:27 -07002697
Andrea Righi59954772008-07-27 17:29:15 +02002698 task_io_accounting_add(&acct, &task->signal->ioac);
2699 while_each_thread(task, t)
2700 task_io_accounting_add(&acct, &t->ioac);
Andrea Righi297c5d92008-07-25 01:48:49 -07002701
Andrea Righi59954772008-07-27 17:29:15 +02002702 unlock_task_sighand(task, &flags);
Andrea Righi297c5d92008-07-25 01:48:49 -07002703 }
Joe Perches25ce3192015-04-15 16:18:17 -07002704 seq_printf(m,
2705 "rchar: %llu\n"
2706 "wchar: %llu\n"
2707 "syscr: %llu\n"
2708 "syscw: %llu\n"
2709 "read_bytes: %llu\n"
2710 "write_bytes: %llu\n"
2711 "cancelled_write_bytes: %llu\n",
2712 (unsigned long long)acct.rchar,
2713 (unsigned long long)acct.wchar,
2714 (unsigned long long)acct.syscr,
2715 (unsigned long long)acct.syscw,
2716 (unsigned long long)acct.read_bytes,
2717 (unsigned long long)acct.write_bytes,
2718 (unsigned long long)acct.cancelled_write_bytes);
2719 result = 0;
2720
Vasiliy Kulikov293eb1e2011-07-26 16:08:38 -07002721out_unlock:
2722 mutex_unlock(&task->signal->cred_guard_mutex);
2723 return result;
Andrew Mortonaba76fd2006-12-10 02:19:48 -08002724}
Andrea Righi297c5d92008-07-25 01:48:49 -07002725
Alexey Dobriyan19aadc92014-08-08 14:21:50 -07002726static int proc_tid_io_accounting(struct seq_file *m, struct pid_namespace *ns,
2727 struct pid *pid, struct task_struct *task)
Andrea Righi297c5d92008-07-25 01:48:49 -07002728{
Alexey Dobriyan19aadc92014-08-08 14:21:50 -07002729 return do_io_accounting(task, m, 0);
Andrea Righi297c5d92008-07-25 01:48:49 -07002730}
2731
Alexey Dobriyan19aadc92014-08-08 14:21:50 -07002732static int proc_tgid_io_accounting(struct seq_file *m, struct pid_namespace *ns,
2733 struct pid *pid, struct task_struct *task)
Andrea Righi297c5d92008-07-25 01:48:49 -07002734{
Alexey Dobriyan19aadc92014-08-08 14:21:50 -07002735 return do_io_accounting(task, m, 1);
Andrea Righi297c5d92008-07-25 01:48:49 -07002736}
2737#endif /* CONFIG_TASK_IO_ACCOUNTING */
Andrew Mortonaba76fd2006-12-10 02:19:48 -08002738
Eric W. Biederman22d917d2011-11-17 00:11:58 -08002739#ifdef CONFIG_USER_NS
2740static int proc_id_map_open(struct inode *inode, struct file *file,
Fabian Frederickccf94f12014-08-08 14:21:22 -07002741 const struct seq_operations *seq_ops)
Eric W. Biederman22d917d2011-11-17 00:11:58 -08002742{
2743 struct user_namespace *ns = NULL;
2744 struct task_struct *task;
2745 struct seq_file *seq;
2746 int ret = -EINVAL;
2747
2748 task = get_proc_task(inode);
2749 if (task) {
2750 rcu_read_lock();
2751 ns = get_user_ns(task_cred_xxx(task, user_ns));
2752 rcu_read_unlock();
2753 put_task_struct(task);
2754 }
2755 if (!ns)
2756 goto err;
2757
2758 ret = seq_open(file, seq_ops);
2759 if (ret)
2760 goto err_put_ns;
2761
2762 seq = file->private_data;
2763 seq->private = ns;
2764
2765 return 0;
2766err_put_ns:
2767 put_user_ns(ns);
2768err:
2769 return ret;
2770}
2771
2772static int proc_id_map_release(struct inode *inode, struct file *file)
2773{
2774 struct seq_file *seq = file->private_data;
2775 struct user_namespace *ns = seq->private;
2776 put_user_ns(ns);
2777 return seq_release(inode, file);
2778}
2779
2780static int proc_uid_map_open(struct inode *inode, struct file *file)
2781{
2782 return proc_id_map_open(inode, file, &proc_uid_seq_operations);
2783}
2784
2785static int proc_gid_map_open(struct inode *inode, struct file *file)
2786{
2787 return proc_id_map_open(inode, file, &proc_gid_seq_operations);
2788}
2789
Eric W. Biedermanf76d2072012-08-30 01:24:05 -07002790static int proc_projid_map_open(struct inode *inode, struct file *file)
2791{
2792 return proc_id_map_open(inode, file, &proc_projid_seq_operations);
2793}
2794
Eric W. Biederman22d917d2011-11-17 00:11:58 -08002795static const struct file_operations proc_uid_map_operations = {
2796 .open = proc_uid_map_open,
2797 .write = proc_uid_map_write,
2798 .read = seq_read,
2799 .llseek = seq_lseek,
2800 .release = proc_id_map_release,
2801};
2802
2803static const struct file_operations proc_gid_map_operations = {
2804 .open = proc_gid_map_open,
2805 .write = proc_gid_map_write,
2806 .read = seq_read,
2807 .llseek = seq_lseek,
2808 .release = proc_id_map_release,
2809};
Eric W. Biedermanf76d2072012-08-30 01:24:05 -07002810
2811static const struct file_operations proc_projid_map_operations = {
2812 .open = proc_projid_map_open,
2813 .write = proc_projid_map_write,
2814 .read = seq_read,
2815 .llseek = seq_lseek,
2816 .release = proc_id_map_release,
2817};
Eric W. Biederman9cc46512014-12-02 12:27:26 -06002818
2819static int proc_setgroups_open(struct inode *inode, struct file *file)
2820{
2821 struct user_namespace *ns = NULL;
2822 struct task_struct *task;
2823 int ret;
2824
2825 ret = -ESRCH;
2826 task = get_proc_task(inode);
2827 if (task) {
2828 rcu_read_lock();
2829 ns = get_user_ns(task_cred_xxx(task, user_ns));
2830 rcu_read_unlock();
2831 put_task_struct(task);
2832 }
2833 if (!ns)
2834 goto err;
2835
2836 if (file->f_mode & FMODE_WRITE) {
2837 ret = -EACCES;
2838 if (!ns_capable(ns, CAP_SYS_ADMIN))
2839 goto err_put_ns;
2840 }
2841
2842 ret = single_open(file, &proc_setgroups_show, ns);
2843 if (ret)
2844 goto err_put_ns;
2845
2846 return 0;
2847err_put_ns:
2848 put_user_ns(ns);
2849err:
2850 return ret;
2851}
2852
2853static int proc_setgroups_release(struct inode *inode, struct file *file)
2854{
2855 struct seq_file *seq = file->private_data;
2856 struct user_namespace *ns = seq->private;
2857 int ret = single_release(inode, file);
2858 put_user_ns(ns);
2859 return ret;
2860}
2861
2862static const struct file_operations proc_setgroups_operations = {
2863 .open = proc_setgroups_open,
2864 .write = proc_setgroups_write,
2865 .read = seq_read,
2866 .llseek = seq_lseek,
2867 .release = proc_setgroups_release,
2868};
Eric W. Biederman22d917d2011-11-17 00:11:58 -08002869#endif /* CONFIG_USER_NS */
2870
Kees Cook47830722008-10-06 03:11:58 +04002871static int proc_pid_personality(struct seq_file *m, struct pid_namespace *ns,
2872 struct pid *pid, struct task_struct *task)
2873{
Al Viroa9712bc2011-03-23 15:52:50 -04002874 int err = lock_trace(task);
2875 if (!err) {
2876 seq_printf(m, "%08x\n", task->personality);
2877 unlock_trace(task);
2878 }
2879 return err;
Kees Cook47830722008-10-06 03:11:58 +04002880}
2881
Eric W. Biederman801199c2006-10-02 02:18:48 -07002882/*
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002883 * Thread groups
2884 */
Arjan van de Ven00977a52007-02-12 00:55:34 -08002885static const struct file_operations proc_task_operations;
Arjan van de Venc5ef1c42007-02-12 00:55:40 -08002886static const struct inode_operations proc_task_inode_operations;
Eric W. Biederman20cdc892006-10-02 02:17:07 -07002887
Eric Dumazetc5141e62007-05-08 00:26:15 -07002888static const struct pid_entry tgid_base_stuff[] = {
Alexey Dobriyan631f9c12008-11-10 01:32:52 +03002889 DIR("task", S_IRUGO|S_IXUGO, proc_task_inode_operations, proc_task_operations),
2890 DIR("fd", S_IRUSR|S_IXUSR, proc_fd_inode_operations, proc_fd_operations),
Pavel Emelyanov640708a2012-01-10 15:11:23 -08002891 DIR("map_files", S_IRUSR|S_IXUSR, proc_map_files_inode_operations, proc_map_files_operations),
Alexey Dobriyan631f9c12008-11-10 01:32:52 +03002892 DIR("fdinfo", S_IRUSR|S_IXUSR, proc_fdinfo_inode_operations, proc_fdinfo_operations),
Eric W. Biederman6b4e3062010-03-07 16:41:34 -08002893 DIR("ns", S_IRUSR|S_IXUGO, proc_ns_dir_inode_operations, proc_ns_dir_operations),
Andrew Mortonb2211a32008-03-11 18:03:35 -07002894#ifdef CONFIG_NET
Alexey Dobriyan631f9c12008-11-10 01:32:52 +03002895 DIR("net", S_IRUGO|S_IXUGO, proc_net_inode_operations, proc_net_operations),
Andrew Mortonb2211a32008-03-11 18:03:35 -07002896#endif
Alexey Dobriyan631f9c12008-11-10 01:32:52 +03002897 REG("environ", S_IRUSR, proc_environ_operations),
Al Viroc5317162016-10-05 18:43:43 -04002898 REG("auxv", S_IRUSR, proc_auxv_operations),
Alexey Dobriyan631f9c12008-11-10 01:32:52 +03002899 ONE("status", S_IRUGO, proc_pid_status),
Djalal Harouni35a35042014-04-07 15:38:36 -07002900 ONE("personality", S_IRUSR, proc_pid_personality),
Alexey Dobriyan1c963eb2014-08-08 14:21:37 -07002901 ONE("limits", S_IRUGO, proc_pid_limits),
Ingo Molnar43ae34c2007-07-09 18:52:00 +02002902#ifdef CONFIG_SCHED_DEBUG
Alexey Dobriyan631f9c12008-11-10 01:32:52 +03002903 REG("sched", S_IRUGO|S_IWUSR, proc_pid_sched_operations),
Ingo Molnar43ae34c2007-07-09 18:52:00 +02002904#endif
Mike Galbraith5091faa2010-11-30 14:18:03 +01002905#ifdef CONFIG_SCHED_AUTOGROUP
2906 REG("autogroup", S_IRUGO|S_IWUSR, proc_pid_sched_autogroup_operations),
2907#endif
john stultz4614a696b2009-12-14 18:00:05 -08002908 REG("comm", S_IRUGO|S_IWUSR, proc_pid_set_comm_operations),
Roland McGrathebcb6732008-07-25 19:46:00 -07002909#ifdef CONFIG_HAVE_ARCH_TRACEHOOK
Alexey Dobriyan09d93bd2014-08-08 14:21:39 -07002910 ONE("syscall", S_IRUSR, proc_pid_syscall),
Roland McGrathebcb6732008-07-25 19:46:00 -07002911#endif
Alexey Dobriyanc2c0bb42015-06-25 15:00:54 -07002912 REG("cmdline", S_IRUGO, proc_pid_cmdline_ops),
Alexey Dobriyan631f9c12008-11-10 01:32:52 +03002913 ONE("stat", S_IRUGO, proc_tgid_stat),
2914 ONE("statm", S_IRUGO, proc_pid_statm),
Siddhesh Poyarekarb7643752012-03-21 16:34:04 -07002915 REG("maps", S_IRUGO, proc_pid_maps_operations),
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002916#ifdef CONFIG_NUMA
Siddhesh Poyarekarb7643752012-03-21 16:34:04 -07002917 REG("numa_maps", S_IRUGO, proc_pid_numa_maps_operations),
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002918#endif
Alexey Dobriyan631f9c12008-11-10 01:32:52 +03002919 REG("mem", S_IRUSR|S_IWUSR, proc_mem_operations),
2920 LNK("cwd", proc_cwd_link),
2921 LNK("root", proc_root_link),
2922 LNK("exe", proc_exe_link),
2923 REG("mounts", S_IRUGO, proc_mounts_operations),
2924 REG("mountinfo", S_IRUGO, proc_mountinfo_operations),
2925 REG("mountstats", S_IRUSR, proc_mountstats_operations),
Matt Mackall1e883282008-02-04 22:29:07 -08002926#ifdef CONFIG_PROC_PAGE_MONITOR
Alexey Dobriyan631f9c12008-11-10 01:32:52 +03002927 REG("clear_refs", S_IWUSR, proc_clear_refs_operations),
Siddhesh Poyarekarb7643752012-03-21 16:34:04 -07002928 REG("smaps", S_IRUGO, proc_pid_smaps_operations),
Daniel Colascione66689622017-09-06 16:25:08 -07002929 REG("smaps_rollup", S_IRUGO, proc_pid_smaps_rollup_operations),
Djalal Harouni32ed74a2014-04-07 15:38:38 -07002930 REG("pagemap", S_IRUSR, proc_pagemap_operations),
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002931#endif
2932#ifdef CONFIG_SECURITY
Alexey Dobriyan631f9c12008-11-10 01:32:52 +03002933 DIR("attr", S_IRUGO|S_IXUGO, proc_attr_dir_inode_operations, proc_attr_dir_operations),
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002934#endif
2935#ifdef CONFIG_KALLSYMS
Alexey Dobriyanedfcd602014-08-08 14:21:44 -07002936 ONE("wchan", S_IRUGO, proc_pid_wchan),
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002937#endif
Ken Chen2ec220e2008-11-10 11:26:08 +03002938#ifdef CONFIG_STACKTRACE
Djalal Harouni35a35042014-04-07 15:38:36 -07002939 ONE("stack", S_IRUSR, proc_pid_stack),
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002940#endif
Naveen N. Rao5968cec2015-06-30 14:36:03 +05302941#ifdef CONFIG_SCHED_INFO
Alexey Dobriyanf6e826c2014-08-08 14:21:46 -07002942 ONE("schedstat", S_IRUGO, proc_pid_schedstat),
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002943#endif
Arjan van de Ven97455122008-01-25 21:08:34 +01002944#ifdef CONFIG_LATENCYTOP
Alexey Dobriyan631f9c12008-11-10 01:32:52 +03002945 REG("latency", S_IRUGO, proc_lstats_operations),
Arjan van de Ven97455122008-01-25 21:08:34 +01002946#endif
Paul Menage8793d852007-10-18 23:39:39 -07002947#ifdef CONFIG_PROC_PID_CPUSET
Zefan Li52de4772014-09-18 16:03:36 +08002948 ONE("cpuset", S_IRUGO, proc_cpuset_show),
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002949#endif
Paul Menagea4243162007-10-18 23:39:35 -07002950#ifdef CONFIG_CGROUPS
Zefan Li006f4ac2014-09-18 16:03:15 +08002951 ONE("cgroup", S_IRUGO, proc_cgroup_show),
Paul Menagea4243162007-10-18 23:39:35 -07002952#endif
Alexey Dobriyan6ba51e32014-08-08 14:21:48 -07002953 ONE("oom_score", S_IRUGO, proc_oom_score),
Greg Hackmann3a6594d2017-11-07 13:21:20 -08002954 REG("oom_adj", S_IRUGO|S_IWUSR, proc_oom_adj_operations),
2955 REG("oom_score_adj", S_IRUGO|S_IWUSR, proc_oom_score_adj_operations),
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002956#ifdef CONFIG_AUDITSYSCALL
Alexey Dobriyan631f9c12008-11-10 01:32:52 +03002957 REG("loginuid", S_IWUSR|S_IRUGO, proc_loginuid_operations),
2958 REG("sessionid", S_IRUGO, proc_sessionid_operations),
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002959#endif
Akinobu Mitaf4f154f2006-12-08 02:39:47 -08002960#ifdef CONFIG_FAULT_INJECTION
Alexey Dobriyan631f9c12008-11-10 01:32:52 +03002961 REG("make-it-fail", S_IRUGO|S_IWUSR, proc_fault_inject_operations),
Akinobu Mitaf4f154f2006-12-08 02:39:47 -08002962#endif
Christoph Hellwig698ba7b2009-12-15 16:47:37 -08002963#ifdef CONFIG_ELF_CORE
Alexey Dobriyan631f9c12008-11-10 01:32:52 +03002964 REG("coredump_filter", S_IRUGO|S_IWUSR, proc_coredump_filter_operations),
Kawai, Hidehiro3cb4a0b2007-07-19 01:48:28 -07002965#endif
Andrew Mortonaba76fd2006-12-10 02:19:48 -08002966#ifdef CONFIG_TASK_IO_ACCOUNTING
Alexey Dobriyan19aadc92014-08-08 14:21:50 -07002967 ONE("io", S_IRUSR, proc_tgid_io_accounting),
Andrew Mortonaba76fd2006-12-10 02:19:48 -08002968#endif
Chris Metcalff133ecc2011-05-26 12:40:09 -04002969#ifdef CONFIG_HARDWALL
Alexey Dobriyand962c142014-08-08 14:21:52 -07002970 ONE("hardwall", S_IRUGO, proc_pid_hardwall),
Chris Metcalff133ecc2011-05-26 12:40:09 -04002971#endif
Eric W. Biederman22d917d2011-11-17 00:11:58 -08002972#ifdef CONFIG_USER_NS
2973 REG("uid_map", S_IRUGO|S_IWUSR, proc_uid_map_operations),
2974 REG("gid_map", S_IRUGO|S_IWUSR, proc_gid_map_operations),
Eric W. Biedermanf76d2072012-08-30 01:24:05 -07002975 REG("projid_map", S_IRUGO|S_IWUSR, proc_projid_map_operations),
Eric W. Biederman9cc46512014-12-02 12:27:26 -06002976 REG("setgroups", S_IRUGO|S_IWUSR, proc_setgroups_operations),
Eric W. Biederman22d917d2011-11-17 00:11:58 -08002977#endif
Pavel Emelyanov48f6a7a2013-03-11 13:12:45 +04002978#ifdef CONFIG_CHECKPOINT_RESTORE
2979 REG("timers", S_IRUGO, proc_timers_operations),
2980#endif
John Stultz5de23d42016-03-17 14:20:54 -07002981 REG("timerslack_ns", S_IRUGO|S_IWUGO, proc_pid_set_timerslack_ns_operations),
Connor O'Brien6e7b83d2018-01-31 18:11:57 -08002982#ifdef CONFIG_CPU_FREQ_TIMES
2983 ONE("time_in_state", 0444, proc_time_in_state_show),
2984#endif
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002985};
2986
Al Virof0c3b502013-05-16 12:07:31 -04002987static int proc_tgid_base_readdir(struct file *file, struct dir_context *ctx)
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002988{
Al Virof0c3b502013-05-16 12:07:31 -04002989 return proc_pident_readdir(file, ctx,
2990 tgid_base_stuff, ARRAY_SIZE(tgid_base_stuff));
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002991}
2992
Arjan van de Ven00977a52007-02-12 00:55:34 -08002993static const struct file_operations proc_tgid_base_operations = {
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002994 .read = generic_read_dir,
Al Virof50752e2016-04-20 17:13:54 -04002995 .iterate_shared = proc_tgid_base_readdir,
2996 .llseek = generic_file_llseek,
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002997};
2998
Christian Braunercf9f8292018-11-19 00:51:56 +01002999struct pid *tgid_pidfd_to_pid(const struct file *file)
3000{
3001 if (!d_is_dir(file->f_path.dentry) ||
3002 (file->f_op != &proc_tgid_base_operations))
3003 return ERR_PTR(-EBADF);
3004
3005 return proc_pid(file_inode(file));
3006}
3007
Al Viro00cd8dd2012-06-10 17:13:09 -04003008static struct dentry *proc_tgid_base_lookup(struct inode *dir, struct dentry *dentry, unsigned int flags)
3009{
Eric W. Biederman7bcd6b02006-10-02 02:18:56 -07003010 return proc_pident_lookup(dir, dentry,
3011 tgid_base_stuff, ARRAY_SIZE(tgid_base_stuff));
Eric W. Biederman28a6d672006-10-02 02:17:05 -07003012}
3013
Arjan van de Venc5ef1c42007-02-12 00:55:40 -08003014static const struct inode_operations proc_tgid_base_inode_operations = {
Eric W. Biederman28a6d672006-10-02 02:17:05 -07003015 .lookup = proc_tgid_base_lookup,
3016 .getattr = pid_getattr,
3017 .setattr = proc_setattr,
Vasiliy Kulikov04996802012-01-10 15:11:31 -08003018 .permission = proc_pid_permission,
Eric W. Biederman28a6d672006-10-02 02:17:05 -07003019};
3020
Pavel Emelyanov60347f62007-10-18 23:40:03 -07003021static void proc_flush_task_mnt(struct vfsmount *mnt, pid_t pid, pid_t tgid)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003022{
Eric W. Biederman48e64842006-06-26 00:25:48 -07003023 struct dentry *dentry, *leader, *dir;
Eric W. Biederman8578cea2006-06-26 00:25:54 -07003024 char buf[PROC_NUMBUF];
Eric W. Biederman48e64842006-06-26 00:25:48 -07003025 struct qstr name;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003026
Eric W. Biederman48e64842006-06-26 00:25:48 -07003027 name.name = buf;
Pavel Emelyanov60347f62007-10-18 23:40:03 -07003028 name.len = snprintf(buf, sizeof(buf), "%d", pid);
Al Viro4f522a22013-02-11 23:20:37 -05003029 /* no ->d_hash() rejects on procfs */
Pavel Emelyanov60347f62007-10-18 23:40:03 -07003030 dentry = d_hash_and_lookup(mnt->mnt_root, &name);
Eric W. Biederman48e64842006-06-26 00:25:48 -07003031 if (dentry) {
Eric W. Biedermanbbd51922014-02-13 10:24:23 -08003032 d_invalidate(dentry);
Eric W. Biederman48e64842006-06-26 00:25:48 -07003033 dput(dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003034 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003035
Oleg Nesterovc35a7f12014-12-10 15:54:56 -08003036 if (pid == tgid)
3037 return;
3038
Eric W. Biederman48e64842006-06-26 00:25:48 -07003039 name.name = buf;
Pavel Emelyanov60347f62007-10-18 23:40:03 -07003040 name.len = snprintf(buf, sizeof(buf), "%d", tgid);
3041 leader = d_hash_and_lookup(mnt->mnt_root, &name);
Eric W. Biederman48e64842006-06-26 00:25:48 -07003042 if (!leader)
3043 goto out;
3044
3045 name.name = "task";
3046 name.len = strlen(name.name);
3047 dir = d_hash_and_lookup(leader, &name);
3048 if (!dir)
3049 goto out_put_leader;
3050
3051 name.name = buf;
Pavel Emelyanov60347f62007-10-18 23:40:03 -07003052 name.len = snprintf(buf, sizeof(buf), "%d", pid);
Eric W. Biederman48e64842006-06-26 00:25:48 -07003053 dentry = d_hash_and_lookup(dir, &name);
3054 if (dentry) {
Eric W. Biedermanbbd51922014-02-13 10:24:23 -08003055 d_invalidate(dentry);
Eric W. Biederman48e64842006-06-26 00:25:48 -07003056 dput(dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003057 }
Eric W. Biederman48e64842006-06-26 00:25:48 -07003058
3059 dput(dir);
3060out_put_leader:
3061 dput(leader);
3062out:
3063 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003064}
3065
Randy Dunlap0895e912007-10-21 21:00:10 -07003066/**
3067 * proc_flush_task - Remove dcache entries for @task from the /proc dcache.
3068 * @task: task that should be flushed.
3069 *
3070 * When flushing dentries from proc, one needs to flush them from global
Pavel Emelyanov60347f62007-10-18 23:40:03 -07003071 * proc (proc_mnt) and from all the namespaces' procs this task was seen
Randy Dunlap0895e912007-10-21 21:00:10 -07003072 * in. This call is supposed to do all of this job.
3073 *
3074 * Looks in the dcache for
3075 * /proc/@pid
3076 * /proc/@tgid/task/@pid
3077 * if either directory is present flushes it and all of it'ts children
3078 * from the dcache.
3079 *
3080 * It is safe and reasonable to cache /proc entries for a task until
3081 * that task exits. After that they just clog up the dcache with
3082 * useless entries, possibly causing useful dcache entries to be
3083 * flushed instead. This routine is proved to flush those useless
3084 * dcache entries at process exit time.
3085 *
3086 * NOTE: This routine is just an optimization so it does not guarantee
3087 * that no dcache entries will exist at process exit time it
3088 * just makes it very unlikely that any will persist.
Pavel Emelyanov60347f62007-10-18 23:40:03 -07003089 */
3090
3091void proc_flush_task(struct task_struct *task)
3092{
Eric W. Biederman9fcc2d12007-11-14 17:00:07 -08003093 int i;
Oleg Nesterov9b4d1cbe2009-09-22 16:45:34 -07003094 struct pid *pid, *tgid;
Pavel Emelyanov130f77e2007-10-18 23:40:11 -07003095 struct upid *upid;
3096
Pavel Emelyanov130f77e2007-10-18 23:40:11 -07003097 pid = task_pid(task);
Oleg Nesterov9b4d1cbe2009-09-22 16:45:34 -07003098 tgid = task_tgid(task);
Pavel Emelyanov130f77e2007-10-18 23:40:11 -07003099
Eric W. Biederman9fcc2d12007-11-14 17:00:07 -08003100 for (i = 0; i <= pid->level; i++) {
Pavel Emelyanov130f77e2007-10-18 23:40:11 -07003101 upid = &pid->numbers[i];
3102 proc_flush_task_mnt(upid->ns->proc_mnt, upid->nr,
Oleg Nesterov9b4d1cbe2009-09-22 16:45:34 -07003103 tgid->numbers[i].nr);
Pavel Emelyanov130f77e2007-10-18 23:40:11 -07003104 }
Pavel Emelyanov60347f62007-10-18 23:40:03 -07003105}
3106
Al Viroc52a47a2013-06-15 11:15:20 +04003107static int proc_pid_instantiate(struct inode *dir,
3108 struct dentry * dentry,
3109 struct task_struct *task, const void *ptr)
Eric W. Biederman444ceed2006-10-02 02:18:49 -07003110{
Eric W. Biederman444ceed2006-10-02 02:18:49 -07003111 struct inode *inode;
3112
Eric W. Biederman61a28782006-10-02 02:18:49 -07003113 inode = proc_pid_make_inode(dir->i_sb, task);
Eric W. Biederman444ceed2006-10-02 02:18:49 -07003114 if (!inode)
3115 goto out;
3116
3117 inode->i_mode = S_IFDIR|S_IRUGO|S_IXUGO;
3118 inode->i_op = &proc_tgid_base_inode_operations;
3119 inode->i_fop = &proc_tgid_base_operations;
3120 inode->i_flags|=S_IMMUTABLE;
Vegard Nossumaed54172008-06-05 22:46:53 -07003121
Miklos Szeredibfe86842011-10-28 14:13:29 +02003122 set_nlink(inode, 2 + pid_entry_count_dirs(tgid_base_stuff,
3123 ARRAY_SIZE(tgid_base_stuff)));
Eric W. Biederman444ceed2006-10-02 02:18:49 -07003124
Nick Pigginfb045ad2011-01-07 17:49:55 +11003125 d_set_d_op(dentry, &pid_dentry_operations);
Eric W. Biederman444ceed2006-10-02 02:18:49 -07003126
3127 d_add(dentry, inode);
3128 /* Close the race of the process dying before we return the dentry */
Al Viro0b728e12012-06-10 16:03:43 -04003129 if (pid_revalidate(dentry, 0))
Al Viroc52a47a2013-06-15 11:15:20 +04003130 return 0;
Eric W. Biederman444ceed2006-10-02 02:18:49 -07003131out:
Al Viroc52a47a2013-06-15 11:15:20 +04003132 return -ENOENT;
Eric W. Biederman444ceed2006-10-02 02:18:49 -07003133}
3134
Al Viro00cd8dd2012-06-10 17:13:09 -04003135struct dentry *proc_pid_lookup(struct inode *dir, struct dentry * dentry, unsigned int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003136{
Alexey Dobriyan335eb532014-08-08 14:21:27 -07003137 int result = -ENOENT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003138 struct task_struct *task;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003139 unsigned tgid;
Pavel Emelyanovb4888932007-10-18 23:40:14 -07003140 struct pid_namespace *ns;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003141
Alexey Dobriyandbcdb502014-08-08 14:21:25 -07003142 tgid = name_to_int(&dentry->d_name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003143 if (tgid == ~0U)
3144 goto out;
3145
Pavel Emelyanovb4888932007-10-18 23:40:14 -07003146 ns = dentry->d_sb->s_fs_info;
Eric W. Biedermande758732006-06-26 00:25:51 -07003147 rcu_read_lock();
Pavel Emelyanovb4888932007-10-18 23:40:14 -07003148 task = find_task_by_pid_ns(tgid, ns);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003149 if (task)
3150 get_task_struct(task);
Eric W. Biedermande758732006-06-26 00:25:51 -07003151 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07003152 if (!task)
3153 goto out;
3154
Eric W. Biederman444ceed2006-10-02 02:18:49 -07003155 result = proc_pid_instantiate(dir, dentry, task, NULL);
Eric W. Biederman48e64842006-06-26 00:25:48 -07003156 put_task_struct(task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003157out:
Al Viroc52a47a2013-06-15 11:15:20 +04003158 return ERR_PTR(result);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003159}
3160
Linus Torvalds1da177e2005-04-16 15:20:36 -07003161/*
Eric W. Biederman0804ef42006-10-02 02:17:04 -07003162 * Find the first task with tgid >= tgid
Eric W. Biederman0bc58a92006-06-26 00:25:50 -07003163 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07003164 */
Eric W. Biederman19fd4bb2007-11-28 16:21:26 -08003165struct tgid_iter {
3166 unsigned int tgid;
Eric W. Biederman0804ef42006-10-02 02:17:04 -07003167 struct task_struct *task;
Eric W. Biederman19fd4bb2007-11-28 16:21:26 -08003168};
3169static struct tgid_iter next_tgid(struct pid_namespace *ns, struct tgid_iter iter)
3170{
Eric W. Biederman0804ef42006-10-02 02:17:04 -07003171 struct pid *pid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003172
Eric W. Biederman19fd4bb2007-11-28 16:21:26 -08003173 if (iter.task)
3174 put_task_struct(iter.task);
Eric W. Biederman0804ef42006-10-02 02:17:04 -07003175 rcu_read_lock();
3176retry:
Eric W. Biederman19fd4bb2007-11-28 16:21:26 -08003177 iter.task = NULL;
3178 pid = find_ge_pid(iter.tgid, ns);
Eric W. Biederman0804ef42006-10-02 02:17:04 -07003179 if (pid) {
Eric W. Biederman19fd4bb2007-11-28 16:21:26 -08003180 iter.tgid = pid_nr_ns(pid, ns);
3181 iter.task = pid_task(pid, PIDTYPE_PID);
Eric W. Biederman0804ef42006-10-02 02:17:04 -07003182 /* What we to know is if the pid we have find is the
3183 * pid of a thread_group_leader. Testing for task
3184 * being a thread_group_leader is the obvious thing
3185 * todo but there is a window when it fails, due to
3186 * the pid transfer logic in de_thread.
3187 *
3188 * So we perform the straight forward test of seeing
3189 * if the pid we have found is the pid of a thread
3190 * group leader, and don't worry if the task we have
3191 * found doesn't happen to be a thread group leader.
3192 * As we don't care in the case of readdir.
3193 */
Eric W. Biederman19fd4bb2007-11-28 16:21:26 -08003194 if (!iter.task || !has_group_leader_pid(iter.task)) {
3195 iter.tgid += 1;
Eric W. Biederman0804ef42006-10-02 02:17:04 -07003196 goto retry;
Eric W. Biederman19fd4bb2007-11-28 16:21:26 -08003197 }
3198 get_task_struct(iter.task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003199 }
Eric W. Biederman454cc102006-06-26 00:25:51 -07003200 rcu_read_unlock();
Eric W. Biederman19fd4bb2007-11-28 16:21:26 -08003201 return iter;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003202}
3203
Eric W. Biederman00978752014-07-31 03:10:50 -07003204#define TGID_OFFSET (FIRST_PROCESS_ENTRY + 2)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003205
3206/* for the /proc/ directory itself, after non-process stuff has been done */
Al Virof0c3b502013-05-16 12:07:31 -04003207int proc_pid_readdir(struct file *file, struct dir_context *ctx)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003208{
Eric W. Biederman19fd4bb2007-11-28 16:21:26 -08003209 struct tgid_iter iter;
Al Viro3aa33772014-10-31 00:42:35 -04003210 struct pid_namespace *ns = file_inode(file)->i_sb->s_fs_info;
Al Virof0c3b502013-05-16 12:07:31 -04003211 loff_t pos = ctx->pos;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003212
Al Viro021ada72013-03-29 19:27:05 -04003213 if (pos >= PID_MAX_LIMIT + TGID_OFFSET)
Al Virof0c3b502013-05-16 12:07:31 -04003214 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003215
Eric W. Biederman00978752014-07-31 03:10:50 -07003216 if (pos == TGID_OFFSET - 2) {
David Howells2b0143b2015-03-17 22:25:59 +00003217 struct inode *inode = d_inode(ns->proc_self);
Al Virodb963162013-06-15 10:45:10 +04003218 if (!dir_emit(ctx, "self", 4, inode->i_ino, DT_LNK))
Al Virof0c3b502013-05-16 12:07:31 -04003219 return 0;
Eric W. Biederman00978752014-07-31 03:10:50 -07003220 ctx->pos = pos = pos + 1;
Al Viro021ada72013-03-29 19:27:05 -04003221 }
Eric W. Biederman00978752014-07-31 03:10:50 -07003222 if (pos == TGID_OFFSET - 1) {
David Howells2b0143b2015-03-17 22:25:59 +00003223 struct inode *inode = d_inode(ns->proc_thread_self);
Eric W. Biederman00978752014-07-31 03:10:50 -07003224 if (!dir_emit(ctx, "thread-self", 11, inode->i_ino, DT_LNK))
3225 return 0;
3226 ctx->pos = pos = pos + 1;
3227 }
3228 iter.tgid = pos - TGID_OFFSET;
Eric W. Biederman19fd4bb2007-11-28 16:21:26 -08003229 iter.task = NULL;
Eric W. Biederman19fd4bb2007-11-28 16:21:26 -08003230 for (iter = next_tgid(ns, iter);
3231 iter.task;
3232 iter.tgid += 1, iter = next_tgid(ns, iter)) {
Al Virof0c3b502013-05-16 12:07:31 -04003233 char name[PROC_NUMBUF];
3234 int len;
Eric Dumazet9618fba2017-01-24 15:18:07 -08003235
3236 cond_resched();
Al Virof0c3b502013-05-16 12:07:31 -04003237 if (!has_pid_permissions(ns, iter.task, 2))
3238 continue;
Vasiliy Kulikov04996802012-01-10 15:11:31 -08003239
Al Virof0c3b502013-05-16 12:07:31 -04003240 len = snprintf(name, sizeof(name), "%d", iter.tgid);
3241 ctx->pos = iter.tgid + TGID_OFFSET;
3242 if (!proc_fill_cache(file, ctx, name, len,
3243 proc_pid_instantiate, iter.task, NULL)) {
Eric W. Biederman19fd4bb2007-11-28 16:21:26 -08003244 put_task_struct(iter.task);
Al Virof0c3b502013-05-16 12:07:31 -04003245 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003246 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003247 }
Al Virof0c3b502013-05-16 12:07:31 -04003248 ctx->pos = PID_MAX_LIMIT + TGID_OFFSET;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003249 return 0;
3250}
3251
Eric W. Biederman0bc58a92006-06-26 00:25:50 -07003252/*
Janis Danisevskis1b3044e2016-05-20 17:00:08 -07003253 * proc_tid_comm_permission is a special permission function exclusively
3254 * used for the node /proc/<pid>/task/<tid>/comm.
3255 * It bypasses generic permission checks in the case where a task of the same
3256 * task group attempts to access the node.
3257 * The rationale behind this is that glibc and bionic access this node for
3258 * cross thread naming (pthread_set/getname_np(!self)). However, if
3259 * PR_SET_DUMPABLE gets set to 0 this node among others becomes uid=0 gid=0,
3260 * which locks out the cross thread naming implementation.
3261 * This function makes sure that the node is always accessible for members of
3262 * same thread group.
3263 */
3264static int proc_tid_comm_permission(struct inode *inode, int mask)
3265{
3266 bool is_same_tgroup;
3267 struct task_struct *task;
3268
3269 task = get_proc_task(inode);
3270 if (!task)
3271 return -ESRCH;
3272 is_same_tgroup = same_thread_group(current, task);
3273 put_task_struct(task);
3274
3275 if (likely(is_same_tgroup && !(mask & MAY_EXEC))) {
3276 /* This file (/proc/<pid>/task/<tid>/comm) can always be
3277 * read or written by the members of the corresponding
3278 * thread group.
3279 */
3280 return 0;
3281 }
3282
3283 return generic_permission(inode, mask);
3284}
3285
3286static const struct inode_operations proc_tid_comm_inode_operations = {
3287 .permission = proc_tid_comm_permission,
3288};
3289
3290/*
Eric W. Biederman28a6d672006-10-02 02:17:05 -07003291 * Tasks
3292 */
Eric Dumazetc5141e62007-05-08 00:26:15 -07003293static const struct pid_entry tid_base_stuff[] = {
Alexey Dobriyan631f9c12008-11-10 01:32:52 +03003294 DIR("fd", S_IRUSR|S_IXUSR, proc_fd_inode_operations, proc_fd_operations),
Jerome Marchand38355412010-04-27 13:13:06 -07003295 DIR("fdinfo", S_IRUSR|S_IXUSR, proc_fdinfo_inode_operations, proc_fdinfo_operations),
Eric W. Biederman6b4e3062010-03-07 16:41:34 -08003296 DIR("ns", S_IRUSR|S_IXUGO, proc_ns_dir_inode_operations, proc_ns_dir_operations),
Eric W. Biederman6ba8ed72014-07-31 16:27:08 -07003297#ifdef CONFIG_NET
3298 DIR("net", S_IRUGO|S_IXUGO, proc_net_inode_operations, proc_net_operations),
3299#endif
Alexey Dobriyan631f9c12008-11-10 01:32:52 +03003300 REG("environ", S_IRUSR, proc_environ_operations),
Al Viroc5317162016-10-05 18:43:43 -04003301 REG("auxv", S_IRUSR, proc_auxv_operations),
Alexey Dobriyan631f9c12008-11-10 01:32:52 +03003302 ONE("status", S_IRUGO, proc_pid_status),
Djalal Harouni35a35042014-04-07 15:38:36 -07003303 ONE("personality", S_IRUSR, proc_pid_personality),
Alexey Dobriyan1c963eb2014-08-08 14:21:37 -07003304 ONE("limits", S_IRUGO, proc_pid_limits),
Ingo Molnar43ae34c2007-07-09 18:52:00 +02003305#ifdef CONFIG_SCHED_DEBUG
Alexey Dobriyan631f9c12008-11-10 01:32:52 +03003306 REG("sched", S_IRUGO|S_IWUSR, proc_pid_sched_operations),
Ingo Molnar43ae34c2007-07-09 18:52:00 +02003307#endif
Janis Danisevskis1b3044e2016-05-20 17:00:08 -07003308 NOD("comm", S_IFREG|S_IRUGO|S_IWUSR,
3309 &proc_tid_comm_inode_operations,
3310 &proc_pid_set_comm_operations, {}),
Roland McGrathebcb6732008-07-25 19:46:00 -07003311#ifdef CONFIG_HAVE_ARCH_TRACEHOOK
Alexey Dobriyan09d93bd2014-08-08 14:21:39 -07003312 ONE("syscall", S_IRUSR, proc_pid_syscall),
Roland McGrathebcb6732008-07-25 19:46:00 -07003313#endif
Alexey Dobriyanc2c0bb42015-06-25 15:00:54 -07003314 REG("cmdline", S_IRUGO, proc_pid_cmdline_ops),
Alexey Dobriyan631f9c12008-11-10 01:32:52 +03003315 ONE("stat", S_IRUGO, proc_tid_stat),
3316 ONE("statm", S_IRUGO, proc_pid_statm),
Siddhesh Poyarekarb7643752012-03-21 16:34:04 -07003317 REG("maps", S_IRUGO, proc_tid_maps_operations),
Iago López Galeiras2e13ba52015-06-25 15:00:57 -07003318#ifdef CONFIG_PROC_CHILDREN
Cyrill Gorcunov818411612012-05-31 16:26:43 -07003319 REG("children", S_IRUGO, proc_tid_children_operations),
3320#endif
Eric W. Biederman28a6d672006-10-02 02:17:05 -07003321#ifdef CONFIG_NUMA
Siddhesh Poyarekarb7643752012-03-21 16:34:04 -07003322 REG("numa_maps", S_IRUGO, proc_tid_numa_maps_operations),
Eric W. Biederman28a6d672006-10-02 02:17:05 -07003323#endif
Alexey Dobriyan631f9c12008-11-10 01:32:52 +03003324 REG("mem", S_IRUSR|S_IWUSR, proc_mem_operations),
3325 LNK("cwd", proc_cwd_link),
3326 LNK("root", proc_root_link),
3327 LNK("exe", proc_exe_link),
3328 REG("mounts", S_IRUGO, proc_mounts_operations),
3329 REG("mountinfo", S_IRUGO, proc_mountinfo_operations),
Matt Mackall1e883282008-02-04 22:29:07 -08003330#ifdef CONFIG_PROC_PAGE_MONITOR
Alexey Dobriyan631f9c12008-11-10 01:32:52 +03003331 REG("clear_refs", S_IWUSR, proc_clear_refs_operations),
Siddhesh Poyarekarb7643752012-03-21 16:34:04 -07003332 REG("smaps", S_IRUGO, proc_tid_smaps_operations),
Daniel Colascione66689622017-09-06 16:25:08 -07003333 REG("smaps_rollup", S_IRUGO, proc_pid_smaps_rollup_operations),
Djalal Harouni32ed74a2014-04-07 15:38:38 -07003334 REG("pagemap", S_IRUSR, proc_pagemap_operations),
Eric W. Biederman28a6d672006-10-02 02:17:05 -07003335#endif
3336#ifdef CONFIG_SECURITY
Alexey Dobriyan631f9c12008-11-10 01:32:52 +03003337 DIR("attr", S_IRUGO|S_IXUGO, proc_attr_dir_inode_operations, proc_attr_dir_operations),
Eric W. Biederman28a6d672006-10-02 02:17:05 -07003338#endif
3339#ifdef CONFIG_KALLSYMS
Alexey Dobriyanedfcd602014-08-08 14:21:44 -07003340 ONE("wchan", S_IRUGO, proc_pid_wchan),
Eric W. Biederman28a6d672006-10-02 02:17:05 -07003341#endif
Ken Chen2ec220e2008-11-10 11:26:08 +03003342#ifdef CONFIG_STACKTRACE
Djalal Harouni35a35042014-04-07 15:38:36 -07003343 ONE("stack", S_IRUSR, proc_pid_stack),
Eric W. Biederman28a6d672006-10-02 02:17:05 -07003344#endif
Naveen N. Rao5968cec2015-06-30 14:36:03 +05303345#ifdef CONFIG_SCHED_INFO
Alexey Dobriyanf6e826c2014-08-08 14:21:46 -07003346 ONE("schedstat", S_IRUGO, proc_pid_schedstat),
Eric W. Biederman28a6d672006-10-02 02:17:05 -07003347#endif
Arjan van de Ven97455122008-01-25 21:08:34 +01003348#ifdef CONFIG_LATENCYTOP
Alexey Dobriyan631f9c12008-11-10 01:32:52 +03003349 REG("latency", S_IRUGO, proc_lstats_operations),
Arjan van de Ven97455122008-01-25 21:08:34 +01003350#endif
Paul Menage8793d852007-10-18 23:39:39 -07003351#ifdef CONFIG_PROC_PID_CPUSET
Zefan Li52de4772014-09-18 16:03:36 +08003352 ONE("cpuset", S_IRUGO, proc_cpuset_show),
Eric W. Biederman28a6d672006-10-02 02:17:05 -07003353#endif
Paul Menagea4243162007-10-18 23:39:35 -07003354#ifdef CONFIG_CGROUPS
Zefan Li006f4ac2014-09-18 16:03:15 +08003355 ONE("cgroup", S_IRUGO, proc_cgroup_show),
Paul Menagea4243162007-10-18 23:39:35 -07003356#endif
Alexey Dobriyan6ba51e32014-08-08 14:21:48 -07003357 ONE("oom_score", S_IRUGO, proc_oom_score),
Greg Hackmann3a6594d2017-11-07 13:21:20 -08003358 REG("oom_adj", S_IRUGO|S_IWUSR, proc_oom_adj_operations),
3359 REG("oom_score_adj", S_IRUGO|S_IWUSR, proc_oom_score_adj_operations),
Eric W. Biederman28a6d672006-10-02 02:17:05 -07003360#ifdef CONFIG_AUDITSYSCALL
Alexey Dobriyan631f9c12008-11-10 01:32:52 +03003361 REG("loginuid", S_IWUSR|S_IRUGO, proc_loginuid_operations),
Al Viro26ec3c62011-02-15 21:24:05 -05003362 REG("sessionid", S_IRUGO, proc_sessionid_operations),
Eric W. Biederman28a6d672006-10-02 02:17:05 -07003363#endif
Akinobu Mitaf4f154f2006-12-08 02:39:47 -08003364#ifdef CONFIG_FAULT_INJECTION
Alexey Dobriyan631f9c12008-11-10 01:32:52 +03003365 REG("make-it-fail", S_IRUGO|S_IWUSR, proc_fault_inject_operations),
Akinobu Mitaf4f154f2006-12-08 02:39:47 -08003366#endif
Andrea Righi297c5d92008-07-25 01:48:49 -07003367#ifdef CONFIG_TASK_IO_ACCOUNTING
Alexey Dobriyan19aadc92014-08-08 14:21:50 -07003368 ONE("io", S_IRUSR, proc_tid_io_accounting),
Andrea Righi297c5d92008-07-25 01:48:49 -07003369#endif
Chris Metcalff133ecc2011-05-26 12:40:09 -04003370#ifdef CONFIG_HARDWALL
Alexey Dobriyand962c142014-08-08 14:21:52 -07003371 ONE("hardwall", S_IRUGO, proc_pid_hardwall),
Chris Metcalff133ecc2011-05-26 12:40:09 -04003372#endif
Eric W. Biederman22d917d2011-11-17 00:11:58 -08003373#ifdef CONFIG_USER_NS
3374 REG("uid_map", S_IRUGO|S_IWUSR, proc_uid_map_operations),
3375 REG("gid_map", S_IRUGO|S_IWUSR, proc_gid_map_operations),
Eric W. Biedermanf76d2072012-08-30 01:24:05 -07003376 REG("projid_map", S_IRUGO|S_IWUSR, proc_projid_map_operations),
Eric W. Biederman9cc46512014-12-02 12:27:26 -06003377 REG("setgroups", S_IRUGO|S_IWUSR, proc_setgroups_operations),
Eric W. Biederman22d917d2011-11-17 00:11:58 -08003378#endif
Connor O'Brien6e7b83d2018-01-31 18:11:57 -08003379#ifdef CONFIG_CPU_FREQ_TIMES
3380 ONE("time_in_state", 0444, proc_time_in_state_show),
3381#endif
Eric W. Biederman28a6d672006-10-02 02:17:05 -07003382};
3383
Al Virof0c3b502013-05-16 12:07:31 -04003384static int proc_tid_base_readdir(struct file *file, struct dir_context *ctx)
Eric W. Biederman28a6d672006-10-02 02:17:05 -07003385{
Al Virof0c3b502013-05-16 12:07:31 -04003386 return proc_pident_readdir(file, ctx,
3387 tid_base_stuff, ARRAY_SIZE(tid_base_stuff));
Eric W. Biederman28a6d672006-10-02 02:17:05 -07003388}
3389
Al Viro00cd8dd2012-06-10 17:13:09 -04003390static struct dentry *proc_tid_base_lookup(struct inode *dir, struct dentry *dentry, unsigned int flags)
3391{
Eric W. Biederman7bcd6b02006-10-02 02:18:56 -07003392 return proc_pident_lookup(dir, dentry,
3393 tid_base_stuff, ARRAY_SIZE(tid_base_stuff));
Eric W. Biederman28a6d672006-10-02 02:17:05 -07003394}
3395
Arjan van de Ven00977a52007-02-12 00:55:34 -08003396static const struct file_operations proc_tid_base_operations = {
Eric W. Biederman28a6d672006-10-02 02:17:05 -07003397 .read = generic_read_dir,
Al Virof50752e2016-04-20 17:13:54 -04003398 .iterate_shared = proc_tid_base_readdir,
3399 .llseek = generic_file_llseek,
Eric W. Biederman28a6d672006-10-02 02:17:05 -07003400};
3401
Arjan van de Venc5ef1c42007-02-12 00:55:40 -08003402static const struct inode_operations proc_tid_base_inode_operations = {
Eric W. Biederman28a6d672006-10-02 02:17:05 -07003403 .lookup = proc_tid_base_lookup,
3404 .getattr = pid_getattr,
3405 .setattr = proc_setattr,
3406};
3407
Al Viroc52a47a2013-06-15 11:15:20 +04003408static int proc_task_instantiate(struct inode *dir,
Eric Dumazetc5141e62007-05-08 00:26:15 -07003409 struct dentry *dentry, struct task_struct *task, const void *ptr)
Eric W. Biederman444ceed2006-10-02 02:18:49 -07003410{
Eric W. Biederman444ceed2006-10-02 02:18:49 -07003411 struct inode *inode;
Eric W. Biederman61a28782006-10-02 02:18:49 -07003412 inode = proc_pid_make_inode(dir->i_sb, task);
Eric W. Biederman444ceed2006-10-02 02:18:49 -07003413
3414 if (!inode)
3415 goto out;
3416 inode->i_mode = S_IFDIR|S_IRUGO|S_IXUGO;
3417 inode->i_op = &proc_tid_base_inode_operations;
3418 inode->i_fop = &proc_tid_base_operations;
3419 inode->i_flags|=S_IMMUTABLE;
Vegard Nossumaed54172008-06-05 22:46:53 -07003420
Miklos Szeredibfe86842011-10-28 14:13:29 +02003421 set_nlink(inode, 2 + pid_entry_count_dirs(tid_base_stuff,
3422 ARRAY_SIZE(tid_base_stuff)));
Eric W. Biederman444ceed2006-10-02 02:18:49 -07003423
Nick Pigginfb045ad2011-01-07 17:49:55 +11003424 d_set_d_op(dentry, &pid_dentry_operations);
Eric W. Biederman444ceed2006-10-02 02:18:49 -07003425
3426 d_add(dentry, inode);
3427 /* Close the race of the process dying before we return the dentry */
Al Viro0b728e12012-06-10 16:03:43 -04003428 if (pid_revalidate(dentry, 0))
Al Viroc52a47a2013-06-15 11:15:20 +04003429 return 0;
Eric W. Biederman444ceed2006-10-02 02:18:49 -07003430out:
Al Viroc52a47a2013-06-15 11:15:20 +04003431 return -ENOENT;
Eric W. Biederman444ceed2006-10-02 02:18:49 -07003432}
3433
Al Viro00cd8dd2012-06-10 17:13:09 -04003434static struct dentry *proc_task_lookup(struct inode *dir, struct dentry * dentry, unsigned int flags)
Eric W. Biederman28a6d672006-10-02 02:17:05 -07003435{
Al Viroc52a47a2013-06-15 11:15:20 +04003436 int result = -ENOENT;
Eric W. Biederman28a6d672006-10-02 02:17:05 -07003437 struct task_struct *task;
3438 struct task_struct *leader = get_proc_task(dir);
Eric W. Biederman28a6d672006-10-02 02:17:05 -07003439 unsigned tid;
Pavel Emelyanovb4888932007-10-18 23:40:14 -07003440 struct pid_namespace *ns;
Eric W. Biederman28a6d672006-10-02 02:17:05 -07003441
3442 if (!leader)
3443 goto out_no_task;
3444
Alexey Dobriyandbcdb502014-08-08 14:21:25 -07003445 tid = name_to_int(&dentry->d_name);
Eric W. Biederman28a6d672006-10-02 02:17:05 -07003446 if (tid == ~0U)
3447 goto out;
3448
Pavel Emelyanovb4888932007-10-18 23:40:14 -07003449 ns = dentry->d_sb->s_fs_info;
Eric W. Biederman28a6d672006-10-02 02:17:05 -07003450 rcu_read_lock();
Pavel Emelyanovb4888932007-10-18 23:40:14 -07003451 task = find_task_by_pid_ns(tid, ns);
Eric W. Biederman28a6d672006-10-02 02:17:05 -07003452 if (task)
3453 get_task_struct(task);
3454 rcu_read_unlock();
3455 if (!task)
3456 goto out;
Pavel Emelyanovbac0abd2007-10-18 23:40:18 -07003457 if (!same_thread_group(leader, task))
Eric W. Biederman28a6d672006-10-02 02:17:05 -07003458 goto out_drop_task;
3459
Eric W. Biederman444ceed2006-10-02 02:18:49 -07003460 result = proc_task_instantiate(dir, dentry, task, NULL);
Eric W. Biederman28a6d672006-10-02 02:17:05 -07003461out_drop_task:
3462 put_task_struct(task);
3463out:
3464 put_task_struct(leader);
3465out_no_task:
Al Viroc52a47a2013-06-15 11:15:20 +04003466 return ERR_PTR(result);
Eric W. Biederman28a6d672006-10-02 02:17:05 -07003467}
3468
3469/*
Eric W. Biederman0bc58a92006-06-26 00:25:50 -07003470 * Find the first tid of a thread group to return to user space.
3471 *
3472 * Usually this is just the thread group leader, but if the users
3473 * buffer was too small or there was a seek into the middle of the
3474 * directory we have more work todo.
3475 *
3476 * In the case of a short read we start with find_task_by_pid.
3477 *
3478 * In the case of a seek we start with the leader and walk nr
3479 * threads past it.
3480 */
Oleg Nesterov9f6e9632014-01-23 15:55:40 -08003481static struct task_struct *first_tid(struct pid *pid, int tid, loff_t f_pos,
3482 struct pid_namespace *ns)
Eric W. Biederman0bc58a92006-06-26 00:25:50 -07003483{
Oleg Nesterovd855a4b2014-01-23 15:55:39 -08003484 struct task_struct *pos, *task;
Oleg Nesterov9f6e9632014-01-23 15:55:40 -08003485 unsigned long nr = f_pos;
3486
3487 if (nr != f_pos) /* 32bit overflow? */
3488 return NULL;
Eric W. Biederman0bc58a92006-06-26 00:25:50 -07003489
Eric W. Biedermancc288732006-06-26 00:26:01 -07003490 rcu_read_lock();
Oleg Nesterovd855a4b2014-01-23 15:55:39 -08003491 task = pid_task(pid, PIDTYPE_PID);
3492 if (!task)
3493 goto fail;
3494
3495 /* Attempt to start with the tid of a thread */
Oleg Nesterov9f6e9632014-01-23 15:55:40 -08003496 if (tid && nr) {
Pavel Emelyanovb4888932007-10-18 23:40:14 -07003497 pos = find_task_by_pid_ns(tid, ns);
Oleg Nesterovd855a4b2014-01-23 15:55:39 -08003498 if (pos && same_thread_group(pos, task))
Oleg Nesterova872ff02006-06-26 00:26:01 -07003499 goto found;
Eric W. Biederman0bc58a92006-06-26 00:25:50 -07003500 }
3501
3502 /* If nr exceeds the number of threads there is nothing todo */
Oleg Nesterov9f6e9632014-01-23 15:55:40 -08003503 if (nr >= get_nr_threads(task))
Oleg Nesterovc986c142014-01-23 15:55:38 -08003504 goto fail;
Oleg Nesterova872ff02006-06-26 00:26:01 -07003505
3506 /* If we haven't found our starting place yet start
3507 * with the leader and walk nr threads forward.
3508 */
Oleg Nesterovd855a4b2014-01-23 15:55:39 -08003509 pos = task = task->group_leader;
Oleg Nesterovc986c142014-01-23 15:55:38 -08003510 do {
Oleg Nesterov9f6e9632014-01-23 15:55:40 -08003511 if (!nr--)
Oleg Nesterovc986c142014-01-23 15:55:38 -08003512 goto found;
Oleg Nesterovd855a4b2014-01-23 15:55:39 -08003513 } while_each_thread(task, pos);
Oleg Nesterovc986c142014-01-23 15:55:38 -08003514fail:
3515 pos = NULL;
3516 goto out;
Oleg Nesterova872ff02006-06-26 00:26:01 -07003517found:
3518 get_task_struct(pos);
3519out:
Eric W. Biedermancc288732006-06-26 00:26:01 -07003520 rcu_read_unlock();
Eric W. Biederman0bc58a92006-06-26 00:25:50 -07003521 return pos;
3522}
3523
3524/*
3525 * Find the next thread in the thread list.
3526 * Return NULL if there is an error or no next thread.
3527 *
3528 * The reference to the input task_struct is released.
3529 */
3530static struct task_struct *next_tid(struct task_struct *start)
3531{
Oleg Nesterovc1df7fb2006-06-26 00:26:02 -07003532 struct task_struct *pos = NULL;
Eric W. Biedermancc288732006-06-26 00:26:01 -07003533 rcu_read_lock();
Oleg Nesterovc1df7fb2006-06-26 00:26:02 -07003534 if (pid_alive(start)) {
Eric W. Biederman0bc58a92006-06-26 00:25:50 -07003535 pos = next_thread(start);
Oleg Nesterovc1df7fb2006-06-26 00:26:02 -07003536 if (thread_group_leader(pos))
3537 pos = NULL;
3538 else
3539 get_task_struct(pos);
3540 }
Eric W. Biedermancc288732006-06-26 00:26:01 -07003541 rcu_read_unlock();
Eric W. Biederman0bc58a92006-06-26 00:25:50 -07003542 put_task_struct(start);
3543 return pos;
3544}
3545
Linus Torvalds1da177e2005-04-16 15:20:36 -07003546/* for the /proc/TGID/task/ directories */
Al Virof0c3b502013-05-16 12:07:31 -04003547static int proc_task_readdir(struct file *file, struct dir_context *ctx)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003548{
Oleg Nesterovd855a4b2014-01-23 15:55:39 -08003549 struct inode *inode = file_inode(file);
3550 struct task_struct *task;
Pavel Emelyanovb4888932007-10-18 23:40:14 -07003551 struct pid_namespace *ns;
Al Virof0c3b502013-05-16 12:07:31 -04003552 int tid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003553
Oleg Nesterovd855a4b2014-01-23 15:55:39 -08003554 if (proc_inode_is_dead(inode))
Al Virof0c3b502013-05-16 12:07:31 -04003555 return -ENOENT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003556
Al Virof0c3b502013-05-16 12:07:31 -04003557 if (!dir_emit_dots(file, ctx))
Oleg Nesterovd855a4b2014-01-23 15:55:39 -08003558 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003559
Eric W. Biederman0bc58a92006-06-26 00:25:50 -07003560 /* f_version caches the tgid value that the last readdir call couldn't
3561 * return. lseek aka telldir automagically resets f_version to 0.
3562 */
Al Viro3aa33772014-10-31 00:42:35 -04003563 ns = inode->i_sb->s_fs_info;
Al Virof0c3b502013-05-16 12:07:31 -04003564 tid = (int)file->f_version;
3565 file->f_version = 0;
Oleg Nesterovd855a4b2014-01-23 15:55:39 -08003566 for (task = first_tid(proc_pid(inode), tid, ctx->pos - 2, ns);
Eric W. Biederman0bc58a92006-06-26 00:25:50 -07003567 task;
Al Virof0c3b502013-05-16 12:07:31 -04003568 task = next_tid(task), ctx->pos++) {
3569 char name[PROC_NUMBUF];
3570 int len;
Pavel Emelyanovb4888932007-10-18 23:40:14 -07003571 tid = task_pid_nr_ns(task, ns);
Al Virof0c3b502013-05-16 12:07:31 -04003572 len = snprintf(name, sizeof(name), "%d", tid);
3573 if (!proc_fill_cache(file, ctx, name, len,
3574 proc_task_instantiate, task, NULL)) {
Eric W. Biederman0bc58a92006-06-26 00:25:50 -07003575 /* returning this tgid failed, save it as the first
3576 * pid for the next readir call */
Al Virof0c3b502013-05-16 12:07:31 -04003577 file->f_version = (u64)tid;
Eric W. Biederman0bc58a92006-06-26 00:25:50 -07003578 put_task_struct(task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003579 break;
Eric W. Biederman0bc58a92006-06-26 00:25:50 -07003580 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003581 }
Oleg Nesterovd855a4b2014-01-23 15:55:39 -08003582
Al Virof0c3b502013-05-16 12:07:31 -04003583 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003584}
Eric W. Biederman6e66b522006-06-26 00:25:47 -07003585
3586static int proc_task_getattr(struct vfsmount *mnt, struct dentry *dentry, struct kstat *stat)
3587{
David Howells2b0143b2015-03-17 22:25:59 +00003588 struct inode *inode = d_inode(dentry);
Eric W. Biederman99f89552006-06-26 00:25:55 -07003589 struct task_struct *p = get_proc_task(inode);
Eric W. Biederman6e66b522006-06-26 00:25:47 -07003590 generic_fillattr(inode, stat);
3591
Eric W. Biederman99f89552006-06-26 00:25:55 -07003592 if (p) {
Eric W. Biederman99f89552006-06-26 00:25:55 -07003593 stat->nlink += get_nr_threads(p);
Eric W. Biederman99f89552006-06-26 00:25:55 -07003594 put_task_struct(p);
Eric W. Biederman6e66b522006-06-26 00:25:47 -07003595 }
3596
3597 return 0;
3598}
Eric W. Biederman28a6d672006-10-02 02:17:05 -07003599
Arjan van de Venc5ef1c42007-02-12 00:55:40 -08003600static const struct inode_operations proc_task_inode_operations = {
Eric W. Biederman28a6d672006-10-02 02:17:05 -07003601 .lookup = proc_task_lookup,
3602 .getattr = proc_task_getattr,
3603 .setattr = proc_setattr,
Vasiliy Kulikov04996802012-01-10 15:11:31 -08003604 .permission = proc_pid_permission,
Eric W. Biederman28a6d672006-10-02 02:17:05 -07003605};
3606
Arjan van de Ven00977a52007-02-12 00:55:34 -08003607static const struct file_operations proc_task_operations = {
Eric W. Biederman28a6d672006-10-02 02:17:05 -07003608 .read = generic_read_dir,
Al Virof50752e2016-04-20 17:13:54 -04003609 .iterate_shared = proc_task_readdir,
3610 .llseek = generic_file_llseek,
Eric W. Biederman28a6d672006-10-02 02:17:05 -07003611};