Branch data Line data Source code
1 : : /*
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.
14 : : *
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.
48 : : */
49 : :
50 : : #include <asm/uaccess.h>
51 : :
52 : : #include <linux/errno.h>
53 : : #include <linux/time.h>
54 : : #include <linux/proc_fs.h>
55 : : #include <linux/stat.h>
56 : : #include <linux/task_io_accounting_ops.h>
57 : : #include <linux/init.h>
58 : : #include <linux/capability.h>
59 : : #include <linux/file.h>
60 : : #include <linux/fdtable.h>
61 : : #include <linux/string.h>
62 : : #include <linux/seq_file.h>
63 : : #include <linux/namei.h>
64 : : #include <linux/mnt_namespace.h>
65 : : #include <linux/mm.h>
66 : : #include <linux/swap.h>
67 : : #include <linux/rcupdate.h>
68 : : #include <linux/kallsyms.h>
69 : : #include <linux/stacktrace.h>
70 : : #include <linux/resource.h>
71 : : #include <linux/module.h>
72 : : #include <linux/mount.h>
73 : : #include <linux/security.h>
74 : : #include <linux/ptrace.h>
75 : : #include <linux/tracehook.h>
76 : : #include <linux/printk.h>
77 : : #include <linux/cgroup.h>
78 : : #include <linux/cpuset.h>
79 : : #include <linux/audit.h>
80 : : #include <linux/poll.h>
81 : : #include <linux/nsproxy.h>
82 : : #include <linux/oom.h>
83 : : #include <linux/elf.h>
84 : : #include <linux/pid_namespace.h>
85 : : #include <linux/user_namespace.h>
86 : : #include <linux/fs_struct.h>
87 : : #include <linux/slab.h>
88 : : #include <linux/flex_array.h>
89 : : #include <linux/posix-timers.h>
90 : : #ifdef CONFIG_HARDWALL
91 : : #include <asm/hardwall.h>
92 : : #endif
93 : : #include <trace/events/oom.h>
94 : : #include "internal.h"
95 : : #include "fd.h"
96 : :
97 : : /* NOTE:
98 : : * Implementing inode permission operations in /proc is almost
99 : : * certainly an error. Permission checks need to happen during
100 : : * each system call not at open time. The reason is that most of
101 : : * what we wish to check for permissions in /proc varies at runtime.
102 : : *
103 : : * The classic example of a problem is opening file descriptors
104 : : * in /proc for a task before it execs a suid executable.
105 : : */
106 : :
107 : : struct pid_entry {
108 : : char *name;
109 : : int len;
110 : : umode_t mode;
111 : : const struct inode_operations *iop;
112 : : const struct file_operations *fop;
113 : : union proc_op op;
114 : : };
115 : :
116 : : #define NOD(NAME, MODE, IOP, FOP, OP) { \
117 : : .name = (NAME), \
118 : : .len = sizeof(NAME) - 1, \
119 : : .mode = MODE, \
120 : : .iop = IOP, \
121 : : .fop = FOP, \
122 : : .op = OP, \
123 : : }
124 : :
125 : : #define DIR(NAME, MODE, iops, fops) \
126 : : NOD(NAME, (S_IFDIR|(MODE)), &iops, &fops, {} )
127 : : #define LNK(NAME, get_link) \
128 : : NOD(NAME, (S_IFLNK|S_IRWXUGO), \
129 : : &proc_pid_link_inode_operations, NULL, \
130 : : { .proc_get_link = get_link } )
131 : : #define REG(NAME, MODE, fops) \
132 : : NOD(NAME, (S_IFREG|(MODE)), NULL, &fops, {})
133 : : #define INF(NAME, MODE, read) \
134 : : NOD(NAME, (S_IFREG|(MODE)), \
135 : : NULL, &proc_info_file_operations, \
136 : : { .proc_read = read } )
137 : : #define ONE(NAME, MODE, show) \
138 : : NOD(NAME, (S_IFREG|(MODE)), \
139 : : NULL, &proc_single_file_operations, \
140 : : { .proc_show = show } )
141 : :
142 : : /* ANDROID is for special files in /proc. */
143 : : #define ANDROID(NAME, MODE, OTYPE) \
144 : : NOD(NAME, (S_IFREG|(MODE)), \
145 : : &proc_##OTYPE##_inode_operations, \
146 : : &proc_##OTYPE##_operations, {})
147 : :
148 : : /*
149 : : * Count the number of hardlinks for the pid_entry table, excluding the .
150 : : * and .. links.
151 : : */
152 : : static unsigned int pid_entry_count_dirs(const struct pid_entry *entries,
153 : : unsigned int n)
154 : : {
155 : : unsigned int i;
156 : : unsigned int count;
157 : :
158 : : count = 0;
159 [ + + ][ + + ]: 102687 : for (i = 0; i < n; ++i) {
160 [ + + ][ + + ]: 100030 : if (S_ISDIR(entries[i].mode))
161 : 15474 : ++count;
162 : : }
163 : :
164 : : return count;
165 : : }
166 : :
167 : 0 : static int get_task_root(struct task_struct *task, struct path *root)
168 : : {
169 : : int result = -ENOENT;
170 : :
171 : : task_lock(task);
172 [ + - ]: 1711 : if (task->fs) {
173 : : get_fs_root(task->fs, root);
174 : : result = 0;
175 : : }
176 : : task_unlock(task);
177 : 1711 : return result;
178 : : }
179 : :
180 : 0 : static int proc_cwd_link(struct dentry *dentry, struct path *path)
181 : : {
182 : 768 : struct task_struct *task = get_proc_task(dentry->d_inode);
183 : : int result = -ENOENT;
184 : :
185 [ + - ]: 768 : if (task) {
186 : : task_lock(task);
187 [ + - ]: 768 : if (task->fs) {
188 : : get_fs_pwd(task->fs, path);
189 : : result = 0;
190 : : }
191 : : task_unlock(task);
192 : : put_task_struct(task);
193 : : }
194 : 768 : return result;
195 : : }
196 : :
197 : 0 : static int proc_root_link(struct dentry *dentry, struct path *path)
198 : : {
199 : 1711 : struct task_struct *task = get_proc_task(dentry->d_inode);
200 : : int result = -ENOENT;
201 : :
202 [ + - ]: 1711 : if (task) {
203 : 1711 : result = get_task_root(task, path);
204 : : put_task_struct(task);
205 : : }
206 : 0 : return result;
207 : : }
208 : :
209 : 0 : static int proc_pid_cmdline(struct task_struct *task, char * buffer)
210 : : {
211 : : int res = 0;
212 : : unsigned int len;
213 : 113 : struct mm_struct *mm = get_task_mm(task);
214 [ + + ]: 113 : if (!mm)
215 : : goto out;
216 [ + - ]: 56 : if (!mm->arg_end)
217 : : goto out_mm; /* Shh! No looking before we're done */
218 : :
219 : 56 : len = mm->arg_end - mm->arg_start;
220 : :
221 [ - + ]: 56 : if (len > PAGE_SIZE)
222 : : len = PAGE_SIZE;
223 : :
224 : 56 : res = access_process_vm(task, mm->arg_start, buffer, len, 0);
225 : :
226 : : // If the nul at the end of args has been overwritten, then
227 : : // assume application is using setproctitle(3).
228 [ + - ][ - + ]: 56 : if (res > 0 && buffer[res-1] != '\0' && len < PAGE_SIZE) {
[ # # ]
229 : 0 : len = strnlen(buffer, res);
230 [ # # ]: 0 : if (len < res) {
231 : 0 : res = len;
232 : : } else {
233 : 0 : len = mm->env_end - mm->env_start;
234 [ # # ]: 0 : if (len > PAGE_SIZE - res)
235 : : len = PAGE_SIZE - res;
236 : 0 : res += access_process_vm(task, mm->env_start, buffer+res, len, 0);
237 : 0 : res = strnlen(buffer, res);
238 : : }
239 : : }
240 : : out_mm:
241 : 56 : mmput(mm);
242 : : out:
243 : 113 : return res;
244 : : }
245 : :
246 : 0 : static int proc_pid_auxv(struct task_struct *task, char *buffer)
247 : : {
248 : 42 : struct mm_struct *mm = mm_access(task, PTRACE_MODE_READ);
249 : : int res = PTR_ERR(mm);
250 [ + - ][ + - ]: 42 : if (mm && !IS_ERR(mm)) {
251 : : unsigned int nwords = 0;
252 : : do {
253 : 756 : nwords += 2;
254 [ + + ]: 756 : } while (mm->saved_auxv[nwords - 2] != 0); /* AT_NULL */
255 : 42 : res = nwords * sizeof(mm->saved_auxv[0]);
256 [ - + ]: 42 : if (res > PAGE_SIZE)
257 : : res = PAGE_SIZE;
258 : 42 : memcpy(buffer, mm->saved_auxv, res);
259 : 42 : mmput(mm);
260 : : }
261 : 0 : return res;
262 : : }
263 : :
264 : :
265 : : #ifdef CONFIG_KALLSYMS
266 : : /*
267 : : * Provides a wchan file via kallsyms in a proper one-value-per-file format.
268 : : * Returns the resolved symbol. If that fails, simply return the address.
269 : : */
270 : 0 : static int proc_pid_wchan(struct task_struct *task, char *buffer)
271 : : {
272 : : unsigned long wchan;
273 : : char symname[KSYM_NAME_LEN];
274 : :
275 : 4 : wchan = get_wchan(task);
276 : :
277 [ + - ]: 4 : if (lookup_symbol_name(wchan, symname) < 0)
278 [ + - ]: 4 : if (!ptrace_may_access(task, PTRACE_MODE_READ))
279 : : return 0;
280 : : else
281 : 4 : return sprintf(buffer, "%lu", wchan);
282 : : else
283 : 0 : return sprintf(buffer, "%s", symname);
284 : : }
285 : : #endif /* CONFIG_KALLSYMS */
286 : :
287 : 0 : static int lock_trace(struct task_struct *task)
288 : : {
289 : 10 : int err = mutex_lock_killable(&task->signal->cred_guard_mutex);
290 [ + - ]: 10 : if (err)
291 : : return err;
292 [ - + ]: 10 : if (!ptrace_may_access(task, PTRACE_MODE_ATTACH)) {
293 : 0 : mutex_unlock(&task->signal->cred_guard_mutex);
294 : 0 : return -EPERM;
295 : : }
296 : : return 0;
297 : : }
298 : :
299 : : static void unlock_trace(struct task_struct *task)
300 : : {
301 : 10 : mutex_unlock(&task->signal->cred_guard_mutex);
302 : : }
303 : :
304 : : #ifdef CONFIG_STACKTRACE
305 : :
306 : : #define MAX_STACK_TRACE_DEPTH 64
307 : :
308 : 0 : static int proc_pid_stack(struct seq_file *m, struct pid_namespace *ns,
309 : : struct pid *pid, struct task_struct *task)
310 : : {
311 : : struct stack_trace trace;
312 : : unsigned long *entries;
313 : : int err;
314 : : int i;
315 : :
316 : : entries = kmalloc(MAX_STACK_TRACE_DEPTH * sizeof(*entries), GFP_KERNEL);
317 [ + - ]: 2 : if (!entries)
318 : : return -ENOMEM;
319 : :
320 : 2 : trace.nr_entries = 0;
321 : 2 : trace.max_entries = MAX_STACK_TRACE_DEPTH;
322 : 2 : trace.entries = entries;
323 : 2 : trace.skip = 0;
324 : :
325 : 2 : err = lock_trace(task);
326 [ + - ]: 2 : if (!err) {
327 : 2 : save_stack_trace_tsk(task, &trace);
328 : :
329 [ + + ]: 20 : for (i = 0; i < trace.nr_entries; i++) {
330 : 16 : seq_printf(m, "[<%pK>] %pS\n",
331 : 16 : (void *)entries[i], (void *)entries[i]);
332 : : }
333 : : unlock_trace(task);
334 : : }
335 : 2 : kfree(entries);
336 : :
337 : 2 : return err;
338 : : }
339 : : #endif
340 : :
341 : : #ifdef CONFIG_SCHEDSTATS
342 : : /*
343 : : * Provides /proc/PID/schedstat
344 : : */
345 : 0 : static int proc_pid_schedstat(struct task_struct *task, char *buffer)
346 : : {
347 : 4 : return sprintf(buffer, "%llu %llu %lu\n",
348 : : (unsigned long long)task->se.sum_exec_runtime,
349 : : (unsigned long long)task->sched_info.run_delay,
350 : : task->sched_info.pcount);
351 : : }
352 : : #endif
353 : :
354 : : #ifdef CONFIG_LATENCYTOP
355 : : static int lstats_show_proc(struct seq_file *m, void *v)
356 : : {
357 : : int i;
358 : : struct inode *inode = m->private;
359 : : struct task_struct *task = get_proc_task(inode);
360 : :
361 : : if (!task)
362 : : return -ESRCH;
363 : : seq_puts(m, "Latency Top version : v0.1\n");
364 : : for (i = 0; i < 32; i++) {
365 : : struct latency_record *lr = &task->latency_record[i];
366 : : if (lr->backtrace[0]) {
367 : : int q;
368 : : seq_printf(m, "%i %li %li",
369 : : lr->count, lr->time, lr->max);
370 : : for (q = 0; q < LT_BACKTRACEDEPTH; q++) {
371 : : unsigned long bt = lr->backtrace[q];
372 : : if (!bt)
373 : : break;
374 : : if (bt == ULONG_MAX)
375 : : break;
376 : : seq_printf(m, " %ps", (void *)bt);
377 : : }
378 : : seq_putc(m, '\n');
379 : : }
380 : :
381 : : }
382 : : put_task_struct(task);
383 : : return 0;
384 : : }
385 : :
386 : : static int lstats_open(struct inode *inode, struct file *file)
387 : : {
388 : : return single_open(file, lstats_show_proc, inode);
389 : : }
390 : :
391 : : static ssize_t lstats_write(struct file *file, const char __user *buf,
392 : : size_t count, loff_t *offs)
393 : : {
394 : : struct task_struct *task = get_proc_task(file_inode(file));
395 : :
396 : : if (!task)
397 : : return -ESRCH;
398 : : clear_all_latency_tracing(task);
399 : : put_task_struct(task);
400 : :
401 : : return count;
402 : : }
403 : :
404 : : static const struct file_operations proc_lstats_operations = {
405 : : .open = lstats_open,
406 : : .read = seq_read,
407 : : .write = lstats_write,
408 : : .llseek = seq_lseek,
409 : : .release = single_release,
410 : : };
411 : :
412 : : #endif
413 : :
414 : : #ifdef CONFIG_CGROUPS
415 : 0 : static int cgroup_open(struct inode *inode, struct file *file)
416 : : {
417 : 2 : struct pid *pid = PROC_I(inode)->pid;
418 : 2 : return single_open(file, proc_cgroup_show, pid);
419 : : }
420 : :
421 : : static const struct file_operations proc_cgroup_operations = {
422 : : .open = cgroup_open,
423 : : .read = seq_read,
424 : : .llseek = seq_lseek,
425 : : .release = single_release,
426 : : };
427 : : #endif
428 : :
429 : : #ifdef CONFIG_PROC_PID_CPUSET
430 : :
431 : : static int cpuset_open(struct inode *inode, struct file *file)
432 : : {
433 : : struct pid *pid = PROC_I(inode)->pid;
434 : : return single_open(file, proc_cpuset_show, pid);
435 : : }
436 : :
437 : : static const struct file_operations proc_cpuset_operations = {
438 : : .open = cpuset_open,
439 : : .read = seq_read,
440 : : .llseek = seq_lseek,
441 : : .release = single_release,
442 : : };
443 : : #endif
444 : :
445 : 0 : static int proc_oom_score(struct task_struct *task, char *buffer)
446 : : {
447 : 4 : unsigned long totalpages = totalram_pages + total_swap_pages;
448 : : unsigned long points = 0;
449 : :
450 : 4 : read_lock(&tasklist_lock);
451 [ + - ]: 4 : if (pid_alive(task))
452 : 4 : points = oom_badness(task, NULL, NULL, totalpages) *
453 : : 1000 / totalpages;
454 : : read_unlock(&tasklist_lock);
455 : 4 : return sprintf(buffer, "%lu\n", points);
456 : : }
457 : :
458 : : struct limit_names {
459 : : char *name;
460 : : char *unit;
461 : : };
462 : :
463 : : static const struct limit_names lnames[RLIM_NLIMITS] = {
464 : : [RLIMIT_CPU] = {"Max cpu time", "seconds"},
465 : : [RLIMIT_FSIZE] = {"Max file size", "bytes"},
466 : : [RLIMIT_DATA] = {"Max data size", "bytes"},
467 : : [RLIMIT_STACK] = {"Max stack size", "bytes"},
468 : : [RLIMIT_CORE] = {"Max core file size", "bytes"},
469 : : [RLIMIT_RSS] = {"Max resident set", "bytes"},
470 : : [RLIMIT_NPROC] = {"Max processes", "processes"},
471 : : [RLIMIT_NOFILE] = {"Max open files", "files"},
472 : : [RLIMIT_MEMLOCK] = {"Max locked memory", "bytes"},
473 : : [RLIMIT_AS] = {"Max address space", "bytes"},
474 : : [RLIMIT_LOCKS] = {"Max file locks", "locks"},
475 : : [RLIMIT_SIGPENDING] = {"Max pending signals", "signals"},
476 : : [RLIMIT_MSGQUEUE] = {"Max msgqueue size", "bytes"},
477 : : [RLIMIT_NICE] = {"Max nice priority", NULL},
478 : : [RLIMIT_RTPRIO] = {"Max realtime priority", NULL},
479 : : [RLIMIT_RTTIME] = {"Max realtime timeout", "us"},
480 : : };
481 : :
482 : : /* Display limits for a process */
483 : 0 : static int proc_pid_limits(struct task_struct *task, char *buffer)
484 : : {
485 : : unsigned int i;
486 : : int count = 0;
487 : : unsigned long flags;
488 : : char *bufptr = buffer;
489 : :
490 : : struct rlimit rlim[RLIM_NLIMITS];
491 : :
492 [ + - ]: 75 : if (!lock_task_sighand(task, &flags))
493 : : return 0;
494 : 75 : memcpy(rlim, task->signal->rlim, sizeof(struct rlimit) * RLIM_NLIMITS);
495 : 75 : unlock_task_sighand(task, &flags);
496 : :
497 : : /*
498 : : * print the file header
499 : : */
500 : 75 : count += sprintf(&bufptr[count], "%-25s %-20s %-20s %-10s\n",
501 : : "Limit", "Soft Limit", "Hard Limit", "Units");
502 : :
503 [ + - ]: 1200 : for (i = 0; i < RLIM_NLIMITS; i++) {
504 [ + + ]: 1200 : if (rlim[i].rlim_cur == RLIM_INFINITY)
505 : 525 : count += sprintf(&bufptr[count], "%-25s %-20s ",
506 : : lnames[i].name, "unlimited");
507 : : else
508 : 675 : count += sprintf(&bufptr[count], "%-25s %-20lu ",
509 : : lnames[i].name, rlim[i].rlim_cur);
510 : :
511 [ + + ]: 1200 : if (rlim[i].rlim_max == RLIM_INFINITY)
512 : 675 : count += sprintf(&bufptr[count], "%-20s ", "unlimited");
513 : : else
514 : 525 : count += sprintf(&bufptr[count], "%-20lu ",
515 : : rlim[i].rlim_max);
516 : :
517 [ + + ]: 1200 : if (lnames[i].unit)
518 : 975 : count += sprintf(&bufptr[count], "%-10s\n",
519 : : lnames[i].unit);
520 : : else
521 : 150 : count += sprintf(&bufptr[count], "\n");
522 : : }
523 : :
524 : : return count;
525 : : }
526 : :
527 : : #ifdef CONFIG_HAVE_ARCH_TRACEHOOK
528 : 0 : static int proc_pid_syscall(struct task_struct *task, char *buffer)
529 : : {
530 : : long nr;
531 : : unsigned long args[6], sp, pc;
532 : 6 : int res = lock_trace(task);
533 [ + - ]: 6 : if (res)
534 : : return res;
535 : :
536 [ - + ]: 6 : if (task_current_syscall(task, &nr, args, 6, &sp, &pc))
537 : 0 : res = sprintf(buffer, "running\n");
538 [ - + ]: 6 : else if (nr < 0)
539 : 0 : res = sprintf(buffer, "%ld 0x%lx 0x%lx\n", nr, sp, pc);
540 : : else
541 : 6 : res = sprintf(buffer,
542 : : "%ld 0x%lx 0x%lx 0x%lx 0x%lx 0x%lx 0x%lx 0x%lx 0x%lx\n",
543 : : nr,
544 : : args[0], args[1], args[2], args[3], args[4], args[5],
545 : : sp, pc);
546 : : unlock_trace(task);
547 : 6 : return res;
548 : : }
549 : : #endif /* CONFIG_HAVE_ARCH_TRACEHOOK */
550 : :
551 : : /************************************************************************/
552 : : /* Here the fs part begins */
553 : : /************************************************************************/
554 : :
555 : : /* permission checks */
556 : 0 : static int proc_fd_access_allowed(struct inode *inode)
557 : : {
558 : : struct task_struct *task;
559 : : int allowed = 0;
560 : : /* Allow access to a task's file descriptors if it is us or we
561 : : * may use ptrace attach to the process and find out that
562 : : * information.
563 : : */
564 : : task = get_proc_task(inode);
565 [ + - ]: 9535 : if (task) {
566 : 9535 : allowed = ptrace_may_access(task, PTRACE_MODE_READ);
567 : : put_task_struct(task);
568 : : }
569 : 0 : return allowed;
570 : : }
571 : :
572 : 0 : int proc_setattr(struct dentry *dentry, struct iattr *attr)
573 : : {
574 : : int error;
575 : 0 : struct inode *inode = dentry->d_inode;
576 : :
577 [ # # ]: 0 : if (attr->ia_valid & ATTR_MODE)
578 : : return -EPERM;
579 : :
580 : 0 : error = inode_change_ok(inode, attr);
581 [ # # ]: 0 : if (error)
582 : : return error;
583 : :
584 : 0 : setattr_copy(inode, attr);
585 : : mark_inode_dirty(inode);
586 : 0 : return 0;
587 : : }
588 : :
589 : : /*
590 : : * May current process learn task's sched/cmdline info (for hide_pid_min=1)
591 : : * or euid/egid (for hide_pid_min=2)?
592 : : */
593 : 114178 : static bool has_pid_permissions(struct pid_namespace *pid,
594 : : struct task_struct *task,
595 : : int hide_pid_min)
596 : : {
597 [ - + ]: 114178 : if (pid->hide_pid < hide_pid_min)
598 : : return true;
599 [ # # ]: 0 : if (in_group_p(pid->pid_gid))
600 : : return true;
601 : 0 : return ptrace_may_access(task, PTRACE_MODE_READ);
602 : : }
603 : :
604 : :
605 : 0 : static int proc_pid_permission(struct inode *inode, int mask)
606 : : {
607 : 130512 : struct pid_namespace *pid = inode->i_sb->s_fs_info;
608 : : struct task_struct *task;
609 : : bool has_perms;
610 : :
611 : : task = get_proc_task(inode);
612 [ + - ]: 65256 : if (!task)
613 : : return -ESRCH;
614 : 65256 : has_perms = has_pid_permissions(pid, task, 1);
615 : : put_task_struct(task);
616 : :
617 [ - + ]: 65255 : if (!has_perms) {
618 [ # # ]: 0 : if (pid->hide_pid == 2) {
619 : : /*
620 : : * Let's make getdents(), stat(), and open()
621 : : * consistent with each other. If a process
622 : : * may not stat() a file, it shouldn't be seen
623 : : * in procfs at all.
624 : : */
625 : : return -ENOENT;
626 : : }
627 : :
628 : 0 : return -EPERM;
629 : : }
630 : 65255 : return generic_permission(inode, mask);
631 : : }
632 : :
633 : :
634 : :
635 : : static const struct inode_operations proc_def_inode_operations = {
636 : : .setattr = proc_setattr,
637 : : };
638 : :
639 : : #define PROC_BLOCK_SIZE (3*1024) /* 4K page size but our output routines use some slack for overruns */
640 : :
641 : 0 : static ssize_t proc_info_read(struct file * file, char __user * buf,
642 : : size_t count, loff_t *ppos)
643 : : {
644 : : struct inode * inode = file_inode(file);
645 : : unsigned long page;
646 : : ssize_t length;
647 : : struct task_struct *task = get_proc_task(inode);
648 : :
649 : : length = -ESRCH;
650 [ + - ]: 248 : if (!task)
651 : : goto out_no_task;
652 : :
653 [ + + ]: 248 : if (count > PROC_BLOCK_SIZE)
654 : : count = PROC_BLOCK_SIZE;
655 : :
656 : : length = -ENOMEM;
657 [ + - ]: 248 : if (!(page = __get_free_page(GFP_TEMPORARY)))
658 : : goto out;
659 : :
660 : 248 : length = PROC_I(inode)->op.proc_read(task, (char*)page);
661 : :
662 [ + - ]: 248 : if (length >= 0)
663 : 248 : length = simple_read_from_buffer(buf, count, ppos, (char *)page, length);
664 : 248 : free_page(page);
665 : : out:
666 : : put_task_struct(task);
667 : : out_no_task:
668 : 248 : return length;
669 : : }
670 : :
671 : : static const struct file_operations proc_info_file_operations = {
672 : : .read = proc_info_read,
673 : : .llseek = generic_file_llseek,
674 : : };
675 : :
676 : 0 : static int proc_single_show(struct seq_file *m, void *v)
677 : : {
678 : 49168 : struct inode *inode = m->private;
679 : : struct pid_namespace *ns;
680 : : struct pid *pid;
681 : : struct task_struct *task;
682 : : int ret;
683 : :
684 : 49168 : ns = inode->i_sb->s_fs_info;
685 : : pid = proc_pid(inode);
686 : 49168 : task = get_pid_task(pid, PIDTYPE_PID);
687 [ + - ]: 49168 : if (!task)
688 : : return -ESRCH;
689 : :
690 : 49168 : ret = PROC_I(inode)->op.proc_show(m, ns, pid, task);
691 : :
692 : : put_task_struct(task);
693 : 49168 : return ret;
694 : : }
695 : :
696 : 0 : static int proc_single_open(struct inode *inode, struct file *filp)
697 : : {
698 : 49168 : return single_open(filp, proc_single_show, inode);
699 : : }
700 : :
701 : : static const struct file_operations proc_single_file_operations = {
702 : : .open = proc_single_open,
703 : : .read = seq_read,
704 : : .llseek = seq_lseek,
705 : : .release = single_release,
706 : : };
707 : :
708 : 0 : static int __mem_open(struct inode *inode, struct file *file, unsigned int mode)
709 : : {
710 : : struct task_struct *task = get_proc_task(file_inode(file));
711 : : struct mm_struct *mm;
712 : :
713 [ + - ]: 4 : if (!task)
714 : : return -ESRCH;
715 : :
716 : 4 : mm = mm_access(task, mode);
717 : : put_task_struct(task);
718 : :
719 [ - + ]: 8 : if (IS_ERR(mm))
720 : : return PTR_ERR(mm);
721 : :
722 [ + - ]: 4 : if (mm) {
723 : : /* ensure this mm_struct can't be freed */
724 : 4 : atomic_inc(&mm->mm_count);
725 : : /* but do not pin its memory */
726 : 4 : mmput(mm);
727 : : }
728 : :
729 : 4 : file->private_data = mm;
730 : :
731 : : return 0;
732 : : }
733 : :
734 : 0 : static int mem_open(struct inode *inode, struct file *file)
735 : : {
736 : 2 : int ret = __mem_open(inode, file, PTRACE_MODE_ATTACH);
737 : :
738 : : /* OK to pass negative loff_t, we can catch out-of-range */
739 : 2 : file->f_mode |= FMODE_UNSIGNED_OFFSET;
740 : :
741 : 2 : return ret;
742 : : }
743 : :
744 : 2 : static ssize_t mem_rw(struct file *file, char __user *buf,
745 : : size_t count, loff_t *ppos, int write)
746 : : {
747 : 2 : struct mm_struct *mm = file->private_data;
748 : 2 : unsigned long addr = *ppos;
749 : : ssize_t copied;
750 : : char *page;
751 : :
752 [ + - ]: 2 : if (!mm)
753 : : return 0;
754 : :
755 : 2 : page = (char *)__get_free_page(GFP_TEMPORARY);
756 [ + - ]: 2 : if (!page)
757 : : return -ENOMEM;
758 : :
759 : : copied = 0;
760 [ + - ]: 2 : if (!atomic_inc_not_zero(&mm->mm_users))
761 : : goto free;
762 : :
763 [ + - ]: 2 : while (count > 0) {
764 : 2 : int this_len = min_t(int, count, PAGE_SIZE);
765 : :
766 [ - + ][ # # ]: 2 : if (write && copy_from_user(page, buf, this_len)) {
767 : : copied = -EFAULT;
768 : : break;
769 : : }
770 : :
771 : 2 : this_len = access_remote_vm(mm, addr, page, this_len, write);
772 [ + - ]: 2 : if (!this_len) {
773 [ + - ]: 2 : if (!copied)
774 : : copied = -EIO;
775 : : break;
776 : : }
777 : :
778 [ # # ][ # # ]: 0 : if (!write && copy_to_user(buf, page, this_len)) {
779 : : copied = -EFAULT;
780 : : break;
781 : : }
782 : :
783 : 0 : buf += this_len;
784 : 0 : addr += this_len;
785 : 0 : copied += this_len;
786 : 0 : count -= this_len;
787 : : }
788 : 2 : *ppos = addr;
789 : :
790 : 2 : mmput(mm);
791 : : free:
792 : 2 : free_page((unsigned long) page);
793 : : return copied;
794 : : }
795 : :
796 : 0 : static ssize_t mem_read(struct file *file, char __user *buf,
797 : : size_t count, loff_t *ppos)
798 : : {
799 : 2 : return mem_rw(file, buf, count, ppos, 0);
800 : : }
801 : :
802 : 0 : static ssize_t mem_write(struct file *file, const char __user *buf,
803 : : size_t count, loff_t *ppos)
804 : : {
805 : 0 : return mem_rw(file, (char __user*)buf, count, ppos, 1);
806 : : }
807 : :
808 : 0 : loff_t mem_lseek(struct file *file, loff_t offset, int orig)
809 : : {
810 [ + - ]: 1 : switch (orig) {
811 : : case 0:
812 : 1 : file->f_pos = offset;
813 : 2 : break;
814 : : case 1:
815 : 0 : file->f_pos += offset;
816 : 0 : break;
817 : : default:
818 : : return -EINVAL;
819 : : }
820 : : force_successful_syscall_return();
821 : 1 : return file->f_pos;
822 : : }
823 : :
824 : 0 : static int mem_release(struct inode *inode, struct file *file)
825 : : {
826 : 4 : struct mm_struct *mm = file->private_data;
827 [ + - ]: 4 : if (mm)
828 : : mmdrop(mm);
829 : 0 : return 0;
830 : : }
831 : :
832 : : static const struct file_operations proc_mem_operations = {
833 : : .llseek = mem_lseek,
834 : : .read = mem_read,
835 : : .write = mem_write,
836 : : .open = mem_open,
837 : : .release = mem_release,
838 : : };
839 : :
840 : 0 : static int environ_open(struct inode *inode, struct file *file)
841 : : {
842 : 2 : return __mem_open(inode, file, PTRACE_MODE_READ);
843 : : }
844 : :
845 : 0 : static ssize_t environ_read(struct file *file, char __user *buf,
846 : : size_t count, loff_t *ppos)
847 : : {
848 : : char *page;
849 : 6 : unsigned long src = *ppos;
850 : : int ret = 0;
851 : 6 : struct mm_struct *mm = file->private_data;
852 : :
853 [ + - ]: 6 : if (!mm)
854 : : return 0;
855 : :
856 : 6 : page = (char *)__get_free_page(GFP_TEMPORARY);
857 [ + - ]: 6 : if (!page)
858 : : return -ENOMEM;
859 : :
860 : : ret = 0;
861 [ + - ]: 6 : if (!atomic_inc_not_zero(&mm->mm_users))
862 : : goto free;
863 [ + + ]: 10 : while (count > 0) {
864 : : size_t this_len, max_len;
865 : : int retval;
866 : :
867 [ + + ]: 8 : if (src >= (mm->env_end - mm->env_start))
868 : : break;
869 : :
870 : 4 : this_len = mm->env_end - (mm->env_start + src);
871 : :
872 : 4 : max_len = min_t(size_t, PAGE_SIZE, count);
873 : 4 : this_len = min(max_len, this_len);
874 : :
875 : 4 : retval = access_remote_vm(mm, (mm->env_start + src),
876 : : page, this_len, 0);
877 : :
878 [ + - ]: 4 : if (retval <= 0) {
879 : : ret = retval;
880 : : break;
881 : : }
882 : :
883 [ + - ]: 4 : if (copy_to_user(buf, page, retval)) {
884 : : ret = -EFAULT;
885 : : break;
886 : : }
887 : :
888 : 4 : ret += retval;
889 : 4 : src += retval;
890 : 4 : buf += retval;
891 : 4 : count -= retval;
892 : : }
893 : 6 : *ppos = src;
894 : 6 : mmput(mm);
895 : :
896 : : free:
897 : 6 : free_page((unsigned long) page);
898 : 6 : return ret;
899 : : }
900 : :
901 : : static const struct file_operations proc_environ_operations = {
902 : : .open = environ_open,
903 : : .read = environ_read,
904 : : .llseek = generic_file_llseek,
905 : : .release = mem_release,
906 : : };
907 : :
908 : 0 : static ssize_t oom_adj_read(struct file *file, char __user *buf, size_t count,
909 : : loff_t *ppos)
910 : : {
911 : : struct task_struct *task = get_proc_task(file_inode(file));
912 : : char buffer[PROC_NUMBUF];
913 : : int oom_adj = OOM_ADJUST_MIN;
914 : : size_t len;
915 : : unsigned long flags;
916 : :
917 [ + - ]: 4 : if (!task)
918 : : return -ESRCH;
919 [ + - ]: 4 : if (lock_task_sighand(task, &flags)) {
920 [ + - ]: 4 : if (task->signal->oom_score_adj == OOM_SCORE_ADJ_MAX)
921 : : oom_adj = OOM_ADJUST_MAX;
922 : : else
923 : 4 : oom_adj = (task->signal->oom_score_adj * -OOM_DISABLE) /
924 : : OOM_SCORE_ADJ_MAX;
925 : 4 : unlock_task_sighand(task, &flags);
926 : : }
927 : : put_task_struct(task);
928 : 4 : len = snprintf(buffer, sizeof(buffer), "%d\n", oom_adj);
929 : 4 : return simple_read_from_buffer(buf, count, ppos, buffer, len);
930 : : }
931 : :
932 : 0 : static ssize_t oom_adj_write(struct file *file, const char __user *buf,
933 : : size_t count, loff_t *ppos)
934 : : {
935 : : struct task_struct *task;
936 : : char buffer[PROC_NUMBUF];
937 : : int oom_adj;
938 : : unsigned long flags;
939 : : int err;
940 : :
941 : 0 : memset(buffer, 0, sizeof(buffer));
942 [ # # ]: 0 : if (count > sizeof(buffer) - 1)
943 : : count = sizeof(buffer) - 1;
944 [ # # ]: 0 : if (copy_from_user(buffer, buf, count)) {
945 : : err = -EFAULT;
946 : : goto out;
947 : : }
948 : :
949 : 0 : err = kstrtoint(strstrip(buffer), 0, &oom_adj);
950 [ # # ]: 0 : if (err)
951 : : goto out;
952 [ # # ][ # # ]: 0 : if ((oom_adj < OOM_ADJUST_MIN || oom_adj > OOM_ADJUST_MAX) &&
953 : : oom_adj != OOM_DISABLE) {
954 : : err = -EINVAL;
955 : : goto out;
956 : : }
957 : :
958 : : task = get_proc_task(file_inode(file));
959 [ # # ]: 0 : if (!task) {
960 : : err = -ESRCH;
961 : : goto out;
962 : : }
963 : :
964 : : task_lock(task);
965 [ # # ]: 0 : if (!task->mm) {
966 : : err = -EINVAL;
967 : : goto err_task_lock;
968 : : }
969 : :
970 [ # # ]: 0 : if (!lock_task_sighand(task, &flags)) {
971 : : err = -ESRCH;
972 : : goto err_task_lock;
973 : : }
974 : :
975 : : /*
976 : : * Scale /proc/pid/oom_score_adj appropriately ensuring that a maximum
977 : : * value is always attainable.
978 : : */
979 [ # # ]: 0 : if (oom_adj == OOM_ADJUST_MAX)
980 : 0 : oom_adj = OOM_SCORE_ADJ_MAX;
981 : : else
982 : 0 : oom_adj = (oom_adj * OOM_SCORE_ADJ_MAX) / -OOM_DISABLE;
983 : :
984 [ # # # # ]: 0 : if (oom_adj < task->signal->oom_score_adj &&
985 : 0 : !capable(CAP_SYS_RESOURCE)) {
986 : : err = -EACCES;
987 : : goto err_sighand;
988 : : }
989 : :
990 : : /*
991 : : * /proc/pid/oom_adj is provided for legacy purposes, ask users to use
992 : : * /proc/pid/oom_score_adj instead.
993 : : */
994 [ # # ]: 0 : pr_warn_once("%s (%d): /proc/%d/oom_adj is deprecated, please use /proc/%d/oom_score_adj instead.\n",
995 : : current->comm, task_pid_nr(current), task_pid_nr(task),
996 : : task_pid_nr(task));
997 : :
998 : 0 : task->signal->oom_score_adj = oom_adj;
999 : : trace_oom_score_adj_update(task);
1000 : : err_sighand:
1001 : 0 : unlock_task_sighand(task, &flags);
1002 : : err_task_lock:
1003 : : task_unlock(task);
1004 : : put_task_struct(task);
1005 : : out:
1006 [ # # ]: 0 : return err < 0 ? err : count;
1007 : : }
1008 : :
1009 : 0 : static int oom_adjust_permission(struct inode *inode, int mask)
1010 : : {
1011 : : kuid_t uid;
1012 : : struct task_struct *p;
1013 : : kuid_t system_server_uid = KUIDT_INIT(1000);
1014 : :
1015 : : p = get_proc_task(inode);
1016 [ + - ]: 1 : if(p) {
1017 : 1 : uid = task_uid(p);
1018 : : put_task_struct(p);
1019 : : }
1020 : :
1021 : : /*
1022 : : * System Server (uid == 1000) is granted access to oom_adj of all
1023 : : * android applications (uid > 10000) as and services (uid >= 1000)
1024 : : */
1025 [ + - ][ - + ]: 1 : if (p && (uid_eq(current_fsuid(), system_server_uid) &&
[ # # ]
1026 : : uid_gt(uid, system_server_uid))) {
1027 [ # # ]: 0 : if (inode->i_mode >> 6 & mask) {
1028 : : return 0;
1029 : : }
1030 : : }
1031 : :
1032 : : /* Fall back to default. */
1033 : 1 : return generic_permission(inode, mask);
1034 : : }
1035 : :
1036 : : static const struct inode_operations proc_oom_adj_inode_operations = {
1037 : : .permission = oom_adjust_permission,
1038 : : };
1039 : :
1040 : : static const struct file_operations proc_oom_adj_operations = {
1041 : : .read = oom_adj_read,
1042 : : .write = oom_adj_write,
1043 : : .llseek = generic_file_llseek,
1044 : : };
1045 : :
1046 : 0 : static ssize_t oom_score_adj_read(struct file *file, char __user *buf,
1047 : : size_t count, loff_t *ppos)
1048 : : {
1049 : : struct task_struct *task = get_proc_task(file_inode(file));
1050 : : char buffer[PROC_NUMBUF];
1051 : : short oom_score_adj = OOM_SCORE_ADJ_MIN;
1052 : : unsigned long flags;
1053 : : size_t len;
1054 : :
1055 [ + - ]: 4 : if (!task)
1056 : : return -ESRCH;
1057 [ + - ]: 4 : if (lock_task_sighand(task, &flags)) {
1058 : 4 : oom_score_adj = task->signal->oom_score_adj;
1059 : 4 : unlock_task_sighand(task, &flags);
1060 : : }
1061 : : put_task_struct(task);
1062 : 4 : len = snprintf(buffer, sizeof(buffer), "%hd\n", oom_score_adj);
1063 : 4 : return simple_read_from_buffer(buf, count, ppos, buffer, len);
1064 : : }
1065 : :
1066 : 0 : static ssize_t oom_score_adj_write(struct file *file, const char __user *buf,
1067 : : size_t count, loff_t *ppos)
1068 : : {
1069 : : struct task_struct *task;
1070 : : char buffer[PROC_NUMBUF];
1071 : : unsigned long flags;
1072 : : int oom_score_adj;
1073 : : int err;
1074 : :
1075 : 0 : memset(buffer, 0, sizeof(buffer));
1076 [ # # ]: 0 : if (count > sizeof(buffer) - 1)
1077 : : count = sizeof(buffer) - 1;
1078 [ # # ]: 0 : if (copy_from_user(buffer, buf, count)) {
1079 : : err = -EFAULT;
1080 : : goto out;
1081 : : }
1082 : :
1083 : 0 : err = kstrtoint(strstrip(buffer), 0, &oom_score_adj);
1084 [ # # ]: 0 : if (err)
1085 : : goto out;
1086 [ # # ]: 0 : if (oom_score_adj < OOM_SCORE_ADJ_MIN ||
1087 : : oom_score_adj > OOM_SCORE_ADJ_MAX) {
1088 : : err = -EINVAL;
1089 : : goto out;
1090 : : }
1091 : :
1092 : : task = get_proc_task(file_inode(file));
1093 [ # # ]: 0 : if (!task) {
1094 : : err = -ESRCH;
1095 : : goto out;
1096 : : }
1097 : :
1098 : : task_lock(task);
1099 [ # # ]: 0 : if (!task->mm) {
1100 : : err = -EINVAL;
1101 : : goto err_task_lock;
1102 : : }
1103 : :
1104 [ # # ]: 0 : if (!lock_task_sighand(task, &flags)) {
1105 : : err = -ESRCH;
1106 : : goto err_task_lock;
1107 : : }
1108 : :
1109 [ # # # # ]: 0 : if ((short)oom_score_adj < task->signal->oom_score_adj_min &&
1110 : 0 : !capable(CAP_SYS_RESOURCE)) {
1111 : : err = -EACCES;
1112 : : goto err_sighand;
1113 : : }
1114 : :
1115 : 0 : task->signal->oom_score_adj = (short)oom_score_adj;
1116 [ # # ]: 0 : if (has_capability_noaudit(current, CAP_SYS_RESOURCE))
1117 : 0 : task->signal->oom_score_adj_min = (short)oom_score_adj;
1118 : : trace_oom_score_adj_update(task);
1119 : :
1120 : : err_sighand:
1121 : 0 : unlock_task_sighand(task, &flags);
1122 : : err_task_lock:
1123 : : task_unlock(task);
1124 : : put_task_struct(task);
1125 : : out:
1126 [ # # ]: 0 : return err < 0 ? err : count;
1127 : : }
1128 : :
1129 : : static const struct file_operations proc_oom_score_adj_operations = {
1130 : : .read = oom_score_adj_read,
1131 : : .write = oom_score_adj_write,
1132 : : .llseek = default_llseek,
1133 : : };
1134 : :
1135 : : #ifdef CONFIG_AUDITSYSCALL
1136 : : #define TMPBUFLEN 21
1137 : 0 : static ssize_t proc_loginuid_read(struct file * file, char __user * buf,
1138 : : size_t count, loff_t *ppos)
1139 : : {
1140 : : struct inode * inode = file_inode(file);
1141 : : struct task_struct *task = get_proc_task(inode);
1142 : : ssize_t length;
1143 : : char tmpbuf[TMPBUFLEN];
1144 : :
1145 [ + - ]: 19 : if (!task)
1146 : : return -ESRCH;
1147 : 19 : length = scnprintf(tmpbuf, TMPBUFLEN, "%u",
1148 : : from_kuid(file->f_cred->user_ns,
1149 : : audit_get_loginuid(task)));
1150 : : put_task_struct(task);
1151 : 19 : return simple_read_from_buffer(buf, count, ppos, tmpbuf, length);
1152 : : }
1153 : :
1154 : 0 : static ssize_t proc_loginuid_write(struct file * file, const char __user * buf,
1155 : : size_t count, loff_t *ppos)
1156 : : {
1157 : : struct inode * inode = file_inode(file);
1158 : : char *page, *tmp;
1159 : : ssize_t length;
1160 : : uid_t loginuid;
1161 : : kuid_t kloginuid;
1162 : :
1163 : : rcu_read_lock();
1164 [ # # ]: 0 : if (current != pid_task(proc_pid(inode), PIDTYPE_PID)) {
1165 : : rcu_read_unlock();
1166 : 0 : return -EPERM;
1167 : : }
1168 : : rcu_read_unlock();
1169 : :
1170 [ # # ]: 0 : if (count >= PAGE_SIZE)
1171 : : count = PAGE_SIZE - 1;
1172 : :
1173 [ # # ]: 0 : if (*ppos != 0) {
1174 : : /* No partial writes. */
1175 : : return -EINVAL;
1176 : : }
1177 : 0 : page = (char*)__get_free_page(GFP_TEMPORARY);
1178 [ # # ]: 0 : if (!page)
1179 : : return -ENOMEM;
1180 : : length = -EFAULT;
1181 [ # # ]: 0 : if (copy_from_user(page, buf, count))
1182 : : goto out_free_page;
1183 : :
1184 : 0 : page[count] = '\0';
1185 : 0 : loginuid = simple_strtoul(page, &tmp, 10);
1186 [ # # ]: 0 : if (tmp == page) {
1187 : : length = -EINVAL;
1188 : : goto out_free_page;
1189 : :
1190 : : }
1191 : :
1192 : : /* is userspace tring to explicitly UNSET the loginuid? */
1193 [ # # ]: 0 : if (loginuid == AUDIT_UID_UNSET) {
1194 : : kloginuid = INVALID_UID;
1195 : : } else {
1196 : : kloginuid = make_kuid(file->f_cred->user_ns, loginuid);
1197 [ # # ]: 0 : if (!uid_valid(kloginuid)) {
1198 : : length = -EINVAL;
1199 : : goto out_free_page;
1200 : : }
1201 : : }
1202 : :
1203 : 0 : length = audit_set_loginuid(kloginuid);
1204 [ # # ]: 0 : if (likely(length == 0))
1205 : 0 : length = count;
1206 : :
1207 : : out_free_page:
1208 : 0 : free_page((unsigned long) page);
1209 : 0 : return length;
1210 : : }
1211 : :
1212 : : static const struct file_operations proc_loginuid_operations = {
1213 : : .read = proc_loginuid_read,
1214 : : .write = proc_loginuid_write,
1215 : : .llseek = generic_file_llseek,
1216 : : };
1217 : :
1218 : 0 : static ssize_t proc_sessionid_read(struct file * file, char __user * buf,
1219 : : size_t count, loff_t *ppos)
1220 : : {
1221 : : struct inode * inode = file_inode(file);
1222 : : struct task_struct *task = get_proc_task(inode);
1223 : : ssize_t length;
1224 : : char tmpbuf[TMPBUFLEN];
1225 : :
1226 [ + - ]: 4 : if (!task)
1227 : : return -ESRCH;
1228 : 4 : length = scnprintf(tmpbuf, TMPBUFLEN, "%u",
1229 : : audit_get_sessionid(task));
1230 : : put_task_struct(task);
1231 : 4 : return simple_read_from_buffer(buf, count, ppos, tmpbuf, length);
1232 : : }
1233 : :
1234 : : static const struct file_operations proc_sessionid_operations = {
1235 : : .read = proc_sessionid_read,
1236 : : .llseek = generic_file_llseek,
1237 : : };
1238 : : #endif
1239 : :
1240 : : #ifdef CONFIG_FAULT_INJECTION
1241 : : static ssize_t proc_fault_inject_read(struct file * file, char __user * buf,
1242 : : size_t count, loff_t *ppos)
1243 : : {
1244 : : struct task_struct *task = get_proc_task(file_inode(file));
1245 : : char buffer[PROC_NUMBUF];
1246 : : size_t len;
1247 : : int make_it_fail;
1248 : :
1249 : : if (!task)
1250 : : return -ESRCH;
1251 : : make_it_fail = task->make_it_fail;
1252 : : put_task_struct(task);
1253 : :
1254 : : len = snprintf(buffer, sizeof(buffer), "%i\n", make_it_fail);
1255 : :
1256 : : return simple_read_from_buffer(buf, count, ppos, buffer, len);
1257 : : }
1258 : :
1259 : : static ssize_t proc_fault_inject_write(struct file * file,
1260 : : const char __user * buf, size_t count, loff_t *ppos)
1261 : : {
1262 : : struct task_struct *task;
1263 : : char buffer[PROC_NUMBUF], *end;
1264 : : int make_it_fail;
1265 : :
1266 : : if (!capable(CAP_SYS_RESOURCE))
1267 : : return -EPERM;
1268 : : memset(buffer, 0, sizeof(buffer));
1269 : : if (count > sizeof(buffer) - 1)
1270 : : count = sizeof(buffer) - 1;
1271 : : if (copy_from_user(buffer, buf, count))
1272 : : return -EFAULT;
1273 : : make_it_fail = simple_strtol(strstrip(buffer), &end, 0);
1274 : : if (*end)
1275 : : return -EINVAL;
1276 : : task = get_proc_task(file_inode(file));
1277 : : if (!task)
1278 : : return -ESRCH;
1279 : : task->make_it_fail = make_it_fail;
1280 : : put_task_struct(task);
1281 : :
1282 : : return count;
1283 : : }
1284 : :
1285 : : static const struct file_operations proc_fault_inject_operations = {
1286 : : .read = proc_fault_inject_read,
1287 : : .write = proc_fault_inject_write,
1288 : : .llseek = generic_file_llseek,
1289 : : };
1290 : : #endif
1291 : :
1292 : :
1293 : : #ifdef CONFIG_SCHED_DEBUG
1294 : : /*
1295 : : * Print out various scheduling related per-task fields:
1296 : : */
1297 : 0 : static int sched_show(struct seq_file *m, void *v)
1298 : : {
1299 : 2 : struct inode *inode = m->private;
1300 : : struct task_struct *p;
1301 : :
1302 : : p = get_proc_task(inode);
1303 [ + - ]: 2 : if (!p)
1304 : : return -ESRCH;
1305 : 2 : proc_sched_show_task(p, m);
1306 : :
1307 : : put_task_struct(p);
1308 : :
1309 : : return 0;
1310 : : }
1311 : :
1312 : : static ssize_t
1313 : 0 : sched_write(struct file *file, const char __user *buf,
1314 : : size_t count, loff_t *offset)
1315 : : {
1316 : : struct inode *inode = file_inode(file);
1317 : : struct task_struct *p;
1318 : :
1319 : : p = get_proc_task(inode);
1320 [ # # ]: 0 : if (!p)
1321 : : return -ESRCH;
1322 : 0 : proc_sched_set_task(p);
1323 : :
1324 : : put_task_struct(p);
1325 : :
1326 : 0 : return count;
1327 : : }
1328 : :
1329 : 0 : static int sched_open(struct inode *inode, struct file *filp)
1330 : : {
1331 : 2 : return single_open(filp, sched_show, inode);
1332 : : }
1333 : :
1334 : : static const struct file_operations proc_pid_sched_operations = {
1335 : : .open = sched_open,
1336 : : .read = seq_read,
1337 : : .write = sched_write,
1338 : : .llseek = seq_lseek,
1339 : : .release = single_release,
1340 : : };
1341 : :
1342 : : #endif
1343 : :
1344 : : #ifdef CONFIG_SCHED_AUTOGROUP
1345 : : /*
1346 : : * Print out autogroup related information:
1347 : : */
1348 : : static int sched_autogroup_show(struct seq_file *m, void *v)
1349 : : {
1350 : : struct inode *inode = m->private;
1351 : : struct task_struct *p;
1352 : :
1353 : : p = get_proc_task(inode);
1354 : : if (!p)
1355 : : return -ESRCH;
1356 : : proc_sched_autogroup_show_task(p, m);
1357 : :
1358 : : put_task_struct(p);
1359 : :
1360 : : return 0;
1361 : : }
1362 : :
1363 : : static ssize_t
1364 : : sched_autogroup_write(struct file *file, const char __user *buf,
1365 : : size_t count, loff_t *offset)
1366 : : {
1367 : : struct inode *inode = file_inode(file);
1368 : : struct task_struct *p;
1369 : : char buffer[PROC_NUMBUF];
1370 : : int nice;
1371 : : int err;
1372 : :
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;
1378 : :
1379 : : err = kstrtoint(strstrip(buffer), 0, &nice);
1380 : : if (err < 0)
1381 : : return err;
1382 : :
1383 : : p = get_proc_task(inode);
1384 : : if (!p)
1385 : : return -ESRCH;
1386 : :
1387 : : err = proc_sched_autogroup_set_nice(p, nice);
1388 : : if (err)
1389 : : count = err;
1390 : :
1391 : : put_task_struct(p);
1392 : :
1393 : : return count;
1394 : : }
1395 : :
1396 : : static int sched_autogroup_open(struct inode *inode, struct file *filp)
1397 : : {
1398 : : int ret;
1399 : :
1400 : : ret = single_open(filp, sched_autogroup_show, NULL);
1401 : : if (!ret) {
1402 : : struct seq_file *m = filp->private_data;
1403 : :
1404 : : m->private = inode;
1405 : : }
1406 : : return ret;
1407 : : }
1408 : :
1409 : : static const struct file_operations proc_pid_sched_autogroup_operations = {
1410 : : .open = sched_autogroup_open,
1411 : : .read = seq_read,
1412 : : .write = sched_autogroup_write,
1413 : : .llseek = seq_lseek,
1414 : : .release = single_release,
1415 : : };
1416 : :
1417 : : #endif /* CONFIG_SCHED_AUTOGROUP */
1418 : :
1419 : 0 : static ssize_t comm_write(struct file *file, const char __user *buf,
1420 : : size_t count, loff_t *offset)
1421 : : {
1422 : : struct inode *inode = file_inode(file);
1423 : : struct task_struct *p;
1424 : : char buffer[TASK_COMM_LEN];
1425 : : const size_t maxlen = sizeof(buffer) - 1;
1426 : :
1427 : 0 : memset(buffer, 0, sizeof(buffer));
1428 [ # # ]: 0 : if (copy_from_user(buffer, buf, count > maxlen ? maxlen : count))
1429 : : return -EFAULT;
1430 : :
1431 : : p = get_proc_task(inode);
1432 [ # # ]: 0 : if (!p)
1433 : : return -ESRCH;
1434 : :
1435 [ # # ]: 0 : if (same_thread_group(current, p))
1436 : 0 : set_task_comm(p, buffer);
1437 : : else
1438 : : count = -EINVAL;
1439 : :
1440 : : put_task_struct(p);
1441 : :
1442 : 0 : return count;
1443 : : }
1444 : :
1445 : 0 : static int comm_show(struct seq_file *m, void *v)
1446 : : {
1447 : 2 : struct inode *inode = m->private;
1448 : : struct task_struct *p;
1449 : :
1450 : : p = get_proc_task(inode);
1451 [ + - ]: 2 : if (!p)
1452 : : return -ESRCH;
1453 : :
1454 : : task_lock(p);
1455 : 2 : seq_printf(m, "%s\n", p->comm);
1456 : : task_unlock(p);
1457 : :
1458 : : put_task_struct(p);
1459 : :
1460 : : return 0;
1461 : : }
1462 : :
1463 : 0 : static int comm_open(struct inode *inode, struct file *filp)
1464 : : {
1465 : 2 : return single_open(filp, comm_show, inode);
1466 : : }
1467 : :
1468 : : static const struct file_operations proc_pid_set_comm_operations = {
1469 : : .open = comm_open,
1470 : : .read = seq_read,
1471 : : .write = comm_write,
1472 : : .llseek = seq_lseek,
1473 : : .release = single_release,
1474 : : };
1475 : :
1476 : 0 : static int proc_exe_link(struct dentry *dentry, struct path *exe_path)
1477 : : {
1478 : : struct task_struct *task;
1479 : : struct mm_struct *mm;
1480 : : struct file *exe_file;
1481 : :
1482 : 2920 : task = get_proc_task(dentry->d_inode);
1483 [ + - ]: 2920 : if (!task)
1484 : : return -ENOENT;
1485 : 2920 : mm = get_task_mm(task);
1486 : : put_task_struct(task);
1487 [ + + ]: 2920 : if (!mm)
1488 : : return -ENOENT;
1489 : 2676 : exe_file = get_mm_exe_file(mm);
1490 : 2676 : mmput(mm);
1491 [ + - ]: 2676 : if (exe_file) {
1492 : 2676 : *exe_path = exe_file->f_path;
1493 : 2676 : path_get(&exe_file->f_path);
1494 : 2676 : fput(exe_file);
1495 : 2676 : return 0;
1496 : : } else
1497 : : return -ENOENT;
1498 : : }
1499 : :
1500 : 0 : static void *proc_pid_follow_link(struct dentry *dentry, struct nameidata *nd)
1501 : : {
1502 : 2444 : struct inode *inode = dentry->d_inode;
1503 : : struct path path;
1504 : : int error = -EACCES;
1505 : :
1506 : : /* Are we allowed to snoop on the tasks file descriptors? */
1507 [ + - ]: 2444 : if (!proc_fd_access_allowed(inode))
1508 : : goto out;
1509 : :
1510 : 2444 : error = PROC_I(inode)->op.proc_get_link(dentry, &path);
1511 [ + - ]: 2444 : if (error)
1512 : : goto out;
1513 : :
1514 : 2444 : nd_jump_link(nd, &path);
1515 : 2444 : return NULL;
1516 : : out:
1517 : 0 : return ERR_PTR(error);
1518 : : }
1519 : :
1520 : 0 : static int do_proc_readlink(struct path *path, char __user *buffer, int buflen)
1521 : : {
1522 : 6847 : char *tmp = (char*)__get_free_page(GFP_TEMPORARY);
1523 : : char *pathname;
1524 : : int len;
1525 : :
1526 [ + - ]: 6847 : if (!tmp)
1527 : : return -ENOMEM;
1528 : :
1529 : 6847 : pathname = d_path(path, tmp, PAGE_SIZE);
1530 : : len = PTR_ERR(pathname);
1531 [ + - ]: 6847 : if (IS_ERR(pathname))
1532 : : goto out;
1533 : 6847 : len = tmp + PAGE_SIZE - 1 - pathname;
1534 : :
1535 [ - + ]: 6847 : if (len > buflen)
1536 : : len = buflen;
1537 [ - + ]: 13694 : if (copy_to_user(buffer, pathname, len))
1538 : : len = -EFAULT;
1539 : : out:
1540 : 6847 : free_page((unsigned long)tmp);
1541 : 6847 : return len;
1542 : : }
1543 : :
1544 : 0 : static int proc_pid_readlink(struct dentry * dentry, char __user * buffer, int buflen)
1545 : : {
1546 : : int error = -EACCES;
1547 : 7091 : struct inode *inode = dentry->d_inode;
1548 : : struct path path;
1549 : :
1550 : : /* Are we allowed to snoop on the tasks file descriptors? */
1551 [ + - ]: 7091 : if (!proc_fd_access_allowed(inode))
1552 : : goto out;
1553 : :
1554 : 7091 : error = PROC_I(inode)->op.proc_get_link(dentry, &path);
1555 [ + + ]: 7091 : if (error)
1556 : : goto out;
1557 : :
1558 : 6847 : error = do_proc_readlink(&path, buffer, buflen);
1559 : 6847 : path_put(&path);
1560 : : out:
1561 : 0 : return error;
1562 : : }
1563 : :
1564 : : const struct inode_operations proc_pid_link_inode_operations = {
1565 : : .readlink = proc_pid_readlink,
1566 : : .follow_link = proc_pid_follow_link,
1567 : : .setattr = proc_setattr,
1568 : : };
1569 : :
1570 : :
1571 : : /* building an inode */
1572 : :
1573 : 0 : struct inode *proc_pid_make_inode(struct super_block * sb, struct task_struct *task)
1574 : : {
1575 : : struct inode * inode;
1576 : : struct proc_inode *ei;
1577 : : const struct cred *cred;
1578 : :
1579 : : /* We need a new inode */
1580 : :
1581 : 14648 : inode = new_inode(sb);
1582 [ + - ]: 14648 : if (!inode)
1583 : : goto out;
1584 : :
1585 : : /* Common stuff */
1586 : : ei = PROC_I(inode);
1587 : 14648 : inode->i_ino = get_next_ino();
1588 : 14648 : inode->i_mtime = inode->i_atime = inode->i_ctime = CURRENT_TIME;
1589 : 14648 : inode->i_op = &proc_def_inode_operations;
1590 : :
1591 : : /*
1592 : : * grab the reference to task.
1593 : : */
1594 : 14648 : ei->pid = get_task_pid(task, PIDTYPE_PID);
1595 [ + - ]: 14648 : if (!ei->pid)
1596 : : goto out_unlock;
1597 : :
1598 [ + + ]: 14648 : if (task_dumpable(task)) {
1599 : : rcu_read_lock();
1600 : 7342 : cred = __task_cred(task);
1601 : 7342 : inode->i_uid = cred->euid;
1602 : 7342 : inode->i_gid = cred->egid;
1603 : : rcu_read_unlock();
1604 : : }
1605 : 14648 : security_task_to_inode(task, inode);
1606 : :
1607 : : out:
1608 : 14648 : return inode;
1609 : :
1610 : : out_unlock:
1611 : 0 : iput(inode);
1612 : 0 : return NULL;
1613 : : }
1614 : :
1615 : 0 : int pid_getattr(struct vfsmount *mnt, struct dentry *dentry, struct kstat *stat)
1616 : : {
1617 : 23781 : struct inode *inode = dentry->d_inode;
1618 : : struct task_struct *task;
1619 : : const struct cred *cred;
1620 : 47562 : struct pid_namespace *pid = dentry->d_sb->s_fs_info;
1621 : :
1622 : 23781 : generic_fillattr(inode, stat);
1623 : :
1624 : : rcu_read_lock();
1625 : 23781 : stat->uid = GLOBAL_ROOT_UID;
1626 : 23781 : stat->gid = GLOBAL_ROOT_GID;
1627 : 23781 : task = pid_task(proc_pid(inode), PIDTYPE_PID);
1628 [ + - ]: 23781 : if (task) {
1629 [ - + ]: 23781 : if (!has_pid_permissions(pid, task, 2)) {
1630 : : rcu_read_unlock();
1631 : : /*
1632 : : * This doesn't prevent learning whether PID exists,
1633 : : * it only makes getattr() consistent with readdir().
1634 : : */
1635 : 0 : return -ENOENT;
1636 : : }
1637 [ + + ][ + - ]: 23783 : if ((inode->i_mode == (S_IFDIR|S_IRUGO|S_IXUGO)) ||
1638 : : task_dumpable(task)) {
1639 : 23781 : cred = __task_cred(task);
1640 : 23781 : stat->uid = cred->euid;
1641 : 23781 : stat->gid = cred->egid;
1642 : : }
1643 : : }
1644 : : rcu_read_unlock();
1645 : 23781 : return 0;
1646 : : }
1647 : :
1648 : : /* dentry stuff */
1649 : :
1650 : : /*
1651 : : * Exceptional case: normally we are not allowed to unhash a busy
1652 : : * directory. In this case, however, we can do it - no aliasing problems
1653 : : * due to the way we treat inodes.
1654 : : *
1655 : : * Rewrite the inode's ownerships here because the owning task may have
1656 : : * performed a setuid(), etc.
1657 : : *
1658 : : * Before the /proc/pid/status file was created the only way to read
1659 : : * the effective uid of a /process was to stat /proc/pid. Reading
1660 : : * /proc/pid/status is slow enough that procps and other packages
1661 : : * kept stating /proc/pid. To keep the rules in /proc simple I have
1662 : : * made this apply to all per process world readable and executable
1663 : : * directories.
1664 : : */
1665 : 0 : int pid_revalidate(struct dentry *dentry, unsigned int flags)
1666 : : {
1667 : : struct inode *inode;
1668 : : struct task_struct *task;
1669 : : const struct cred *cred;
1670 : :
1671 [ + + ]: 235635 : if (flags & LOOKUP_RCU)
1672 : : return -ECHILD;
1673 : :
1674 : 153035 : inode = dentry->d_inode;
1675 : : task = get_proc_task(inode);
1676 : :
1677 [ + - ]: 153036 : if (task) {
1678 [ + + ][ + + ]: 215076 : if ((inode->i_mode == (S_IFDIR|S_IRUGO|S_IXUGO)) ||
1679 : : task_dumpable(task)) {
1680 : : rcu_read_lock();
1681 : 114446 : cred = __task_cred(task);
1682 : 114446 : inode->i_uid = cred->euid;
1683 : 114446 : inode->i_gid = cred->egid;
1684 : : rcu_read_unlock();
1685 : : } else {
1686 : 38590 : inode->i_uid = GLOBAL_ROOT_UID;
1687 : 38590 : inode->i_gid = GLOBAL_ROOT_GID;
1688 : : }
1689 : 153036 : inode->i_mode &= ~(S_ISUID | S_ISGID);
1690 : 153036 : security_task_to_inode(task, inode);
1691 : : put_task_struct(task);
1692 : : return 1;
1693 : : }
1694 : 0 : d_drop(dentry);
1695 : 0 : return 0;
1696 : : }
1697 : :
1698 : : static inline bool proc_inode_is_dead(struct inode *inode)
1699 : : {
1700 : 71906 : return !proc_pid(inode)->tasks[PIDTYPE_PID].first;
1701 : : }
1702 : :
1703 : 0 : int pid_delete_dentry(const struct dentry *dentry)
1704 : : {
1705 : : /* Is the task we represent dead?
1706 : : * If so, then don't put the dentry on the lru list,
1707 : : * kill it immediately.
1708 : : */
1709 : 139228 : return proc_inode_is_dead(dentry->d_inode);
1710 : : }
1711 : :
1712 : : const struct dentry_operations pid_dentry_operations =
1713 : : {
1714 : : .d_revalidate = pid_revalidate,
1715 : : .d_delete = pid_delete_dentry,
1716 : : };
1717 : :
1718 : : /* Lookups */
1719 : :
1720 : : /*
1721 : : * Fill a directory entry.
1722 : : *
1723 : : * If possible create the dcache entry and derive our inode number and
1724 : : * file type from dcache entry.
1725 : : *
1726 : : * Since all of the proc inode numbers are dynamically generated, the inode
1727 : : * numbers do not exist until the inode is cache. This means creating the
1728 : : * the dcache entry in readdir is necessary to keep the inode numbers
1729 : : * reported by readdir in sync with the inode numbers reported
1730 : : * by stat.
1731 : : */
1732 : 0 : bool proc_fill_cache(struct file *file, struct dir_context *ctx,
1733 : : const char *name, int len,
1734 : : instantiate_t instantiate, struct task_struct *task, const void *ptr)
1735 : : {
1736 : 31655 : struct dentry *child, *dir = file->f_path.dentry;
1737 : 31655 : struct qstr qname = QSTR_INIT(name, len);
1738 : : struct inode *inode;
1739 : : unsigned type;
1740 : : ino_t ino;
1741 : :
1742 : 31655 : child = d_hash_and_lookup(dir, &qname);
1743 [ + + ]: 31655 : if (!child) {
1744 : 5822 : child = d_alloc(dir, &qname);
1745 [ + - ]: 5822 : if (!child)
1746 : : goto end_instantiate;
1747 [ - + ]: 5822 : if (instantiate(dir->d_inode, child, task, ptr) < 0) {
1748 : 0 : dput(child);
1749 : 0 : goto end_instantiate;
1750 : : }
1751 : : }
1752 : 31655 : inode = child->d_inode;
1753 : 31655 : ino = inode->i_ino;
1754 : 31655 : type = inode->i_mode >> 12;
1755 : 31655 : dput(child);
1756 : 31655 : return dir_emit(ctx, name, len, ino, type);
1757 : :
1758 : : end_instantiate:
1759 : 0 : return dir_emit(ctx, name, len, 1, DT_UNKNOWN);
1760 : : }
1761 : :
1762 : : #ifdef CONFIG_CHECKPOINT_RESTORE
1763 : :
1764 : : /*
1765 : : * dname_to_vma_addr - maps a dentry name into two unsigned longs
1766 : : * which represent vma start and end addresses.
1767 : : */
1768 : : static int dname_to_vma_addr(struct dentry *dentry,
1769 : : unsigned long *start, unsigned long *end)
1770 : : {
1771 : : if (sscanf(dentry->d_name.name, "%lx-%lx", start, end) != 2)
1772 : : return -EINVAL;
1773 : :
1774 : : return 0;
1775 : : }
1776 : :
1777 : : static int map_files_d_revalidate(struct dentry *dentry, unsigned int flags)
1778 : : {
1779 : : unsigned long vm_start, vm_end;
1780 : : bool exact_vma_exists = false;
1781 : : struct mm_struct *mm = NULL;
1782 : : struct task_struct *task;
1783 : : const struct cred *cred;
1784 : : struct inode *inode;
1785 : : int status = 0;
1786 : :
1787 : : if (flags & LOOKUP_RCU)
1788 : : return -ECHILD;
1789 : :
1790 : : if (!capable(CAP_SYS_ADMIN)) {
1791 : : status = -EPERM;
1792 : : goto out_notask;
1793 : : }
1794 : :
1795 : : inode = dentry->d_inode;
1796 : : task = get_proc_task(inode);
1797 : : if (!task)
1798 : : goto out_notask;
1799 : :
1800 : : mm = mm_access(task, PTRACE_MODE_READ);
1801 : : if (IS_ERR_OR_NULL(mm))
1802 : : goto out;
1803 : :
1804 : : if (!dname_to_vma_addr(dentry, &vm_start, &vm_end)) {
1805 : : down_read(&mm->mmap_sem);
1806 : : exact_vma_exists = !!find_exact_vma(mm, vm_start, vm_end);
1807 : : up_read(&mm->mmap_sem);
1808 : : }
1809 : :
1810 : : mmput(mm);
1811 : :
1812 : : if (exact_vma_exists) {
1813 : : if (task_dumpable(task)) {
1814 : : rcu_read_lock();
1815 : : cred = __task_cred(task);
1816 : : inode->i_uid = cred->euid;
1817 : : inode->i_gid = cred->egid;
1818 : : rcu_read_unlock();
1819 : : } else {
1820 : : inode->i_uid = GLOBAL_ROOT_UID;
1821 : : inode->i_gid = GLOBAL_ROOT_GID;
1822 : : }
1823 : : security_task_to_inode(task, inode);
1824 : : status = 1;
1825 : : }
1826 : :
1827 : : out:
1828 : : put_task_struct(task);
1829 : :
1830 : : out_notask:
1831 : : if (status <= 0)
1832 : : d_drop(dentry);
1833 : :
1834 : : return status;
1835 : : }
1836 : :
1837 : : static const struct dentry_operations tid_map_files_dentry_operations = {
1838 : : .d_revalidate = map_files_d_revalidate,
1839 : : .d_delete = pid_delete_dentry,
1840 : : };
1841 : :
1842 : : static int proc_map_files_get_link(struct dentry *dentry, struct path *path)
1843 : : {
1844 : : unsigned long vm_start, vm_end;
1845 : : struct vm_area_struct *vma;
1846 : : struct task_struct *task;
1847 : : struct mm_struct *mm;
1848 : : int rc;
1849 : :
1850 : : rc = -ENOENT;
1851 : : task = get_proc_task(dentry->d_inode);
1852 : : if (!task)
1853 : : goto out;
1854 : :
1855 : : mm = get_task_mm(task);
1856 : : put_task_struct(task);
1857 : : if (!mm)
1858 : : goto out;
1859 : :
1860 : : rc = dname_to_vma_addr(dentry, &vm_start, &vm_end);
1861 : : if (rc)
1862 : : goto out_mmput;
1863 : :
1864 : : rc = -ENOENT;
1865 : : down_read(&mm->mmap_sem);
1866 : : vma = find_exact_vma(mm, vm_start, vm_end);
1867 : : if (vma && vma->vm_file) {
1868 : : *path = vma->vm_file->f_path;
1869 : : path_get(path);
1870 : : rc = 0;
1871 : : }
1872 : : up_read(&mm->mmap_sem);
1873 : :
1874 : : out_mmput:
1875 : : mmput(mm);
1876 : : out:
1877 : : return rc;
1878 : : }
1879 : :
1880 : : struct map_files_info {
1881 : : fmode_t mode;
1882 : : unsigned long len;
1883 : : unsigned char name[4*sizeof(long)+2]; /* max: %lx-%lx\0 */
1884 : : };
1885 : :
1886 : : static int
1887 : : proc_map_files_instantiate(struct inode *dir, struct dentry *dentry,
1888 : : struct task_struct *task, const void *ptr)
1889 : : {
1890 : : fmode_t mode = (fmode_t)(unsigned long)ptr;
1891 : : struct proc_inode *ei;
1892 : : struct inode *inode;
1893 : :
1894 : : inode = proc_pid_make_inode(dir->i_sb, task);
1895 : : if (!inode)
1896 : : return -ENOENT;
1897 : :
1898 : : ei = PROC_I(inode);
1899 : : ei->op.proc_get_link = proc_map_files_get_link;
1900 : :
1901 : : inode->i_op = &proc_pid_link_inode_operations;
1902 : : inode->i_size = 64;
1903 : : inode->i_mode = S_IFLNK;
1904 : :
1905 : : if (mode & FMODE_READ)
1906 : : inode->i_mode |= S_IRUSR;
1907 : : if (mode & FMODE_WRITE)
1908 : : inode->i_mode |= S_IWUSR;
1909 : :
1910 : : d_set_d_op(dentry, &tid_map_files_dentry_operations);
1911 : : d_add(dentry, inode);
1912 : :
1913 : : return 0;
1914 : : }
1915 : :
1916 : : static struct dentry *proc_map_files_lookup(struct inode *dir,
1917 : : struct dentry *dentry, unsigned int flags)
1918 : : {
1919 : : unsigned long vm_start, vm_end;
1920 : : struct vm_area_struct *vma;
1921 : : struct task_struct *task;
1922 : : int result;
1923 : : struct mm_struct *mm;
1924 : :
1925 : : result = -EPERM;
1926 : : if (!capable(CAP_SYS_ADMIN))
1927 : : goto out;
1928 : :
1929 : : result = -ENOENT;
1930 : : task = get_proc_task(dir);
1931 : : if (!task)
1932 : : goto out;
1933 : :
1934 : : result = -EACCES;
1935 : : if (!ptrace_may_access(task, PTRACE_MODE_READ))
1936 : : goto out_put_task;
1937 : :
1938 : : result = -ENOENT;
1939 : : if (dname_to_vma_addr(dentry, &vm_start, &vm_end))
1940 : : goto out_put_task;
1941 : :
1942 : : mm = get_task_mm(task);
1943 : : if (!mm)
1944 : : goto out_put_task;
1945 : :
1946 : : down_read(&mm->mmap_sem);
1947 : : vma = find_exact_vma(mm, vm_start, vm_end);
1948 : : if (!vma)
1949 : : goto out_no_vma;
1950 : :
1951 : : if (vma->vm_file)
1952 : : result = proc_map_files_instantiate(dir, dentry, task,
1953 : : (void *)(unsigned long)vma->vm_file->f_mode);
1954 : :
1955 : : out_no_vma:
1956 : : up_read(&mm->mmap_sem);
1957 : : mmput(mm);
1958 : : out_put_task:
1959 : : put_task_struct(task);
1960 : : out:
1961 : : return ERR_PTR(result);
1962 : : }
1963 : :
1964 : : static const struct inode_operations proc_map_files_inode_operations = {
1965 : : .lookup = proc_map_files_lookup,
1966 : : .permission = proc_fd_permission,
1967 : : .setattr = proc_setattr,
1968 : : };
1969 : :
1970 : : static int
1971 : : proc_map_files_readdir(struct file *file, struct dir_context *ctx)
1972 : : {
1973 : : struct vm_area_struct *vma;
1974 : : struct task_struct *task;
1975 : : struct mm_struct *mm;
1976 : : unsigned long nr_files, pos, i;
1977 : : struct flex_array *fa = NULL;
1978 : : struct map_files_info info;
1979 : : struct map_files_info *p;
1980 : : int ret;
1981 : :
1982 : : ret = -EPERM;
1983 : : if (!capable(CAP_SYS_ADMIN))
1984 : : goto out;
1985 : :
1986 : : ret = -ENOENT;
1987 : : task = get_proc_task(file_inode(file));
1988 : : if (!task)
1989 : : goto out;
1990 : :
1991 : : ret = -EACCES;
1992 : : if (!ptrace_may_access(task, PTRACE_MODE_READ))
1993 : : goto out_put_task;
1994 : :
1995 : : ret = 0;
1996 : : if (!dir_emit_dots(file, ctx))
1997 : : goto out_put_task;
1998 : :
1999 : : mm = get_task_mm(task);
2000 : : if (!mm)
2001 : : goto out_put_task;
2002 : : down_read(&mm->mmap_sem);
2003 : :
2004 : : nr_files = 0;
2005 : :
2006 : : /*
2007 : : * We need two passes here:
2008 : : *
2009 : : * 1) Collect vmas of mapped files with mmap_sem taken
2010 : : * 2) Release mmap_sem and instantiate entries
2011 : : *
2012 : : * otherwise we get lockdep complained, since filldir()
2013 : : * routine might require mmap_sem taken in might_fault().
2014 : : */
2015 : :
2016 : : for (vma = mm->mmap, pos = 2; vma; vma = vma->vm_next) {
2017 : : if (vma->vm_file && ++pos > ctx->pos)
2018 : : nr_files++;
2019 : : }
2020 : :
2021 : : if (nr_files) {
2022 : : fa = flex_array_alloc(sizeof(info), nr_files,
2023 : : GFP_KERNEL);
2024 : : if (!fa || flex_array_prealloc(fa, 0, nr_files,
2025 : : GFP_KERNEL)) {
2026 : : ret = -ENOMEM;
2027 : : if (fa)
2028 : : flex_array_free(fa);
2029 : : up_read(&mm->mmap_sem);
2030 : : mmput(mm);
2031 : : goto out_put_task;
2032 : : }
2033 : : for (i = 0, vma = mm->mmap, pos = 2; vma;
2034 : : vma = vma->vm_next) {
2035 : : if (!vma->vm_file)
2036 : : continue;
2037 : : if (++pos <= ctx->pos)
2038 : : continue;
2039 : :
2040 : : info.mode = vma->vm_file->f_mode;
2041 : : info.len = snprintf(info.name,
2042 : : sizeof(info.name), "%lx-%lx",
2043 : : vma->vm_start, vma->vm_end);
2044 : : if (flex_array_put(fa, i++, &info, GFP_KERNEL))
2045 : : BUG();
2046 : : }
2047 : : }
2048 : : up_read(&mm->mmap_sem);
2049 : :
2050 : : for (i = 0; i < nr_files; i++) {
2051 : : p = flex_array_get(fa, i);
2052 : : if (!proc_fill_cache(file, ctx,
2053 : : p->name, p->len,
2054 : : proc_map_files_instantiate,
2055 : : task,
2056 : : (void *)(unsigned long)p->mode))
2057 : : break;
2058 : : ctx->pos++;
2059 : : }
2060 : : if (fa)
2061 : : flex_array_free(fa);
2062 : : mmput(mm);
2063 : :
2064 : : out_put_task:
2065 : : put_task_struct(task);
2066 : : out:
2067 : : return ret;
2068 : : }
2069 : :
2070 : : static const struct file_operations proc_map_files_operations = {
2071 : : .read = generic_read_dir,
2072 : : .iterate = proc_map_files_readdir,
2073 : : .llseek = default_llseek,
2074 : : };
2075 : :
2076 : : struct timers_private {
2077 : : struct pid *pid;
2078 : : struct task_struct *task;
2079 : : struct sighand_struct *sighand;
2080 : : struct pid_namespace *ns;
2081 : : unsigned long flags;
2082 : : };
2083 : :
2084 : : static void *timers_start(struct seq_file *m, loff_t *pos)
2085 : : {
2086 : : struct timers_private *tp = m->private;
2087 : :
2088 : : tp->task = get_pid_task(tp->pid, PIDTYPE_PID);
2089 : : if (!tp->task)
2090 : : return ERR_PTR(-ESRCH);
2091 : :
2092 : : tp->sighand = lock_task_sighand(tp->task, &tp->flags);
2093 : : if (!tp->sighand)
2094 : : return ERR_PTR(-ESRCH);
2095 : :
2096 : : return seq_list_start(&tp->task->signal->posix_timers, *pos);
2097 : : }
2098 : :
2099 : : static void *timers_next(struct seq_file *m, void *v, loff_t *pos)
2100 : : {
2101 : : struct timers_private *tp = m->private;
2102 : : return seq_list_next(v, &tp->task->signal->posix_timers, pos);
2103 : : }
2104 : :
2105 : : static void timers_stop(struct seq_file *m, void *v)
2106 : : {
2107 : : struct timers_private *tp = m->private;
2108 : :
2109 : : if (tp->sighand) {
2110 : : unlock_task_sighand(tp->task, &tp->flags);
2111 : : tp->sighand = NULL;
2112 : : }
2113 : :
2114 : : if (tp->task) {
2115 : : put_task_struct(tp->task);
2116 : : tp->task = NULL;
2117 : : }
2118 : : }
2119 : :
2120 : : static int show_timer(struct seq_file *m, void *v)
2121 : : {
2122 : : struct k_itimer *timer;
2123 : : struct timers_private *tp = m->private;
2124 : : int notify;
2125 : : static char *nstr[] = {
2126 : : [SIGEV_SIGNAL] = "signal",
2127 : : [SIGEV_NONE] = "none",
2128 : : [SIGEV_THREAD] = "thread",
2129 : : };
2130 : :
2131 : : timer = list_entry((struct list_head *)v, struct k_itimer, list);
2132 : : notify = timer->it_sigev_notify;
2133 : :
2134 : : seq_printf(m, "ID: %d\n", timer->it_id);
2135 : : seq_printf(m, "signal: %d/%p\n", timer->sigq->info.si_signo,
2136 : : timer->sigq->info.si_value.sival_ptr);
2137 : : seq_printf(m, "notify: %s/%s.%d\n",
2138 : : nstr[notify & ~SIGEV_THREAD_ID],
2139 : : (notify & SIGEV_THREAD_ID) ? "tid" : "pid",
2140 : : pid_nr_ns(timer->it_pid, tp->ns));
2141 : : seq_printf(m, "ClockID: %d\n", timer->it_clock);
2142 : :
2143 : : return 0;
2144 : : }
2145 : :
2146 : : static const struct seq_operations proc_timers_seq_ops = {
2147 : : .start = timers_start,
2148 : : .next = timers_next,
2149 : : .stop = timers_stop,
2150 : : .show = show_timer,
2151 : : };
2152 : :
2153 : : static int proc_timers_open(struct inode *inode, struct file *file)
2154 : : {
2155 : : struct timers_private *tp;
2156 : :
2157 : : tp = __seq_open_private(file, &proc_timers_seq_ops,
2158 : : sizeof(struct timers_private));
2159 : : if (!tp)
2160 : : return -ENOMEM;
2161 : :
2162 : : tp->pid = proc_pid(inode);
2163 : : tp->ns = inode->i_sb->s_fs_info;
2164 : : return 0;
2165 : : }
2166 : :
2167 : : static const struct file_operations proc_timers_operations = {
2168 : : .open = proc_timers_open,
2169 : : .read = seq_read,
2170 : : .llseek = seq_lseek,
2171 : : .release = seq_release_private,
2172 : : };
2173 : : #endif /* CONFIG_CHECKPOINT_RESTORE */
2174 : :
2175 : 0 : static int proc_pident_instantiate(struct inode *dir,
2176 : : struct dentry *dentry, struct task_struct *task, const void *ptr)
2177 : : {
2178 : : const struct pid_entry *p = ptr;
2179 : : struct inode *inode;
2180 : : struct proc_inode *ei;
2181 : :
2182 : 6158 : inode = proc_pid_make_inode(dir->i_sb, task);
2183 [ + - ]: 6158 : if (!inode)
2184 : : goto out;
2185 : :
2186 : : ei = PROC_I(inode);
2187 : 6158 : inode->i_mode = p->mode;
2188 [ + + ]: 6158 : if (S_ISDIR(inode->i_mode))
2189 : 2070 : set_nlink(inode, 2); /* Use getattr to fix if necessary */
2190 [ + + ]: 12316 : if (p->iop)
2191 : 3214 : inode->i_op = p->iop;
2192 [ + + ]: 6158 : if (p->fop)
2193 : 5015 : inode->i_fop = p->fop;
2194 : 6158 : ei->op = p->op;
2195 : 6158 : d_set_d_op(dentry, &pid_dentry_operations);
2196 : : d_add(dentry, inode);
2197 : : /* Close the race of the process dying before we return the dentry */
2198 [ - + ]: 6158 : if (pid_revalidate(dentry, 0))
2199 : : return 0;
2200 : : out:
2201 : : return -ENOENT;
2202 : : }
2203 : :
2204 : 0 : static struct dentry *proc_pident_lookup(struct inode *dir,
2205 : : struct dentry *dentry,
2206 : : const struct pid_entry *ents,
2207 : : unsigned int nents)
2208 : : {
2209 : : int error;
2210 : : struct task_struct *task = get_proc_task(dir);
2211 : : const struct pid_entry *p, *last;
2212 : :
2213 : : error = -ENOENT;
2214 : :
2215 [ + - ]: 6081 : if (!task)
2216 : : goto out_no_task;
2217 : :
2218 : : /*
2219 : : * Yes, it does not scale. And it should not. Don't add
2220 : : * new entries into /proc/<tgid>/ without very good reasons.
2221 : : */
2222 : 6081 : last = &ents[nents - 1];
2223 [ + + ]: 64245 : for (p = ents; p <= last; p++) {
2224 [ + + ]: 64238 : if (p->len != dentry->d_name.len)
2225 : 47535 : continue;
2226 [ + + ]: 16703 : if (!memcmp(dentry->d_name.name, p->name, p->len))
2227 : : break;
2228 : : }
2229 [ + + ]: 6081 : if (p > last)
2230 : : goto out;
2231 : :
2232 : 6074 : error = proc_pident_instantiate(dir, dentry, task, p);
2233 : : out:
2234 : : put_task_struct(task);
2235 : : out_no_task:
2236 : 6081 : return ERR_PTR(error);
2237 : : }
2238 : :
2239 : 0 : static int proc_pident_readdir(struct file *file, struct dir_context *ctx,
2240 : : const struct pid_entry *ents, unsigned int nents)
2241 : : {
2242 : : struct task_struct *task = get_proc_task(file_inode(file));
2243 : : const struct pid_entry *p;
2244 : :
2245 [ + - ]: 8 : if (!task)
2246 : : return -ENOENT;
2247 : :
2248 [ + - ]: 8 : if (!dir_emit_dots(file, ctx))
2249 : : goto out;
2250 : :
2251 [ + + ]: 8 : if (ctx->pos >= nents + 2)
2252 : : goto out;
2253 : :
2254 [ + + ]: 88 : for (p = ents + (ctx->pos - 2); p <= ents + nents - 1; p++) {
2255 [ + - ]: 84 : if (!proc_fill_cache(file, ctx, p->name, p->len,
2256 : : proc_pident_instantiate, task, p))
2257 : : break;
2258 : 84 : ctx->pos++;
2259 : : }
2260 : : out:
2261 : : put_task_struct(task);
2262 : : return 0;
2263 : : }
2264 : :
2265 : : #ifdef CONFIG_SECURITY
2266 : 0 : static ssize_t proc_pid_attr_read(struct file * file, char __user * buf,
2267 : : size_t count, loff_t *ppos)
2268 : : {
2269 : : struct inode * inode = file_inode(file);
2270 : 14 : char *p = NULL;
2271 : : ssize_t length;
2272 : : struct task_struct *task = get_proc_task(inode);
2273 : :
2274 [ + - ]: 14 : if (!task)
2275 : : return -ESRCH;
2276 : :
2277 : 14 : length = security_getprocattr(task,
2278 : 14 : (char*)file->f_path.dentry->d_name.name,
2279 : : &p);
2280 : : put_task_struct(task);
2281 [ + + ]: 14 : if (length > 0)
2282 : 4 : length = simple_read_from_buffer(buf, count, ppos, p, length);
2283 : 14 : kfree(p);
2284 : 14 : return length;
2285 : : }
2286 : :
2287 : 0 : static ssize_t proc_pid_attr_write(struct file * file, const char __user * buf,
2288 : : size_t count, loff_t *ppos)
2289 : : {
2290 : : struct inode * inode = file_inode(file);
2291 : : char *page;
2292 : : ssize_t length;
2293 : : struct task_struct *task = get_proc_task(inode);
2294 : :
2295 : : length = -ESRCH;
2296 [ # # ]: 0 : if (!task)
2297 : : goto out_no_task;
2298 [ # # ]: 0 : if (count > PAGE_SIZE)
2299 : : count = PAGE_SIZE;
2300 : :
2301 : : /* No partial writes. */
2302 : : length = -EINVAL;
2303 [ # # ]: 0 : if (*ppos != 0)
2304 : : goto out;
2305 : :
2306 : : length = -ENOMEM;
2307 : 0 : page = (char*)__get_free_page(GFP_TEMPORARY);
2308 [ # # ]: 0 : if (!page)
2309 : : goto out;
2310 : :
2311 : : length = -EFAULT;
2312 [ # # ]: 0 : if (copy_from_user(page, buf, count))
2313 : : goto out_free;
2314 : :
2315 : : /* Guard against adverse ptrace interaction */
2316 : 0 : length = mutex_lock_interruptible(&task->signal->cred_guard_mutex);
2317 [ # # ]: 0 : if (length < 0)
2318 : : goto out_free;
2319 : :
2320 : 0 : length = security_setprocattr(task,
2321 : 0 : (char*)file->f_path.dentry->d_name.name,
2322 : : (void*)page, count);
2323 : 0 : mutex_unlock(&task->signal->cred_guard_mutex);
2324 : : out_free:
2325 : 0 : free_page((unsigned long) page);
2326 : : out:
2327 : : put_task_struct(task);
2328 : : out_no_task:
2329 : 0 : return length;
2330 : : }
2331 : :
2332 : : static const struct file_operations proc_pid_attr_operations = {
2333 : : .read = proc_pid_attr_read,
2334 : : .write = proc_pid_attr_write,
2335 : : .llseek = generic_file_llseek,
2336 : : };
2337 : :
2338 : : static const struct pid_entry attr_dir_stuff[] = {
2339 : : REG("current", S_IRUGO|S_IWUGO, proc_pid_attr_operations),
2340 : : REG("prev", S_IRUGO, proc_pid_attr_operations),
2341 : : REG("exec", S_IRUGO|S_IWUGO, proc_pid_attr_operations),
2342 : : REG("fscreate", S_IRUGO|S_IWUGO, proc_pid_attr_operations),
2343 : : REG("keycreate", S_IRUGO|S_IWUGO, proc_pid_attr_operations),
2344 : : REG("sockcreate", S_IRUGO|S_IWUGO, proc_pid_attr_operations),
2345 : : };
2346 : :
2347 : 0 : static int proc_attr_dir_readdir(struct file *file, struct dir_context *ctx)
2348 : : {
2349 : 4 : return proc_pident_readdir(file, ctx,
2350 : : attr_dir_stuff, ARRAY_SIZE(attr_dir_stuff));
2351 : : }
2352 : :
2353 : : static const struct file_operations proc_attr_dir_operations = {
2354 : : .read = generic_read_dir,
2355 : : .iterate = proc_attr_dir_readdir,
2356 : : .llseek = default_llseek,
2357 : : };
2358 : :
2359 : 0 : static struct dentry *proc_attr_dir_lookup(struct inode *dir,
2360 : : struct dentry *dentry, unsigned int flags)
2361 : : {
2362 : 0 : return proc_pident_lookup(dir, dentry,
2363 : : attr_dir_stuff, ARRAY_SIZE(attr_dir_stuff));
2364 : : }
2365 : :
2366 : : static const struct inode_operations proc_attr_dir_inode_operations = {
2367 : : .lookup = proc_attr_dir_lookup,
2368 : : .getattr = pid_getattr,
2369 : : .setattr = proc_setattr,
2370 : : };
2371 : :
2372 : : #endif
2373 : :
2374 : : #ifdef CONFIG_ELF_CORE
2375 : 0 : static ssize_t proc_coredump_filter_read(struct file *file, char __user *buf,
2376 : : size_t count, loff_t *ppos)
2377 : : {
2378 : : struct task_struct *task = get_proc_task(file_inode(file));
2379 : : struct mm_struct *mm;
2380 : : char buffer[PROC_NUMBUF];
2381 : : size_t len;
2382 : : int ret;
2383 : :
2384 [ + - ]: 2 : if (!task)
2385 : : return -ESRCH;
2386 : :
2387 : : ret = 0;
2388 : 2 : mm = get_task_mm(task);
2389 [ + - ]: 2 : if (mm) {
2390 : 2 : len = snprintf(buffer, sizeof(buffer), "%08lx\n",
2391 : 2 : ((mm->flags & MMF_DUMP_FILTER_MASK) >>
2392 : : MMF_DUMP_FILTER_SHIFT));
2393 : 2 : mmput(mm);
2394 : 2 : ret = simple_read_from_buffer(buf, count, ppos, buffer, len);
2395 : : }
2396 : :
2397 : : put_task_struct(task);
2398 : :
2399 : 2 : return ret;
2400 : : }
2401 : :
2402 : 0 : static ssize_t proc_coredump_filter_write(struct file *file,
2403 : : const char __user *buf,
2404 : : size_t count,
2405 : : loff_t *ppos)
2406 : : {
2407 : : struct task_struct *task;
2408 : : struct mm_struct *mm;
2409 : : char buffer[PROC_NUMBUF], *end;
2410 : : unsigned int val;
2411 : : int ret;
2412 : : int i;
2413 : : unsigned long mask;
2414 : :
2415 : : ret = -EFAULT;
2416 : 0 : memset(buffer, 0, sizeof(buffer));
2417 [ # # ]: 0 : if (count > sizeof(buffer) - 1)
2418 : : count = sizeof(buffer) - 1;
2419 [ # # ]: 0 : if (copy_from_user(buffer, buf, count))
2420 : : goto out_no_task;
2421 : :
2422 : : ret = -EINVAL;
2423 : 0 : val = (unsigned int)simple_strtoul(buffer, &end, 0);
2424 [ # # ]: 0 : if (*end == '\n')
2425 : 0 : end++;
2426 [ # # ]: 0 : if (end - buffer == 0)
2427 : : goto out_no_task;
2428 : :
2429 : : ret = -ESRCH;
2430 : : task = get_proc_task(file_inode(file));
2431 [ # # ]: 0 : if (!task)
2432 : : goto out_no_task;
2433 : :
2434 : 0 : ret = end - buffer;
2435 : 0 : mm = get_task_mm(task);
2436 [ # # ]: 0 : if (!mm)
2437 : : goto out_no_mm;
2438 : :
2439 [ # # ]: 0 : for (i = 0, mask = 1; i < MMF_DUMP_FILTER_BITS; i++, mask <<= 1) {
2440 [ # # ]: 0 : if (val & mask)
2441 : 0 : set_bit(i + MMF_DUMP_FILTER_SHIFT, &mm->flags);
2442 : : else
2443 : 0 : clear_bit(i + MMF_DUMP_FILTER_SHIFT, &mm->flags);
2444 : : }
2445 : :
2446 : 0 : mmput(mm);
2447 : : out_no_mm:
2448 : : put_task_struct(task);
2449 : : out_no_task:
2450 : 0 : return ret;
2451 : : }
2452 : :
2453 : : static const struct file_operations proc_coredump_filter_operations = {
2454 : : .read = proc_coredump_filter_read,
2455 : : .write = proc_coredump_filter_write,
2456 : : .llseek = generic_file_llseek,
2457 : : };
2458 : : #endif
2459 : :
2460 : : #ifdef CONFIG_TASK_IO_ACCOUNTING
2461 : : static int do_io_accounting(struct task_struct *task, char *buffer, int whole)
2462 : : {
2463 : : struct task_io_accounting acct = task->ioac;
2464 : : unsigned long flags;
2465 : : int result;
2466 : :
2467 : : result = mutex_lock_killable(&task->signal->cred_guard_mutex);
2468 : : if (result)
2469 : : return result;
2470 : :
2471 : : if (!ptrace_may_access(task, PTRACE_MODE_READ)) {
2472 : : result = -EACCES;
2473 : : goto out_unlock;
2474 : : }
2475 : :
2476 : : if (whole && lock_task_sighand(task, &flags)) {
2477 : : struct task_struct *t = task;
2478 : :
2479 : : task_io_accounting_add(&acct, &task->signal->ioac);
2480 : : while_each_thread(task, t)
2481 : : task_io_accounting_add(&acct, &t->ioac);
2482 : :
2483 : : unlock_task_sighand(task, &flags);
2484 : : }
2485 : : result = sprintf(buffer,
2486 : : "rchar: %llu\n"
2487 : : "wchar: %llu\n"
2488 : : "syscr: %llu\n"
2489 : : "syscw: %llu\n"
2490 : : "read_bytes: %llu\n"
2491 : : "write_bytes: %llu\n"
2492 : : "cancelled_write_bytes: %llu\n",
2493 : : (unsigned long long)acct.rchar,
2494 : : (unsigned long long)acct.wchar,
2495 : : (unsigned long long)acct.syscr,
2496 : : (unsigned long long)acct.syscw,
2497 : : (unsigned long long)acct.read_bytes,
2498 : : (unsigned long long)acct.write_bytes,
2499 : : (unsigned long long)acct.cancelled_write_bytes);
2500 : : out_unlock:
2501 : : mutex_unlock(&task->signal->cred_guard_mutex);
2502 : : return result;
2503 : : }
2504 : :
2505 : : static int proc_tid_io_accounting(struct task_struct *task, char *buffer)
2506 : : {
2507 : : return do_io_accounting(task, buffer, 0);
2508 : : }
2509 : :
2510 : : static int proc_tgid_io_accounting(struct task_struct *task, char *buffer)
2511 : : {
2512 : : return do_io_accounting(task, buffer, 1);
2513 : : }
2514 : : #endif /* CONFIG_TASK_IO_ACCOUNTING */
2515 : :
2516 : : #ifdef CONFIG_USER_NS
2517 : : static int proc_id_map_open(struct inode *inode, struct file *file,
2518 : : struct seq_operations *seq_ops)
2519 : : {
2520 : : struct user_namespace *ns = NULL;
2521 : : struct task_struct *task;
2522 : : struct seq_file *seq;
2523 : : int ret = -EINVAL;
2524 : :
2525 : : task = get_proc_task(inode);
2526 : : if (task) {
2527 : : rcu_read_lock();
2528 : : ns = get_user_ns(task_cred_xxx(task, user_ns));
2529 : : rcu_read_unlock();
2530 : : put_task_struct(task);
2531 : : }
2532 : : if (!ns)
2533 : : goto err;
2534 : :
2535 : : ret = seq_open(file, seq_ops);
2536 : : if (ret)
2537 : : goto err_put_ns;
2538 : :
2539 : : seq = file->private_data;
2540 : : seq->private = ns;
2541 : :
2542 : : return 0;
2543 : : err_put_ns:
2544 : : put_user_ns(ns);
2545 : : err:
2546 : : return ret;
2547 : : }
2548 : :
2549 : : static int proc_id_map_release(struct inode *inode, struct file *file)
2550 : : {
2551 : : struct seq_file *seq = file->private_data;
2552 : : struct user_namespace *ns = seq->private;
2553 : : put_user_ns(ns);
2554 : : return seq_release(inode, file);
2555 : : }
2556 : :
2557 : : static int proc_uid_map_open(struct inode *inode, struct file *file)
2558 : : {
2559 : : return proc_id_map_open(inode, file, &proc_uid_seq_operations);
2560 : : }
2561 : :
2562 : : static int proc_gid_map_open(struct inode *inode, struct file *file)
2563 : : {
2564 : : return proc_id_map_open(inode, file, &proc_gid_seq_operations);
2565 : : }
2566 : :
2567 : : static int proc_projid_map_open(struct inode *inode, struct file *file)
2568 : : {
2569 : : return proc_id_map_open(inode, file, &proc_projid_seq_operations);
2570 : : }
2571 : :
2572 : : static const struct file_operations proc_uid_map_operations = {
2573 : : .open = proc_uid_map_open,
2574 : : .write = proc_uid_map_write,
2575 : : .read = seq_read,
2576 : : .llseek = seq_lseek,
2577 : : .release = proc_id_map_release,
2578 : : };
2579 : :
2580 : : static const struct file_operations proc_gid_map_operations = {
2581 : : .open = proc_gid_map_open,
2582 : : .write = proc_gid_map_write,
2583 : : .read = seq_read,
2584 : : .llseek = seq_lseek,
2585 : : .release = proc_id_map_release,
2586 : : };
2587 : :
2588 : : static const struct file_operations proc_projid_map_operations = {
2589 : : .open = proc_projid_map_open,
2590 : : .write = proc_projid_map_write,
2591 : : .read = seq_read,
2592 : : .llseek = seq_lseek,
2593 : : .release = proc_id_map_release,
2594 : : };
2595 : : #endif /* CONFIG_USER_NS */
2596 : :
2597 : 0 : static int proc_pid_personality(struct seq_file *m, struct pid_namespace *ns,
2598 : : struct pid *pid, struct task_struct *task)
2599 : : {
2600 : 2 : int err = lock_trace(task);
2601 [ + - ]: 2 : if (!err) {
2602 : 2 : seq_printf(m, "%08x\n", task->personality);
2603 : : unlock_trace(task);
2604 : : }
2605 : 0 : return err;
2606 : : }
2607 : :
2608 : : /*
2609 : : * Thread groups
2610 : : */
2611 : : static const struct file_operations proc_task_operations;
2612 : : static const struct inode_operations proc_task_inode_operations;
2613 : :
2614 : : static const struct pid_entry tgid_base_stuff[] = {
2615 : : DIR("task", S_IRUGO|S_IXUGO, proc_task_inode_operations, proc_task_operations),
2616 : : DIR("fd", S_IRUSR|S_IXUSR, proc_fd_inode_operations, proc_fd_operations),
2617 : : #ifdef CONFIG_CHECKPOINT_RESTORE
2618 : : DIR("map_files", S_IRUSR|S_IXUSR, proc_map_files_inode_operations, proc_map_files_operations),
2619 : : #endif
2620 : : DIR("fdinfo", S_IRUSR|S_IXUSR, proc_fdinfo_inode_operations, proc_fdinfo_operations),
2621 : : DIR("ns", S_IRUSR|S_IXUGO, proc_ns_dir_inode_operations, proc_ns_dir_operations),
2622 : : #ifdef CONFIG_NET
2623 : : DIR("net", S_IRUGO|S_IXUGO, proc_net_inode_operations, proc_net_operations),
2624 : : #endif
2625 : : REG("environ", S_IRUSR, proc_environ_operations),
2626 : : INF("auxv", S_IRUSR, proc_pid_auxv),
2627 : : ONE("status", S_IRUGO, proc_pid_status),
2628 : : ONE("personality", S_IRUGO, proc_pid_personality),
2629 : : INF("limits", S_IRUGO, proc_pid_limits),
2630 : : #ifdef CONFIG_SCHED_DEBUG
2631 : : REG("sched", S_IRUGO|S_IWUSR, proc_pid_sched_operations),
2632 : : #endif
2633 : : #ifdef CONFIG_SCHED_AUTOGROUP
2634 : : REG("autogroup", S_IRUGO|S_IWUSR, proc_pid_sched_autogroup_operations),
2635 : : #endif
2636 : : REG("comm", S_IRUGO|S_IWUSR, proc_pid_set_comm_operations),
2637 : : #ifdef CONFIG_HAVE_ARCH_TRACEHOOK
2638 : : INF("syscall", S_IRUGO, proc_pid_syscall),
2639 : : #endif
2640 : : INF("cmdline", S_IRUGO, proc_pid_cmdline),
2641 : : ONE("stat", S_IRUGO, proc_tgid_stat),
2642 : : ONE("statm", S_IRUGO, proc_pid_statm),
2643 : : REG("maps", S_IRUGO, proc_pid_maps_operations),
2644 : : #ifdef CONFIG_NUMA
2645 : : REG("numa_maps", S_IRUGO, proc_pid_numa_maps_operations),
2646 : : #endif
2647 : : REG("mem", S_IRUSR|S_IWUSR, proc_mem_operations),
2648 : : LNK("cwd", proc_cwd_link),
2649 : : LNK("root", proc_root_link),
2650 : : LNK("exe", proc_exe_link),
2651 : : REG("mounts", S_IRUGO, proc_mounts_operations),
2652 : : REG("mountinfo", S_IRUGO, proc_mountinfo_operations),
2653 : : REG("mountstats", S_IRUSR, proc_mountstats_operations),
2654 : : #ifdef CONFIG_PROC_PAGE_MONITOR
2655 : : REG("clear_refs", S_IWUSR, proc_clear_refs_operations),
2656 : : REG("smaps", S_IRUGO, proc_pid_smaps_operations),
2657 : : REG("pagemap", S_IRUGO, proc_pagemap_operations),
2658 : : #endif
2659 : : #ifdef CONFIG_SECURITY
2660 : : DIR("attr", S_IRUGO|S_IXUGO, proc_attr_dir_inode_operations, proc_attr_dir_operations),
2661 : : #endif
2662 : : #ifdef CONFIG_KALLSYMS
2663 : : INF("wchan", S_IRUGO, proc_pid_wchan),
2664 : : #endif
2665 : : #ifdef CONFIG_STACKTRACE
2666 : : ONE("stack", S_IRUGO, proc_pid_stack),
2667 : : #endif
2668 : : #ifdef CONFIG_SCHEDSTATS
2669 : : INF("schedstat", S_IRUGO, proc_pid_schedstat),
2670 : : #endif
2671 : : #ifdef CONFIG_LATENCYTOP
2672 : : REG("latency", S_IRUGO, proc_lstats_operations),
2673 : : #endif
2674 : : #ifdef CONFIG_PROC_PID_CPUSET
2675 : : REG("cpuset", S_IRUGO, proc_cpuset_operations),
2676 : : #endif
2677 : : #ifdef CONFIG_CGROUPS
2678 : : REG("cgroup", S_IRUGO, proc_cgroup_operations),
2679 : : #endif
2680 : : INF("oom_score", S_IRUGO, proc_oom_score),
2681 : : ANDROID("oom_adj", S_IRUGO|S_IWUSR, oom_adj),
2682 : : REG("oom_score_adj", S_IRUGO|S_IWUSR, proc_oom_score_adj_operations),
2683 : : #ifdef CONFIG_AUDITSYSCALL
2684 : : REG("loginuid", S_IWUSR|S_IRUGO, proc_loginuid_operations),
2685 : : REG("sessionid", S_IRUGO, proc_sessionid_operations),
2686 : : #endif
2687 : : #ifdef CONFIG_FAULT_INJECTION
2688 : : REG("make-it-fail", S_IRUGO|S_IWUSR, proc_fault_inject_operations),
2689 : : #endif
2690 : : #ifdef CONFIG_ELF_CORE
2691 : : REG("coredump_filter", S_IRUGO|S_IWUSR, proc_coredump_filter_operations),
2692 : : #endif
2693 : : #ifdef CONFIG_TASK_IO_ACCOUNTING
2694 : : INF("io", S_IRUSR, proc_tgid_io_accounting),
2695 : : #endif
2696 : : #ifdef CONFIG_HARDWALL
2697 : : INF("hardwall", S_IRUGO, proc_pid_hardwall),
2698 : : #endif
2699 : : #ifdef CONFIG_USER_NS
2700 : : REG("uid_map", S_IRUGO|S_IWUSR, proc_uid_map_operations),
2701 : : REG("gid_map", S_IRUGO|S_IWUSR, proc_gid_map_operations),
2702 : : REG("projid_map", S_IRUGO|S_IWUSR, proc_projid_map_operations),
2703 : : #endif
2704 : : #ifdef CONFIG_CHECKPOINT_RESTORE
2705 : : REG("timers", S_IRUGO, proc_timers_operations),
2706 : : #endif
2707 : : };
2708 : :
2709 : 0 : static int proc_tgid_base_readdir(struct file *file, struct dir_context *ctx)
2710 : : {
2711 : 2 : return proc_pident_readdir(file, ctx,
2712 : : tgid_base_stuff, ARRAY_SIZE(tgid_base_stuff));
2713 : : }
2714 : :
2715 : : static const struct file_operations proc_tgid_base_operations = {
2716 : : .read = generic_read_dir,
2717 : : .iterate = proc_tgid_base_readdir,
2718 : : .llseek = default_llseek,
2719 : : };
2720 : :
2721 : 0 : static struct dentry *proc_tgid_base_lookup(struct inode *dir, struct dentry *dentry, unsigned int flags)
2722 : : {
2723 : 6055 : return proc_pident_lookup(dir, dentry,
2724 : : tgid_base_stuff, ARRAY_SIZE(tgid_base_stuff));
2725 : : }
2726 : :
2727 : : static const struct inode_operations proc_tgid_base_inode_operations = {
2728 : : .lookup = proc_tgid_base_lookup,
2729 : : .getattr = pid_getattr,
2730 : : .setattr = proc_setattr,
2731 : : .permission = proc_pid_permission,
2732 : : };
2733 : :
2734 : 1104220 : static void proc_flush_task_mnt(struct vfsmount *mnt, pid_t pid, pid_t tgid)
2735 : : {
2736 : : struct dentry *dentry, *leader, *dir;
2737 : : char buf[PROC_NUMBUF];
2738 : : struct qstr name;
2739 : :
2740 : 1104220 : name.name = buf;
2741 : 1104220 : name.len = snprintf(buf, sizeof(buf), "%d", pid);
2742 : : /* no ->d_hash() rejects on procfs */
2743 : 1104220 : dentry = d_hash_and_lookup(mnt->mnt_root, &name);
2744 [ + + ]: 1104226 : if (dentry) {
2745 : 2272 : shrink_dcache_parent(dentry);
2746 : 2272 : d_drop(dentry);
2747 : 2272 : dput(dentry);
2748 : : }
2749 : :
2750 : 1104226 : name.name = buf;
2751 : 1104226 : name.len = snprintf(buf, sizeof(buf), "%d", tgid);
2752 : 1104226 : leader = d_hash_and_lookup(mnt->mnt_root, &name);
2753 [ + + ]: 1104228 : if (!leader)
2754 : : goto out;
2755 : :
2756 : 103 : name.name = "task";
2757 : 103 : name.len = strlen(name.name);
2758 : 103 : dir = d_hash_and_lookup(leader, &name);
2759 [ + + ]: 103 : if (!dir)
2760 : : goto out_put_leader;
2761 : :
2762 : 3 : name.name = buf;
2763 : 3 : name.len = snprintf(buf, sizeof(buf), "%d", pid);
2764 : 3 : dentry = d_hash_and_lookup(dir, &name);
2765 [ + - ]: 3 : if (dentry) {
2766 : 3 : shrink_dcache_parent(dentry);
2767 : 3 : d_drop(dentry);
2768 : 3 : dput(dentry);
2769 : : }
2770 : :
2771 : 3 : dput(dir);
2772 : : out_put_leader:
2773 : 103 : dput(leader);
2774 : : out:
2775 : 1104228 : return;
2776 : : }
2777 : :
2778 : : /**
2779 : : * proc_flush_task - Remove dcache entries for @task from the /proc dcache.
2780 : : * @task: task that should be flushed.
2781 : : *
2782 : : * When flushing dentries from proc, one needs to flush them from global
2783 : : * proc (proc_mnt) and from all the namespaces' procs this task was seen
2784 : : * in. This call is supposed to do all of this job.
2785 : : *
2786 : : * Looks in the dcache for
2787 : : * /proc/@pid
2788 : : * /proc/@tgid/task/@pid
2789 : : * if either directory is present flushes it and all of it'ts children
2790 : : * from the dcache.
2791 : : *
2792 : : * It is safe and reasonable to cache /proc entries for a task until
2793 : : * that task exits. After that they just clog up the dcache with
2794 : : * useless entries, possibly causing useful dcache entries to be
2795 : : * flushed instead. This routine is proved to flush those useless
2796 : : * dcache entries at process exit time.
2797 : : *
2798 : : * NOTE: This routine is just an optimization so it does not guarantee
2799 : : * that no dcache entries will exist at process exit time it
2800 : : * just makes it very unlikely that any will persist.
2801 : : */
2802 : :
2803 : 0 : void proc_flush_task(struct task_struct *task)
2804 : : {
2805 : : int i;
2806 : : struct pid *pid, *tgid;
2807 : : struct upid *upid;
2808 : :
2809 : : pid = task_pid(task);
2810 : : tgid = task_tgid(task);
2811 : :
2812 [ + + ]: 2208444 : for (i = 0; i <= pid->level; i++) {
2813 : 1104223 : upid = &pid->numbers[i];
2814 : 1104223 : proc_flush_task_mnt(upid->ns->proc_mnt, upid->nr,
2815 : : tgid->numbers[i].nr);
2816 : : }
2817 : 1104221 : }
2818 : :
2819 : 0 : static int proc_pid_instantiate(struct inode *dir,
2820 : : struct dentry * dentry,
2821 : : struct task_struct *task, const void *ptr)
2822 : : {
2823 : : struct inode *inode;
2824 : :
2825 : 2423 : inode = proc_pid_make_inode(dir->i_sb, task);
2826 [ + ]: 2423 : if (!inode)
2827 : : goto out;
2828 : :
2829 : 2423 : inode->i_mode = S_IFDIR|S_IRUGO|S_IXUGO;
2830 : 2423 : inode->i_op = &proc_tgid_base_inode_operations;
2831 : 2423 : inode->i_fop = &proc_tgid_base_operations;
2832 : 4846 : inode->i_flags|=S_IMMUTABLE;
2833 : :
2834 : 2423 : set_nlink(inode, 2 + pid_entry_count_dirs(tgid_base_stuff,
2835 : : ARRAY_SIZE(tgid_base_stuff)));
2836 : :
2837 : 2423 : d_set_d_op(dentry, &pid_dentry_operations);
2838 : :
2839 : : d_add(dentry, inode);
2840 : : /* Close the race of the process dying before we return the dentry */
2841 [ - + ]: 2423 : if (pid_revalidate(dentry, 0))
2842 : : return 0;
2843 : : out:
2844 : : return -ENOENT;
2845 : : }
2846 : :
2847 : 0 : struct dentry *proc_pid_lookup(struct inode *dir, struct dentry * dentry, unsigned int flags)
2848 : : {
2849 : : int result = 0;
2850 : : struct task_struct *task;
2851 : : unsigned tgid;
2852 : : struct pid_namespace *ns;
2853 : :
2854 : : tgid = name_to_int(dentry);
2855 [ + + ]: 1978 : if (tgid == ~0U)
2856 : : goto out;
2857 : :
2858 : 1976 : ns = dentry->d_sb->s_fs_info;
2859 : : rcu_read_lock();
2860 : 1976 : task = find_task_by_pid_ns(tgid, ns);
2861 [ + + ]: 1976 : if (task)
2862 : 1659 : get_task_struct(task);
2863 : : rcu_read_unlock();
2864 [ + + ]: 1976 : if (!task)
2865 : : goto out;
2866 : :
2867 : 1659 : result = proc_pid_instantiate(dir, dentry, task, NULL);
2868 : : put_task_struct(task);
2869 : : out:
2870 : 0 : return ERR_PTR(result);
2871 : : }
2872 : :
2873 : : /*
2874 : : * Find the first task with tgid >= tgid
2875 : : *
2876 : : */
2877 : : struct tgid_iter {
2878 : : unsigned int tgid;
2879 : : struct task_struct *task;
2880 : : };
2881 : 0 : static struct tgid_iter next_tgid(struct pid_namespace *ns, struct tgid_iter iter)
2882 : : {
2883 : : struct pid *pid;
2884 : :
2885 [ + + ]: 25424 : if (iter.task)
2886 : : put_task_struct(iter.task);
2887 : : rcu_read_lock();
2888 : : retry:
2889 : : iter.task = NULL;
2890 : 27405 : pid = find_ge_pid(iter.tgid, ns);
2891 [ + + ]: 27405 : if (pid) {
2892 : 27122 : iter.tgid = pid_nr_ns(pid, ns);
2893 : 27122 : iter.task = pid_task(pid, PIDTYPE_PID);
2894 : : /* What we to know is if the pid we have find is the
2895 : : * pid of a thread_group_leader. Testing for task
2896 : : * being a thread_group_leader is the obvious thing
2897 : : * todo but there is a window when it fails, due to
2898 : : * the pid transfer logic in de_thread.
2899 : : *
2900 : : * So we perform the straight forward test of seeing
2901 : : * if the pid we have found is the pid of a thread
2902 : : * group leader, and don't worry if the task we have
2903 : : * found doesn't happen to be a thread group leader.
2904 : : * As we don't care in the case of readdir.
2905 : : */
2906 [ + + ][ + + ]: 52546 : if (!iter.task || !has_group_leader_pid(iter.task)) {
2907 : 1981 : iter.tgid += 1;
2908 : 1981 : goto retry;
2909 : : }
2910 : 25141 : get_task_struct(iter.task);
2911 : : }
2912 : : rcu_read_unlock();
2913 : 25424 : return iter;
2914 : : }
2915 : :
2916 : : #define TGID_OFFSET (FIRST_PROCESS_ENTRY + 1)
2917 : :
2918 : : /* for the /proc/ directory itself, after non-process stuff has been done */
2919 : 0 : int proc_pid_readdir(struct file *file, struct dir_context *ctx)
2920 : : {
2921 : : struct tgid_iter iter;
2922 : 25707 : struct pid_namespace *ns = file->f_dentry->d_sb->s_fs_info;
2923 : 566 : loff_t pos = ctx->pos;
2924 : :
2925 [ + + ]: 566 : if (pos >= PID_MAX_LIMIT + TGID_OFFSET)
2926 : : return 0;
2927 : :
2928 [ + - ]: 283 : if (pos == TGID_OFFSET - 1) {
2929 : 283 : struct inode *inode = ns->proc_self->d_inode;
2930 [ + - ]: 283 : if (!dir_emit(ctx, "self", 4, inode->i_ino, DT_LNK))
2931 : : return 0;
2932 : : iter.tgid = 0;
2933 : : } else {
2934 : 0 : iter.tgid = pos - TGID_OFFSET;
2935 : : }
2936 : : iter.task = NULL;
2937 [ + + ]: 25424 : for (iter = next_tgid(ns, iter);
2938 : : iter.task;
2939 : 25141 : iter.tgid += 1, iter = next_tgid(ns, iter)) {
2940 : : char name[PROC_NUMBUF];
2941 : : int len;
2942 [ - + ]: 25141 : if (!has_pid_permissions(ns, iter.task, 2))
2943 : 0 : continue;
2944 : :
2945 : 25141 : len = snprintf(name, sizeof(name), "%d", iter.tgid);
2946 : 25141 : ctx->pos = iter.tgid + TGID_OFFSET;
2947 [ - + ]: 25141 : if (!proc_fill_cache(file, ctx, name, len,
2948 : : proc_pid_instantiate, iter.task, NULL)) {
2949 : : put_task_struct(iter.task);
2950 : 25141 : return 0;
2951 : : }
2952 : : }
2953 : 283 : ctx->pos = PID_MAX_LIMIT + TGID_OFFSET;
2954 : 283 : return 0;
2955 : : }
2956 : :
2957 : : /*
2958 : : * Tasks
2959 : : */
2960 : : static const struct pid_entry tid_base_stuff[] = {
2961 : : DIR("fd", S_IRUSR|S_IXUSR, proc_fd_inode_operations, proc_fd_operations),
2962 : : DIR("fdinfo", S_IRUSR|S_IXUSR, proc_fdinfo_inode_operations, proc_fdinfo_operations),
2963 : : DIR("ns", S_IRUSR|S_IXUGO, proc_ns_dir_inode_operations, proc_ns_dir_operations),
2964 : : REG("environ", S_IRUSR, proc_environ_operations),
2965 : : INF("auxv", S_IRUSR, proc_pid_auxv),
2966 : : ONE("status", S_IRUGO, proc_pid_status),
2967 : : ONE("personality", S_IRUGO, proc_pid_personality),
2968 : : INF("limits", S_IRUGO, proc_pid_limits),
2969 : : #ifdef CONFIG_SCHED_DEBUG
2970 : : REG("sched", S_IRUGO|S_IWUSR, proc_pid_sched_operations),
2971 : : #endif
2972 : : REG("comm", S_IRUGO|S_IWUSR, proc_pid_set_comm_operations),
2973 : : #ifdef CONFIG_HAVE_ARCH_TRACEHOOK
2974 : : INF("syscall", S_IRUGO, proc_pid_syscall),
2975 : : #endif
2976 : : INF("cmdline", S_IRUGO, proc_pid_cmdline),
2977 : : ONE("stat", S_IRUGO, proc_tid_stat),
2978 : : ONE("statm", S_IRUGO, proc_pid_statm),
2979 : : REG("maps", S_IRUGO, proc_tid_maps_operations),
2980 : : #ifdef CONFIG_CHECKPOINT_RESTORE
2981 : : REG("children", S_IRUGO, proc_tid_children_operations),
2982 : : #endif
2983 : : #ifdef CONFIG_NUMA
2984 : : REG("numa_maps", S_IRUGO, proc_tid_numa_maps_operations),
2985 : : #endif
2986 : : REG("mem", S_IRUSR|S_IWUSR, proc_mem_operations),
2987 : : LNK("cwd", proc_cwd_link),
2988 : : LNK("root", proc_root_link),
2989 : : LNK("exe", proc_exe_link),
2990 : : REG("mounts", S_IRUGO, proc_mounts_operations),
2991 : : REG("mountinfo", S_IRUGO, proc_mountinfo_operations),
2992 : : #ifdef CONFIG_PROC_PAGE_MONITOR
2993 : : REG("clear_refs", S_IWUSR, proc_clear_refs_operations),
2994 : : REG("smaps", S_IRUGO, proc_tid_smaps_operations),
2995 : : REG("pagemap", S_IRUGO, proc_pagemap_operations),
2996 : : #endif
2997 : : #ifdef CONFIG_SECURITY
2998 : : DIR("attr", S_IRUGO|S_IXUGO, proc_attr_dir_inode_operations, proc_attr_dir_operations),
2999 : : #endif
3000 : : #ifdef CONFIG_KALLSYMS
3001 : : INF("wchan", S_IRUGO, proc_pid_wchan),
3002 : : #endif
3003 : : #ifdef CONFIG_STACKTRACE
3004 : : ONE("stack", S_IRUGO, proc_pid_stack),
3005 : : #endif
3006 : : #ifdef CONFIG_SCHEDSTATS
3007 : : INF("schedstat", S_IRUGO, proc_pid_schedstat),
3008 : : #endif
3009 : : #ifdef CONFIG_LATENCYTOP
3010 : : REG("latency", S_IRUGO, proc_lstats_operations),
3011 : : #endif
3012 : : #ifdef CONFIG_PROC_PID_CPUSET
3013 : : REG("cpuset", S_IRUGO, proc_cpuset_operations),
3014 : : #endif
3015 : : #ifdef CONFIG_CGROUPS
3016 : : REG("cgroup", S_IRUGO, proc_cgroup_operations),
3017 : : #endif
3018 : : INF("oom_score", S_IRUGO, proc_oom_score),
3019 : : REG("oom_adj", S_IRUGO|S_IWUSR, proc_oom_adj_operations),
3020 : : REG("oom_score_adj", S_IRUGO|S_IWUSR, proc_oom_score_adj_operations),
3021 : : #ifdef CONFIG_AUDITSYSCALL
3022 : : REG("loginuid", S_IWUSR|S_IRUGO, proc_loginuid_operations),
3023 : : REG("sessionid", S_IRUGO, proc_sessionid_operations),
3024 : : #endif
3025 : : #ifdef CONFIG_FAULT_INJECTION
3026 : : REG("make-it-fail", S_IRUGO|S_IWUSR, proc_fault_inject_operations),
3027 : : #endif
3028 : : #ifdef CONFIG_TASK_IO_ACCOUNTING
3029 : : INF("io", S_IRUSR, proc_tid_io_accounting),
3030 : : #endif
3031 : : #ifdef CONFIG_HARDWALL
3032 : : INF("hardwall", S_IRUGO, proc_pid_hardwall),
3033 : : #endif
3034 : : #ifdef CONFIG_USER_NS
3035 : : REG("uid_map", S_IRUGO|S_IWUSR, proc_uid_map_operations),
3036 : : REG("gid_map", S_IRUGO|S_IWUSR, proc_gid_map_operations),
3037 : : REG("projid_map", S_IRUGO|S_IWUSR, proc_projid_map_operations),
3038 : : #endif
3039 : : };
3040 : :
3041 : 0 : static int proc_tid_base_readdir(struct file *file, struct dir_context *ctx)
3042 : : {
3043 : 2 : return proc_pident_readdir(file, ctx,
3044 : : tid_base_stuff, ARRAY_SIZE(tid_base_stuff));
3045 : : }
3046 : :
3047 : 0 : static struct dentry *proc_tid_base_lookup(struct inode *dir, struct dentry *dentry, unsigned int flags)
3048 : : {
3049 : 26 : return proc_pident_lookup(dir, dentry,
3050 : : tid_base_stuff, ARRAY_SIZE(tid_base_stuff));
3051 : : }
3052 : :
3053 : : static const struct file_operations proc_tid_base_operations = {
3054 : : .read = generic_read_dir,
3055 : : .iterate = proc_tid_base_readdir,
3056 : : .llseek = default_llseek,
3057 : : };
3058 : :
3059 : : static const struct inode_operations proc_tid_base_inode_operations = {
3060 : : .lookup = proc_tid_base_lookup,
3061 : : .getattr = pid_getattr,
3062 : : .setattr = proc_setattr,
3063 : : };
3064 : :
3065 : 0 : static int proc_task_instantiate(struct inode *dir,
3066 : : struct dentry *dentry, struct task_struct *task, const void *ptr)
3067 : : {
3068 : : struct inode *inode;
3069 : 234 : inode = proc_pid_make_inode(dir->i_sb, task);
3070 : :
3071 [ + ]: 234 : if (!inode)
3072 : : goto out;
3073 : 234 : inode->i_mode = S_IFDIR|S_IRUGO|S_IXUGO;
3074 : 234 : inode->i_op = &proc_tid_base_inode_operations;
3075 : 234 : inode->i_fop = &proc_tid_base_operations;
3076 : 468 : inode->i_flags|=S_IMMUTABLE;
3077 : :
3078 : 234 : set_nlink(inode, 2 + pid_entry_count_dirs(tid_base_stuff,
3079 : : ARRAY_SIZE(tid_base_stuff)));
3080 : :
3081 : 234 : d_set_d_op(dentry, &pid_dentry_operations);
3082 : :
3083 : : d_add(dentry, inode);
3084 : : /* Close the race of the process dying before we return the dentry */
3085 [ - + ]: 234 : if (pid_revalidate(dentry, 0))
3086 : : return 0;
3087 : : out:
3088 : : return -ENOENT;
3089 : : }
3090 : :
3091 : 0 : static struct dentry *proc_task_lookup(struct inode *dir, struct dentry * dentry, unsigned int flags)
3092 : : {
3093 : : int result = -ENOENT;
3094 : : struct task_struct *task;
3095 : : struct task_struct *leader = get_proc_task(dir);
3096 : : unsigned tid;
3097 : : struct pid_namespace *ns;
3098 : :
3099 [ + - ]: 27 : if (!leader)
3100 : : goto out_no_task;
3101 : :
3102 : : tid = name_to_int(dentry);
3103 [ - + ]: 27 : if (tid == ~0U)
3104 : : goto out;
3105 : :
3106 : 0 : ns = dentry->d_sb->s_fs_info;
3107 : : rcu_read_lock();
3108 : 0 : task = find_task_by_pid_ns(tid, ns);
3109 [ # # ]: 0 : if (task)
3110 : 0 : get_task_struct(task);
3111 : : rcu_read_unlock();
3112 [ # # ]: 0 : if (!task)
3113 : : goto out;
3114 [ # # ]: 0 : if (!same_thread_group(leader, task))
3115 : : goto out_drop_task;
3116 : :
3117 : 0 : result = proc_task_instantiate(dir, dentry, task, NULL);
3118 : : out_drop_task:
3119 : : put_task_struct(task);
3120 : : out:
3121 : : put_task_struct(leader);
3122 : : out_no_task:
3123 : 27 : return ERR_PTR(result);
3124 : : }
3125 : :
3126 : : /*
3127 : : * Find the first tid of a thread group to return to user space.
3128 : : *
3129 : : * Usually this is just the thread group leader, but if the users
3130 : : * buffer was too small or there was a seek into the middle of the
3131 : : * directory we have more work todo.
3132 : : *
3133 : : * In the case of a short read we start with find_task_by_pid.
3134 : : *
3135 : : * In the case of a seek we start with the leader and walk nr
3136 : : * threads past it.
3137 : : */
3138 : 0 : static struct task_struct *first_tid(struct pid *pid, int tid, loff_t f_pos,
3139 : : struct pid_namespace *ns)
3140 : : {
3141 : : struct task_struct *pos, *task;
3142 : 2292 : unsigned long nr = f_pos;
3143 : :
3144 [ + - ]: 2292 : if (nr != f_pos) /* 32bit overflow? */
3145 : : return NULL;
3146 : :
3147 : : rcu_read_lock();
3148 : 2292 : task = pid_task(pid, PIDTYPE_PID);
3149 [ + - ]: 2292 : if (!task)
3150 : : goto fail;
3151 : :
3152 : : /* Attempt to start with the tid of a thread */
3153 [ - + ]: 2292 : if (tid && nr) {
3154 : 0 : pos = find_task_by_pid_ns(tid, ns);
3155 [ # # ][ # # ]: 0 : if (pos && same_thread_group(pos, task))
3156 : : goto found;
3157 : : }
3158 : :
3159 : : /* If nr exceeds the number of threads there is nothing todo */
3160 [ + + ]: 2292 : if (nr >= get_nr_threads(task))
3161 : : goto fail;
3162 : :
3163 : : /* If we haven't found our starting place yet start
3164 : : * with the leader and walk nr threads forward.
3165 : : */
3166 : 1146 : pos = task = task->group_leader;
3167 : : do {
3168 [ - + ]: 1146 : if (!nr--)
3169 : : goto found;
3170 [ # # ]: 0 : } while_each_thread(task, pos);
3171 : : fail:
3172 : : pos = NULL;
3173 : : goto out;
3174 : : found:
3175 : 0 : get_task_struct(pos);
3176 : : out:
3177 : : rcu_read_unlock();
3178 : 2292 : return pos;
3179 : : }
3180 : :
3181 : : /*
3182 : : * Find the next thread in the thread list.
3183 : : * Return NULL if there is an error or no next thread.
3184 : : *
3185 : : * The reference to the input task_struct is released.
3186 : : */
3187 : 0 : static struct task_struct *next_tid(struct task_struct *start)
3188 : : {
3189 : : struct task_struct *pos = NULL;
3190 : : rcu_read_lock();
3191 [ + - ]: 1188 : if (pid_alive(start)) {
3192 : 1188 : pos = next_thread(start);
3193 [ + + ]: 1188 : if (thread_group_leader(pos))
3194 : : pos = NULL;
3195 : : else
3196 : 42 : get_task_struct(pos);
3197 : : }
3198 : : rcu_read_unlock();
3199 : : put_task_struct(start);
3200 : 1188 : return pos;
3201 : : }
3202 : :
3203 : : /* for the /proc/TGID/task/ directories */
3204 : 0 : static int proc_task_readdir(struct file *file, struct dir_context *ctx)
3205 : : {
3206 : : struct inode *inode = file_inode(file);
3207 : : struct task_struct *task;
3208 : : struct pid_namespace *ns;
3209 : : int tid;
3210 : :
3211 [ + - ]: 2292 : if (proc_inode_is_dead(inode))
3212 : : return -ENOENT;
3213 : :
3214 [ # # ]: 2292 : if (!dir_emit_dots(file, ctx))
3215 : : return 0;
3216 : :
3217 : : /* f_version caches the tgid value that the last readdir call couldn't
3218 : : * return. lseek aka telldir automagically resets f_version to 0.
3219 : : */
3220 : 2292 : ns = file->f_dentry->d_sb->s_fs_info;
3221 : 2292 : tid = (int)file->f_version;
3222 : 2292 : file->f_version = 0;
3223 [ + + ]: 3480 : for (task = first_tid(proc_pid(inode), tid, ctx->pos - 2, ns);
3224 : : task;
3225 : 1188 : task = next_tid(task), ctx->pos++) {
3226 : : char name[PROC_NUMBUF];
3227 : : int len;
3228 : : tid = task_pid_nr_ns(task, ns);
3229 : 1188 : len = snprintf(name, sizeof(name), "%d", tid);
3230 [ - + ]: 1188 : if (!proc_fill_cache(file, ctx, name, len,
3231 : : proc_task_instantiate, task, NULL)) {
3232 : : /* returning this tgid failed, save it as the first
3233 : : * pid for the next readir call */
3234 : 0 : file->f_version = (u64)tid;
3235 : : put_task_struct(task);
3236 : 0 : break;
3237 : : }
3238 : : }
3239 : :
3240 : : return 0;
3241 : : }
3242 : :
3243 : 0 : static int proc_task_getattr(struct vfsmount *mnt, struct dentry *dentry, struct kstat *stat)
3244 : : {
3245 : 264 : struct inode *inode = dentry->d_inode;
3246 : : struct task_struct *p = get_proc_task(inode);
3247 : 264 : generic_fillattr(inode, stat);
3248 : :
3249 [ + - ]: 264 : if (p) {
3250 : 528 : stat->nlink += get_nr_threads(p);
3251 : : put_task_struct(p);
3252 : : }
3253 : :
3254 : 0 : return 0;
3255 : : }
3256 : :
3257 : : static const struct inode_operations proc_task_inode_operations = {
3258 : : .lookup = proc_task_lookup,
3259 : : .getattr = proc_task_getattr,
3260 : : .setattr = proc_setattr,
3261 : : .permission = proc_pid_permission,
3262 : : };
3263 : :
3264 : : static const struct file_operations proc_task_operations = {
3265 : : .read = generic_read_dir,
3266 : : .iterate = proc_task_readdir,
3267 : : .llseek = default_llseek,
3268 : : };
|