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