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