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