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 [ + + ][ + + ]: 93093 : for (i = 0; i < n; ++i) {
160 [ + + ][ + + ]: 90686 : if (S_ISDIR(entries[i].mode))
161 : 14052 : ++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 [ + - ]: 856 : if (task->fs) {
173 : : get_fs_root(task->fs, root);
174 : : result = 0;
175 : : }
176 : : task_unlock(task);
177 : 856 : return result;
178 : : }
179 : :
180 : 0 : static int proc_cwd_link(struct dentry *dentry, struct path *path)
181 : : {
182 : 0 : struct task_struct *task = get_proc_task(dentry->d_inode);
183 : : int result = -ENOENT;
184 : :
185 [ # # ]: 0 : if (task) {
186 : : task_lock(task);
187 [ # # ]: 0 : 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 : 0 : return result;
195 : : }
196 : :
197 : 0 : static int proc_root_link(struct dentry *dentry, struct path *path)
198 : : {
199 : 856 : struct task_struct *task = get_proc_task(dentry->d_inode);
200 : : int result = -ENOENT;
201 : :
202 [ + - ]: 856 : if (task) {
203 : 856 : 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 : 116 : struct mm_struct *mm = get_task_mm(task);
214 [ + + ]: 116 : if (!mm)
215 : : goto out;
216 [ + - ]: 58 : if (!mm->arg_end)
217 : : goto out_mm; /* Shh! No looking before we're done */
218 : :
219 : 58 : len = mm->arg_end - mm->arg_start;
220 : :
221 [ - + ]: 58 : if (len > PAGE_SIZE)
222 : : len = PAGE_SIZE;
223 : :
224 : 58 : 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 [ + - ][ - + ]: 58 : 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 : 58 : mmput(mm);
242 : : out:
243 : 116 : 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 [ + - ]: 120 : if (!lock_task_sighand(task, &flags))
493 : : return 0;
494 : 120 : memcpy(rlim, task->signal->rlim, sizeof(struct rlimit) * RLIM_NLIMITS);
495 : 120 : unlock_task_sighand(task, &flags);
496 : :
497 : : /*
498 : : * print the file header
499 : : */
500 : 120 : count += sprintf(&bufptr[count], "%-25s %-20s %-20s %-10s\n",
501 : : "Limit", "Soft Limit", "Hard Limit", "Units");
502 : :
503 [ + - ]: 1920 : for (i = 0; i < RLIM_NLIMITS; i++) {
504 [ + + ]: 1920 : if (rlim[i].rlim_cur == RLIM_INFINITY)
505 : 840 : count += sprintf(&bufptr[count], "%-25s %-20s ",
506 : : lnames[i].name, "unlimited");
507 : : else
508 : 1080 : count += sprintf(&bufptr[count], "%-25s %-20lu ",
509 : : lnames[i].name, rlim[i].rlim_cur);
510 : :
511 [ + + ]: 1920 : if (rlim[i].rlim_max == RLIM_INFINITY)
512 : 1080 : count += sprintf(&bufptr[count], "%-20s ", "unlimited");
513 : : else
514 : 840 : count += sprintf(&bufptr[count], "%-20lu ",
515 : : rlim[i].rlim_max);
516 : :
517 [ + + ]: 1920 : if (lnames[i].unit)
518 : 1560 : count += sprintf(&bufptr[count], "%-10s\n",
519 : : lnames[i].unit);
520 : : else
521 : 240 : 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 [ + - ]: 4937 : if (task) {
566 : 4937 : 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 : 1 : struct inode *inode = dentry->d_inode;
576 : :
577 [ + - ]: 1 : if (attr->ia_valid & ATTR_MODE)
578 : : return -EPERM;
579 : :
580 : 1 : error = inode_change_ok(inode, attr);
581 [ + - ]: 1 : if (error)
582 : : return error;
583 : :
584 : 1 : setattr_copy(inode, attr);
585 : : mark_inode_dirty(inode);
586 : 1 : 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 : 60383 : static bool has_pid_permissions(struct pid_namespace *pid,
594 : : struct task_struct *task,
595 : : int hide_pid_min)
596 : : {
597 [ - + ]: 60383 : 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 : 69218 : 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 [ + - ]: 34609 : if (!task)
613 : : return -ESRCH;
614 : 34609 : has_perms = has_pid_permissions(pid, task, 1);
615 : : put_task_struct(task);
616 : :
617 [ - + ]: 34609 : 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 : 34609 : 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 [ + - ]: 296 : if (!task)
651 : : goto out_no_task;
652 : :
653 [ + + ]: 296 : if (count > PROC_BLOCK_SIZE)
654 : : count = PROC_BLOCK_SIZE;
655 : :
656 : : length = -ENOMEM;
657 [ + - ]: 296 : if (!(page = __get_free_page(GFP_TEMPORARY)))
658 : : goto out;
659 : :
660 : 296 : length = PROC_I(inode)->op.proc_read(task, (char*)page);
661 : :
662 [ + - ]: 296 : if (length >= 0)
663 : 296 : length = simple_read_from_buffer(buf, count, ppos, (char *)page, length);
664 : 296 : free_page(page);
665 : : out:
666 : : put_task_struct(task);
667 : : out_no_task:
668 : 296 : 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 : 26586 : struct inode *inode = m->private;
679 : : struct pid_namespace *ns;
680 : : struct pid *pid;
681 : : struct task_struct *task;
682 : : int ret;
683 : :
684 : 26586 : ns = inode->i_sb->s_fs_info;
685 : : pid = proc_pid(inode);
686 : 26586 : task = get_pid_task(pid, PIDTYPE_PID);
687 [ + - ]: 26586 : if (!task)
688 : : return -ESRCH;
689 : :
690 : 26586 : ret = PROC_I(inode)->op.proc_show(m, ns, pid, task);
691 : :
692 : : put_task_struct(task);
693 : 26586 : return ret;
694 : : }
695 : :
696 : 0 : static int proc_single_open(struct inode *inode, struct file *filp)
697 : : {
698 : 26586 : 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 : : uid_t uid;
1012 : : struct task_struct *p;
1013 : :
1014 : : p = get_proc_task(inode);
1015 [ + - ]: 1 : if(p) {
1016 : 1 : uid = task_uid(p);
1017 : : put_task_struct(p);
1018 : : }
1019 : :
1020 : : /*
1021 : : * System Server (uid == 1000) is granted access to oom_adj of all
1022 : : * android applications (uid > 10000) as and services (uid >= 1000)
1023 : : */
1024 [ + - ][ - + ]: 1 : if (p && (current_fsuid() == 1000) && (uid >= 1000)) {
[ # # ]
1025 [ # # ]: 0 : if (inode->i_mode >> 6 & mask) {
1026 : : return 0;
1027 : : }
1028 : : }
1029 : :
1030 : : /* Fall back to default. */
1031 : 1 : return generic_permission(inode, mask);
1032 : : }
1033 : :
1034 : : static const struct inode_operations proc_oom_adj_inode_operations = {
1035 : : .permission = oom_adjust_permission,
1036 : : };
1037 : :
1038 : : static const struct file_operations proc_oom_adj_operations = {
1039 : : .read = oom_adj_read,
1040 : : .write = oom_adj_write,
1041 : : .llseek = generic_file_llseek,
1042 : : };
1043 : :
1044 : 0 : static ssize_t oom_score_adj_read(struct file *file, char __user *buf,
1045 : : size_t count, loff_t *ppos)
1046 : : {
1047 : : struct task_struct *task = get_proc_task(file_inode(file));
1048 : : char buffer[PROC_NUMBUF];
1049 : : short oom_score_adj = OOM_SCORE_ADJ_MIN;
1050 : : unsigned long flags;
1051 : : size_t len;
1052 : :
1053 [ + - ]: 4 : if (!task)
1054 : : return -ESRCH;
1055 [ + - ]: 4 : if (lock_task_sighand(task, &flags)) {
1056 : 4 : oom_score_adj = task->signal->oom_score_adj;
1057 : 4 : unlock_task_sighand(task, &flags);
1058 : : }
1059 : : put_task_struct(task);
1060 : 4 : len = snprintf(buffer, sizeof(buffer), "%hd\n", oom_score_adj);
1061 : 4 : return simple_read_from_buffer(buf, count, ppos, buffer, len);
1062 : : }
1063 : :
1064 : 0 : static ssize_t oom_score_adj_write(struct file *file, const char __user *buf,
1065 : : size_t count, loff_t *ppos)
1066 : : {
1067 : : struct task_struct *task;
1068 : : char buffer[PROC_NUMBUF];
1069 : : unsigned long flags;
1070 : : int oom_score_adj;
1071 : : int err;
1072 : :
1073 : 1 : memset(buffer, 0, sizeof(buffer));
1074 [ - + ]: 1 : if (count > sizeof(buffer) - 1)
1075 : : count = sizeof(buffer) - 1;
1076 [ + - ]: 1 : if (copy_from_user(buffer, buf, count)) {
1077 : : err = -EFAULT;
1078 : : goto out;
1079 : : }
1080 : :
1081 : 1 : err = kstrtoint(strstrip(buffer), 0, &oom_score_adj);
1082 [ + - ]: 1 : if (err)
1083 : : goto out;
1084 [ + - ]: 1 : if (oom_score_adj < OOM_SCORE_ADJ_MIN ||
1085 : : oom_score_adj > OOM_SCORE_ADJ_MAX) {
1086 : : err = -EINVAL;
1087 : : goto out;
1088 : : }
1089 : :
1090 : : task = get_proc_task(file_inode(file));
1091 [ + - ]: 1 : if (!task) {
1092 : : err = -ESRCH;
1093 : : goto out;
1094 : : }
1095 : :
1096 : : task_lock(task);
1097 [ + - ]: 1 : if (!task->mm) {
1098 : : err = -EINVAL;
1099 : : goto err_task_lock;
1100 : : }
1101 : :
1102 [ + - ]: 1 : if (!lock_task_sighand(task, &flags)) {
1103 : : err = -ESRCH;
1104 : : goto err_task_lock;
1105 : : }
1106 : :
1107 [ - + # # ]: 1 : if ((short)oom_score_adj < task->signal->oom_score_adj_min &&
1108 : 0 : !capable(CAP_SYS_RESOURCE)) {
1109 : : err = -EACCES;
1110 : : goto err_sighand;
1111 : : }
1112 : :
1113 : 1 : task->signal->oom_score_adj = (short)oom_score_adj;
1114 [ + - ]: 1 : if (has_capability_noaudit(current, CAP_SYS_RESOURCE))
1115 : 1 : task->signal->oom_score_adj_min = (short)oom_score_adj;
1116 : : trace_oom_score_adj_update(task);
1117 : :
1118 : : err_sighand:
1119 : 0 : unlock_task_sighand(task, &flags);
1120 : : err_task_lock:
1121 : : task_unlock(task);
1122 : : put_task_struct(task);
1123 : : out:
1124 [ + - ]: 1 : return err < 0 ? err : count;
1125 : : }
1126 : :
1127 : : static const struct file_operations proc_oom_score_adj_operations = {
1128 : : .read = oom_score_adj_read,
1129 : : .write = oom_score_adj_write,
1130 : : .llseek = default_llseek,
1131 : : };
1132 : :
1133 : : #ifdef CONFIG_AUDITSYSCALL
1134 : : #define TMPBUFLEN 21
1135 : 0 : static ssize_t proc_loginuid_read(struct file * file, char __user * buf,
1136 : : size_t count, loff_t *ppos)
1137 : : {
1138 : : struct inode * inode = file_inode(file);
1139 : : struct task_struct *task = get_proc_task(inode);
1140 : : ssize_t length;
1141 : : char tmpbuf[TMPBUFLEN];
1142 : :
1143 [ + - ]: 18 : if (!task)
1144 : : return -ESRCH;
1145 : 18 : length = scnprintf(tmpbuf, TMPBUFLEN, "%u",
1146 : : from_kuid(file->f_cred->user_ns,
1147 : : audit_get_loginuid(task)));
1148 : : put_task_struct(task);
1149 : 18 : return simple_read_from_buffer(buf, count, ppos, tmpbuf, length);
1150 : : }
1151 : :
1152 : 0 : static ssize_t proc_loginuid_write(struct file * file, const char __user * buf,
1153 : : size_t count, loff_t *ppos)
1154 : : {
1155 : : struct inode * inode = file_inode(file);
1156 : : char *page, *tmp;
1157 : : ssize_t length;
1158 : : uid_t loginuid;
1159 : : kuid_t kloginuid;
1160 : :
1161 : : rcu_read_lock();
1162 [ # # ]: 0 : if (current != pid_task(proc_pid(inode), PIDTYPE_PID)) {
1163 : : rcu_read_unlock();
1164 : 0 : return -EPERM;
1165 : : }
1166 : : rcu_read_unlock();
1167 : :
1168 [ # # ]: 0 : if (count >= PAGE_SIZE)
1169 : : count = PAGE_SIZE - 1;
1170 : :
1171 [ # # ]: 0 : if (*ppos != 0) {
1172 : : /* No partial writes. */
1173 : : return -EINVAL;
1174 : : }
1175 : 0 : page = (char*)__get_free_page(GFP_TEMPORARY);
1176 [ # # ]: 0 : if (!page)
1177 : : return -ENOMEM;
1178 : : length = -EFAULT;
1179 [ # # ]: 0 : if (copy_from_user(page, buf, count))
1180 : : goto out_free_page;
1181 : :
1182 : 0 : page[count] = '\0';
1183 : 0 : loginuid = simple_strtoul(page, &tmp, 10);
1184 [ # # ]: 0 : if (tmp == page) {
1185 : : length = -EINVAL;
1186 : : goto out_free_page;
1187 : :
1188 : : }
1189 : :
1190 : : /* is userspace tring to explicitly UNSET the loginuid? */
1191 [ # # ]: 0 : if (loginuid == AUDIT_UID_UNSET) {
1192 : : kloginuid = INVALID_UID;
1193 : : } else {
1194 : : kloginuid = make_kuid(file->f_cred->user_ns, loginuid);
1195 [ # # ]: 0 : if (!uid_valid(kloginuid)) {
1196 : : length = -EINVAL;
1197 : : goto out_free_page;
1198 : : }
1199 : : }
1200 : :
1201 : 0 : length = audit_set_loginuid(kloginuid);
1202 [ # # ]: 0 : if (likely(length == 0))
1203 : 0 : length = count;
1204 : :
1205 : : out_free_page:
1206 : 0 : free_page((unsigned long) page);
1207 : 0 : return length;
1208 : : }
1209 : :
1210 : : static const struct file_operations proc_loginuid_operations = {
1211 : : .read = proc_loginuid_read,
1212 : : .write = proc_loginuid_write,
1213 : : .llseek = generic_file_llseek,
1214 : : };
1215 : :
1216 : 0 : static ssize_t proc_sessionid_read(struct file * file, char __user * buf,
1217 : : size_t count, loff_t *ppos)
1218 : : {
1219 : : struct inode * inode = file_inode(file);
1220 : : struct task_struct *task = get_proc_task(inode);
1221 : : ssize_t length;
1222 : : char tmpbuf[TMPBUFLEN];
1223 : :
1224 [ + - ]: 4 : if (!task)
1225 : : return -ESRCH;
1226 : 4 : length = scnprintf(tmpbuf, TMPBUFLEN, "%u",
1227 : : audit_get_sessionid(task));
1228 : : put_task_struct(task);
1229 : 4 : return simple_read_from_buffer(buf, count, ppos, tmpbuf, length);
1230 : : }
1231 : :
1232 : : static const struct file_operations proc_sessionid_operations = {
1233 : : .read = proc_sessionid_read,
1234 : : .llseek = generic_file_llseek,
1235 : : };
1236 : : #endif
1237 : :
1238 : : #ifdef CONFIG_FAULT_INJECTION
1239 : : static ssize_t proc_fault_inject_read(struct file * file, char __user * buf,
1240 : : size_t count, loff_t *ppos)
1241 : : {
1242 : : struct task_struct *task = get_proc_task(file_inode(file));
1243 : : char buffer[PROC_NUMBUF];
1244 : : size_t len;
1245 : : int make_it_fail;
1246 : :
1247 : : if (!task)
1248 : : return -ESRCH;
1249 : : make_it_fail = task->make_it_fail;
1250 : : put_task_struct(task);
1251 : :
1252 : : len = snprintf(buffer, sizeof(buffer), "%i\n", make_it_fail);
1253 : :
1254 : : return simple_read_from_buffer(buf, count, ppos, buffer, len);
1255 : : }
1256 : :
1257 : : static ssize_t proc_fault_inject_write(struct file * file,
1258 : : const char __user * buf, size_t count, loff_t *ppos)
1259 : : {
1260 : : struct task_struct *task;
1261 : : char buffer[PROC_NUMBUF], *end;
1262 : : int make_it_fail;
1263 : :
1264 : : if (!capable(CAP_SYS_RESOURCE))
1265 : : return -EPERM;
1266 : : memset(buffer, 0, sizeof(buffer));
1267 : : if (count > sizeof(buffer) - 1)
1268 : : count = sizeof(buffer) - 1;
1269 : : if (copy_from_user(buffer, buf, count))
1270 : : return -EFAULT;
1271 : : make_it_fail = simple_strtol(strstrip(buffer), &end, 0);
1272 : : if (*end)
1273 : : return -EINVAL;
1274 : : task = get_proc_task(file_inode(file));
1275 : : if (!task)
1276 : : return -ESRCH;
1277 : : task->make_it_fail = make_it_fail;
1278 : : put_task_struct(task);
1279 : :
1280 : : return count;
1281 : : }
1282 : :
1283 : : static const struct file_operations proc_fault_inject_operations = {
1284 : : .read = proc_fault_inject_read,
1285 : : .write = proc_fault_inject_write,
1286 : : .llseek = generic_file_llseek,
1287 : : };
1288 : : #endif
1289 : :
1290 : :
1291 : : #ifdef CONFIG_SCHED_DEBUG
1292 : : /*
1293 : : * Print out various scheduling related per-task fields:
1294 : : */
1295 : 0 : static int sched_show(struct seq_file *m, void *v)
1296 : : {
1297 : 2 : struct inode *inode = m->private;
1298 : : struct task_struct *p;
1299 : :
1300 : : p = get_proc_task(inode);
1301 [ + - ]: 2 : if (!p)
1302 : : return -ESRCH;
1303 : 2 : proc_sched_show_task(p, m);
1304 : :
1305 : : put_task_struct(p);
1306 : :
1307 : : return 0;
1308 : : }
1309 : :
1310 : : static ssize_t
1311 : 0 : sched_write(struct file *file, const char __user *buf,
1312 : : size_t count, loff_t *offset)
1313 : : {
1314 : : struct inode *inode = file_inode(file);
1315 : : struct task_struct *p;
1316 : :
1317 : : p = get_proc_task(inode);
1318 [ # # ]: 0 : if (!p)
1319 : : return -ESRCH;
1320 : 0 : proc_sched_set_task(p);
1321 : :
1322 : : put_task_struct(p);
1323 : :
1324 : 0 : return count;
1325 : : }
1326 : :
1327 : 0 : static int sched_open(struct inode *inode, struct file *filp)
1328 : : {
1329 : 2 : return single_open(filp, sched_show, inode);
1330 : : }
1331 : :
1332 : : static const struct file_operations proc_pid_sched_operations = {
1333 : : .open = sched_open,
1334 : : .read = seq_read,
1335 : : .write = sched_write,
1336 : : .llseek = seq_lseek,
1337 : : .release = single_release,
1338 : : };
1339 : :
1340 : : #endif
1341 : :
1342 : : #ifdef CONFIG_SCHED_AUTOGROUP
1343 : : /*
1344 : : * Print out autogroup related information:
1345 : : */
1346 : : static int sched_autogroup_show(struct seq_file *m, void *v)
1347 : : {
1348 : : struct inode *inode = m->private;
1349 : : struct task_struct *p;
1350 : :
1351 : : p = get_proc_task(inode);
1352 : : if (!p)
1353 : : return -ESRCH;
1354 : : proc_sched_autogroup_show_task(p, m);
1355 : :
1356 : : put_task_struct(p);
1357 : :
1358 : : return 0;
1359 : : }
1360 : :
1361 : : static ssize_t
1362 : : sched_autogroup_write(struct file *file, const char __user *buf,
1363 : : size_t count, loff_t *offset)
1364 : : {
1365 : : struct inode *inode = file_inode(file);
1366 : : struct task_struct *p;
1367 : : char buffer[PROC_NUMBUF];
1368 : : int nice;
1369 : : int err;
1370 : :
1371 : : memset(buffer, 0, sizeof(buffer));
1372 : : if (count > sizeof(buffer) - 1)
1373 : : count = sizeof(buffer) - 1;
1374 : : if (copy_from_user(buffer, buf, count))
1375 : : return -EFAULT;
1376 : :
1377 : : err = kstrtoint(strstrip(buffer), 0, &nice);
1378 : : if (err < 0)
1379 : : return err;
1380 : :
1381 : : p = get_proc_task(inode);
1382 : : if (!p)
1383 : : return -ESRCH;
1384 : :
1385 : : err = proc_sched_autogroup_set_nice(p, nice);
1386 : : if (err)
1387 : : count = err;
1388 : :
1389 : : put_task_struct(p);
1390 : :
1391 : : return count;
1392 : : }
1393 : :
1394 : : static int sched_autogroup_open(struct inode *inode, struct file *filp)
1395 : : {
1396 : : int ret;
1397 : :
1398 : : ret = single_open(filp, sched_autogroup_show, NULL);
1399 : : if (!ret) {
1400 : : struct seq_file *m = filp->private_data;
1401 : :
1402 : : m->private = inode;
1403 : : }
1404 : : return ret;
1405 : : }
1406 : :
1407 : : static const struct file_operations proc_pid_sched_autogroup_operations = {
1408 : : .open = sched_autogroup_open,
1409 : : .read = seq_read,
1410 : : .write = sched_autogroup_write,
1411 : : .llseek = seq_lseek,
1412 : : .release = single_release,
1413 : : };
1414 : :
1415 : : #endif /* CONFIG_SCHED_AUTOGROUP */
1416 : :
1417 : 0 : static ssize_t comm_write(struct file *file, const char __user *buf,
1418 : : size_t count, loff_t *offset)
1419 : : {
1420 : : struct inode *inode = file_inode(file);
1421 : : struct task_struct *p;
1422 : : char buffer[TASK_COMM_LEN];
1423 : : const size_t maxlen = sizeof(buffer) - 1;
1424 : :
1425 : 0 : memset(buffer, 0, sizeof(buffer));
1426 [ # # ]: 0 : if (copy_from_user(buffer, buf, count > maxlen ? maxlen : count))
1427 : : return -EFAULT;
1428 : :
1429 : : p = get_proc_task(inode);
1430 [ # # ]: 0 : if (!p)
1431 : : return -ESRCH;
1432 : :
1433 [ # # ]: 0 : if (same_thread_group(current, p))
1434 : 0 : set_task_comm(p, buffer);
1435 : : else
1436 : : count = -EINVAL;
1437 : :
1438 : : put_task_struct(p);
1439 : :
1440 : 0 : return count;
1441 : : }
1442 : :
1443 : 0 : static int comm_show(struct seq_file *m, void *v)
1444 : : {
1445 : 2 : struct inode *inode = m->private;
1446 : : struct task_struct *p;
1447 : :
1448 : : p = get_proc_task(inode);
1449 [ + - ]: 2 : if (!p)
1450 : : return -ESRCH;
1451 : :
1452 : : task_lock(p);
1453 : 2 : seq_printf(m, "%s\n", p->comm);
1454 : : task_unlock(p);
1455 : :
1456 : : put_task_struct(p);
1457 : :
1458 : : return 0;
1459 : : }
1460 : :
1461 : 0 : static int comm_open(struct inode *inode, struct file *filp)
1462 : : {
1463 : 2 : return single_open(filp, comm_show, inode);
1464 : : }
1465 : :
1466 : : static const struct file_operations proc_pid_set_comm_operations = {
1467 : : .open = comm_open,
1468 : : .read = seq_read,
1469 : : .write = comm_write,
1470 : : .llseek = seq_lseek,
1471 : : .release = single_release,
1472 : : };
1473 : :
1474 : 0 : static int proc_exe_link(struct dentry *dentry, struct path *exe_path)
1475 : : {
1476 : : struct task_struct *task;
1477 : : struct mm_struct *mm;
1478 : : struct file *exe_file;
1479 : :
1480 : 2507 : task = get_proc_task(dentry->d_inode);
1481 [ + - ]: 2507 : if (!task)
1482 : : return -ENOENT;
1483 : 2507 : mm = get_task_mm(task);
1484 : : put_task_struct(task);
1485 [ + - ]: 2507 : if (!mm)
1486 : : return -ENOENT;
1487 : 2507 : exe_file = get_mm_exe_file(mm);
1488 : 2507 : mmput(mm);
1489 [ + - ]: 2507 : if (exe_file) {
1490 : 2507 : *exe_path = exe_file->f_path;
1491 : 2507 : path_get(&exe_file->f_path);
1492 : 2507 : fput(exe_file);
1493 : 2507 : return 0;
1494 : : } else
1495 : : return -ENOENT;
1496 : : }
1497 : :
1498 : 0 : static void *proc_pid_follow_link(struct dentry *dentry, struct nameidata *nd)
1499 : : {
1500 : 724 : struct inode *inode = dentry->d_inode;
1501 : : struct path path;
1502 : : int error = -EACCES;
1503 : :
1504 : : /* Are we allowed to snoop on the tasks file descriptors? */
1505 [ + - ]: 724 : if (!proc_fd_access_allowed(inode))
1506 : : goto out;
1507 : :
1508 : 724 : error = PROC_I(inode)->op.proc_get_link(dentry, &path);
1509 [ + - ]: 724 : if (error)
1510 : : goto out;
1511 : :
1512 : 724 : nd_jump_link(nd, &path);
1513 : 724 : return NULL;
1514 : : out:
1515 : 0 : return ERR_PTR(error);
1516 : : }
1517 : :
1518 : 0 : static int do_proc_readlink(struct path *path, char __user *buffer, int buflen)
1519 : : {
1520 : 4213 : char *tmp = (char*)__get_free_page(GFP_TEMPORARY);
1521 : : char *pathname;
1522 : : int len;
1523 : :
1524 [ + - ]: 4213 : if (!tmp)
1525 : : return -ENOMEM;
1526 : :
1527 : 4213 : pathname = d_path(path, tmp, PAGE_SIZE);
1528 : : len = PTR_ERR(pathname);
1529 [ + - ]: 4213 : if (IS_ERR(pathname))
1530 : : goto out;
1531 : 4213 : len = tmp + PAGE_SIZE - 1 - pathname;
1532 : :
1533 [ - + ]: 4213 : if (len > buflen)
1534 : : len = buflen;
1535 [ - + ]: 8426 : if (copy_to_user(buffer, pathname, len))
1536 : : len = -EFAULT;
1537 : : out:
1538 : 4213 : free_page((unsigned long)tmp);
1539 : 4213 : return len;
1540 : : }
1541 : :
1542 : 0 : static int proc_pid_readlink(struct dentry * dentry, char __user * buffer, int buflen)
1543 : : {
1544 : : int error = -EACCES;
1545 : 4213 : struct inode *inode = dentry->d_inode;
1546 : : struct path path;
1547 : :
1548 : : /* Are we allowed to snoop on the tasks file descriptors? */
1549 [ + - ]: 4213 : if (!proc_fd_access_allowed(inode))
1550 : : goto out;
1551 : :
1552 : 4213 : error = PROC_I(inode)->op.proc_get_link(dentry, &path);
1553 [ + - ]: 4213 : if (error)
1554 : : goto out;
1555 : :
1556 : 4213 : error = do_proc_readlink(&path, buffer, buflen);
1557 : 4213 : path_put(&path);
1558 : : out:
1559 : 0 : return error;
1560 : : }
1561 : :
1562 : : const struct inode_operations proc_pid_link_inode_operations = {
1563 : : .readlink = proc_pid_readlink,
1564 : : .follow_link = proc_pid_follow_link,
1565 : : .setattr = proc_setattr,
1566 : : };
1567 : :
1568 : :
1569 : : /* building an inode */
1570 : :
1571 : 0 : struct inode *proc_pid_make_inode(struct super_block * sb, struct task_struct *task)
1572 : : {
1573 : : struct inode * inode;
1574 : : struct proc_inode *ei;
1575 : : const struct cred *cred;
1576 : :
1577 : : /* We need a new inode */
1578 : :
1579 : 11823 : inode = new_inode(sb);
1580 [ + - ]: 11823 : if (!inode)
1581 : : goto out;
1582 : :
1583 : : /* Common stuff */
1584 : : ei = PROC_I(inode);
1585 : 11823 : inode->i_ino = get_next_ino();
1586 : 11823 : inode->i_mtime = inode->i_atime = inode->i_ctime = CURRENT_TIME;
1587 : 11823 : inode->i_op = &proc_def_inode_operations;
1588 : :
1589 : : /*
1590 : : * grab the reference to task.
1591 : : */
1592 : 11823 : ei->pid = get_task_pid(task, PIDTYPE_PID);
1593 [ + - ]: 11823 : if (!ei->pid)
1594 : : goto out_unlock;
1595 : :
1596 [ + + ]: 11823 : if (task_dumpable(task)) {
1597 : : rcu_read_lock();
1598 : 5031 : cred = __task_cred(task);
1599 : 5031 : inode->i_uid = cred->euid;
1600 : 5031 : inode->i_gid = cred->egid;
1601 : : rcu_read_unlock();
1602 : : }
1603 : 11823 : security_task_to_inode(task, inode);
1604 : :
1605 : : out:
1606 : 11823 : return inode;
1607 : :
1608 : : out_unlock:
1609 : 0 : iput(inode);
1610 : 0 : return NULL;
1611 : : }
1612 : :
1613 : 0 : int pid_getattr(struct vfsmount *mnt, struct dentry *dentry, struct kstat *stat)
1614 : : {
1615 : 12415 : struct inode *inode = dentry->d_inode;
1616 : : struct task_struct *task;
1617 : : const struct cred *cred;
1618 : 24830 : struct pid_namespace *pid = dentry->d_sb->s_fs_info;
1619 : :
1620 : 12415 : generic_fillattr(inode, stat);
1621 : :
1622 : : rcu_read_lock();
1623 : 12415 : stat->uid = GLOBAL_ROOT_UID;
1624 : 12415 : stat->gid = GLOBAL_ROOT_GID;
1625 : 12415 : task = pid_task(proc_pid(inode), PIDTYPE_PID);
1626 [ + - ]: 12415 : if (task) {
1627 [ - + ]: 12415 : if (!has_pid_permissions(pid, task, 2)) {
1628 : : rcu_read_unlock();
1629 : : /*
1630 : : * This doesn't prevent learning whether PID exists,
1631 : : * it only makes getattr() consistent with readdir().
1632 : : */
1633 : 0 : return -ENOENT;
1634 : : }
1635 [ + + ][ + - ]: 12417 : if ((inode->i_mode == (S_IFDIR|S_IRUGO|S_IXUGO)) ||
1636 : : task_dumpable(task)) {
1637 : 12415 : cred = __task_cred(task);
1638 : 12415 : stat->uid = cred->euid;
1639 : 12415 : stat->gid = cred->egid;
1640 : : }
1641 : : }
1642 : : rcu_read_unlock();
1643 : 12415 : return 0;
1644 : : }
1645 : :
1646 : : /* dentry stuff */
1647 : :
1648 : : /*
1649 : : * Exceptional case: normally we are not allowed to unhash a busy
1650 : : * directory. In this case, however, we can do it - no aliasing problems
1651 : : * due to the way we treat inodes.
1652 : : *
1653 : : * Rewrite the inode's ownerships here because the owning task may have
1654 : : * performed a setuid(), etc.
1655 : : *
1656 : : * Before the /proc/pid/status file was created the only way to read
1657 : : * the effective uid of a /process was to stat /proc/pid. Reading
1658 : : * /proc/pid/status is slow enough that procps and other packages
1659 : : * kept stating /proc/pid. To keep the rules in /proc simple I have
1660 : : * made this apply to all per process world readable and executable
1661 : : * directories.
1662 : : */
1663 : 0 : int pid_revalidate(struct dentry *dentry, unsigned int flags)
1664 : : {
1665 : : struct inode *inode;
1666 : : struct task_struct *task;
1667 : : const struct cred *cred;
1668 : :
1669 [ + + ]: 122332 : if (flags & LOOKUP_RCU)
1670 : : return -ECHILD;
1671 : :
1672 : 80924 : inode = dentry->d_inode;
1673 : : task = get_proc_task(inode);
1674 : :
1675 [ + - ]: 80924 : if (task) {
1676 [ + + ][ + + ]: 113774 : if ((inode->i_mode == (S_IFDIR|S_IRUGO|S_IXUGO)) ||
1677 : : task_dumpable(task)) {
1678 : : rcu_read_lock();
1679 : 60156 : cred = __task_cred(task);
1680 : 60156 : inode->i_uid = cred->euid;
1681 : 60156 : inode->i_gid = cred->egid;
1682 : : rcu_read_unlock();
1683 : : } else {
1684 : 20768 : inode->i_uid = GLOBAL_ROOT_UID;
1685 : 20768 : inode->i_gid = GLOBAL_ROOT_GID;
1686 : : }
1687 : 80924 : inode->i_mode &= ~(S_ISUID | S_ISGID);
1688 : 80924 : security_task_to_inode(task, inode);
1689 : : put_task_struct(task);
1690 : : return 1;
1691 : : }
1692 : 0 : d_drop(dentry);
1693 : 0 : return 0;
1694 : : }
1695 : :
1696 : 0 : int pid_delete_dentry(const struct dentry *dentry)
1697 : : {
1698 : : /* Is the task we represent dead?
1699 : : * If so, then don't put the dentry on the lru list,
1700 : : * kill it immediately.
1701 : : */
1702 : 76370 : return !proc_pid(dentry->d_inode)->tasks[PIDTYPE_PID].first;
1703 : : }
1704 : :
1705 : : const struct dentry_operations pid_dentry_operations =
1706 : : {
1707 : : .d_revalidate = pid_revalidate,
1708 : : .d_delete = pid_delete_dentry,
1709 : : };
1710 : :
1711 : : /* Lookups */
1712 : :
1713 : : /*
1714 : : * Fill a directory entry.
1715 : : *
1716 : : * If possible create the dcache entry and derive our inode number and
1717 : : * file type from dcache entry.
1718 : : *
1719 : : * Since all of the proc inode numbers are dynamically generated, the inode
1720 : : * numbers do not exist until the inode is cache. This means creating the
1721 : : * the dcache entry in readdir is necessary to keep the inode numbers
1722 : : * reported by readdir in sync with the inode numbers reported
1723 : : * by stat.
1724 : : */
1725 : 0 : bool proc_fill_cache(struct file *file, struct dir_context *ctx,
1726 : : const char *name, int len,
1727 : : instantiate_t instantiate, struct task_struct *task, const void *ptr)
1728 : : {
1729 : 18651 : struct dentry *child, *dir = file->f_path.dentry;
1730 : 18651 : struct qstr qname = QSTR_INIT(name, len);
1731 : : struct inode *inode;
1732 : : unsigned type;
1733 : : ino_t ino;
1734 : :
1735 : 18651 : child = d_hash_and_lookup(dir, &qname);
1736 [ + + ]: 18651 : if (!child) {
1737 : 5196 : child = d_alloc(dir, &qname);
1738 [ + - ]: 5196 : if (!child)
1739 : : goto end_instantiate;
1740 [ - + ]: 5196 : if (instantiate(dir->d_inode, child, task, ptr) < 0) {
1741 : 0 : dput(child);
1742 : 0 : goto end_instantiate;
1743 : : }
1744 : : }
1745 : 18651 : inode = child->d_inode;
1746 : 18651 : ino = inode->i_ino;
1747 : 18651 : type = inode->i_mode >> 12;
1748 : 18651 : dput(child);
1749 : 18651 : return dir_emit(ctx, name, len, ino, type);
1750 : :
1751 : : end_instantiate:
1752 : 0 : return dir_emit(ctx, name, len, 1, DT_UNKNOWN);
1753 : : }
1754 : :
1755 : : #ifdef CONFIG_CHECKPOINT_RESTORE
1756 : :
1757 : : /*
1758 : : * dname_to_vma_addr - maps a dentry name into two unsigned longs
1759 : : * which represent vma start and end addresses.
1760 : : */
1761 : : static int dname_to_vma_addr(struct dentry *dentry,
1762 : : unsigned long *start, unsigned long *end)
1763 : : {
1764 : : if (sscanf(dentry->d_name.name, "%lx-%lx", start, end) != 2)
1765 : : return -EINVAL;
1766 : :
1767 : : return 0;
1768 : : }
1769 : :
1770 : : static int map_files_d_revalidate(struct dentry *dentry, unsigned int flags)
1771 : : {
1772 : : unsigned long vm_start, vm_end;
1773 : : bool exact_vma_exists = false;
1774 : : struct mm_struct *mm = NULL;
1775 : : struct task_struct *task;
1776 : : const struct cred *cred;
1777 : : struct inode *inode;
1778 : : int status = 0;
1779 : :
1780 : : if (flags & LOOKUP_RCU)
1781 : : return -ECHILD;
1782 : :
1783 : : if (!capable(CAP_SYS_ADMIN)) {
1784 : : status = -EPERM;
1785 : : goto out_notask;
1786 : : }
1787 : :
1788 : : inode = dentry->d_inode;
1789 : : task = get_proc_task(inode);
1790 : : if (!task)
1791 : : goto out_notask;
1792 : :
1793 : : mm = mm_access(task, PTRACE_MODE_READ);
1794 : : if (IS_ERR_OR_NULL(mm))
1795 : : goto out;
1796 : :
1797 : : if (!dname_to_vma_addr(dentry, &vm_start, &vm_end)) {
1798 : : down_read(&mm->mmap_sem);
1799 : : exact_vma_exists = !!find_exact_vma(mm, vm_start, vm_end);
1800 : : up_read(&mm->mmap_sem);
1801 : : }
1802 : :
1803 : : mmput(mm);
1804 : :
1805 : : if (exact_vma_exists) {
1806 : : if (task_dumpable(task)) {
1807 : : rcu_read_lock();
1808 : : cred = __task_cred(task);
1809 : : inode->i_uid = cred->euid;
1810 : : inode->i_gid = cred->egid;
1811 : : rcu_read_unlock();
1812 : : } else {
1813 : : inode->i_uid = GLOBAL_ROOT_UID;
1814 : : inode->i_gid = GLOBAL_ROOT_GID;
1815 : : }
1816 : : security_task_to_inode(task, inode);
1817 : : status = 1;
1818 : : }
1819 : :
1820 : : out:
1821 : : put_task_struct(task);
1822 : :
1823 : : out_notask:
1824 : : if (status <= 0)
1825 : : d_drop(dentry);
1826 : :
1827 : : return status;
1828 : : }
1829 : :
1830 : : static const struct dentry_operations tid_map_files_dentry_operations = {
1831 : : .d_revalidate = map_files_d_revalidate,
1832 : : .d_delete = pid_delete_dentry,
1833 : : };
1834 : :
1835 : : static int proc_map_files_get_link(struct dentry *dentry, struct path *path)
1836 : : {
1837 : : unsigned long vm_start, vm_end;
1838 : : struct vm_area_struct *vma;
1839 : : struct task_struct *task;
1840 : : struct mm_struct *mm;
1841 : : int rc;
1842 : :
1843 : : rc = -ENOENT;
1844 : : task = get_proc_task(dentry->d_inode);
1845 : : if (!task)
1846 : : goto out;
1847 : :
1848 : : mm = get_task_mm(task);
1849 : : put_task_struct(task);
1850 : : if (!mm)
1851 : : goto out;
1852 : :
1853 : : rc = dname_to_vma_addr(dentry, &vm_start, &vm_end);
1854 : : if (rc)
1855 : : goto out_mmput;
1856 : :
1857 : : down_read(&mm->mmap_sem);
1858 : : vma = find_exact_vma(mm, vm_start, vm_end);
1859 : : if (vma && vma->vm_file) {
1860 : : *path = vma->vm_file->f_path;
1861 : : path_get(path);
1862 : : rc = 0;
1863 : : }
1864 : : up_read(&mm->mmap_sem);
1865 : :
1866 : : out_mmput:
1867 : : mmput(mm);
1868 : : out:
1869 : : return rc;
1870 : : }
1871 : :
1872 : : struct map_files_info {
1873 : : fmode_t mode;
1874 : : unsigned long len;
1875 : : unsigned char name[4*sizeof(long)+2]; /* max: %lx-%lx\0 */
1876 : : };
1877 : :
1878 : : static int
1879 : : proc_map_files_instantiate(struct inode *dir, struct dentry *dentry,
1880 : : struct task_struct *task, const void *ptr)
1881 : : {
1882 : : fmode_t mode = (fmode_t)(unsigned long)ptr;
1883 : : struct proc_inode *ei;
1884 : : struct inode *inode;
1885 : :
1886 : : inode = proc_pid_make_inode(dir->i_sb, task);
1887 : : if (!inode)
1888 : : return -ENOENT;
1889 : :
1890 : : ei = PROC_I(inode);
1891 : : ei->op.proc_get_link = proc_map_files_get_link;
1892 : :
1893 : : inode->i_op = &proc_pid_link_inode_operations;
1894 : : inode->i_size = 64;
1895 : : inode->i_mode = S_IFLNK;
1896 : :
1897 : : if (mode & FMODE_READ)
1898 : : inode->i_mode |= S_IRUSR;
1899 : : if (mode & FMODE_WRITE)
1900 : : inode->i_mode |= S_IWUSR;
1901 : :
1902 : : d_set_d_op(dentry, &tid_map_files_dentry_operations);
1903 : : d_add(dentry, inode);
1904 : :
1905 : : return 0;
1906 : : }
1907 : :
1908 : : static struct dentry *proc_map_files_lookup(struct inode *dir,
1909 : : struct dentry *dentry, unsigned int flags)
1910 : : {
1911 : : unsigned long vm_start, vm_end;
1912 : : struct vm_area_struct *vma;
1913 : : struct task_struct *task;
1914 : : int result;
1915 : : struct mm_struct *mm;
1916 : :
1917 : : result = -EPERM;
1918 : : if (!capable(CAP_SYS_ADMIN))
1919 : : goto out;
1920 : :
1921 : : result = -ENOENT;
1922 : : task = get_proc_task(dir);
1923 : : if (!task)
1924 : : goto out;
1925 : :
1926 : : result = -EACCES;
1927 : : if (!ptrace_may_access(task, PTRACE_MODE_READ))
1928 : : goto out_put_task;
1929 : :
1930 : : result = -ENOENT;
1931 : : if (dname_to_vma_addr(dentry, &vm_start, &vm_end))
1932 : : goto out_put_task;
1933 : :
1934 : : mm = get_task_mm(task);
1935 : : if (!mm)
1936 : : goto out_put_task;
1937 : :
1938 : : down_read(&mm->mmap_sem);
1939 : : vma = find_exact_vma(mm, vm_start, vm_end);
1940 : : if (!vma)
1941 : : goto out_no_vma;
1942 : :
1943 : : if (vma->vm_file)
1944 : : result = proc_map_files_instantiate(dir, dentry, task,
1945 : : (void *)(unsigned long)vma->vm_file->f_mode);
1946 : :
1947 : : out_no_vma:
1948 : : up_read(&mm->mmap_sem);
1949 : : mmput(mm);
1950 : : out_put_task:
1951 : : put_task_struct(task);
1952 : : out:
1953 : : return ERR_PTR(result);
1954 : : }
1955 : :
1956 : : static const struct inode_operations proc_map_files_inode_operations = {
1957 : : .lookup = proc_map_files_lookup,
1958 : : .permission = proc_fd_permission,
1959 : : .setattr = proc_setattr,
1960 : : };
1961 : :
1962 : : static int
1963 : : proc_map_files_readdir(struct file *file, struct dir_context *ctx)
1964 : : {
1965 : : struct vm_area_struct *vma;
1966 : : struct task_struct *task;
1967 : : struct mm_struct *mm;
1968 : : unsigned long nr_files, pos, i;
1969 : : struct flex_array *fa = NULL;
1970 : : struct map_files_info info;
1971 : : struct map_files_info *p;
1972 : : int ret;
1973 : :
1974 : : ret = -EPERM;
1975 : : if (!capable(CAP_SYS_ADMIN))
1976 : : goto out;
1977 : :
1978 : : ret = -ENOENT;
1979 : : task = get_proc_task(file_inode(file));
1980 : : if (!task)
1981 : : goto out;
1982 : :
1983 : : ret = -EACCES;
1984 : : if (!ptrace_may_access(task, PTRACE_MODE_READ))
1985 : : goto out_put_task;
1986 : :
1987 : : ret = 0;
1988 : : if (!dir_emit_dots(file, ctx))
1989 : : goto out_put_task;
1990 : :
1991 : : mm = get_task_mm(task);
1992 : : if (!mm)
1993 : : goto out_put_task;
1994 : : down_read(&mm->mmap_sem);
1995 : :
1996 : : nr_files = 0;
1997 : :
1998 : : /*
1999 : : * We need two passes here:
2000 : : *
2001 : : * 1) Collect vmas of mapped files with mmap_sem taken
2002 : : * 2) Release mmap_sem and instantiate entries
2003 : : *
2004 : : * otherwise we get lockdep complained, since filldir()
2005 : : * routine might require mmap_sem taken in might_fault().
2006 : : */
2007 : :
2008 : : for (vma = mm->mmap, pos = 2; vma; vma = vma->vm_next) {
2009 : : if (vma->vm_file && ++pos > ctx->pos)
2010 : : nr_files++;
2011 : : }
2012 : :
2013 : : if (nr_files) {
2014 : : fa = flex_array_alloc(sizeof(info), nr_files,
2015 : : GFP_KERNEL);
2016 : : if (!fa || flex_array_prealloc(fa, 0, nr_files,
2017 : : GFP_KERNEL)) {
2018 : : ret = -ENOMEM;
2019 : : if (fa)
2020 : : flex_array_free(fa);
2021 : : up_read(&mm->mmap_sem);
2022 : : mmput(mm);
2023 : : goto out_put_task;
2024 : : }
2025 : : for (i = 0, vma = mm->mmap, pos = 2; vma;
2026 : : vma = vma->vm_next) {
2027 : : if (!vma->vm_file)
2028 : : continue;
2029 : : if (++pos <= ctx->pos)
2030 : : continue;
2031 : :
2032 : : info.mode = vma->vm_file->f_mode;
2033 : : info.len = snprintf(info.name,
2034 : : sizeof(info.name), "%lx-%lx",
2035 : : vma->vm_start, vma->vm_end);
2036 : : if (flex_array_put(fa, i++, &info, GFP_KERNEL))
2037 : : BUG();
2038 : : }
2039 : : }
2040 : : up_read(&mm->mmap_sem);
2041 : :
2042 : : for (i = 0; i < nr_files; i++) {
2043 : : p = flex_array_get(fa, i);
2044 : : if (!proc_fill_cache(file, ctx,
2045 : : p->name, p->len,
2046 : : proc_map_files_instantiate,
2047 : : task,
2048 : : (void *)(unsigned long)p->mode))
2049 : : break;
2050 : : ctx->pos++;
2051 : : }
2052 : : if (fa)
2053 : : flex_array_free(fa);
2054 : : mmput(mm);
2055 : :
2056 : : out_put_task:
2057 : : put_task_struct(task);
2058 : : out:
2059 : : return ret;
2060 : : }
2061 : :
2062 : : static const struct file_operations proc_map_files_operations = {
2063 : : .read = generic_read_dir,
2064 : : .iterate = proc_map_files_readdir,
2065 : : .llseek = default_llseek,
2066 : : };
2067 : :
2068 : : struct timers_private {
2069 : : struct pid *pid;
2070 : : struct task_struct *task;
2071 : : struct sighand_struct *sighand;
2072 : : struct pid_namespace *ns;
2073 : : unsigned long flags;
2074 : : };
2075 : :
2076 : : static void *timers_start(struct seq_file *m, loff_t *pos)
2077 : : {
2078 : : struct timers_private *tp = m->private;
2079 : :
2080 : : tp->task = get_pid_task(tp->pid, PIDTYPE_PID);
2081 : : if (!tp->task)
2082 : : return ERR_PTR(-ESRCH);
2083 : :
2084 : : tp->sighand = lock_task_sighand(tp->task, &tp->flags);
2085 : : if (!tp->sighand)
2086 : : return ERR_PTR(-ESRCH);
2087 : :
2088 : : return seq_list_start(&tp->task->signal->posix_timers, *pos);
2089 : : }
2090 : :
2091 : : static void *timers_next(struct seq_file *m, void *v, loff_t *pos)
2092 : : {
2093 : : struct timers_private *tp = m->private;
2094 : : return seq_list_next(v, &tp->task->signal->posix_timers, pos);
2095 : : }
2096 : :
2097 : : static void timers_stop(struct seq_file *m, void *v)
2098 : : {
2099 : : struct timers_private *tp = m->private;
2100 : :
2101 : : if (tp->sighand) {
2102 : : unlock_task_sighand(tp->task, &tp->flags);
2103 : : tp->sighand = NULL;
2104 : : }
2105 : :
2106 : : if (tp->task) {
2107 : : put_task_struct(tp->task);
2108 : : tp->task = NULL;
2109 : : }
2110 : : }
2111 : :
2112 : : static int show_timer(struct seq_file *m, void *v)
2113 : : {
2114 : : struct k_itimer *timer;
2115 : : struct timers_private *tp = m->private;
2116 : : int notify;
2117 : : static char *nstr[] = {
2118 : : [SIGEV_SIGNAL] = "signal",
2119 : : [SIGEV_NONE] = "none",
2120 : : [SIGEV_THREAD] = "thread",
2121 : : };
2122 : :
2123 : : timer = list_entry((struct list_head *)v, struct k_itimer, list);
2124 : : notify = timer->it_sigev_notify;
2125 : :
2126 : : seq_printf(m, "ID: %d\n", timer->it_id);
2127 : : seq_printf(m, "signal: %d/%p\n", timer->sigq->info.si_signo,
2128 : : timer->sigq->info.si_value.sival_ptr);
2129 : : seq_printf(m, "notify: %s/%s.%d\n",
2130 : : nstr[notify & ~SIGEV_THREAD_ID],
2131 : : (notify & SIGEV_THREAD_ID) ? "tid" : "pid",
2132 : : pid_nr_ns(timer->it_pid, tp->ns));
2133 : : seq_printf(m, "ClockID: %d\n", timer->it_clock);
2134 : :
2135 : : return 0;
2136 : : }
2137 : :
2138 : : static const struct seq_operations proc_timers_seq_ops = {
2139 : : .start = timers_start,
2140 : : .next = timers_next,
2141 : : .stop = timers_stop,
2142 : : .show = show_timer,
2143 : : };
2144 : :
2145 : : static int proc_timers_open(struct inode *inode, struct file *file)
2146 : : {
2147 : : struct timers_private *tp;
2148 : :
2149 : : tp = __seq_open_private(file, &proc_timers_seq_ops,
2150 : : sizeof(struct timers_private));
2151 : : if (!tp)
2152 : : return -ENOMEM;
2153 : :
2154 : : tp->pid = proc_pid(inode);
2155 : : tp->ns = inode->i_sb->s_fs_info;
2156 : : return 0;
2157 : : }
2158 : :
2159 : : static const struct file_operations proc_timers_operations = {
2160 : : .open = proc_timers_open,
2161 : : .read = seq_read,
2162 : : .llseek = seq_lseek,
2163 : : .release = seq_release_private,
2164 : : };
2165 : : #endif /* CONFIG_CHECKPOINT_RESTORE */
2166 : :
2167 : 0 : static int proc_pident_instantiate(struct inode *dir,
2168 : : struct dentry *dentry, struct task_struct *task, const void *ptr)
2169 : : {
2170 : : const struct pid_entry *p = ptr;
2171 : : struct inode *inode;
2172 : : struct proc_inode *ei;
2173 : :
2174 : 4449 : inode = proc_pid_make_inode(dir->i_sb, task);
2175 [ + - ]: 4449 : if (!inode)
2176 : : goto out;
2177 : :
2178 : : ei = PROC_I(inode);
2179 : 4449 : inode->i_mode = p->mode;
2180 [ + + ]: 4449 : if (S_ISDIR(inode->i_mode))
2181 : 1371 : set_nlink(inode, 2); /* Use getattr to fix if necessary */
2182 [ + + ]: 8898 : if (p->iop)
2183 : 2312 : inode->i_op = p->iop;
2184 [ + + ]: 4449 : if (p->fop)
2185 : 3509 : inode->i_fop = p->fop;
2186 : 4449 : ei->op = p->op;
2187 : 4449 : d_set_d_op(dentry, &pid_dentry_operations);
2188 : : d_add(dentry, inode);
2189 : : /* Close the race of the process dying before we return the dentry */
2190 [ - + ]: 4449 : if (pid_revalidate(dentry, 0))
2191 : : return 0;
2192 : : out:
2193 : : return -ENOENT;
2194 : : }
2195 : :
2196 : 0 : static struct dentry *proc_pident_lookup(struct inode *dir,
2197 : : struct dentry *dentry,
2198 : : const struct pid_entry *ents,
2199 : : unsigned int nents)
2200 : : {
2201 : : int error;
2202 : : struct task_struct *task = get_proc_task(dir);
2203 : : const struct pid_entry *p, *last;
2204 : :
2205 : : error = -ENOENT;
2206 : :
2207 [ + - ]: 4372 : if (!task)
2208 : : goto out_no_task;
2209 : :
2210 : : /*
2211 : : * Yes, it does not scale. And it should not. Don't add
2212 : : * new entries into /proc/<tgid>/ without very good reasons.
2213 : : */
2214 : 4372 : last = &ents[nents - 1];
2215 [ + + ]: 50815 : for (p = ents; p <= last; p++) {
2216 [ + + ]: 50808 : if (p->len != dentry->d_name.len)
2217 : 37985 : continue;
2218 [ + + ]: 12823 : if (!memcmp(dentry->d_name.name, p->name, p->len))
2219 : : break;
2220 : : }
2221 [ + + ]: 4372 : if (p > last)
2222 : : goto out;
2223 : :
2224 : 4365 : error = proc_pident_instantiate(dir, dentry, task, p);
2225 : : out:
2226 : : put_task_struct(task);
2227 : : out_no_task:
2228 : 4372 : return ERR_PTR(error);
2229 : : }
2230 : :
2231 : 0 : static int proc_pident_readdir(struct file *file, struct dir_context *ctx,
2232 : : const struct pid_entry *ents, unsigned int nents)
2233 : : {
2234 : : struct task_struct *task = get_proc_task(file_inode(file));
2235 : : const struct pid_entry *p;
2236 : :
2237 [ + - ]: 8 : if (!task)
2238 : : return -ENOENT;
2239 : :
2240 [ + - ]: 8 : if (!dir_emit_dots(file, ctx))
2241 : : goto out;
2242 : :
2243 [ + + ]: 8 : if (ctx->pos >= nents + 2)
2244 : : goto out;
2245 : :
2246 [ + + ]: 88 : for (p = ents + (ctx->pos - 2); p <= ents + nents - 1; p++) {
2247 [ + - ]: 84 : if (!proc_fill_cache(file, ctx, p->name, p->len,
2248 : : proc_pident_instantiate, task, p))
2249 : : break;
2250 : 84 : ctx->pos++;
2251 : : }
2252 : : out:
2253 : : put_task_struct(task);
2254 : : return 0;
2255 : : }
2256 : :
2257 : : #ifdef CONFIG_SECURITY
2258 : 0 : static ssize_t proc_pid_attr_read(struct file * file, char __user * buf,
2259 : : size_t count, loff_t *ppos)
2260 : : {
2261 : : struct inode * inode = file_inode(file);
2262 : 14 : char *p = NULL;
2263 : : ssize_t length;
2264 : : struct task_struct *task = get_proc_task(inode);
2265 : :
2266 [ + - ]: 14 : if (!task)
2267 : : return -ESRCH;
2268 : :
2269 : 14 : length = security_getprocattr(task,
2270 : 14 : (char*)file->f_path.dentry->d_name.name,
2271 : : &p);
2272 : : put_task_struct(task);
2273 [ + + ]: 14 : if (length > 0)
2274 : 4 : length = simple_read_from_buffer(buf, count, ppos, p, length);
2275 : 14 : kfree(p);
2276 : 14 : return length;
2277 : : }
2278 : :
2279 : 0 : static ssize_t proc_pid_attr_write(struct file * file, const char __user * buf,
2280 : : size_t count, loff_t *ppos)
2281 : : {
2282 : : struct inode * inode = file_inode(file);
2283 : : char *page;
2284 : : ssize_t length;
2285 : : struct task_struct *task = get_proc_task(inode);
2286 : :
2287 : : length = -ESRCH;
2288 [ # # ]: 0 : if (!task)
2289 : : goto out_no_task;
2290 [ # # ]: 0 : if (count > PAGE_SIZE)
2291 : : count = PAGE_SIZE;
2292 : :
2293 : : /* No partial writes. */
2294 : : length = -EINVAL;
2295 [ # # ]: 0 : if (*ppos != 0)
2296 : : goto out;
2297 : :
2298 : : length = -ENOMEM;
2299 : 0 : page = (char*)__get_free_page(GFP_TEMPORARY);
2300 [ # # ]: 0 : if (!page)
2301 : : goto out;
2302 : :
2303 : : length = -EFAULT;
2304 [ # # ]: 0 : if (copy_from_user(page, buf, count))
2305 : : goto out_free;
2306 : :
2307 : : /* Guard against adverse ptrace interaction */
2308 : 0 : length = mutex_lock_interruptible(&task->signal->cred_guard_mutex);
2309 [ # # ]: 0 : if (length < 0)
2310 : : goto out_free;
2311 : :
2312 : 0 : length = security_setprocattr(task,
2313 : 0 : (char*)file->f_path.dentry->d_name.name,
2314 : : (void*)page, count);
2315 : 0 : mutex_unlock(&task->signal->cred_guard_mutex);
2316 : : out_free:
2317 : 0 : free_page((unsigned long) page);
2318 : : out:
2319 : : put_task_struct(task);
2320 : : out_no_task:
2321 : 0 : return length;
2322 : : }
2323 : :
2324 : : static const struct file_operations proc_pid_attr_operations = {
2325 : : .read = proc_pid_attr_read,
2326 : : .write = proc_pid_attr_write,
2327 : : .llseek = generic_file_llseek,
2328 : : };
2329 : :
2330 : : static const struct pid_entry attr_dir_stuff[] = {
2331 : : REG("current", S_IRUGO|S_IWUGO, proc_pid_attr_operations),
2332 : : REG("prev", S_IRUGO, proc_pid_attr_operations),
2333 : : REG("exec", S_IRUGO|S_IWUGO, proc_pid_attr_operations),
2334 : : REG("fscreate", S_IRUGO|S_IWUGO, proc_pid_attr_operations),
2335 : : REG("keycreate", S_IRUGO|S_IWUGO, proc_pid_attr_operations),
2336 : : REG("sockcreate", S_IRUGO|S_IWUGO, proc_pid_attr_operations),
2337 : : };
2338 : :
2339 : 0 : static int proc_attr_dir_readdir(struct file *file, struct dir_context *ctx)
2340 : : {
2341 : 4 : return proc_pident_readdir(file, ctx,
2342 : : attr_dir_stuff, ARRAY_SIZE(attr_dir_stuff));
2343 : : }
2344 : :
2345 : : static const struct file_operations proc_attr_dir_operations = {
2346 : : .read = generic_read_dir,
2347 : : .iterate = proc_attr_dir_readdir,
2348 : : .llseek = default_llseek,
2349 : : };
2350 : :
2351 : 0 : static struct dentry *proc_attr_dir_lookup(struct inode *dir,
2352 : : struct dentry *dentry, unsigned int flags)
2353 : : {
2354 : 0 : return proc_pident_lookup(dir, dentry,
2355 : : attr_dir_stuff, ARRAY_SIZE(attr_dir_stuff));
2356 : : }
2357 : :
2358 : : static const struct inode_operations proc_attr_dir_inode_operations = {
2359 : : .lookup = proc_attr_dir_lookup,
2360 : : .getattr = pid_getattr,
2361 : : .setattr = proc_setattr,
2362 : : };
2363 : :
2364 : : #endif
2365 : :
2366 : : #ifdef CONFIG_ELF_CORE
2367 : 0 : static ssize_t proc_coredump_filter_read(struct file *file, char __user *buf,
2368 : : size_t count, loff_t *ppos)
2369 : : {
2370 : : struct task_struct *task = get_proc_task(file_inode(file));
2371 : : struct mm_struct *mm;
2372 : : char buffer[PROC_NUMBUF];
2373 : : size_t len;
2374 : : int ret;
2375 : :
2376 [ + - ]: 2 : if (!task)
2377 : : return -ESRCH;
2378 : :
2379 : : ret = 0;
2380 : 2 : mm = get_task_mm(task);
2381 [ + - ]: 2 : if (mm) {
2382 : 2 : len = snprintf(buffer, sizeof(buffer), "%08lx\n",
2383 : 2 : ((mm->flags & MMF_DUMP_FILTER_MASK) >>
2384 : : MMF_DUMP_FILTER_SHIFT));
2385 : 2 : mmput(mm);
2386 : 2 : ret = simple_read_from_buffer(buf, count, ppos, buffer, len);
2387 : : }
2388 : :
2389 : : put_task_struct(task);
2390 : :
2391 : 2 : return ret;
2392 : : }
2393 : :
2394 : 0 : static ssize_t proc_coredump_filter_write(struct file *file,
2395 : : const char __user *buf,
2396 : : size_t count,
2397 : : loff_t *ppos)
2398 : : {
2399 : : struct task_struct *task;
2400 : : struct mm_struct *mm;
2401 : : char buffer[PROC_NUMBUF], *end;
2402 : : unsigned int val;
2403 : : int ret;
2404 : : int i;
2405 : : unsigned long mask;
2406 : :
2407 : : ret = -EFAULT;
2408 : 0 : memset(buffer, 0, sizeof(buffer));
2409 [ # # ]: 0 : if (count > sizeof(buffer) - 1)
2410 : : count = sizeof(buffer) - 1;
2411 [ # # ]: 0 : if (copy_from_user(buffer, buf, count))
2412 : : goto out_no_task;
2413 : :
2414 : : ret = -EINVAL;
2415 : 0 : val = (unsigned int)simple_strtoul(buffer, &end, 0);
2416 [ # # ]: 0 : if (*end == '\n')
2417 : 0 : end++;
2418 [ # # ]: 0 : if (end - buffer == 0)
2419 : : goto out_no_task;
2420 : :
2421 : : ret = -ESRCH;
2422 : : task = get_proc_task(file_inode(file));
2423 [ # # ]: 0 : if (!task)
2424 : : goto out_no_task;
2425 : :
2426 : 0 : ret = end - buffer;
2427 : 0 : mm = get_task_mm(task);
2428 [ # # ]: 0 : if (!mm)
2429 : : goto out_no_mm;
2430 : :
2431 [ # # ]: 0 : for (i = 0, mask = 1; i < MMF_DUMP_FILTER_BITS; i++, mask <<= 1) {
2432 [ # # ]: 0 : if (val & mask)
2433 : 0 : set_bit(i + MMF_DUMP_FILTER_SHIFT, &mm->flags);
2434 : : else
2435 : 0 : clear_bit(i + MMF_DUMP_FILTER_SHIFT, &mm->flags);
2436 : : }
2437 : :
2438 : 0 : mmput(mm);
2439 : : out_no_mm:
2440 : : put_task_struct(task);
2441 : : out_no_task:
2442 : 0 : return ret;
2443 : : }
2444 : :
2445 : : static const struct file_operations proc_coredump_filter_operations = {
2446 : : .read = proc_coredump_filter_read,
2447 : : .write = proc_coredump_filter_write,
2448 : : .llseek = generic_file_llseek,
2449 : : };
2450 : : #endif
2451 : :
2452 : : #ifdef CONFIG_TASK_IO_ACCOUNTING
2453 : : static int do_io_accounting(struct task_struct *task, char *buffer, int whole)
2454 : : {
2455 : : struct task_io_accounting acct = task->ioac;
2456 : : unsigned long flags;
2457 : : int result;
2458 : :
2459 : : result = mutex_lock_killable(&task->signal->cred_guard_mutex);
2460 : : if (result)
2461 : : return result;
2462 : :
2463 : : if (!ptrace_may_access(task, PTRACE_MODE_READ)) {
2464 : : result = -EACCES;
2465 : : goto out_unlock;
2466 : : }
2467 : :
2468 : : if (whole && lock_task_sighand(task, &flags)) {
2469 : : struct task_struct *t = task;
2470 : :
2471 : : task_io_accounting_add(&acct, &task->signal->ioac);
2472 : : while_each_thread(task, t)
2473 : : task_io_accounting_add(&acct, &t->ioac);
2474 : :
2475 : : unlock_task_sighand(task, &flags);
2476 : : }
2477 : : result = sprintf(buffer,
2478 : : "rchar: %llu\n"
2479 : : "wchar: %llu\n"
2480 : : "syscr: %llu\n"
2481 : : "syscw: %llu\n"
2482 : : "read_bytes: %llu\n"
2483 : : "write_bytes: %llu\n"
2484 : : "cancelled_write_bytes: %llu\n",
2485 : : (unsigned long long)acct.rchar,
2486 : : (unsigned long long)acct.wchar,
2487 : : (unsigned long long)acct.syscr,
2488 : : (unsigned long long)acct.syscw,
2489 : : (unsigned long long)acct.read_bytes,
2490 : : (unsigned long long)acct.write_bytes,
2491 : : (unsigned long long)acct.cancelled_write_bytes);
2492 : : out_unlock:
2493 : : mutex_unlock(&task->signal->cred_guard_mutex);
2494 : : return result;
2495 : : }
2496 : :
2497 : : static int proc_tid_io_accounting(struct task_struct *task, char *buffer)
2498 : : {
2499 : : return do_io_accounting(task, buffer, 0);
2500 : : }
2501 : :
2502 : : static int proc_tgid_io_accounting(struct task_struct *task, char *buffer)
2503 : : {
2504 : : return do_io_accounting(task, buffer, 1);
2505 : : }
2506 : : #endif /* CONFIG_TASK_IO_ACCOUNTING */
2507 : :
2508 : : #ifdef CONFIG_USER_NS
2509 : : static int proc_id_map_open(struct inode *inode, struct file *file,
2510 : : struct seq_operations *seq_ops)
2511 : : {
2512 : : struct user_namespace *ns = NULL;
2513 : : struct task_struct *task;
2514 : : struct seq_file *seq;
2515 : : int ret = -EINVAL;
2516 : :
2517 : : task = get_proc_task(inode);
2518 : : if (task) {
2519 : : rcu_read_lock();
2520 : : ns = get_user_ns(task_cred_xxx(task, user_ns));
2521 : : rcu_read_unlock();
2522 : : put_task_struct(task);
2523 : : }
2524 : : if (!ns)
2525 : : goto err;
2526 : :
2527 : : ret = seq_open(file, seq_ops);
2528 : : if (ret)
2529 : : goto err_put_ns;
2530 : :
2531 : : seq = file->private_data;
2532 : : seq->private = ns;
2533 : :
2534 : : return 0;
2535 : : err_put_ns:
2536 : : put_user_ns(ns);
2537 : : err:
2538 : : return ret;
2539 : : }
2540 : :
2541 : : static int proc_id_map_release(struct inode *inode, struct file *file)
2542 : : {
2543 : : struct seq_file *seq = file->private_data;
2544 : : struct user_namespace *ns = seq->private;
2545 : : put_user_ns(ns);
2546 : : return seq_release(inode, file);
2547 : : }
2548 : :
2549 : : static int proc_uid_map_open(struct inode *inode, struct file *file)
2550 : : {
2551 : : return proc_id_map_open(inode, file, &proc_uid_seq_operations);
2552 : : }
2553 : :
2554 : : static int proc_gid_map_open(struct inode *inode, struct file *file)
2555 : : {
2556 : : return proc_id_map_open(inode, file, &proc_gid_seq_operations);
2557 : : }
2558 : :
2559 : : static int proc_projid_map_open(struct inode *inode, struct file *file)
2560 : : {
2561 : : return proc_id_map_open(inode, file, &proc_projid_seq_operations);
2562 : : }
2563 : :
2564 : : static const struct file_operations proc_uid_map_operations = {
2565 : : .open = proc_uid_map_open,
2566 : : .write = proc_uid_map_write,
2567 : : .read = seq_read,
2568 : : .llseek = seq_lseek,
2569 : : .release = proc_id_map_release,
2570 : : };
2571 : :
2572 : : static const struct file_operations proc_gid_map_operations = {
2573 : : .open = proc_gid_map_open,
2574 : : .write = proc_gid_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_projid_map_operations = {
2581 : : .open = proc_projid_map_open,
2582 : : .write = proc_projid_map_write,
2583 : : .read = seq_read,
2584 : : .llseek = seq_lseek,
2585 : : .release = proc_id_map_release,
2586 : : };
2587 : : #endif /* CONFIG_USER_NS */
2588 : :
2589 : 0 : static int proc_pid_personality(struct seq_file *m, struct pid_namespace *ns,
2590 : : struct pid *pid, struct task_struct *task)
2591 : : {
2592 : 2 : int err = lock_trace(task);
2593 [ + - ]: 2 : if (!err) {
2594 : 2 : seq_printf(m, "%08x\n", task->personality);
2595 : : unlock_trace(task);
2596 : : }
2597 : 0 : return err;
2598 : : }
2599 : :
2600 : : /*
2601 : : * Thread groups
2602 : : */
2603 : : static const struct file_operations proc_task_operations;
2604 : : static const struct inode_operations proc_task_inode_operations;
2605 : :
2606 : : static const struct pid_entry tgid_base_stuff[] = {
2607 : : DIR("task", S_IRUGO|S_IXUGO, proc_task_inode_operations, proc_task_operations),
2608 : : DIR("fd", S_IRUSR|S_IXUSR, proc_fd_inode_operations, proc_fd_operations),
2609 : : #ifdef CONFIG_CHECKPOINT_RESTORE
2610 : : DIR("map_files", S_IRUSR|S_IXUSR, proc_map_files_inode_operations, proc_map_files_operations),
2611 : : #endif
2612 : : DIR("fdinfo", S_IRUSR|S_IXUSR, proc_fdinfo_inode_operations, proc_fdinfo_operations),
2613 : : DIR("ns", S_IRUSR|S_IXUGO, proc_ns_dir_inode_operations, proc_ns_dir_operations),
2614 : : #ifdef CONFIG_NET
2615 : : DIR("net", S_IRUGO|S_IXUGO, proc_net_inode_operations, proc_net_operations),
2616 : : #endif
2617 : : REG("environ", S_IRUSR, proc_environ_operations),
2618 : : INF("auxv", S_IRUSR, proc_pid_auxv),
2619 : : ONE("status", S_IRUGO, proc_pid_status),
2620 : : ONE("personality", S_IRUGO, proc_pid_personality),
2621 : : INF("limits", S_IRUGO, proc_pid_limits),
2622 : : #ifdef CONFIG_SCHED_DEBUG
2623 : : REG("sched", S_IRUGO|S_IWUSR, proc_pid_sched_operations),
2624 : : #endif
2625 : : #ifdef CONFIG_SCHED_AUTOGROUP
2626 : : REG("autogroup", S_IRUGO|S_IWUSR, proc_pid_sched_autogroup_operations),
2627 : : #endif
2628 : : REG("comm", S_IRUGO|S_IWUSR, proc_pid_set_comm_operations),
2629 : : #ifdef CONFIG_HAVE_ARCH_TRACEHOOK
2630 : : INF("syscall", S_IRUGO, proc_pid_syscall),
2631 : : #endif
2632 : : INF("cmdline", S_IRUGO, proc_pid_cmdline),
2633 : : ONE("stat", S_IRUGO, proc_tgid_stat),
2634 : : ONE("statm", S_IRUGO, proc_pid_statm),
2635 : : REG("maps", S_IRUGO, proc_pid_maps_operations),
2636 : : #ifdef CONFIG_NUMA
2637 : : REG("numa_maps", S_IRUGO, proc_pid_numa_maps_operations),
2638 : : #endif
2639 : : REG("mem", S_IRUSR|S_IWUSR, proc_mem_operations),
2640 : : LNK("cwd", proc_cwd_link),
2641 : : LNK("root", proc_root_link),
2642 : : LNK("exe", proc_exe_link),
2643 : : REG("mounts", S_IRUGO, proc_mounts_operations),
2644 : : REG("mountinfo", S_IRUGO, proc_mountinfo_operations),
2645 : : REG("mountstats", S_IRUSR, proc_mountstats_operations),
2646 : : #ifdef CONFIG_PROC_PAGE_MONITOR
2647 : : REG("clear_refs", S_IWUSR, proc_clear_refs_operations),
2648 : : REG("smaps", S_IRUGO, proc_pid_smaps_operations),
2649 : : REG("pagemap", S_IRUGO, proc_pagemap_operations),
2650 : : #endif
2651 : : #ifdef CONFIG_SECURITY
2652 : : DIR("attr", S_IRUGO|S_IXUGO, proc_attr_dir_inode_operations, proc_attr_dir_operations),
2653 : : #endif
2654 : : #ifdef CONFIG_KALLSYMS
2655 : : INF("wchan", S_IRUGO, proc_pid_wchan),
2656 : : #endif
2657 : : #ifdef CONFIG_STACKTRACE
2658 : : ONE("stack", S_IRUGO, proc_pid_stack),
2659 : : #endif
2660 : : #ifdef CONFIG_SCHEDSTATS
2661 : : INF("schedstat", S_IRUGO, proc_pid_schedstat),
2662 : : #endif
2663 : : #ifdef CONFIG_LATENCYTOP
2664 : : REG("latency", S_IRUGO, proc_lstats_operations),
2665 : : #endif
2666 : : #ifdef CONFIG_PROC_PID_CPUSET
2667 : : REG("cpuset", S_IRUGO, proc_cpuset_operations),
2668 : : #endif
2669 : : #ifdef CONFIG_CGROUPS
2670 : : REG("cgroup", S_IRUGO, proc_cgroup_operations),
2671 : : #endif
2672 : : INF("oom_score", S_IRUGO, proc_oom_score),
2673 : : ANDROID("oom_adj", S_IRUGO|S_IWUSR, oom_adj),
2674 : : REG("oom_score_adj", S_IRUGO|S_IWUSR, proc_oom_score_adj_operations),
2675 : : #ifdef CONFIG_AUDITSYSCALL
2676 : : REG("loginuid", S_IWUSR|S_IRUGO, proc_loginuid_operations),
2677 : : REG("sessionid", S_IRUGO, proc_sessionid_operations),
2678 : : #endif
2679 : : #ifdef CONFIG_FAULT_INJECTION
2680 : : REG("make-it-fail", S_IRUGO|S_IWUSR, proc_fault_inject_operations),
2681 : : #endif
2682 : : #ifdef CONFIG_ELF_CORE
2683 : : REG("coredump_filter", S_IRUGO|S_IWUSR, proc_coredump_filter_operations),
2684 : : #endif
2685 : : #ifdef CONFIG_TASK_IO_ACCOUNTING
2686 : : INF("io", S_IRUSR, proc_tgid_io_accounting),
2687 : : #endif
2688 : : #ifdef CONFIG_HARDWALL
2689 : : INF("hardwall", S_IRUGO, proc_pid_hardwall),
2690 : : #endif
2691 : : #ifdef CONFIG_USER_NS
2692 : : REG("uid_map", S_IRUGO|S_IWUSR, proc_uid_map_operations),
2693 : : REG("gid_map", S_IRUGO|S_IWUSR, proc_gid_map_operations),
2694 : : REG("projid_map", S_IRUGO|S_IWUSR, proc_projid_map_operations),
2695 : : #endif
2696 : : #ifdef CONFIG_CHECKPOINT_RESTORE
2697 : : REG("timers", S_IRUGO, proc_timers_operations),
2698 : : #endif
2699 : : };
2700 : :
2701 : 0 : static int proc_tgid_base_readdir(struct file *file, struct dir_context *ctx)
2702 : : {
2703 : 2 : return proc_pident_readdir(file, ctx,
2704 : : tgid_base_stuff, ARRAY_SIZE(tgid_base_stuff));
2705 : : }
2706 : :
2707 : : static const struct file_operations proc_tgid_base_operations = {
2708 : : .read = generic_read_dir,
2709 : : .iterate = proc_tgid_base_readdir,
2710 : : .llseek = default_llseek,
2711 : : };
2712 : :
2713 : 0 : static struct dentry *proc_tgid_base_lookup(struct inode *dir, struct dentry *dentry, unsigned int flags)
2714 : : {
2715 : 4364 : return proc_pident_lookup(dir, dentry,
2716 : : tgid_base_stuff, ARRAY_SIZE(tgid_base_stuff));
2717 : : }
2718 : :
2719 : : static const struct inode_operations proc_tgid_base_inode_operations = {
2720 : : .lookup = proc_tgid_base_lookup,
2721 : : .getattr = pid_getattr,
2722 : : .setattr = proc_setattr,
2723 : : .permission = proc_pid_permission,
2724 : : };
2725 : :
2726 : 1151922 : static void proc_flush_task_mnt(struct vfsmount *mnt, pid_t pid, pid_t tgid)
2727 : : {
2728 : : struct dentry *dentry, *leader, *dir;
2729 : : char buf[PROC_NUMBUF];
2730 : : struct qstr name;
2731 : :
2732 : 1151922 : name.name = buf;
2733 : 1151922 : name.len = snprintf(buf, sizeof(buf), "%d", pid);
2734 : : /* no ->d_hash() rejects on procfs */
2735 : 1151922 : dentry = d_hash_and_lookup(mnt->mnt_root, &name);
2736 [ + + ]: 1151920 : if (dentry) {
2737 : 2126 : shrink_dcache_parent(dentry);
2738 : 2126 : d_drop(dentry);
2739 : 2126 : dput(dentry);
2740 : : }
2741 : :
2742 : 1151920 : name.name = buf;
2743 : 1151920 : name.len = snprintf(buf, sizeof(buf), "%d", tgid);
2744 : 1151920 : leader = d_hash_and_lookup(mnt->mnt_root, &name);
2745 [ + + ]: 1151923 : if (!leader)
2746 : : goto out;
2747 : :
2748 : 103 : name.name = "task";
2749 : 103 : name.len = strlen(name.name);
2750 : 103 : dir = d_hash_and_lookup(leader, &name);
2751 [ + + ]: 103 : if (!dir)
2752 : : goto out_put_leader;
2753 : :
2754 : 3 : name.name = buf;
2755 : 3 : name.len = snprintf(buf, sizeof(buf), "%d", pid);
2756 : 3 : dentry = d_hash_and_lookup(dir, &name);
2757 [ + - ]: 3 : if (dentry) {
2758 : 3 : shrink_dcache_parent(dentry);
2759 : 3 : d_drop(dentry);
2760 : 3 : dput(dentry);
2761 : : }
2762 : :
2763 : 3 : dput(dir);
2764 : : out_put_leader:
2765 : 103 : dput(leader);
2766 : : out:
2767 : 1151923 : return;
2768 : : }
2769 : :
2770 : : /**
2771 : : * proc_flush_task - Remove dcache entries for @task from the /proc dcache.
2772 : : * @task: task that should be flushed.
2773 : : *
2774 : : * When flushing dentries from proc, one needs to flush them from global
2775 : : * proc (proc_mnt) and from all the namespaces' procs this task was seen
2776 : : * in. This call is supposed to do all of this job.
2777 : : *
2778 : : * Looks in the dcache for
2779 : : * /proc/@pid
2780 : : * /proc/@tgid/task/@pid
2781 : : * if either directory is present flushes it and all of it'ts children
2782 : : * from the dcache.
2783 : : *
2784 : : * It is safe and reasonable to cache /proc entries for a task until
2785 : : * that task exits. After that they just clog up the dcache with
2786 : : * useless entries, possibly causing useful dcache entries to be
2787 : : * flushed instead. This routine is proved to flush those useless
2788 : : * dcache entries at process exit time.
2789 : : *
2790 : : * NOTE: This routine is just an optimization so it does not guarantee
2791 : : * that no dcache entries will exist at process exit time it
2792 : : * just makes it very unlikely that any will persist.
2793 : : */
2794 : :
2795 : 0 : void proc_flush_task(struct task_struct *task)
2796 : : {
2797 : : int i;
2798 : : struct pid *pid, *tgid;
2799 : : struct upid *upid;
2800 : :
2801 : : pid = task_pid(task);
2802 : : tgid = task_tgid(task);
2803 : :
2804 [ + + ]: 2303841 : for (i = 0; i <= pid->level; i++) {
2805 : 1151920 : upid = &pid->numbers[i];
2806 : 1151920 : proc_flush_task_mnt(upid->ns->proc_mnt, upid->nr,
2807 : : tgid->numbers[i].nr);
2808 : : }
2809 : 1151921 : }
2810 : :
2811 : 0 : static int proc_pid_instantiate(struct inode *dir,
2812 : : struct dentry * dentry,
2813 : : struct task_struct *task, const void *ptr)
2814 : : {
2815 : : struct inode *inode;
2816 : :
2817 : 2212 : inode = proc_pid_make_inode(dir->i_sb, task);
2818 [ + ]: 2212 : if (!inode)
2819 : : goto out;
2820 : :
2821 : 2212 : inode->i_mode = S_IFDIR|S_IRUGO|S_IXUGO;
2822 : 2212 : inode->i_op = &proc_tgid_base_inode_operations;
2823 : 2212 : inode->i_fop = &proc_tgid_base_operations;
2824 : 4424 : inode->i_flags|=S_IMMUTABLE;
2825 : :
2826 : 2212 : set_nlink(inode, 2 + pid_entry_count_dirs(tgid_base_stuff,
2827 : : ARRAY_SIZE(tgid_base_stuff)));
2828 : :
2829 : 2212 : d_set_d_op(dentry, &pid_dentry_operations);
2830 : :
2831 : : d_add(dentry, inode);
2832 : : /* Close the race of the process dying before we return the dentry */
2833 [ - + ]: 2212 : if (pid_revalidate(dentry, 0))
2834 : : return 0;
2835 : : out:
2836 : : return -ENOENT;
2837 : : }
2838 : :
2839 : 0 : struct dentry *proc_pid_lookup(struct inode *dir, struct dentry * dentry, unsigned int flags)
2840 : : {
2841 : : int result = 0;
2842 : : struct task_struct *task;
2843 : : unsigned tgid;
2844 : : struct pid_namespace *ns;
2845 : :
2846 : : tgid = name_to_int(dentry);
2847 [ + + ]: 1899 : if (tgid == ~0U)
2848 : : goto out;
2849 : :
2850 : 1897 : ns = dentry->d_sb->s_fs_info;
2851 : : rcu_read_lock();
2852 : 1897 : task = find_task_by_pid_ns(tgid, ns);
2853 [ + + ]: 1897 : if (task)
2854 : 1785 : get_task_struct(task);
2855 : : rcu_read_unlock();
2856 [ + + ]: 1897 : if (!task)
2857 : : goto out;
2858 : :
2859 : 1785 : result = proc_pid_instantiate(dir, dentry, task, NULL);
2860 : : put_task_struct(task);
2861 : : out:
2862 : 0 : return ERR_PTR(result);
2863 : : }
2864 : :
2865 : : /*
2866 : : * Find the first task with tgid >= tgid
2867 : : *
2868 : : */
2869 : : struct tgid_iter {
2870 : : unsigned int tgid;
2871 : : struct task_struct *task;
2872 : : };
2873 : 0 : static struct tgid_iter next_tgid(struct pid_namespace *ns, struct tgid_iter iter)
2874 : : {
2875 : : struct pid *pid;
2876 : :
2877 [ + + ]: 13508 : if (iter.task)
2878 : : put_task_struct(iter.task);
2879 : : rcu_read_lock();
2880 : : retry:
2881 : : iter.task = NULL;
2882 : 14551 : pid = find_ge_pid(iter.tgid, ns);
2883 [ + + ]: 14551 : if (pid) {
2884 : 14402 : iter.tgid = pid_nr_ns(pid, ns);
2885 : 14402 : iter.task = pid_task(pid, PIDTYPE_PID);
2886 : : /* What we to know is if the pid we have find is the
2887 : : * pid of a thread_group_leader. Testing for task
2888 : : * being a thread_group_leader is the obvious thing
2889 : : * todo but there is a window when it fails, due to
2890 : : * the pid transfer logic in de_thread.
2891 : : *
2892 : : * So we perform the straight forward test of seeing
2893 : : * if the pid we have found is the pid of a thread
2894 : : * group leader, and don't worry if the task we have
2895 : : * found doesn't happen to be a thread group leader.
2896 : : * As we don't care in the case of readdir.
2897 : : */
2898 [ + + ][ + + ]: 27910 : if (!iter.task || !has_group_leader_pid(iter.task)) {
2899 : 1043 : iter.tgid += 1;
2900 : 1043 : goto retry;
2901 : : }
2902 : 13359 : get_task_struct(iter.task);
2903 : : }
2904 : : rcu_read_unlock();
2905 : 13508 : return iter;
2906 : : }
2907 : :
2908 : : #define TGID_OFFSET (FIRST_PROCESS_ENTRY + 1)
2909 : :
2910 : : /* for the /proc/ directory itself, after non-process stuff has been done */
2911 : 0 : int proc_pid_readdir(struct file *file, struct dir_context *ctx)
2912 : : {
2913 : : struct tgid_iter iter;
2914 : 13657 : struct pid_namespace *ns = file->f_dentry->d_sb->s_fs_info;
2915 : 298 : loff_t pos = ctx->pos;
2916 : :
2917 [ + + ]: 298 : if (pos >= PID_MAX_LIMIT + TGID_OFFSET)
2918 : : return 0;
2919 : :
2920 [ + - ]: 149 : if (pos == TGID_OFFSET - 1) {
2921 : 149 : struct inode *inode = ns->proc_self->d_inode;
2922 [ + - ]: 149 : if (!dir_emit(ctx, "self", 4, inode->i_ino, DT_LNK))
2923 : : return 0;
2924 : : iter.tgid = 0;
2925 : : } else {
2926 : 0 : iter.tgid = pos - TGID_OFFSET;
2927 : : }
2928 : : iter.task = NULL;
2929 [ + + ]: 13508 : for (iter = next_tgid(ns, iter);
2930 : : iter.task;
2931 : 13359 : iter.tgid += 1, iter = next_tgid(ns, iter)) {
2932 : : char name[PROC_NUMBUF];
2933 : : int len;
2934 [ - + ]: 13359 : if (!has_pid_permissions(ns, iter.task, 2))
2935 : 0 : continue;
2936 : :
2937 : 13359 : len = snprintf(name, sizeof(name), "%d", iter.tgid);
2938 : 13359 : ctx->pos = iter.tgid + TGID_OFFSET;
2939 [ - + ]: 13359 : if (!proc_fill_cache(file, ctx, name, len,
2940 : : proc_pid_instantiate, iter.task, NULL)) {
2941 : : put_task_struct(iter.task);
2942 : 13359 : return 0;
2943 : : }
2944 : : }
2945 : 149 : ctx->pos = PID_MAX_LIMIT + TGID_OFFSET;
2946 : 149 : return 0;
2947 : : }
2948 : :
2949 : : /*
2950 : : * Tasks
2951 : : */
2952 : : static const struct pid_entry tid_base_stuff[] = {
2953 : : DIR("fd", S_IRUSR|S_IXUSR, proc_fd_inode_operations, proc_fd_operations),
2954 : : DIR("fdinfo", S_IRUSR|S_IXUSR, proc_fdinfo_inode_operations, proc_fdinfo_operations),
2955 : : DIR("ns", S_IRUSR|S_IXUGO, proc_ns_dir_inode_operations, proc_ns_dir_operations),
2956 : : REG("environ", S_IRUSR, proc_environ_operations),
2957 : : INF("auxv", S_IRUSR, proc_pid_auxv),
2958 : : ONE("status", S_IRUGO, proc_pid_status),
2959 : : ONE("personality", S_IRUGO, proc_pid_personality),
2960 : : INF("limits", S_IRUGO, proc_pid_limits),
2961 : : #ifdef CONFIG_SCHED_DEBUG
2962 : : REG("sched", S_IRUGO|S_IWUSR, proc_pid_sched_operations),
2963 : : #endif
2964 : : REG("comm", S_IRUGO|S_IWUSR, proc_pid_set_comm_operations),
2965 : : #ifdef CONFIG_HAVE_ARCH_TRACEHOOK
2966 : : INF("syscall", S_IRUGO, proc_pid_syscall),
2967 : : #endif
2968 : : INF("cmdline", S_IRUGO, proc_pid_cmdline),
2969 : : ONE("stat", S_IRUGO, proc_tid_stat),
2970 : : ONE("statm", S_IRUGO, proc_pid_statm),
2971 : : REG("maps", S_IRUGO, proc_tid_maps_operations),
2972 : : #ifdef CONFIG_CHECKPOINT_RESTORE
2973 : : REG("children", S_IRUGO, proc_tid_children_operations),
2974 : : #endif
2975 : : #ifdef CONFIG_NUMA
2976 : : REG("numa_maps", S_IRUGO, proc_tid_numa_maps_operations),
2977 : : #endif
2978 : : REG("mem", S_IRUSR|S_IWUSR, proc_mem_operations),
2979 : : LNK("cwd", proc_cwd_link),
2980 : : LNK("root", proc_root_link),
2981 : : LNK("exe", proc_exe_link),
2982 : : REG("mounts", S_IRUGO, proc_mounts_operations),
2983 : : REG("mountinfo", S_IRUGO, proc_mountinfo_operations),
2984 : : #ifdef CONFIG_PROC_PAGE_MONITOR
2985 : : REG("clear_refs", S_IWUSR, proc_clear_refs_operations),
2986 : : REG("smaps", S_IRUGO, proc_tid_smaps_operations),
2987 : : REG("pagemap", S_IRUGO, proc_pagemap_operations),
2988 : : #endif
2989 : : #ifdef CONFIG_SECURITY
2990 : : DIR("attr", S_IRUGO|S_IXUGO, proc_attr_dir_inode_operations, proc_attr_dir_operations),
2991 : : #endif
2992 : : #ifdef CONFIG_KALLSYMS
2993 : : INF("wchan", S_IRUGO, proc_pid_wchan),
2994 : : #endif
2995 : : #ifdef CONFIG_STACKTRACE
2996 : : ONE("stack", S_IRUGO, proc_pid_stack),
2997 : : #endif
2998 : : #ifdef CONFIG_SCHEDSTATS
2999 : : INF("schedstat", S_IRUGO, proc_pid_schedstat),
3000 : : #endif
3001 : : #ifdef CONFIG_LATENCYTOP
3002 : : REG("latency", S_IRUGO, proc_lstats_operations),
3003 : : #endif
3004 : : #ifdef CONFIG_PROC_PID_CPUSET
3005 : : REG("cpuset", S_IRUGO, proc_cpuset_operations),
3006 : : #endif
3007 : : #ifdef CONFIG_CGROUPS
3008 : : REG("cgroup", S_IRUGO, proc_cgroup_operations),
3009 : : #endif
3010 : : INF("oom_score", S_IRUGO, proc_oom_score),
3011 : : REG("oom_adj", S_IRUGO|S_IWUSR, proc_oom_adj_operations),
3012 : : REG("oom_score_adj", S_IRUGO|S_IWUSR, proc_oom_score_adj_operations),
3013 : : #ifdef CONFIG_AUDITSYSCALL
3014 : : REG("loginuid", S_IWUSR|S_IRUGO, proc_loginuid_operations),
3015 : : REG("sessionid", S_IRUGO, proc_sessionid_operations),
3016 : : #endif
3017 : : #ifdef CONFIG_FAULT_INJECTION
3018 : : REG("make-it-fail", S_IRUGO|S_IWUSR, proc_fault_inject_operations),
3019 : : #endif
3020 : : #ifdef CONFIG_TASK_IO_ACCOUNTING
3021 : : INF("io", S_IRUSR, proc_tid_io_accounting),
3022 : : #endif
3023 : : #ifdef CONFIG_HARDWALL
3024 : : INF("hardwall", S_IRUGO, proc_pid_hardwall),
3025 : : #endif
3026 : : #ifdef CONFIG_USER_NS
3027 : : REG("uid_map", S_IRUGO|S_IWUSR, proc_uid_map_operations),
3028 : : REG("gid_map", S_IRUGO|S_IWUSR, proc_gid_map_operations),
3029 : : REG("projid_map", S_IRUGO|S_IWUSR, proc_projid_map_operations),
3030 : : #endif
3031 : : };
3032 : :
3033 : 0 : static int proc_tid_base_readdir(struct file *file, struct dir_context *ctx)
3034 : : {
3035 : 2 : return proc_pident_readdir(file, ctx,
3036 : : tid_base_stuff, ARRAY_SIZE(tid_base_stuff));
3037 : : }
3038 : :
3039 : 0 : static struct dentry *proc_tid_base_lookup(struct inode *dir, struct dentry *dentry, unsigned int flags)
3040 : : {
3041 : 8 : return proc_pident_lookup(dir, dentry,
3042 : : tid_base_stuff, ARRAY_SIZE(tid_base_stuff));
3043 : : }
3044 : :
3045 : : static const struct file_operations proc_tid_base_operations = {
3046 : : .read = generic_read_dir,
3047 : : .iterate = proc_tid_base_readdir,
3048 : : .llseek = default_llseek,
3049 : : };
3050 : :
3051 : : static const struct inode_operations proc_tid_base_inode_operations = {
3052 : : .lookup = proc_tid_base_lookup,
3053 : : .getattr = pid_getattr,
3054 : : .setattr = proc_setattr,
3055 : : };
3056 : :
3057 : 0 : static int proc_task_instantiate(struct inode *dir,
3058 : : struct dentry *dentry, struct task_struct *task, const void *ptr)
3059 : : {
3060 : : struct inode *inode;
3061 : 195 : inode = proc_pid_make_inode(dir->i_sb, task);
3062 : :
3063 [ + ]: 195 : if (!inode)
3064 : : goto out;
3065 : 195 : inode->i_mode = S_IFDIR|S_IRUGO|S_IXUGO;
3066 : 195 : inode->i_op = &proc_tid_base_inode_operations;
3067 : 195 : inode->i_fop = &proc_tid_base_operations;
3068 : 390 : inode->i_flags|=S_IMMUTABLE;
3069 : :
3070 : 195 : set_nlink(inode, 2 + pid_entry_count_dirs(tid_base_stuff,
3071 : : ARRAY_SIZE(tid_base_stuff)));
3072 : :
3073 : 195 : d_set_d_op(dentry, &pid_dentry_operations);
3074 : :
3075 : : d_add(dentry, inode);
3076 : : /* Close the race of the process dying before we return the dentry */
3077 [ - + ]: 195 : if (pid_revalidate(dentry, 0))
3078 : : return 0;
3079 : : out:
3080 : : return -ENOENT;
3081 : : }
3082 : :
3083 : 0 : static struct dentry *proc_task_lookup(struct inode *dir, struct dentry * dentry, unsigned int flags)
3084 : : {
3085 : : int result = -ENOENT;
3086 : : struct task_struct *task;
3087 : : struct task_struct *leader = get_proc_task(dir);
3088 : : unsigned tid;
3089 : : struct pid_namespace *ns;
3090 : :
3091 [ + - ]: 24 : if (!leader)
3092 : : goto out_no_task;
3093 : :
3094 : : tid = name_to_int(dentry);
3095 [ - + ]: 24 : if (tid == ~0U)
3096 : : goto out;
3097 : :
3098 : 0 : ns = dentry->d_sb->s_fs_info;
3099 : : rcu_read_lock();
3100 : 0 : task = find_task_by_pid_ns(tid, ns);
3101 [ # # ]: 0 : if (task)
3102 : 0 : get_task_struct(task);
3103 : : rcu_read_unlock();
3104 [ # # ]: 0 : if (!task)
3105 : : goto out;
3106 [ # # ]: 0 : if (!same_thread_group(leader, task))
3107 : : goto out_drop_task;
3108 : :
3109 : 0 : result = proc_task_instantiate(dir, dentry, task, NULL);
3110 : : out_drop_task:
3111 : : put_task_struct(task);
3112 : : out:
3113 : : put_task_struct(leader);
3114 : : out_no_task:
3115 : 24 : return ERR_PTR(result);
3116 : : }
3117 : :
3118 : : /*
3119 : : * Find the first tid of a thread group to return to user space.
3120 : : *
3121 : : * Usually this is just the thread group leader, but if the users
3122 : : * buffer was too small or there was a seek into the middle of the
3123 : : * directory we have more work todo.
3124 : : *
3125 : : * In the case of a short read we start with find_task_by_pid.
3126 : : *
3127 : : * In the case of a seek we start with the leader and walk nr
3128 : : * threads past it.
3129 : : */
3130 : 0 : static struct task_struct *first_tid(struct task_struct *leader,
3131 : : int tid, int nr, struct pid_namespace *ns)
3132 : : {
3133 : : struct task_struct *pos;
3134 : :
3135 : : rcu_read_lock();
3136 : : /* Attempt to start with the pid of a thread */
3137 [ - + ]: 1374 : if (tid && (nr > 0)) {
3138 : 0 : pos = find_task_by_pid_ns(tid, ns);
3139 [ # # ][ # # ]: 0 : if (pos && (pos->group_leader == leader))
3140 : : goto found;
3141 : : }
3142 : :
3143 : : /* If nr exceeds the number of threads there is nothing todo */
3144 : : pos = NULL;
3145 [ + + ][ - + ]: 1374 : if (nr && nr >= get_nr_threads(leader))
3146 : : goto out;
3147 : :
3148 : : /* If we haven't found our starting place yet start
3149 : : * with the leader and walk nr threads forward.
3150 : : */
3151 [ - + ]: 687 : for (pos = leader; nr > 0; --nr) {
3152 : 0 : pos = next_thread(pos);
3153 [ # # ]: 0 : if (pos == leader) {
3154 : : pos = NULL;
3155 : : goto out;
3156 : : }
3157 : : }
3158 : : found:
3159 : 0 : get_task_struct(pos);
3160 : : out:
3161 : : rcu_read_unlock();
3162 : 1374 : return pos;
3163 : : }
3164 : :
3165 : : /*
3166 : : * Find the next thread in the thread list.
3167 : : * Return NULL if there is an error or no next thread.
3168 : : *
3169 : : * The reference to the input task_struct is released.
3170 : : */
3171 : 0 : static struct task_struct *next_tid(struct task_struct *start)
3172 : : {
3173 : : struct task_struct *pos = NULL;
3174 : : rcu_read_lock();
3175 [ + - ]: 714 : if (pid_alive(start)) {
3176 : 714 : pos = next_thread(start);
3177 [ + + ]: 714 : if (thread_group_leader(pos))
3178 : : pos = NULL;
3179 : : else
3180 : 27 : get_task_struct(pos);
3181 : : }
3182 : : rcu_read_unlock();
3183 : : put_task_struct(start);
3184 : 714 : return pos;
3185 : : }
3186 : :
3187 : : /* for the /proc/TGID/task/ directories */
3188 : 0 : static int proc_task_readdir(struct file *file, struct dir_context *ctx)
3189 : : {
3190 : : struct task_struct *leader = NULL;
3191 : : struct task_struct *task = get_proc_task(file_inode(file));
3192 : : struct pid_namespace *ns;
3193 : : int tid;
3194 : :
3195 [ + - ]: 1374 : if (!task)
3196 : : return -ENOENT;
3197 : : rcu_read_lock();
3198 [ + - ]: 1374 : if (pid_alive(task)) {
3199 : 1374 : leader = task->group_leader;
3200 : 1374 : get_task_struct(leader);
3201 : : }
3202 : : rcu_read_unlock();
3203 : : put_task_struct(task);
3204 [ + - ]: 1374 : if (!leader)
3205 : : return -ENOENT;
3206 : :
3207 [ + - ]: 1374 : if (!dir_emit_dots(file, ctx))
3208 : : goto out;
3209 : :
3210 : : /* f_version caches the tgid value that the last readdir call couldn't
3211 : : * return. lseek aka telldir automagically resets f_version to 0.
3212 : : */
3213 : 1374 : ns = file->f_dentry->d_sb->s_fs_info;
3214 : 1374 : tid = (int)file->f_version;
3215 : 1374 : file->f_version = 0;
3216 [ + + ]: 2088 : for (task = first_tid(leader, tid, ctx->pos - 2, ns);
3217 : : task;
3218 : 714 : task = next_tid(task), ctx->pos++) {
3219 : : char name[PROC_NUMBUF];
3220 : : int len;
3221 : : tid = task_pid_nr_ns(task, ns);
3222 : 714 : len = snprintf(name, sizeof(name), "%d", tid);
3223 [ - + ]: 714 : if (!proc_fill_cache(file, ctx, name, len,
3224 : : proc_task_instantiate, task, NULL)) {
3225 : : /* returning this tgid failed, save it as the first
3226 : : * pid for the next readir call */
3227 : 0 : file->f_version = (u64)tid;
3228 : : put_task_struct(task);
3229 : 0 : break;
3230 : : }
3231 : : }
3232 : : out:
3233 : : put_task_struct(leader);
3234 : : return 0;
3235 : : }
3236 : :
3237 : 0 : static int proc_task_getattr(struct vfsmount *mnt, struct dentry *dentry, struct kstat *stat)
3238 : : {
3239 : 139 : struct inode *inode = dentry->d_inode;
3240 : : struct task_struct *p = get_proc_task(inode);
3241 : 139 : generic_fillattr(inode, stat);
3242 : :
3243 [ + - ]: 139 : if (p) {
3244 : 278 : stat->nlink += get_nr_threads(p);
3245 : : put_task_struct(p);
3246 : : }
3247 : :
3248 : 0 : return 0;
3249 : : }
3250 : :
3251 : : static const struct inode_operations proc_task_inode_operations = {
3252 : : .lookup = proc_task_lookup,
3253 : : .getattr = proc_task_getattr,
3254 : : .setattr = proc_setattr,
3255 : : .permission = proc_pid_permission,
3256 : : };
3257 : :
3258 : : static const struct file_operations proc_task_operations = {
3259 : : .read = generic_read_dir,
3260 : : .iterate = proc_task_readdir,
3261 : : .llseek = default_llseek,
3262 : : };
|