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