blob: 3b5e6aa2a326f63c506332befe2f494ea7225c86 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * linux/fs/proc/base.c
3 *
4 * Copyright (C) 1991, 1992 Linus Torvalds
5 *
6 * proc base directory handling functions
7 *
8 * 1999, Al Viro. Rewritten. Now it covers the whole per-process part.
9 * Instead of using magical inumbers to determine the kind of object
10 * we allocate and fill in-core inodes upon lookup. They don't even
11 * go into icache. We cache the reference to task_struct upon lookup too.
12 * Eventually it should become a filesystem in its own. We don't use the
13 * rest of procfs anymore.
Mauricio Line070ad42005-09-03 15:55:10 -070014 *
15 *
16 * Changelog:
17 * 17-Jan-2005
18 * Allan Bezerra
19 * Bruna Moreira <bruna.moreira@indt.org.br>
20 * Edjard Mota <edjard.mota@indt.org.br>
21 * Ilias Biris <ilias.biris@indt.org.br>
22 * Mauricio Lin <mauricio.lin@indt.org.br>
23 *
24 * Embedded Linux Lab - 10LE Instituto Nokia de Tecnologia - INdT
25 *
26 * A new process specific entry (smaps) included in /proc. It shows the
27 * size of rss for each memory area. The maps entry lacks information
28 * about physical memory size (rss) for each mapped file, i.e.,
29 * rss information for executables and library files.
30 * This additional information is useful for any tools that need to know
31 * about physical memory consumption for a process specific library.
32 *
33 * Changelog:
34 * 21-Feb-2005
35 * Embedded Linux Lab - 10LE Instituto Nokia de Tecnologia - INdT
36 * Pud inclusion in the page table walking.
37 *
38 * ChangeLog:
39 * 10-Mar-2005
40 * 10LE Instituto Nokia de Tecnologia - INdT:
41 * A better way to walks through the page table as suggested by Hugh Dickins.
42 *
43 * Simo Piiroinen <simo.piiroinen@nokia.com>:
44 * Smaps information related to shared, private, clean and dirty pages.
45 *
46 * Paul Mundt <paul.mundt@nokia.com>:
47 * Overall revision about smaps.
Linus Torvalds1da177e2005-04-16 15:20:36 -070048 */
49
Linus Torvalds7c0f6ba2016-12-24 11:46:01 -080050#include <linux/uaccess.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070051
Linus Torvalds1da177e2005-04-16 15:20:36 -070052#include <linux/errno.h>
53#include <linux/time.h>
54#include <linux/proc_fs.h>
55#include <linux/stat.h>
Andrea Righi59954772008-07-27 17:29:15 +020056#include <linux/task_io_accounting_ops.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070057#include <linux/init.h>
Randy Dunlap16f7e0f2006-01-11 12:17:46 -080058#include <linux/capability.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070059#include <linux/file.h>
Al Viro9f3acc32008-04-24 07:44:08 -040060#include <linux/fdtable.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070061#include <linux/string.h>
62#include <linux/seq_file.h>
63#include <linux/namei.h>
Kirill Korotaev6b3286e2006-12-08 02:37:56 -080064#include <linux/mnt_namespace.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070065#include <linux/mm.h>
David Rientjesa63d83f2010-08-09 17:19:46 -070066#include <linux/swap.h>
Dipankar Sarmab8359962005-09-09 13:04:14 -070067#include <linux/rcupdate.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070068#include <linux/kallsyms.h>
Ken Chen2ec220e2008-11-10 11:26:08 +030069#include <linux/stacktrace.h>
Neil Hormand85f50d2007-10-18 23:40:37 -070070#include <linux/resource.h>
Kees Cook5096add2007-05-08 00:26:04 -070071#include <linux/module.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070072#include <linux/mount.h>
73#include <linux/security.h>
74#include <linux/ptrace.h>
Roland McGrath0d094ef2008-07-25 19:45:49 -070075#include <linux/tracehook.h>
Andrew Morton87ebdc02013-02-27 17:03:16 -080076#include <linux/printk.h>
Paul Menagea4243162007-10-18 23:39:35 -070077#include <linux/cgroup.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070078#include <linux/cpuset.h>
79#include <linux/audit.h>
Al Viro5addc5d2005-11-07 17:15:49 -050080#include <linux/poll.h>
Serge E. Hallyn1651e142006-10-02 02:18:08 -070081#include <linux/nsproxy.h>
Alexey Dobriyan8ac773b2006-10-19 23:28:32 -070082#include <linux/oom.h>
Kawai, Hidehiro3cb4a0b2007-07-19 01:48:28 -070083#include <linux/elf.h>
Pavel Emelyanov60347f62007-10-18 23:40:03 -070084#include <linux/pid_namespace.h>
Eric W. Biederman22d917d2011-11-17 00:11:58 -080085#include <linux/user_namespace.h>
Al Viro5ad4e532009-03-29 19:50:06 -040086#include <linux/fs_struct.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090087#include <linux/slab.h>
Pavel Emelyanov640708a2012-01-10 15:11:23 -080088#include <linux/flex_array.h>
Pavel Emelyanov48f6a7a2013-03-11 13:12:45 +040089#include <linux/posix-timers.h>
Chris Metcalff133ecc2011-05-26 12:40:09 -040090#ifdef CONFIG_HARDWALL
91#include <asm/hardwall.h>
92#endif
KAMEZAWA Hiroyuki43d2b112012-01-10 15:08:09 -080093#include <trace/events/oom.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070094#include "internal.h"
Cyrill Gorcunovfaf60af2012-08-23 14:43:24 +040095#include "fd.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070096
Eric W. Biederman0f2fe202006-06-26 00:25:46 -070097/* NOTE:
98 * Implementing inode permission operations in /proc is almost
99 * certainly an error. Permission checks need to happen during
100 * each system call not at open time. The reason is that most of
101 * what we wish to check for permissions in /proc varies at runtime.
102 *
103 * The classic example of a problem is opening file descriptors
104 * in /proc for a task before it execs a suid executable.
105 */
106
Alexey Dobriyan1270dd82016-12-12 16:45:32 -0800107static u8 nlink_tid;
108static u8 nlink_tgid;
109
Linus Torvalds1da177e2005-04-16 15:20:36 -0700110struct pid_entry {
Alexey Dobriyancedbcca2014-08-08 14:21:33 -0700111 const char *name;
Alexey Dobriyan623f5942016-12-12 16:45:08 -0800112 unsigned int len;
Al Virod161a132011-07-24 03:36:29 -0400113 umode_t mode;
Arjan van de Venc5ef1c42007-02-12 00:55:40 -0800114 const struct inode_operations *iop;
Arjan van de Ven00977a52007-02-12 00:55:34 -0800115 const struct file_operations *fop;
Eric W. Biederman20cdc892006-10-02 02:17:07 -0700116 union proc_op op;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700117};
118
Eric W. Biederman61a28782006-10-02 02:18:49 -0700119#define NOD(NAME, MODE, IOP, FOP, OP) { \
Eric W. Biederman20cdc892006-10-02 02:17:07 -0700120 .name = (NAME), \
Eric Dumazetc5141e62007-05-08 00:26:15 -0700121 .len = sizeof(NAME) - 1, \
Eric W. Biederman20cdc892006-10-02 02:17:07 -0700122 .mode = MODE, \
123 .iop = IOP, \
124 .fop = FOP, \
125 .op = OP, \
126}
127
Alexey Dobriyan631f9c12008-11-10 01:32:52 +0300128#define DIR(NAME, MODE, iops, fops) \
129 NOD(NAME, (S_IFDIR|(MODE)), &iops, &fops, {} )
130#define LNK(NAME, get_link) \
Eric W. Biederman61a28782006-10-02 02:18:49 -0700131 NOD(NAME, (S_IFLNK|S_IRWXUGO), \
Eric W. Biederman20cdc892006-10-02 02:17:07 -0700132 &proc_pid_link_inode_operations, NULL, \
Alexey Dobriyan631f9c12008-11-10 01:32:52 +0300133 { .proc_get_link = get_link } )
134#define REG(NAME, MODE, fops) \
135 NOD(NAME, (S_IFREG|(MODE)), NULL, &fops, {})
Alexey Dobriyan631f9c12008-11-10 01:32:52 +0300136#define ONE(NAME, MODE, show) \
Eric W. Biedermanbe614082008-02-08 04:18:30 -0800137 NOD(NAME, (S_IFREG|(MODE)), \
138 NULL, &proc_single_file_operations, \
Alexey Dobriyan631f9c12008-11-10 01:32:52 +0300139 { .proc_show = show } )
Linus Torvalds1da177e2005-04-16 15:20:36 -0700140
Vegard Nossumaed54172008-06-05 22:46:53 -0700141/*
142 * Count the number of hardlinks for the pid_entry table, excluding the .
143 * and .. links.
144 */
Alexey Dobriyan1270dd82016-12-12 16:45:32 -0800145static unsigned int __init pid_entry_nlink(const struct pid_entry *entries,
Vegard Nossumaed54172008-06-05 22:46:53 -0700146 unsigned int n)
147{
148 unsigned int i;
149 unsigned int count;
150
Alexey Dobriyan1270dd82016-12-12 16:45:32 -0800151 count = 2;
Vegard Nossumaed54172008-06-05 22:46:53 -0700152 for (i = 0; i < n; ++i) {
153 if (S_ISDIR(entries[i].mode))
154 ++count;
155 }
156
157 return count;
158}
159
Miklos Szeredif7ad3c62010-08-10 11:41:36 +0200160static int get_task_root(struct task_struct *task, struct path *root)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700161{
Hugh Dickins7c2c7d92009-03-28 23:21:27 +0000162 int result = -ENOENT;
163
Miklos Szeredi0494f6e2005-09-06 15:18:22 -0700164 task_lock(task);
Miklos Szeredif7ad3c62010-08-10 11:41:36 +0200165 if (task->fs) {
166 get_fs_root(task->fs, root);
Hugh Dickins7c2c7d92009-03-28 23:21:27 +0000167 result = 0;
168 }
Miklos Szeredi0494f6e2005-09-06 15:18:22 -0700169 task_unlock(task);
Hugh Dickins7c2c7d92009-03-28 23:21:27 +0000170 return result;
Miklos Szeredi0494f6e2005-09-06 15:18:22 -0700171}
172
Cyrill Gorcunov7773fbc2012-01-10 15:11:20 -0800173static int proc_cwd_link(struct dentry *dentry, struct path *path)
Miklos Szeredi0494f6e2005-09-06 15:18:22 -0700174{
David Howells2b0143b2015-03-17 22:25:59 +0000175 struct task_struct *task = get_proc_task(d_inode(dentry));
Miklos Szeredi0494f6e2005-09-06 15:18:22 -0700176 int result = -ENOENT;
Eric W. Biederman99f89552006-06-26 00:25:55 -0700177
178 if (task) {
Miklos Szeredif7ad3c62010-08-10 11:41:36 +0200179 task_lock(task);
180 if (task->fs) {
181 get_fs_pwd(task->fs, path);
182 result = 0;
183 }
184 task_unlock(task);
Eric W. Biederman99f89552006-06-26 00:25:55 -0700185 put_task_struct(task);
186 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700187 return result;
188}
189
Cyrill Gorcunov7773fbc2012-01-10 15:11:20 -0800190static int proc_root_link(struct dentry *dentry, struct path *path)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700191{
David Howells2b0143b2015-03-17 22:25:59 +0000192 struct task_struct *task = get_proc_task(d_inode(dentry));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700193 int result = -ENOENT;
Eric W. Biederman99f89552006-06-26 00:25:55 -0700194
195 if (task) {
Miklos Szeredif7ad3c62010-08-10 11:41:36 +0200196 result = get_task_root(task, path);
Eric W. Biederman99f89552006-06-26 00:25:55 -0700197 put_task_struct(task);
198 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700199 return result;
200}
201
Alexey Dobriyanc2c0bb42015-06-25 15:00:54 -0700202static ssize_t proc_pid_cmdline_read(struct file *file, char __user *buf,
203 size_t _count, loff_t *pos)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700204{
Alexey Dobriyanc2c0bb42015-06-25 15:00:54 -0700205 struct task_struct *tsk;
206 struct mm_struct *mm;
207 char *page;
208 unsigned long count = _count;
209 unsigned long arg_start, arg_end, env_start, env_end;
210 unsigned long len1, len2, len;
211 unsigned long p;
212 char c;
213 ssize_t rv;
214
215 BUG_ON(*pos < 0);
216
217 tsk = get_proc_task(file_inode(file));
218 if (!tsk)
219 return -ESRCH;
220 mm = get_task_mm(tsk);
221 put_task_struct(tsk);
222 if (!mm)
223 return 0;
224 /* Check if process spawned far enough to have cmdline. */
225 if (!mm->env_end) {
226 rv = 0;
227 goto out_mmput;
228 }
229
230 page = (char *)__get_free_page(GFP_TEMPORARY);
231 if (!page) {
232 rv = -ENOMEM;
233 goto out_mmput;
234 }
235
236 down_read(&mm->mmap_sem);
237 arg_start = mm->arg_start;
238 arg_end = mm->arg_end;
239 env_start = mm->env_start;
240 env_end = mm->env_end;
241 up_read(&mm->mmap_sem);
242
243 BUG_ON(arg_start > arg_end);
244 BUG_ON(env_start > env_end);
245
246 len1 = arg_end - arg_start;
247 len2 = env_end - env_start;
248
Alexey Dobriyan3581d452015-07-17 16:24:09 -0700249 /* Empty ARGV. */
250 if (len1 == 0) {
251 rv = 0;
252 goto out_free_page;
253 }
Alexey Dobriyan2ca66ff2014-08-08 14:21:41 -0700254 /*
Alexey Dobriyanc2c0bb42015-06-25 15:00:54 -0700255 * Inherently racy -- command line shares address space
256 * with code and data.
Alexey Dobriyan2ca66ff2014-08-08 14:21:41 -0700257 */
Linus Torvalds272ddc82016-10-24 19:00:44 -0700258 rv = access_remote_vm(mm, arg_end - 1, &c, 1, 0);
Alexey Dobriyanc2c0bb42015-06-25 15:00:54 -0700259 if (rv <= 0)
260 goto out_free_page;
261
262 rv = 0;
263
264 if (c == '\0') {
265 /* Command line (set of strings) occupies whole ARGV. */
266 if (len1 <= *pos)
267 goto out_free_page;
268
269 p = arg_start + *pos;
270 len = len1 - *pos;
271 while (count > 0 && len > 0) {
272 unsigned int _count;
273 int nr_read;
274
275 _count = min3(count, len, PAGE_SIZE);
Linus Torvalds272ddc82016-10-24 19:00:44 -0700276 nr_read = access_remote_vm(mm, p, page, _count, 0);
Alexey Dobriyanc2c0bb42015-06-25 15:00:54 -0700277 if (nr_read < 0)
278 rv = nr_read;
279 if (nr_read <= 0)
280 goto out_free_page;
281
282 if (copy_to_user(buf, page, nr_read)) {
283 rv = -EFAULT;
284 goto out_free_page;
285 }
286
287 p += nr_read;
288 len -= nr_read;
289 buf += nr_read;
290 count -= nr_read;
291 rv += nr_read;
292 }
293 } else {
294 /*
Alexey Dobriyanc2c0bb42015-06-25 15:00:54 -0700295 * Command line (1 string) occupies ARGV and
296 * extends into ENVP.
297 */
Alexey Dobriyana0a07b82017-02-24 15:00:20 -0800298 struct {
299 unsigned long p;
300 unsigned long len;
301 } cmdline[2] = {
302 { .p = arg_start, .len = len1 },
303 { .p = env_start, .len = len2 },
304 };
305 loff_t pos1 = *pos;
306 unsigned int i;
307
308 i = 0;
309 while (i < 2 && pos1 >= cmdline[i].len) {
310 pos1 -= cmdline[i].len;
311 i++;
Alexey Dobriyanc2c0bb42015-06-25 15:00:54 -0700312 }
Alexey Dobriyana0a07b82017-02-24 15:00:20 -0800313 while (i < 2) {
314 p = cmdline[i].p + pos1;
315 len = cmdline[i].len - pos1;
316 while (count > 0 && len > 0) {
317 unsigned int _count, l;
318 int nr_read;
319 bool final;
Alexey Dobriyanc2c0bb42015-06-25 15:00:54 -0700320
Alexey Dobriyana0a07b82017-02-24 15:00:20 -0800321 _count = min3(count, len, PAGE_SIZE);
322 nr_read = access_remote_vm(mm, p, page, _count, 0);
323 if (nr_read < 0)
324 rv = nr_read;
325 if (nr_read <= 0)
326 goto out_free_page;
Alexey Dobriyanc2c0bb42015-06-25 15:00:54 -0700327
Alexey Dobriyana0a07b82017-02-24 15:00:20 -0800328 /*
329 * Command line can be shorter than whole ARGV
330 * even if last "marker" byte says it is not.
331 */
332 final = false;
333 l = strnlen(page, nr_read);
334 if (l < nr_read) {
335 nr_read = l;
336 final = true;
337 }
338
339 if (copy_to_user(buf, page, nr_read)) {
340 rv = -EFAULT;
341 goto out_free_page;
342 }
343
344 p += nr_read;
345 len -= nr_read;
346 buf += nr_read;
347 count -= nr_read;
348 rv += nr_read;
349
350 if (final)
351 goto out_free_page;
Alexey Dobriyanc2c0bb42015-06-25 15:00:54 -0700352 }
353
Alexey Dobriyana0a07b82017-02-24 15:00:20 -0800354 /* Only first chunk can be read partially. */
355 pos1 = 0;
356 i++;
Alexey Dobriyanc2c0bb42015-06-25 15:00:54 -0700357 }
Alexey Dobriyanc2c0bb42015-06-25 15:00:54 -0700358 }
359
360out_free_page:
361 free_page((unsigned long)page);
362out_mmput:
363 mmput(mm);
364 if (rv > 0)
365 *pos += rv;
366 return rv;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700367}
368
Alexey Dobriyanc2c0bb42015-06-25 15:00:54 -0700369static const struct file_operations proc_pid_cmdline_ops = {
370 .read = proc_pid_cmdline_read,
371 .llseek = generic_file_llseek,
372};
373
Linus Torvalds1da177e2005-04-16 15:20:36 -0700374#ifdef CONFIG_KALLSYMS
375/*
376 * Provides a wchan file via kallsyms in a proper one-value-per-file format.
377 * Returns the resolved symbol. If that fails, simply return the address.
378 */
Alexey Dobriyanedfcd602014-08-08 14:21:44 -0700379static int proc_pid_wchan(struct seq_file *m, struct pid_namespace *ns,
380 struct pid *pid, struct task_struct *task)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700381{
Alexey Dobriyanffb45122007-05-08 00:28:41 -0700382 unsigned long wchan;
Tejun Heo9281ace2007-07-17 04:03:51 -0700383 char symname[KSYM_NAME_LEN];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700384
385 wchan = get_wchan(task);
386
Jann Horncaaee622016-01-20 15:00:04 -0800387 if (wchan && ptrace_may_access(task, PTRACE_MODE_READ_FSCREDS)
388 && !lookup_symbol_name(wchan, symname))
Joe Perches25ce3192015-04-15 16:18:17 -0700389 seq_printf(m, "%s", symname);
Ingo Molnarb2f73922015-09-30 15:59:17 +0200390 else
Robin Humble1e92a612016-05-09 16:28:46 -0700391 seq_putc(m, '0');
Joe Perches25ce3192015-04-15 16:18:17 -0700392
393 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700394}
395#endif /* CONFIG_KALLSYMS */
396
Al Viroa9712bc2011-03-23 15:52:50 -0400397static int lock_trace(struct task_struct *task)
398{
399 int err = mutex_lock_killable(&task->signal->cred_guard_mutex);
400 if (err)
401 return err;
Jann Horncaaee622016-01-20 15:00:04 -0800402 if (!ptrace_may_access(task, PTRACE_MODE_ATTACH_FSCREDS)) {
Al Viroa9712bc2011-03-23 15:52:50 -0400403 mutex_unlock(&task->signal->cred_guard_mutex);
404 return -EPERM;
405 }
406 return 0;
407}
408
409static void unlock_trace(struct task_struct *task)
410{
411 mutex_unlock(&task->signal->cred_guard_mutex);
412}
413
Ken Chen2ec220e2008-11-10 11:26:08 +0300414#ifdef CONFIG_STACKTRACE
415
416#define MAX_STACK_TRACE_DEPTH 64
417
418static int proc_pid_stack(struct seq_file *m, struct pid_namespace *ns,
419 struct pid *pid, struct task_struct *task)
420{
421 struct stack_trace trace;
422 unsigned long *entries;
Al Viroa9712bc2011-03-23 15:52:50 -0400423 int err;
Ken Chen2ec220e2008-11-10 11:26:08 +0300424 int i;
425
426 entries = kmalloc(MAX_STACK_TRACE_DEPTH * sizeof(*entries), GFP_KERNEL);
427 if (!entries)
428 return -ENOMEM;
429
430 trace.nr_entries = 0;
431 trace.max_entries = MAX_STACK_TRACE_DEPTH;
432 trace.entries = entries;
433 trace.skip = 0;
Ken Chen2ec220e2008-11-10 11:26:08 +0300434
Al Viroa9712bc2011-03-23 15:52:50 -0400435 err = lock_trace(task);
436 if (!err) {
437 save_stack_trace_tsk(task, &trace);
438
439 for (i = 0; i < trace.nr_entries; i++) {
Josh Poimboeuf8b927d72016-08-18 10:59:07 -0500440 seq_printf(m, "[<%pK>] %pB\n",
Al Viroa9712bc2011-03-23 15:52:50 -0400441 (void *)entries[i], (void *)entries[i]);
442 }
443 unlock_trace(task);
Ken Chen2ec220e2008-11-10 11:26:08 +0300444 }
445 kfree(entries);
446
Al Viroa9712bc2011-03-23 15:52:50 -0400447 return err;
Ken Chen2ec220e2008-11-10 11:26:08 +0300448}
449#endif
450
Naveen N. Rao5968cec2015-06-30 14:36:03 +0530451#ifdef CONFIG_SCHED_INFO
Linus Torvalds1da177e2005-04-16 15:20:36 -0700452/*
453 * Provides /proc/PID/schedstat
454 */
Alexey Dobriyanf6e826c2014-08-08 14:21:46 -0700455static int proc_pid_schedstat(struct seq_file *m, struct pid_namespace *ns,
456 struct pid *pid, struct task_struct *task)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700457{
Naveen N. Rao5968cec2015-06-30 14:36:03 +0530458 if (unlikely(!sched_info_on()))
459 seq_printf(m, "0 0 0\n");
460 else
461 seq_printf(m, "%llu %llu %lu\n",
Joe Perches25ce3192015-04-15 16:18:17 -0700462 (unsigned long long)task->se.sum_exec_runtime,
463 (unsigned long long)task->sched_info.run_delay,
464 task->sched_info.pcount);
465
466 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700467}
468#endif
469
Arjan van de Ven97455122008-01-25 21:08:34 +0100470#ifdef CONFIG_LATENCYTOP
471static int lstats_show_proc(struct seq_file *m, void *v)
472{
473 int i;
Hiroshi Shimamoto13d77c32008-02-20 16:53:29 -0800474 struct inode *inode = m->private;
475 struct task_struct *task = get_proc_task(inode);
Arjan van de Ven97455122008-01-25 21:08:34 +0100476
Hiroshi Shimamoto13d77c32008-02-20 16:53:29 -0800477 if (!task)
478 return -ESRCH;
479 seq_puts(m, "Latency Top version : v0.1\n");
Arjan van de Ven97455122008-01-25 21:08:34 +0100480 for (i = 0; i < 32; i++) {
Joe Perches34e49d42011-01-12 17:00:30 -0800481 struct latency_record *lr = &task->latency_record[i];
482 if (lr->backtrace[0]) {
Arjan van de Ven97455122008-01-25 21:08:34 +0100483 int q;
Joe Perches34e49d42011-01-12 17:00:30 -0800484 seq_printf(m, "%i %li %li",
485 lr->count, lr->time, lr->max);
Arjan van de Ven97455122008-01-25 21:08:34 +0100486 for (q = 0; q < LT_BACKTRACEDEPTH; q++) {
Joe Perches34e49d42011-01-12 17:00:30 -0800487 unsigned long bt = lr->backtrace[q];
488 if (!bt)
Arjan van de Ven97455122008-01-25 21:08:34 +0100489 break;
Joe Perches34e49d42011-01-12 17:00:30 -0800490 if (bt == ULONG_MAX)
Arjan van de Ven97455122008-01-25 21:08:34 +0100491 break;
Joe Perches34e49d42011-01-12 17:00:30 -0800492 seq_printf(m, " %ps", (void *)bt);
Arjan van de Ven97455122008-01-25 21:08:34 +0100493 }
Alexey Dobriyan9d6de122011-01-12 17:00:32 -0800494 seq_putc(m, '\n');
Arjan van de Ven97455122008-01-25 21:08:34 +0100495 }
496
497 }
Hiroshi Shimamoto13d77c32008-02-20 16:53:29 -0800498 put_task_struct(task);
Arjan van de Ven97455122008-01-25 21:08:34 +0100499 return 0;
500}
501
502static int lstats_open(struct inode *inode, struct file *file)
503{
Hiroshi Shimamoto13d77c32008-02-20 16:53:29 -0800504 return single_open(file, lstats_show_proc, inode);
Hiroshi Shimamotod6643d12008-02-14 10:27:00 -0800505}
506
Arjan van de Ven97455122008-01-25 21:08:34 +0100507static ssize_t lstats_write(struct file *file, const char __user *buf,
508 size_t count, loff_t *offs)
509{
Al Viro496ad9a2013-01-23 17:07:38 -0500510 struct task_struct *task = get_proc_task(file_inode(file));
Arjan van de Ven97455122008-01-25 21:08:34 +0100511
Hiroshi Shimamoto13d77c32008-02-20 16:53:29 -0800512 if (!task)
513 return -ESRCH;
Arjan van de Ven97455122008-01-25 21:08:34 +0100514 clear_all_latency_tracing(task);
Hiroshi Shimamoto13d77c32008-02-20 16:53:29 -0800515 put_task_struct(task);
Arjan van de Ven97455122008-01-25 21:08:34 +0100516
517 return count;
518}
519
520static const struct file_operations proc_lstats_operations = {
521 .open = lstats_open,
522 .read = seq_read,
523 .write = lstats_write,
524 .llseek = seq_lseek,
Hiroshi Shimamoto13d77c32008-02-20 16:53:29 -0800525 .release = single_release,
Arjan van de Ven97455122008-01-25 21:08:34 +0100526};
527
528#endif
529
Alexey Dobriyan6ba51e32014-08-08 14:21:48 -0700530static int proc_oom_score(struct seq_file *m, struct pid_namespace *ns,
531 struct pid *pid, struct task_struct *task)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700532{
David Rientjesa7f638f2012-05-29 15:06:47 -0700533 unsigned long totalpages = totalram_pages + total_swap_pages;
Oleg Nesterovb95c35e2010-04-01 15:13:57 +0200534 unsigned long points = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700535
Oleg Nesterovef419392016-08-02 14:03:19 -0700536 points = oom_badness(task, NULL, NULL, totalpages) *
537 1000 / totalpages;
Joe Perches25ce3192015-04-15 16:18:17 -0700538 seq_printf(m, "%lu\n", points);
539
540 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700541}
542
Neil Hormand85f50d2007-10-18 23:40:37 -0700543struct limit_names {
Alexey Dobriyancedbcca2014-08-08 14:21:33 -0700544 const char *name;
545 const char *unit;
Neil Hormand85f50d2007-10-18 23:40:37 -0700546};
547
548static const struct limit_names lnames[RLIM_NLIMITS] = {
Kees Cookcff4edb2009-09-22 16:45:32 -0700549 [RLIMIT_CPU] = {"Max cpu time", "seconds"},
Neil Hormand85f50d2007-10-18 23:40:37 -0700550 [RLIMIT_FSIZE] = {"Max file size", "bytes"},
551 [RLIMIT_DATA] = {"Max data size", "bytes"},
552 [RLIMIT_STACK] = {"Max stack size", "bytes"},
553 [RLIMIT_CORE] = {"Max core file size", "bytes"},
554 [RLIMIT_RSS] = {"Max resident set", "bytes"},
555 [RLIMIT_NPROC] = {"Max processes", "processes"},
556 [RLIMIT_NOFILE] = {"Max open files", "files"},
557 [RLIMIT_MEMLOCK] = {"Max locked memory", "bytes"},
558 [RLIMIT_AS] = {"Max address space", "bytes"},
559 [RLIMIT_LOCKS] = {"Max file locks", "locks"},
560 [RLIMIT_SIGPENDING] = {"Max pending signals", "signals"},
561 [RLIMIT_MSGQUEUE] = {"Max msgqueue size", "bytes"},
562 [RLIMIT_NICE] = {"Max nice priority", NULL},
563 [RLIMIT_RTPRIO] = {"Max realtime priority", NULL},
Eugene Teo88081172008-02-23 15:23:52 -0800564 [RLIMIT_RTTIME] = {"Max realtime timeout", "us"},
Neil Hormand85f50d2007-10-18 23:40:37 -0700565};
566
567/* Display limits for a process */
Alexey Dobriyan1c963eb2014-08-08 14:21:37 -0700568static int proc_pid_limits(struct seq_file *m, struct pid_namespace *ns,
569 struct pid *pid, struct task_struct *task)
Neil Hormand85f50d2007-10-18 23:40:37 -0700570{
571 unsigned int i;
Neil Hormand85f50d2007-10-18 23:40:37 -0700572 unsigned long flags;
Neil Hormand85f50d2007-10-18 23:40:37 -0700573
574 struct rlimit rlim[RLIM_NLIMITS];
575
Lai Jiangshana6bebbc2008-10-05 00:51:15 +0400576 if (!lock_task_sighand(task, &flags))
Neil Hormand85f50d2007-10-18 23:40:37 -0700577 return 0;
Neil Hormand85f50d2007-10-18 23:40:37 -0700578 memcpy(rlim, task->signal->rlim, sizeof(struct rlimit) * RLIM_NLIMITS);
579 unlock_task_sighand(task, &flags);
Neil Hormand85f50d2007-10-18 23:40:37 -0700580
581 /*
582 * print the file header
583 */
Alexey Dobriyan1c963eb2014-08-08 14:21:37 -0700584 seq_printf(m, "%-25s %-20s %-20s %-10s\n",
Joe Perches25ce3192015-04-15 16:18:17 -0700585 "Limit", "Soft Limit", "Hard Limit", "Units");
Neil Hormand85f50d2007-10-18 23:40:37 -0700586
587 for (i = 0; i < RLIM_NLIMITS; i++) {
588 if (rlim[i].rlim_cur == RLIM_INFINITY)
Alexey Dobriyan1c963eb2014-08-08 14:21:37 -0700589 seq_printf(m, "%-25s %-20s ",
Joe Perches25ce3192015-04-15 16:18:17 -0700590 lnames[i].name, "unlimited");
Neil Hormand85f50d2007-10-18 23:40:37 -0700591 else
Alexey Dobriyan1c963eb2014-08-08 14:21:37 -0700592 seq_printf(m, "%-25s %-20lu ",
Joe Perches25ce3192015-04-15 16:18:17 -0700593 lnames[i].name, rlim[i].rlim_cur);
Neil Hormand85f50d2007-10-18 23:40:37 -0700594
595 if (rlim[i].rlim_max == RLIM_INFINITY)
Alexey Dobriyan1c963eb2014-08-08 14:21:37 -0700596 seq_printf(m, "%-20s ", "unlimited");
Neil Hormand85f50d2007-10-18 23:40:37 -0700597 else
Alexey Dobriyan1c963eb2014-08-08 14:21:37 -0700598 seq_printf(m, "%-20lu ", rlim[i].rlim_max);
Neil Hormand85f50d2007-10-18 23:40:37 -0700599
600 if (lnames[i].unit)
Alexey Dobriyan1c963eb2014-08-08 14:21:37 -0700601 seq_printf(m, "%-10s\n", lnames[i].unit);
Neil Hormand85f50d2007-10-18 23:40:37 -0700602 else
Alexey Dobriyan1c963eb2014-08-08 14:21:37 -0700603 seq_putc(m, '\n');
Neil Hormand85f50d2007-10-18 23:40:37 -0700604 }
605
Alexey Dobriyan1c963eb2014-08-08 14:21:37 -0700606 return 0;
Neil Hormand85f50d2007-10-18 23:40:37 -0700607}
608
Roland McGrathebcb6732008-07-25 19:46:00 -0700609#ifdef CONFIG_HAVE_ARCH_TRACEHOOK
Alexey Dobriyan09d93bd2014-08-08 14:21:39 -0700610static int proc_pid_syscall(struct seq_file *m, struct pid_namespace *ns,
611 struct pid *pid, struct task_struct *task)
Roland McGrathebcb6732008-07-25 19:46:00 -0700612{
613 long nr;
614 unsigned long args[6], sp, pc;
Joe Perches25ce3192015-04-15 16:18:17 -0700615 int res;
616
617 res = lock_trace(task);
Al Viroa9712bc2011-03-23 15:52:50 -0400618 if (res)
619 return res;
Roland McGrathebcb6732008-07-25 19:46:00 -0700620
621 if (task_current_syscall(task, &nr, args, 6, &sp, &pc))
Alexey Dobriyan09d93bd2014-08-08 14:21:39 -0700622 seq_puts(m, "running\n");
Al Viroa9712bc2011-03-23 15:52:50 -0400623 else if (nr < 0)
Alexey Dobriyan09d93bd2014-08-08 14:21:39 -0700624 seq_printf(m, "%ld 0x%lx 0x%lx\n", nr, sp, pc);
Al Viroa9712bc2011-03-23 15:52:50 -0400625 else
Alexey Dobriyan09d93bd2014-08-08 14:21:39 -0700626 seq_printf(m,
Roland McGrathebcb6732008-07-25 19:46:00 -0700627 "%ld 0x%lx 0x%lx 0x%lx 0x%lx 0x%lx 0x%lx 0x%lx 0x%lx\n",
628 nr,
629 args[0], args[1], args[2], args[3], args[4], args[5],
630 sp, pc);
Al Viroa9712bc2011-03-23 15:52:50 -0400631 unlock_trace(task);
Joe Perches25ce3192015-04-15 16:18:17 -0700632
633 return 0;
Roland McGrathebcb6732008-07-25 19:46:00 -0700634}
635#endif /* CONFIG_HAVE_ARCH_TRACEHOOK */
636
Linus Torvalds1da177e2005-04-16 15:20:36 -0700637/************************************************************************/
638/* Here the fs part begins */
639/************************************************************************/
640
641/* permission checks */
Eric W. Biederman778c1142006-06-26 00:25:58 -0700642static int proc_fd_access_allowed(struct inode *inode)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700643{
Eric W. Biederman778c1142006-06-26 00:25:58 -0700644 struct task_struct *task;
645 int allowed = 0;
Eric W. Biedermandf26c402006-06-26 00:25:59 -0700646 /* Allow access to a task's file descriptors if it is us or we
647 * may use ptrace attach to the process and find out that
648 * information.
Eric W. Biederman778c1142006-06-26 00:25:58 -0700649 */
650 task = get_proc_task(inode);
Eric W. Biedermandf26c402006-06-26 00:25:59 -0700651 if (task) {
Jann Horncaaee622016-01-20 15:00:04 -0800652 allowed = ptrace_may_access(task, PTRACE_MODE_READ_FSCREDS);
Eric W. Biederman778c1142006-06-26 00:25:58 -0700653 put_task_struct(task);
Eric W. Biedermandf26c402006-06-26 00:25:59 -0700654 }
Eric W. Biederman778c1142006-06-26 00:25:58 -0700655 return allowed;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700656}
657
Eric W. Biederman6b4e3062010-03-07 16:41:34 -0800658int proc_setattr(struct dentry *dentry, struct iattr *attr)
Linus Torvalds6d76fa52006-07-15 12:26:45 -0700659{
660 int error;
David Howells2b0143b2015-03-17 22:25:59 +0000661 struct inode *inode = d_inode(dentry);
Linus Torvalds6d76fa52006-07-15 12:26:45 -0700662
663 if (attr->ia_valid & ATTR_MODE)
664 return -EPERM;
665
Jan Kara31051c82016-05-26 16:55:18 +0200666 error = setattr_prepare(dentry, attr);
Christoph Hellwig10257742010-06-04 11:30:02 +0200667 if (error)
668 return error;
669
Christoph Hellwig10257742010-06-04 11:30:02 +0200670 setattr_copy(inode, attr);
671 mark_inode_dirty(inode);
672 return 0;
Linus Torvalds6d76fa52006-07-15 12:26:45 -0700673}
674
Vasiliy Kulikov04996802012-01-10 15:11:31 -0800675/*
676 * May current process learn task's sched/cmdline info (for hide_pid_min=1)
677 * or euid/egid (for hide_pid_min=2)?
678 */
679static bool has_pid_permissions(struct pid_namespace *pid,
680 struct task_struct *task,
681 int hide_pid_min)
682{
683 if (pid->hide_pid < hide_pid_min)
684 return true;
685 if (in_group_p(pid->pid_gid))
686 return true;
Jann Horncaaee622016-01-20 15:00:04 -0800687 return ptrace_may_access(task, PTRACE_MODE_READ_FSCREDS);
Vasiliy Kulikov04996802012-01-10 15:11:31 -0800688}
689
690
691static int proc_pid_permission(struct inode *inode, int mask)
692{
693 struct pid_namespace *pid = inode->i_sb->s_fs_info;
694 struct task_struct *task;
695 bool has_perms;
696
697 task = get_proc_task(inode);
Xiaotian Fenga2ef9902012-01-12 17:17:08 -0800698 if (!task)
699 return -ESRCH;
Lafcadio Wluiki796f5712017-02-24 15:00:23 -0800700 has_perms = has_pid_permissions(pid, task, HIDEPID_NO_ACCESS);
Vasiliy Kulikov04996802012-01-10 15:11:31 -0800701 put_task_struct(task);
702
703 if (!has_perms) {
Lafcadio Wluiki796f5712017-02-24 15:00:23 -0800704 if (pid->hide_pid == HIDEPID_INVISIBLE) {
Vasiliy Kulikov04996802012-01-10 15:11:31 -0800705 /*
706 * Let's make getdents(), stat(), and open()
707 * consistent with each other. If a process
708 * may not stat() a file, it shouldn't be seen
709 * in procfs at all.
710 */
711 return -ENOENT;
712 }
713
714 return -EPERM;
715 }
716 return generic_permission(inode, mask);
717}
718
719
720
Arjan van de Venc5ef1c42007-02-12 00:55:40 -0800721static const struct inode_operations proc_def_inode_operations = {
Linus Torvalds6d76fa52006-07-15 12:26:45 -0700722 .setattr = proc_setattr,
723};
724
Eric W. Biedermanbe614082008-02-08 04:18:30 -0800725static int proc_single_show(struct seq_file *m, void *v)
726{
727 struct inode *inode = m->private;
728 struct pid_namespace *ns;
729 struct pid *pid;
730 struct task_struct *task;
731 int ret;
732
733 ns = inode->i_sb->s_fs_info;
734 pid = proc_pid(inode);
735 task = get_pid_task(pid, PIDTYPE_PID);
736 if (!task)
737 return -ESRCH;
738
739 ret = PROC_I(inode)->op.proc_show(m, ns, pid, task);
740
741 put_task_struct(task);
742 return ret;
743}
744
745static int proc_single_open(struct inode *inode, struct file *filp)
746{
Jovi Zhangc6a34052011-01-12 17:00:34 -0800747 return single_open(filp, proc_single_show, inode);
Eric W. Biedermanbe614082008-02-08 04:18:30 -0800748}
749
750static const struct file_operations proc_single_file_operations = {
751 .open = proc_single_open,
752 .read = seq_read,
753 .llseek = seq_lseek,
754 .release = single_release,
755};
756
Oleg Nesterov5381e162014-10-09 15:25:24 -0700757
758struct mm_struct *proc_mem_open(struct inode *inode, unsigned int mode)
759{
760 struct task_struct *task = get_proc_task(inode);
761 struct mm_struct *mm = ERR_PTR(-ESRCH);
762
763 if (task) {
Jann Horncaaee622016-01-20 15:00:04 -0800764 mm = mm_access(task, mode | PTRACE_MODE_FSCREDS);
Oleg Nesterov5381e162014-10-09 15:25:24 -0700765 put_task_struct(task);
766
767 if (!IS_ERR_OR_NULL(mm)) {
768 /* ensure this mm_struct can't be freed */
Vegard Nossumf1f10072017-02-27 14:30:07 -0800769 mmgrab(mm);
Oleg Nesterov5381e162014-10-09 15:25:24 -0700770 /* but do not pin its memory */
771 mmput(mm);
772 }
773 }
774
775 return mm;
776}
777
Cong Wangb409e572012-05-31 16:26:17 -0700778static int __mem_open(struct inode *inode, struct file *file, unsigned int mode)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700779{
Oleg Nesterov5381e162014-10-09 15:25:24 -0700780 struct mm_struct *mm = proc_mem_open(inode, mode);
Linus Torvaldse2683372012-01-17 15:21:19 -0800781
782 if (IS_ERR(mm))
783 return PTR_ERR(mm);
784
Linus Torvaldse2683372012-01-17 15:21:19 -0800785 file->private_data = mm;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700786 return 0;
787}
788
Cong Wangb409e572012-05-31 16:26:17 -0700789static int mem_open(struct inode *inode, struct file *file)
790{
Djalal Harounibc452b42012-07-30 14:42:28 -0700791 int ret = __mem_open(inode, file, PTRACE_MODE_ATTACH);
792
793 /* OK to pass negative loff_t, we can catch out-of-range */
794 file->f_mode |= FMODE_UNSIGNED_OFFSET;
795
796 return ret;
Cong Wangb409e572012-05-31 16:26:17 -0700797}
798
Oleg Nesterov572d34b2012-01-31 17:14:54 +0100799static ssize_t mem_rw(struct file *file, char __user *buf,
800 size_t count, loff_t *ppos, int write)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700801{
Linus Torvaldse2683372012-01-17 15:21:19 -0800802 struct mm_struct *mm = file->private_data;
Oleg Nesterov572d34b2012-01-31 17:14:54 +0100803 unsigned long addr = *ppos;
804 ssize_t copied;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700805 char *page;
Linus Torvalds272ddc82016-10-24 19:00:44 -0700806 unsigned int flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700807
Linus Torvaldse2683372012-01-17 15:21:19 -0800808 if (!mm)
809 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700810
Mel Gormane12ba742007-10-16 01:25:52 -0700811 page = (char *)__get_free_page(GFP_TEMPORARY);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700812 if (!page)
Linus Torvaldse2683372012-01-17 15:21:19 -0800813 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700814
Frederik Deweerdtf7ca54f2006-09-29 02:01:02 -0700815 copied = 0;
Vegard Nossum388f7932017-02-27 14:30:13 -0800816 if (!mmget_not_zero(mm))
Oleg Nesterov6d08f2c72012-01-31 17:15:11 +0100817 goto free;
818
Linus Torvalds272ddc82016-10-24 19:00:44 -0700819 /* Maybe we should limit FOLL_FORCE to actual ptrace users? */
820 flags = FOLL_FORCE;
Lorenzo Stoakes6347e8d2016-10-13 01:20:19 +0100821 if (write)
822 flags |= FOLL_WRITE;
823
Linus Torvalds1da177e2005-04-16 15:20:36 -0700824 while (count > 0) {
Oleg Nesterov572d34b2012-01-31 17:14:54 +0100825 int this_len = min_t(int, count, PAGE_SIZE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700826
Oleg Nesterov572d34b2012-01-31 17:14:54 +0100827 if (write && copy_from_user(page, buf, this_len)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700828 copied = -EFAULT;
829 break;
830 }
Oleg Nesterov572d34b2012-01-31 17:14:54 +0100831
Lorenzo Stoakes6347e8d2016-10-13 01:20:19 +0100832 this_len = access_remote_vm(mm, addr, page, this_len, flags);
Oleg Nesterov572d34b2012-01-31 17:14:54 +0100833 if (!this_len) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700834 if (!copied)
835 copied = -EIO;
836 break;
837 }
Oleg Nesterov572d34b2012-01-31 17:14:54 +0100838
839 if (!write && copy_to_user(buf, page, this_len)) {
840 copied = -EFAULT;
841 break;
842 }
843
844 buf += this_len;
845 addr += this_len;
846 copied += this_len;
847 count -= this_len;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700848 }
Oleg Nesterov572d34b2012-01-31 17:14:54 +0100849 *ppos = addr;
KOSAKI Motohiro30cd8902011-05-26 16:25:52 -0700850
Oleg Nesterov6d08f2c72012-01-31 17:15:11 +0100851 mmput(mm);
852free:
KOSAKI Motohiro30cd8902011-05-26 16:25:52 -0700853 free_page((unsigned long) page);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700854 return copied;
855}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700856
Oleg Nesterov572d34b2012-01-31 17:14:54 +0100857static ssize_t mem_read(struct file *file, char __user *buf,
858 size_t count, loff_t *ppos)
859{
860 return mem_rw(file, buf, count, ppos, 0);
861}
862
863static ssize_t mem_write(struct file *file, const char __user *buf,
864 size_t count, loff_t *ppos)
865{
866 return mem_rw(file, (char __user*)buf, count, ppos, 1);
867}
868
Matt Mackall85863e42008-02-04 22:29:04 -0800869loff_t mem_lseek(struct file *file, loff_t offset, int orig)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700870{
871 switch (orig) {
872 case 0:
873 file->f_pos = offset;
874 break;
875 case 1:
876 file->f_pos += offset;
877 break;
878 default:
879 return -EINVAL;
880 }
881 force_successful_syscall_return();
882 return file->f_pos;
883}
884
Linus Torvaldse2683372012-01-17 15:21:19 -0800885static int mem_release(struct inode *inode, struct file *file)
886{
887 struct mm_struct *mm = file->private_data;
Oleg Nesterov71879d32012-01-31 17:14:38 +0100888 if (mm)
Oleg Nesterov6d08f2c72012-01-31 17:15:11 +0100889 mmdrop(mm);
Linus Torvaldse2683372012-01-17 15:21:19 -0800890 return 0;
891}
892
Arjan van de Ven00977a52007-02-12 00:55:34 -0800893static const struct file_operations proc_mem_operations = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700894 .llseek = mem_lseek,
895 .read = mem_read,
896 .write = mem_write,
897 .open = mem_open,
Linus Torvaldse2683372012-01-17 15:21:19 -0800898 .release = mem_release,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700899};
900
Cong Wangb409e572012-05-31 16:26:17 -0700901static int environ_open(struct inode *inode, struct file *file)
902{
903 return __mem_open(inode, file, PTRACE_MODE_READ);
904}
905
James Pearson315e28c2007-10-16 23:30:17 -0700906static ssize_t environ_read(struct file *file, char __user *buf,
907 size_t count, loff_t *ppos)
908{
James Pearson315e28c2007-10-16 23:30:17 -0700909 char *page;
910 unsigned long src = *ppos;
Cong Wangb409e572012-05-31 16:26:17 -0700911 int ret = 0;
912 struct mm_struct *mm = file->private_data;
Mateusz Guzika3b609e2016-01-20 15:01:05 -0800913 unsigned long env_start, env_end;
James Pearson315e28c2007-10-16 23:30:17 -0700914
Mathias Krause8148a732016-05-05 16:22:26 -0700915 /* Ensure the process spawned far enough to have an environment. */
916 if (!mm || !mm->env_end)
Cong Wangb409e572012-05-31 16:26:17 -0700917 return 0;
James Pearson315e28c2007-10-16 23:30:17 -0700918
James Pearson315e28c2007-10-16 23:30:17 -0700919 page = (char *)__get_free_page(GFP_TEMPORARY);
920 if (!page)
Cong Wangb409e572012-05-31 16:26:17 -0700921 return -ENOMEM;
James Pearson315e28c2007-10-16 23:30:17 -0700922
Al Virod6f64b82011-02-15 22:26:01 -0500923 ret = 0;
Vegard Nossum388f7932017-02-27 14:30:13 -0800924 if (!mmget_not_zero(mm))
Cong Wangb409e572012-05-31 16:26:17 -0700925 goto free;
Mateusz Guzika3b609e2016-01-20 15:01:05 -0800926
927 down_read(&mm->mmap_sem);
928 env_start = mm->env_start;
929 env_end = mm->env_end;
930 up_read(&mm->mmap_sem);
931
James Pearson315e28c2007-10-16 23:30:17 -0700932 while (count > 0) {
Djalal Harounie8905ec2012-07-30 14:42:26 -0700933 size_t this_len, max_len;
934 int retval;
935
Mateusz Guzika3b609e2016-01-20 15:01:05 -0800936 if (src >= (env_end - env_start))
Djalal Harounie8905ec2012-07-30 14:42:26 -0700937 break;
James Pearson315e28c2007-10-16 23:30:17 -0700938
Mateusz Guzika3b609e2016-01-20 15:01:05 -0800939 this_len = env_end - (env_start + src);
James Pearson315e28c2007-10-16 23:30:17 -0700940
Djalal Harounie8905ec2012-07-30 14:42:26 -0700941 max_len = min_t(size_t, PAGE_SIZE, count);
942 this_len = min(max_len, this_len);
James Pearson315e28c2007-10-16 23:30:17 -0700943
Linus Torvalds272ddc82016-10-24 19:00:44 -0700944 retval = access_remote_vm(mm, (env_start + src), page, this_len, 0);
James Pearson315e28c2007-10-16 23:30:17 -0700945
946 if (retval <= 0) {
947 ret = retval;
948 break;
949 }
950
951 if (copy_to_user(buf, page, retval)) {
952 ret = -EFAULT;
953 break;
954 }
955
956 ret += retval;
957 src += retval;
958 buf += retval;
959 count -= retval;
960 }
961 *ppos = src;
James Pearson315e28c2007-10-16 23:30:17 -0700962 mmput(mm);
Cong Wangb409e572012-05-31 16:26:17 -0700963
964free:
James Pearson315e28c2007-10-16 23:30:17 -0700965 free_page((unsigned long) page);
James Pearson315e28c2007-10-16 23:30:17 -0700966 return ret;
967}
968
969static const struct file_operations proc_environ_operations = {
Cong Wangb409e572012-05-31 16:26:17 -0700970 .open = environ_open,
James Pearson315e28c2007-10-16 23:30:17 -0700971 .read = environ_read,
Arnd Bergmann87df8422010-03-17 23:06:02 +0100972 .llseek = generic_file_llseek,
Cong Wangb409e572012-05-31 16:26:17 -0700973 .release = mem_release,
James Pearson315e28c2007-10-16 23:30:17 -0700974};
975
Al Viroc5317162016-10-05 18:43:43 -0400976static int auxv_open(struct inode *inode, struct file *file)
977{
978 return __mem_open(inode, file, PTRACE_MODE_READ_FSCREDS);
979}
980
981static ssize_t auxv_read(struct file *file, char __user *buf,
982 size_t count, loff_t *ppos)
983{
984 struct mm_struct *mm = file->private_data;
985 unsigned int nwords = 0;
Leon Yu06b28492016-10-27 17:46:50 -0700986
987 if (!mm)
988 return 0;
Al Viroc5317162016-10-05 18:43:43 -0400989 do {
990 nwords += 2;
991 } while (mm->saved_auxv[nwords - 2] != 0); /* AT_NULL */
992 return simple_read_from_buffer(buf, count, ppos, mm->saved_auxv,
993 nwords * sizeof(mm->saved_auxv[0]));
994}
995
996static const struct file_operations proc_auxv_operations = {
997 .open = auxv_open,
998 .read = auxv_read,
999 .llseek = generic_file_llseek,
1000 .release = mem_release,
1001};
1002
David Rientjesfa0cbbf2012-11-12 17:53:04 -08001003static ssize_t oom_adj_read(struct file *file, char __user *buf, size_t count,
1004 loff_t *ppos)
1005{
Al Viro496ad9a2013-01-23 17:07:38 -05001006 struct task_struct *task = get_proc_task(file_inode(file));
David Rientjesfa0cbbf2012-11-12 17:53:04 -08001007 char buffer[PROC_NUMBUF];
1008 int oom_adj = OOM_ADJUST_MIN;
1009 size_t len;
David Rientjesfa0cbbf2012-11-12 17:53:04 -08001010
1011 if (!task)
1012 return -ESRCH;
Michal Hockof913da52016-07-28 15:44:37 -07001013 if (task->signal->oom_score_adj == OOM_SCORE_ADJ_MAX)
1014 oom_adj = OOM_ADJUST_MAX;
1015 else
1016 oom_adj = (task->signal->oom_score_adj * -OOM_DISABLE) /
1017 OOM_SCORE_ADJ_MAX;
David Rientjesfa0cbbf2012-11-12 17:53:04 -08001018 put_task_struct(task);
1019 len = snprintf(buffer, sizeof(buffer), "%d\n", oom_adj);
1020 return simple_read_from_buffer(buf, count, ppos, buffer, len);
1021}
1022
Michal Hocko1d5f0ac2016-07-28 15:44:40 -07001023static int __set_oom_adj(struct file *file, int oom_adj, bool legacy)
1024{
1025 static DEFINE_MUTEX(oom_adj_mutex);
Michal Hocko44a70ade2016-07-28 15:44:43 -07001026 struct mm_struct *mm = NULL;
Michal Hocko1d5f0ac2016-07-28 15:44:40 -07001027 struct task_struct *task;
1028 int err = 0;
1029
1030 task = get_proc_task(file_inode(file));
1031 if (!task)
1032 return -ESRCH;
1033
1034 mutex_lock(&oom_adj_mutex);
1035 if (legacy) {
1036 if (oom_adj < task->signal->oom_score_adj &&
1037 !capable(CAP_SYS_RESOURCE)) {
1038 err = -EACCES;
1039 goto err_unlock;
1040 }
1041 /*
1042 * /proc/pid/oom_adj is provided for legacy purposes, ask users to use
1043 * /proc/pid/oom_score_adj instead.
1044 */
1045 pr_warn_once("%s (%d): /proc/%d/oom_adj is deprecated, please use /proc/%d/oom_score_adj instead.\n",
1046 current->comm, task_pid_nr(current), task_pid_nr(task),
1047 task_pid_nr(task));
1048 } else {
1049 if ((short)oom_adj < task->signal->oom_score_adj_min &&
1050 !capable(CAP_SYS_RESOURCE)) {
1051 err = -EACCES;
1052 goto err_unlock;
1053 }
1054 }
1055
Michal Hocko44a70ade2016-07-28 15:44:43 -07001056 /*
1057 * Make sure we will check other processes sharing the mm if this is
1058 * not vfrok which wants its own oom_score_adj.
1059 * pin the mm so it doesn't go away and get reused after task_unlock
1060 */
1061 if (!task->vfork_done) {
1062 struct task_struct *p = find_lock_task_mm(task);
1063
1064 if (p) {
1065 if (atomic_read(&p->mm->mm_users) > 1) {
1066 mm = p->mm;
Vegard Nossumf1f10072017-02-27 14:30:07 -08001067 mmgrab(mm);
Michal Hocko44a70ade2016-07-28 15:44:43 -07001068 }
1069 task_unlock(p);
1070 }
1071 }
1072
Michal Hocko1d5f0ac2016-07-28 15:44:40 -07001073 task->signal->oom_score_adj = oom_adj;
1074 if (!legacy && has_capability_noaudit(current, CAP_SYS_RESOURCE))
1075 task->signal->oom_score_adj_min = (short)oom_adj;
1076 trace_oom_score_adj_update(task);
Michal Hocko44a70ade2016-07-28 15:44:43 -07001077
1078 if (mm) {
1079 struct task_struct *p;
1080
1081 rcu_read_lock();
1082 for_each_process(p) {
1083 if (same_thread_group(task, p))
1084 continue;
1085
1086 /* do not touch kernel threads or the global init */
1087 if (p->flags & PF_KTHREAD || is_global_init(p))
1088 continue;
1089
1090 task_lock(p);
1091 if (!p->vfork_done && process_shares_mm(p, mm)) {
1092 pr_info("updating oom_score_adj for %d (%s) from %d to %d because it shares mm with %d (%s). Report if this is unexpected.\n",
1093 task_pid_nr(p), p->comm,
1094 p->signal->oom_score_adj, oom_adj,
1095 task_pid_nr(task), task->comm);
1096 p->signal->oom_score_adj = oom_adj;
1097 if (!legacy && has_capability_noaudit(current, CAP_SYS_RESOURCE))
1098 p->signal->oom_score_adj_min = (short)oom_adj;
1099 }
1100 task_unlock(p);
1101 }
1102 rcu_read_unlock();
1103 mmdrop(mm);
1104 }
Michal Hocko1d5f0ac2016-07-28 15:44:40 -07001105err_unlock:
1106 mutex_unlock(&oom_adj_mutex);
1107 put_task_struct(task);
1108 return err;
1109}
Michal Hockof913da52016-07-28 15:44:37 -07001110
David Rientjesb72bdfa2015-11-05 18:50:32 -08001111/*
1112 * /proc/pid/oom_adj exists solely for backwards compatibility with previous
1113 * kernels. The effective policy is defined by oom_score_adj, which has a
1114 * different scale: oom_adj grew exponentially and oom_score_adj grows linearly.
1115 * Values written to oom_adj are simply mapped linearly to oom_score_adj.
1116 * Processes that become oom disabled via oom_adj will still be oom disabled
1117 * with this implementation.
1118 *
1119 * oom_adj cannot be removed since existing userspace binaries use it.
1120 */
David Rientjesfa0cbbf2012-11-12 17:53:04 -08001121static ssize_t oom_adj_write(struct file *file, const char __user *buf,
1122 size_t count, loff_t *ppos)
1123{
David Rientjesfa0cbbf2012-11-12 17:53:04 -08001124 char buffer[PROC_NUMBUF];
1125 int oom_adj;
David Rientjesfa0cbbf2012-11-12 17:53:04 -08001126 int err;
1127
1128 memset(buffer, 0, sizeof(buffer));
1129 if (count > sizeof(buffer) - 1)
1130 count = sizeof(buffer) - 1;
1131 if (copy_from_user(buffer, buf, count)) {
1132 err = -EFAULT;
1133 goto out;
1134 }
1135
1136 err = kstrtoint(strstrip(buffer), 0, &oom_adj);
1137 if (err)
1138 goto out;
1139 if ((oom_adj < OOM_ADJUST_MIN || oom_adj > OOM_ADJUST_MAX) &&
1140 oom_adj != OOM_DISABLE) {
1141 err = -EINVAL;
1142 goto out;
1143 }
1144
David Rientjesfa0cbbf2012-11-12 17:53:04 -08001145 /*
1146 * Scale /proc/pid/oom_score_adj appropriately ensuring that a maximum
1147 * value is always attainable.
1148 */
1149 if (oom_adj == OOM_ADJUST_MAX)
1150 oom_adj = OOM_SCORE_ADJ_MAX;
1151 else
1152 oom_adj = (oom_adj * OOM_SCORE_ADJ_MAX) / -OOM_DISABLE;
1153
Michal Hocko1d5f0ac2016-07-28 15:44:40 -07001154 err = __set_oom_adj(file, oom_adj, true);
David Rientjesfa0cbbf2012-11-12 17:53:04 -08001155out:
1156 return err < 0 ? err : count;
1157}
1158
1159static const struct file_operations proc_oom_adj_operations = {
1160 .read = oom_adj_read,
1161 .write = oom_adj_write,
1162 .llseek = generic_file_llseek,
1163};
1164
David Rientjesa63d83f2010-08-09 17:19:46 -07001165static ssize_t oom_score_adj_read(struct file *file, char __user *buf,
1166 size_t count, loff_t *ppos)
1167{
Al Viro496ad9a2013-01-23 17:07:38 -05001168 struct task_struct *task = get_proc_task(file_inode(file));
David Rientjesa63d83f2010-08-09 17:19:46 -07001169 char buffer[PROC_NUMBUF];
David Rientjesa9c58b902012-12-11 16:02:54 -08001170 short oom_score_adj = OOM_SCORE_ADJ_MIN;
David Rientjesa63d83f2010-08-09 17:19:46 -07001171 size_t len;
1172
1173 if (!task)
1174 return -ESRCH;
Michal Hockof913da52016-07-28 15:44:37 -07001175 oom_score_adj = task->signal->oom_score_adj;
David Rientjesa63d83f2010-08-09 17:19:46 -07001176 put_task_struct(task);
David Rientjesa9c58b902012-12-11 16:02:54 -08001177 len = snprintf(buffer, sizeof(buffer), "%hd\n", oom_score_adj);
David Rientjesa63d83f2010-08-09 17:19:46 -07001178 return simple_read_from_buffer(buf, count, ppos, buffer, len);
1179}
1180
1181static ssize_t oom_score_adj_write(struct file *file, const char __user *buf,
1182 size_t count, loff_t *ppos)
1183{
David Rientjesa63d83f2010-08-09 17:19:46 -07001184 char buffer[PROC_NUMBUF];
Alexey Dobriyan0a8cb8e2011-05-26 16:25:50 -07001185 int oom_score_adj;
David Rientjesa63d83f2010-08-09 17:19:46 -07001186 int err;
1187
1188 memset(buffer, 0, sizeof(buffer));
1189 if (count > sizeof(buffer) - 1)
1190 count = sizeof(buffer) - 1;
David Rientjes723548b2010-10-26 14:21:25 -07001191 if (copy_from_user(buffer, buf, count)) {
1192 err = -EFAULT;
1193 goto out;
1194 }
David Rientjesa63d83f2010-08-09 17:19:46 -07001195
Alexey Dobriyan0a8cb8e2011-05-26 16:25:50 -07001196 err = kstrtoint(strstrip(buffer), 0, &oom_score_adj);
David Rientjesa63d83f2010-08-09 17:19:46 -07001197 if (err)
David Rientjes723548b2010-10-26 14:21:25 -07001198 goto out;
David Rientjesa63d83f2010-08-09 17:19:46 -07001199 if (oom_score_adj < OOM_SCORE_ADJ_MIN ||
David Rientjes723548b2010-10-26 14:21:25 -07001200 oom_score_adj > OOM_SCORE_ADJ_MAX) {
1201 err = -EINVAL;
1202 goto out;
1203 }
David Rientjesa63d83f2010-08-09 17:19:46 -07001204
Michal Hocko1d5f0ac2016-07-28 15:44:40 -07001205 err = __set_oom_adj(file, oom_score_adj, false);
David Rientjes723548b2010-10-26 14:21:25 -07001206out:
1207 return err < 0 ? err : count;
David Rientjesa63d83f2010-08-09 17:19:46 -07001208}
1209
1210static const struct file_operations proc_oom_score_adj_operations = {
1211 .read = oom_score_adj_read,
1212 .write = oom_score_adj_write,
Arnd Bergmann6038f372010-08-15 18:52:59 +02001213 .llseek = default_llseek,
David Rientjesa63d83f2010-08-09 17:19:46 -07001214};
1215
Linus Torvalds1da177e2005-04-16 15:20:36 -07001216#ifdef CONFIG_AUDITSYSCALL
Alexey Dobriyanb4eb4f72016-10-29 19:04:39 +03001217#define TMPBUFLEN 11
Linus Torvalds1da177e2005-04-16 15:20:36 -07001218static ssize_t proc_loginuid_read(struct file * file, char __user * buf,
1219 size_t count, loff_t *ppos)
1220{
Al Viro496ad9a2013-01-23 17:07:38 -05001221 struct inode * inode = file_inode(file);
Eric W. Biederman99f89552006-06-26 00:25:55 -07001222 struct task_struct *task = get_proc_task(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001223 ssize_t length;
1224 char tmpbuf[TMPBUFLEN];
1225
Eric W. Biederman99f89552006-06-26 00:25:55 -07001226 if (!task)
1227 return -ESRCH;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001228 length = scnprintf(tmpbuf, TMPBUFLEN, "%u",
Eric W. Biedermane1760bd2012-09-10 22:39:43 -07001229 from_kuid(file->f_cred->user_ns,
1230 audit_get_loginuid(task)));
Eric W. Biederman99f89552006-06-26 00:25:55 -07001231 put_task_struct(task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001232 return simple_read_from_buffer(buf, count, ppos, tmpbuf, length);
1233}
1234
1235static ssize_t proc_loginuid_write(struct file * file, const char __user * buf,
1236 size_t count, loff_t *ppos)
1237{
Al Viro496ad9a2013-01-23 17:07:38 -05001238 struct inode * inode = file_inode(file);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001239 uid_t loginuid;
Eric W. Biedermane1760bd2012-09-10 22:39:43 -07001240 kuid_t kloginuid;
Alexey Dobriyan774636e2015-09-09 15:36:59 -07001241 int rv;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001242
Paul E. McKenney7dc52152010-02-22 17:04:52 -08001243 rcu_read_lock();
1244 if (current != pid_task(proc_pid(inode), PIDTYPE_PID)) {
1245 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001246 return -EPERM;
Paul E. McKenney7dc52152010-02-22 17:04:52 -08001247 }
1248 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001249
Linus Torvalds1da177e2005-04-16 15:20:36 -07001250 if (*ppos != 0) {
1251 /* No partial writes. */
1252 return -EINVAL;
1253 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001254
Alexey Dobriyan774636e2015-09-09 15:36:59 -07001255 rv = kstrtou32_from_user(buf, count, 10, &loginuid);
1256 if (rv < 0)
1257 return rv;
Eric Paris81407c82013-05-24 09:49:14 -04001258
1259 /* is userspace tring to explicitly UNSET the loginuid? */
1260 if (loginuid == AUDIT_UID_UNSET) {
1261 kloginuid = INVALID_UID;
1262 } else {
1263 kloginuid = make_kuid(file->f_cred->user_ns, loginuid);
Alexey Dobriyan774636e2015-09-09 15:36:59 -07001264 if (!uid_valid(kloginuid))
1265 return -EINVAL;
Eric W. Biedermane1760bd2012-09-10 22:39:43 -07001266 }
1267
Alexey Dobriyan774636e2015-09-09 15:36:59 -07001268 rv = audit_set_loginuid(kloginuid);
1269 if (rv < 0)
1270 return rv;
1271 return count;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001272}
1273
Arjan van de Ven00977a52007-02-12 00:55:34 -08001274static const struct file_operations proc_loginuid_operations = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001275 .read = proc_loginuid_read,
1276 .write = proc_loginuid_write,
Arnd Bergmann87df8422010-03-17 23:06:02 +01001277 .llseek = generic_file_llseek,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001278};
Eric Paris1e0bd752008-03-13 08:15:31 -04001279
1280static ssize_t proc_sessionid_read(struct file * file, char __user * buf,
1281 size_t count, loff_t *ppos)
1282{
Al Viro496ad9a2013-01-23 17:07:38 -05001283 struct inode * inode = file_inode(file);
Eric Paris1e0bd752008-03-13 08:15:31 -04001284 struct task_struct *task = get_proc_task(inode);
1285 ssize_t length;
1286 char tmpbuf[TMPBUFLEN];
1287
1288 if (!task)
1289 return -ESRCH;
1290 length = scnprintf(tmpbuf, TMPBUFLEN, "%u",
1291 audit_get_sessionid(task));
1292 put_task_struct(task);
1293 return simple_read_from_buffer(buf, count, ppos, tmpbuf, length);
1294}
1295
1296static const struct file_operations proc_sessionid_operations = {
1297 .read = proc_sessionid_read,
Arnd Bergmann87df8422010-03-17 23:06:02 +01001298 .llseek = generic_file_llseek,
Eric Paris1e0bd752008-03-13 08:15:31 -04001299};
Linus Torvalds1da177e2005-04-16 15:20:36 -07001300#endif
1301
Akinobu Mitaf4f154f2006-12-08 02:39:47 -08001302#ifdef CONFIG_FAULT_INJECTION
1303static ssize_t proc_fault_inject_read(struct file * file, char __user * buf,
1304 size_t count, loff_t *ppos)
1305{
Al Viro496ad9a2013-01-23 17:07:38 -05001306 struct task_struct *task = get_proc_task(file_inode(file));
Akinobu Mitaf4f154f2006-12-08 02:39:47 -08001307 char buffer[PROC_NUMBUF];
1308 size_t len;
1309 int make_it_fail;
Akinobu Mitaf4f154f2006-12-08 02:39:47 -08001310
1311 if (!task)
1312 return -ESRCH;
1313 make_it_fail = task->make_it_fail;
1314 put_task_struct(task);
1315
1316 len = snprintf(buffer, sizeof(buffer), "%i\n", make_it_fail);
Akinobu Mita0c28f282007-05-08 00:31:41 -07001317
1318 return simple_read_from_buffer(buf, count, ppos, buffer, len);
Akinobu Mitaf4f154f2006-12-08 02:39:47 -08001319}
1320
1321static ssize_t proc_fault_inject_write(struct file * file,
1322 const char __user * buf, size_t count, loff_t *ppos)
1323{
1324 struct task_struct *task;
Alexey Dobriyan774636e2015-09-09 15:36:59 -07001325 char buffer[PROC_NUMBUF];
Akinobu Mitaf4f154f2006-12-08 02:39:47 -08001326 int make_it_fail;
Alexey Dobriyan774636e2015-09-09 15:36:59 -07001327 int rv;
Akinobu Mitaf4f154f2006-12-08 02:39:47 -08001328
1329 if (!capable(CAP_SYS_RESOURCE))
1330 return -EPERM;
1331 memset(buffer, 0, sizeof(buffer));
1332 if (count > sizeof(buffer) - 1)
1333 count = sizeof(buffer) - 1;
1334 if (copy_from_user(buffer, buf, count))
1335 return -EFAULT;
Alexey Dobriyan774636e2015-09-09 15:36:59 -07001336 rv = kstrtoint(strstrip(buffer), 0, &make_it_fail);
1337 if (rv < 0)
1338 return rv;
Dave Jones16caed32014-04-07 15:39:15 -07001339 if (make_it_fail < 0 || make_it_fail > 1)
1340 return -EINVAL;
1341
Al Viro496ad9a2013-01-23 17:07:38 -05001342 task = get_proc_task(file_inode(file));
Akinobu Mitaf4f154f2006-12-08 02:39:47 -08001343 if (!task)
1344 return -ESRCH;
1345 task->make_it_fail = make_it_fail;
1346 put_task_struct(task);
Vincent Licba8aaf2009-09-22 16:45:38 -07001347
1348 return count;
Akinobu Mitaf4f154f2006-12-08 02:39:47 -08001349}
1350
Arjan van de Ven00977a52007-02-12 00:55:34 -08001351static const struct file_operations proc_fault_inject_operations = {
Akinobu Mitaf4f154f2006-12-08 02:39:47 -08001352 .read = proc_fault_inject_read,
1353 .write = proc_fault_inject_write,
Arnd Bergmann87df8422010-03-17 23:06:02 +01001354 .llseek = generic_file_llseek,
Akinobu Mitaf4f154f2006-12-08 02:39:47 -08001355};
1356#endif
1357
Arjan van de Ven97455122008-01-25 21:08:34 +01001358
Ingo Molnar43ae34c2007-07-09 18:52:00 +02001359#ifdef CONFIG_SCHED_DEBUG
1360/*
1361 * Print out various scheduling related per-task fields:
1362 */
1363static int sched_show(struct seq_file *m, void *v)
1364{
1365 struct inode *inode = m->private;
1366 struct task_struct *p;
1367
Ingo Molnar43ae34c2007-07-09 18:52:00 +02001368 p = get_proc_task(inode);
1369 if (!p)
1370 return -ESRCH;
1371 proc_sched_show_task(p, m);
1372
1373 put_task_struct(p);
1374
1375 return 0;
1376}
1377
1378static ssize_t
1379sched_write(struct file *file, const char __user *buf,
1380 size_t count, loff_t *offset)
1381{
Al Viro496ad9a2013-01-23 17:07:38 -05001382 struct inode *inode = file_inode(file);
Ingo Molnar43ae34c2007-07-09 18:52:00 +02001383 struct task_struct *p;
1384
Ingo Molnar43ae34c2007-07-09 18:52:00 +02001385 p = get_proc_task(inode);
1386 if (!p)
1387 return -ESRCH;
1388 proc_sched_set_task(p);
1389
1390 put_task_struct(p);
1391
1392 return count;
1393}
1394
1395static int sched_open(struct inode *inode, struct file *filp)
1396{
Jovi Zhangc6a34052011-01-12 17:00:34 -08001397 return single_open(filp, sched_show, inode);
Ingo Molnar43ae34c2007-07-09 18:52:00 +02001398}
1399
1400static const struct file_operations proc_pid_sched_operations = {
1401 .open = sched_open,
1402 .read = seq_read,
1403 .write = sched_write,
1404 .llseek = seq_lseek,
Alexey Dobriyan5ea473a2007-07-31 00:38:50 -07001405 .release = single_release,
Ingo Molnar43ae34c2007-07-09 18:52:00 +02001406};
1407
1408#endif
1409
Mike Galbraith5091faa2010-11-30 14:18:03 +01001410#ifdef CONFIG_SCHED_AUTOGROUP
1411/*
1412 * Print out autogroup related information:
1413 */
1414static int sched_autogroup_show(struct seq_file *m, void *v)
1415{
1416 struct inode *inode = m->private;
1417 struct task_struct *p;
1418
1419 p = get_proc_task(inode);
1420 if (!p)
1421 return -ESRCH;
1422 proc_sched_autogroup_show_task(p, m);
1423
1424 put_task_struct(p);
1425
1426 return 0;
1427}
1428
1429static ssize_t
1430sched_autogroup_write(struct file *file, const char __user *buf,
1431 size_t count, loff_t *offset)
1432{
Al Viro496ad9a2013-01-23 17:07:38 -05001433 struct inode *inode = file_inode(file);
Mike Galbraith5091faa2010-11-30 14:18:03 +01001434 struct task_struct *p;
1435 char buffer[PROC_NUMBUF];
Alexey Dobriyan0a8cb8e2011-05-26 16:25:50 -07001436 int nice;
Mike Galbraith5091faa2010-11-30 14:18:03 +01001437 int err;
1438
1439 memset(buffer, 0, sizeof(buffer));
1440 if (count > sizeof(buffer) - 1)
1441 count = sizeof(buffer) - 1;
1442 if (copy_from_user(buffer, buf, count))
1443 return -EFAULT;
1444
Alexey Dobriyan0a8cb8e2011-05-26 16:25:50 -07001445 err = kstrtoint(strstrip(buffer), 0, &nice);
1446 if (err < 0)
1447 return err;
Mike Galbraith5091faa2010-11-30 14:18:03 +01001448
1449 p = get_proc_task(inode);
1450 if (!p)
1451 return -ESRCH;
1452
Hiroshi Shimamoto2e5b5b32012-02-23 17:41:27 +09001453 err = proc_sched_autogroup_set_nice(p, nice);
Mike Galbraith5091faa2010-11-30 14:18:03 +01001454 if (err)
1455 count = err;
1456
1457 put_task_struct(p);
1458
1459 return count;
1460}
1461
1462static int sched_autogroup_open(struct inode *inode, struct file *filp)
1463{
1464 int ret;
1465
1466 ret = single_open(filp, sched_autogroup_show, NULL);
1467 if (!ret) {
1468 struct seq_file *m = filp->private_data;
1469
1470 m->private = inode;
1471 }
1472 return ret;
1473}
1474
1475static const struct file_operations proc_pid_sched_autogroup_operations = {
1476 .open = sched_autogroup_open,
1477 .read = seq_read,
1478 .write = sched_autogroup_write,
1479 .llseek = seq_lseek,
1480 .release = single_release,
1481};
1482
1483#endif /* CONFIG_SCHED_AUTOGROUP */
1484
john stultz4614a696b2009-12-14 18:00:05 -08001485static ssize_t comm_write(struct file *file, const char __user *buf,
1486 size_t count, loff_t *offset)
1487{
Al Viro496ad9a2013-01-23 17:07:38 -05001488 struct inode *inode = file_inode(file);
john stultz4614a696b2009-12-14 18:00:05 -08001489 struct task_struct *p;
1490 char buffer[TASK_COMM_LEN];
David Rientjes830e0fc2013-04-30 15:28:18 -07001491 const size_t maxlen = sizeof(buffer) - 1;
john stultz4614a696b2009-12-14 18:00:05 -08001492
1493 memset(buffer, 0, sizeof(buffer));
David Rientjes830e0fc2013-04-30 15:28:18 -07001494 if (copy_from_user(buffer, buf, count > maxlen ? maxlen : count))
john stultz4614a696b2009-12-14 18:00:05 -08001495 return -EFAULT;
1496
1497 p = get_proc_task(inode);
1498 if (!p)
1499 return -ESRCH;
1500
1501 if (same_thread_group(current, p))
1502 set_task_comm(p, buffer);
1503 else
1504 count = -EINVAL;
1505
1506 put_task_struct(p);
1507
1508 return count;
1509}
1510
1511static int comm_show(struct seq_file *m, void *v)
1512{
1513 struct inode *inode = m->private;
1514 struct task_struct *p;
1515
1516 p = get_proc_task(inode);
1517 if (!p)
1518 return -ESRCH;
1519
1520 task_lock(p);
1521 seq_printf(m, "%s\n", p->comm);
1522 task_unlock(p);
1523
1524 put_task_struct(p);
1525
1526 return 0;
1527}
1528
1529static int comm_open(struct inode *inode, struct file *filp)
1530{
Jovi Zhangc6a34052011-01-12 17:00:34 -08001531 return single_open(filp, comm_show, inode);
john stultz4614a696b2009-12-14 18:00:05 -08001532}
1533
1534static const struct file_operations proc_pid_set_comm_operations = {
1535 .open = comm_open,
1536 .read = seq_read,
1537 .write = comm_write,
1538 .llseek = seq_lseek,
1539 .release = single_release,
1540};
1541
Cyrill Gorcunov7773fbc2012-01-10 15:11:20 -08001542static int proc_exe_link(struct dentry *dentry, struct path *exe_path)
Matt Helsley925d1c42008-04-29 01:01:36 -07001543{
1544 struct task_struct *task;
Matt Helsley925d1c42008-04-29 01:01:36 -07001545 struct file *exe_file;
1546
David Howells2b0143b2015-03-17 22:25:59 +00001547 task = get_proc_task(d_inode(dentry));
Matt Helsley925d1c42008-04-29 01:01:36 -07001548 if (!task)
1549 return -ENOENT;
Mateusz Guzikcd81a9172016-08-23 16:20:38 +02001550 exe_file = get_task_exe_file(task);
Matt Helsley925d1c42008-04-29 01:01:36 -07001551 put_task_struct(task);
Matt Helsley925d1c42008-04-29 01:01:36 -07001552 if (exe_file) {
1553 *exe_path = exe_file->f_path;
1554 path_get(&exe_file->f_path);
1555 fput(exe_file);
1556 return 0;
1557 } else
1558 return -ENOENT;
1559}
1560
Al Viro6b255392015-11-17 10:20:54 -05001561static const char *proc_pid_get_link(struct dentry *dentry,
Al Virofceef392015-12-29 15:58:39 -05001562 struct inode *inode,
1563 struct delayed_call *done)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001564{
Christoph Hellwig408ef012012-06-18 10:47:03 -04001565 struct path path;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001566 int error = -EACCES;
1567
Al Viro6b255392015-11-17 10:20:54 -05001568 if (!dentry)
1569 return ERR_PTR(-ECHILD);
1570
Eric W. Biederman778c1142006-06-26 00:25:58 -07001571 /* Are we allowed to snoop on the tasks file descriptors? */
1572 if (!proc_fd_access_allowed(inode))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001573 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001574
Christoph Hellwig408ef012012-06-18 10:47:03 -04001575 error = PROC_I(inode)->op.proc_get_link(dentry, &path);
1576 if (error)
1577 goto out;
1578
Al Viro6e77137b2015-05-02 13:37:52 -04001579 nd_jump_link(&path);
Christoph Hellwig408ef012012-06-18 10:47:03 -04001580 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001581out:
Al Viro008b1502005-08-20 00:17:39 +01001582 return ERR_PTR(error);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001583}
1584
Jan Blunck3dcd25f2008-02-14 19:38:35 -08001585static int do_proc_readlink(struct path *path, char __user *buffer, int buflen)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001586{
Mel Gormane12ba742007-10-16 01:25:52 -07001587 char *tmp = (char*)__get_free_page(GFP_TEMPORARY);
Jan Blunck3dcd25f2008-02-14 19:38:35 -08001588 char *pathname;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001589 int len;
1590
1591 if (!tmp)
1592 return -ENOMEM;
Akinobu Mita0c28f282007-05-08 00:31:41 -07001593
Eric W. Biederman7b2a69b2010-12-05 15:51:21 -08001594 pathname = d_path(path, tmp, PAGE_SIZE);
Jan Blunck3dcd25f2008-02-14 19:38:35 -08001595 len = PTR_ERR(pathname);
1596 if (IS_ERR(pathname))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001597 goto out;
Jan Blunck3dcd25f2008-02-14 19:38:35 -08001598 len = tmp + PAGE_SIZE - 1 - pathname;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001599
1600 if (len > buflen)
1601 len = buflen;
Jan Blunck3dcd25f2008-02-14 19:38:35 -08001602 if (copy_to_user(buffer, pathname, len))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001603 len = -EFAULT;
1604 out:
1605 free_page((unsigned long)tmp);
1606 return len;
1607}
1608
1609static int proc_pid_readlink(struct dentry * dentry, char __user * buffer, int buflen)
1610{
1611 int error = -EACCES;
David Howells2b0143b2015-03-17 22:25:59 +00001612 struct inode *inode = d_inode(dentry);
Jan Blunck3dcd25f2008-02-14 19:38:35 -08001613 struct path path;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001614
Eric W. Biederman778c1142006-06-26 00:25:58 -07001615 /* Are we allowed to snoop on the tasks file descriptors? */
1616 if (!proc_fd_access_allowed(inode))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001617 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001618
Cyrill Gorcunov7773fbc2012-01-10 15:11:20 -08001619 error = PROC_I(inode)->op.proc_get_link(dentry, &path);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001620 if (error)
1621 goto out;
1622
Jan Blunck3dcd25f2008-02-14 19:38:35 -08001623 error = do_proc_readlink(&path, buffer, buflen);
1624 path_put(&path);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001625out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001626 return error;
1627}
1628
Cyrill Gorcunovfaf60af2012-08-23 14:43:24 +04001629const struct inode_operations proc_pid_link_inode_operations = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001630 .readlink = proc_pid_readlink,
Al Viro6b255392015-11-17 10:20:54 -05001631 .get_link = proc_pid_get_link,
Linus Torvalds6d76fa52006-07-15 12:26:45 -07001632 .setattr = proc_setattr,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001633};
1634
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001635
1636/* building an inode */
1637
Eric W. Biederman68eb94f2017-01-03 10:23:11 +13001638void task_dump_owner(struct task_struct *task, mode_t mode,
1639 kuid_t *ruid, kgid_t *rgid)
1640{
1641 /* Depending on the state of dumpable compute who should own a
1642 * proc file for a task.
1643 */
1644 const struct cred *cred;
1645 kuid_t uid;
1646 kgid_t gid;
1647
1648 /* Default to the tasks effective ownership */
1649 rcu_read_lock();
1650 cred = __task_cred(task);
1651 uid = cred->euid;
1652 gid = cred->egid;
1653 rcu_read_unlock();
1654
1655 /*
1656 * Before the /proc/pid/status file was created the only way to read
1657 * the effective uid of a /process was to stat /proc/pid. Reading
1658 * /proc/pid/status is slow enough that procps and other packages
1659 * kept stating /proc/pid. To keep the rules in /proc simple I have
1660 * made this apply to all per process world readable and executable
1661 * directories.
1662 */
1663 if (mode != (S_IFDIR|S_IRUGO|S_IXUGO)) {
1664 struct mm_struct *mm;
1665 task_lock(task);
1666 mm = task->mm;
1667 /* Make non-dumpable tasks owned by some root */
1668 if (mm) {
1669 if (get_dumpable(mm) != SUID_DUMP_USER) {
1670 struct user_namespace *user_ns = mm->user_ns;
1671
1672 uid = make_kuid(user_ns, 0);
1673 if (!uid_valid(uid))
1674 uid = GLOBAL_ROOT_UID;
1675
1676 gid = make_kgid(user_ns, 0);
1677 if (!gid_valid(gid))
1678 gid = GLOBAL_ROOT_GID;
1679 }
1680 } else {
1681 uid = GLOBAL_ROOT_UID;
1682 gid = GLOBAL_ROOT_GID;
1683 }
1684 task_unlock(task);
1685 }
1686 *ruid = uid;
1687 *rgid = gid;
1688}
1689
Andreas Gruenbacherdb978da2016-11-10 22:18:28 +01001690struct inode *proc_pid_make_inode(struct super_block * sb,
1691 struct task_struct *task, umode_t mode)
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001692{
1693 struct inode * inode;
1694 struct proc_inode *ei;
1695
1696 /* We need a new inode */
1697
1698 inode = new_inode(sb);
1699 if (!inode)
1700 goto out;
1701
1702 /* Common stuff */
1703 ei = PROC_I(inode);
Andreas Gruenbacherdb978da2016-11-10 22:18:28 +01001704 inode->i_mode = mode;
Christoph Hellwig85fe4022010-10-23 11:19:54 -04001705 inode->i_ino = get_next_ino();
Deepa Dinamani078cd822016-09-14 07:48:04 -07001706 inode->i_mtime = inode->i_atime = inode->i_ctime = current_time(inode);
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001707 inode->i_op = &proc_def_inode_operations;
1708
1709 /*
1710 * grab the reference to task.
1711 */
Oleg Nesterov1a657f782006-10-02 02:18:59 -07001712 ei->pid = get_task_pid(task, PIDTYPE_PID);
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001713 if (!ei->pid)
1714 goto out_unlock;
1715
Eric W. Biederman68eb94f2017-01-03 10:23:11 +13001716 task_dump_owner(task, 0, &inode->i_uid, &inode->i_gid);
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001717 security_task_to_inode(task, inode);
1718
1719out:
1720 return inode;
1721
1722out_unlock:
1723 iput(inode);
1724 return NULL;
1725}
1726
David Howellsa528d352017-01-31 16:46:22 +00001727int pid_getattr(const struct path *path, struct kstat *stat,
1728 u32 request_mask, unsigned int query_flags)
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001729{
David Howellsa528d352017-01-31 16:46:22 +00001730 struct inode *inode = d_inode(path->dentry);
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001731 struct task_struct *task;
David Howellsa528d352017-01-31 16:46:22 +00001732 struct pid_namespace *pid = path->dentry->d_sb->s_fs_info;
David Howellsc69e8d92008-11-14 10:39:19 +11001733
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001734 generic_fillattr(inode, stat);
1735
1736 rcu_read_lock();
Eric W. Biedermandcb0f222012-02-09 08:48:21 -08001737 stat->uid = GLOBAL_ROOT_UID;
1738 stat->gid = GLOBAL_ROOT_GID;
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001739 task = pid_task(proc_pid(inode), PIDTYPE_PID);
1740 if (task) {
Lafcadio Wluiki796f5712017-02-24 15:00:23 -08001741 if (!has_pid_permissions(pid, task, HIDEPID_INVISIBLE)) {
Vasiliy Kulikov04996802012-01-10 15:11:31 -08001742 rcu_read_unlock();
1743 /*
1744 * This doesn't prevent learning whether PID exists,
1745 * it only makes getattr() consistent with readdir().
1746 */
1747 return -ENOENT;
1748 }
Eric W. Biederman68eb94f2017-01-03 10:23:11 +13001749 task_dump_owner(task, inode->i_mode, &stat->uid, &stat->gid);
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001750 }
1751 rcu_read_unlock();
1752 return 0;
1753}
1754
1755/* dentry stuff */
1756
1757/*
1758 * Exceptional case: normally we are not allowed to unhash a busy
1759 * directory. In this case, however, we can do it - no aliasing problems
1760 * due to the way we treat inodes.
1761 *
1762 * Rewrite the inode's ownerships here because the owning task may have
1763 * performed a setuid(), etc.
1764 *
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001765 */
Al Viro0b728e12012-06-10 16:03:43 -04001766int pid_revalidate(struct dentry *dentry, unsigned int flags)
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001767{
Nick Piggin34286d62011-01-07 17:49:57 +11001768 struct inode *inode;
1769 struct task_struct *task;
David Howellsc69e8d92008-11-14 10:39:19 +11001770
Al Viro0b728e12012-06-10 16:03:43 -04001771 if (flags & LOOKUP_RCU)
Nick Piggin34286d62011-01-07 17:49:57 +11001772 return -ECHILD;
1773
David Howells2b0143b2015-03-17 22:25:59 +00001774 inode = d_inode(dentry);
Nick Piggin34286d62011-01-07 17:49:57 +11001775 task = get_proc_task(inode);
1776
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001777 if (task) {
Eric W. Biederman68eb94f2017-01-03 10:23:11 +13001778 task_dump_owner(task, inode->i_mode, &inode->i_uid, &inode->i_gid);
1779
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001780 inode->i_mode &= ~(S_ISUID | S_ISGID);
1781 security_task_to_inode(task, inode);
1782 put_task_struct(task);
1783 return 1;
1784 }
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001785 return 0;
1786}
1787
Oleg Nesterovd855a4b2014-01-23 15:55:39 -08001788static inline bool proc_inode_is_dead(struct inode *inode)
1789{
1790 return !proc_pid(inode)->tasks[PIDTYPE_PID].first;
1791}
1792
David Howells1dd704b2013-04-12 01:08:50 +01001793int pid_delete_dentry(const struct dentry *dentry)
1794{
1795 /* Is the task we represent dead?
1796 * If so, then don't put the dentry on the lru list,
1797 * kill it immediately.
1798 */
David Howells2b0143b2015-03-17 22:25:59 +00001799 return proc_inode_is_dead(d_inode(dentry));
David Howells1dd704b2013-04-12 01:08:50 +01001800}
1801
Eric W. Biederman6b4e3062010-03-07 16:41:34 -08001802const struct dentry_operations pid_dentry_operations =
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001803{
1804 .d_revalidate = pid_revalidate,
1805 .d_delete = pid_delete_dentry,
1806};
1807
1808/* Lookups */
1809
Eric W. Biederman1c0d04c2006-10-02 02:18:57 -07001810/*
1811 * Fill a directory entry.
1812 *
1813 * If possible create the dcache entry and derive our inode number and
1814 * file type from dcache entry.
1815 *
1816 * Since all of the proc inode numbers are dynamically generated, the inode
1817 * numbers do not exist until the inode is cache. This means creating the
1818 * the dcache entry in readdir is necessary to keep the inode numbers
1819 * reported by readdir in sync with the inode numbers reported
1820 * by stat.
1821 */
Al Virof0c3b502013-05-16 12:07:31 -04001822bool proc_fill_cache(struct file *file, struct dir_context *ctx,
Eric W. Biederman6b4e3062010-03-07 16:41:34 -08001823 const char *name, int len,
Eric Dumazetc5141e62007-05-08 00:26:15 -07001824 instantiate_t instantiate, struct task_struct *task, const void *ptr)
Eric W. Biederman61a28782006-10-02 02:18:49 -07001825{
Al Virof0c3b502013-05-16 12:07:31 -04001826 struct dentry *child, *dir = file->f_path.dentry;
Al Viro1df98b82013-06-15 11:33:10 +04001827 struct qstr qname = QSTR_INIT(name, len);
Eric W. Biederman61a28782006-10-02 02:18:49 -07001828 struct inode *inode;
Al Viro1df98b82013-06-15 11:33:10 +04001829 unsigned type;
1830 ino_t ino;
Eric W. Biederman61a28782006-10-02 02:18:49 -07001831
Al Viro1df98b82013-06-15 11:33:10 +04001832 child = d_hash_and_lookup(dir, &qname);
Eric W. Biederman61a28782006-10-02 02:18:49 -07001833 if (!child) {
Al Viro37817642016-04-20 16:31:31 -04001834 DECLARE_WAIT_QUEUE_HEAD_ONSTACK(wq);
1835 child = d_alloc_parallel(dir, &qname, &wq);
1836 if (IS_ERR(child))
Al Viro1df98b82013-06-15 11:33:10 +04001837 goto end_instantiate;
Al Viro37817642016-04-20 16:31:31 -04001838 if (d_in_lookup(child)) {
1839 int err = instantiate(d_inode(dir), child, task, ptr);
1840 d_lookup_done(child);
1841 if (err < 0) {
1842 dput(child);
1843 goto end_instantiate;
1844 }
Eric W. Biederman61a28782006-10-02 02:18:49 -07001845 }
1846 }
David Howells2b0143b2015-03-17 22:25:59 +00001847 inode = d_inode(child);
Al Viro147ce692013-06-15 10:26:35 +04001848 ino = inode->i_ino;
1849 type = inode->i_mode >> 12;
Eric W. Biederman61a28782006-10-02 02:18:49 -07001850 dput(child);
Al Virof0c3b502013-05-16 12:07:31 -04001851 return dir_emit(ctx, name, len, ino, type);
Al Viro1df98b82013-06-15 11:33:10 +04001852
1853end_instantiate:
1854 return dir_emit(ctx, name, len, 1, DT_UNKNOWN);
Eric W. Biederman61a28782006-10-02 02:18:49 -07001855}
1856
Pavel Emelyanov640708a2012-01-10 15:11:23 -08001857/*
1858 * dname_to_vma_addr - maps a dentry name into two unsigned longs
1859 * which represent vma start and end addresses.
1860 */
1861static int dname_to_vma_addr(struct dentry *dentry,
1862 unsigned long *start, unsigned long *end)
1863{
1864 if (sscanf(dentry->d_name.name, "%lx-%lx", start, end) != 2)
1865 return -EINVAL;
1866
1867 return 0;
1868}
1869
Al Viro0b728e12012-06-10 16:03:43 -04001870static int map_files_d_revalidate(struct dentry *dentry, unsigned int flags)
Pavel Emelyanov640708a2012-01-10 15:11:23 -08001871{
1872 unsigned long vm_start, vm_end;
1873 bool exact_vma_exists = false;
1874 struct mm_struct *mm = NULL;
1875 struct task_struct *task;
Pavel Emelyanov640708a2012-01-10 15:11:23 -08001876 struct inode *inode;
1877 int status = 0;
1878
Al Viro0b728e12012-06-10 16:03:43 -04001879 if (flags & LOOKUP_RCU)
Pavel Emelyanov640708a2012-01-10 15:11:23 -08001880 return -ECHILD;
1881
David Howells2b0143b2015-03-17 22:25:59 +00001882 inode = d_inode(dentry);
Pavel Emelyanov640708a2012-01-10 15:11:23 -08001883 task = get_proc_task(inode);
1884 if (!task)
1885 goto out_notask;
1886
Jann Horncaaee622016-01-20 15:00:04 -08001887 mm = mm_access(task, PTRACE_MODE_READ_FSCREDS);
Cong Wang2344bec2012-05-31 16:26:18 -07001888 if (IS_ERR_OR_NULL(mm))
Pavel Emelyanov640708a2012-01-10 15:11:23 -08001889 goto out;
1890
1891 if (!dname_to_vma_addr(dentry, &vm_start, &vm_end)) {
1892 down_read(&mm->mmap_sem);
1893 exact_vma_exists = !!find_exact_vma(mm, vm_start, vm_end);
1894 up_read(&mm->mmap_sem);
1895 }
1896
1897 mmput(mm);
1898
1899 if (exact_vma_exists) {
Eric W. Biederman68eb94f2017-01-03 10:23:11 +13001900 task_dump_owner(task, 0, &inode->i_uid, &inode->i_gid);
1901
Pavel Emelyanov640708a2012-01-10 15:11:23 -08001902 security_task_to_inode(task, inode);
1903 status = 1;
1904 }
1905
1906out:
1907 put_task_struct(task);
1908
1909out_notask:
Pavel Emelyanov640708a2012-01-10 15:11:23 -08001910 return status;
1911}
1912
1913static const struct dentry_operations tid_map_files_dentry_operations = {
1914 .d_revalidate = map_files_d_revalidate,
1915 .d_delete = pid_delete_dentry,
1916};
1917
Al Viro6b255392015-11-17 10:20:54 -05001918static int map_files_get_link(struct dentry *dentry, struct path *path)
Pavel Emelyanov640708a2012-01-10 15:11:23 -08001919{
1920 unsigned long vm_start, vm_end;
1921 struct vm_area_struct *vma;
1922 struct task_struct *task;
1923 struct mm_struct *mm;
1924 int rc;
1925
1926 rc = -ENOENT;
David Howells2b0143b2015-03-17 22:25:59 +00001927 task = get_proc_task(d_inode(dentry));
Pavel Emelyanov640708a2012-01-10 15:11:23 -08001928 if (!task)
1929 goto out;
1930
1931 mm = get_task_mm(task);
1932 put_task_struct(task);
1933 if (!mm)
1934 goto out;
1935
1936 rc = dname_to_vma_addr(dentry, &vm_start, &vm_end);
1937 if (rc)
1938 goto out_mmput;
1939
Artem Fetishev70335ab2014-03-10 15:49:45 -07001940 rc = -ENOENT;
Pavel Emelyanov640708a2012-01-10 15:11:23 -08001941 down_read(&mm->mmap_sem);
1942 vma = find_exact_vma(mm, vm_start, vm_end);
1943 if (vma && vma->vm_file) {
1944 *path = vma->vm_file->f_path;
1945 path_get(path);
1946 rc = 0;
1947 }
1948 up_read(&mm->mmap_sem);
1949
1950out_mmput:
1951 mmput(mm);
1952out:
1953 return rc;
1954}
1955
1956struct map_files_info {
Al Viro7b540d02012-08-27 14:55:26 -04001957 fmode_t mode;
Alexey Dobriyan9a87fe02016-12-12 16:45:11 -08001958 unsigned int len;
Pavel Emelyanov640708a2012-01-10 15:11:23 -08001959 unsigned char name[4*sizeof(long)+2]; /* max: %lx-%lx\0 */
1960};
1961
Calvin Owensbdb4d102015-09-09 15:35:54 -07001962/*
1963 * Only allow CAP_SYS_ADMIN to follow the links, due to concerns about how the
1964 * symlinks may be used to bypass permissions on ancestor directories in the
1965 * path to the file in question.
1966 */
1967static const char *
Al Viro6b255392015-11-17 10:20:54 -05001968proc_map_files_get_link(struct dentry *dentry,
Al Virofceef392015-12-29 15:58:39 -05001969 struct inode *inode,
1970 struct delayed_call *done)
Calvin Owensbdb4d102015-09-09 15:35:54 -07001971{
1972 if (!capable(CAP_SYS_ADMIN))
1973 return ERR_PTR(-EPERM);
1974
Al Virofceef392015-12-29 15:58:39 -05001975 return proc_pid_get_link(dentry, inode, done);
Calvin Owensbdb4d102015-09-09 15:35:54 -07001976}
1977
1978/*
Al Viro6b255392015-11-17 10:20:54 -05001979 * Identical to proc_pid_link_inode_operations except for get_link()
Calvin Owensbdb4d102015-09-09 15:35:54 -07001980 */
1981static const struct inode_operations proc_map_files_link_inode_operations = {
1982 .readlink = proc_pid_readlink,
Al Viro6b255392015-11-17 10:20:54 -05001983 .get_link = proc_map_files_get_link,
Calvin Owensbdb4d102015-09-09 15:35:54 -07001984 .setattr = proc_setattr,
1985};
1986
Al Viroc52a47a2013-06-15 11:15:20 +04001987static int
Pavel Emelyanov640708a2012-01-10 15:11:23 -08001988proc_map_files_instantiate(struct inode *dir, struct dentry *dentry,
1989 struct task_struct *task, const void *ptr)
1990{
Al Viro7b540d02012-08-27 14:55:26 -04001991 fmode_t mode = (fmode_t)(unsigned long)ptr;
Pavel Emelyanov640708a2012-01-10 15:11:23 -08001992 struct proc_inode *ei;
1993 struct inode *inode;
1994
Andreas Gruenbacherdb978da2016-11-10 22:18:28 +01001995 inode = proc_pid_make_inode(dir->i_sb, task, S_IFLNK |
1996 ((mode & FMODE_READ ) ? S_IRUSR : 0) |
1997 ((mode & FMODE_WRITE) ? S_IWUSR : 0));
Pavel Emelyanov640708a2012-01-10 15:11:23 -08001998 if (!inode)
Al Viroc52a47a2013-06-15 11:15:20 +04001999 return -ENOENT;
Pavel Emelyanov640708a2012-01-10 15:11:23 -08002000
2001 ei = PROC_I(inode);
Al Viro6b255392015-11-17 10:20:54 -05002002 ei->op.proc_get_link = map_files_get_link;
Pavel Emelyanov640708a2012-01-10 15:11:23 -08002003
Calvin Owensbdb4d102015-09-09 15:35:54 -07002004 inode->i_op = &proc_map_files_link_inode_operations;
Pavel Emelyanov640708a2012-01-10 15:11:23 -08002005 inode->i_size = 64;
Pavel Emelyanov640708a2012-01-10 15:11:23 -08002006
2007 d_set_d_op(dentry, &tid_map_files_dentry_operations);
2008 d_add(dentry, inode);
2009
Al Viroc52a47a2013-06-15 11:15:20 +04002010 return 0;
Pavel Emelyanov640708a2012-01-10 15:11:23 -08002011}
2012
2013static struct dentry *proc_map_files_lookup(struct inode *dir,
Al Viro00cd8dd2012-06-10 17:13:09 -04002014 struct dentry *dentry, unsigned int flags)
Pavel Emelyanov640708a2012-01-10 15:11:23 -08002015{
2016 unsigned long vm_start, vm_end;
2017 struct vm_area_struct *vma;
2018 struct task_struct *task;
Al Viroc52a47a2013-06-15 11:15:20 +04002019 int result;
Pavel Emelyanov640708a2012-01-10 15:11:23 -08002020 struct mm_struct *mm;
2021
Al Viroc52a47a2013-06-15 11:15:20 +04002022 result = -ENOENT;
Pavel Emelyanov640708a2012-01-10 15:11:23 -08002023 task = get_proc_task(dir);
2024 if (!task)
2025 goto out;
2026
Al Viroc52a47a2013-06-15 11:15:20 +04002027 result = -EACCES;
Jann Horncaaee622016-01-20 15:00:04 -08002028 if (!ptrace_may_access(task, PTRACE_MODE_READ_FSCREDS))
Pavel Emelyanov640708a2012-01-10 15:11:23 -08002029 goto out_put_task;
2030
Al Viroc52a47a2013-06-15 11:15:20 +04002031 result = -ENOENT;
Pavel Emelyanov640708a2012-01-10 15:11:23 -08002032 if (dname_to_vma_addr(dentry, &vm_start, &vm_end))
Cyrill Gorcunoveb94cd92012-05-17 17:03:25 -07002033 goto out_put_task;
Pavel Emelyanov640708a2012-01-10 15:11:23 -08002034
2035 mm = get_task_mm(task);
2036 if (!mm)
Cyrill Gorcunoveb94cd92012-05-17 17:03:25 -07002037 goto out_put_task;
Pavel Emelyanov640708a2012-01-10 15:11:23 -08002038
2039 down_read(&mm->mmap_sem);
2040 vma = find_exact_vma(mm, vm_start, vm_end);
2041 if (!vma)
2042 goto out_no_vma;
2043
Stanislav Kinsbursky05f56482012-11-26 16:29:42 -08002044 if (vma->vm_file)
2045 result = proc_map_files_instantiate(dir, dentry, task,
2046 (void *)(unsigned long)vma->vm_file->f_mode);
Pavel Emelyanov640708a2012-01-10 15:11:23 -08002047
2048out_no_vma:
2049 up_read(&mm->mmap_sem);
2050 mmput(mm);
Pavel Emelyanov640708a2012-01-10 15:11:23 -08002051out_put_task:
2052 put_task_struct(task);
2053out:
Al Viroc52a47a2013-06-15 11:15:20 +04002054 return ERR_PTR(result);
Pavel Emelyanov640708a2012-01-10 15:11:23 -08002055}
2056
2057static const struct inode_operations proc_map_files_inode_operations = {
2058 .lookup = proc_map_files_lookup,
2059 .permission = proc_fd_permission,
2060 .setattr = proc_setattr,
2061};
2062
2063static int
Al Virof0c3b502013-05-16 12:07:31 -04002064proc_map_files_readdir(struct file *file, struct dir_context *ctx)
Pavel Emelyanov640708a2012-01-10 15:11:23 -08002065{
Pavel Emelyanov640708a2012-01-10 15:11:23 -08002066 struct vm_area_struct *vma;
2067 struct task_struct *task;
2068 struct mm_struct *mm;
Al Virof0c3b502013-05-16 12:07:31 -04002069 unsigned long nr_files, pos, i;
2070 struct flex_array *fa = NULL;
2071 struct map_files_info info;
2072 struct map_files_info *p;
Pavel Emelyanov640708a2012-01-10 15:11:23 -08002073 int ret;
2074
Pavel Emelyanov640708a2012-01-10 15:11:23 -08002075 ret = -ENOENT;
Al Virof0c3b502013-05-16 12:07:31 -04002076 task = get_proc_task(file_inode(file));
Pavel Emelyanov640708a2012-01-10 15:11:23 -08002077 if (!task)
2078 goto out;
2079
2080 ret = -EACCES;
Jann Horncaaee622016-01-20 15:00:04 -08002081 if (!ptrace_may_access(task, PTRACE_MODE_READ_FSCREDS))
Pavel Emelyanov640708a2012-01-10 15:11:23 -08002082 goto out_put_task;
2083
2084 ret = 0;
Al Virof0c3b502013-05-16 12:07:31 -04002085 if (!dir_emit_dots(file, ctx))
2086 goto out_put_task;
Pavel Emelyanov640708a2012-01-10 15:11:23 -08002087
Al Virof0c3b502013-05-16 12:07:31 -04002088 mm = get_task_mm(task);
2089 if (!mm)
2090 goto out_put_task;
2091 down_read(&mm->mmap_sem);
Pavel Emelyanov640708a2012-01-10 15:11:23 -08002092
Al Virof0c3b502013-05-16 12:07:31 -04002093 nr_files = 0;
Pavel Emelyanov640708a2012-01-10 15:11:23 -08002094
Al Virof0c3b502013-05-16 12:07:31 -04002095 /*
2096 * We need two passes here:
2097 *
2098 * 1) Collect vmas of mapped files with mmap_sem taken
2099 * 2) Release mmap_sem and instantiate entries
2100 *
2101 * otherwise we get lockdep complained, since filldir()
2102 * routine might require mmap_sem taken in might_fault().
2103 */
Pavel Emelyanov640708a2012-01-10 15:11:23 -08002104
Al Virof0c3b502013-05-16 12:07:31 -04002105 for (vma = mm->mmap, pos = 2; vma; vma = vma->vm_next) {
2106 if (vma->vm_file && ++pos > ctx->pos)
2107 nr_files++;
Pavel Emelyanov640708a2012-01-10 15:11:23 -08002108 }
Al Virof0c3b502013-05-16 12:07:31 -04002109
2110 if (nr_files) {
2111 fa = flex_array_alloc(sizeof(info), nr_files,
2112 GFP_KERNEL);
2113 if (!fa || flex_array_prealloc(fa, 0, nr_files,
2114 GFP_KERNEL)) {
2115 ret = -ENOMEM;
2116 if (fa)
2117 flex_array_free(fa);
2118 up_read(&mm->mmap_sem);
2119 mmput(mm);
2120 goto out_put_task;
2121 }
2122 for (i = 0, vma = mm->mmap, pos = 2; vma;
2123 vma = vma->vm_next) {
2124 if (!vma->vm_file)
2125 continue;
2126 if (++pos <= ctx->pos)
2127 continue;
2128
2129 info.mode = vma->vm_file->f_mode;
2130 info.len = snprintf(info.name,
2131 sizeof(info.name), "%lx-%lx",
2132 vma->vm_start, vma->vm_end);
2133 if (flex_array_put(fa, i++, &info, GFP_KERNEL))
2134 BUG();
2135 }
Pavel Emelyanov640708a2012-01-10 15:11:23 -08002136 }
Al Virof0c3b502013-05-16 12:07:31 -04002137 up_read(&mm->mmap_sem);
2138
2139 for (i = 0; i < nr_files; i++) {
2140 p = flex_array_get(fa, i);
2141 if (!proc_fill_cache(file, ctx,
2142 p->name, p->len,
2143 proc_map_files_instantiate,
2144 task,
2145 (void *)(unsigned long)p->mode))
2146 break;
2147 ctx->pos++;
2148 }
2149 if (fa)
2150 flex_array_free(fa);
2151 mmput(mm);
Pavel Emelyanov640708a2012-01-10 15:11:23 -08002152
Pavel Emelyanov640708a2012-01-10 15:11:23 -08002153out_put_task:
2154 put_task_struct(task);
2155out:
2156 return ret;
2157}
2158
2159static const struct file_operations proc_map_files_operations = {
2160 .read = generic_read_dir,
Al Virof50752e2016-04-20 17:13:54 -04002161 .iterate_shared = proc_map_files_readdir,
2162 .llseek = generic_file_llseek,
Pavel Emelyanov640708a2012-01-10 15:11:23 -08002163};
2164
Nicolas Pitreb18b6a92017-01-21 00:09:08 -05002165#if defined(CONFIG_CHECKPOINT_RESTORE) && defined(CONFIG_POSIX_TIMERS)
Pavel Emelyanov48f6a7a2013-03-11 13:12:45 +04002166struct timers_private {
2167 struct pid *pid;
2168 struct task_struct *task;
2169 struct sighand_struct *sighand;
Pavel Emelyanov57b80152013-03-11 13:13:08 +04002170 struct pid_namespace *ns;
Pavel Emelyanov48f6a7a2013-03-11 13:12:45 +04002171 unsigned long flags;
2172};
2173
2174static void *timers_start(struct seq_file *m, loff_t *pos)
2175{
2176 struct timers_private *tp = m->private;
2177
2178 tp->task = get_pid_task(tp->pid, PIDTYPE_PID);
2179 if (!tp->task)
2180 return ERR_PTR(-ESRCH);
2181
2182 tp->sighand = lock_task_sighand(tp->task, &tp->flags);
2183 if (!tp->sighand)
2184 return ERR_PTR(-ESRCH);
2185
2186 return seq_list_start(&tp->task->signal->posix_timers, *pos);
2187}
2188
2189static void *timers_next(struct seq_file *m, void *v, loff_t *pos)
2190{
2191 struct timers_private *tp = m->private;
2192 return seq_list_next(v, &tp->task->signal->posix_timers, pos);
2193}
2194
2195static void timers_stop(struct seq_file *m, void *v)
2196{
2197 struct timers_private *tp = m->private;
2198
2199 if (tp->sighand) {
2200 unlock_task_sighand(tp->task, &tp->flags);
2201 tp->sighand = NULL;
2202 }
2203
2204 if (tp->task) {
2205 put_task_struct(tp->task);
2206 tp->task = NULL;
2207 }
2208}
2209
2210static int show_timer(struct seq_file *m, void *v)
2211{
2212 struct k_itimer *timer;
Pavel Emelyanov57b80152013-03-11 13:13:08 +04002213 struct timers_private *tp = m->private;
2214 int notify;
Alexey Dobriyancedbcca2014-08-08 14:21:33 -07002215 static const char * const nstr[] = {
Pavel Emelyanov57b80152013-03-11 13:13:08 +04002216 [SIGEV_SIGNAL] = "signal",
2217 [SIGEV_NONE] = "none",
2218 [SIGEV_THREAD] = "thread",
2219 };
Pavel Emelyanov48f6a7a2013-03-11 13:12:45 +04002220
2221 timer = list_entry((struct list_head *)v, struct k_itimer, list);
Pavel Emelyanov57b80152013-03-11 13:13:08 +04002222 notify = timer->it_sigev_notify;
2223
Pavel Emelyanov48f6a7a2013-03-11 13:12:45 +04002224 seq_printf(m, "ID: %d\n", timer->it_id);
Joe Perches25ce3192015-04-15 16:18:17 -07002225 seq_printf(m, "signal: %d/%p\n",
2226 timer->sigq->info.si_signo,
2227 timer->sigq->info.si_value.sival_ptr);
Pavel Emelyanov57b80152013-03-11 13:13:08 +04002228 seq_printf(m, "notify: %s/%s.%d\n",
Joe Perches25ce3192015-04-15 16:18:17 -07002229 nstr[notify & ~SIGEV_THREAD_ID],
2230 (notify & SIGEV_THREAD_ID) ? "tid" : "pid",
2231 pid_nr_ns(timer->it_pid, tp->ns));
Pavel Tikhomirov15ef0292013-05-17 02:12:03 +04002232 seq_printf(m, "ClockID: %d\n", timer->it_clock);
Pavel Emelyanov48f6a7a2013-03-11 13:12:45 +04002233
2234 return 0;
2235}
2236
2237static const struct seq_operations proc_timers_seq_ops = {
2238 .start = timers_start,
2239 .next = timers_next,
2240 .stop = timers_stop,
2241 .show = show_timer,
2242};
2243
2244static int proc_timers_open(struct inode *inode, struct file *file)
2245{
2246 struct timers_private *tp;
2247
2248 tp = __seq_open_private(file, &proc_timers_seq_ops,
2249 sizeof(struct timers_private));
2250 if (!tp)
2251 return -ENOMEM;
2252
2253 tp->pid = proc_pid(inode);
Pavel Emelyanov57b80152013-03-11 13:13:08 +04002254 tp->ns = inode->i_sb->s_fs_info;
Pavel Emelyanov48f6a7a2013-03-11 13:12:45 +04002255 return 0;
2256}
2257
2258static const struct file_operations proc_timers_operations = {
2259 .open = proc_timers_open,
2260 .read = seq_read,
2261 .llseek = seq_lseek,
2262 .release = seq_release_private,
2263};
Eric Engestromb5946be2016-03-17 14:20:57 -07002264#endif
Pavel Emelyanov640708a2012-01-10 15:11:23 -08002265
John Stultz5de23d42016-03-17 14:20:54 -07002266static ssize_t timerslack_ns_write(struct file *file, const char __user *buf,
2267 size_t count, loff_t *offset)
2268{
2269 struct inode *inode = file_inode(file);
2270 struct task_struct *p;
2271 u64 slack_ns;
2272 int err;
2273
2274 err = kstrtoull_from_user(buf, count, 10, &slack_ns);
2275 if (err < 0)
2276 return err;
2277
2278 p = get_proc_task(inode);
2279 if (!p)
2280 return -ESRCH;
2281
John Stultz4b2bd5f2016-10-07 17:02:33 -07002282 if (p != current) {
2283 if (!capable(CAP_SYS_NICE)) {
2284 count = -EPERM;
2285 goto out;
2286 }
John Stultz5de23d42016-03-17 14:20:54 -07002287
John Stultz4b2bd5f2016-10-07 17:02:33 -07002288 err = security_task_setscheduler(p);
2289 if (err) {
2290 count = err;
2291 goto out;
2292 }
John Stultz904763e2016-10-07 17:02:29 -07002293 }
2294
John Stultz7abbaf92016-10-07 17:02:26 -07002295 task_lock(p);
2296 if (slack_ns == 0)
2297 p->timer_slack_ns = p->default_timer_slack_ns;
2298 else
2299 p->timer_slack_ns = slack_ns;
2300 task_unlock(p);
2301
2302out:
John Stultz5de23d42016-03-17 14:20:54 -07002303 put_task_struct(p);
2304
2305 return count;
2306}
2307
2308static int timerslack_ns_show(struct seq_file *m, void *v)
2309{
2310 struct inode *inode = m->private;
2311 struct task_struct *p;
John Stultz7abbaf92016-10-07 17:02:26 -07002312 int err = 0;
John Stultz5de23d42016-03-17 14:20:54 -07002313
2314 p = get_proc_task(inode);
2315 if (!p)
2316 return -ESRCH;
2317
John Stultz4b2bd5f2016-10-07 17:02:33 -07002318 if (p != current) {
John Stultz5de23d42016-03-17 14:20:54 -07002319
John Stultz4b2bd5f2016-10-07 17:02:33 -07002320 if (!capable(CAP_SYS_NICE)) {
2321 err = -EPERM;
2322 goto out;
2323 }
2324 err = security_task_getscheduler(p);
2325 if (err)
2326 goto out;
2327 }
John Stultz904763e2016-10-07 17:02:29 -07002328
John Stultz7abbaf92016-10-07 17:02:26 -07002329 task_lock(p);
2330 seq_printf(m, "%llu\n", p->timer_slack_ns);
2331 task_unlock(p);
2332
2333out:
John Stultz5de23d42016-03-17 14:20:54 -07002334 put_task_struct(p);
2335
2336 return err;
2337}
2338
2339static int timerslack_ns_open(struct inode *inode, struct file *filp)
2340{
2341 return single_open(filp, timerslack_ns_show, inode);
2342}
2343
2344static const struct file_operations proc_pid_set_timerslack_ns_operations = {
2345 .open = timerslack_ns_open,
2346 .read = seq_read,
2347 .write = timerslack_ns_write,
2348 .llseek = seq_lseek,
2349 .release = single_release,
2350};
2351
Al Viroc52a47a2013-06-15 11:15:20 +04002352static int proc_pident_instantiate(struct inode *dir,
Eric Dumazetc5141e62007-05-08 00:26:15 -07002353 struct dentry *dentry, struct task_struct *task, const void *ptr)
Eric W. Biederman444ceed2006-10-02 02:18:49 -07002354{
Eric Dumazetc5141e62007-05-08 00:26:15 -07002355 const struct pid_entry *p = ptr;
Eric W. Biederman444ceed2006-10-02 02:18:49 -07002356 struct inode *inode;
2357 struct proc_inode *ei;
Eric W. Biederman444ceed2006-10-02 02:18:49 -07002358
Andreas Gruenbacherdb978da2016-11-10 22:18:28 +01002359 inode = proc_pid_make_inode(dir->i_sb, task, p->mode);
Eric W. Biederman444ceed2006-10-02 02:18:49 -07002360 if (!inode)
2361 goto out;
2362
2363 ei = PROC_I(inode);
Eric W. Biederman444ceed2006-10-02 02:18:49 -07002364 if (S_ISDIR(inode->i_mode))
Miklos Szeredibfe86842011-10-28 14:13:29 +02002365 set_nlink(inode, 2); /* Use getattr to fix if necessary */
Eric W. Biederman444ceed2006-10-02 02:18:49 -07002366 if (p->iop)
2367 inode->i_op = p->iop;
2368 if (p->fop)
2369 inode->i_fop = p->fop;
2370 ei->op = p->op;
Nick Pigginfb045ad2011-01-07 17:49:55 +11002371 d_set_d_op(dentry, &pid_dentry_operations);
Eric W. Biederman444ceed2006-10-02 02:18:49 -07002372 d_add(dentry, inode);
2373 /* Close the race of the process dying before we return the dentry */
Al Viro0b728e12012-06-10 16:03:43 -04002374 if (pid_revalidate(dentry, 0))
Al Viroc52a47a2013-06-15 11:15:20 +04002375 return 0;
Eric W. Biederman444ceed2006-10-02 02:18:49 -07002376out:
Al Viroc52a47a2013-06-15 11:15:20 +04002377 return -ENOENT;
Eric W. Biederman444ceed2006-10-02 02:18:49 -07002378}
2379
Linus Torvalds1da177e2005-04-16 15:20:36 -07002380static struct dentry *proc_pident_lookup(struct inode *dir,
2381 struct dentry *dentry,
Eric Dumazetc5141e62007-05-08 00:26:15 -07002382 const struct pid_entry *ents,
Eric W. Biederman7bcd6b02006-10-02 02:18:56 -07002383 unsigned int nents)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002384{
Al Viroc52a47a2013-06-15 11:15:20 +04002385 int error;
Eric W. Biederman99f89552006-06-26 00:25:55 -07002386 struct task_struct *task = get_proc_task(dir);
Eric Dumazetc5141e62007-05-08 00:26:15 -07002387 const struct pid_entry *p, *last;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002388
Al Viroc52a47a2013-06-15 11:15:20 +04002389 error = -ENOENT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002390
Eric W. Biederman99f89552006-06-26 00:25:55 -07002391 if (!task)
2392 goto out_no_task;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002393
Eric W. Biederman20cdc892006-10-02 02:17:07 -07002394 /*
2395 * Yes, it does not scale. And it should not. Don't add
2396 * new entries into /proc/<tgid>/ without very good reasons.
2397 */
Alexey Dobriyanbac5f5d2016-12-12 16:45:28 -08002398 last = &ents[nents];
2399 for (p = ents; p < last; p++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002400 if (p->len != dentry->d_name.len)
2401 continue;
2402 if (!memcmp(dentry->d_name.name, p->name, p->len))
2403 break;
2404 }
Alexey Dobriyanbac5f5d2016-12-12 16:45:28 -08002405 if (p >= last)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002406 goto out;
2407
Eric W. Biederman444ceed2006-10-02 02:18:49 -07002408 error = proc_pident_instantiate(dir, dentry, task, p);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002409out:
Eric W. Biederman99f89552006-06-26 00:25:55 -07002410 put_task_struct(task);
2411out_no_task:
Al Viroc52a47a2013-06-15 11:15:20 +04002412 return ERR_PTR(error);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002413}
2414
Al Virof0c3b502013-05-16 12:07:31 -04002415static int proc_pident_readdir(struct file *file, struct dir_context *ctx,
Eric Dumazetc5141e62007-05-08 00:26:15 -07002416 const struct pid_entry *ents, unsigned int nents)
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002417{
Al Virof0c3b502013-05-16 12:07:31 -04002418 struct task_struct *task = get_proc_task(file_inode(file));
2419 const struct pid_entry *p;
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002420
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002421 if (!task)
Al Virof0c3b502013-05-16 12:07:31 -04002422 return -ENOENT;
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002423
Al Virof0c3b502013-05-16 12:07:31 -04002424 if (!dir_emit_dots(file, ctx))
2425 goto out;
2426
2427 if (ctx->pos >= nents + 2)
2428 goto out;
2429
Alexey Dobriyanbac5f5d2016-12-12 16:45:28 -08002430 for (p = ents + (ctx->pos - 2); p < ents + nents; p++) {
Al Virof0c3b502013-05-16 12:07:31 -04002431 if (!proc_fill_cache(file, ctx, p->name, p->len,
2432 proc_pident_instantiate, task, p))
2433 break;
2434 ctx->pos++;
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002435 }
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002436out:
Eric W. Biederman61a28782006-10-02 02:18:49 -07002437 put_task_struct(task);
Al Virof0c3b502013-05-16 12:07:31 -04002438 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002439}
2440
Linus Torvalds1da177e2005-04-16 15:20:36 -07002441#ifdef CONFIG_SECURITY
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002442static ssize_t proc_pid_attr_read(struct file * file, char __user * buf,
2443 size_t count, loff_t *ppos)
2444{
Al Viro496ad9a2013-01-23 17:07:38 -05002445 struct inode * inode = file_inode(file);
Al Viro04ff9702007-03-12 16:17:58 +00002446 char *p = NULL;
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002447 ssize_t length;
2448 struct task_struct *task = get_proc_task(inode);
2449
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002450 if (!task)
Al Viro04ff9702007-03-12 16:17:58 +00002451 return -ESRCH;
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002452
2453 length = security_getprocattr(task,
Josef "Jeff" Sipek2fddfee2006-12-08 02:36:36 -08002454 (char*)file->f_path.dentry->d_name.name,
Al Viro04ff9702007-03-12 16:17:58 +00002455 &p);
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002456 put_task_struct(task);
Al Viro04ff9702007-03-12 16:17:58 +00002457 if (length > 0)
2458 length = simple_read_from_buffer(buf, count, ppos, p, length);
2459 kfree(p);
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002460 return length;
2461}
2462
2463static ssize_t proc_pid_attr_write(struct file * file, const char __user * buf,
2464 size_t count, loff_t *ppos)
2465{
Al Viro496ad9a2013-01-23 17:07:38 -05002466 struct inode * inode = file_inode(file);
Al Virobb646cd2015-12-24 00:16:30 -05002467 void *page;
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002468 ssize_t length;
2469 struct task_struct *task = get_proc_task(inode);
2470
2471 length = -ESRCH;
2472 if (!task)
2473 goto out_no_task;
Stephen Smalleyb21507e2017-01-09 10:07:31 -05002474
2475 /* A task may only write its own attributes. */
2476 length = -EACCES;
2477 if (current != task)
2478 goto out;
2479
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002480 if (count > PAGE_SIZE)
2481 count = PAGE_SIZE;
2482
2483 /* No partial writes. */
2484 length = -EINVAL;
2485 if (*ppos != 0)
2486 goto out;
2487
Al Virobb646cd2015-12-24 00:16:30 -05002488 page = memdup_user(buf, count);
2489 if (IS_ERR(page)) {
2490 length = PTR_ERR(page);
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002491 goto out;
Al Virobb646cd2015-12-24 00:16:30 -05002492 }
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002493
David Howells107db7c2009-05-08 13:55:27 +01002494 /* Guard against adverse ptrace interaction */
Stephen Smalleyb21507e2017-01-09 10:07:31 -05002495 length = mutex_lock_interruptible(&current->signal->cred_guard_mutex);
David Howells107db7c2009-05-08 13:55:27 +01002496 if (length < 0)
2497 goto out_free;
2498
Stephen Smalleyb21507e2017-01-09 10:07:31 -05002499 length = security_setprocattr(file->f_path.dentry->d_name.name,
Al Virobb646cd2015-12-24 00:16:30 -05002500 page, count);
Stephen Smalleyb21507e2017-01-09 10:07:31 -05002501 mutex_unlock(&current->signal->cred_guard_mutex);
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002502out_free:
Al Virobb646cd2015-12-24 00:16:30 -05002503 kfree(page);
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002504out:
2505 put_task_struct(task);
2506out_no_task:
2507 return length;
2508}
2509
Arjan van de Ven00977a52007-02-12 00:55:34 -08002510static const struct file_operations proc_pid_attr_operations = {
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002511 .read = proc_pid_attr_read,
2512 .write = proc_pid_attr_write,
Arnd Bergmann87df8422010-03-17 23:06:02 +01002513 .llseek = generic_file_llseek,
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002514};
2515
Eric Dumazetc5141e62007-05-08 00:26:15 -07002516static const struct pid_entry attr_dir_stuff[] = {
Alexey Dobriyan631f9c12008-11-10 01:32:52 +03002517 REG("current", S_IRUGO|S_IWUGO, proc_pid_attr_operations),
2518 REG("prev", S_IRUGO, proc_pid_attr_operations),
2519 REG("exec", S_IRUGO|S_IWUGO, proc_pid_attr_operations),
2520 REG("fscreate", S_IRUGO|S_IWUGO, proc_pid_attr_operations),
2521 REG("keycreate", S_IRUGO|S_IWUGO, proc_pid_attr_operations),
2522 REG("sockcreate", S_IRUGO|S_IWUGO, proc_pid_attr_operations),
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002523};
2524
Al Virof0c3b502013-05-16 12:07:31 -04002525static int proc_attr_dir_readdir(struct file *file, struct dir_context *ctx)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002526{
Al Virof0c3b502013-05-16 12:07:31 -04002527 return proc_pident_readdir(file, ctx,
2528 attr_dir_stuff, ARRAY_SIZE(attr_dir_stuff));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002529}
2530
Arjan van de Ven00977a52007-02-12 00:55:34 -08002531static const struct file_operations proc_attr_dir_operations = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002532 .read = generic_read_dir,
Al Virof50752e2016-04-20 17:13:54 -04002533 .iterate_shared = proc_attr_dir_readdir,
2534 .llseek = generic_file_llseek,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002535};
2536
Eric W. Biederman72d9dcf2006-10-02 02:18:50 -07002537static struct dentry *proc_attr_dir_lookup(struct inode *dir,
Al Viro00cd8dd2012-06-10 17:13:09 -04002538 struct dentry *dentry, unsigned int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002539{
Eric W. Biederman7bcd6b02006-10-02 02:18:56 -07002540 return proc_pident_lookup(dir, dentry,
2541 attr_dir_stuff, ARRAY_SIZE(attr_dir_stuff));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002542}
2543
Arjan van de Venc5ef1c42007-02-12 00:55:40 -08002544static const struct inode_operations proc_attr_dir_inode_operations = {
Eric W. Biederman72d9dcf2006-10-02 02:18:50 -07002545 .lookup = proc_attr_dir_lookup,
Eric W. Biederman99f89552006-06-26 00:25:55 -07002546 .getattr = pid_getattr,
Linus Torvalds6d76fa52006-07-15 12:26:45 -07002547 .setattr = proc_setattr,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002548};
2549
Linus Torvalds1da177e2005-04-16 15:20:36 -07002550#endif
2551
Christoph Hellwig698ba7b2009-12-15 16:47:37 -08002552#ifdef CONFIG_ELF_CORE
Kawai, Hidehiro3cb4a0b2007-07-19 01:48:28 -07002553static ssize_t proc_coredump_filter_read(struct file *file, char __user *buf,
2554 size_t count, loff_t *ppos)
2555{
Al Viro496ad9a2013-01-23 17:07:38 -05002556 struct task_struct *task = get_proc_task(file_inode(file));
Kawai, Hidehiro3cb4a0b2007-07-19 01:48:28 -07002557 struct mm_struct *mm;
2558 char buffer[PROC_NUMBUF];
2559 size_t len;
2560 int ret;
2561
2562 if (!task)
2563 return -ESRCH;
2564
2565 ret = 0;
2566 mm = get_task_mm(task);
2567 if (mm) {
2568 len = snprintf(buffer, sizeof(buffer), "%08lx\n",
2569 ((mm->flags & MMF_DUMP_FILTER_MASK) >>
2570 MMF_DUMP_FILTER_SHIFT));
2571 mmput(mm);
2572 ret = simple_read_from_buffer(buf, count, ppos, buffer, len);
2573 }
2574
2575 put_task_struct(task);
2576
2577 return ret;
2578}
2579
2580static ssize_t proc_coredump_filter_write(struct file *file,
2581 const char __user *buf,
2582 size_t count,
2583 loff_t *ppos)
2584{
2585 struct task_struct *task;
2586 struct mm_struct *mm;
Kawai, Hidehiro3cb4a0b2007-07-19 01:48:28 -07002587 unsigned int val;
2588 int ret;
2589 int i;
2590 unsigned long mask;
2591
Alexey Dobriyan774636e2015-09-09 15:36:59 -07002592 ret = kstrtouint_from_user(buf, count, 0, &val);
2593 if (ret < 0)
2594 return ret;
Kawai, Hidehiro3cb4a0b2007-07-19 01:48:28 -07002595
2596 ret = -ESRCH;
Al Viro496ad9a2013-01-23 17:07:38 -05002597 task = get_proc_task(file_inode(file));
Kawai, Hidehiro3cb4a0b2007-07-19 01:48:28 -07002598 if (!task)
2599 goto out_no_task;
2600
Kawai, Hidehiro3cb4a0b2007-07-19 01:48:28 -07002601 mm = get_task_mm(task);
2602 if (!mm)
2603 goto out_no_mm;
Colin Ian King41a0c2492015-12-18 14:22:01 -08002604 ret = 0;
Kawai, Hidehiro3cb4a0b2007-07-19 01:48:28 -07002605
2606 for (i = 0, mask = 1; i < MMF_DUMP_FILTER_BITS; i++, mask <<= 1) {
2607 if (val & mask)
2608 set_bit(i + MMF_DUMP_FILTER_SHIFT, &mm->flags);
2609 else
2610 clear_bit(i + MMF_DUMP_FILTER_SHIFT, &mm->flags);
2611 }
2612
2613 mmput(mm);
2614 out_no_mm:
2615 put_task_struct(task);
2616 out_no_task:
Alexey Dobriyan774636e2015-09-09 15:36:59 -07002617 if (ret < 0)
2618 return ret;
2619 return count;
Kawai, Hidehiro3cb4a0b2007-07-19 01:48:28 -07002620}
2621
2622static const struct file_operations proc_coredump_filter_operations = {
2623 .read = proc_coredump_filter_read,
2624 .write = proc_coredump_filter_write,
Arnd Bergmann87df8422010-03-17 23:06:02 +01002625 .llseek = generic_file_llseek,
Kawai, Hidehiro3cb4a0b2007-07-19 01:48:28 -07002626};
2627#endif
2628
Andrew Mortonaba76fd2006-12-10 02:19:48 -08002629#ifdef CONFIG_TASK_IO_ACCOUNTING
Alexey Dobriyan19aadc92014-08-08 14:21:50 -07002630static int do_io_accounting(struct task_struct *task, struct seq_file *m, int whole)
Andrew Mortonaba76fd2006-12-10 02:19:48 -08002631{
Andrea Righi940389b2008-07-28 00:48:12 +02002632 struct task_io_accounting acct = task->ioac;
Andrea Righi59954772008-07-27 17:29:15 +02002633 unsigned long flags;
Vasiliy Kulikov293eb1e2011-07-26 16:08:38 -07002634 int result;
Andrea Righi297c5d92008-07-25 01:48:49 -07002635
Vasiliy Kulikov293eb1e2011-07-26 16:08:38 -07002636 result = mutex_lock_killable(&task->signal->cred_guard_mutex);
2637 if (result)
2638 return result;
2639
Jann Horncaaee622016-01-20 15:00:04 -08002640 if (!ptrace_may_access(task, PTRACE_MODE_READ_FSCREDS)) {
Vasiliy Kulikov293eb1e2011-07-26 16:08:38 -07002641 result = -EACCES;
2642 goto out_unlock;
2643 }
Vasiliy Kulikov1d1221f2011-06-24 16:08:38 +04002644
Andrea Righi59954772008-07-27 17:29:15 +02002645 if (whole && lock_task_sighand(task, &flags)) {
2646 struct task_struct *t = task;
Andrea Righib2d002d2008-07-26 15:22:27 -07002647
Andrea Righi59954772008-07-27 17:29:15 +02002648 task_io_accounting_add(&acct, &task->signal->ioac);
2649 while_each_thread(task, t)
2650 task_io_accounting_add(&acct, &t->ioac);
Andrea Righi297c5d92008-07-25 01:48:49 -07002651
Andrea Righi59954772008-07-27 17:29:15 +02002652 unlock_task_sighand(task, &flags);
Andrea Righi297c5d92008-07-25 01:48:49 -07002653 }
Joe Perches25ce3192015-04-15 16:18:17 -07002654 seq_printf(m,
2655 "rchar: %llu\n"
2656 "wchar: %llu\n"
2657 "syscr: %llu\n"
2658 "syscw: %llu\n"
2659 "read_bytes: %llu\n"
2660 "write_bytes: %llu\n"
2661 "cancelled_write_bytes: %llu\n",
2662 (unsigned long long)acct.rchar,
2663 (unsigned long long)acct.wchar,
2664 (unsigned long long)acct.syscr,
2665 (unsigned long long)acct.syscw,
2666 (unsigned long long)acct.read_bytes,
2667 (unsigned long long)acct.write_bytes,
2668 (unsigned long long)acct.cancelled_write_bytes);
2669 result = 0;
2670
Vasiliy Kulikov293eb1e2011-07-26 16:08:38 -07002671out_unlock:
2672 mutex_unlock(&task->signal->cred_guard_mutex);
2673 return result;
Andrew Mortonaba76fd2006-12-10 02:19:48 -08002674}
Andrea Righi297c5d92008-07-25 01:48:49 -07002675
Alexey Dobriyan19aadc92014-08-08 14:21:50 -07002676static int proc_tid_io_accounting(struct seq_file *m, struct pid_namespace *ns,
2677 struct pid *pid, struct task_struct *task)
Andrea Righi297c5d92008-07-25 01:48:49 -07002678{
Alexey Dobriyan19aadc92014-08-08 14:21:50 -07002679 return do_io_accounting(task, m, 0);
Andrea Righi297c5d92008-07-25 01:48:49 -07002680}
2681
Alexey Dobriyan19aadc92014-08-08 14:21:50 -07002682static int proc_tgid_io_accounting(struct seq_file *m, struct pid_namespace *ns,
2683 struct pid *pid, struct task_struct *task)
Andrea Righi297c5d92008-07-25 01:48:49 -07002684{
Alexey Dobriyan19aadc92014-08-08 14:21:50 -07002685 return do_io_accounting(task, m, 1);
Andrea Righi297c5d92008-07-25 01:48:49 -07002686}
2687#endif /* CONFIG_TASK_IO_ACCOUNTING */
Andrew Mortonaba76fd2006-12-10 02:19:48 -08002688
Eric W. Biederman22d917d2011-11-17 00:11:58 -08002689#ifdef CONFIG_USER_NS
2690static int proc_id_map_open(struct inode *inode, struct file *file,
Fabian Frederickccf94f12014-08-08 14:21:22 -07002691 const struct seq_operations *seq_ops)
Eric W. Biederman22d917d2011-11-17 00:11:58 -08002692{
2693 struct user_namespace *ns = NULL;
2694 struct task_struct *task;
2695 struct seq_file *seq;
2696 int ret = -EINVAL;
2697
2698 task = get_proc_task(inode);
2699 if (task) {
2700 rcu_read_lock();
2701 ns = get_user_ns(task_cred_xxx(task, user_ns));
2702 rcu_read_unlock();
2703 put_task_struct(task);
2704 }
2705 if (!ns)
2706 goto err;
2707
2708 ret = seq_open(file, seq_ops);
2709 if (ret)
2710 goto err_put_ns;
2711
2712 seq = file->private_data;
2713 seq->private = ns;
2714
2715 return 0;
2716err_put_ns:
2717 put_user_ns(ns);
2718err:
2719 return ret;
2720}
2721
2722static int proc_id_map_release(struct inode *inode, struct file *file)
2723{
2724 struct seq_file *seq = file->private_data;
2725 struct user_namespace *ns = seq->private;
2726 put_user_ns(ns);
2727 return seq_release(inode, file);
2728}
2729
2730static int proc_uid_map_open(struct inode *inode, struct file *file)
2731{
2732 return proc_id_map_open(inode, file, &proc_uid_seq_operations);
2733}
2734
2735static int proc_gid_map_open(struct inode *inode, struct file *file)
2736{
2737 return proc_id_map_open(inode, file, &proc_gid_seq_operations);
2738}
2739
Eric W. Biedermanf76d2072012-08-30 01:24:05 -07002740static int proc_projid_map_open(struct inode *inode, struct file *file)
2741{
2742 return proc_id_map_open(inode, file, &proc_projid_seq_operations);
2743}
2744
Eric W. Biederman22d917d2011-11-17 00:11:58 -08002745static const struct file_operations proc_uid_map_operations = {
2746 .open = proc_uid_map_open,
2747 .write = proc_uid_map_write,
2748 .read = seq_read,
2749 .llseek = seq_lseek,
2750 .release = proc_id_map_release,
2751};
2752
2753static const struct file_operations proc_gid_map_operations = {
2754 .open = proc_gid_map_open,
2755 .write = proc_gid_map_write,
2756 .read = seq_read,
2757 .llseek = seq_lseek,
2758 .release = proc_id_map_release,
2759};
Eric W. Biedermanf76d2072012-08-30 01:24:05 -07002760
2761static const struct file_operations proc_projid_map_operations = {
2762 .open = proc_projid_map_open,
2763 .write = proc_projid_map_write,
2764 .read = seq_read,
2765 .llseek = seq_lseek,
2766 .release = proc_id_map_release,
2767};
Eric W. Biederman9cc46512014-12-02 12:27:26 -06002768
2769static int proc_setgroups_open(struct inode *inode, struct file *file)
2770{
2771 struct user_namespace *ns = NULL;
2772 struct task_struct *task;
2773 int ret;
2774
2775 ret = -ESRCH;
2776 task = get_proc_task(inode);
2777 if (task) {
2778 rcu_read_lock();
2779 ns = get_user_ns(task_cred_xxx(task, user_ns));
2780 rcu_read_unlock();
2781 put_task_struct(task);
2782 }
2783 if (!ns)
2784 goto err;
2785
2786 if (file->f_mode & FMODE_WRITE) {
2787 ret = -EACCES;
2788 if (!ns_capable(ns, CAP_SYS_ADMIN))
2789 goto err_put_ns;
2790 }
2791
2792 ret = single_open(file, &proc_setgroups_show, ns);
2793 if (ret)
2794 goto err_put_ns;
2795
2796 return 0;
2797err_put_ns:
2798 put_user_ns(ns);
2799err:
2800 return ret;
2801}
2802
2803static int proc_setgroups_release(struct inode *inode, struct file *file)
2804{
2805 struct seq_file *seq = file->private_data;
2806 struct user_namespace *ns = seq->private;
2807 int ret = single_release(inode, file);
2808 put_user_ns(ns);
2809 return ret;
2810}
2811
2812static const struct file_operations proc_setgroups_operations = {
2813 .open = proc_setgroups_open,
2814 .write = proc_setgroups_write,
2815 .read = seq_read,
2816 .llseek = seq_lseek,
2817 .release = proc_setgroups_release,
2818};
Eric W. Biederman22d917d2011-11-17 00:11:58 -08002819#endif /* CONFIG_USER_NS */
2820
Kees Cook47830722008-10-06 03:11:58 +04002821static int proc_pid_personality(struct seq_file *m, struct pid_namespace *ns,
2822 struct pid *pid, struct task_struct *task)
2823{
Al Viroa9712bc2011-03-23 15:52:50 -04002824 int err = lock_trace(task);
2825 if (!err) {
2826 seq_printf(m, "%08x\n", task->personality);
2827 unlock_trace(task);
2828 }
2829 return err;
Kees Cook47830722008-10-06 03:11:58 +04002830}
2831
Eric W. Biederman801199c2006-10-02 02:18:48 -07002832/*
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002833 * Thread groups
2834 */
Arjan van de Ven00977a52007-02-12 00:55:34 -08002835static const struct file_operations proc_task_operations;
Arjan van de Venc5ef1c42007-02-12 00:55:40 -08002836static const struct inode_operations proc_task_inode_operations;
Eric W. Biederman20cdc892006-10-02 02:17:07 -07002837
Eric Dumazetc5141e62007-05-08 00:26:15 -07002838static const struct pid_entry tgid_base_stuff[] = {
Alexey Dobriyan631f9c12008-11-10 01:32:52 +03002839 DIR("task", S_IRUGO|S_IXUGO, proc_task_inode_operations, proc_task_operations),
2840 DIR("fd", S_IRUSR|S_IXUSR, proc_fd_inode_operations, proc_fd_operations),
Pavel Emelyanov640708a2012-01-10 15:11:23 -08002841 DIR("map_files", S_IRUSR|S_IXUSR, proc_map_files_inode_operations, proc_map_files_operations),
Alexey Dobriyan631f9c12008-11-10 01:32:52 +03002842 DIR("fdinfo", S_IRUSR|S_IXUSR, proc_fdinfo_inode_operations, proc_fdinfo_operations),
Eric W. Biederman6b4e3062010-03-07 16:41:34 -08002843 DIR("ns", S_IRUSR|S_IXUGO, proc_ns_dir_inode_operations, proc_ns_dir_operations),
Andrew Mortonb2211a32008-03-11 18:03:35 -07002844#ifdef CONFIG_NET
Alexey Dobriyan631f9c12008-11-10 01:32:52 +03002845 DIR("net", S_IRUGO|S_IXUGO, proc_net_inode_operations, proc_net_operations),
Andrew Mortonb2211a32008-03-11 18:03:35 -07002846#endif
Alexey Dobriyan631f9c12008-11-10 01:32:52 +03002847 REG("environ", S_IRUSR, proc_environ_operations),
Al Viroc5317162016-10-05 18:43:43 -04002848 REG("auxv", S_IRUSR, proc_auxv_operations),
Alexey Dobriyan631f9c12008-11-10 01:32:52 +03002849 ONE("status", S_IRUGO, proc_pid_status),
Djalal Harouni35a35042014-04-07 15:38:36 -07002850 ONE("personality", S_IRUSR, proc_pid_personality),
Alexey Dobriyan1c963eb2014-08-08 14:21:37 -07002851 ONE("limits", S_IRUGO, proc_pid_limits),
Ingo Molnar43ae34c2007-07-09 18:52:00 +02002852#ifdef CONFIG_SCHED_DEBUG
Alexey Dobriyan631f9c12008-11-10 01:32:52 +03002853 REG("sched", S_IRUGO|S_IWUSR, proc_pid_sched_operations),
Ingo Molnar43ae34c2007-07-09 18:52:00 +02002854#endif
Mike Galbraith5091faa2010-11-30 14:18:03 +01002855#ifdef CONFIG_SCHED_AUTOGROUP
2856 REG("autogroup", S_IRUGO|S_IWUSR, proc_pid_sched_autogroup_operations),
2857#endif
john stultz4614a696b2009-12-14 18:00:05 -08002858 REG("comm", S_IRUGO|S_IWUSR, proc_pid_set_comm_operations),
Roland McGrathebcb6732008-07-25 19:46:00 -07002859#ifdef CONFIG_HAVE_ARCH_TRACEHOOK
Alexey Dobriyan09d93bd2014-08-08 14:21:39 -07002860 ONE("syscall", S_IRUSR, proc_pid_syscall),
Roland McGrathebcb6732008-07-25 19:46:00 -07002861#endif
Alexey Dobriyanc2c0bb42015-06-25 15:00:54 -07002862 REG("cmdline", S_IRUGO, proc_pid_cmdline_ops),
Alexey Dobriyan631f9c12008-11-10 01:32:52 +03002863 ONE("stat", S_IRUGO, proc_tgid_stat),
2864 ONE("statm", S_IRUGO, proc_pid_statm),
Siddhesh Poyarekarb7643752012-03-21 16:34:04 -07002865 REG("maps", S_IRUGO, proc_pid_maps_operations),
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002866#ifdef CONFIG_NUMA
Siddhesh Poyarekarb7643752012-03-21 16:34:04 -07002867 REG("numa_maps", S_IRUGO, proc_pid_numa_maps_operations),
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002868#endif
Alexey Dobriyan631f9c12008-11-10 01:32:52 +03002869 REG("mem", S_IRUSR|S_IWUSR, proc_mem_operations),
2870 LNK("cwd", proc_cwd_link),
2871 LNK("root", proc_root_link),
2872 LNK("exe", proc_exe_link),
2873 REG("mounts", S_IRUGO, proc_mounts_operations),
2874 REG("mountinfo", S_IRUGO, proc_mountinfo_operations),
2875 REG("mountstats", S_IRUSR, proc_mountstats_operations),
Matt Mackall1e883282008-02-04 22:29:07 -08002876#ifdef CONFIG_PROC_PAGE_MONITOR
Alexey Dobriyan631f9c12008-11-10 01:32:52 +03002877 REG("clear_refs", S_IWUSR, proc_clear_refs_operations),
Siddhesh Poyarekarb7643752012-03-21 16:34:04 -07002878 REG("smaps", S_IRUGO, proc_pid_smaps_operations),
Djalal Harouni32ed74a2014-04-07 15:38:38 -07002879 REG("pagemap", S_IRUSR, proc_pagemap_operations),
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002880#endif
2881#ifdef CONFIG_SECURITY
Alexey Dobriyan631f9c12008-11-10 01:32:52 +03002882 DIR("attr", S_IRUGO|S_IXUGO, proc_attr_dir_inode_operations, proc_attr_dir_operations),
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002883#endif
2884#ifdef CONFIG_KALLSYMS
Alexey Dobriyanedfcd602014-08-08 14:21:44 -07002885 ONE("wchan", S_IRUGO, proc_pid_wchan),
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002886#endif
Ken Chen2ec220e2008-11-10 11:26:08 +03002887#ifdef CONFIG_STACKTRACE
Djalal Harouni35a35042014-04-07 15:38:36 -07002888 ONE("stack", S_IRUSR, proc_pid_stack),
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002889#endif
Naveen N. Rao5968cec2015-06-30 14:36:03 +05302890#ifdef CONFIG_SCHED_INFO
Alexey Dobriyanf6e826c2014-08-08 14:21:46 -07002891 ONE("schedstat", S_IRUGO, proc_pid_schedstat),
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002892#endif
Arjan van de Ven97455122008-01-25 21:08:34 +01002893#ifdef CONFIG_LATENCYTOP
Alexey Dobriyan631f9c12008-11-10 01:32:52 +03002894 REG("latency", S_IRUGO, proc_lstats_operations),
Arjan van de Ven97455122008-01-25 21:08:34 +01002895#endif
Paul Menage8793d852007-10-18 23:39:39 -07002896#ifdef CONFIG_PROC_PID_CPUSET
Zefan Li52de4772014-09-18 16:03:36 +08002897 ONE("cpuset", S_IRUGO, proc_cpuset_show),
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002898#endif
Paul Menagea4243162007-10-18 23:39:35 -07002899#ifdef CONFIG_CGROUPS
Zefan Li006f4ac2014-09-18 16:03:15 +08002900 ONE("cgroup", S_IRUGO, proc_cgroup_show),
Paul Menagea4243162007-10-18 23:39:35 -07002901#endif
Alexey Dobriyan6ba51e32014-08-08 14:21:48 -07002902 ONE("oom_score", S_IRUGO, proc_oom_score),
David Rientjesfa0cbbf2012-11-12 17:53:04 -08002903 REG("oom_adj", S_IRUGO|S_IWUSR, proc_oom_adj_operations),
David Rientjesa63d83f2010-08-09 17:19:46 -07002904 REG("oom_score_adj", S_IRUGO|S_IWUSR, proc_oom_score_adj_operations),
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002905#ifdef CONFIG_AUDITSYSCALL
Alexey Dobriyan631f9c12008-11-10 01:32:52 +03002906 REG("loginuid", S_IWUSR|S_IRUGO, proc_loginuid_operations),
2907 REG("sessionid", S_IRUGO, proc_sessionid_operations),
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002908#endif
Akinobu Mitaf4f154f2006-12-08 02:39:47 -08002909#ifdef CONFIG_FAULT_INJECTION
Alexey Dobriyan631f9c12008-11-10 01:32:52 +03002910 REG("make-it-fail", S_IRUGO|S_IWUSR, proc_fault_inject_operations),
Akinobu Mitaf4f154f2006-12-08 02:39:47 -08002911#endif
Christoph Hellwig698ba7b2009-12-15 16:47:37 -08002912#ifdef CONFIG_ELF_CORE
Alexey Dobriyan631f9c12008-11-10 01:32:52 +03002913 REG("coredump_filter", S_IRUGO|S_IWUSR, proc_coredump_filter_operations),
Kawai, Hidehiro3cb4a0b2007-07-19 01:48:28 -07002914#endif
Andrew Mortonaba76fd2006-12-10 02:19:48 -08002915#ifdef CONFIG_TASK_IO_ACCOUNTING
Alexey Dobriyan19aadc92014-08-08 14:21:50 -07002916 ONE("io", S_IRUSR, proc_tgid_io_accounting),
Andrew Mortonaba76fd2006-12-10 02:19:48 -08002917#endif
Chris Metcalff133ecc2011-05-26 12:40:09 -04002918#ifdef CONFIG_HARDWALL
Alexey Dobriyand962c142014-08-08 14:21:52 -07002919 ONE("hardwall", S_IRUGO, proc_pid_hardwall),
Chris Metcalff133ecc2011-05-26 12:40:09 -04002920#endif
Eric W. Biederman22d917d2011-11-17 00:11:58 -08002921#ifdef CONFIG_USER_NS
2922 REG("uid_map", S_IRUGO|S_IWUSR, proc_uid_map_operations),
2923 REG("gid_map", S_IRUGO|S_IWUSR, proc_gid_map_operations),
Eric W. Biedermanf76d2072012-08-30 01:24:05 -07002924 REG("projid_map", S_IRUGO|S_IWUSR, proc_projid_map_operations),
Eric W. Biederman9cc46512014-12-02 12:27:26 -06002925 REG("setgroups", S_IRUGO|S_IWUSR, proc_setgroups_operations),
Eric W. Biederman22d917d2011-11-17 00:11:58 -08002926#endif
Nicolas Pitreb18b6a92017-01-21 00:09:08 -05002927#if defined(CONFIG_CHECKPOINT_RESTORE) && defined(CONFIG_POSIX_TIMERS)
Pavel Emelyanov48f6a7a2013-03-11 13:12:45 +04002928 REG("timers", S_IRUGO, proc_timers_operations),
2929#endif
John Stultz5de23d42016-03-17 14:20:54 -07002930 REG("timerslack_ns", S_IRUGO|S_IWUGO, proc_pid_set_timerslack_ns_operations),
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002931};
2932
Al Virof0c3b502013-05-16 12:07:31 -04002933static int proc_tgid_base_readdir(struct file *file, struct dir_context *ctx)
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002934{
Al Virof0c3b502013-05-16 12:07:31 -04002935 return proc_pident_readdir(file, ctx,
2936 tgid_base_stuff, ARRAY_SIZE(tgid_base_stuff));
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002937}
2938
Arjan van de Ven00977a52007-02-12 00:55:34 -08002939static const struct file_operations proc_tgid_base_operations = {
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002940 .read = generic_read_dir,
Al Virof50752e2016-04-20 17:13:54 -04002941 .iterate_shared = proc_tgid_base_readdir,
2942 .llseek = generic_file_llseek,
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002943};
2944
Al Viro00cd8dd2012-06-10 17:13:09 -04002945static struct dentry *proc_tgid_base_lookup(struct inode *dir, struct dentry *dentry, unsigned int flags)
2946{
Eric W. Biederman7bcd6b02006-10-02 02:18:56 -07002947 return proc_pident_lookup(dir, dentry,
2948 tgid_base_stuff, ARRAY_SIZE(tgid_base_stuff));
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002949}
2950
Arjan van de Venc5ef1c42007-02-12 00:55:40 -08002951static const struct inode_operations proc_tgid_base_inode_operations = {
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002952 .lookup = proc_tgid_base_lookup,
2953 .getattr = pid_getattr,
2954 .setattr = proc_setattr,
Vasiliy Kulikov04996802012-01-10 15:11:31 -08002955 .permission = proc_pid_permission,
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002956};
2957
Pavel Emelyanov60347f62007-10-18 23:40:03 -07002958static void proc_flush_task_mnt(struct vfsmount *mnt, pid_t pid, pid_t tgid)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002959{
Eric W. Biederman48e64842006-06-26 00:25:48 -07002960 struct dentry *dentry, *leader, *dir;
Eric W. Biederman8578cea2006-06-26 00:25:54 -07002961 char buf[PROC_NUMBUF];
Eric W. Biederman48e64842006-06-26 00:25:48 -07002962 struct qstr name;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002963
Eric W. Biederman48e64842006-06-26 00:25:48 -07002964 name.name = buf;
Pavel Emelyanov60347f62007-10-18 23:40:03 -07002965 name.len = snprintf(buf, sizeof(buf), "%d", pid);
Al Viro4f522a22013-02-11 23:20:37 -05002966 /* no ->d_hash() rejects on procfs */
Pavel Emelyanov60347f62007-10-18 23:40:03 -07002967 dentry = d_hash_and_lookup(mnt->mnt_root, &name);
Eric W. Biederman48e64842006-06-26 00:25:48 -07002968 if (dentry) {
Eric W. Biedermanbbd51922014-02-13 10:24:23 -08002969 d_invalidate(dentry);
Eric W. Biederman48e64842006-06-26 00:25:48 -07002970 dput(dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002971 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002972
Oleg Nesterovc35a7f12014-12-10 15:54:56 -08002973 if (pid == tgid)
2974 return;
2975
Eric W. Biederman48e64842006-06-26 00:25:48 -07002976 name.name = buf;
Pavel Emelyanov60347f62007-10-18 23:40:03 -07002977 name.len = snprintf(buf, sizeof(buf), "%d", tgid);
2978 leader = d_hash_and_lookup(mnt->mnt_root, &name);
Eric W. Biederman48e64842006-06-26 00:25:48 -07002979 if (!leader)
2980 goto out;
2981
2982 name.name = "task";
2983 name.len = strlen(name.name);
2984 dir = d_hash_and_lookup(leader, &name);
2985 if (!dir)
2986 goto out_put_leader;
2987
2988 name.name = buf;
Pavel Emelyanov60347f62007-10-18 23:40:03 -07002989 name.len = snprintf(buf, sizeof(buf), "%d", pid);
Eric W. Biederman48e64842006-06-26 00:25:48 -07002990 dentry = d_hash_and_lookup(dir, &name);
2991 if (dentry) {
Eric W. Biedermanbbd51922014-02-13 10:24:23 -08002992 d_invalidate(dentry);
Eric W. Biederman48e64842006-06-26 00:25:48 -07002993 dput(dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002994 }
Eric W. Biederman48e64842006-06-26 00:25:48 -07002995
2996 dput(dir);
2997out_put_leader:
2998 dput(leader);
2999out:
3000 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003001}
3002
Randy Dunlap0895e912007-10-21 21:00:10 -07003003/**
3004 * proc_flush_task - Remove dcache entries for @task from the /proc dcache.
3005 * @task: task that should be flushed.
3006 *
3007 * When flushing dentries from proc, one needs to flush them from global
Pavel Emelyanov60347f62007-10-18 23:40:03 -07003008 * proc (proc_mnt) and from all the namespaces' procs this task was seen
Randy Dunlap0895e912007-10-21 21:00:10 -07003009 * in. This call is supposed to do all of this job.
3010 *
3011 * Looks in the dcache for
3012 * /proc/@pid
3013 * /proc/@tgid/task/@pid
3014 * if either directory is present flushes it and all of it'ts children
3015 * from the dcache.
3016 *
3017 * It is safe and reasonable to cache /proc entries for a task until
3018 * that task exits. After that they just clog up the dcache with
3019 * useless entries, possibly causing useful dcache entries to be
3020 * flushed instead. This routine is proved to flush those useless
3021 * dcache entries at process exit time.
3022 *
3023 * NOTE: This routine is just an optimization so it does not guarantee
3024 * that no dcache entries will exist at process exit time it
3025 * just makes it very unlikely that any will persist.
Pavel Emelyanov60347f62007-10-18 23:40:03 -07003026 */
3027
3028void proc_flush_task(struct task_struct *task)
3029{
Eric W. Biederman9fcc2d12007-11-14 17:00:07 -08003030 int i;
Oleg Nesterov9b4d1cbe2009-09-22 16:45:34 -07003031 struct pid *pid, *tgid;
Pavel Emelyanov130f77e2007-10-18 23:40:11 -07003032 struct upid *upid;
3033
Pavel Emelyanov130f77e2007-10-18 23:40:11 -07003034 pid = task_pid(task);
Oleg Nesterov9b4d1cbe2009-09-22 16:45:34 -07003035 tgid = task_tgid(task);
Pavel Emelyanov130f77e2007-10-18 23:40:11 -07003036
Eric W. Biederman9fcc2d12007-11-14 17:00:07 -08003037 for (i = 0; i <= pid->level; i++) {
Pavel Emelyanov130f77e2007-10-18 23:40:11 -07003038 upid = &pid->numbers[i];
3039 proc_flush_task_mnt(upid->ns->proc_mnt, upid->nr,
Oleg Nesterov9b4d1cbe2009-09-22 16:45:34 -07003040 tgid->numbers[i].nr);
Pavel Emelyanov130f77e2007-10-18 23:40:11 -07003041 }
Pavel Emelyanov60347f62007-10-18 23:40:03 -07003042}
3043
Al Viroc52a47a2013-06-15 11:15:20 +04003044static int proc_pid_instantiate(struct inode *dir,
3045 struct dentry * dentry,
3046 struct task_struct *task, const void *ptr)
Eric W. Biederman444ceed2006-10-02 02:18:49 -07003047{
Eric W. Biederman444ceed2006-10-02 02:18:49 -07003048 struct inode *inode;
3049
Andreas Gruenbacherdb978da2016-11-10 22:18:28 +01003050 inode = proc_pid_make_inode(dir->i_sb, task, S_IFDIR | S_IRUGO | S_IXUGO);
Eric W. Biederman444ceed2006-10-02 02:18:49 -07003051 if (!inode)
3052 goto out;
3053
Eric W. Biederman444ceed2006-10-02 02:18:49 -07003054 inode->i_op = &proc_tgid_base_inode_operations;
3055 inode->i_fop = &proc_tgid_base_operations;
3056 inode->i_flags|=S_IMMUTABLE;
Vegard Nossumaed54172008-06-05 22:46:53 -07003057
Alexey Dobriyan1270dd82016-12-12 16:45:32 -08003058 set_nlink(inode, nlink_tgid);
Eric W. Biederman444ceed2006-10-02 02:18:49 -07003059
Nick Pigginfb045ad2011-01-07 17:49:55 +11003060 d_set_d_op(dentry, &pid_dentry_operations);
Eric W. Biederman444ceed2006-10-02 02:18:49 -07003061
3062 d_add(dentry, inode);
3063 /* Close the race of the process dying before we return the dentry */
Al Viro0b728e12012-06-10 16:03:43 -04003064 if (pid_revalidate(dentry, 0))
Al Viroc52a47a2013-06-15 11:15:20 +04003065 return 0;
Eric W. Biederman444ceed2006-10-02 02:18:49 -07003066out:
Al Viroc52a47a2013-06-15 11:15:20 +04003067 return -ENOENT;
Eric W. Biederman444ceed2006-10-02 02:18:49 -07003068}
3069
Al Viro00cd8dd2012-06-10 17:13:09 -04003070struct dentry *proc_pid_lookup(struct inode *dir, struct dentry * dentry, unsigned int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003071{
Alexey Dobriyan335eb532014-08-08 14:21:27 -07003072 int result = -ENOENT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003073 struct task_struct *task;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003074 unsigned tgid;
Pavel Emelyanovb4888932007-10-18 23:40:14 -07003075 struct pid_namespace *ns;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003076
Alexey Dobriyandbcdb502014-08-08 14:21:25 -07003077 tgid = name_to_int(&dentry->d_name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003078 if (tgid == ~0U)
3079 goto out;
3080
Pavel Emelyanovb4888932007-10-18 23:40:14 -07003081 ns = dentry->d_sb->s_fs_info;
Eric W. Biedermande758732006-06-26 00:25:51 -07003082 rcu_read_lock();
Pavel Emelyanovb4888932007-10-18 23:40:14 -07003083 task = find_task_by_pid_ns(tgid, ns);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003084 if (task)
3085 get_task_struct(task);
Eric W. Biedermande758732006-06-26 00:25:51 -07003086 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07003087 if (!task)
3088 goto out;
3089
Eric W. Biederman444ceed2006-10-02 02:18:49 -07003090 result = proc_pid_instantiate(dir, dentry, task, NULL);
Eric W. Biederman48e64842006-06-26 00:25:48 -07003091 put_task_struct(task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003092out:
Al Viroc52a47a2013-06-15 11:15:20 +04003093 return ERR_PTR(result);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003094}
3095
Linus Torvalds1da177e2005-04-16 15:20:36 -07003096/*
Eric W. Biederman0804ef42006-10-02 02:17:04 -07003097 * Find the first task with tgid >= tgid
Eric W. Biederman0bc58a92006-06-26 00:25:50 -07003098 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07003099 */
Eric W. Biederman19fd4bb2007-11-28 16:21:26 -08003100struct tgid_iter {
3101 unsigned int tgid;
Eric W. Biederman0804ef42006-10-02 02:17:04 -07003102 struct task_struct *task;
Eric W. Biederman19fd4bb2007-11-28 16:21:26 -08003103};
3104static struct tgid_iter next_tgid(struct pid_namespace *ns, struct tgid_iter iter)
3105{
Eric W. Biederman0804ef42006-10-02 02:17:04 -07003106 struct pid *pid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003107
Eric W. Biederman19fd4bb2007-11-28 16:21:26 -08003108 if (iter.task)
3109 put_task_struct(iter.task);
Eric W. Biederman0804ef42006-10-02 02:17:04 -07003110 rcu_read_lock();
3111retry:
Eric W. Biederman19fd4bb2007-11-28 16:21:26 -08003112 iter.task = NULL;
3113 pid = find_ge_pid(iter.tgid, ns);
Eric W. Biederman0804ef42006-10-02 02:17:04 -07003114 if (pid) {
Eric W. Biederman19fd4bb2007-11-28 16:21:26 -08003115 iter.tgid = pid_nr_ns(pid, ns);
3116 iter.task = pid_task(pid, PIDTYPE_PID);
Eric W. Biederman0804ef42006-10-02 02:17:04 -07003117 /* What we to know is if the pid we have find is the
3118 * pid of a thread_group_leader. Testing for task
3119 * being a thread_group_leader is the obvious thing
3120 * todo but there is a window when it fails, due to
3121 * the pid transfer logic in de_thread.
3122 *
3123 * So we perform the straight forward test of seeing
3124 * if the pid we have found is the pid of a thread
3125 * group leader, and don't worry if the task we have
3126 * found doesn't happen to be a thread group leader.
3127 * As we don't care in the case of readdir.
3128 */
Eric W. Biederman19fd4bb2007-11-28 16:21:26 -08003129 if (!iter.task || !has_group_leader_pid(iter.task)) {
3130 iter.tgid += 1;
Eric W. Biederman0804ef42006-10-02 02:17:04 -07003131 goto retry;
Eric W. Biederman19fd4bb2007-11-28 16:21:26 -08003132 }
3133 get_task_struct(iter.task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003134 }
Eric W. Biederman454cc102006-06-26 00:25:51 -07003135 rcu_read_unlock();
Eric W. Biederman19fd4bb2007-11-28 16:21:26 -08003136 return iter;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003137}
3138
Eric W. Biederman00978752014-07-31 03:10:50 -07003139#define TGID_OFFSET (FIRST_PROCESS_ENTRY + 2)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003140
3141/* for the /proc/ directory itself, after non-process stuff has been done */
Al Virof0c3b502013-05-16 12:07:31 -04003142int proc_pid_readdir(struct file *file, struct dir_context *ctx)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003143{
Eric W. Biederman19fd4bb2007-11-28 16:21:26 -08003144 struct tgid_iter iter;
Al Viro3aa33772014-10-31 00:42:35 -04003145 struct pid_namespace *ns = file_inode(file)->i_sb->s_fs_info;
Al Virof0c3b502013-05-16 12:07:31 -04003146 loff_t pos = ctx->pos;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003147
Al Viro021ada72013-03-29 19:27:05 -04003148 if (pos >= PID_MAX_LIMIT + TGID_OFFSET)
Al Virof0c3b502013-05-16 12:07:31 -04003149 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003150
Eric W. Biederman00978752014-07-31 03:10:50 -07003151 if (pos == TGID_OFFSET - 2) {
David Howells2b0143b2015-03-17 22:25:59 +00003152 struct inode *inode = d_inode(ns->proc_self);
Al Virodb963162013-06-15 10:45:10 +04003153 if (!dir_emit(ctx, "self", 4, inode->i_ino, DT_LNK))
Al Virof0c3b502013-05-16 12:07:31 -04003154 return 0;
Eric W. Biederman00978752014-07-31 03:10:50 -07003155 ctx->pos = pos = pos + 1;
Al Viro021ada72013-03-29 19:27:05 -04003156 }
Eric W. Biederman00978752014-07-31 03:10:50 -07003157 if (pos == TGID_OFFSET - 1) {
David Howells2b0143b2015-03-17 22:25:59 +00003158 struct inode *inode = d_inode(ns->proc_thread_self);
Eric W. Biederman00978752014-07-31 03:10:50 -07003159 if (!dir_emit(ctx, "thread-self", 11, inode->i_ino, DT_LNK))
3160 return 0;
3161 ctx->pos = pos = pos + 1;
3162 }
3163 iter.tgid = pos - TGID_OFFSET;
Eric W. Biederman19fd4bb2007-11-28 16:21:26 -08003164 iter.task = NULL;
Eric W. Biederman19fd4bb2007-11-28 16:21:26 -08003165 for (iter = next_tgid(ns, iter);
3166 iter.task;
3167 iter.tgid += 1, iter = next_tgid(ns, iter)) {
Al Virof0c3b502013-05-16 12:07:31 -04003168 char name[PROC_NUMBUF];
3169 int len;
Eric Dumazet3ba4bce2017-01-24 15:18:07 -08003170
3171 cond_resched();
Lafcadio Wluiki796f5712017-02-24 15:00:23 -08003172 if (!has_pid_permissions(ns, iter.task, HIDEPID_INVISIBLE))
Al Virof0c3b502013-05-16 12:07:31 -04003173 continue;
Vasiliy Kulikov04996802012-01-10 15:11:31 -08003174
Al Virof0c3b502013-05-16 12:07:31 -04003175 len = snprintf(name, sizeof(name), "%d", iter.tgid);
3176 ctx->pos = iter.tgid + TGID_OFFSET;
3177 if (!proc_fill_cache(file, ctx, name, len,
3178 proc_pid_instantiate, iter.task, NULL)) {
Eric W. Biederman19fd4bb2007-11-28 16:21:26 -08003179 put_task_struct(iter.task);
Al Virof0c3b502013-05-16 12:07:31 -04003180 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003181 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003182 }
Al Virof0c3b502013-05-16 12:07:31 -04003183 ctx->pos = PID_MAX_LIMIT + TGID_OFFSET;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003184 return 0;
3185}
3186
Eric W. Biederman0bc58a92006-06-26 00:25:50 -07003187/*
Janis Danisevskis1b3044e2016-05-20 17:00:08 -07003188 * proc_tid_comm_permission is a special permission function exclusively
3189 * used for the node /proc/<pid>/task/<tid>/comm.
3190 * It bypasses generic permission checks in the case where a task of the same
3191 * task group attempts to access the node.
3192 * The rationale behind this is that glibc and bionic access this node for
3193 * cross thread naming (pthread_set/getname_np(!self)). However, if
3194 * PR_SET_DUMPABLE gets set to 0 this node among others becomes uid=0 gid=0,
3195 * which locks out the cross thread naming implementation.
3196 * This function makes sure that the node is always accessible for members of
3197 * same thread group.
3198 */
3199static int proc_tid_comm_permission(struct inode *inode, int mask)
3200{
3201 bool is_same_tgroup;
3202 struct task_struct *task;
3203
3204 task = get_proc_task(inode);
3205 if (!task)
3206 return -ESRCH;
3207 is_same_tgroup = same_thread_group(current, task);
3208 put_task_struct(task);
3209
3210 if (likely(is_same_tgroup && !(mask & MAY_EXEC))) {
3211 /* This file (/proc/<pid>/task/<tid>/comm) can always be
3212 * read or written by the members of the corresponding
3213 * thread group.
3214 */
3215 return 0;
3216 }
3217
3218 return generic_permission(inode, mask);
3219}
3220
3221static const struct inode_operations proc_tid_comm_inode_operations = {
3222 .permission = proc_tid_comm_permission,
3223};
3224
3225/*
Eric W. Biederman28a6d672006-10-02 02:17:05 -07003226 * Tasks
3227 */
Eric Dumazetc5141e62007-05-08 00:26:15 -07003228static const struct pid_entry tid_base_stuff[] = {
Alexey Dobriyan631f9c12008-11-10 01:32:52 +03003229 DIR("fd", S_IRUSR|S_IXUSR, proc_fd_inode_operations, proc_fd_operations),
Jerome Marchand38355412010-04-27 13:13:06 -07003230 DIR("fdinfo", S_IRUSR|S_IXUSR, proc_fdinfo_inode_operations, proc_fdinfo_operations),
Eric W. Biederman6b4e3062010-03-07 16:41:34 -08003231 DIR("ns", S_IRUSR|S_IXUGO, proc_ns_dir_inode_operations, proc_ns_dir_operations),
Eric W. Biederman6ba8ed72014-07-31 16:27:08 -07003232#ifdef CONFIG_NET
3233 DIR("net", S_IRUGO|S_IXUGO, proc_net_inode_operations, proc_net_operations),
3234#endif
Alexey Dobriyan631f9c12008-11-10 01:32:52 +03003235 REG("environ", S_IRUSR, proc_environ_operations),
Al Viroc5317162016-10-05 18:43:43 -04003236 REG("auxv", S_IRUSR, proc_auxv_operations),
Alexey Dobriyan631f9c12008-11-10 01:32:52 +03003237 ONE("status", S_IRUGO, proc_pid_status),
Djalal Harouni35a35042014-04-07 15:38:36 -07003238 ONE("personality", S_IRUSR, proc_pid_personality),
Alexey Dobriyan1c963eb2014-08-08 14:21:37 -07003239 ONE("limits", S_IRUGO, proc_pid_limits),
Ingo Molnar43ae34c2007-07-09 18:52:00 +02003240#ifdef CONFIG_SCHED_DEBUG
Alexey Dobriyan631f9c12008-11-10 01:32:52 +03003241 REG("sched", S_IRUGO|S_IWUSR, proc_pid_sched_operations),
Ingo Molnar43ae34c2007-07-09 18:52:00 +02003242#endif
Janis Danisevskis1b3044e2016-05-20 17:00:08 -07003243 NOD("comm", S_IFREG|S_IRUGO|S_IWUSR,
3244 &proc_tid_comm_inode_operations,
3245 &proc_pid_set_comm_operations, {}),
Roland McGrathebcb6732008-07-25 19:46:00 -07003246#ifdef CONFIG_HAVE_ARCH_TRACEHOOK
Alexey Dobriyan09d93bd2014-08-08 14:21:39 -07003247 ONE("syscall", S_IRUSR, proc_pid_syscall),
Roland McGrathebcb6732008-07-25 19:46:00 -07003248#endif
Alexey Dobriyanc2c0bb42015-06-25 15:00:54 -07003249 REG("cmdline", S_IRUGO, proc_pid_cmdline_ops),
Alexey Dobriyan631f9c12008-11-10 01:32:52 +03003250 ONE("stat", S_IRUGO, proc_tid_stat),
3251 ONE("statm", S_IRUGO, proc_pid_statm),
Siddhesh Poyarekarb7643752012-03-21 16:34:04 -07003252 REG("maps", S_IRUGO, proc_tid_maps_operations),
Iago López Galeiras2e13ba52015-06-25 15:00:57 -07003253#ifdef CONFIG_PROC_CHILDREN
Cyrill Gorcunov818411612012-05-31 16:26:43 -07003254 REG("children", S_IRUGO, proc_tid_children_operations),
3255#endif
Eric W. Biederman28a6d672006-10-02 02:17:05 -07003256#ifdef CONFIG_NUMA
Siddhesh Poyarekarb7643752012-03-21 16:34:04 -07003257 REG("numa_maps", S_IRUGO, proc_tid_numa_maps_operations),
Eric W. Biederman28a6d672006-10-02 02:17:05 -07003258#endif
Alexey Dobriyan631f9c12008-11-10 01:32:52 +03003259 REG("mem", S_IRUSR|S_IWUSR, proc_mem_operations),
3260 LNK("cwd", proc_cwd_link),
3261 LNK("root", proc_root_link),
3262 LNK("exe", proc_exe_link),
3263 REG("mounts", S_IRUGO, proc_mounts_operations),
3264 REG("mountinfo", S_IRUGO, proc_mountinfo_operations),
Matt Mackall1e883282008-02-04 22:29:07 -08003265#ifdef CONFIG_PROC_PAGE_MONITOR
Alexey Dobriyan631f9c12008-11-10 01:32:52 +03003266 REG("clear_refs", S_IWUSR, proc_clear_refs_operations),
Siddhesh Poyarekarb7643752012-03-21 16:34:04 -07003267 REG("smaps", S_IRUGO, proc_tid_smaps_operations),
Djalal Harouni32ed74a2014-04-07 15:38:38 -07003268 REG("pagemap", S_IRUSR, proc_pagemap_operations),
Eric W. Biederman28a6d672006-10-02 02:17:05 -07003269#endif
3270#ifdef CONFIG_SECURITY
Alexey Dobriyan631f9c12008-11-10 01:32:52 +03003271 DIR("attr", S_IRUGO|S_IXUGO, proc_attr_dir_inode_operations, proc_attr_dir_operations),
Eric W. Biederman28a6d672006-10-02 02:17:05 -07003272#endif
3273#ifdef CONFIG_KALLSYMS
Alexey Dobriyanedfcd602014-08-08 14:21:44 -07003274 ONE("wchan", S_IRUGO, proc_pid_wchan),
Eric W. Biederman28a6d672006-10-02 02:17:05 -07003275#endif
Ken Chen2ec220e2008-11-10 11:26:08 +03003276#ifdef CONFIG_STACKTRACE
Djalal Harouni35a35042014-04-07 15:38:36 -07003277 ONE("stack", S_IRUSR, proc_pid_stack),
Eric W. Biederman28a6d672006-10-02 02:17:05 -07003278#endif
Naveen N. Rao5968cec2015-06-30 14:36:03 +05303279#ifdef CONFIG_SCHED_INFO
Alexey Dobriyanf6e826c2014-08-08 14:21:46 -07003280 ONE("schedstat", S_IRUGO, proc_pid_schedstat),
Eric W. Biederman28a6d672006-10-02 02:17:05 -07003281#endif
Arjan van de Ven97455122008-01-25 21:08:34 +01003282#ifdef CONFIG_LATENCYTOP
Alexey Dobriyan631f9c12008-11-10 01:32:52 +03003283 REG("latency", S_IRUGO, proc_lstats_operations),
Arjan van de Ven97455122008-01-25 21:08:34 +01003284#endif
Paul Menage8793d852007-10-18 23:39:39 -07003285#ifdef CONFIG_PROC_PID_CPUSET
Zefan Li52de4772014-09-18 16:03:36 +08003286 ONE("cpuset", S_IRUGO, proc_cpuset_show),
Eric W. Biederman28a6d672006-10-02 02:17:05 -07003287#endif
Paul Menagea4243162007-10-18 23:39:35 -07003288#ifdef CONFIG_CGROUPS
Zefan Li006f4ac2014-09-18 16:03:15 +08003289 ONE("cgroup", S_IRUGO, proc_cgroup_show),
Paul Menagea4243162007-10-18 23:39:35 -07003290#endif
Alexey Dobriyan6ba51e32014-08-08 14:21:48 -07003291 ONE("oom_score", S_IRUGO, proc_oom_score),
David Rientjesfa0cbbf2012-11-12 17:53:04 -08003292 REG("oom_adj", S_IRUGO|S_IWUSR, proc_oom_adj_operations),
David Rientjesa63d83f2010-08-09 17:19:46 -07003293 REG("oom_score_adj", S_IRUGO|S_IWUSR, proc_oom_score_adj_operations),
Eric W. Biederman28a6d672006-10-02 02:17:05 -07003294#ifdef CONFIG_AUDITSYSCALL
Alexey Dobriyan631f9c12008-11-10 01:32:52 +03003295 REG("loginuid", S_IWUSR|S_IRUGO, proc_loginuid_operations),
Al Viro26ec3c62011-02-15 21:24:05 -05003296 REG("sessionid", S_IRUGO, proc_sessionid_operations),
Eric W. Biederman28a6d672006-10-02 02:17:05 -07003297#endif
Akinobu Mitaf4f154f2006-12-08 02:39:47 -08003298#ifdef CONFIG_FAULT_INJECTION
Alexey Dobriyan631f9c12008-11-10 01:32:52 +03003299 REG("make-it-fail", S_IRUGO|S_IWUSR, proc_fault_inject_operations),
Akinobu Mitaf4f154f2006-12-08 02:39:47 -08003300#endif
Andrea Righi297c5d92008-07-25 01:48:49 -07003301#ifdef CONFIG_TASK_IO_ACCOUNTING
Alexey Dobriyan19aadc92014-08-08 14:21:50 -07003302 ONE("io", S_IRUSR, proc_tid_io_accounting),
Andrea Righi297c5d92008-07-25 01:48:49 -07003303#endif
Chris Metcalff133ecc2011-05-26 12:40:09 -04003304#ifdef CONFIG_HARDWALL
Alexey Dobriyand962c142014-08-08 14:21:52 -07003305 ONE("hardwall", S_IRUGO, proc_pid_hardwall),
Chris Metcalff133ecc2011-05-26 12:40:09 -04003306#endif
Eric W. Biederman22d917d2011-11-17 00:11:58 -08003307#ifdef CONFIG_USER_NS
3308 REG("uid_map", S_IRUGO|S_IWUSR, proc_uid_map_operations),
3309 REG("gid_map", S_IRUGO|S_IWUSR, proc_gid_map_operations),
Eric W. Biedermanf76d2072012-08-30 01:24:05 -07003310 REG("projid_map", S_IRUGO|S_IWUSR, proc_projid_map_operations),
Eric W. Biederman9cc46512014-12-02 12:27:26 -06003311 REG("setgroups", S_IRUGO|S_IWUSR, proc_setgroups_operations),
Eric W. Biederman22d917d2011-11-17 00:11:58 -08003312#endif
Eric W. Biederman28a6d672006-10-02 02:17:05 -07003313};
3314
Al Virof0c3b502013-05-16 12:07:31 -04003315static int proc_tid_base_readdir(struct file *file, struct dir_context *ctx)
Eric W. Biederman28a6d672006-10-02 02:17:05 -07003316{
Al Virof0c3b502013-05-16 12:07:31 -04003317 return proc_pident_readdir(file, ctx,
3318 tid_base_stuff, ARRAY_SIZE(tid_base_stuff));
Eric W. Biederman28a6d672006-10-02 02:17:05 -07003319}
3320
Al Viro00cd8dd2012-06-10 17:13:09 -04003321static struct dentry *proc_tid_base_lookup(struct inode *dir, struct dentry *dentry, unsigned int flags)
3322{
Eric W. Biederman7bcd6b02006-10-02 02:18:56 -07003323 return proc_pident_lookup(dir, dentry,
3324 tid_base_stuff, ARRAY_SIZE(tid_base_stuff));
Eric W. Biederman28a6d672006-10-02 02:17:05 -07003325}
3326
Arjan van de Ven00977a52007-02-12 00:55:34 -08003327static const struct file_operations proc_tid_base_operations = {
Eric W. Biederman28a6d672006-10-02 02:17:05 -07003328 .read = generic_read_dir,
Al Virof50752e2016-04-20 17:13:54 -04003329 .iterate_shared = proc_tid_base_readdir,
3330 .llseek = generic_file_llseek,
Eric W. Biederman28a6d672006-10-02 02:17:05 -07003331};
3332
Arjan van de Venc5ef1c42007-02-12 00:55:40 -08003333static const struct inode_operations proc_tid_base_inode_operations = {
Eric W. Biederman28a6d672006-10-02 02:17:05 -07003334 .lookup = proc_tid_base_lookup,
3335 .getattr = pid_getattr,
3336 .setattr = proc_setattr,
3337};
3338
Al Viroc52a47a2013-06-15 11:15:20 +04003339static int proc_task_instantiate(struct inode *dir,
Eric Dumazetc5141e62007-05-08 00:26:15 -07003340 struct dentry *dentry, struct task_struct *task, const void *ptr)
Eric W. Biederman444ceed2006-10-02 02:18:49 -07003341{
Eric W. Biederman444ceed2006-10-02 02:18:49 -07003342 struct inode *inode;
Andreas Gruenbacherdb978da2016-11-10 22:18:28 +01003343 inode = proc_pid_make_inode(dir->i_sb, task, S_IFDIR | S_IRUGO | S_IXUGO);
Eric W. Biederman444ceed2006-10-02 02:18:49 -07003344
3345 if (!inode)
3346 goto out;
Eric W. Biederman444ceed2006-10-02 02:18:49 -07003347 inode->i_op = &proc_tid_base_inode_operations;
3348 inode->i_fop = &proc_tid_base_operations;
3349 inode->i_flags|=S_IMMUTABLE;
Vegard Nossumaed54172008-06-05 22:46:53 -07003350
Alexey Dobriyan1270dd82016-12-12 16:45:32 -08003351 set_nlink(inode, nlink_tid);
Eric W. Biederman444ceed2006-10-02 02:18:49 -07003352
Nick Pigginfb045ad2011-01-07 17:49:55 +11003353 d_set_d_op(dentry, &pid_dentry_operations);
Eric W. Biederman444ceed2006-10-02 02:18:49 -07003354
3355 d_add(dentry, inode);
3356 /* Close the race of the process dying before we return the dentry */
Al Viro0b728e12012-06-10 16:03:43 -04003357 if (pid_revalidate(dentry, 0))
Al Viroc52a47a2013-06-15 11:15:20 +04003358 return 0;
Eric W. Biederman444ceed2006-10-02 02:18:49 -07003359out:
Al Viroc52a47a2013-06-15 11:15:20 +04003360 return -ENOENT;
Eric W. Biederman444ceed2006-10-02 02:18:49 -07003361}
3362
Al Viro00cd8dd2012-06-10 17:13:09 -04003363static struct dentry *proc_task_lookup(struct inode *dir, struct dentry * dentry, unsigned int flags)
Eric W. Biederman28a6d672006-10-02 02:17:05 -07003364{
Al Viroc52a47a2013-06-15 11:15:20 +04003365 int result = -ENOENT;
Eric W. Biederman28a6d672006-10-02 02:17:05 -07003366 struct task_struct *task;
3367 struct task_struct *leader = get_proc_task(dir);
Eric W. Biederman28a6d672006-10-02 02:17:05 -07003368 unsigned tid;
Pavel Emelyanovb4888932007-10-18 23:40:14 -07003369 struct pid_namespace *ns;
Eric W. Biederman28a6d672006-10-02 02:17:05 -07003370
3371 if (!leader)
3372 goto out_no_task;
3373
Alexey Dobriyandbcdb502014-08-08 14:21:25 -07003374 tid = name_to_int(&dentry->d_name);
Eric W. Biederman28a6d672006-10-02 02:17:05 -07003375 if (tid == ~0U)
3376 goto out;
3377
Pavel Emelyanovb4888932007-10-18 23:40:14 -07003378 ns = dentry->d_sb->s_fs_info;
Eric W. Biederman28a6d672006-10-02 02:17:05 -07003379 rcu_read_lock();
Pavel Emelyanovb4888932007-10-18 23:40:14 -07003380 task = find_task_by_pid_ns(tid, ns);
Eric W. Biederman28a6d672006-10-02 02:17:05 -07003381 if (task)
3382 get_task_struct(task);
3383 rcu_read_unlock();
3384 if (!task)
3385 goto out;
Pavel Emelyanovbac0abd2007-10-18 23:40:18 -07003386 if (!same_thread_group(leader, task))
Eric W. Biederman28a6d672006-10-02 02:17:05 -07003387 goto out_drop_task;
3388
Eric W. Biederman444ceed2006-10-02 02:18:49 -07003389 result = proc_task_instantiate(dir, dentry, task, NULL);
Eric W. Biederman28a6d672006-10-02 02:17:05 -07003390out_drop_task:
3391 put_task_struct(task);
3392out:
3393 put_task_struct(leader);
3394out_no_task:
Al Viroc52a47a2013-06-15 11:15:20 +04003395 return ERR_PTR(result);
Eric W. Biederman28a6d672006-10-02 02:17:05 -07003396}
3397
3398/*
Eric W. Biederman0bc58a92006-06-26 00:25:50 -07003399 * Find the first tid of a thread group to return to user space.
3400 *
3401 * Usually this is just the thread group leader, but if the users
3402 * buffer was too small or there was a seek into the middle of the
3403 * directory we have more work todo.
3404 *
3405 * In the case of a short read we start with find_task_by_pid.
3406 *
3407 * In the case of a seek we start with the leader and walk nr
3408 * threads past it.
3409 */
Oleg Nesterov9f6e9632014-01-23 15:55:40 -08003410static struct task_struct *first_tid(struct pid *pid, int tid, loff_t f_pos,
3411 struct pid_namespace *ns)
Eric W. Biederman0bc58a92006-06-26 00:25:50 -07003412{
Oleg Nesterovd855a4b2014-01-23 15:55:39 -08003413 struct task_struct *pos, *task;
Oleg Nesterov9f6e9632014-01-23 15:55:40 -08003414 unsigned long nr = f_pos;
3415
3416 if (nr != f_pos) /* 32bit overflow? */
3417 return NULL;
Eric W. Biederman0bc58a92006-06-26 00:25:50 -07003418
Eric W. Biedermancc288732006-06-26 00:26:01 -07003419 rcu_read_lock();
Oleg Nesterovd855a4b2014-01-23 15:55:39 -08003420 task = pid_task(pid, PIDTYPE_PID);
3421 if (!task)
3422 goto fail;
3423
3424 /* Attempt to start with the tid of a thread */
Oleg Nesterov9f6e9632014-01-23 15:55:40 -08003425 if (tid && nr) {
Pavel Emelyanovb4888932007-10-18 23:40:14 -07003426 pos = find_task_by_pid_ns(tid, ns);
Oleg Nesterovd855a4b2014-01-23 15:55:39 -08003427 if (pos && same_thread_group(pos, task))
Oleg Nesterova872ff02006-06-26 00:26:01 -07003428 goto found;
Eric W. Biederman0bc58a92006-06-26 00:25:50 -07003429 }
3430
3431 /* If nr exceeds the number of threads there is nothing todo */
Oleg Nesterov9f6e9632014-01-23 15:55:40 -08003432 if (nr >= get_nr_threads(task))
Oleg Nesterovc986c142014-01-23 15:55:38 -08003433 goto fail;
Oleg Nesterova872ff02006-06-26 00:26:01 -07003434
3435 /* If we haven't found our starting place yet start
3436 * with the leader and walk nr threads forward.
3437 */
Oleg Nesterovd855a4b2014-01-23 15:55:39 -08003438 pos = task = task->group_leader;
Oleg Nesterovc986c142014-01-23 15:55:38 -08003439 do {
Oleg Nesterov9f6e9632014-01-23 15:55:40 -08003440 if (!nr--)
Oleg Nesterovc986c142014-01-23 15:55:38 -08003441 goto found;
Oleg Nesterovd855a4b2014-01-23 15:55:39 -08003442 } while_each_thread(task, pos);
Oleg Nesterovc986c142014-01-23 15:55:38 -08003443fail:
3444 pos = NULL;
3445 goto out;
Oleg Nesterova872ff02006-06-26 00:26:01 -07003446found:
3447 get_task_struct(pos);
3448out:
Eric W. Biedermancc288732006-06-26 00:26:01 -07003449 rcu_read_unlock();
Eric W. Biederman0bc58a92006-06-26 00:25:50 -07003450 return pos;
3451}
3452
3453/*
3454 * Find the next thread in the thread list.
3455 * Return NULL if there is an error or no next thread.
3456 *
3457 * The reference to the input task_struct is released.
3458 */
3459static struct task_struct *next_tid(struct task_struct *start)
3460{
Oleg Nesterovc1df7fb2006-06-26 00:26:02 -07003461 struct task_struct *pos = NULL;
Eric W. Biedermancc288732006-06-26 00:26:01 -07003462 rcu_read_lock();
Oleg Nesterovc1df7fb2006-06-26 00:26:02 -07003463 if (pid_alive(start)) {
Eric W. Biederman0bc58a92006-06-26 00:25:50 -07003464 pos = next_thread(start);
Oleg Nesterovc1df7fb2006-06-26 00:26:02 -07003465 if (thread_group_leader(pos))
3466 pos = NULL;
3467 else
3468 get_task_struct(pos);
3469 }
Eric W. Biedermancc288732006-06-26 00:26:01 -07003470 rcu_read_unlock();
Eric W. Biederman0bc58a92006-06-26 00:25:50 -07003471 put_task_struct(start);
3472 return pos;
3473}
3474
Linus Torvalds1da177e2005-04-16 15:20:36 -07003475/* for the /proc/TGID/task/ directories */
Al Virof0c3b502013-05-16 12:07:31 -04003476static int proc_task_readdir(struct file *file, struct dir_context *ctx)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003477{
Oleg Nesterovd855a4b2014-01-23 15:55:39 -08003478 struct inode *inode = file_inode(file);
3479 struct task_struct *task;
Pavel Emelyanovb4888932007-10-18 23:40:14 -07003480 struct pid_namespace *ns;
Al Virof0c3b502013-05-16 12:07:31 -04003481 int tid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003482
Oleg Nesterovd855a4b2014-01-23 15:55:39 -08003483 if (proc_inode_is_dead(inode))
Al Virof0c3b502013-05-16 12:07:31 -04003484 return -ENOENT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003485
Al Virof0c3b502013-05-16 12:07:31 -04003486 if (!dir_emit_dots(file, ctx))
Oleg Nesterovd855a4b2014-01-23 15:55:39 -08003487 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003488
Eric W. Biederman0bc58a92006-06-26 00:25:50 -07003489 /* f_version caches the tgid value that the last readdir call couldn't
3490 * return. lseek aka telldir automagically resets f_version to 0.
3491 */
Al Viro3aa33772014-10-31 00:42:35 -04003492 ns = inode->i_sb->s_fs_info;
Al Virof0c3b502013-05-16 12:07:31 -04003493 tid = (int)file->f_version;
3494 file->f_version = 0;
Oleg Nesterovd855a4b2014-01-23 15:55:39 -08003495 for (task = first_tid(proc_pid(inode), tid, ctx->pos - 2, ns);
Eric W. Biederman0bc58a92006-06-26 00:25:50 -07003496 task;
Al Virof0c3b502013-05-16 12:07:31 -04003497 task = next_tid(task), ctx->pos++) {
3498 char name[PROC_NUMBUF];
3499 int len;
Pavel Emelyanovb4888932007-10-18 23:40:14 -07003500 tid = task_pid_nr_ns(task, ns);
Al Virof0c3b502013-05-16 12:07:31 -04003501 len = snprintf(name, sizeof(name), "%d", tid);
3502 if (!proc_fill_cache(file, ctx, name, len,
3503 proc_task_instantiate, task, NULL)) {
Eric W. Biederman0bc58a92006-06-26 00:25:50 -07003504 /* returning this tgid failed, save it as the first
3505 * pid for the next readir call */
Al Virof0c3b502013-05-16 12:07:31 -04003506 file->f_version = (u64)tid;
Eric W. Biederman0bc58a92006-06-26 00:25:50 -07003507 put_task_struct(task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003508 break;
Eric W. Biederman0bc58a92006-06-26 00:25:50 -07003509 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003510 }
Oleg Nesterovd855a4b2014-01-23 15:55:39 -08003511
Al Virof0c3b502013-05-16 12:07:31 -04003512 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003513}
Eric W. Biederman6e66b522006-06-26 00:25:47 -07003514
David Howellsa528d352017-01-31 16:46:22 +00003515static int proc_task_getattr(const struct path *path, struct kstat *stat,
3516 u32 request_mask, unsigned int query_flags)
Eric W. Biederman6e66b522006-06-26 00:25:47 -07003517{
David Howellsa528d352017-01-31 16:46:22 +00003518 struct inode *inode = d_inode(path->dentry);
Eric W. Biederman99f89552006-06-26 00:25:55 -07003519 struct task_struct *p = get_proc_task(inode);
Eric W. Biederman6e66b522006-06-26 00:25:47 -07003520 generic_fillattr(inode, stat);
3521
Eric W. Biederman99f89552006-06-26 00:25:55 -07003522 if (p) {
Eric W. Biederman99f89552006-06-26 00:25:55 -07003523 stat->nlink += get_nr_threads(p);
Eric W. Biederman99f89552006-06-26 00:25:55 -07003524 put_task_struct(p);
Eric W. Biederman6e66b522006-06-26 00:25:47 -07003525 }
3526
3527 return 0;
3528}
Eric W. Biederman28a6d672006-10-02 02:17:05 -07003529
Arjan van de Venc5ef1c42007-02-12 00:55:40 -08003530static const struct inode_operations proc_task_inode_operations = {
Eric W. Biederman28a6d672006-10-02 02:17:05 -07003531 .lookup = proc_task_lookup,
3532 .getattr = proc_task_getattr,
3533 .setattr = proc_setattr,
Vasiliy Kulikov04996802012-01-10 15:11:31 -08003534 .permission = proc_pid_permission,
Eric W. Biederman28a6d672006-10-02 02:17:05 -07003535};
3536
Arjan van de Ven00977a52007-02-12 00:55:34 -08003537static const struct file_operations proc_task_operations = {
Eric W. Biederman28a6d672006-10-02 02:17:05 -07003538 .read = generic_read_dir,
Al Virof50752e2016-04-20 17:13:54 -04003539 .iterate_shared = proc_task_readdir,
3540 .llseek = generic_file_llseek,
Eric W. Biederman28a6d672006-10-02 02:17:05 -07003541};
Alexey Dobriyan1270dd82016-12-12 16:45:32 -08003542
3543void __init set_proc_pid_nlink(void)
3544{
3545 nlink_tid = pid_entry_nlink(tid_base_stuff, ARRAY_SIZE(tid_base_stuff));
3546 nlink_tgid = pid_entry_nlink(tgid_base_stuff, ARRAY_SIZE(tgid_base_stuff));
3547}