1 // SPDX-License-Identifier: GPL-2.0
2 /*
3 * linux/fs/proc/base.c
4 *
5 * Copyright (C) 1991, 1992 Linus Torvalds
6 *
7 * proc base directory handling functions
8 *
9 * 1999, Al Viro. Rewritten. Now it covers the whole per-process part.
10 * Instead of using magical inumbers to determine the kind of object
11 * we allocate and fill in-core inodes upon lookup. They don't even
12 * go into icache. We cache the reference to task_struct upon lookup too.
13 * Eventually it should become a filesystem in its own. We don't use the
14 * rest of procfs anymore.
15 *
16 *
17 * Changelog:
18 * 17-Jan-2005
19 * Allan Bezerra
20 * Bruna Moreira <bruna.moreira@indt.org.br>
21 * Edjard Mota <edjard.mota@indt.org.br>
22 * Ilias Biris <ilias.biris@indt.org.br>
23 * Mauricio Lin <mauricio.lin@indt.org.br>
24 *
25 * Embedded Linux Lab - 10LE Instituto Nokia de Tecnologia - INdT
26 *
27 * A new process specific entry (smaps) included in /proc. It shows the
28 * size of rss for each memory area. The maps entry lacks information
29 * about physical memory size (rss) for each mapped file, i.e.,
30 * rss information for executables and library files.
31 * This additional information is useful for any tools that need to know
32 * about physical memory consumption for a process specific library.
33 *
34 * Changelog:
35 * 21-Feb-2005
36 * Embedded Linux Lab - 10LE Instituto Nokia de Tecnologia - INdT
37 * Pud inclusion in the page table walking.
38 *
39 * ChangeLog:
40 * 10-Mar-2005
41 * 10LE Instituto Nokia de Tecnologia - INdT:
42 * A better way to walks through the page table as suggested by Hugh Dickins.
43 *
44 * Simo Piiroinen <simo.piiroinen@nokia.com>:
45 * Smaps information related to shared, private, clean and dirty pages.
46 *
47 * Paul Mundt <paul.mundt@nokia.com>:
48 * Overall revision about smaps.
49 */
50
51 #include <linux/uaccess.h>
52
53 #include <linux/errno.h>
54 #include <linux/time.h>
55 #include <linux/proc_fs.h>
56 #include <linux/stat.h>
57 #include <linux/task_io_accounting_ops.h>
58 #include <linux/init.h>
59 #include <linux/capability.h>
60 #include <linux/file.h>
61 #include <linux/fdtable.h>
62 #include <linux/generic-radix-tree.h>
63 #include <linux/string.h>
64 #include <linux/seq_file.h>
65 #include <linux/namei.h>
66 #include <linux/mnt_namespace.h>
67 #include <linux/mm.h>
68 #include <linux/swap.h>
69 #include <linux/rcupdate.h>
70 #include <linux/kallsyms.h>
71 #include <linux/stacktrace.h>
72 #include <linux/resource.h>
73 #include <linux/module.h>
74 #include <linux/mount.h>
75 #include <linux/security.h>
76 #include <linux/ptrace.h>
77 #include <linux/tracehook.h>
78 #include <linux/printk.h>
79 #include <linux/cache.h>
80 #include <linux/cgroup.h>
81 #include <linux/cpuset.h>
82 #include <linux/audit.h>
83 #include <linux/poll.h>
84 #include <linux/nsproxy.h>
85 #include <linux/oom.h>
86 #include <linux/elf.h>
87 #include <linux/pid_namespace.h>
88 #include <linux/user_namespace.h>
89 #include <linux/fs_struct.h>
90 #include <linux/slab.h>
91 #include <linux/sched.h>
92 #include <linux/sched/autogroup.h>
93 #include <linux/sched/mm.h>
94 #include <linux/sched/coredump.h>
95 #include <linux/sched/debug.h>
96 #include <linux/sched/stat.h>
97 #include <linux/posix-timers.h>
98 #include <linux/time_namespace.h>
99 #include <linux/resctrl.h>
100 #include <trace/events/oom.h>
101 #include "internal.h"
102 #include "fd.h"
103
104 #include "../../lib/kstrtox.h"
105
106 /* NOTE:
107 * Implementing inode permission operations in /proc is almost
108 * certainly an error. Permission checks need to happen during
109 * each system call not at open time. The reason is that most of
110 * what we wish to check for permissions in /proc varies at runtime.
111 *
112 * The classic example of a problem is opening file descriptors
113 * in /proc for a task before it execs a suid executable.
114 */
115
116 static u8 nlink_tid __ro_after_init;
117 static u8 nlink_tgid __ro_after_init;
118
119 struct pid_entry {
120 const char *name;
121 unsigned int len;
122 umode_t mode;
123 const struct inode_operations *iop;
124 const struct file_operations *fop;
125 union proc_op op;
126 };
127
128 #define NOD(NAME, MODE, IOP, FOP, OP) { \
129 .name = (NAME), \
130 .len = sizeof(NAME) - 1, \
131 .mode = MODE, \
132 .iop = IOP, \
133 .fop = FOP, \
134 .op = OP, \
135 }
136
137 #define DIR(NAME, MODE, iops, fops) \
138 NOD(NAME, (S_IFDIR|(MODE)), &iops, &fops, {} )
139 #define LNK(NAME, get_link) \
140 NOD(NAME, (S_IFLNK|S_IRWXUGO), \
141 &proc_pid_link_inode_operations, NULL, \
142 { .proc_get_link = get_link } )
143 #define REG(NAME, MODE, fops) \
144 NOD(NAME, (S_IFREG|(MODE)), NULL, &fops, {})
145 #define ONE(NAME, MODE, show) \
146 NOD(NAME, (S_IFREG|(MODE)), \
147 NULL, &proc_single_file_operations, \
148 { .proc_show = show } )
149 #define ATTR(LSM, NAME, MODE) \
150 NOD(NAME, (S_IFREG|(MODE)), \
151 NULL, &proc_pid_attr_operations, \
152 { .lsm = LSM })
153
154 /*
155 * Count the number of hardlinks for the pid_entry table, excluding the .
156 * and .. links.
157 */
pid_entry_nlink(const struct pid_entry * entries,unsigned int n)158 static unsigned int __init pid_entry_nlink(const struct pid_entry *entries,
159 unsigned int n)
160 {
161 unsigned int i;
162 unsigned int count;
163
164 count = 2;
165 for (i = 0; i < n; ++i) {
166 if (S_ISDIR(entries[i].mode))
167 ++count;
168 }
169
170 return count;
171 }
172
get_task_root(struct task_struct * task,struct path * root)173 static int get_task_root(struct task_struct *task, struct path *root)
174 {
175 int result = -ENOENT;
176
177 task_lock(task);
178 if (task->fs) {
179 get_fs_root(task->fs, root);
180 result = 0;
181 }
182 task_unlock(task);
183 return result;
184 }
185
proc_cwd_link(struct dentry * dentry,struct path * path)186 static int proc_cwd_link(struct dentry *dentry, struct path *path)
187 {
188 struct task_struct *task = get_proc_task(d_inode(dentry));
189 int result = -ENOENT;
190
191 if (task) {
192 task_lock(task);
193 if (task->fs) {
194 get_fs_pwd(task->fs, path);
195 result = 0;
196 }
197 task_unlock(task);
198 put_task_struct(task);
199 }
200 return result;
201 }
202
proc_root_link(struct dentry * dentry,struct path * path)203 static int proc_root_link(struct dentry *dentry, struct path *path)
204 {
205 struct task_struct *task = get_proc_task(d_inode(dentry));
206 int result = -ENOENT;
207
208 if (task) {
209 result = get_task_root(task, path);
210 put_task_struct(task);
211 }
212 return result;
213 }
214
215 /*
216 * If the user used setproctitle(), we just get the string from
217 * user space at arg_start, and limit it to a maximum of one page.
218 */
get_mm_proctitle(struct mm_struct * mm,char __user * buf,size_t count,unsigned long pos,unsigned long arg_start)219 static ssize_t get_mm_proctitle(struct mm_struct *mm, char __user *buf,
220 size_t count, unsigned long pos,
221 unsigned long arg_start)
222 {
223 char *page;
224 int ret, got;
225
226 if (pos >= PAGE_SIZE)
227 return 0;
228
229 page = (char *)__get_free_page(GFP_KERNEL);
230 if (!page)
231 return -ENOMEM;
232
233 ret = 0;
234 got = access_remote_vm(mm, arg_start, page, PAGE_SIZE, FOLL_ANON);
235 if (got > 0) {
236 int len = strnlen(page, got);
237
238 /* Include the NUL character if it was found */
239 if (len < got)
240 len++;
241
242 if (len > pos) {
243 len -= pos;
244 if (len > count)
245 len = count;
246 len -= copy_to_user(buf, page+pos, len);
247 if (!len)
248 len = -EFAULT;
249 ret = len;
250 }
251 }
252 free_page((unsigned long)page);
253 return ret;
254 }
255
get_mm_cmdline(struct mm_struct * mm,char __user * buf,size_t count,loff_t * ppos)256 static ssize_t get_mm_cmdline(struct mm_struct *mm, char __user *buf,
257 size_t count, loff_t *ppos)
258 {
259 unsigned long arg_start, arg_end, env_start, env_end;
260 unsigned long pos, len;
261 char *page, c;
262
263 /* Check if process spawned far enough to have cmdline. */
264 if (!mm->env_end)
265 return 0;
266
267 spin_lock(&mm->arg_lock);
268 arg_start = mm->arg_start;
269 arg_end = mm->arg_end;
270 env_start = mm->env_start;
271 env_end = mm->env_end;
272 spin_unlock(&mm->arg_lock);
273
274 if (arg_start >= arg_end)
275 return 0;
276
277 /*
278 * We allow setproctitle() to overwrite the argument
279 * strings, and overflow past the original end. But
280 * only when it overflows into the environment area.
281 */
282 if (env_start != arg_end || env_end < env_start)
283 env_start = env_end = arg_end;
284 len = env_end - arg_start;
285
286 /* We're not going to care if "*ppos" has high bits set */
287 pos = *ppos;
288 if (pos >= len)
289 return 0;
290 if (count > len - pos)
291 count = len - pos;
292 if (!count)
293 return 0;
294
295 /*
296 * Magical special case: if the argv[] end byte is not
297 * zero, the user has overwritten it with setproctitle(3).
298 *
299 * Possible future enhancement: do this only once when
300 * pos is 0, and set a flag in the 'struct file'.
301 */
302 if (access_remote_vm(mm, arg_end-1, &c, 1, FOLL_ANON) == 1 && c)
303 return get_mm_proctitle(mm, buf, count, pos, arg_start);
304
305 /*
306 * For the non-setproctitle() case we limit things strictly
307 * to the [arg_start, arg_end[ range.
308 */
309 pos += arg_start;
310 if (pos < arg_start || pos >= arg_end)
311 return 0;
312 if (count > arg_end - pos)
313 count = arg_end - pos;
314
315 page = (char *)__get_free_page(GFP_KERNEL);
316 if (!page)
317 return -ENOMEM;
318
319 len = 0;
320 while (count) {
321 int got;
322 size_t size = min_t(size_t, PAGE_SIZE, count);
323
324 got = access_remote_vm(mm, pos, page, size, FOLL_ANON);
325 if (got <= 0)
326 break;
327 got -= copy_to_user(buf, page, got);
328 if (unlikely(!got)) {
329 if (!len)
330 len = -EFAULT;
331 break;
332 }
333 pos += got;
334 buf += got;
335 len += got;
336 count -= got;
337 }
338
339 free_page((unsigned long)page);
340 return len;
341 }
342
get_task_cmdline(struct task_struct * tsk,char __user * buf,size_t count,loff_t * pos)343 static ssize_t get_task_cmdline(struct task_struct *tsk, char __user *buf,
344 size_t count, loff_t *pos)
345 {
346 struct mm_struct *mm;
347 ssize_t ret;
348
349 mm = get_task_mm(tsk);
350 if (!mm)
351 return 0;
352
353 ret = get_mm_cmdline(mm, buf, count, pos);
354 mmput(mm);
355 return ret;
356 }
357
proc_pid_cmdline_read(struct file * file,char __user * buf,size_t count,loff_t * pos)358 static ssize_t proc_pid_cmdline_read(struct file *file, char __user *buf,
359 size_t count, loff_t *pos)
360 {
361 struct task_struct *tsk;
362 ssize_t ret;
363
364 BUG_ON(*pos < 0);
365
366 tsk = get_proc_task(file_inode(file));
367 if (!tsk)
368 return -ESRCH;
369 ret = get_task_cmdline(tsk, buf, count, pos);
370 put_task_struct(tsk);
371 if (ret > 0)
372 *pos += ret;
373 return ret;
374 }
375
376 static const struct file_operations proc_pid_cmdline_ops = {
377 .read = proc_pid_cmdline_read,
378 .llseek = generic_file_llseek,
379 };
380
381 #ifdef CONFIG_KALLSYMS
382 /*
383 * Provides a wchan file via kallsyms in a proper one-value-per-file format.
384 * Returns the resolved symbol. If that fails, simply return the address.
385 */
proc_pid_wchan(struct seq_file * m,struct pid_namespace * ns,struct pid * pid,struct task_struct * task)386 static int proc_pid_wchan(struct seq_file *m, struct pid_namespace *ns,
387 struct pid *pid, struct task_struct *task)
388 {
389 unsigned long wchan;
390 char symname[KSYM_NAME_LEN];
391
392 if (!ptrace_may_access(task, PTRACE_MODE_READ_FSCREDS))
393 goto print0;
394
395 wchan = get_wchan(task);
396 if (wchan && !lookup_symbol_name(wchan, symname)) {
397 seq_puts(m, symname);
398 return 0;
399 }
400
401 print0:
402 seq_putc(m, '0');
403 return 0;
404 }
405 #endif /* CONFIG_KALLSYMS */
406
lock_trace(struct task_struct * task)407 static int lock_trace(struct task_struct *task)
408 {
409 int err = down_read_killable(&task->signal->exec_update_lock);
410 if (err)
411 return err;
412 if (!ptrace_may_access(task, PTRACE_MODE_ATTACH_FSCREDS)) {
413 up_read(&task->signal->exec_update_lock);
414 return -EPERM;
415 }
416 return 0;
417 }
418
unlock_trace(struct task_struct * task)419 static void unlock_trace(struct task_struct *task)
420 {
421 up_read(&task->signal->exec_update_lock);
422 }
423
424 #ifdef CONFIG_STACKTRACE
425
426 #define MAX_STACK_TRACE_DEPTH 64
427
proc_pid_stack(struct seq_file * m,struct pid_namespace * ns,struct pid * pid,struct task_struct * task)428 static int proc_pid_stack(struct seq_file *m, struct pid_namespace *ns,
429 struct pid *pid, struct task_struct *task)
430 {
431 unsigned long *entries;
432 int err;
433
434 /*
435 * The ability to racily run the kernel stack unwinder on a running task
436 * and then observe the unwinder output is scary; while it is useful for
437 * debugging kernel issues, it can also allow an attacker to leak kernel
438 * stack contents.
439 * Doing this in a manner that is at least safe from races would require
440 * some work to ensure that the remote task can not be scheduled; and
441 * even then, this would still expose the unwinder as local attack
442 * surface.
443 * Therefore, this interface is restricted to root.
444 */
445 if (!file_ns_capable(m->file, &init_user_ns, CAP_SYS_ADMIN))
446 return -EACCES;
447
448 entries = kmalloc_array(MAX_STACK_TRACE_DEPTH, sizeof(*entries),
449 GFP_KERNEL);
450 if (!entries)
451 return -ENOMEM;
452
453 err = lock_trace(task);
454 if (!err) {
455 unsigned int i, nr_entries;
456
457 nr_entries = stack_trace_save_tsk(task, entries,
458 MAX_STACK_TRACE_DEPTH, 0);
459
460 for (i = 0; i < nr_entries; i++) {
461 seq_printf(m, "[<0>] %pB\n", (void *)entries[i]);
462 }
463
464 unlock_trace(task);
465 }
466 kfree(entries);
467
468 return err;
469 }
470 #endif
471
472 #ifdef CONFIG_SCHED_INFO
473 /*
474 * Provides /proc/PID/schedstat
475 */
proc_pid_schedstat(struct seq_file * m,struct pid_namespace * ns,struct pid * pid,struct task_struct * task)476 static int proc_pid_schedstat(struct seq_file *m, struct pid_namespace *ns,
477 struct pid *pid, struct task_struct *task)
478 {
479 if (unlikely(!sched_info_on()))
480 seq_puts(m, "0 0 0\n");
481 else
482 seq_printf(m, "%llu %llu %lu\n",
483 (unsigned long long)task->se.sum_exec_runtime,
484 (unsigned long long)task->sched_info.run_delay,
485 task->sched_info.pcount);
486
487 return 0;
488 }
489 #endif
490
491 #ifdef CONFIG_LATENCYTOP
lstats_show_proc(struct seq_file * m,void * v)492 static int lstats_show_proc(struct seq_file *m, void *v)
493 {
494 int i;
495 struct inode *inode = m->private;
496 struct task_struct *task = get_proc_task(inode);
497
498 if (!task)
499 return -ESRCH;
500 seq_puts(m, "Latency Top version : v0.1\n");
501 for (i = 0; i < LT_SAVECOUNT; i++) {
502 struct latency_record *lr = &task->latency_record[i];
503 if (lr->backtrace[0]) {
504 int q;
505 seq_printf(m, "%i %li %li",
506 lr->count, lr->time, lr->max);
507 for (q = 0; q < LT_BACKTRACEDEPTH; q++) {
508 unsigned long bt = lr->backtrace[q];
509
510 if (!bt)
511 break;
512 seq_printf(m, " %ps", (void *)bt);
513 }
514 seq_putc(m, '\n');
515 }
516
517 }
518 put_task_struct(task);
519 return 0;
520 }
521
lstats_open(struct inode * inode,struct file * file)522 static int lstats_open(struct inode *inode, struct file *file)
523 {
524 return single_open(file, lstats_show_proc, inode);
525 }
526
lstats_write(struct file * file,const char __user * buf,size_t count,loff_t * offs)527 static ssize_t lstats_write(struct file *file, const char __user *buf,
528 size_t count, loff_t *offs)
529 {
530 struct task_struct *task = get_proc_task(file_inode(file));
531
532 if (!task)
533 return -ESRCH;
534 clear_tsk_latency_tracing(task);
535 put_task_struct(task);
536
537 return count;
538 }
539
540 static const struct file_operations proc_lstats_operations = {
541 .open = lstats_open,
542 .read = seq_read,
543 .write = lstats_write,
544 .llseek = seq_lseek,
545 .release = single_release,
546 };
547
548 #endif
549
proc_oom_score(struct seq_file * m,struct pid_namespace * ns,struct pid * pid,struct task_struct * task)550 static int proc_oom_score(struct seq_file *m, struct pid_namespace *ns,
551 struct pid *pid, struct task_struct *task)
552 {
553 unsigned long totalpages = totalram_pages() + total_swap_pages;
554 unsigned long points = 0;
555 long badness;
556
557 badness = oom_badness(task, totalpages);
558 /*
559 * Special case OOM_SCORE_ADJ_MIN for all others scale the
560 * badness value into [0, 2000] range which we have been
561 * exporting for a long time so userspace might depend on it.
562 */
563 if (badness != LONG_MIN)
564 points = (1000 + badness * 1000 / (long)totalpages) * 2 / 3;
565
566 seq_printf(m, "%lu\n", points);
567
568 return 0;
569 }
570
571 struct limit_names {
572 const char *name;
573 const char *unit;
574 };
575
576 static const struct limit_names lnames[RLIM_NLIMITS] = {
577 [RLIMIT_CPU] = {"Max cpu time", "seconds"},
578 [RLIMIT_FSIZE] = {"Max file size", "bytes"},
579 [RLIMIT_DATA] = {"Max data size", "bytes"},
580 [RLIMIT_STACK] = {"Max stack size", "bytes"},
581 [RLIMIT_CORE] = {"Max core file size", "bytes"},
582 [RLIMIT_RSS] = {"Max resident set", "bytes"},
583 [RLIMIT_NPROC] = {"Max processes", "processes"},
584 [RLIMIT_NOFILE] = {"Max open files", "files"},
585 [RLIMIT_MEMLOCK] = {"Max locked memory", "bytes"},
586 [RLIMIT_AS] = {"Max address space", "bytes"},
587 [RLIMIT_LOCKS] = {"Max file locks", "locks"},
588 [RLIMIT_SIGPENDING] = {"Max pending signals", "signals"},
589 [RLIMIT_MSGQUEUE] = {"Max msgqueue size", "bytes"},
590 [RLIMIT_NICE] = {"Max nice priority", NULL},
591 [RLIMIT_RTPRIO] = {"Max realtime priority", NULL},
592 [RLIMIT_RTTIME] = {"Max realtime timeout", "us"},
593 };
594
595 /* Display limits for a process */
proc_pid_limits(struct seq_file * m,struct pid_namespace * ns,struct pid * pid,struct task_struct * task)596 static int proc_pid_limits(struct seq_file *m, struct pid_namespace *ns,
597 struct pid *pid, struct task_struct *task)
598 {
599 unsigned int i;
600 unsigned long flags;
601
602 struct rlimit rlim[RLIM_NLIMITS];
603
604 if (!lock_task_sighand(task, &flags))
605 return 0;
606 memcpy(rlim, task->signal->rlim, sizeof(struct rlimit) * RLIM_NLIMITS);
607 unlock_task_sighand(task, &flags);
608
609 /*
610 * print the file header
611 */
612 seq_puts(m, "Limit "
613 "Soft Limit "
614 "Hard Limit "
615 "Units \n");
616
617 for (i = 0; i < RLIM_NLIMITS; i++) {
618 if (rlim[i].rlim_cur == RLIM_INFINITY)
619 seq_printf(m, "%-25s %-20s ",
620 lnames[i].name, "unlimited");
621 else
622 seq_printf(m, "%-25s %-20lu ",
623 lnames[i].name, rlim[i].rlim_cur);
624
625 if (rlim[i].rlim_max == RLIM_INFINITY)
626 seq_printf(m, "%-20s ", "unlimited");
627 else
628 seq_printf(m, "%-20lu ", rlim[i].rlim_max);
629
630 if (lnames[i].unit)
631 seq_printf(m, "%-10s\n", lnames[i].unit);
632 else
633 seq_putc(m, '\n');
634 }
635
636 return 0;
637 }
638
639 #ifdef CONFIG_HAVE_ARCH_TRACEHOOK
proc_pid_syscall(struct seq_file * m,struct pid_namespace * ns,struct pid * pid,struct task_struct * task)640 static int proc_pid_syscall(struct seq_file *m, struct pid_namespace *ns,
641 struct pid *pid, struct task_struct *task)
642 {
643 struct syscall_info info;
644 u64 *args = &info.data.args[0];
645 int res;
646
647 res = lock_trace(task);
648 if (res)
649 return res;
650
651 if (task_current_syscall(task, &info))
652 seq_puts(m, "running\n");
653 else if (info.data.nr < 0)
654 seq_printf(m, "%d 0x%llx 0x%llx\n",
655 info.data.nr, info.sp, info.data.instruction_pointer);
656 else
657 seq_printf(m,
658 "%d 0x%llx 0x%llx 0x%llx 0x%llx 0x%llx 0x%llx 0x%llx 0x%llx\n",
659 info.data.nr,
660 args[0], args[1], args[2], args[3], args[4], args[5],
661 info.sp, info.data.instruction_pointer);
662 unlock_trace(task);
663
664 return 0;
665 }
666 #endif /* CONFIG_HAVE_ARCH_TRACEHOOK */
667
668 /************************************************************************/
669 /* Here the fs part begins */
670 /************************************************************************/
671
672 /* permission checks */
proc_fd_access_allowed(struct inode * inode)673 static int proc_fd_access_allowed(struct inode *inode)
674 {
675 struct task_struct *task;
676 int allowed = 0;
677 /* Allow access to a task's file descriptors if it is us or we
678 * may use ptrace attach to the process and find out that
679 * information.
680 */
681 task = get_proc_task(inode);
682 if (task) {
683 allowed = ptrace_may_access(task, PTRACE_MODE_READ_FSCREDS);
684 put_task_struct(task);
685 }
686 return allowed;
687 }
688
proc_setattr(struct dentry * dentry,struct iattr * attr)689 int proc_setattr(struct dentry *dentry, struct iattr *attr)
690 {
691 int error;
692 struct inode *inode = d_inode(dentry);
693
694 if (attr->ia_valid & ATTR_MODE)
695 return -EPERM;
696
697 error = setattr_prepare(dentry, attr);
698 if (error)
699 return error;
700
701 setattr_copy(inode, attr);
702 mark_inode_dirty(inode);
703 return 0;
704 }
705
706 /*
707 * May current process learn task's sched/cmdline info (for hide_pid_min=1)
708 * or euid/egid (for hide_pid_min=2)?
709 */
has_pid_permissions(struct proc_fs_info * fs_info,struct task_struct * task,enum proc_hidepid hide_pid_min)710 static bool has_pid_permissions(struct proc_fs_info *fs_info,
711 struct task_struct *task,
712 enum proc_hidepid hide_pid_min)
713 {
714 /*
715 * If 'hidpid' mount option is set force a ptrace check,
716 * we indicate that we are using a filesystem syscall
717 * by passing PTRACE_MODE_READ_FSCREDS
718 */
719 if (fs_info->hide_pid == HIDEPID_NOT_PTRACEABLE)
720 return ptrace_may_access(task, PTRACE_MODE_READ_FSCREDS);
721
722 if (fs_info->hide_pid < hide_pid_min)
723 return true;
724 if (in_group_p(fs_info->pid_gid))
725 return true;
726 return ptrace_may_access(task, PTRACE_MODE_READ_FSCREDS);
727 }
728
729
proc_pid_permission(struct inode * inode,int mask)730 static int proc_pid_permission(struct inode *inode, int mask)
731 {
732 struct proc_fs_info *fs_info = proc_sb_info(inode->i_sb);
733 struct task_struct *task;
734 bool has_perms;
735
736 task = get_proc_task(inode);
737 if (!task)
738 return -ESRCH;
739 has_perms = has_pid_permissions(fs_info, task, HIDEPID_NO_ACCESS);
740 put_task_struct(task);
741
742 if (!has_perms) {
743 if (fs_info->hide_pid == HIDEPID_INVISIBLE) {
744 /*
745 * Let's make getdents(), stat(), and open()
746 * consistent with each other. If a process
747 * may not stat() a file, it shouldn't be seen
748 * in procfs at all.
749 */
750 return -ENOENT;
751 }
752
753 return -EPERM;
754 }
755 return generic_permission(inode, mask);
756 }
757
758
759
760 static const struct inode_operations proc_def_inode_operations = {
761 .setattr = proc_setattr,
762 };
763
proc_single_show(struct seq_file * m,void * v)764 static int proc_single_show(struct seq_file *m, void *v)
765 {
766 struct inode *inode = m->private;
767 struct pid_namespace *ns = proc_pid_ns(inode->i_sb);
768 struct pid *pid = proc_pid(inode);
769 struct task_struct *task;
770 int ret;
771
772 task = get_pid_task(pid, PIDTYPE_PID);
773 if (!task)
774 return -ESRCH;
775
776 ret = PROC_I(inode)->op.proc_show(m, ns, pid, task);
777
778 put_task_struct(task);
779 return ret;
780 }
781
proc_single_open(struct inode * inode,struct file * filp)782 static int proc_single_open(struct inode *inode, struct file *filp)
783 {
784 return single_open(filp, proc_single_show, inode);
785 }
786
787 static const struct file_operations proc_single_file_operations = {
788 .open = proc_single_open,
789 .read = seq_read,
790 .llseek = seq_lseek,
791 .release = single_release,
792 };
793
794
proc_mem_open(struct inode * inode,unsigned int mode)795 struct mm_struct *proc_mem_open(struct inode *inode, unsigned int mode)
796 {
797 struct task_struct *task = get_proc_task(inode);
798 struct mm_struct *mm = ERR_PTR(-ESRCH);
799
800 if (task) {
801 mm = mm_access(task, mode | PTRACE_MODE_FSCREDS);
802 put_task_struct(task);
803
804 if (!IS_ERR_OR_NULL(mm)) {
805 /* ensure this mm_struct can't be freed */
806 mmgrab(mm);
807 /* but do not pin its memory */
808 mmput(mm);
809 }
810 }
811
812 return mm;
813 }
814
__mem_open(struct inode * inode,struct file * file,unsigned int mode)815 static int __mem_open(struct inode *inode, struct file *file, unsigned int mode)
816 {
817 struct mm_struct *mm = proc_mem_open(inode, mode);
818
819 if (IS_ERR(mm))
820 return PTR_ERR(mm);
821
822 file->private_data = mm;
823 return 0;
824 }
825
mem_open(struct inode * inode,struct file * file)826 static int mem_open(struct inode *inode, struct file *file)
827 {
828 int ret = __mem_open(inode, file, PTRACE_MODE_ATTACH);
829
830 /* OK to pass negative loff_t, we can catch out-of-range */
831 file->f_mode |= FMODE_UNSIGNED_OFFSET;
832
833 return ret;
834 }
835
mem_rw(struct file * file,char __user * buf,size_t count,loff_t * ppos,int write)836 static ssize_t mem_rw(struct file *file, char __user *buf,
837 size_t count, loff_t *ppos, int write)
838 {
839 struct mm_struct *mm = file->private_data;
840 unsigned long addr = *ppos;
841 ssize_t copied;
842 char *page;
843 unsigned int flags;
844
845 if (!mm)
846 return 0;
847
848 page = (char *)__get_free_page(GFP_KERNEL);
849 if (!page)
850 return -ENOMEM;
851
852 copied = 0;
853 if (!mmget_not_zero(mm))
854 goto free;
855
856 flags = FOLL_FORCE | (write ? FOLL_WRITE : 0);
857
858 while (count > 0) {
859 size_t this_len = min_t(size_t, count, PAGE_SIZE);
860
861 if (write && copy_from_user(page, buf, this_len)) {
862 copied = -EFAULT;
863 break;
864 }
865
866 this_len = access_remote_vm(mm, addr, page, this_len, flags);
867 if (!this_len) {
868 if (!copied)
869 copied = -EIO;
870 break;
871 }
872
873 if (!write && copy_to_user(buf, page, this_len)) {
874 copied = -EFAULT;
875 break;
876 }
877
878 buf += this_len;
879 addr += this_len;
880 copied += this_len;
881 count -= this_len;
882 }
883 *ppos = addr;
884
885 mmput(mm);
886 free:
887 free_page((unsigned long) page);
888 return copied;
889 }
890
mem_read(struct file * file,char __user * buf,size_t count,loff_t * ppos)891 static ssize_t mem_read(struct file *file, char __user *buf,
892 size_t count, loff_t *ppos)
893 {
894 return mem_rw(file, buf, count, ppos, 0);
895 }
896
mem_write(struct file * file,const char __user * buf,size_t count,loff_t * ppos)897 static ssize_t mem_write(struct file *file, const char __user *buf,
898 size_t count, loff_t *ppos)
899 {
900 return mem_rw(file, (char __user*)buf, count, ppos, 1);
901 }
902
mem_lseek(struct file * file,loff_t offset,int orig)903 loff_t mem_lseek(struct file *file, loff_t offset, int orig)
904 {
905 loff_t ret = 0;
906
907 spin_lock(&file->f_lock);
908 switch (orig) {
909 case SEEK_CUR:
910 offset += file->f_pos;
911 /* fall through */
912 case SEEK_SET:
913 /* to avoid userland mistaking f_pos=-9 as -EBADF=-9 */
914 if ((unsigned long long)offset >= -MAX_ERRNO)
915 ret = -EOVERFLOW;
916 break;
917 default:
918 ret = -EINVAL;
919 }
920
921 if (!ret) {
922 if (offset < 0 && !(unsigned_offsets(file))) {
923 ret = -EINVAL;
924 } else {
925 file->f_pos = offset;
926 ret = file->f_pos;
927 force_successful_syscall_return();
928 }
929 }
930
931 spin_unlock(&file->f_lock);
932 return ret;
933 }
934
mem_release(struct inode * inode,struct file * file)935 static int mem_release(struct inode *inode, struct file *file)
936 {
937 struct mm_struct *mm = file->private_data;
938 if (mm)
939 mmdrop(mm);
940 return 0;
941 }
942
943 static const struct file_operations proc_mem_operations = {
944 .llseek = mem_lseek,
945 .read = mem_read,
946 .write = mem_write,
947 .open = mem_open,
948 .release = mem_release,
949 };
950
environ_open(struct inode * inode,struct file * file)951 static int environ_open(struct inode *inode, struct file *file)
952 {
953 return __mem_open(inode, file, PTRACE_MODE_READ);
954 }
955
environ_read(struct file * file,char __user * buf,size_t count,loff_t * ppos)956 static ssize_t environ_read(struct file *file, char __user *buf,
957 size_t count, loff_t *ppos)
958 {
959 char *page;
960 unsigned long src = *ppos;
961 int ret = 0;
962 struct mm_struct *mm = file->private_data;
963 unsigned long env_start, env_end;
964
965 /* Ensure the process spawned far enough to have an environment. */
966 if (!mm || !mm->env_end)
967 return 0;
968
969 page = (char *)__get_free_page(GFP_KERNEL);
970 if (!page)
971 return -ENOMEM;
972
973 ret = 0;
974 if (!mmget_not_zero(mm))
975 goto free;
976
977 spin_lock(&mm->arg_lock);
978 env_start = mm->env_start;
979 env_end = mm->env_end;
980 spin_unlock(&mm->arg_lock);
981
982 while (count > 0) {
983 size_t this_len, max_len;
984 int retval;
985
986 if (src >= (env_end - env_start))
987 break;
988
989 this_len = env_end - (env_start + src);
990
991 max_len = min_t(size_t, PAGE_SIZE, count);
992 this_len = min(max_len, this_len);
993
994 retval = access_remote_vm(mm, (env_start + src), page, this_len, FOLL_ANON);
995
996 if (retval <= 0) {
997 ret = retval;
998 break;
999 }
1000
1001 if (copy_to_user(buf, page, retval)) {
1002 ret = -EFAULT;
1003 break;
1004 }
1005
1006 ret += retval;
1007 src += retval;
1008 buf += retval;
1009 count -= retval;
1010 }
1011 *ppos = src;
1012 mmput(mm);
1013
1014 free:
1015 free_page((unsigned long) page);
1016 return ret;
1017 }
1018
1019 static const struct file_operations proc_environ_operations = {
1020 .open = environ_open,
1021 .read = environ_read,
1022 .llseek = generic_file_llseek,
1023 .release = mem_release,
1024 };
1025
auxv_open(struct inode * inode,struct file * file)1026 static int auxv_open(struct inode *inode, struct file *file)
1027 {
1028 return __mem_open(inode, file, PTRACE_MODE_READ_FSCREDS);
1029 }
1030
auxv_read(struct file * file,char __user * buf,size_t count,loff_t * ppos)1031 static ssize_t auxv_read(struct file *file, char __user *buf,
1032 size_t count, loff_t *ppos)
1033 {
1034 struct mm_struct *mm = file->private_data;
1035 unsigned int nwords = 0;
1036
1037 if (!mm)
1038 return 0;
1039 do {
1040 nwords += 2;
1041 } while (mm->saved_auxv[nwords - 2] != 0); /* AT_NULL */
1042 return simple_read_from_buffer(buf, count, ppos, mm->saved_auxv,
1043 nwords * sizeof(mm->saved_auxv[0]));
1044 }
1045
1046 static const struct file_operations proc_auxv_operations = {
1047 .open = auxv_open,
1048 .read = auxv_read,
1049 .llseek = generic_file_llseek,
1050 .release = mem_release,
1051 };
1052
oom_adj_read(struct file * file,char __user * buf,size_t count,loff_t * ppos)1053 static ssize_t oom_adj_read(struct file *file, char __user *buf, size_t count,
1054 loff_t *ppos)
1055 {
1056 struct task_struct *task = get_proc_task(file_inode(file));
1057 char buffer[PROC_NUMBUF];
1058 int oom_adj = OOM_ADJUST_MIN;
1059 size_t len;
1060
1061 if (!task)
1062 return -ESRCH;
1063 if (task->signal->oom_score_adj == OOM_SCORE_ADJ_MAX)
1064 oom_adj = OOM_ADJUST_MAX;
1065 else
1066 oom_adj = (task->signal->oom_score_adj * -OOM_DISABLE) /
1067 OOM_SCORE_ADJ_MAX;
1068 put_task_struct(task);
1069 if (oom_adj > OOM_ADJUST_MAX)
1070 oom_adj = OOM_ADJUST_MAX;
1071 len = snprintf(buffer, sizeof(buffer), "%d\n", oom_adj);
1072 return simple_read_from_buffer(buf, count, ppos, buffer, len);
1073 }
1074
__set_oom_adj(struct file * file,int oom_adj,bool legacy)1075 static int __set_oom_adj(struct file *file, int oom_adj, bool legacy)
1076 {
1077 struct mm_struct *mm = NULL;
1078 struct task_struct *task;
1079 int err = 0;
1080
1081 task = get_proc_task(file_inode(file));
1082 if (!task)
1083 return -ESRCH;
1084
1085 mutex_lock(&oom_adj_mutex);
1086 if (legacy) {
1087 if (oom_adj < task->signal->oom_score_adj &&
1088 !capable(CAP_SYS_RESOURCE)) {
1089 err = -EACCES;
1090 goto err_unlock;
1091 }
1092 /*
1093 * /proc/pid/oom_adj is provided for legacy purposes, ask users to use
1094 * /proc/pid/oom_score_adj instead.
1095 */
1096 pr_warn_once("%s (%d): /proc/%d/oom_adj is deprecated, please use /proc/%d/oom_score_adj instead.\n",
1097 current->comm, task_pid_nr(current), task_pid_nr(task),
1098 task_pid_nr(task));
1099 } else {
1100 if ((short)oom_adj < task->signal->oom_score_adj_min &&
1101 !capable(CAP_SYS_RESOURCE)) {
1102 err = -EACCES;
1103 goto err_unlock;
1104 }
1105 }
1106
1107 /*
1108 * Make sure we will check other processes sharing the mm if this is
1109 * not vfrok which wants its own oom_score_adj.
1110 * pin the mm so it doesn't go away and get reused after task_unlock
1111 */
1112 if (!task->vfork_done) {
1113 struct task_struct *p = find_lock_task_mm(task);
1114
1115 if (p) {
1116 if (test_bit(MMF_MULTIPROCESS, &p->mm->flags)) {
1117 mm = p->mm;
1118 mmgrab(mm);
1119 }
1120 task_unlock(p);
1121 }
1122 }
1123
1124 task->signal->oom_score_adj = oom_adj;
1125 if (!legacy && has_capability_noaudit(current, CAP_SYS_RESOURCE))
1126 task->signal->oom_score_adj_min = (short)oom_adj;
1127 trace_oom_score_adj_update(task);
1128
1129 if (mm) {
1130 struct task_struct *p;
1131
1132 rcu_read_lock();
1133 for_each_process(p) {
1134 if (same_thread_group(task, p))
1135 continue;
1136
1137 /* do not touch kernel threads or the global init */
1138 if (p->flags & PF_KTHREAD || is_global_init(p))
1139 continue;
1140
1141 task_lock(p);
1142 if (!p->vfork_done && process_shares_mm(p, mm)) {
1143 p->signal->oom_score_adj = oom_adj;
1144 if (!legacy && has_capability_noaudit(current, CAP_SYS_RESOURCE))
1145 p->signal->oom_score_adj_min = (short)oom_adj;
1146 }
1147 task_unlock(p);
1148 }
1149 rcu_read_unlock();
1150 mmdrop(mm);
1151 }
1152 err_unlock:
1153 mutex_unlock(&oom_adj_mutex);
1154 put_task_struct(task);
1155 return err;
1156 }
1157
1158 /*
1159 * /proc/pid/oom_adj exists solely for backwards compatibility with previous
1160 * kernels. The effective policy is defined by oom_score_adj, which has a
1161 * different scale: oom_adj grew exponentially and oom_score_adj grows linearly.
1162 * Values written to oom_adj are simply mapped linearly to oom_score_adj.
1163 * Processes that become oom disabled via oom_adj will still be oom disabled
1164 * with this implementation.
1165 *
1166 * oom_adj cannot be removed since existing userspace binaries use it.
1167 */
oom_adj_write(struct file * file,const char __user * buf,size_t count,loff_t * ppos)1168 static ssize_t oom_adj_write(struct file *file, const char __user *buf,
1169 size_t count, loff_t *ppos)
1170 {
1171 char buffer[PROC_NUMBUF];
1172 int oom_adj;
1173 int err;
1174
1175 memset(buffer, 0, sizeof(buffer));
1176 if (count > sizeof(buffer) - 1)
1177 count = sizeof(buffer) - 1;
1178 if (copy_from_user(buffer, buf, count)) {
1179 err = -EFAULT;
1180 goto out;
1181 }
1182
1183 err = kstrtoint(strstrip(buffer), 0, &oom_adj);
1184 if (err)
1185 goto out;
1186 if ((oom_adj < OOM_ADJUST_MIN || oom_adj > OOM_ADJUST_MAX) &&
1187 oom_adj != OOM_DISABLE) {
1188 err = -EINVAL;
1189 goto out;
1190 }
1191
1192 /*
1193 * Scale /proc/pid/oom_score_adj appropriately ensuring that a maximum
1194 * value is always attainable.
1195 */
1196 if (oom_adj == OOM_ADJUST_MAX)
1197 oom_adj = OOM_SCORE_ADJ_MAX;
1198 else
1199 oom_adj = (oom_adj * OOM_SCORE_ADJ_MAX) / -OOM_DISABLE;
1200
1201 err = __set_oom_adj(file, oom_adj, true);
1202 out:
1203 return err < 0 ? err : count;
1204 }
1205
1206 static const struct file_operations proc_oom_adj_operations = {
1207 .read = oom_adj_read,
1208 .write = oom_adj_write,
1209 .llseek = generic_file_llseek,
1210 };
1211
oom_score_adj_read(struct file * file,char __user * buf,size_t count,loff_t * ppos)1212 static ssize_t oom_score_adj_read(struct file *file, char __user *buf,
1213 size_t count, loff_t *ppos)
1214 {
1215 struct task_struct *task = get_proc_task(file_inode(file));
1216 char buffer[PROC_NUMBUF];
1217 short oom_score_adj = OOM_SCORE_ADJ_MIN;
1218 size_t len;
1219
1220 if (!task)
1221 return -ESRCH;
1222 oom_score_adj = task->signal->oom_score_adj;
1223 put_task_struct(task);
1224 len = snprintf(buffer, sizeof(buffer), "%hd\n", oom_score_adj);
1225 return simple_read_from_buffer(buf, count, ppos, buffer, len);
1226 }
1227
oom_score_adj_write(struct file * file,const char __user * buf,size_t count,loff_t * ppos)1228 static ssize_t oom_score_adj_write(struct file *file, const char __user *buf,
1229 size_t count, loff_t *ppos)
1230 {
1231 char buffer[PROC_NUMBUF];
1232 int oom_score_adj;
1233 int err;
1234
1235 memset(buffer, 0, sizeof(buffer));
1236 if (count > sizeof(buffer) - 1)
1237 count = sizeof(buffer) - 1;
1238 if (copy_from_user(buffer, buf, count)) {
1239 err = -EFAULT;
1240 goto out;
1241 }
1242
1243 err = kstrtoint(strstrip(buffer), 0, &oom_score_adj);
1244 if (err)
1245 goto out;
1246 if (oom_score_adj < OOM_SCORE_ADJ_MIN ||
1247 oom_score_adj > OOM_SCORE_ADJ_MAX) {
1248 err = -EINVAL;
1249 goto out;
1250 }
1251
1252 err = __set_oom_adj(file, oom_score_adj, false);
1253 out:
1254 return err < 0 ? err : count;
1255 }
1256
1257 static const struct file_operations proc_oom_score_adj_operations = {
1258 .read = oom_score_adj_read,
1259 .write = oom_score_adj_write,
1260 .llseek = default_llseek,
1261 };
1262
1263 #ifdef CONFIG_AUDIT
1264 #define TMPBUFLEN 11
proc_loginuid_read(struct file * file,char __user * buf,size_t count,loff_t * ppos)1265 static ssize_t proc_loginuid_read(struct file * file, char __user * buf,
1266 size_t count, loff_t *ppos)
1267 {
1268 struct inode * inode = file_inode(file);
1269 struct task_struct *task = get_proc_task(inode);
1270 ssize_t length;
1271 char tmpbuf[TMPBUFLEN];
1272
1273 if (!task)
1274 return -ESRCH;
1275 length = scnprintf(tmpbuf, TMPBUFLEN, "%u",
1276 from_kuid(file->f_cred->user_ns,
1277 audit_get_loginuid(task)));
1278 put_task_struct(task);
1279 return simple_read_from_buffer(buf, count, ppos, tmpbuf, length);
1280 }
1281
proc_loginuid_write(struct file * file,const char __user * buf,size_t count,loff_t * ppos)1282 static ssize_t proc_loginuid_write(struct file * file, const char __user * buf,
1283 size_t count, loff_t *ppos)
1284 {
1285 struct inode * inode = file_inode(file);
1286 uid_t loginuid;
1287 kuid_t kloginuid;
1288 int rv;
1289
1290 /* Don't let kthreads write their own loginuid */
1291 if (current->flags & PF_KTHREAD)
1292 return -EPERM;
1293
1294 rcu_read_lock();
1295 if (current != pid_task(proc_pid(inode), PIDTYPE_PID)) {
1296 rcu_read_unlock();
1297 return -EPERM;
1298 }
1299 rcu_read_unlock();
1300
1301 if (*ppos != 0) {
1302 /* No partial writes. */
1303 return -EINVAL;
1304 }
1305
1306 rv = kstrtou32_from_user(buf, count, 10, &loginuid);
1307 if (rv < 0)
1308 return rv;
1309
1310 /* is userspace tring to explicitly UNSET the loginuid? */
1311 if (loginuid == AUDIT_UID_UNSET) {
1312 kloginuid = INVALID_UID;
1313 } else {
1314 kloginuid = make_kuid(file->f_cred->user_ns, loginuid);
1315 if (!uid_valid(kloginuid))
1316 return -EINVAL;
1317 }
1318
1319 rv = audit_set_loginuid(kloginuid);
1320 if (rv < 0)
1321 return rv;
1322 return count;
1323 }
1324
1325 static const struct file_operations proc_loginuid_operations = {
1326 .read = proc_loginuid_read,
1327 .write = proc_loginuid_write,
1328 .llseek = generic_file_llseek,
1329 };
1330
proc_sessionid_read(struct file * file,char __user * buf,size_t count,loff_t * ppos)1331 static ssize_t proc_sessionid_read(struct file * file, char __user * buf,
1332 size_t count, loff_t *ppos)
1333 {
1334 struct inode * inode = file_inode(file);
1335 struct task_struct *task = get_proc_task(inode);
1336 ssize_t length;
1337 char tmpbuf[TMPBUFLEN];
1338
1339 if (!task)
1340 return -ESRCH;
1341 length = scnprintf(tmpbuf, TMPBUFLEN, "%u",
1342 audit_get_sessionid(task));
1343 put_task_struct(task);
1344 return simple_read_from_buffer(buf, count, ppos, tmpbuf, length);
1345 }
1346
1347 static const struct file_operations proc_sessionid_operations = {
1348 .read = proc_sessionid_read,
1349 .llseek = generic_file_llseek,
1350 };
1351 #endif
1352
1353 #ifdef CONFIG_FAULT_INJECTION
proc_fault_inject_read(struct file * file,char __user * buf,size_t count,loff_t * ppos)1354 static ssize_t proc_fault_inject_read(struct file * file, char __user * buf,
1355 size_t count, loff_t *ppos)
1356 {
1357 struct task_struct *task = get_proc_task(file_inode(file));
1358 char buffer[PROC_NUMBUF];
1359 size_t len;
1360 int make_it_fail;
1361
1362 if (!task)
1363 return -ESRCH;
1364 make_it_fail = task->make_it_fail;
1365 put_task_struct(task);
1366
1367 len = snprintf(buffer, sizeof(buffer), "%i\n", make_it_fail);
1368
1369 return simple_read_from_buffer(buf, count, ppos, buffer, len);
1370 }
1371
proc_fault_inject_write(struct file * file,const char __user * buf,size_t count,loff_t * ppos)1372 static ssize_t proc_fault_inject_write(struct file * file,
1373 const char __user * buf, size_t count, loff_t *ppos)
1374 {
1375 struct task_struct *task;
1376 char buffer[PROC_NUMBUF];
1377 int make_it_fail;
1378 int rv;
1379
1380 if (!capable(CAP_SYS_RESOURCE))
1381 return -EPERM;
1382 memset(buffer, 0, sizeof(buffer));
1383 if (count > sizeof(buffer) - 1)
1384 count = sizeof(buffer) - 1;
1385 if (copy_from_user(buffer, buf, count))
1386 return -EFAULT;
1387 rv = kstrtoint(strstrip(buffer), 0, &make_it_fail);
1388 if (rv < 0)
1389 return rv;
1390 if (make_it_fail < 0 || make_it_fail > 1)
1391 return -EINVAL;
1392
1393 task = get_proc_task(file_inode(file));
1394 if (!task)
1395 return -ESRCH;
1396 task->make_it_fail = make_it_fail;
1397 put_task_struct(task);
1398
1399 return count;
1400 }
1401
1402 static const struct file_operations proc_fault_inject_operations = {
1403 .read = proc_fault_inject_read,
1404 .write = proc_fault_inject_write,
1405 .llseek = generic_file_llseek,
1406 };
1407
proc_fail_nth_write(struct file * file,const char __user * buf,size_t count,loff_t * ppos)1408 static ssize_t proc_fail_nth_write(struct file *file, const char __user *buf,
1409 size_t count, loff_t *ppos)
1410 {
1411 struct task_struct *task;
1412 int err;
1413 unsigned int n;
1414
1415 err = kstrtouint_from_user(buf, count, 0, &n);
1416 if (err)
1417 return err;
1418
1419 task = get_proc_task(file_inode(file));
1420 if (!task)
1421 return -ESRCH;
1422 task->fail_nth = n;
1423 put_task_struct(task);
1424
1425 return count;
1426 }
1427
proc_fail_nth_read(struct file * file,char __user * buf,size_t count,loff_t * ppos)1428 static ssize_t proc_fail_nth_read(struct file *file, char __user *buf,
1429 size_t count, loff_t *ppos)
1430 {
1431 struct task_struct *task;
1432 char numbuf[PROC_NUMBUF];
1433 ssize_t len;
1434
1435 task = get_proc_task(file_inode(file));
1436 if (!task)
1437 return -ESRCH;
1438 len = snprintf(numbuf, sizeof(numbuf), "%u\n", task->fail_nth);
1439 put_task_struct(task);
1440 return simple_read_from_buffer(buf, count, ppos, numbuf, len);
1441 }
1442
1443 static const struct file_operations proc_fail_nth_operations = {
1444 .read = proc_fail_nth_read,
1445 .write = proc_fail_nth_write,
1446 };
1447 #endif
1448
1449
1450 #ifdef CONFIG_SCHED_DEBUG
1451 /*
1452 * Print out various scheduling related per-task fields:
1453 */
sched_show(struct seq_file * m,void * v)1454 static int sched_show(struct seq_file *m, void *v)
1455 {
1456 struct inode *inode = m->private;
1457 struct pid_namespace *ns = proc_pid_ns(inode->i_sb);
1458 struct task_struct *p;
1459
1460 p = get_proc_task(inode);
1461 if (!p)
1462 return -ESRCH;
1463 proc_sched_show_task(p, ns, m);
1464
1465 put_task_struct(p);
1466
1467 return 0;
1468 }
1469
1470 static ssize_t
sched_write(struct file * file,const char __user * buf,size_t count,loff_t * offset)1471 sched_write(struct file *file, const char __user *buf,
1472 size_t count, loff_t *offset)
1473 {
1474 struct inode *inode = file_inode(file);
1475 struct task_struct *p;
1476
1477 p = get_proc_task(inode);
1478 if (!p)
1479 return -ESRCH;
1480 proc_sched_set_task(p);
1481
1482 put_task_struct(p);
1483
1484 return count;
1485 }
1486
sched_open(struct inode * inode,struct file * filp)1487 static int sched_open(struct inode *inode, struct file *filp)
1488 {
1489 return single_open(filp, sched_show, inode);
1490 }
1491
1492 static const struct file_operations proc_pid_sched_operations = {
1493 .open = sched_open,
1494 .read = seq_read,
1495 .write = sched_write,
1496 .llseek = seq_lseek,
1497 .release = single_release,
1498 };
1499
1500 #endif
1501
1502 #ifdef CONFIG_SCHED_RTG_DEBUG
sched_group_id_show(struct seq_file * m,void * v)1503 static int sched_group_id_show(struct seq_file *m, void *v)
1504 {
1505 struct inode *inode = m->private;
1506 struct task_struct *p;
1507
1508 p = get_proc_task(inode);
1509 if (!p)
1510 return -ESRCH;
1511
1512 seq_printf(m, "%d\n", sched_get_group_id(p));
1513
1514 put_task_struct(p);
1515
1516 return 0;
1517 }
1518
1519 static ssize_t
sched_group_id_write(struct file * file,const char __user * buf,size_t count,loff_t * offset)1520 sched_group_id_write(struct file *file, const char __user *buf,
1521 size_t count, loff_t *offset)
1522 {
1523 struct inode *inode = file_inode(file);
1524 struct task_struct *p;
1525 char buffer[PROC_NUMBUF];
1526 int group_id, err;
1527
1528 memset(buffer, 0, sizeof(buffer));
1529 if (count > sizeof(buffer) - 1)
1530 count = sizeof(buffer) - 1;
1531 if (copy_from_user(buffer, buf, count)) {
1532 err = -EFAULT;
1533 goto out;
1534 }
1535
1536 err = kstrtoint(strstrip(buffer), 0, &group_id);
1537 if (err)
1538 goto out;
1539
1540 p = get_proc_task(inode);
1541 if (!p)
1542 return -ESRCH;
1543
1544 err = sched_set_group_id(p, group_id);
1545
1546 put_task_struct(p);
1547
1548 out:
1549 return err < 0 ? err : count;
1550 }
1551
sched_group_id_open(struct inode * inode,struct file * filp)1552 static int sched_group_id_open(struct inode *inode, struct file *filp)
1553 {
1554 return single_open(filp, sched_group_id_show, inode);
1555 }
1556
1557 static const struct file_operations proc_pid_sched_group_id_operations = {
1558 .open = sched_group_id_open,
1559 .read = seq_read,
1560 .write = sched_group_id_write,
1561 .llseek = seq_lseek,
1562 .release = single_release,
1563 };
1564 #endif /* CONFIG_SCHED_RTG_DEBUG */
1565
1566 #ifdef CONFIG_SCHED_AUTOGROUP
1567 /*
1568 * Print out autogroup related information:
1569 */
sched_autogroup_show(struct seq_file * m,void * v)1570 static int sched_autogroup_show(struct seq_file *m, void *v)
1571 {
1572 struct inode *inode = m->private;
1573 struct task_struct *p;
1574
1575 p = get_proc_task(inode);
1576 if (!p)
1577 return -ESRCH;
1578 proc_sched_autogroup_show_task(p, m);
1579
1580 put_task_struct(p);
1581
1582 return 0;
1583 }
1584
1585 static ssize_t
sched_autogroup_write(struct file * file,const char __user * buf,size_t count,loff_t * offset)1586 sched_autogroup_write(struct file *file, const char __user *buf,
1587 size_t count, loff_t *offset)
1588 {
1589 struct inode *inode = file_inode(file);
1590 struct task_struct *p;
1591 char buffer[PROC_NUMBUF];
1592 int nice;
1593 int err;
1594
1595 memset(buffer, 0, sizeof(buffer));
1596 if (count > sizeof(buffer) - 1)
1597 count = sizeof(buffer) - 1;
1598 if (copy_from_user(buffer, buf, count))
1599 return -EFAULT;
1600
1601 err = kstrtoint(strstrip(buffer), 0, &nice);
1602 if (err < 0)
1603 return err;
1604
1605 p = get_proc_task(inode);
1606 if (!p)
1607 return -ESRCH;
1608
1609 err = proc_sched_autogroup_set_nice(p, nice);
1610 if (err)
1611 count = err;
1612
1613 put_task_struct(p);
1614
1615 return count;
1616 }
1617
sched_autogroup_open(struct inode * inode,struct file * filp)1618 static int sched_autogroup_open(struct inode *inode, struct file *filp)
1619 {
1620 int ret;
1621
1622 ret = single_open(filp, sched_autogroup_show, NULL);
1623 if (!ret) {
1624 struct seq_file *m = filp->private_data;
1625
1626 m->private = inode;
1627 }
1628 return ret;
1629 }
1630
1631 static const struct file_operations proc_pid_sched_autogroup_operations = {
1632 .open = sched_autogroup_open,
1633 .read = seq_read,
1634 .write = sched_autogroup_write,
1635 .llseek = seq_lseek,
1636 .release = single_release,
1637 };
1638
1639 #endif /* CONFIG_SCHED_AUTOGROUP */
1640
1641 #ifdef CONFIG_SCHED_WALT
sched_init_task_load_show(struct seq_file * m,void * v)1642 static int sched_init_task_load_show(struct seq_file *m, void *v)
1643 {
1644 struct inode *inode = m->private;
1645 struct task_struct *p;
1646
1647 p = get_proc_task(inode);
1648 if (!p)
1649 return -ESRCH;
1650
1651 seq_printf(m, "%d\n", sched_get_init_task_load(p));
1652
1653 put_task_struct(p);
1654
1655 return 0;
1656 }
1657
1658 static ssize_t
sched_init_task_load_write(struct file * file,const char __user * buf,size_t count,loff_t * offset)1659 sched_init_task_load_write(struct file *file, const char __user *buf,
1660 size_t count, loff_t *offset)
1661 {
1662 struct inode *inode = file_inode(file);
1663 struct task_struct *p;
1664 char buffer[PROC_NUMBUF];
1665 int init_task_load, err;
1666
1667 memset(buffer, 0, sizeof(buffer));
1668 if (count > sizeof(buffer) - 1)
1669 count = sizeof(buffer) - 1;
1670 if (copy_from_user(buffer, buf, count)) {
1671 err = -EFAULT;
1672 goto out;
1673 }
1674
1675 err = kstrtoint(strstrip(buffer), 0, &init_task_load);
1676 if (err)
1677 goto out;
1678
1679 p = get_proc_task(inode);
1680 if (!p)
1681 return -ESRCH;
1682
1683 err = sched_set_init_task_load(p, init_task_load);
1684
1685 put_task_struct(p);
1686
1687 out:
1688 return err < 0 ? err : count;
1689 }
1690
sched_init_task_load_open(struct inode * inode,struct file * filp)1691 static int sched_init_task_load_open(struct inode *inode, struct file *filp)
1692 {
1693 return single_open(filp, sched_init_task_load_show, inode);
1694 }
1695
1696 static const struct file_operations proc_pid_sched_init_task_load_operations = {
1697 .open = sched_init_task_load_open,
1698 .read = seq_read,
1699 .write = sched_init_task_load_write,
1700 .llseek = seq_lseek,
1701 .release = single_release,
1702 };
1703 #endif /* CONFIG_SCHED_WALT */
1704
1705 #ifdef CONFIG_TIME_NS
timens_offsets_show(struct seq_file * m,void * v)1706 static int timens_offsets_show(struct seq_file *m, void *v)
1707 {
1708 struct task_struct *p;
1709
1710 p = get_proc_task(file_inode(m->file));
1711 if (!p)
1712 return -ESRCH;
1713 proc_timens_show_offsets(p, m);
1714
1715 put_task_struct(p);
1716
1717 return 0;
1718 }
1719
timens_offsets_write(struct file * file,const char __user * buf,size_t count,loff_t * ppos)1720 static ssize_t timens_offsets_write(struct file *file, const char __user *buf,
1721 size_t count, loff_t *ppos)
1722 {
1723 struct inode *inode = file_inode(file);
1724 struct proc_timens_offset offsets[2];
1725 char *kbuf = NULL, *pos, *next_line;
1726 struct task_struct *p;
1727 int ret, noffsets;
1728
1729 /* Only allow < page size writes at the beginning of the file */
1730 if ((*ppos != 0) || (count >= PAGE_SIZE))
1731 return -EINVAL;
1732
1733 /* Slurp in the user data */
1734 kbuf = memdup_user_nul(buf, count);
1735 if (IS_ERR(kbuf))
1736 return PTR_ERR(kbuf);
1737
1738 /* Parse the user data */
1739 ret = -EINVAL;
1740 noffsets = 0;
1741 for (pos = kbuf; pos; pos = next_line) {
1742 struct proc_timens_offset *off = &offsets[noffsets];
1743 char clock[10];
1744 int err;
1745
1746 /* Find the end of line and ensure we don't look past it */
1747 next_line = strchr(pos, '\n');
1748 if (next_line) {
1749 *next_line = '\0';
1750 next_line++;
1751 if (*next_line == '\0')
1752 next_line = NULL;
1753 }
1754
1755 err = sscanf(pos, "%9s %lld %lu", clock,
1756 &off->val.tv_sec, &off->val.tv_nsec);
1757 if (err != 3 || off->val.tv_nsec >= NSEC_PER_SEC)
1758 goto out;
1759
1760 clock[sizeof(clock) - 1] = 0;
1761 if (strcmp(clock, "monotonic") == 0 ||
1762 strcmp(clock, __stringify(CLOCK_MONOTONIC)) == 0)
1763 off->clockid = CLOCK_MONOTONIC;
1764 else if (strcmp(clock, "boottime") == 0 ||
1765 strcmp(clock, __stringify(CLOCK_BOOTTIME)) == 0)
1766 off->clockid = CLOCK_BOOTTIME;
1767 else
1768 goto out;
1769
1770 noffsets++;
1771 if (noffsets == ARRAY_SIZE(offsets)) {
1772 if (next_line)
1773 count = next_line - kbuf;
1774 break;
1775 }
1776 }
1777
1778 ret = -ESRCH;
1779 p = get_proc_task(inode);
1780 if (!p)
1781 goto out;
1782 ret = proc_timens_set_offset(file, p, offsets, noffsets);
1783 put_task_struct(p);
1784 if (ret)
1785 goto out;
1786
1787 ret = count;
1788 out:
1789 kfree(kbuf);
1790 return ret;
1791 }
1792
timens_offsets_open(struct inode * inode,struct file * filp)1793 static int timens_offsets_open(struct inode *inode, struct file *filp)
1794 {
1795 return single_open(filp, timens_offsets_show, inode);
1796 }
1797
1798 static const struct file_operations proc_timens_offsets_operations = {
1799 .open = timens_offsets_open,
1800 .read = seq_read,
1801 .write = timens_offsets_write,
1802 .llseek = seq_lseek,
1803 .release = single_release,
1804 };
1805 #endif /* CONFIG_TIME_NS */
1806
comm_write(struct file * file,const char __user * buf,size_t count,loff_t * offset)1807 static ssize_t comm_write(struct file *file, const char __user *buf,
1808 size_t count, loff_t *offset)
1809 {
1810 struct inode *inode = file_inode(file);
1811 struct task_struct *p;
1812 char buffer[TASK_COMM_LEN];
1813 const size_t maxlen = sizeof(buffer) - 1;
1814
1815 memset(buffer, 0, sizeof(buffer));
1816 if (copy_from_user(buffer, buf, count > maxlen ? maxlen : count))
1817 return -EFAULT;
1818
1819 p = get_proc_task(inode);
1820 if (!p)
1821 return -ESRCH;
1822
1823 if (same_thread_group(current, p))
1824 set_task_comm(p, buffer);
1825 else
1826 count = -EINVAL;
1827
1828 put_task_struct(p);
1829
1830 return count;
1831 }
1832
comm_show(struct seq_file * m,void * v)1833 static int comm_show(struct seq_file *m, void *v)
1834 {
1835 struct inode *inode = m->private;
1836 struct task_struct *p;
1837
1838 p = get_proc_task(inode);
1839 if (!p)
1840 return -ESRCH;
1841
1842 proc_task_name(m, p, false);
1843 seq_putc(m, '\n');
1844
1845 put_task_struct(p);
1846
1847 return 0;
1848 }
1849
comm_open(struct inode * inode,struct file * filp)1850 static int comm_open(struct inode *inode, struct file *filp)
1851 {
1852 return single_open(filp, comm_show, inode);
1853 }
1854
1855 static const struct file_operations proc_pid_set_comm_operations = {
1856 .open = comm_open,
1857 .read = seq_read,
1858 .write = comm_write,
1859 .llseek = seq_lseek,
1860 .release = single_release,
1861 };
1862
proc_exe_link(struct dentry * dentry,struct path * exe_path)1863 static int proc_exe_link(struct dentry *dentry, struct path *exe_path)
1864 {
1865 struct task_struct *task;
1866 struct file *exe_file;
1867
1868 task = get_proc_task(d_inode(dentry));
1869 if (!task)
1870 return -ENOENT;
1871 exe_file = get_task_exe_file(task);
1872 put_task_struct(task);
1873 if (exe_file) {
1874 *exe_path = exe_file->f_path;
1875 path_get(&exe_file->f_path);
1876 fput(exe_file);
1877 return 0;
1878 } else
1879 return -ENOENT;
1880 }
1881
proc_pid_get_link(struct dentry * dentry,struct inode * inode,struct delayed_call * done)1882 static const char *proc_pid_get_link(struct dentry *dentry,
1883 struct inode *inode,
1884 struct delayed_call *done)
1885 {
1886 struct path path;
1887 int error = -EACCES;
1888
1889 if (!dentry)
1890 return ERR_PTR(-ECHILD);
1891
1892 /* Are we allowed to snoop on the tasks file descriptors? */
1893 if (!proc_fd_access_allowed(inode))
1894 goto out;
1895
1896 error = PROC_I(inode)->op.proc_get_link(dentry, &path);
1897 if (error)
1898 goto out;
1899
1900 error = nd_jump_link(&path);
1901 out:
1902 return ERR_PTR(error);
1903 }
1904
do_proc_readlink(struct path * path,char __user * buffer,int buflen)1905 static int do_proc_readlink(struct path *path, char __user *buffer, int buflen)
1906 {
1907 char *tmp = (char *)__get_free_page(GFP_KERNEL);
1908 char *pathname;
1909 int len;
1910
1911 if (!tmp)
1912 return -ENOMEM;
1913
1914 pathname = d_path(path, tmp, PAGE_SIZE);
1915 len = PTR_ERR(pathname);
1916 if (IS_ERR(pathname))
1917 goto out;
1918 len = tmp + PAGE_SIZE - 1 - pathname;
1919
1920 if (len > buflen)
1921 len = buflen;
1922 if (copy_to_user(buffer, pathname, len))
1923 len = -EFAULT;
1924 out:
1925 free_page((unsigned long)tmp);
1926 return len;
1927 }
1928
proc_pid_readlink(struct dentry * dentry,char __user * buffer,int buflen)1929 static int proc_pid_readlink(struct dentry * dentry, char __user * buffer, int buflen)
1930 {
1931 int error = -EACCES;
1932 struct inode *inode = d_inode(dentry);
1933 struct path path;
1934
1935 /* Are we allowed to snoop on the tasks file descriptors? */
1936 if (!proc_fd_access_allowed(inode))
1937 goto out;
1938
1939 error = PROC_I(inode)->op.proc_get_link(dentry, &path);
1940 if (error)
1941 goto out;
1942
1943 error = do_proc_readlink(&path, buffer, buflen);
1944 path_put(&path);
1945 out:
1946 return error;
1947 }
1948
1949 const struct inode_operations proc_pid_link_inode_operations = {
1950 .readlink = proc_pid_readlink,
1951 .get_link = proc_pid_get_link,
1952 .setattr = proc_setattr,
1953 };
1954
1955
1956 /* building an inode */
1957
task_dump_owner(struct task_struct * task,umode_t mode,kuid_t * ruid,kgid_t * rgid)1958 void task_dump_owner(struct task_struct *task, umode_t mode,
1959 kuid_t *ruid, kgid_t *rgid)
1960 {
1961 /* Depending on the state of dumpable compute who should own a
1962 * proc file for a task.
1963 */
1964 const struct cred *cred;
1965 kuid_t uid;
1966 kgid_t gid;
1967
1968 if (unlikely(task->flags & PF_KTHREAD)) {
1969 *ruid = GLOBAL_ROOT_UID;
1970 *rgid = GLOBAL_ROOT_GID;
1971 return;
1972 }
1973
1974 /* Default to the tasks effective ownership */
1975 rcu_read_lock();
1976 cred = __task_cred(task);
1977 uid = cred->euid;
1978 gid = cred->egid;
1979 rcu_read_unlock();
1980
1981 /*
1982 * Before the /proc/pid/status file was created the only way to read
1983 * the effective uid of a /process was to stat /proc/pid. Reading
1984 * /proc/pid/status is slow enough that procps and other packages
1985 * kept stating /proc/pid. To keep the rules in /proc simple I have
1986 * made this apply to all per process world readable and executable
1987 * directories.
1988 */
1989 if (mode != (S_IFDIR|S_IRUGO|S_IXUGO)) {
1990 struct mm_struct *mm;
1991 task_lock(task);
1992 mm = task->mm;
1993 /* Make non-dumpable tasks owned by some root */
1994 if (mm) {
1995 if (get_dumpable(mm) != SUID_DUMP_USER) {
1996 struct user_namespace *user_ns = mm->user_ns;
1997
1998 uid = make_kuid(user_ns, 0);
1999 if (!uid_valid(uid))
2000 uid = GLOBAL_ROOT_UID;
2001
2002 gid = make_kgid(user_ns, 0);
2003 if (!gid_valid(gid))
2004 gid = GLOBAL_ROOT_GID;
2005 }
2006 } else {
2007 uid = GLOBAL_ROOT_UID;
2008 gid = GLOBAL_ROOT_GID;
2009 }
2010 task_unlock(task);
2011 }
2012 *ruid = uid;
2013 *rgid = gid;
2014 }
2015
proc_pid_evict_inode(struct proc_inode * ei)2016 void proc_pid_evict_inode(struct proc_inode *ei)
2017 {
2018 struct pid *pid = ei->pid;
2019
2020 if (S_ISDIR(ei->vfs_inode.i_mode)) {
2021 spin_lock(&pid->lock);
2022 hlist_del_init_rcu(&ei->sibling_inodes);
2023 spin_unlock(&pid->lock);
2024 }
2025
2026 put_pid(pid);
2027 }
2028
proc_pid_make_inode(struct super_block * sb,struct task_struct * task,umode_t mode)2029 struct inode *proc_pid_make_inode(struct super_block * sb,
2030 struct task_struct *task, umode_t mode)
2031 {
2032 struct inode * inode;
2033 struct proc_inode *ei;
2034 struct pid *pid;
2035
2036 /* We need a new inode */
2037
2038 inode = new_inode(sb);
2039 if (!inode)
2040 goto out;
2041
2042 /* Common stuff */
2043 ei = PROC_I(inode);
2044 inode->i_mode = mode;
2045 inode->i_ino = get_next_ino();
2046 inode->i_mtime = inode->i_atime = inode->i_ctime = current_time(inode);
2047 inode->i_op = &proc_def_inode_operations;
2048
2049 /*
2050 * grab the reference to task.
2051 */
2052 pid = get_task_pid(task, PIDTYPE_PID);
2053 if (!pid)
2054 goto out_unlock;
2055
2056 /* Let the pid remember us for quick removal */
2057 ei->pid = pid;
2058 if (S_ISDIR(mode)) {
2059 spin_lock(&pid->lock);
2060 hlist_add_head_rcu(&ei->sibling_inodes, &pid->inodes);
2061 spin_unlock(&pid->lock);
2062 }
2063
2064 task_dump_owner(task, 0, &inode->i_uid, &inode->i_gid);
2065 security_task_to_inode(task, inode);
2066
2067 out:
2068 return inode;
2069
2070 out_unlock:
2071 iput(inode);
2072 return NULL;
2073 }
2074
pid_getattr(const struct path * path,struct kstat * stat,u32 request_mask,unsigned int query_flags)2075 int pid_getattr(const struct path *path, struct kstat *stat,
2076 u32 request_mask, unsigned int query_flags)
2077 {
2078 struct inode *inode = d_inode(path->dentry);
2079 struct proc_fs_info *fs_info = proc_sb_info(inode->i_sb);
2080 struct task_struct *task;
2081
2082 generic_fillattr(inode, stat);
2083
2084 stat->uid = GLOBAL_ROOT_UID;
2085 stat->gid = GLOBAL_ROOT_GID;
2086 rcu_read_lock();
2087 task = pid_task(proc_pid(inode), PIDTYPE_PID);
2088 if (task) {
2089 if (!has_pid_permissions(fs_info, task, HIDEPID_INVISIBLE)) {
2090 rcu_read_unlock();
2091 /*
2092 * This doesn't prevent learning whether PID exists,
2093 * it only makes getattr() consistent with readdir().
2094 */
2095 return -ENOENT;
2096 }
2097 task_dump_owner(task, inode->i_mode, &stat->uid, &stat->gid);
2098 }
2099 rcu_read_unlock();
2100 return 0;
2101 }
2102
2103 /* dentry stuff */
2104
2105 /*
2106 * Set <pid>/... inode ownership (can change due to setuid(), etc.)
2107 */
pid_update_inode(struct task_struct * task,struct inode * inode)2108 void pid_update_inode(struct task_struct *task, struct inode *inode)
2109 {
2110 task_dump_owner(task, inode->i_mode, &inode->i_uid, &inode->i_gid);
2111
2112 inode->i_mode &= ~(S_ISUID | S_ISGID);
2113 security_task_to_inode(task, inode);
2114 }
2115
2116 /*
2117 * Rewrite the inode's ownerships here because the owning task may have
2118 * performed a setuid(), etc.
2119 *
2120 */
pid_revalidate(struct dentry * dentry,unsigned int flags)2121 static int pid_revalidate(struct dentry *dentry, unsigned int flags)
2122 {
2123 struct inode *inode;
2124 struct task_struct *task;
2125
2126 if (flags & LOOKUP_RCU)
2127 return -ECHILD;
2128
2129 inode = d_inode(dentry);
2130 task = get_proc_task(inode);
2131
2132 if (task) {
2133 pid_update_inode(task, inode);
2134 put_task_struct(task);
2135 return 1;
2136 }
2137 return 0;
2138 }
2139
proc_inode_is_dead(struct inode * inode)2140 static inline bool proc_inode_is_dead(struct inode *inode)
2141 {
2142 return !proc_pid(inode)->tasks[PIDTYPE_PID].first;
2143 }
2144
pid_delete_dentry(const struct dentry * dentry)2145 int pid_delete_dentry(const struct dentry *dentry)
2146 {
2147 /* Is the task we represent dead?
2148 * If so, then don't put the dentry on the lru list,
2149 * kill it immediately.
2150 */
2151 return proc_inode_is_dead(d_inode(dentry));
2152 }
2153
2154 const struct dentry_operations pid_dentry_operations =
2155 {
2156 .d_revalidate = pid_revalidate,
2157 .d_delete = pid_delete_dentry,
2158 };
2159
2160 /* Lookups */
2161
2162 /*
2163 * Fill a directory entry.
2164 *
2165 * If possible create the dcache entry and derive our inode number and
2166 * file type from dcache entry.
2167 *
2168 * Since all of the proc inode numbers are dynamically generated, the inode
2169 * numbers do not exist until the inode is cache. This means creating the
2170 * the dcache entry in readdir is necessary to keep the inode numbers
2171 * reported by readdir in sync with the inode numbers reported
2172 * by stat.
2173 */
proc_fill_cache(struct file * file,struct dir_context * ctx,const char * name,unsigned int len,instantiate_t instantiate,struct task_struct * task,const void * ptr)2174 bool proc_fill_cache(struct file *file, struct dir_context *ctx,
2175 const char *name, unsigned int len,
2176 instantiate_t instantiate, struct task_struct *task, const void *ptr)
2177 {
2178 struct dentry *child, *dir = file->f_path.dentry;
2179 struct qstr qname = QSTR_INIT(name, len);
2180 struct inode *inode;
2181 unsigned type = DT_UNKNOWN;
2182 ino_t ino = 1;
2183
2184 child = d_hash_and_lookup(dir, &qname);
2185 if (!child) {
2186 DECLARE_WAIT_QUEUE_HEAD_ONSTACK(wq);
2187 child = d_alloc_parallel(dir, &qname, &wq);
2188 if (IS_ERR(child))
2189 goto end_instantiate;
2190 if (d_in_lookup(child)) {
2191 struct dentry *res;
2192 res = instantiate(child, task, ptr);
2193 d_lookup_done(child);
2194 if (unlikely(res)) {
2195 dput(child);
2196 child = res;
2197 if (IS_ERR(child))
2198 goto end_instantiate;
2199 }
2200 }
2201 }
2202 inode = d_inode(child);
2203 ino = inode->i_ino;
2204 type = inode->i_mode >> 12;
2205 dput(child);
2206 end_instantiate:
2207 return dir_emit(ctx, name, len, ino, type);
2208 }
2209
2210 /*
2211 * dname_to_vma_addr - maps a dentry name into two unsigned longs
2212 * which represent vma start and end addresses.
2213 */
dname_to_vma_addr(struct dentry * dentry,unsigned long * start,unsigned long * end)2214 static int dname_to_vma_addr(struct dentry *dentry,
2215 unsigned long *start, unsigned long *end)
2216 {
2217 const char *str = dentry->d_name.name;
2218 unsigned long long sval, eval;
2219 unsigned int len;
2220
2221 if (str[0] == '0' && str[1] != '-')
2222 return -EINVAL;
2223 len = _parse_integer(str, 16, &sval);
2224 if (len & KSTRTOX_OVERFLOW)
2225 return -EINVAL;
2226 if (sval != (unsigned long)sval)
2227 return -EINVAL;
2228 str += len;
2229
2230 if (*str != '-')
2231 return -EINVAL;
2232 str++;
2233
2234 if (str[0] == '0' && str[1])
2235 return -EINVAL;
2236 len = _parse_integer(str, 16, &eval);
2237 if (len & KSTRTOX_OVERFLOW)
2238 return -EINVAL;
2239 if (eval != (unsigned long)eval)
2240 return -EINVAL;
2241 str += len;
2242
2243 if (*str != '\0')
2244 return -EINVAL;
2245
2246 *start = sval;
2247 *end = eval;
2248
2249 return 0;
2250 }
2251
map_files_d_revalidate(struct dentry * dentry,unsigned int flags)2252 static int map_files_d_revalidate(struct dentry *dentry, unsigned int flags)
2253 {
2254 unsigned long vm_start, vm_end;
2255 bool exact_vma_exists = false;
2256 struct mm_struct *mm = NULL;
2257 struct task_struct *task;
2258 struct inode *inode;
2259 int status = 0;
2260
2261 if (flags & LOOKUP_RCU)
2262 return -ECHILD;
2263
2264 inode = d_inode(dentry);
2265 task = get_proc_task(inode);
2266 if (!task)
2267 goto out_notask;
2268
2269 mm = mm_access(task, PTRACE_MODE_READ_FSCREDS);
2270 if (IS_ERR_OR_NULL(mm))
2271 goto out;
2272
2273 if (!dname_to_vma_addr(dentry, &vm_start, &vm_end)) {
2274 status = mmap_read_lock_killable(mm);
2275 if (!status) {
2276 exact_vma_exists = !!find_exact_vma(mm, vm_start,
2277 vm_end);
2278 mmap_read_unlock(mm);
2279 }
2280 }
2281
2282 mmput(mm);
2283
2284 if (exact_vma_exists) {
2285 task_dump_owner(task, 0, &inode->i_uid, &inode->i_gid);
2286
2287 security_task_to_inode(task, inode);
2288 status = 1;
2289 }
2290
2291 out:
2292 put_task_struct(task);
2293
2294 out_notask:
2295 return status;
2296 }
2297
2298 static const struct dentry_operations tid_map_files_dentry_operations = {
2299 .d_revalidate = map_files_d_revalidate,
2300 .d_delete = pid_delete_dentry,
2301 };
2302
map_files_get_link(struct dentry * dentry,struct path * path)2303 static int map_files_get_link(struct dentry *dentry, struct path *path)
2304 {
2305 unsigned long vm_start, vm_end;
2306 struct vm_area_struct *vma;
2307 struct task_struct *task;
2308 struct mm_struct *mm;
2309 int rc;
2310
2311 rc = -ENOENT;
2312 task = get_proc_task(d_inode(dentry));
2313 if (!task)
2314 goto out;
2315
2316 mm = get_task_mm(task);
2317 put_task_struct(task);
2318 if (!mm)
2319 goto out;
2320
2321 rc = dname_to_vma_addr(dentry, &vm_start, &vm_end);
2322 if (rc)
2323 goto out_mmput;
2324
2325 rc = mmap_read_lock_killable(mm);
2326 if (rc)
2327 goto out_mmput;
2328
2329 rc = -ENOENT;
2330 vma = find_exact_vma(mm, vm_start, vm_end);
2331 if (vma && vma->vm_file) {
2332 *path = vma->vm_file->f_path;
2333 path_get(path);
2334 rc = 0;
2335 }
2336 mmap_read_unlock(mm);
2337
2338 out_mmput:
2339 mmput(mm);
2340 out:
2341 return rc;
2342 }
2343
2344 struct map_files_info {
2345 unsigned long start;
2346 unsigned long end;
2347 fmode_t mode;
2348 };
2349
2350 /*
2351 * Only allow CAP_SYS_ADMIN and CAP_CHECKPOINT_RESTORE to follow the links, due
2352 * to concerns about how the symlinks may be used to bypass permissions on
2353 * ancestor directories in the path to the file in question.
2354 */
2355 static const char *
proc_map_files_get_link(struct dentry * dentry,struct inode * inode,struct delayed_call * done)2356 proc_map_files_get_link(struct dentry *dentry,
2357 struct inode *inode,
2358 struct delayed_call *done)
2359 {
2360 if (!checkpoint_restore_ns_capable(&init_user_ns))
2361 return ERR_PTR(-EPERM);
2362
2363 return proc_pid_get_link(dentry, inode, done);
2364 }
2365
2366 /*
2367 * Identical to proc_pid_link_inode_operations except for get_link()
2368 */
2369 static const struct inode_operations proc_map_files_link_inode_operations = {
2370 .readlink = proc_pid_readlink,
2371 .get_link = proc_map_files_get_link,
2372 .setattr = proc_setattr,
2373 };
2374
2375 static struct dentry *
proc_map_files_instantiate(struct dentry * dentry,struct task_struct * task,const void * ptr)2376 proc_map_files_instantiate(struct dentry *dentry,
2377 struct task_struct *task, const void *ptr)
2378 {
2379 fmode_t mode = (fmode_t)(unsigned long)ptr;
2380 struct proc_inode *ei;
2381 struct inode *inode;
2382
2383 inode = proc_pid_make_inode(dentry->d_sb, task, S_IFLNK |
2384 ((mode & FMODE_READ ) ? S_IRUSR : 0) |
2385 ((mode & FMODE_WRITE) ? S_IWUSR : 0));
2386 if (!inode)
2387 return ERR_PTR(-ENOENT);
2388
2389 ei = PROC_I(inode);
2390 ei->op.proc_get_link = map_files_get_link;
2391
2392 inode->i_op = &proc_map_files_link_inode_operations;
2393 inode->i_size = 64;
2394
2395 d_set_d_op(dentry, &tid_map_files_dentry_operations);
2396 return d_splice_alias(inode, dentry);
2397 }
2398
proc_map_files_lookup(struct inode * dir,struct dentry * dentry,unsigned int flags)2399 static struct dentry *proc_map_files_lookup(struct inode *dir,
2400 struct dentry *dentry, unsigned int flags)
2401 {
2402 unsigned long vm_start, vm_end;
2403 struct vm_area_struct *vma;
2404 struct task_struct *task;
2405 struct dentry *result;
2406 struct mm_struct *mm;
2407
2408 result = ERR_PTR(-ENOENT);
2409 task = get_proc_task(dir);
2410 if (!task)
2411 goto out;
2412
2413 result = ERR_PTR(-EACCES);
2414 if (!ptrace_may_access(task, PTRACE_MODE_READ_FSCREDS))
2415 goto out_put_task;
2416
2417 result = ERR_PTR(-ENOENT);
2418 if (dname_to_vma_addr(dentry, &vm_start, &vm_end))
2419 goto out_put_task;
2420
2421 mm = get_task_mm(task);
2422 if (!mm)
2423 goto out_put_task;
2424
2425 result = ERR_PTR(-EINTR);
2426 if (mmap_read_lock_killable(mm))
2427 goto out_put_mm;
2428
2429 result = ERR_PTR(-ENOENT);
2430 vma = find_exact_vma(mm, vm_start, vm_end);
2431 if (!vma)
2432 goto out_no_vma;
2433
2434 if (vma->vm_file)
2435 result = proc_map_files_instantiate(dentry, task,
2436 (void *)(unsigned long)vma->vm_file->f_mode);
2437
2438 out_no_vma:
2439 mmap_read_unlock(mm);
2440 out_put_mm:
2441 mmput(mm);
2442 out_put_task:
2443 put_task_struct(task);
2444 out:
2445 return result;
2446 }
2447
2448 static const struct inode_operations proc_map_files_inode_operations = {
2449 .lookup = proc_map_files_lookup,
2450 .permission = proc_fd_permission,
2451 .setattr = proc_setattr,
2452 };
2453
2454 static int
proc_map_files_readdir(struct file * file,struct dir_context * ctx)2455 proc_map_files_readdir(struct file *file, struct dir_context *ctx)
2456 {
2457 struct vm_area_struct *vma;
2458 struct task_struct *task;
2459 struct mm_struct *mm;
2460 unsigned long nr_files, pos, i;
2461 GENRADIX(struct map_files_info) fa;
2462 struct map_files_info *p;
2463 int ret;
2464
2465 genradix_init(&fa);
2466
2467 ret = -ENOENT;
2468 task = get_proc_task(file_inode(file));
2469 if (!task)
2470 goto out;
2471
2472 ret = -EACCES;
2473 if (!ptrace_may_access(task, PTRACE_MODE_READ_FSCREDS))
2474 goto out_put_task;
2475
2476 ret = 0;
2477 if (!dir_emit_dots(file, ctx))
2478 goto out_put_task;
2479
2480 mm = get_task_mm(task);
2481 if (!mm)
2482 goto out_put_task;
2483
2484 ret = mmap_read_lock_killable(mm);
2485 if (ret) {
2486 mmput(mm);
2487 goto out_put_task;
2488 }
2489
2490 nr_files = 0;
2491
2492 /*
2493 * We need two passes here:
2494 *
2495 * 1) Collect vmas of mapped files with mmap_lock taken
2496 * 2) Release mmap_lock and instantiate entries
2497 *
2498 * otherwise we get lockdep complained, since filldir()
2499 * routine might require mmap_lock taken in might_fault().
2500 */
2501
2502 for (vma = mm->mmap, pos = 2; vma; vma = vma->vm_next) {
2503 if (!vma->vm_file)
2504 continue;
2505 if (++pos <= ctx->pos)
2506 continue;
2507
2508 p = genradix_ptr_alloc(&fa, nr_files++, GFP_KERNEL);
2509 if (!p) {
2510 ret = -ENOMEM;
2511 mmap_read_unlock(mm);
2512 mmput(mm);
2513 goto out_put_task;
2514 }
2515
2516 p->start = vma->vm_start;
2517 p->end = vma->vm_end;
2518 p->mode = vma->vm_file->f_mode;
2519 }
2520 mmap_read_unlock(mm);
2521 mmput(mm);
2522
2523 for (i = 0; i < nr_files; i++) {
2524 char buf[4 * sizeof(long) + 2]; /* max: %lx-%lx\0 */
2525 unsigned int len;
2526
2527 p = genradix_ptr(&fa, i);
2528 len = snprintf(buf, sizeof(buf), "%lx-%lx", p->start, p->end);
2529 if (!proc_fill_cache(file, ctx,
2530 buf, len,
2531 proc_map_files_instantiate,
2532 task,
2533 (void *)(unsigned long)p->mode))
2534 break;
2535 ctx->pos++;
2536 }
2537
2538 out_put_task:
2539 put_task_struct(task);
2540 out:
2541 genradix_free(&fa);
2542 return ret;
2543 }
2544
2545 static const struct file_operations proc_map_files_operations = {
2546 .read = generic_read_dir,
2547 .iterate_shared = proc_map_files_readdir,
2548 .llseek = generic_file_llseek,
2549 };
2550
2551 #if defined(CONFIG_CHECKPOINT_RESTORE) && defined(CONFIG_POSIX_TIMERS)
2552 struct timers_private {
2553 struct pid *pid;
2554 struct task_struct *task;
2555 struct sighand_struct *sighand;
2556 struct pid_namespace *ns;
2557 unsigned long flags;
2558 };
2559
timers_start(struct seq_file * m,loff_t * pos)2560 static void *timers_start(struct seq_file *m, loff_t *pos)
2561 {
2562 struct timers_private *tp = m->private;
2563
2564 tp->task = get_pid_task(tp->pid, PIDTYPE_PID);
2565 if (!tp->task)
2566 return ERR_PTR(-ESRCH);
2567
2568 tp->sighand = lock_task_sighand(tp->task, &tp->flags);
2569 if (!tp->sighand)
2570 return ERR_PTR(-ESRCH);
2571
2572 return seq_list_start(&tp->task->signal->posix_timers, *pos);
2573 }
2574
timers_next(struct seq_file * m,void * v,loff_t * pos)2575 static void *timers_next(struct seq_file *m, void *v, loff_t *pos)
2576 {
2577 struct timers_private *tp = m->private;
2578 return seq_list_next(v, &tp->task->signal->posix_timers, pos);
2579 }
2580
timers_stop(struct seq_file * m,void * v)2581 static void timers_stop(struct seq_file *m, void *v)
2582 {
2583 struct timers_private *tp = m->private;
2584
2585 if (tp->sighand) {
2586 unlock_task_sighand(tp->task, &tp->flags);
2587 tp->sighand = NULL;
2588 }
2589
2590 if (tp->task) {
2591 put_task_struct(tp->task);
2592 tp->task = NULL;
2593 }
2594 }
2595
show_timer(struct seq_file * m,void * v)2596 static int show_timer(struct seq_file *m, void *v)
2597 {
2598 struct k_itimer *timer;
2599 struct timers_private *tp = m->private;
2600 int notify;
2601 static const char * const nstr[] = {
2602 [SIGEV_SIGNAL] = "signal",
2603 [SIGEV_NONE] = "none",
2604 [SIGEV_THREAD] = "thread",
2605 };
2606
2607 timer = list_entry((struct list_head *)v, struct k_itimer, list);
2608 notify = timer->it_sigev_notify;
2609
2610 seq_printf(m, "ID: %d\n", timer->it_id);
2611 seq_printf(m, "signal: %d/%px\n",
2612 timer->sigq->info.si_signo,
2613 timer->sigq->info.si_value.sival_ptr);
2614 seq_printf(m, "notify: %s/%s.%d\n",
2615 nstr[notify & ~SIGEV_THREAD_ID],
2616 (notify & SIGEV_THREAD_ID) ? "tid" : "pid",
2617 pid_nr_ns(timer->it_pid, tp->ns));
2618 seq_printf(m, "ClockID: %d\n", timer->it_clock);
2619
2620 return 0;
2621 }
2622
2623 static const struct seq_operations proc_timers_seq_ops = {
2624 .start = timers_start,
2625 .next = timers_next,
2626 .stop = timers_stop,
2627 .show = show_timer,
2628 };
2629
proc_timers_open(struct inode * inode,struct file * file)2630 static int proc_timers_open(struct inode *inode, struct file *file)
2631 {
2632 struct timers_private *tp;
2633
2634 tp = __seq_open_private(file, &proc_timers_seq_ops,
2635 sizeof(struct timers_private));
2636 if (!tp)
2637 return -ENOMEM;
2638
2639 tp->pid = proc_pid(inode);
2640 tp->ns = proc_pid_ns(inode->i_sb);
2641 return 0;
2642 }
2643
2644 static const struct file_operations proc_timers_operations = {
2645 .open = proc_timers_open,
2646 .read = seq_read,
2647 .llseek = seq_lseek,
2648 .release = seq_release_private,
2649 };
2650 #endif
2651
timerslack_ns_write(struct file * file,const char __user * buf,size_t count,loff_t * offset)2652 static ssize_t timerslack_ns_write(struct file *file, const char __user *buf,
2653 size_t count, loff_t *offset)
2654 {
2655 struct inode *inode = file_inode(file);
2656 struct task_struct *p;
2657 u64 slack_ns;
2658 int err;
2659
2660 err = kstrtoull_from_user(buf, count, 10, &slack_ns);
2661 if (err < 0)
2662 return err;
2663
2664 p = get_proc_task(inode);
2665 if (!p)
2666 return -ESRCH;
2667
2668 if (p != current) {
2669 rcu_read_lock();
2670 if (!ns_capable(__task_cred(p)->user_ns, CAP_SYS_NICE)) {
2671 rcu_read_unlock();
2672 count = -EPERM;
2673 goto out;
2674 }
2675 rcu_read_unlock();
2676
2677 err = security_task_setscheduler(p);
2678 if (err) {
2679 count = err;
2680 goto out;
2681 }
2682 }
2683
2684 task_lock(p);
2685 if (slack_ns == 0)
2686 p->timer_slack_ns = p->default_timer_slack_ns;
2687 else
2688 p->timer_slack_ns = slack_ns;
2689 task_unlock(p);
2690
2691 out:
2692 put_task_struct(p);
2693
2694 return count;
2695 }
2696
timerslack_ns_show(struct seq_file * m,void * v)2697 static int timerslack_ns_show(struct seq_file *m, void *v)
2698 {
2699 struct inode *inode = m->private;
2700 struct task_struct *p;
2701 int err = 0;
2702
2703 p = get_proc_task(inode);
2704 if (!p)
2705 return -ESRCH;
2706
2707 if (p != current) {
2708 rcu_read_lock();
2709 if (!ns_capable(__task_cred(p)->user_ns, CAP_SYS_NICE)) {
2710 rcu_read_unlock();
2711 err = -EPERM;
2712 goto out;
2713 }
2714 rcu_read_unlock();
2715
2716 err = security_task_getscheduler(p);
2717 if (err)
2718 goto out;
2719 }
2720
2721 task_lock(p);
2722 seq_printf(m, "%llu\n", p->timer_slack_ns);
2723 task_unlock(p);
2724
2725 out:
2726 put_task_struct(p);
2727
2728 return err;
2729 }
2730
timerslack_ns_open(struct inode * inode,struct file * filp)2731 static int timerslack_ns_open(struct inode *inode, struct file *filp)
2732 {
2733 return single_open(filp, timerslack_ns_show, inode);
2734 }
2735
2736 static const struct file_operations proc_pid_set_timerslack_ns_operations = {
2737 .open = timerslack_ns_open,
2738 .read = seq_read,
2739 .write = timerslack_ns_write,
2740 .llseek = seq_lseek,
2741 .release = single_release,
2742 };
2743
proc_pident_instantiate(struct dentry * dentry,struct task_struct * task,const void * ptr)2744 static struct dentry *proc_pident_instantiate(struct dentry *dentry,
2745 struct task_struct *task, const void *ptr)
2746 {
2747 const struct pid_entry *p = ptr;
2748 struct inode *inode;
2749 struct proc_inode *ei;
2750
2751 inode = proc_pid_make_inode(dentry->d_sb, task, p->mode);
2752 if (!inode)
2753 return ERR_PTR(-ENOENT);
2754
2755 ei = PROC_I(inode);
2756 if (S_ISDIR(inode->i_mode))
2757 set_nlink(inode, 2); /* Use getattr to fix if necessary */
2758 if (p->iop)
2759 inode->i_op = p->iop;
2760 if (p->fop)
2761 inode->i_fop = p->fop;
2762 ei->op = p->op;
2763 pid_update_inode(task, inode);
2764 d_set_d_op(dentry, &pid_dentry_operations);
2765 return d_splice_alias(inode, dentry);
2766 }
2767
proc_pident_lookup(struct inode * dir,struct dentry * dentry,const struct pid_entry * p,const struct pid_entry * end)2768 static struct dentry *proc_pident_lookup(struct inode *dir,
2769 struct dentry *dentry,
2770 const struct pid_entry *p,
2771 const struct pid_entry *end)
2772 {
2773 struct task_struct *task = get_proc_task(dir);
2774 struct dentry *res = ERR_PTR(-ENOENT);
2775
2776 if (!task)
2777 goto out_no_task;
2778
2779 /*
2780 * Yes, it does not scale. And it should not. Don't add
2781 * new entries into /proc/<tgid>/ without very good reasons.
2782 */
2783 for (; p < end; p++) {
2784 if (p->len != dentry->d_name.len)
2785 continue;
2786 if (!memcmp(dentry->d_name.name, p->name, p->len)) {
2787 res = proc_pident_instantiate(dentry, task, p);
2788 break;
2789 }
2790 }
2791 put_task_struct(task);
2792 out_no_task:
2793 return res;
2794 }
2795
proc_pident_readdir(struct file * file,struct dir_context * ctx,const struct pid_entry * ents,unsigned int nents)2796 static int proc_pident_readdir(struct file *file, struct dir_context *ctx,
2797 const struct pid_entry *ents, unsigned int nents)
2798 {
2799 struct task_struct *task = get_proc_task(file_inode(file));
2800 const struct pid_entry *p;
2801
2802 if (!task)
2803 return -ENOENT;
2804
2805 if (!dir_emit_dots(file, ctx))
2806 goto out;
2807
2808 if (ctx->pos >= nents + 2)
2809 goto out;
2810
2811 for (p = ents + (ctx->pos - 2); p < ents + nents; p++) {
2812 if (!proc_fill_cache(file, ctx, p->name, p->len,
2813 proc_pident_instantiate, task, p))
2814 break;
2815 ctx->pos++;
2816 }
2817 out:
2818 put_task_struct(task);
2819 return 0;
2820 }
2821
2822 #ifdef CONFIG_SECURITY
proc_pid_attr_open(struct inode * inode,struct file * file)2823 static int proc_pid_attr_open(struct inode *inode, struct file *file)
2824 {
2825 file->private_data = NULL;
2826 __mem_open(inode, file, PTRACE_MODE_READ_FSCREDS);
2827 return 0;
2828 }
2829
proc_pid_attr_read(struct file * file,char __user * buf,size_t count,loff_t * ppos)2830 static ssize_t proc_pid_attr_read(struct file * file, char __user * buf,
2831 size_t count, loff_t *ppos)
2832 {
2833 struct inode * inode = file_inode(file);
2834 char *p = NULL;
2835 ssize_t length;
2836 struct task_struct *task = get_proc_task(inode);
2837
2838 if (!task)
2839 return -ESRCH;
2840
2841 length = security_getprocattr(task, PROC_I(inode)->op.lsm,
2842 (char*)file->f_path.dentry->d_name.name,
2843 &p);
2844 put_task_struct(task);
2845 if (length > 0)
2846 length = simple_read_from_buffer(buf, count, ppos, p, length);
2847 kfree(p);
2848 return length;
2849 }
2850
proc_pid_attr_write(struct file * file,const char __user * buf,size_t count,loff_t * ppos)2851 static ssize_t proc_pid_attr_write(struct file * file, const char __user * buf,
2852 size_t count, loff_t *ppos)
2853 {
2854 struct inode * inode = file_inode(file);
2855 struct task_struct *task;
2856 void *page;
2857 int rv;
2858
2859 /* A task may only write when it was the opener. */
2860 if (file->private_data != current->mm)
2861 return -EPERM;
2862
2863 rcu_read_lock();
2864 task = pid_task(proc_pid(inode), PIDTYPE_PID);
2865 if (!task) {
2866 rcu_read_unlock();
2867 return -ESRCH;
2868 }
2869 /* A task may only write its own attributes. */
2870 if (current != task) {
2871 rcu_read_unlock();
2872 return -EACCES;
2873 }
2874 /* Prevent changes to overridden credentials. */
2875 if (current_cred() != current_real_cred()) {
2876 rcu_read_unlock();
2877 return -EBUSY;
2878 }
2879 rcu_read_unlock();
2880
2881 if (count > PAGE_SIZE)
2882 count = PAGE_SIZE;
2883
2884 /* No partial writes. */
2885 if (*ppos != 0)
2886 return -EINVAL;
2887
2888 page = memdup_user(buf, count);
2889 if (IS_ERR(page)) {
2890 rv = PTR_ERR(page);
2891 goto out;
2892 }
2893
2894 /* Guard against adverse ptrace interaction */
2895 rv = mutex_lock_interruptible(¤t->signal->cred_guard_mutex);
2896 if (rv < 0)
2897 goto out_free;
2898
2899 rv = security_setprocattr(PROC_I(inode)->op.lsm,
2900 file->f_path.dentry->d_name.name, page,
2901 count);
2902 mutex_unlock(¤t->signal->cred_guard_mutex);
2903 out_free:
2904 kfree(page);
2905 out:
2906 return rv;
2907 }
2908
2909 static const struct file_operations proc_pid_attr_operations = {
2910 .open = proc_pid_attr_open,
2911 .read = proc_pid_attr_read,
2912 .write = proc_pid_attr_write,
2913 .llseek = generic_file_llseek,
2914 .release = mem_release,
2915 };
2916
2917 #define LSM_DIR_OPS(LSM) \
2918 static int proc_##LSM##_attr_dir_iterate(struct file *filp, \
2919 struct dir_context *ctx) \
2920 { \
2921 return proc_pident_readdir(filp, ctx, \
2922 LSM##_attr_dir_stuff, \
2923 ARRAY_SIZE(LSM##_attr_dir_stuff)); \
2924 } \
2925 \
2926 static const struct file_operations proc_##LSM##_attr_dir_ops = { \
2927 .read = generic_read_dir, \
2928 .iterate = proc_##LSM##_attr_dir_iterate, \
2929 .llseek = default_llseek, \
2930 }; \
2931 \
2932 static struct dentry *proc_##LSM##_attr_dir_lookup(struct inode *dir, \
2933 struct dentry *dentry, unsigned int flags) \
2934 { \
2935 return proc_pident_lookup(dir, dentry, \
2936 LSM##_attr_dir_stuff, \
2937 LSM##_attr_dir_stuff + ARRAY_SIZE(LSM##_attr_dir_stuff)); \
2938 } \
2939 \
2940 static const struct inode_operations proc_##LSM##_attr_dir_inode_ops = { \
2941 .lookup = proc_##LSM##_attr_dir_lookup, \
2942 .getattr = pid_getattr, \
2943 .setattr = proc_setattr, \
2944 }
2945
2946 #ifdef CONFIG_SECURITY_SMACK
2947 static const struct pid_entry smack_attr_dir_stuff[] = {
2948 ATTR("smack", "current", 0666),
2949 };
2950 LSM_DIR_OPS(smack);
2951 #endif
2952
2953 #ifdef CONFIG_SECURITY_APPARMOR
2954 static const struct pid_entry apparmor_attr_dir_stuff[] = {
2955 ATTR("apparmor", "current", 0666),
2956 ATTR("apparmor", "prev", 0444),
2957 ATTR("apparmor", "exec", 0666),
2958 };
2959 LSM_DIR_OPS(apparmor);
2960 #endif
2961
2962 static const struct pid_entry attr_dir_stuff[] = {
2963 ATTR(NULL, "current", 0666),
2964 ATTR(NULL, "prev", 0444),
2965 ATTR(NULL, "exec", 0666),
2966 ATTR(NULL, "fscreate", 0666),
2967 ATTR(NULL, "keycreate", 0666),
2968 ATTR(NULL, "sockcreate", 0666),
2969 #ifdef CONFIG_SECURITY_SMACK
2970 DIR("smack", 0555,
2971 proc_smack_attr_dir_inode_ops, proc_smack_attr_dir_ops),
2972 #endif
2973 #ifdef CONFIG_SECURITY_APPARMOR
2974 DIR("apparmor", 0555,
2975 proc_apparmor_attr_dir_inode_ops, proc_apparmor_attr_dir_ops),
2976 #endif
2977 };
2978
proc_attr_dir_readdir(struct file * file,struct dir_context * ctx)2979 static int proc_attr_dir_readdir(struct file *file, struct dir_context *ctx)
2980 {
2981 return proc_pident_readdir(file, ctx,
2982 attr_dir_stuff, ARRAY_SIZE(attr_dir_stuff));
2983 }
2984
2985 static const struct file_operations proc_attr_dir_operations = {
2986 .read = generic_read_dir,
2987 .iterate_shared = proc_attr_dir_readdir,
2988 .llseek = generic_file_llseek,
2989 };
2990
proc_attr_dir_lookup(struct inode * dir,struct dentry * dentry,unsigned int flags)2991 static struct dentry *proc_attr_dir_lookup(struct inode *dir,
2992 struct dentry *dentry, unsigned int flags)
2993 {
2994 return proc_pident_lookup(dir, dentry,
2995 attr_dir_stuff,
2996 attr_dir_stuff + ARRAY_SIZE(attr_dir_stuff));
2997 }
2998
2999 static const struct inode_operations proc_attr_dir_inode_operations = {
3000 .lookup = proc_attr_dir_lookup,
3001 .getattr = pid_getattr,
3002 .setattr = proc_setattr,
3003 };
3004
3005 #endif
3006
3007 #ifdef CONFIG_ELF_CORE
proc_coredump_filter_read(struct file * file,char __user * buf,size_t count,loff_t * ppos)3008 static ssize_t proc_coredump_filter_read(struct file *file, char __user *buf,
3009 size_t count, loff_t *ppos)
3010 {
3011 struct task_struct *task = get_proc_task(file_inode(file));
3012 struct mm_struct *mm;
3013 char buffer[PROC_NUMBUF];
3014 size_t len;
3015 int ret;
3016
3017 if (!task)
3018 return -ESRCH;
3019
3020 ret = 0;
3021 mm = get_task_mm(task);
3022 if (mm) {
3023 len = snprintf(buffer, sizeof(buffer), "%08lx\n",
3024 ((mm->flags & MMF_DUMP_FILTER_MASK) >>
3025 MMF_DUMP_FILTER_SHIFT));
3026 mmput(mm);
3027 ret = simple_read_from_buffer(buf, count, ppos, buffer, len);
3028 }
3029
3030 put_task_struct(task);
3031
3032 return ret;
3033 }
3034
proc_coredump_filter_write(struct file * file,const char __user * buf,size_t count,loff_t * ppos)3035 static ssize_t proc_coredump_filter_write(struct file *file,
3036 const char __user *buf,
3037 size_t count,
3038 loff_t *ppos)
3039 {
3040 struct task_struct *task;
3041 struct mm_struct *mm;
3042 unsigned int val;
3043 int ret;
3044 int i;
3045 unsigned long mask;
3046
3047 ret = kstrtouint_from_user(buf, count, 0, &val);
3048 if (ret < 0)
3049 return ret;
3050
3051 ret = -ESRCH;
3052 task = get_proc_task(file_inode(file));
3053 if (!task)
3054 goto out_no_task;
3055
3056 mm = get_task_mm(task);
3057 if (!mm)
3058 goto out_no_mm;
3059 ret = 0;
3060
3061 for (i = 0, mask = 1; i < MMF_DUMP_FILTER_BITS; i++, mask <<= 1) {
3062 if (val & mask)
3063 set_bit(i + MMF_DUMP_FILTER_SHIFT, &mm->flags);
3064 else
3065 clear_bit(i + MMF_DUMP_FILTER_SHIFT, &mm->flags);
3066 }
3067
3068 mmput(mm);
3069 out_no_mm:
3070 put_task_struct(task);
3071 out_no_task:
3072 if (ret < 0)
3073 return ret;
3074 return count;
3075 }
3076
3077 static const struct file_operations proc_coredump_filter_operations = {
3078 .read = proc_coredump_filter_read,
3079 .write = proc_coredump_filter_write,
3080 .llseek = generic_file_llseek,
3081 };
3082 #endif
3083
3084 #ifdef CONFIG_TASK_IO_ACCOUNTING
do_io_accounting(struct task_struct * task,struct seq_file * m,int whole)3085 static int do_io_accounting(struct task_struct *task, struct seq_file *m, int whole)
3086 {
3087 struct task_io_accounting acct = task->ioac;
3088 unsigned long flags;
3089 int result;
3090
3091 result = down_read_killable(&task->signal->exec_update_lock);
3092 if (result)
3093 return result;
3094
3095 if (!ptrace_may_access(task, PTRACE_MODE_READ_FSCREDS)) {
3096 result = -EACCES;
3097 goto out_unlock;
3098 }
3099
3100 if (whole && lock_task_sighand(task, &flags)) {
3101 struct task_struct *t = task;
3102
3103 task_io_accounting_add(&acct, &task->signal->ioac);
3104 while_each_thread(task, t)
3105 task_io_accounting_add(&acct, &t->ioac);
3106
3107 unlock_task_sighand(task, &flags);
3108 }
3109 seq_printf(m,
3110 "rchar: %llu\n"
3111 "wchar: %llu\n"
3112 "syscr: %llu\n"
3113 "syscw: %llu\n"
3114 "read_bytes: %llu\n"
3115 "write_bytes: %llu\n"
3116 "cancelled_write_bytes: %llu\n",
3117 (unsigned long long)acct.rchar,
3118 (unsigned long long)acct.wchar,
3119 (unsigned long long)acct.syscr,
3120 (unsigned long long)acct.syscw,
3121 (unsigned long long)acct.read_bytes,
3122 (unsigned long long)acct.write_bytes,
3123 (unsigned long long)acct.cancelled_write_bytes);
3124 result = 0;
3125
3126 out_unlock:
3127 up_read(&task->signal->exec_update_lock);
3128 return result;
3129 }
3130
proc_tid_io_accounting(struct seq_file * m,struct pid_namespace * ns,struct pid * pid,struct task_struct * task)3131 static int proc_tid_io_accounting(struct seq_file *m, struct pid_namespace *ns,
3132 struct pid *pid, struct task_struct *task)
3133 {
3134 return do_io_accounting(task, m, 0);
3135 }
3136
proc_tgid_io_accounting(struct seq_file * m,struct pid_namespace * ns,struct pid * pid,struct task_struct * task)3137 static int proc_tgid_io_accounting(struct seq_file *m, struct pid_namespace *ns,
3138 struct pid *pid, struct task_struct *task)
3139 {
3140 return do_io_accounting(task, m, 1);
3141 }
3142 #endif /* CONFIG_TASK_IO_ACCOUNTING */
3143
3144 #ifdef CONFIG_USER_NS
proc_id_map_open(struct inode * inode,struct file * file,const struct seq_operations * seq_ops)3145 static int proc_id_map_open(struct inode *inode, struct file *file,
3146 const struct seq_operations *seq_ops)
3147 {
3148 struct user_namespace *ns = NULL;
3149 struct task_struct *task;
3150 struct seq_file *seq;
3151 int ret = -EINVAL;
3152
3153 task = get_proc_task(inode);
3154 if (task) {
3155 rcu_read_lock();
3156 ns = get_user_ns(task_cred_xxx(task, user_ns));
3157 rcu_read_unlock();
3158 put_task_struct(task);
3159 }
3160 if (!ns)
3161 goto err;
3162
3163 ret = seq_open(file, seq_ops);
3164 if (ret)
3165 goto err_put_ns;
3166
3167 seq = file->private_data;
3168 seq->private = ns;
3169
3170 return 0;
3171 err_put_ns:
3172 put_user_ns(ns);
3173 err:
3174 return ret;
3175 }
3176
proc_id_map_release(struct inode * inode,struct file * file)3177 static int proc_id_map_release(struct inode *inode, struct file *file)
3178 {
3179 struct seq_file *seq = file->private_data;
3180 struct user_namespace *ns = seq->private;
3181 put_user_ns(ns);
3182 return seq_release(inode, file);
3183 }
3184
proc_uid_map_open(struct inode * inode,struct file * file)3185 static int proc_uid_map_open(struct inode *inode, struct file *file)
3186 {
3187 return proc_id_map_open(inode, file, &proc_uid_seq_operations);
3188 }
3189
proc_gid_map_open(struct inode * inode,struct file * file)3190 static int proc_gid_map_open(struct inode *inode, struct file *file)
3191 {
3192 return proc_id_map_open(inode, file, &proc_gid_seq_operations);
3193 }
3194
proc_projid_map_open(struct inode * inode,struct file * file)3195 static int proc_projid_map_open(struct inode *inode, struct file *file)
3196 {
3197 return proc_id_map_open(inode, file, &proc_projid_seq_operations);
3198 }
3199
3200 static const struct file_operations proc_uid_map_operations = {
3201 .open = proc_uid_map_open,
3202 .write = proc_uid_map_write,
3203 .read = seq_read,
3204 .llseek = seq_lseek,
3205 .release = proc_id_map_release,
3206 };
3207
3208 static const struct file_operations proc_gid_map_operations = {
3209 .open = proc_gid_map_open,
3210 .write = proc_gid_map_write,
3211 .read = seq_read,
3212 .llseek = seq_lseek,
3213 .release = proc_id_map_release,
3214 };
3215
3216 static const struct file_operations proc_projid_map_operations = {
3217 .open = proc_projid_map_open,
3218 .write = proc_projid_map_write,
3219 .read = seq_read,
3220 .llseek = seq_lseek,
3221 .release = proc_id_map_release,
3222 };
3223
proc_setgroups_open(struct inode * inode,struct file * file)3224 static int proc_setgroups_open(struct inode *inode, struct file *file)
3225 {
3226 struct user_namespace *ns = NULL;
3227 struct task_struct *task;
3228 int ret;
3229
3230 ret = -ESRCH;
3231 task = get_proc_task(inode);
3232 if (task) {
3233 rcu_read_lock();
3234 ns = get_user_ns(task_cred_xxx(task, user_ns));
3235 rcu_read_unlock();
3236 put_task_struct(task);
3237 }
3238 if (!ns)
3239 goto err;
3240
3241 if (file->f_mode & FMODE_WRITE) {
3242 ret = -EACCES;
3243 if (!ns_capable(ns, CAP_SYS_ADMIN))
3244 goto err_put_ns;
3245 }
3246
3247 ret = single_open(file, &proc_setgroups_show, ns);
3248 if (ret)
3249 goto err_put_ns;
3250
3251 return 0;
3252 err_put_ns:
3253 put_user_ns(ns);
3254 err:
3255 return ret;
3256 }
3257
proc_setgroups_release(struct inode * inode,struct file * file)3258 static int proc_setgroups_release(struct inode *inode, struct file *file)
3259 {
3260 struct seq_file *seq = file->private_data;
3261 struct user_namespace *ns = seq->private;
3262 int ret = single_release(inode, file);
3263 put_user_ns(ns);
3264 return ret;
3265 }
3266
3267 static const struct file_operations proc_setgroups_operations = {
3268 .open = proc_setgroups_open,
3269 .write = proc_setgroups_write,
3270 .read = seq_read,
3271 .llseek = seq_lseek,
3272 .release = proc_setgroups_release,
3273 };
3274 #endif /* CONFIG_USER_NS */
3275
proc_pid_personality(struct seq_file * m,struct pid_namespace * ns,struct pid * pid,struct task_struct * task)3276 static int proc_pid_personality(struct seq_file *m, struct pid_namespace *ns,
3277 struct pid *pid, struct task_struct *task)
3278 {
3279 int err = lock_trace(task);
3280 if (!err) {
3281 seq_printf(m, "%08x\n", task->personality);
3282 unlock_trace(task);
3283 }
3284 return err;
3285 }
3286
3287 #ifdef CONFIG_LIVEPATCH
proc_pid_patch_state(struct seq_file * m,struct pid_namespace * ns,struct pid * pid,struct task_struct * task)3288 static int proc_pid_patch_state(struct seq_file *m, struct pid_namespace *ns,
3289 struct pid *pid, struct task_struct *task)
3290 {
3291 seq_printf(m, "%d\n", task->patch_state);
3292 return 0;
3293 }
3294 #endif /* CONFIG_LIVEPATCH */
3295
3296 #ifdef CONFIG_STACKLEAK_METRICS
proc_stack_depth(struct seq_file * m,struct pid_namespace * ns,struct pid * pid,struct task_struct * task)3297 static int proc_stack_depth(struct seq_file *m, struct pid_namespace *ns,
3298 struct pid *pid, struct task_struct *task)
3299 {
3300 unsigned long prev_depth = THREAD_SIZE -
3301 (task->prev_lowest_stack & (THREAD_SIZE - 1));
3302 unsigned long depth = THREAD_SIZE -
3303 (task->lowest_stack & (THREAD_SIZE - 1));
3304
3305 seq_printf(m, "previous stack depth: %lu\nstack depth: %lu\n",
3306 prev_depth, depth);
3307 return 0;
3308 }
3309 #endif /* CONFIG_STACKLEAK_METRICS */
3310
3311 #ifdef CONFIG_ACCESS_TOKENID
proc_token_operations(struct seq_file * m,struct pid_namespace * ns,struct pid * pid,struct task_struct * task)3312 static int proc_token_operations(struct seq_file *m, struct pid_namespace *ns,
3313 struct pid *pid, struct task_struct *task)
3314 {
3315 seq_printf(m, "%#llx %#llx\n", task->token, task->ftoken);
3316 return 0;
3317 }
3318 #endif /* CONFIG_ACCESS_TOKENID */
3319
3320 /*
3321 * Thread groups
3322 */
3323 static const struct file_operations proc_task_operations;
3324 static const struct inode_operations proc_task_inode_operations;
3325
3326 static const struct pid_entry tgid_base_stuff[] = {
3327 DIR("task", S_IRUGO|S_IXUGO, proc_task_inode_operations, proc_task_operations),
3328 DIR("fd", S_IRUSR|S_IXUSR, proc_fd_inode_operations, proc_fd_operations),
3329 DIR("map_files", S_IRUSR|S_IXUSR, proc_map_files_inode_operations, proc_map_files_operations),
3330 DIR("fdinfo", S_IRUSR|S_IXUSR, proc_fdinfo_inode_operations, proc_fdinfo_operations),
3331 DIR("ns", S_IRUSR|S_IXUGO, proc_ns_dir_inode_operations, proc_ns_dir_operations),
3332 #ifdef CONFIG_NET
3333 DIR("net", S_IRUGO|S_IXUGO, proc_net_inode_operations, proc_net_operations),
3334 #endif
3335 REG("environ", S_IRUSR, proc_environ_operations),
3336 REG("auxv", S_IRUSR, proc_auxv_operations),
3337 ONE("status", S_IRUGO, proc_pid_status),
3338 ONE("personality", S_IRUSR, proc_pid_personality),
3339 ONE("limits", S_IRUGO, proc_pid_limits),
3340 #ifdef CONFIG_SCHED_WALT
3341 REG("sched_init_task_load", 00644, proc_pid_sched_init_task_load_operations),
3342 #endif
3343 #ifdef CONFIG_SCHED_DEBUG
3344 REG("sched", S_IRUGO|S_IWUSR, proc_pid_sched_operations),
3345 #endif
3346 #ifdef CONFIG_SCHED_AUTOGROUP
3347 REG("autogroup", S_IRUGO|S_IWUSR, proc_pid_sched_autogroup_operations),
3348 #endif
3349 #ifdef CONFIG_TIME_NS
3350 REG("timens_offsets", S_IRUGO|S_IWUSR, proc_timens_offsets_operations),
3351 #endif
3352 REG("comm", S_IRUGO|S_IWUSR, proc_pid_set_comm_operations),
3353 #ifdef CONFIG_HAVE_ARCH_TRACEHOOK
3354 ONE("syscall", S_IRUSR, proc_pid_syscall),
3355 #endif
3356 REG("cmdline", S_IRUGO, proc_pid_cmdline_ops),
3357 ONE("stat", S_IRUGO, proc_tgid_stat),
3358 ONE("statm", S_IRUGO, proc_pid_statm),
3359 REG("maps", S_IRUGO, proc_pid_maps_operations),
3360 #ifdef CONFIG_NUMA
3361 REG("numa_maps", S_IRUGO, proc_pid_numa_maps_operations),
3362 #endif
3363 REG("mem", S_IRUSR|S_IWUSR, proc_mem_operations),
3364 LNK("cwd", proc_cwd_link),
3365 LNK("root", proc_root_link),
3366 LNK("exe", proc_exe_link),
3367 REG("mounts", S_IRUGO, proc_mounts_operations),
3368 REG("mountinfo", S_IRUGO, proc_mountinfo_operations),
3369 REG("mountstats", S_IRUSR, proc_mountstats_operations),
3370 #ifdef CONFIG_PROC_PAGE_MONITOR
3371 REG("clear_refs", S_IWUSR, proc_clear_refs_operations),
3372 REG("smaps", S_IRUGO, proc_pid_smaps_operations),
3373 REG("smaps_rollup", S_IRUGO, proc_pid_smaps_rollup_operations),
3374 REG("pagemap", S_IRUSR, proc_pagemap_operations),
3375 #endif
3376 #ifdef CONFIG_SECURITY
3377 DIR("attr", S_IRUGO|S_IXUGO, proc_attr_dir_inode_operations, proc_attr_dir_operations),
3378 #endif
3379 #ifdef CONFIG_KALLSYMS
3380 ONE("wchan", S_IRUGO, proc_pid_wchan),
3381 #endif
3382 #ifdef CONFIG_STACKTRACE
3383 ONE("stack", S_IRUSR, proc_pid_stack),
3384 #endif
3385 #ifdef CONFIG_SCHED_INFO
3386 ONE("schedstat", S_IRUGO, proc_pid_schedstat),
3387 #endif
3388 #ifdef CONFIG_LATENCYTOP
3389 REG("latency", S_IRUGO, proc_lstats_operations),
3390 #endif
3391 #ifdef CONFIG_PROC_PID_CPUSET
3392 ONE("cpuset", S_IRUGO, proc_cpuset_show),
3393 #endif
3394 #ifdef CONFIG_CGROUPS
3395 ONE("cgroup", S_IRUGO, proc_cgroup_show),
3396 #endif
3397 #ifdef CONFIG_PROC_CPU_RESCTRL
3398 ONE("cpu_resctrl_groups", S_IRUGO, proc_resctrl_show),
3399 #endif
3400 ONE("oom_score", S_IRUGO, proc_oom_score),
3401 REG("oom_adj", S_IRUGO|S_IWUSR, proc_oom_adj_operations),
3402 REG("oom_score_adj", S_IRUGO|S_IWUSR, proc_oom_score_adj_operations),
3403 #ifdef CONFIG_AUDIT
3404 REG("loginuid", S_IWUSR|S_IRUGO, proc_loginuid_operations),
3405 REG("sessionid", S_IRUGO, proc_sessionid_operations),
3406 #endif
3407 #ifdef CONFIG_FAULT_INJECTION
3408 REG("make-it-fail", S_IRUGO|S_IWUSR, proc_fault_inject_operations),
3409 REG("fail-nth", 0644, proc_fail_nth_operations),
3410 #endif
3411 #ifdef CONFIG_ELF_CORE
3412 REG("coredump_filter", S_IRUGO|S_IWUSR, proc_coredump_filter_operations),
3413 #endif
3414 #ifdef CONFIG_TASK_IO_ACCOUNTING
3415 ONE("io", S_IRUSR, proc_tgid_io_accounting),
3416 #endif
3417 #ifdef CONFIG_USER_NS
3418 REG("uid_map", S_IRUGO|S_IWUSR, proc_uid_map_operations),
3419 REG("gid_map", S_IRUGO|S_IWUSR, proc_gid_map_operations),
3420 REG("projid_map", S_IRUGO|S_IWUSR, proc_projid_map_operations),
3421 REG("setgroups", S_IRUGO|S_IWUSR, proc_setgroups_operations),
3422 #endif
3423 #if defined(CONFIG_CHECKPOINT_RESTORE) && defined(CONFIG_POSIX_TIMERS)
3424 REG("timers", S_IRUGO, proc_timers_operations),
3425 #endif
3426 REG("timerslack_ns", S_IRUGO|S_IWUGO, proc_pid_set_timerslack_ns_operations),
3427 #ifdef CONFIG_LIVEPATCH
3428 ONE("patch_state", S_IRUSR, proc_pid_patch_state),
3429 #endif
3430 #ifdef CONFIG_STACKLEAK_METRICS
3431 ONE("stack_depth", S_IRUGO, proc_stack_depth),
3432 #endif
3433 #ifdef CONFIG_PROC_PID_ARCH_STATUS
3434 ONE("arch_status", S_IRUGO, proc_pid_arch_status),
3435 #endif
3436 #ifdef CONFIG_ACCESS_TOKENID
3437 ONE("tokenid", S_IRUSR, proc_token_operations),
3438 #endif
3439 #ifdef CONFIG_SCHED_RTG_DEBUG
3440 REG("sched_group_id", S_IRUGO|S_IWUGO, proc_pid_sched_group_id_operations),
3441 #endif
3442 };
3443
proc_tgid_base_readdir(struct file * file,struct dir_context * ctx)3444 static int proc_tgid_base_readdir(struct file *file, struct dir_context *ctx)
3445 {
3446 return proc_pident_readdir(file, ctx,
3447 tgid_base_stuff, ARRAY_SIZE(tgid_base_stuff));
3448 }
3449
3450 static const struct file_operations proc_tgid_base_operations = {
3451 .read = generic_read_dir,
3452 .iterate_shared = proc_tgid_base_readdir,
3453 .llseek = generic_file_llseek,
3454 };
3455
tgid_pidfd_to_pid(const struct file * file)3456 struct pid *tgid_pidfd_to_pid(const struct file *file)
3457 {
3458 if (file->f_op != &proc_tgid_base_operations)
3459 return ERR_PTR(-EBADF);
3460
3461 return proc_pid(file_inode(file));
3462 }
3463
proc_tgid_base_lookup(struct inode * dir,struct dentry * dentry,unsigned int flags)3464 static struct dentry *proc_tgid_base_lookup(struct inode *dir, struct dentry *dentry, unsigned int flags)
3465 {
3466 return proc_pident_lookup(dir, dentry,
3467 tgid_base_stuff,
3468 tgid_base_stuff + ARRAY_SIZE(tgid_base_stuff));
3469 }
3470
3471 static const struct inode_operations proc_tgid_base_inode_operations = {
3472 .lookup = proc_tgid_base_lookup,
3473 .getattr = pid_getattr,
3474 .setattr = proc_setattr,
3475 .permission = proc_pid_permission,
3476 };
3477
3478 /**
3479 * proc_flush_pid - Remove dcache entries for @pid from the /proc dcache.
3480 * @pid: pid that should be flushed.
3481 *
3482 * This function walks a list of inodes (that belong to any proc
3483 * filesystem) that are attached to the pid and flushes them from
3484 * the dentry cache.
3485 *
3486 * It is safe and reasonable to cache /proc entries for a task until
3487 * that task exits. After that they just clog up the dcache with
3488 * useless entries, possibly causing useful dcache entries to be
3489 * flushed instead. This routine is provided to flush those useless
3490 * dcache entries when a process is reaped.
3491 *
3492 * NOTE: This routine is just an optimization so it does not guarantee
3493 * that no dcache entries will exist after a process is reaped
3494 * it just makes it very unlikely that any will persist.
3495 */
3496
proc_flush_pid(struct pid * pid)3497 void proc_flush_pid(struct pid *pid)
3498 {
3499 proc_invalidate_siblings_dcache(&pid->inodes, &pid->lock);
3500 }
3501
proc_pid_instantiate(struct dentry * dentry,struct task_struct * task,const void * ptr)3502 static struct dentry *proc_pid_instantiate(struct dentry * dentry,
3503 struct task_struct *task, const void *ptr)
3504 {
3505 struct inode *inode;
3506
3507 inode = proc_pid_make_inode(dentry->d_sb, task, S_IFDIR | S_IRUGO | S_IXUGO);
3508 if (!inode)
3509 return ERR_PTR(-ENOENT);
3510
3511 inode->i_op = &proc_tgid_base_inode_operations;
3512 inode->i_fop = &proc_tgid_base_operations;
3513 inode->i_flags|=S_IMMUTABLE;
3514
3515 set_nlink(inode, nlink_tgid);
3516 pid_update_inode(task, inode);
3517
3518 d_set_d_op(dentry, &pid_dentry_operations);
3519 return d_splice_alias(inode, dentry);
3520 }
3521
proc_pid_lookup(struct dentry * dentry,unsigned int flags)3522 struct dentry *proc_pid_lookup(struct dentry *dentry, unsigned int flags)
3523 {
3524 struct task_struct *task;
3525 unsigned tgid;
3526 struct proc_fs_info *fs_info;
3527 struct pid_namespace *ns;
3528 struct dentry *result = ERR_PTR(-ENOENT);
3529
3530 tgid = name_to_int(&dentry->d_name);
3531 if (tgid == ~0U)
3532 goto out;
3533
3534 fs_info = proc_sb_info(dentry->d_sb);
3535 ns = fs_info->pid_ns;
3536 rcu_read_lock();
3537 task = find_task_by_pid_ns(tgid, ns);
3538 if (task)
3539 get_task_struct(task);
3540 rcu_read_unlock();
3541 if (!task)
3542 goto out;
3543
3544 /* Limit procfs to only ptraceable tasks */
3545 if (fs_info->hide_pid == HIDEPID_NOT_PTRACEABLE) {
3546 if (!has_pid_permissions(fs_info, task, HIDEPID_NO_ACCESS))
3547 goto out_put_task;
3548 }
3549
3550 result = proc_pid_instantiate(dentry, task, NULL);
3551 out_put_task:
3552 put_task_struct(task);
3553 out:
3554 return result;
3555 }
3556
3557 /*
3558 * Find the first task with tgid >= tgid
3559 *
3560 */
3561 struct tgid_iter {
3562 unsigned int tgid;
3563 struct task_struct *task;
3564 };
next_tgid(struct pid_namespace * ns,struct tgid_iter iter)3565 static struct tgid_iter next_tgid(struct pid_namespace *ns, struct tgid_iter iter)
3566 {
3567 struct pid *pid;
3568
3569 if (iter.task)
3570 put_task_struct(iter.task);
3571 rcu_read_lock();
3572 retry:
3573 iter.task = NULL;
3574 pid = find_ge_pid(iter.tgid, ns);
3575 if (pid) {
3576 iter.tgid = pid_nr_ns(pid, ns);
3577 iter.task = pid_task(pid, PIDTYPE_TGID);
3578 if (!iter.task) {
3579 iter.tgid += 1;
3580 goto retry;
3581 }
3582 get_task_struct(iter.task);
3583 }
3584 rcu_read_unlock();
3585 return iter;
3586 }
3587
3588 #define TGID_OFFSET (FIRST_PROCESS_ENTRY + 2)
3589
3590 /* for the /proc/ directory itself, after non-process stuff has been done */
proc_pid_readdir(struct file * file,struct dir_context * ctx)3591 int proc_pid_readdir(struct file *file, struct dir_context *ctx)
3592 {
3593 struct tgid_iter iter;
3594 struct proc_fs_info *fs_info = proc_sb_info(file_inode(file)->i_sb);
3595 struct pid_namespace *ns = proc_pid_ns(file_inode(file)->i_sb);
3596 loff_t pos = ctx->pos;
3597
3598 if (pos >= PID_MAX_LIMIT + TGID_OFFSET)
3599 return 0;
3600
3601 if (pos == TGID_OFFSET - 2) {
3602 struct inode *inode = d_inode(fs_info->proc_self);
3603 if (!dir_emit(ctx, "self", 4, inode->i_ino, DT_LNK))
3604 return 0;
3605 ctx->pos = pos = pos + 1;
3606 }
3607 if (pos == TGID_OFFSET - 1) {
3608 struct inode *inode = d_inode(fs_info->proc_thread_self);
3609 if (!dir_emit(ctx, "thread-self", 11, inode->i_ino, DT_LNK))
3610 return 0;
3611 ctx->pos = pos = pos + 1;
3612 }
3613 iter.tgid = pos - TGID_OFFSET;
3614 iter.task = NULL;
3615 for (iter = next_tgid(ns, iter);
3616 iter.task;
3617 iter.tgid += 1, iter = next_tgid(ns, iter)) {
3618 char name[10 + 1];
3619 unsigned int len;
3620
3621 cond_resched();
3622 if (!has_pid_permissions(fs_info, iter.task, HIDEPID_INVISIBLE))
3623 continue;
3624
3625 len = snprintf(name, sizeof(name), "%u", iter.tgid);
3626 ctx->pos = iter.tgid + TGID_OFFSET;
3627 if (!proc_fill_cache(file, ctx, name, len,
3628 proc_pid_instantiate, iter.task, NULL)) {
3629 put_task_struct(iter.task);
3630 return 0;
3631 }
3632 }
3633 ctx->pos = PID_MAX_LIMIT + TGID_OFFSET;
3634 return 0;
3635 }
3636
3637 /*
3638 * proc_tid_comm_permission is a special permission function exclusively
3639 * used for the node /proc/<pid>/task/<tid>/comm.
3640 * It bypasses generic permission checks in the case where a task of the same
3641 * task group attempts to access the node.
3642 * The rationale behind this is that glibc and bionic access this node for
3643 * cross thread naming (pthread_set/getname_np(!self)). However, if
3644 * PR_SET_DUMPABLE gets set to 0 this node among others becomes uid=0 gid=0,
3645 * which locks out the cross thread naming implementation.
3646 * This function makes sure that the node is always accessible for members of
3647 * same thread group.
3648 */
proc_tid_comm_permission(struct inode * inode,int mask)3649 static int proc_tid_comm_permission(struct inode *inode, int mask)
3650 {
3651 bool is_same_tgroup;
3652 struct task_struct *task;
3653
3654 task = get_proc_task(inode);
3655 if (!task)
3656 return -ESRCH;
3657 is_same_tgroup = same_thread_group(current, task);
3658 put_task_struct(task);
3659
3660 if (likely(is_same_tgroup && !(mask & MAY_EXEC))) {
3661 /* This file (/proc/<pid>/task/<tid>/comm) can always be
3662 * read or written by the members of the corresponding
3663 * thread group.
3664 */
3665 return 0;
3666 }
3667
3668 return generic_permission(inode, mask);
3669 }
3670
3671 static const struct inode_operations proc_tid_comm_inode_operations = {
3672 .permission = proc_tid_comm_permission,
3673 };
3674
3675 /*
3676 * Tasks
3677 */
3678 static const struct pid_entry tid_base_stuff[] = {
3679 DIR("fd", S_IRUSR|S_IXUSR, proc_fd_inode_operations, proc_fd_operations),
3680 DIR("fdinfo", S_IRUSR|S_IXUSR, proc_fdinfo_inode_operations, proc_fdinfo_operations),
3681 DIR("ns", S_IRUSR|S_IXUGO, proc_ns_dir_inode_operations, proc_ns_dir_operations),
3682 #ifdef CONFIG_NET
3683 DIR("net", S_IRUGO|S_IXUGO, proc_net_inode_operations, proc_net_operations),
3684 #endif
3685 REG("environ", S_IRUSR, proc_environ_operations),
3686 REG("auxv", S_IRUSR, proc_auxv_operations),
3687 ONE("status", S_IRUGO, proc_pid_status),
3688 ONE("personality", S_IRUSR, proc_pid_personality),
3689 ONE("limits", S_IRUGO, proc_pid_limits),
3690 #ifdef CONFIG_SCHED_DEBUG
3691 REG("sched", S_IRUGO|S_IWUSR, proc_pid_sched_operations),
3692 #endif
3693 NOD("comm", S_IFREG|S_IRUGO|S_IWUSR,
3694 &proc_tid_comm_inode_operations,
3695 &proc_pid_set_comm_operations, {}),
3696 #ifdef CONFIG_HAVE_ARCH_TRACEHOOK
3697 ONE("syscall", S_IRUSR, proc_pid_syscall),
3698 #endif
3699 REG("cmdline", S_IRUGO, proc_pid_cmdline_ops),
3700 ONE("stat", S_IRUGO, proc_tid_stat),
3701 ONE("statm", S_IRUGO, proc_pid_statm),
3702 REG("maps", S_IRUGO, proc_pid_maps_operations),
3703 #ifdef CONFIG_PROC_CHILDREN
3704 REG("children", S_IRUGO, proc_tid_children_operations),
3705 #endif
3706 #ifdef CONFIG_NUMA
3707 REG("numa_maps", S_IRUGO, proc_pid_numa_maps_operations),
3708 #endif
3709 REG("mem", S_IRUSR|S_IWUSR, proc_mem_operations),
3710 LNK("cwd", proc_cwd_link),
3711 LNK("root", proc_root_link),
3712 LNK("exe", proc_exe_link),
3713 REG("mounts", S_IRUGO, proc_mounts_operations),
3714 REG("mountinfo", S_IRUGO, proc_mountinfo_operations),
3715 #ifdef CONFIG_PROC_PAGE_MONITOR
3716 REG("clear_refs", S_IWUSR, proc_clear_refs_operations),
3717 REG("smaps", S_IRUGO, proc_pid_smaps_operations),
3718 REG("smaps_rollup", S_IRUGO, proc_pid_smaps_rollup_operations),
3719 REG("pagemap", S_IRUSR, proc_pagemap_operations),
3720 #endif
3721 #ifdef CONFIG_SECURITY
3722 DIR("attr", S_IRUGO|S_IXUGO, proc_attr_dir_inode_operations, proc_attr_dir_operations),
3723 #endif
3724 #ifdef CONFIG_KALLSYMS
3725 ONE("wchan", S_IRUGO, proc_pid_wchan),
3726 #endif
3727 #ifdef CONFIG_STACKTRACE
3728 ONE("stack", S_IRUSR, proc_pid_stack),
3729 #endif
3730 #ifdef CONFIG_SCHED_INFO
3731 ONE("schedstat", S_IRUGO, proc_pid_schedstat),
3732 #endif
3733 #ifdef CONFIG_LATENCYTOP
3734 REG("latency", S_IRUGO, proc_lstats_operations),
3735 #endif
3736 #ifdef CONFIG_PROC_PID_CPUSET
3737 ONE("cpuset", S_IRUGO, proc_cpuset_show),
3738 #endif
3739 #ifdef CONFIG_CGROUPS
3740 ONE("cgroup", S_IRUGO, proc_cgroup_show),
3741 #endif
3742 #ifdef CONFIG_PROC_CPU_RESCTRL
3743 ONE("cpu_resctrl_groups", S_IRUGO, proc_resctrl_show),
3744 #endif
3745 ONE("oom_score", S_IRUGO, proc_oom_score),
3746 REG("oom_adj", S_IRUGO|S_IWUSR, proc_oom_adj_operations),
3747 REG("oom_score_adj", S_IRUGO|S_IWUSR, proc_oom_score_adj_operations),
3748 #ifdef CONFIG_AUDIT
3749 REG("loginuid", S_IWUSR|S_IRUGO, proc_loginuid_operations),
3750 REG("sessionid", S_IRUGO, proc_sessionid_operations),
3751 #endif
3752 #ifdef CONFIG_FAULT_INJECTION
3753 REG("make-it-fail", S_IRUGO|S_IWUSR, proc_fault_inject_operations),
3754 REG("fail-nth", 0644, proc_fail_nth_operations),
3755 #endif
3756 #ifdef CONFIG_TASK_IO_ACCOUNTING
3757 ONE("io", S_IRUSR, proc_tid_io_accounting),
3758 #endif
3759 #ifdef CONFIG_USER_NS
3760 REG("uid_map", S_IRUGO|S_IWUSR, proc_uid_map_operations),
3761 REG("gid_map", S_IRUGO|S_IWUSR, proc_gid_map_operations),
3762 REG("projid_map", S_IRUGO|S_IWUSR, proc_projid_map_operations),
3763 REG("setgroups", S_IRUGO|S_IWUSR, proc_setgroups_operations),
3764 #endif
3765 #ifdef CONFIG_LIVEPATCH
3766 ONE("patch_state", S_IRUSR, proc_pid_patch_state),
3767 #endif
3768 #ifdef CONFIG_PROC_PID_ARCH_STATUS
3769 ONE("arch_status", S_IRUGO, proc_pid_arch_status),
3770 #endif
3771 #ifdef CONFIG_ACCESS_TOKENID
3772 ONE("tokenid", S_IRUSR, proc_token_operations),
3773 #endif
3774 #ifdef CONFIG_SCHED_RTG_DEBUG
3775 REG("sched_group_id", S_IRUGO|S_IWUGO, proc_pid_sched_group_id_operations),
3776 #endif
3777 };
3778
proc_tid_base_readdir(struct file * file,struct dir_context * ctx)3779 static int proc_tid_base_readdir(struct file *file, struct dir_context *ctx)
3780 {
3781 return proc_pident_readdir(file, ctx,
3782 tid_base_stuff, ARRAY_SIZE(tid_base_stuff));
3783 }
3784
proc_tid_base_lookup(struct inode * dir,struct dentry * dentry,unsigned int flags)3785 static struct dentry *proc_tid_base_lookup(struct inode *dir, struct dentry *dentry, unsigned int flags)
3786 {
3787 return proc_pident_lookup(dir, dentry,
3788 tid_base_stuff,
3789 tid_base_stuff + ARRAY_SIZE(tid_base_stuff));
3790 }
3791
3792 static const struct file_operations proc_tid_base_operations = {
3793 .read = generic_read_dir,
3794 .iterate_shared = proc_tid_base_readdir,
3795 .llseek = generic_file_llseek,
3796 };
3797
3798 static const struct inode_operations proc_tid_base_inode_operations = {
3799 .lookup = proc_tid_base_lookup,
3800 .getattr = pid_getattr,
3801 .setattr = proc_setattr,
3802 };
3803
proc_task_instantiate(struct dentry * dentry,struct task_struct * task,const void * ptr)3804 static struct dentry *proc_task_instantiate(struct dentry *dentry,
3805 struct task_struct *task, const void *ptr)
3806 {
3807 struct inode *inode;
3808 inode = proc_pid_make_inode(dentry->d_sb, task, S_IFDIR | S_IRUGO | S_IXUGO);
3809 if (!inode)
3810 return ERR_PTR(-ENOENT);
3811
3812 inode->i_op = &proc_tid_base_inode_operations;
3813 inode->i_fop = &proc_tid_base_operations;
3814 inode->i_flags |= S_IMMUTABLE;
3815
3816 set_nlink(inode, nlink_tid);
3817 pid_update_inode(task, inode);
3818
3819 d_set_d_op(dentry, &pid_dentry_operations);
3820 return d_splice_alias(inode, dentry);
3821 }
3822
proc_task_lookup(struct inode * dir,struct dentry * dentry,unsigned int flags)3823 static struct dentry *proc_task_lookup(struct inode *dir, struct dentry * dentry, unsigned int flags)
3824 {
3825 struct task_struct *task;
3826 struct task_struct *leader = get_proc_task(dir);
3827 unsigned tid;
3828 struct proc_fs_info *fs_info;
3829 struct pid_namespace *ns;
3830 struct dentry *result = ERR_PTR(-ENOENT);
3831
3832 if (!leader)
3833 goto out_no_task;
3834
3835 tid = name_to_int(&dentry->d_name);
3836 if (tid == ~0U)
3837 goto out;
3838
3839 fs_info = proc_sb_info(dentry->d_sb);
3840 ns = fs_info->pid_ns;
3841 rcu_read_lock();
3842 task = find_task_by_pid_ns(tid, ns);
3843 if (task)
3844 get_task_struct(task);
3845 rcu_read_unlock();
3846 if (!task)
3847 goto out;
3848 if (!same_thread_group(leader, task))
3849 goto out_drop_task;
3850
3851 result = proc_task_instantiate(dentry, task, NULL);
3852 out_drop_task:
3853 put_task_struct(task);
3854 out:
3855 put_task_struct(leader);
3856 out_no_task:
3857 return result;
3858 }
3859
3860 /*
3861 * Find the first tid of a thread group to return to user space.
3862 *
3863 * Usually this is just the thread group leader, but if the users
3864 * buffer was too small or there was a seek into the middle of the
3865 * directory we have more work todo.
3866 *
3867 * In the case of a short read we start with find_task_by_pid.
3868 *
3869 * In the case of a seek we start with the leader and walk nr
3870 * threads past it.
3871 */
first_tid(struct pid * pid,int tid,loff_t f_pos,struct pid_namespace * ns)3872 static struct task_struct *first_tid(struct pid *pid, int tid, loff_t f_pos,
3873 struct pid_namespace *ns)
3874 {
3875 struct task_struct *pos, *task;
3876 unsigned long nr = f_pos;
3877
3878 if (nr != f_pos) /* 32bit overflow? */
3879 return NULL;
3880
3881 rcu_read_lock();
3882 task = pid_task(pid, PIDTYPE_PID);
3883 if (!task)
3884 goto fail;
3885
3886 /* Attempt to start with the tid of a thread */
3887 if (tid && nr) {
3888 pos = find_task_by_pid_ns(tid, ns);
3889 if (pos && same_thread_group(pos, task))
3890 goto found;
3891 }
3892
3893 /* If nr exceeds the number of threads there is nothing todo */
3894 if (nr >= get_nr_threads(task))
3895 goto fail;
3896
3897 /* If we haven't found our starting place yet start
3898 * with the leader and walk nr threads forward.
3899 */
3900 pos = task = task->group_leader;
3901 do {
3902 if (!nr--)
3903 goto found;
3904 } while_each_thread(task, pos);
3905 fail:
3906 pos = NULL;
3907 goto out;
3908 found:
3909 get_task_struct(pos);
3910 out:
3911 rcu_read_unlock();
3912 return pos;
3913 }
3914
3915 /*
3916 * Find the next thread in the thread list.
3917 * Return NULL if there is an error or no next thread.
3918 *
3919 * The reference to the input task_struct is released.
3920 */
next_tid(struct task_struct * start)3921 static struct task_struct *next_tid(struct task_struct *start)
3922 {
3923 struct task_struct *pos = NULL;
3924 rcu_read_lock();
3925 if (pid_alive(start)) {
3926 pos = next_thread(start);
3927 if (thread_group_leader(pos))
3928 pos = NULL;
3929 else
3930 get_task_struct(pos);
3931 }
3932 rcu_read_unlock();
3933 put_task_struct(start);
3934 return pos;
3935 }
3936
3937 /* for the /proc/TGID/task/ directories */
proc_task_readdir(struct file * file,struct dir_context * ctx)3938 static int proc_task_readdir(struct file *file, struct dir_context *ctx)
3939 {
3940 struct inode *inode = file_inode(file);
3941 struct task_struct *task;
3942 struct pid_namespace *ns;
3943 int tid;
3944
3945 if (proc_inode_is_dead(inode))
3946 return -ENOENT;
3947
3948 if (!dir_emit_dots(file, ctx))
3949 return 0;
3950
3951 /* f_version caches the tgid value that the last readdir call couldn't
3952 * return. lseek aka telldir automagically resets f_version to 0.
3953 */
3954 ns = proc_pid_ns(inode->i_sb);
3955 tid = (int)file->f_version;
3956 file->f_version = 0;
3957 for (task = first_tid(proc_pid(inode), tid, ctx->pos - 2, ns);
3958 task;
3959 task = next_tid(task), ctx->pos++) {
3960 char name[10 + 1];
3961 unsigned int len;
3962 tid = task_pid_nr_ns(task, ns);
3963 len = snprintf(name, sizeof(name), "%u", tid);
3964 if (!proc_fill_cache(file, ctx, name, len,
3965 proc_task_instantiate, task, NULL)) {
3966 /* returning this tgid failed, save it as the first
3967 * pid for the next readir call */
3968 file->f_version = (u64)tid;
3969 put_task_struct(task);
3970 break;
3971 }
3972 }
3973
3974 return 0;
3975 }
3976
proc_task_getattr(const struct path * path,struct kstat * stat,u32 request_mask,unsigned int query_flags)3977 static int proc_task_getattr(const struct path *path, struct kstat *stat,
3978 u32 request_mask, unsigned int query_flags)
3979 {
3980 struct inode *inode = d_inode(path->dentry);
3981 struct task_struct *p = get_proc_task(inode);
3982 generic_fillattr(inode, stat);
3983
3984 if (p) {
3985 stat->nlink += get_nr_threads(p);
3986 put_task_struct(p);
3987 }
3988
3989 return 0;
3990 }
3991
3992 static const struct inode_operations proc_task_inode_operations = {
3993 .lookup = proc_task_lookup,
3994 .getattr = proc_task_getattr,
3995 .setattr = proc_setattr,
3996 .permission = proc_pid_permission,
3997 };
3998
3999 static const struct file_operations proc_task_operations = {
4000 .read = generic_read_dir,
4001 .iterate_shared = proc_task_readdir,
4002 .llseek = generic_file_llseek,
4003 };
4004
set_proc_pid_nlink(void)4005 void __init set_proc_pid_nlink(void)
4006 {
4007 nlink_tid = pid_entry_nlink(tid_base_stuff, ARRAY_SIZE(tid_base_stuff));
4008 nlink_tgid = pid_entry_nlink(tgid_base_stuff, ARRAY_SIZE(tgid_base_stuff));
4009 }
4010