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