blob: c4d963a121626fc9a550cb28ba03b6a9cb402308 [file] [log] [blame]
Greg Kroah-Hartmanb2441312017-11-01 15:07:57 +01001// SPDX-License-Identifier: GPL-2.0
Linus Torvalds1da177e2005-04-16 15:20:36 -07002/*
3 * linux/fs/proc/base.c
4 *
5 * Copyright (C) 1991, 1992 Linus Torvalds
6 *
7 * proc base directory handling functions
8 *
9 * 1999, Al Viro. Rewritten. Now it covers the whole per-process part.
10 * Instead of using magical inumbers to determine the kind of object
11 * we allocate and fill in-core inodes upon lookup. They don't even
12 * go into icache. We cache the reference to task_struct upon lookup too.
13 * Eventually it should become a filesystem in its own. We don't use the
14 * rest of procfs anymore.
Mauricio Line070ad42005-09-03 15:55:10 -070015 *
16 *
17 * Changelog:
18 * 17-Jan-2005
19 * Allan Bezerra
20 * Bruna Moreira <bruna.moreira@indt.org.br>
21 * Edjard Mota <edjard.mota@indt.org.br>
22 * Ilias Biris <ilias.biris@indt.org.br>
23 * Mauricio Lin <mauricio.lin@indt.org.br>
24 *
25 * Embedded Linux Lab - 10LE Instituto Nokia de Tecnologia - INdT
26 *
27 * A new process specific entry (smaps) included in /proc. It shows the
28 * size of rss for each memory area. The maps entry lacks information
29 * about physical memory size (rss) for each mapped file, i.e.,
30 * rss information for executables and library files.
31 * This additional information is useful for any tools that need to know
32 * about physical memory consumption for a process specific library.
33 *
34 * Changelog:
35 * 21-Feb-2005
36 * Embedded Linux Lab - 10LE Instituto Nokia de Tecnologia - INdT
37 * Pud inclusion in the page table walking.
38 *
39 * ChangeLog:
40 * 10-Mar-2005
41 * 10LE Instituto Nokia de Tecnologia - INdT:
42 * A better way to walks through the page table as suggested by Hugh Dickins.
43 *
44 * Simo Piiroinen <simo.piiroinen@nokia.com>:
45 * Smaps information related to shared, private, clean and dirty pages.
46 *
47 * Paul Mundt <paul.mundt@nokia.com>:
48 * Overall revision about smaps.
Linus Torvalds1da177e2005-04-16 15:20:36 -070049 */
50
Linus Torvalds7c0f6ba2016-12-24 11:46:01 -080051#include <linux/uaccess.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070052
Linus Torvalds1da177e2005-04-16 15:20:36 -070053#include <linux/errno.h>
54#include <linux/time.h>
55#include <linux/proc_fs.h>
56#include <linux/stat.h>
Andrea Righi59954772008-07-27 17:29:15 +020057#include <linux/task_io_accounting_ops.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070058#include <linux/init.h>
Randy Dunlap16f7e0f2006-01-11 12:17:46 -080059#include <linux/capability.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070060#include <linux/file.h>
Al Viro9f3acc32008-04-24 07:44:08 -040061#include <linux/fdtable.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070062#include <linux/string.h>
63#include <linux/seq_file.h>
64#include <linux/namei.h>
Kirill Korotaev6b3286e2006-12-08 02:37:56 -080065#include <linux/mnt_namespace.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070066#include <linux/mm.h>
David Rientjesa63d83f2010-08-09 17:19:46 -070067#include <linux/swap.h>
Dipankar Sarmab8359962005-09-09 13:04:14 -070068#include <linux/rcupdate.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070069#include <linux/kallsyms.h>
Ken Chen2ec220e2008-11-10 11:26:08 +030070#include <linux/stacktrace.h>
Neil Hormand85f50d2007-10-18 23:40:37 -070071#include <linux/resource.h>
Kees Cook5096add2007-05-08 00:26:04 -070072#include <linux/module.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070073#include <linux/mount.h>
74#include <linux/security.h>
75#include <linux/ptrace.h>
Roland McGrath0d094ef2008-07-25 19:45:49 -070076#include <linux/tracehook.h>
Andrew Morton87ebdc02013-02-27 17:03:16 -080077#include <linux/printk.h>
Alexey Dobriyanefb1a572018-02-06 15:37:24 -080078#include <linux/cache.h>
Paul Menagea4243162007-10-18 23:39:35 -070079#include <linux/cgroup.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070080#include <linux/cpuset.h>
81#include <linux/audit.h>
Al Viro5addc5d2005-11-07 17:15:49 -050082#include <linux/poll.h>
Serge E. Hallyn1651e142006-10-02 02:18:08 -070083#include <linux/nsproxy.h>
Alexey Dobriyan8ac773b2006-10-19 23:28:32 -070084#include <linux/oom.h>
Kawai, Hidehiro3cb4a0b2007-07-19 01:48:28 -070085#include <linux/elf.h>
Pavel Emelyanov60347f62007-10-18 23:40:03 -070086#include <linux/pid_namespace.h>
Eric W. Biederman22d917d2011-11-17 00:11:58 -080087#include <linux/user_namespace.h>
Al Viro5ad4e532009-03-29 19:50:06 -040088#include <linux/fs_struct.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090089#include <linux/slab.h>
Ingo Molnar4eb5aaa2017-02-08 18:51:29 +010090#include <linux/sched/autogroup.h>
Ingo Molnar6e84f312017-02-08 18:51:29 +010091#include <linux/sched/mm.h>
Ingo Molnarf7ccbae2017-02-08 18:51:30 +010092#include <linux/sched/coredump.h>
Ingo Molnarb17b0152017-02-08 18:51:35 +010093#include <linux/sched/debug.h>
Ingo Molnar3905f9a2017-02-05 12:07:04 +010094#include <linux/sched/stat.h>
Pavel Emelyanov640708a2012-01-10 15:11:23 -080095#include <linux/flex_array.h>
Pavel Emelyanov48f6a7a2013-03-11 13:12:45 +040096#include <linux/posix-timers.h>
KAMEZAWA Hiroyuki43d2b112012-01-10 15:08:09 -080097#include <trace/events/oom.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070098#include "internal.h"
Cyrill Gorcunovfaf60af2012-08-23 14:43:24 +040099#include "fd.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -0700100
Alexey Dobriyanac7f1062018-02-06 15:36:59 -0800101#include "../../lib/kstrtox.h"
102
Eric W. Biederman0f2fe202006-06-26 00:25:46 -0700103/* NOTE:
104 * Implementing inode permission operations in /proc is almost
105 * certainly an error. Permission checks need to happen during
106 * each system call not at open time. The reason is that most of
107 * what we wish to check for permissions in /proc varies at runtime.
108 *
109 * The classic example of a problem is opening file descriptors
110 * in /proc for a task before it execs a suid executable.
111 */
112
Alexey Dobriyanefb1a572018-02-06 15:37:24 -0800113static u8 nlink_tid __ro_after_init;
114static u8 nlink_tgid __ro_after_init;
Alexey Dobriyan1270dd82016-12-12 16:45:32 -0800115
Linus Torvalds1da177e2005-04-16 15:20:36 -0700116struct pid_entry {
Alexey Dobriyancedbcca2014-08-08 14:21:33 -0700117 const char *name;
Alexey Dobriyan623f5942016-12-12 16:45:08 -0800118 unsigned int len;
Al Virod161a132011-07-24 03:36:29 -0400119 umode_t mode;
Arjan van de Venc5ef1c42007-02-12 00:55:40 -0800120 const struct inode_operations *iop;
Arjan van de Ven00977a52007-02-12 00:55:34 -0800121 const struct file_operations *fop;
Eric W. Biederman20cdc892006-10-02 02:17:07 -0700122 union proc_op op;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700123};
124
Eric W. Biederman61a28782006-10-02 02:18:49 -0700125#define NOD(NAME, MODE, IOP, FOP, OP) { \
Eric W. Biederman20cdc892006-10-02 02:17:07 -0700126 .name = (NAME), \
Eric Dumazetc5141e62007-05-08 00:26:15 -0700127 .len = sizeof(NAME) - 1, \
Eric W. Biederman20cdc892006-10-02 02:17:07 -0700128 .mode = MODE, \
129 .iop = IOP, \
130 .fop = FOP, \
131 .op = OP, \
132}
133
Alexey Dobriyan631f9c12008-11-10 01:32:52 +0300134#define DIR(NAME, MODE, iops, fops) \
135 NOD(NAME, (S_IFDIR|(MODE)), &iops, &fops, {} )
136#define LNK(NAME, get_link) \
Eric W. Biederman61a28782006-10-02 02:18:49 -0700137 NOD(NAME, (S_IFLNK|S_IRWXUGO), \
Eric W. Biederman20cdc892006-10-02 02:17:07 -0700138 &proc_pid_link_inode_operations, NULL, \
Alexey Dobriyan631f9c12008-11-10 01:32:52 +0300139 { .proc_get_link = get_link } )
140#define REG(NAME, MODE, fops) \
141 NOD(NAME, (S_IFREG|(MODE)), NULL, &fops, {})
Alexey Dobriyan631f9c12008-11-10 01:32:52 +0300142#define ONE(NAME, MODE, show) \
Eric W. Biedermanbe614082008-02-08 04:18:30 -0800143 NOD(NAME, (S_IFREG|(MODE)), \
144 NULL, &proc_single_file_operations, \
Alexey Dobriyan631f9c12008-11-10 01:32:52 +0300145 { .proc_show = show } )
Linus Torvalds1da177e2005-04-16 15:20:36 -0700146
Vegard Nossumaed54172008-06-05 22:46:53 -0700147/*
148 * Count the number of hardlinks for the pid_entry table, excluding the .
149 * and .. links.
150 */
Alexey Dobriyan1270dd82016-12-12 16:45:32 -0800151static unsigned int __init pid_entry_nlink(const struct pid_entry *entries,
Vegard Nossumaed54172008-06-05 22:46:53 -0700152 unsigned int n)
153{
154 unsigned int i;
155 unsigned int count;
156
Alexey Dobriyan1270dd82016-12-12 16:45:32 -0800157 count = 2;
Vegard Nossumaed54172008-06-05 22:46:53 -0700158 for (i = 0; i < n; ++i) {
159 if (S_ISDIR(entries[i].mode))
160 ++count;
161 }
162
163 return count;
164}
165
Miklos Szeredif7ad3c62010-08-10 11:41:36 +0200166static int get_task_root(struct task_struct *task, struct path *root)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700167{
Hugh Dickins7c2c7d92009-03-28 23:21:27 +0000168 int result = -ENOENT;
169
Miklos Szeredi0494f6e2005-09-06 15:18:22 -0700170 task_lock(task);
Miklos Szeredif7ad3c62010-08-10 11:41:36 +0200171 if (task->fs) {
172 get_fs_root(task->fs, root);
Hugh Dickins7c2c7d92009-03-28 23:21:27 +0000173 result = 0;
174 }
Miklos Szeredi0494f6e2005-09-06 15:18:22 -0700175 task_unlock(task);
Hugh Dickins7c2c7d92009-03-28 23:21:27 +0000176 return result;
Miklos Szeredi0494f6e2005-09-06 15:18:22 -0700177}
178
Cyrill Gorcunov7773fbc2012-01-10 15:11:20 -0800179static int proc_cwd_link(struct dentry *dentry, struct path *path)
Miklos Szeredi0494f6e2005-09-06 15:18:22 -0700180{
David Howells2b0143b2015-03-17 22:25:59 +0000181 struct task_struct *task = get_proc_task(d_inode(dentry));
Miklos Szeredi0494f6e2005-09-06 15:18:22 -0700182 int result = -ENOENT;
Eric W. Biederman99f89552006-06-26 00:25:55 -0700183
184 if (task) {
Miklos Szeredif7ad3c62010-08-10 11:41:36 +0200185 task_lock(task);
186 if (task->fs) {
187 get_fs_pwd(task->fs, path);
188 result = 0;
189 }
190 task_unlock(task);
Eric W. Biederman99f89552006-06-26 00:25:55 -0700191 put_task_struct(task);
192 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700193 return result;
194}
195
Cyrill Gorcunov7773fbc2012-01-10 15:11:20 -0800196static int proc_root_link(struct dentry *dentry, struct path *path)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700197{
David Howells2b0143b2015-03-17 22:25:59 +0000198 struct task_struct *task = get_proc_task(d_inode(dentry));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700199 int result = -ENOENT;
Eric W. Biederman99f89552006-06-26 00:25:55 -0700200
201 if (task) {
Miklos Szeredif7ad3c62010-08-10 11:41:36 +0200202 result = get_task_root(task, path);
Eric W. Biederman99f89552006-06-26 00:25:55 -0700203 put_task_struct(task);
204 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700205 return result;
206}
207
Linus Torvaldse4b4e442018-05-17 13:04:17 -0700208static ssize_t get_mm_cmdline(struct mm_struct *mm, char __user *buf,
209 size_t _count, loff_t *pos)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700210{
Alexey Dobriyanc2c0bb42015-06-25 15:00:54 -0700211 char *page;
212 unsigned long count = _count;
213 unsigned long arg_start, arg_end, env_start, env_end;
214 unsigned long len1, len2, len;
215 unsigned long p;
216 char c;
217 ssize_t rv;
218
Alexey Dobriyanc2c0bb42015-06-25 15:00:54 -0700219 /* Check if process spawned far enough to have cmdline. */
Linus Torvaldse4b4e442018-05-17 13:04:17 -0700220 if (!mm->env_end)
221 return 0;
Alexey Dobriyanc2c0bb42015-06-25 15:00:54 -0700222
Michal Hocko0ee931c2017-09-13 16:28:29 -0700223 page = (char *)__get_free_page(GFP_KERNEL);
Linus Torvaldse4b4e442018-05-17 13:04:17 -0700224 if (!page)
225 return -ENOMEM;
Alexey Dobriyanc2c0bb42015-06-25 15:00:54 -0700226
227 down_read(&mm->mmap_sem);
228 arg_start = mm->arg_start;
229 arg_end = mm->arg_end;
230 env_start = mm->env_start;
231 env_end = mm->env_end;
232 up_read(&mm->mmap_sem);
233
234 BUG_ON(arg_start > arg_end);
235 BUG_ON(env_start > env_end);
236
237 len1 = arg_end - arg_start;
238 len2 = env_end - env_start;
239
Alexey Dobriyan3581d452015-07-17 16:24:09 -0700240 /* Empty ARGV. */
241 if (len1 == 0) {
242 rv = 0;
243 goto out_free_page;
244 }
Alexey Dobriyan2ca66ff2014-08-08 14:21:41 -0700245 /*
Alexey Dobriyanc2c0bb42015-06-25 15:00:54 -0700246 * Inherently racy -- command line shares address space
247 * with code and data.
Alexey Dobriyan2ca66ff2014-08-08 14:21:41 -0700248 */
Willy Tarreau7f7ccc22018-05-11 08:11:44 +0200249 rv = access_remote_vm(mm, arg_end - 1, &c, 1, FOLL_ANON);
Alexey Dobriyanc2c0bb42015-06-25 15:00:54 -0700250 if (rv <= 0)
251 goto out_free_page;
252
253 rv = 0;
254
255 if (c == '\0') {
256 /* Command line (set of strings) occupies whole ARGV. */
257 if (len1 <= *pos)
258 goto out_free_page;
259
260 p = arg_start + *pos;
261 len = len1 - *pos;
262 while (count > 0 && len > 0) {
263 unsigned int _count;
264 int nr_read;
265
266 _count = min3(count, len, PAGE_SIZE);
Willy Tarreau7f7ccc22018-05-11 08:11:44 +0200267 nr_read = access_remote_vm(mm, p, page, _count, FOLL_ANON);
Alexey Dobriyanc2c0bb42015-06-25 15:00:54 -0700268 if (nr_read < 0)
269 rv = nr_read;
270 if (nr_read <= 0)
271 goto out_free_page;
272
273 if (copy_to_user(buf, page, nr_read)) {
274 rv = -EFAULT;
275 goto out_free_page;
276 }
277
278 p += nr_read;
279 len -= nr_read;
280 buf += nr_read;
281 count -= nr_read;
282 rv += nr_read;
283 }
284 } else {
285 /*
Alexey Dobriyanc2c0bb42015-06-25 15:00:54 -0700286 * Command line (1 string) occupies ARGV and
287 * extends into ENVP.
288 */
Alexey Dobriyana0a07b82017-02-24 15:00:20 -0800289 struct {
290 unsigned long p;
291 unsigned long len;
292 } cmdline[2] = {
293 { .p = arg_start, .len = len1 },
294 { .p = env_start, .len = len2 },
295 };
296 loff_t pos1 = *pos;
297 unsigned int i;
298
299 i = 0;
300 while (i < 2 && pos1 >= cmdline[i].len) {
301 pos1 -= cmdline[i].len;
302 i++;
Alexey Dobriyanc2c0bb42015-06-25 15:00:54 -0700303 }
Alexey Dobriyana0a07b82017-02-24 15:00:20 -0800304 while (i < 2) {
305 p = cmdline[i].p + pos1;
306 len = cmdline[i].len - pos1;
307 while (count > 0 && len > 0) {
308 unsigned int _count, l;
309 int nr_read;
310 bool final;
Alexey Dobriyanc2c0bb42015-06-25 15:00:54 -0700311
Alexey Dobriyana0a07b82017-02-24 15:00:20 -0800312 _count = min3(count, len, PAGE_SIZE);
Willy Tarreau7f7ccc22018-05-11 08:11:44 +0200313 nr_read = access_remote_vm(mm, p, page, _count, FOLL_ANON);
Alexey Dobriyana0a07b82017-02-24 15:00:20 -0800314 if (nr_read < 0)
315 rv = nr_read;
316 if (nr_read <= 0)
317 goto out_free_page;
Alexey Dobriyanc2c0bb42015-06-25 15:00:54 -0700318
Alexey Dobriyana0a07b82017-02-24 15:00:20 -0800319 /*
320 * Command line can be shorter than whole ARGV
321 * even if last "marker" byte says it is not.
322 */
323 final = false;
324 l = strnlen(page, nr_read);
325 if (l < nr_read) {
326 nr_read = l;
327 final = true;
328 }
329
330 if (copy_to_user(buf, page, nr_read)) {
331 rv = -EFAULT;
332 goto out_free_page;
333 }
334
335 p += nr_read;
336 len -= nr_read;
337 buf += nr_read;
338 count -= nr_read;
339 rv += nr_read;
340
341 if (final)
342 goto out_free_page;
Alexey Dobriyanc2c0bb42015-06-25 15:00:54 -0700343 }
344
Alexey Dobriyana0a07b82017-02-24 15:00:20 -0800345 /* Only first chunk can be read partially. */
346 pos1 = 0;
347 i++;
Alexey Dobriyanc2c0bb42015-06-25 15:00:54 -0700348 }
Alexey Dobriyanc2c0bb42015-06-25 15:00:54 -0700349 }
350
351out_free_page:
352 free_page((unsigned long)page);
Alexey Dobriyanc2c0bb42015-06-25 15:00:54 -0700353 return rv;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700354}
355
Linus Torvaldse4b4e442018-05-17 13:04:17 -0700356static ssize_t get_task_cmdline(struct task_struct *tsk, char __user *buf,
357 size_t count, loff_t *pos)
358{
359 struct mm_struct *mm;
360 ssize_t ret;
361
362 mm = get_task_mm(tsk);
363 if (!mm)
364 return 0;
365
366 ret = get_mm_cmdline(mm, buf, count, pos);
367 mmput(mm);
368 return ret;
369}
370
371static ssize_t proc_pid_cmdline_read(struct file *file, char __user *buf,
372 size_t count, loff_t *pos)
373{
374 struct task_struct *tsk;
375 ssize_t ret;
376
377 BUG_ON(*pos < 0);
378
379 tsk = get_proc_task(file_inode(file));
380 if (!tsk)
381 return -ESRCH;
382 ret = get_task_cmdline(tsk, buf, count, pos);
383 put_task_struct(tsk);
384 if (ret > 0)
385 *pos += ret;
386 return ret;
387}
388
Alexey Dobriyanc2c0bb42015-06-25 15:00:54 -0700389static const struct file_operations proc_pid_cmdline_ops = {
390 .read = proc_pid_cmdline_read,
391 .llseek = generic_file_llseek,
392};
393
Linus Torvalds1da177e2005-04-16 15:20:36 -0700394#ifdef CONFIG_KALLSYMS
395/*
396 * Provides a wchan file via kallsyms in a proper one-value-per-file format.
397 * Returns the resolved symbol. If that fails, simply return the address.
398 */
Alexey Dobriyanedfcd602014-08-08 14:21:44 -0700399static int proc_pid_wchan(struct seq_file *m, struct pid_namespace *ns,
400 struct pid *pid, struct task_struct *task)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700401{
Alexey Dobriyanffb45122007-05-08 00:28:41 -0700402 unsigned long wchan;
Tejun Heo9281ace2007-07-17 04:03:51 -0700403 char symname[KSYM_NAME_LEN];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700404
Alexey Dobriyan24b2ec22018-04-10 16:31:30 -0700405 if (!ptrace_may_access(task, PTRACE_MODE_READ_FSCREDS))
406 goto print0;
407
Linus Torvalds1da177e2005-04-16 15:20:36 -0700408 wchan = get_wchan(task);
Alexey Dobriyan24b2ec22018-04-10 16:31:30 -0700409 if (wchan && !lookup_symbol_name(wchan, symname)) {
Alexey Dobriyan21dae0a2018-04-10 16:31:34 -0700410 seq_puts(m, symname);
Alexey Dobriyan24b2ec22018-04-10 16:31:30 -0700411 return 0;
412 }
Joe Perches25ce3192015-04-15 16:18:17 -0700413
Alexey Dobriyan24b2ec22018-04-10 16:31:30 -0700414print0:
415 seq_putc(m, '0');
Joe Perches25ce3192015-04-15 16:18:17 -0700416 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700417}
418#endif /* CONFIG_KALLSYMS */
419
Al Viroa9712bc2011-03-23 15:52:50 -0400420static int lock_trace(struct task_struct *task)
421{
422 int err = mutex_lock_killable(&task->signal->cred_guard_mutex);
423 if (err)
424 return err;
Jann Horncaaee622016-01-20 15:00:04 -0800425 if (!ptrace_may_access(task, PTRACE_MODE_ATTACH_FSCREDS)) {
Al Viroa9712bc2011-03-23 15:52:50 -0400426 mutex_unlock(&task->signal->cred_guard_mutex);
427 return -EPERM;
428 }
429 return 0;
430}
431
432static void unlock_trace(struct task_struct *task)
433{
434 mutex_unlock(&task->signal->cred_guard_mutex);
435}
436
Ken Chen2ec220e2008-11-10 11:26:08 +0300437#ifdef CONFIG_STACKTRACE
438
439#define MAX_STACK_TRACE_DEPTH 64
440
441static int proc_pid_stack(struct seq_file *m, struct pid_namespace *ns,
442 struct pid *pid, struct task_struct *task)
443{
444 struct stack_trace trace;
445 unsigned long *entries;
Al Viroa9712bc2011-03-23 15:52:50 -0400446 int err;
Ken Chen2ec220e2008-11-10 11:26:08 +0300447 int i;
448
449 entries = kmalloc(MAX_STACK_TRACE_DEPTH * sizeof(*entries), GFP_KERNEL);
450 if (!entries)
451 return -ENOMEM;
452
453 trace.nr_entries = 0;
454 trace.max_entries = MAX_STACK_TRACE_DEPTH;
455 trace.entries = entries;
456 trace.skip = 0;
Ken Chen2ec220e2008-11-10 11:26:08 +0300457
Al Viroa9712bc2011-03-23 15:52:50 -0400458 err = lock_trace(task);
459 if (!err) {
460 save_stack_trace_tsk(task, &trace);
461
462 for (i = 0; i < trace.nr_entries; i++) {
Linus Torvalds8f5abe82017-11-27 16:45:56 -0800463 seq_printf(m, "[<0>] %pB\n", (void *)entries[i]);
Al Viroa9712bc2011-03-23 15:52:50 -0400464 }
465 unlock_trace(task);
Ken Chen2ec220e2008-11-10 11:26:08 +0300466 }
467 kfree(entries);
468
Al Viroa9712bc2011-03-23 15:52:50 -0400469 return err;
Ken Chen2ec220e2008-11-10 11:26:08 +0300470}
471#endif
472
Naveen N. Rao5968cec2015-06-30 14:36:03 +0530473#ifdef CONFIG_SCHED_INFO
Linus Torvalds1da177e2005-04-16 15:20:36 -0700474/*
475 * Provides /proc/PID/schedstat
476 */
Alexey Dobriyanf6e826c2014-08-08 14:21:46 -0700477static int proc_pid_schedstat(struct seq_file *m, struct pid_namespace *ns,
478 struct pid *pid, struct task_struct *task)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700479{
Naveen N. Rao5968cec2015-06-30 14:36:03 +0530480 if (unlikely(!sched_info_on()))
481 seq_printf(m, "0 0 0\n");
482 else
483 seq_printf(m, "%llu %llu %lu\n",
Joe Perches25ce3192015-04-15 16:18:17 -0700484 (unsigned long long)task->se.sum_exec_runtime,
485 (unsigned long long)task->sched_info.run_delay,
486 task->sched_info.pcount);
487
488 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700489}
490#endif
491
Arjan van de Ven97455122008-01-25 21:08:34 +0100492#ifdef CONFIG_LATENCYTOP
493static int lstats_show_proc(struct seq_file *m, void *v)
494{
495 int i;
Hiroshi Shimamoto13d77c32008-02-20 16:53:29 -0800496 struct inode *inode = m->private;
497 struct task_struct *task = get_proc_task(inode);
Arjan van de Ven97455122008-01-25 21:08:34 +0100498
Hiroshi Shimamoto13d77c32008-02-20 16:53:29 -0800499 if (!task)
500 return -ESRCH;
501 seq_puts(m, "Latency Top version : v0.1\n");
Arjan van de Ven97455122008-01-25 21:08:34 +0100502 for (i = 0; i < 32; i++) {
Joe Perches34e49d42011-01-12 17:00:30 -0800503 struct latency_record *lr = &task->latency_record[i];
504 if (lr->backtrace[0]) {
Arjan van de Ven97455122008-01-25 21:08:34 +0100505 int q;
Joe Perches34e49d42011-01-12 17:00:30 -0800506 seq_printf(m, "%i %li %li",
507 lr->count, lr->time, lr->max);
Arjan van de Ven97455122008-01-25 21:08:34 +0100508 for (q = 0; q < LT_BACKTRACEDEPTH; q++) {
Joe Perches34e49d42011-01-12 17:00:30 -0800509 unsigned long bt = lr->backtrace[q];
510 if (!bt)
Arjan van de Ven97455122008-01-25 21:08:34 +0100511 break;
Joe Perches34e49d42011-01-12 17:00:30 -0800512 if (bt == ULONG_MAX)
Arjan van de Ven97455122008-01-25 21:08:34 +0100513 break;
Joe Perches34e49d42011-01-12 17:00:30 -0800514 seq_printf(m, " %ps", (void *)bt);
Arjan van de Ven97455122008-01-25 21:08:34 +0100515 }
Alexey Dobriyan9d6de122011-01-12 17:00:32 -0800516 seq_putc(m, '\n');
Arjan van de Ven97455122008-01-25 21:08:34 +0100517 }
518
519 }
Hiroshi Shimamoto13d77c32008-02-20 16:53:29 -0800520 put_task_struct(task);
Arjan van de Ven97455122008-01-25 21:08:34 +0100521 return 0;
522}
523
524static int lstats_open(struct inode *inode, struct file *file)
525{
Hiroshi Shimamoto13d77c32008-02-20 16:53:29 -0800526 return single_open(file, lstats_show_proc, inode);
Hiroshi Shimamotod6643d12008-02-14 10:27:00 -0800527}
528
Arjan van de Ven97455122008-01-25 21:08:34 +0100529static ssize_t lstats_write(struct file *file, const char __user *buf,
530 size_t count, loff_t *offs)
531{
Al Viro496ad9a2013-01-23 17:07:38 -0500532 struct task_struct *task = get_proc_task(file_inode(file));
Arjan van de Ven97455122008-01-25 21:08:34 +0100533
Hiroshi Shimamoto13d77c32008-02-20 16:53:29 -0800534 if (!task)
535 return -ESRCH;
Arjan van de Ven97455122008-01-25 21:08:34 +0100536 clear_all_latency_tracing(task);
Hiroshi Shimamoto13d77c32008-02-20 16:53:29 -0800537 put_task_struct(task);
Arjan van de Ven97455122008-01-25 21:08:34 +0100538
539 return count;
540}
541
542static const struct file_operations proc_lstats_operations = {
543 .open = lstats_open,
544 .read = seq_read,
545 .write = lstats_write,
546 .llseek = seq_lseek,
Hiroshi Shimamoto13d77c32008-02-20 16:53:29 -0800547 .release = single_release,
Arjan van de Ven97455122008-01-25 21:08:34 +0100548};
549
550#endif
551
Alexey Dobriyan6ba51e32014-08-08 14:21:48 -0700552static int proc_oom_score(struct seq_file *m, struct pid_namespace *ns,
553 struct pid *pid, struct task_struct *task)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700554{
David Rientjesa7f638f2012-05-29 15:06:47 -0700555 unsigned long totalpages = totalram_pages + total_swap_pages;
Oleg Nesterovb95c35e2010-04-01 15:13:57 +0200556 unsigned long points = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700557
Oleg Nesterovef419392016-08-02 14:03:19 -0700558 points = oom_badness(task, NULL, NULL, totalpages) *
559 1000 / totalpages;
Joe Perches25ce3192015-04-15 16:18:17 -0700560 seq_printf(m, "%lu\n", points);
561
562 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700563}
564
Neil Hormand85f50d2007-10-18 23:40:37 -0700565struct limit_names {
Alexey Dobriyancedbcca2014-08-08 14:21:33 -0700566 const char *name;
567 const char *unit;
Neil Hormand85f50d2007-10-18 23:40:37 -0700568};
569
570static const struct limit_names lnames[RLIM_NLIMITS] = {
Kees Cookcff4edb2009-09-22 16:45:32 -0700571 [RLIMIT_CPU] = {"Max cpu time", "seconds"},
Neil Hormand85f50d2007-10-18 23:40:37 -0700572 [RLIMIT_FSIZE] = {"Max file size", "bytes"},
573 [RLIMIT_DATA] = {"Max data size", "bytes"},
574 [RLIMIT_STACK] = {"Max stack size", "bytes"},
575 [RLIMIT_CORE] = {"Max core file size", "bytes"},
576 [RLIMIT_RSS] = {"Max resident set", "bytes"},
577 [RLIMIT_NPROC] = {"Max processes", "processes"},
578 [RLIMIT_NOFILE] = {"Max open files", "files"},
579 [RLIMIT_MEMLOCK] = {"Max locked memory", "bytes"},
580 [RLIMIT_AS] = {"Max address space", "bytes"},
581 [RLIMIT_LOCKS] = {"Max file locks", "locks"},
582 [RLIMIT_SIGPENDING] = {"Max pending signals", "signals"},
583 [RLIMIT_MSGQUEUE] = {"Max msgqueue size", "bytes"},
584 [RLIMIT_NICE] = {"Max nice priority", NULL},
585 [RLIMIT_RTPRIO] = {"Max realtime priority", NULL},
Eugene Teo88081172008-02-23 15:23:52 -0800586 [RLIMIT_RTTIME] = {"Max realtime timeout", "us"},
Neil Hormand85f50d2007-10-18 23:40:37 -0700587};
588
589/* Display limits for a process */
Alexey Dobriyan1c963eb2014-08-08 14:21:37 -0700590static int proc_pid_limits(struct seq_file *m, struct pid_namespace *ns,
591 struct pid *pid, struct task_struct *task)
Neil Hormand85f50d2007-10-18 23:40:37 -0700592{
593 unsigned int i;
Neil Hormand85f50d2007-10-18 23:40:37 -0700594 unsigned long flags;
Neil Hormand85f50d2007-10-18 23:40:37 -0700595
596 struct rlimit rlim[RLIM_NLIMITS];
597
Lai Jiangshana6bebbc2008-10-05 00:51:15 +0400598 if (!lock_task_sighand(task, &flags))
Neil Hormand85f50d2007-10-18 23:40:37 -0700599 return 0;
Neil Hormand85f50d2007-10-18 23:40:37 -0700600 memcpy(rlim, task->signal->rlim, sizeof(struct rlimit) * RLIM_NLIMITS);
601 unlock_task_sighand(task, &flags);
Neil Hormand85f50d2007-10-18 23:40:37 -0700602
603 /*
604 * print the file header
605 */
Alexey Dobriyan1c963eb2014-08-08 14:21:37 -0700606 seq_printf(m, "%-25s %-20s %-20s %-10s\n",
Joe Perches25ce3192015-04-15 16:18:17 -0700607 "Limit", "Soft Limit", "Hard Limit", "Units");
Neil Hormand85f50d2007-10-18 23:40:37 -0700608
609 for (i = 0; i < RLIM_NLIMITS; i++) {
610 if (rlim[i].rlim_cur == RLIM_INFINITY)
Alexey Dobriyan1c963eb2014-08-08 14:21:37 -0700611 seq_printf(m, "%-25s %-20s ",
Joe Perches25ce3192015-04-15 16:18:17 -0700612 lnames[i].name, "unlimited");
Neil Hormand85f50d2007-10-18 23:40:37 -0700613 else
Alexey Dobriyan1c963eb2014-08-08 14:21:37 -0700614 seq_printf(m, "%-25s %-20lu ",
Joe Perches25ce3192015-04-15 16:18:17 -0700615 lnames[i].name, rlim[i].rlim_cur);
Neil Hormand85f50d2007-10-18 23:40:37 -0700616
617 if (rlim[i].rlim_max == RLIM_INFINITY)
Alexey Dobriyan1c963eb2014-08-08 14:21:37 -0700618 seq_printf(m, "%-20s ", "unlimited");
Neil Hormand85f50d2007-10-18 23:40:37 -0700619 else
Alexey Dobriyan1c963eb2014-08-08 14:21:37 -0700620 seq_printf(m, "%-20lu ", rlim[i].rlim_max);
Neil Hormand85f50d2007-10-18 23:40:37 -0700621
622 if (lnames[i].unit)
Alexey Dobriyan1c963eb2014-08-08 14:21:37 -0700623 seq_printf(m, "%-10s\n", lnames[i].unit);
Neil Hormand85f50d2007-10-18 23:40:37 -0700624 else
Alexey Dobriyan1c963eb2014-08-08 14:21:37 -0700625 seq_putc(m, '\n');
Neil Hormand85f50d2007-10-18 23:40:37 -0700626 }
627
Alexey Dobriyan1c963eb2014-08-08 14:21:37 -0700628 return 0;
Neil Hormand85f50d2007-10-18 23:40:37 -0700629}
630
Roland McGrathebcb6732008-07-25 19:46:00 -0700631#ifdef CONFIG_HAVE_ARCH_TRACEHOOK
Alexey Dobriyan09d93bd2014-08-08 14:21:39 -0700632static int proc_pid_syscall(struct seq_file *m, struct pid_namespace *ns,
633 struct pid *pid, struct task_struct *task)
Roland McGrathebcb6732008-07-25 19:46:00 -0700634{
635 long nr;
636 unsigned long args[6], sp, pc;
Joe Perches25ce3192015-04-15 16:18:17 -0700637 int res;
638
639 res = lock_trace(task);
Al Viroa9712bc2011-03-23 15:52:50 -0400640 if (res)
641 return res;
Roland McGrathebcb6732008-07-25 19:46:00 -0700642
643 if (task_current_syscall(task, &nr, args, 6, &sp, &pc))
Alexey Dobriyan09d93bd2014-08-08 14:21:39 -0700644 seq_puts(m, "running\n");
Al Viroa9712bc2011-03-23 15:52:50 -0400645 else if (nr < 0)
Alexey Dobriyan09d93bd2014-08-08 14:21:39 -0700646 seq_printf(m, "%ld 0x%lx 0x%lx\n", nr, sp, pc);
Al Viroa9712bc2011-03-23 15:52:50 -0400647 else
Alexey Dobriyan09d93bd2014-08-08 14:21:39 -0700648 seq_printf(m,
Roland McGrathebcb6732008-07-25 19:46:00 -0700649 "%ld 0x%lx 0x%lx 0x%lx 0x%lx 0x%lx 0x%lx 0x%lx 0x%lx\n",
650 nr,
651 args[0], args[1], args[2], args[3], args[4], args[5],
652 sp, pc);
Al Viroa9712bc2011-03-23 15:52:50 -0400653 unlock_trace(task);
Joe Perches25ce3192015-04-15 16:18:17 -0700654
655 return 0;
Roland McGrathebcb6732008-07-25 19:46:00 -0700656}
657#endif /* CONFIG_HAVE_ARCH_TRACEHOOK */
658
Linus Torvalds1da177e2005-04-16 15:20:36 -0700659/************************************************************************/
660/* Here the fs part begins */
661/************************************************************************/
662
663/* permission checks */
Eric W. Biederman778c1142006-06-26 00:25:58 -0700664static int proc_fd_access_allowed(struct inode *inode)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700665{
Eric W. Biederman778c1142006-06-26 00:25:58 -0700666 struct task_struct *task;
667 int allowed = 0;
Eric W. Biedermandf26c402006-06-26 00:25:59 -0700668 /* Allow access to a task's file descriptors if it is us or we
669 * may use ptrace attach to the process and find out that
670 * information.
Eric W. Biederman778c1142006-06-26 00:25:58 -0700671 */
672 task = get_proc_task(inode);
Eric W. Biedermandf26c402006-06-26 00:25:59 -0700673 if (task) {
Jann Horncaaee622016-01-20 15:00:04 -0800674 allowed = ptrace_may_access(task, PTRACE_MODE_READ_FSCREDS);
Eric W. Biederman778c1142006-06-26 00:25:58 -0700675 put_task_struct(task);
Eric W. Biedermandf26c402006-06-26 00:25:59 -0700676 }
Eric W. Biederman778c1142006-06-26 00:25:58 -0700677 return allowed;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700678}
679
Eric W. Biederman6b4e3062010-03-07 16:41:34 -0800680int proc_setattr(struct dentry *dentry, struct iattr *attr)
Linus Torvalds6d76fa52006-07-15 12:26:45 -0700681{
682 int error;
David Howells2b0143b2015-03-17 22:25:59 +0000683 struct inode *inode = d_inode(dentry);
Linus Torvalds6d76fa52006-07-15 12:26:45 -0700684
685 if (attr->ia_valid & ATTR_MODE)
686 return -EPERM;
687
Jan Kara31051c82016-05-26 16:55:18 +0200688 error = setattr_prepare(dentry, attr);
Christoph Hellwig10257742010-06-04 11:30:02 +0200689 if (error)
690 return error;
691
Christoph Hellwig10257742010-06-04 11:30:02 +0200692 setattr_copy(inode, attr);
693 mark_inode_dirty(inode);
694 return 0;
Linus Torvalds6d76fa52006-07-15 12:26:45 -0700695}
696
Vasiliy Kulikov04996802012-01-10 15:11:31 -0800697/*
698 * May current process learn task's sched/cmdline info (for hide_pid_min=1)
699 * or euid/egid (for hide_pid_min=2)?
700 */
701static bool has_pid_permissions(struct pid_namespace *pid,
702 struct task_struct *task,
703 int hide_pid_min)
704{
705 if (pid->hide_pid < hide_pid_min)
706 return true;
707 if (in_group_p(pid->pid_gid))
708 return true;
Jann Horncaaee622016-01-20 15:00:04 -0800709 return ptrace_may_access(task, PTRACE_MODE_READ_FSCREDS);
Vasiliy Kulikov04996802012-01-10 15:11:31 -0800710}
711
712
713static int proc_pid_permission(struct inode *inode, int mask)
714{
715 struct pid_namespace *pid = inode->i_sb->s_fs_info;
716 struct task_struct *task;
717 bool has_perms;
718
719 task = get_proc_task(inode);
Xiaotian Fenga2ef9902012-01-12 17:17:08 -0800720 if (!task)
721 return -ESRCH;
Lafcadio Wluiki796f5712017-02-24 15:00:23 -0800722 has_perms = has_pid_permissions(pid, task, HIDEPID_NO_ACCESS);
Vasiliy Kulikov04996802012-01-10 15:11:31 -0800723 put_task_struct(task);
724
725 if (!has_perms) {
Lafcadio Wluiki796f5712017-02-24 15:00:23 -0800726 if (pid->hide_pid == HIDEPID_INVISIBLE) {
Vasiliy Kulikov04996802012-01-10 15:11:31 -0800727 /*
728 * Let's make getdents(), stat(), and open()
729 * consistent with each other. If a process
730 * may not stat() a file, it shouldn't be seen
731 * in procfs at all.
732 */
733 return -ENOENT;
734 }
735
736 return -EPERM;
737 }
738 return generic_permission(inode, mask);
739}
740
741
742
Arjan van de Venc5ef1c42007-02-12 00:55:40 -0800743static const struct inode_operations proc_def_inode_operations = {
Linus Torvalds6d76fa52006-07-15 12:26:45 -0700744 .setattr = proc_setattr,
745};
746
Eric W. Biedermanbe614082008-02-08 04:18:30 -0800747static int proc_single_show(struct seq_file *m, void *v)
748{
749 struct inode *inode = m->private;
750 struct pid_namespace *ns;
751 struct pid *pid;
752 struct task_struct *task;
753 int ret;
754
755 ns = inode->i_sb->s_fs_info;
756 pid = proc_pid(inode);
757 task = get_pid_task(pid, PIDTYPE_PID);
758 if (!task)
759 return -ESRCH;
760
761 ret = PROC_I(inode)->op.proc_show(m, ns, pid, task);
762
763 put_task_struct(task);
764 return ret;
765}
766
767static int proc_single_open(struct inode *inode, struct file *filp)
768{
Jovi Zhangc6a34052011-01-12 17:00:34 -0800769 return single_open(filp, proc_single_show, inode);
Eric W. Biedermanbe614082008-02-08 04:18:30 -0800770}
771
772static const struct file_operations proc_single_file_operations = {
773 .open = proc_single_open,
774 .read = seq_read,
775 .llseek = seq_lseek,
776 .release = single_release,
777};
778
Oleg Nesterov5381e162014-10-09 15:25:24 -0700779
780struct mm_struct *proc_mem_open(struct inode *inode, unsigned int mode)
781{
782 struct task_struct *task = get_proc_task(inode);
783 struct mm_struct *mm = ERR_PTR(-ESRCH);
784
785 if (task) {
Jann Horncaaee622016-01-20 15:00:04 -0800786 mm = mm_access(task, mode | PTRACE_MODE_FSCREDS);
Oleg Nesterov5381e162014-10-09 15:25:24 -0700787 put_task_struct(task);
788
789 if (!IS_ERR_OR_NULL(mm)) {
790 /* ensure this mm_struct can't be freed */
Vegard Nossumf1f10072017-02-27 14:30:07 -0800791 mmgrab(mm);
Oleg Nesterov5381e162014-10-09 15:25:24 -0700792 /* but do not pin its memory */
793 mmput(mm);
794 }
795 }
796
797 return mm;
798}
799
Cong Wangb409e572012-05-31 16:26:17 -0700800static int __mem_open(struct inode *inode, struct file *file, unsigned int mode)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700801{
Oleg Nesterov5381e162014-10-09 15:25:24 -0700802 struct mm_struct *mm = proc_mem_open(inode, mode);
Linus Torvaldse2683372012-01-17 15:21:19 -0800803
804 if (IS_ERR(mm))
805 return PTR_ERR(mm);
806
Linus Torvaldse2683372012-01-17 15:21:19 -0800807 file->private_data = mm;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700808 return 0;
809}
810
Cong Wangb409e572012-05-31 16:26:17 -0700811static int mem_open(struct inode *inode, struct file *file)
812{
Djalal Harounibc452b42012-07-30 14:42:28 -0700813 int ret = __mem_open(inode, file, PTRACE_MODE_ATTACH);
814
815 /* OK to pass negative loff_t, we can catch out-of-range */
816 file->f_mode |= FMODE_UNSIGNED_OFFSET;
817
818 return ret;
Cong Wangb409e572012-05-31 16:26:17 -0700819}
820
Oleg Nesterov572d34b2012-01-31 17:14:54 +0100821static ssize_t mem_rw(struct file *file, char __user *buf,
822 size_t count, loff_t *ppos, int write)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700823{
Linus Torvaldse2683372012-01-17 15:21:19 -0800824 struct mm_struct *mm = file->private_data;
Oleg Nesterov572d34b2012-01-31 17:14:54 +0100825 unsigned long addr = *ppos;
826 ssize_t copied;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700827 char *page;
Linus Torvalds272ddc82016-10-24 19:00:44 -0700828 unsigned int flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700829
Linus Torvaldse2683372012-01-17 15:21:19 -0800830 if (!mm)
831 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700832
Michal Hocko0ee931c2017-09-13 16:28:29 -0700833 page = (char *)__get_free_page(GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700834 if (!page)
Linus Torvaldse2683372012-01-17 15:21:19 -0800835 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700836
Frederik Deweerdtf7ca54f2006-09-29 02:01:02 -0700837 copied = 0;
Vegard Nossum388f7932017-02-27 14:30:13 -0800838 if (!mmget_not_zero(mm))
Oleg Nesterov6d08f2c2012-01-31 17:15:11 +0100839 goto free;
840
Linus Torvaldsf511c0b2017-05-30 12:38:59 -0700841 flags = FOLL_FORCE | (write ? FOLL_WRITE : 0);
Lorenzo Stoakes6347e8d2016-10-13 01:20:19 +0100842
Linus Torvalds1da177e2005-04-16 15:20:36 -0700843 while (count > 0) {
Oleg Nesterov572d34b2012-01-31 17:14:54 +0100844 int this_len = min_t(int, count, PAGE_SIZE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700845
Oleg Nesterov572d34b2012-01-31 17:14:54 +0100846 if (write && copy_from_user(page, buf, this_len)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700847 copied = -EFAULT;
848 break;
849 }
Oleg Nesterov572d34b2012-01-31 17:14:54 +0100850
Lorenzo Stoakes6347e8d2016-10-13 01:20:19 +0100851 this_len = access_remote_vm(mm, addr, page, this_len, flags);
Oleg Nesterov572d34b2012-01-31 17:14:54 +0100852 if (!this_len) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700853 if (!copied)
854 copied = -EIO;
855 break;
856 }
Oleg Nesterov572d34b2012-01-31 17:14:54 +0100857
858 if (!write && copy_to_user(buf, page, this_len)) {
859 copied = -EFAULT;
860 break;
861 }
862
863 buf += this_len;
864 addr += this_len;
865 copied += this_len;
866 count -= this_len;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700867 }
Oleg Nesterov572d34b2012-01-31 17:14:54 +0100868 *ppos = addr;
KOSAKI Motohiro30cd8902011-05-26 16:25:52 -0700869
Oleg Nesterov6d08f2c2012-01-31 17:15:11 +0100870 mmput(mm);
871free:
KOSAKI Motohiro30cd8902011-05-26 16:25:52 -0700872 free_page((unsigned long) page);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700873 return copied;
874}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700875
Oleg Nesterov572d34b2012-01-31 17:14:54 +0100876static ssize_t mem_read(struct file *file, char __user *buf,
877 size_t count, loff_t *ppos)
878{
879 return mem_rw(file, buf, count, ppos, 0);
880}
881
882static ssize_t mem_write(struct file *file, const char __user *buf,
883 size_t count, loff_t *ppos)
884{
885 return mem_rw(file, (char __user*)buf, count, ppos, 1);
886}
887
Matt Mackall85863e42008-02-04 22:29:04 -0800888loff_t mem_lseek(struct file *file, loff_t offset, int orig)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700889{
890 switch (orig) {
891 case 0:
892 file->f_pos = offset;
893 break;
894 case 1:
895 file->f_pos += offset;
896 break;
897 default:
898 return -EINVAL;
899 }
900 force_successful_syscall_return();
901 return file->f_pos;
902}
903
Linus Torvaldse2683372012-01-17 15:21:19 -0800904static int mem_release(struct inode *inode, struct file *file)
905{
906 struct mm_struct *mm = file->private_data;
Oleg Nesterov71879d32012-01-31 17:14:38 +0100907 if (mm)
Oleg Nesterov6d08f2c2012-01-31 17:15:11 +0100908 mmdrop(mm);
Linus Torvaldse2683372012-01-17 15:21:19 -0800909 return 0;
910}
911
Arjan van de Ven00977a52007-02-12 00:55:34 -0800912static const struct file_operations proc_mem_operations = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700913 .llseek = mem_lseek,
914 .read = mem_read,
915 .write = mem_write,
916 .open = mem_open,
Linus Torvaldse2683372012-01-17 15:21:19 -0800917 .release = mem_release,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700918};
919
Cong Wangb409e572012-05-31 16:26:17 -0700920static int environ_open(struct inode *inode, struct file *file)
921{
922 return __mem_open(inode, file, PTRACE_MODE_READ);
923}
924
James Pearson315e28c2007-10-16 23:30:17 -0700925static ssize_t environ_read(struct file *file, char __user *buf,
926 size_t count, loff_t *ppos)
927{
James Pearson315e28c2007-10-16 23:30:17 -0700928 char *page;
929 unsigned long src = *ppos;
Cong Wangb409e572012-05-31 16:26:17 -0700930 int ret = 0;
931 struct mm_struct *mm = file->private_data;
Mateusz Guzika3b609e2016-01-20 15:01:05 -0800932 unsigned long env_start, env_end;
James Pearson315e28c2007-10-16 23:30:17 -0700933
Mathias Krause8148a732016-05-05 16:22:26 -0700934 /* Ensure the process spawned far enough to have an environment. */
935 if (!mm || !mm->env_end)
Cong Wangb409e572012-05-31 16:26:17 -0700936 return 0;
James Pearson315e28c2007-10-16 23:30:17 -0700937
Michal Hocko0ee931c2017-09-13 16:28:29 -0700938 page = (char *)__get_free_page(GFP_KERNEL);
James Pearson315e28c2007-10-16 23:30:17 -0700939 if (!page)
Cong Wangb409e572012-05-31 16:26:17 -0700940 return -ENOMEM;
James Pearson315e28c2007-10-16 23:30:17 -0700941
Al Virod6f64b82011-02-15 22:26:01 -0500942 ret = 0;
Vegard Nossum388f7932017-02-27 14:30:13 -0800943 if (!mmget_not_zero(mm))
Cong Wangb409e572012-05-31 16:26:17 -0700944 goto free;
Mateusz Guzika3b609e2016-01-20 15:01:05 -0800945
946 down_read(&mm->mmap_sem);
947 env_start = mm->env_start;
948 env_end = mm->env_end;
949 up_read(&mm->mmap_sem);
950
James Pearson315e28c2007-10-16 23:30:17 -0700951 while (count > 0) {
Djalal Harounie8905ec2012-07-30 14:42:26 -0700952 size_t this_len, max_len;
953 int retval;
954
Mateusz Guzika3b609e2016-01-20 15:01:05 -0800955 if (src >= (env_end - env_start))
Djalal Harounie8905ec2012-07-30 14:42:26 -0700956 break;
James Pearson315e28c2007-10-16 23:30:17 -0700957
Mateusz Guzika3b609e2016-01-20 15:01:05 -0800958 this_len = env_end - (env_start + src);
James Pearson315e28c2007-10-16 23:30:17 -0700959
Djalal Harounie8905ec2012-07-30 14:42:26 -0700960 max_len = min_t(size_t, PAGE_SIZE, count);
961 this_len = min(max_len, this_len);
James Pearson315e28c2007-10-16 23:30:17 -0700962
Willy Tarreau7f7ccc22018-05-11 08:11:44 +0200963 retval = access_remote_vm(mm, (env_start + src), page, this_len, FOLL_ANON);
James Pearson315e28c2007-10-16 23:30:17 -0700964
965 if (retval <= 0) {
966 ret = retval;
967 break;
968 }
969
970 if (copy_to_user(buf, page, retval)) {
971 ret = -EFAULT;
972 break;
973 }
974
975 ret += retval;
976 src += retval;
977 buf += retval;
978 count -= retval;
979 }
980 *ppos = src;
James Pearson315e28c2007-10-16 23:30:17 -0700981 mmput(mm);
Cong Wangb409e572012-05-31 16:26:17 -0700982
983free:
James Pearson315e28c2007-10-16 23:30:17 -0700984 free_page((unsigned long) page);
James Pearson315e28c2007-10-16 23:30:17 -0700985 return ret;
986}
987
988static const struct file_operations proc_environ_operations = {
Cong Wangb409e572012-05-31 16:26:17 -0700989 .open = environ_open,
James Pearson315e28c2007-10-16 23:30:17 -0700990 .read = environ_read,
Arnd Bergmann87df8422010-03-17 23:06:02 +0100991 .llseek = generic_file_llseek,
Cong Wangb409e572012-05-31 16:26:17 -0700992 .release = mem_release,
James Pearson315e28c2007-10-16 23:30:17 -0700993};
994
Al Viroc5317162016-10-05 18:43:43 -0400995static int auxv_open(struct inode *inode, struct file *file)
996{
997 return __mem_open(inode, file, PTRACE_MODE_READ_FSCREDS);
998}
999
1000static ssize_t auxv_read(struct file *file, char __user *buf,
1001 size_t count, loff_t *ppos)
1002{
1003 struct mm_struct *mm = file->private_data;
1004 unsigned int nwords = 0;
Leon Yu06b28492016-10-27 17:46:50 -07001005
1006 if (!mm)
1007 return 0;
Al Viroc5317162016-10-05 18:43:43 -04001008 do {
1009 nwords += 2;
1010 } while (mm->saved_auxv[nwords - 2] != 0); /* AT_NULL */
1011 return simple_read_from_buffer(buf, count, ppos, mm->saved_auxv,
1012 nwords * sizeof(mm->saved_auxv[0]));
1013}
1014
1015static const struct file_operations proc_auxv_operations = {
1016 .open = auxv_open,
1017 .read = auxv_read,
1018 .llseek = generic_file_llseek,
1019 .release = mem_release,
1020};
1021
David Rientjesfa0cbbf2012-11-12 17:53:04 -08001022static ssize_t oom_adj_read(struct file *file, char __user *buf, size_t count,
1023 loff_t *ppos)
1024{
Al Viro496ad9a2013-01-23 17:07:38 -05001025 struct task_struct *task = get_proc_task(file_inode(file));
David Rientjesfa0cbbf2012-11-12 17:53:04 -08001026 char buffer[PROC_NUMBUF];
1027 int oom_adj = OOM_ADJUST_MIN;
1028 size_t len;
David Rientjesfa0cbbf2012-11-12 17:53:04 -08001029
1030 if (!task)
1031 return -ESRCH;
Michal Hockof913da52016-07-28 15:44:37 -07001032 if (task->signal->oom_score_adj == OOM_SCORE_ADJ_MAX)
1033 oom_adj = OOM_ADJUST_MAX;
1034 else
1035 oom_adj = (task->signal->oom_score_adj * -OOM_DISABLE) /
1036 OOM_SCORE_ADJ_MAX;
David Rientjesfa0cbbf2012-11-12 17:53:04 -08001037 put_task_struct(task);
1038 len = snprintf(buffer, sizeof(buffer), "%d\n", oom_adj);
1039 return simple_read_from_buffer(buf, count, ppos, buffer, len);
1040}
1041
Michal Hocko1d5f0ac2016-07-28 15:44:40 -07001042static int __set_oom_adj(struct file *file, int oom_adj, bool legacy)
1043{
1044 static DEFINE_MUTEX(oom_adj_mutex);
Michal Hocko44a70ade2016-07-28 15:44:43 -07001045 struct mm_struct *mm = NULL;
Michal Hocko1d5f0ac2016-07-28 15:44:40 -07001046 struct task_struct *task;
1047 int err = 0;
1048
1049 task = get_proc_task(file_inode(file));
1050 if (!task)
1051 return -ESRCH;
1052
1053 mutex_lock(&oom_adj_mutex);
1054 if (legacy) {
1055 if (oom_adj < task->signal->oom_score_adj &&
1056 !capable(CAP_SYS_RESOURCE)) {
1057 err = -EACCES;
1058 goto err_unlock;
1059 }
1060 /*
1061 * /proc/pid/oom_adj is provided for legacy purposes, ask users to use
1062 * /proc/pid/oom_score_adj instead.
1063 */
1064 pr_warn_once("%s (%d): /proc/%d/oom_adj is deprecated, please use /proc/%d/oom_score_adj instead.\n",
1065 current->comm, task_pid_nr(current), task_pid_nr(task),
1066 task_pid_nr(task));
1067 } else {
1068 if ((short)oom_adj < task->signal->oom_score_adj_min &&
1069 !capable(CAP_SYS_RESOURCE)) {
1070 err = -EACCES;
1071 goto err_unlock;
1072 }
1073 }
1074
Michal Hocko44a70ade2016-07-28 15:44:43 -07001075 /*
1076 * Make sure we will check other processes sharing the mm if this is
1077 * not vfrok which wants its own oom_score_adj.
1078 * pin the mm so it doesn't go away and get reused after task_unlock
1079 */
1080 if (!task->vfork_done) {
1081 struct task_struct *p = find_lock_task_mm(task);
1082
1083 if (p) {
1084 if (atomic_read(&p->mm->mm_users) > 1) {
1085 mm = p->mm;
Vegard Nossumf1f10072017-02-27 14:30:07 -08001086 mmgrab(mm);
Michal Hocko44a70ade2016-07-28 15:44:43 -07001087 }
1088 task_unlock(p);
1089 }
1090 }
1091
Michal Hocko1d5f0ac2016-07-28 15:44:40 -07001092 task->signal->oom_score_adj = oom_adj;
1093 if (!legacy && has_capability_noaudit(current, CAP_SYS_RESOURCE))
1094 task->signal->oom_score_adj_min = (short)oom_adj;
1095 trace_oom_score_adj_update(task);
Michal Hocko44a70ade2016-07-28 15:44:43 -07001096
1097 if (mm) {
1098 struct task_struct *p;
1099
1100 rcu_read_lock();
1101 for_each_process(p) {
1102 if (same_thread_group(task, p))
1103 continue;
1104
1105 /* do not touch kernel threads or the global init */
1106 if (p->flags & PF_KTHREAD || is_global_init(p))
1107 continue;
1108
1109 task_lock(p);
1110 if (!p->vfork_done && process_shares_mm(p, mm)) {
1111 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",
1112 task_pid_nr(p), p->comm,
1113 p->signal->oom_score_adj, oom_adj,
1114 task_pid_nr(task), task->comm);
1115 p->signal->oom_score_adj = oom_adj;
1116 if (!legacy && has_capability_noaudit(current, CAP_SYS_RESOURCE))
1117 p->signal->oom_score_adj_min = (short)oom_adj;
1118 }
1119 task_unlock(p);
1120 }
1121 rcu_read_unlock();
1122 mmdrop(mm);
1123 }
Michal Hocko1d5f0ac2016-07-28 15:44:40 -07001124err_unlock:
1125 mutex_unlock(&oom_adj_mutex);
1126 put_task_struct(task);
1127 return err;
1128}
Michal Hockof913da52016-07-28 15:44:37 -07001129
David Rientjesb72bdfa2015-11-05 18:50:32 -08001130/*
1131 * /proc/pid/oom_adj exists solely for backwards compatibility with previous
1132 * kernels. The effective policy is defined by oom_score_adj, which has a
1133 * different scale: oom_adj grew exponentially and oom_score_adj grows linearly.
1134 * Values written to oom_adj are simply mapped linearly to oom_score_adj.
1135 * Processes that become oom disabled via oom_adj will still be oom disabled
1136 * with this implementation.
1137 *
1138 * oom_adj cannot be removed since existing userspace binaries use it.
1139 */
David Rientjesfa0cbbf2012-11-12 17:53:04 -08001140static ssize_t oom_adj_write(struct file *file, const char __user *buf,
1141 size_t count, loff_t *ppos)
1142{
David Rientjesfa0cbbf2012-11-12 17:53:04 -08001143 char buffer[PROC_NUMBUF];
1144 int oom_adj;
David Rientjesfa0cbbf2012-11-12 17:53:04 -08001145 int err;
1146
1147 memset(buffer, 0, sizeof(buffer));
1148 if (count > sizeof(buffer) - 1)
1149 count = sizeof(buffer) - 1;
1150 if (copy_from_user(buffer, buf, count)) {
1151 err = -EFAULT;
1152 goto out;
1153 }
1154
1155 err = kstrtoint(strstrip(buffer), 0, &oom_adj);
1156 if (err)
1157 goto out;
1158 if ((oom_adj < OOM_ADJUST_MIN || oom_adj > OOM_ADJUST_MAX) &&
1159 oom_adj != OOM_DISABLE) {
1160 err = -EINVAL;
1161 goto out;
1162 }
1163
David Rientjesfa0cbbf2012-11-12 17:53:04 -08001164 /*
1165 * Scale /proc/pid/oom_score_adj appropriately ensuring that a maximum
1166 * value is always attainable.
1167 */
1168 if (oom_adj == OOM_ADJUST_MAX)
1169 oom_adj = OOM_SCORE_ADJ_MAX;
1170 else
1171 oom_adj = (oom_adj * OOM_SCORE_ADJ_MAX) / -OOM_DISABLE;
1172
Michal Hocko1d5f0ac2016-07-28 15:44:40 -07001173 err = __set_oom_adj(file, oom_adj, true);
David Rientjesfa0cbbf2012-11-12 17:53:04 -08001174out:
1175 return err < 0 ? err : count;
1176}
1177
1178static const struct file_operations proc_oom_adj_operations = {
1179 .read = oom_adj_read,
1180 .write = oom_adj_write,
1181 .llseek = generic_file_llseek,
1182};
1183
David Rientjesa63d83f2010-08-09 17:19:46 -07001184static ssize_t oom_score_adj_read(struct file *file, char __user *buf,
1185 size_t count, loff_t *ppos)
1186{
Al Viro496ad9a2013-01-23 17:07:38 -05001187 struct task_struct *task = get_proc_task(file_inode(file));
David Rientjesa63d83f2010-08-09 17:19:46 -07001188 char buffer[PROC_NUMBUF];
David Rientjesa9c58b902012-12-11 16:02:54 -08001189 short oom_score_adj = OOM_SCORE_ADJ_MIN;
David Rientjesa63d83f2010-08-09 17:19:46 -07001190 size_t len;
1191
1192 if (!task)
1193 return -ESRCH;
Michal Hockof913da52016-07-28 15:44:37 -07001194 oom_score_adj = task->signal->oom_score_adj;
David Rientjesa63d83f2010-08-09 17:19:46 -07001195 put_task_struct(task);
David Rientjesa9c58b902012-12-11 16:02:54 -08001196 len = snprintf(buffer, sizeof(buffer), "%hd\n", oom_score_adj);
David Rientjesa63d83f2010-08-09 17:19:46 -07001197 return simple_read_from_buffer(buf, count, ppos, buffer, len);
1198}
1199
1200static ssize_t oom_score_adj_write(struct file *file, const char __user *buf,
1201 size_t count, loff_t *ppos)
1202{
David Rientjesa63d83f2010-08-09 17:19:46 -07001203 char buffer[PROC_NUMBUF];
Alexey Dobriyan0a8cb8e2011-05-26 16:25:50 -07001204 int oom_score_adj;
David Rientjesa63d83f2010-08-09 17:19:46 -07001205 int err;
1206
1207 memset(buffer, 0, sizeof(buffer));
1208 if (count > sizeof(buffer) - 1)
1209 count = sizeof(buffer) - 1;
David Rientjes723548b2010-10-26 14:21:25 -07001210 if (copy_from_user(buffer, buf, count)) {
1211 err = -EFAULT;
1212 goto out;
1213 }
David Rientjesa63d83f2010-08-09 17:19:46 -07001214
Alexey Dobriyan0a8cb8e2011-05-26 16:25:50 -07001215 err = kstrtoint(strstrip(buffer), 0, &oom_score_adj);
David Rientjesa63d83f2010-08-09 17:19:46 -07001216 if (err)
David Rientjes723548b2010-10-26 14:21:25 -07001217 goto out;
David Rientjesa63d83f2010-08-09 17:19:46 -07001218 if (oom_score_adj < OOM_SCORE_ADJ_MIN ||
David Rientjes723548b2010-10-26 14:21:25 -07001219 oom_score_adj > OOM_SCORE_ADJ_MAX) {
1220 err = -EINVAL;
1221 goto out;
1222 }
David Rientjesa63d83f2010-08-09 17:19:46 -07001223
Michal Hocko1d5f0ac2016-07-28 15:44:40 -07001224 err = __set_oom_adj(file, oom_score_adj, false);
David Rientjes723548b2010-10-26 14:21:25 -07001225out:
1226 return err < 0 ? err : count;
David Rientjesa63d83f2010-08-09 17:19:46 -07001227}
1228
1229static const struct file_operations proc_oom_score_adj_operations = {
1230 .read = oom_score_adj_read,
1231 .write = oom_score_adj_write,
Arnd Bergmann6038f372010-08-15 18:52:59 +02001232 .llseek = default_llseek,
David Rientjesa63d83f2010-08-09 17:19:46 -07001233};
1234
Linus Torvalds1da177e2005-04-16 15:20:36 -07001235#ifdef CONFIG_AUDITSYSCALL
Alexey Dobriyanb4eb4f72016-10-29 19:04:39 +03001236#define TMPBUFLEN 11
Linus Torvalds1da177e2005-04-16 15:20:36 -07001237static ssize_t proc_loginuid_read(struct file * file, char __user * buf,
1238 size_t count, loff_t *ppos)
1239{
Al Viro496ad9a2013-01-23 17:07:38 -05001240 struct inode * inode = file_inode(file);
Eric W. Biederman99f89552006-06-26 00:25:55 -07001241 struct task_struct *task = get_proc_task(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001242 ssize_t length;
1243 char tmpbuf[TMPBUFLEN];
1244
Eric W. Biederman99f89552006-06-26 00:25:55 -07001245 if (!task)
1246 return -ESRCH;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001247 length = scnprintf(tmpbuf, TMPBUFLEN, "%u",
Eric W. Biedermane1760bd2012-09-10 22:39:43 -07001248 from_kuid(file->f_cred->user_ns,
1249 audit_get_loginuid(task)));
Eric W. Biederman99f89552006-06-26 00:25:55 -07001250 put_task_struct(task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001251 return simple_read_from_buffer(buf, count, ppos, tmpbuf, length);
1252}
1253
1254static ssize_t proc_loginuid_write(struct file * file, const char __user * buf,
1255 size_t count, loff_t *ppos)
1256{
Al Viro496ad9a2013-01-23 17:07:38 -05001257 struct inode * inode = file_inode(file);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001258 uid_t loginuid;
Eric W. Biedermane1760bd2012-09-10 22:39:43 -07001259 kuid_t kloginuid;
Alexey Dobriyan774636e2015-09-09 15:36:59 -07001260 int rv;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001261
Paul E. McKenney7dc52152010-02-22 17:04:52 -08001262 rcu_read_lock();
1263 if (current != pid_task(proc_pid(inode), PIDTYPE_PID)) {
1264 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001265 return -EPERM;
Paul E. McKenney7dc52152010-02-22 17:04:52 -08001266 }
1267 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001268
Linus Torvalds1da177e2005-04-16 15:20:36 -07001269 if (*ppos != 0) {
1270 /* No partial writes. */
1271 return -EINVAL;
1272 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001273
Alexey Dobriyan774636e2015-09-09 15:36:59 -07001274 rv = kstrtou32_from_user(buf, count, 10, &loginuid);
1275 if (rv < 0)
1276 return rv;
Eric Paris81407c82013-05-24 09:49:14 -04001277
1278 /* is userspace tring to explicitly UNSET the loginuid? */
1279 if (loginuid == AUDIT_UID_UNSET) {
1280 kloginuid = INVALID_UID;
1281 } else {
1282 kloginuid = make_kuid(file->f_cred->user_ns, loginuid);
Alexey Dobriyan774636e2015-09-09 15:36:59 -07001283 if (!uid_valid(kloginuid))
1284 return -EINVAL;
Eric W. Biedermane1760bd2012-09-10 22:39:43 -07001285 }
1286
Alexey Dobriyan774636e2015-09-09 15:36:59 -07001287 rv = audit_set_loginuid(kloginuid);
1288 if (rv < 0)
1289 return rv;
1290 return count;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001291}
1292
Arjan van de Ven00977a52007-02-12 00:55:34 -08001293static const struct file_operations proc_loginuid_operations = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001294 .read = proc_loginuid_read,
1295 .write = proc_loginuid_write,
Arnd Bergmann87df8422010-03-17 23:06:02 +01001296 .llseek = generic_file_llseek,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001297};
Eric Paris1e0bd752008-03-13 08:15:31 -04001298
1299static ssize_t proc_sessionid_read(struct file * file, char __user * buf,
1300 size_t count, loff_t *ppos)
1301{
Al Viro496ad9a2013-01-23 17:07:38 -05001302 struct inode * inode = file_inode(file);
Eric Paris1e0bd752008-03-13 08:15:31 -04001303 struct task_struct *task = get_proc_task(inode);
1304 ssize_t length;
1305 char tmpbuf[TMPBUFLEN];
1306
1307 if (!task)
1308 return -ESRCH;
1309 length = scnprintf(tmpbuf, TMPBUFLEN, "%u",
1310 audit_get_sessionid(task));
1311 put_task_struct(task);
1312 return simple_read_from_buffer(buf, count, ppos, tmpbuf, length);
1313}
1314
1315static const struct file_operations proc_sessionid_operations = {
1316 .read = proc_sessionid_read,
Arnd Bergmann87df8422010-03-17 23:06:02 +01001317 .llseek = generic_file_llseek,
Eric Paris1e0bd752008-03-13 08:15:31 -04001318};
Linus Torvalds1da177e2005-04-16 15:20:36 -07001319#endif
1320
Akinobu Mitaf4f154f2006-12-08 02:39:47 -08001321#ifdef CONFIG_FAULT_INJECTION
1322static ssize_t proc_fault_inject_read(struct file * file, char __user * buf,
1323 size_t count, loff_t *ppos)
1324{
Al Viro496ad9a2013-01-23 17:07:38 -05001325 struct task_struct *task = get_proc_task(file_inode(file));
Akinobu Mitaf4f154f2006-12-08 02:39:47 -08001326 char buffer[PROC_NUMBUF];
1327 size_t len;
1328 int make_it_fail;
Akinobu Mitaf4f154f2006-12-08 02:39:47 -08001329
1330 if (!task)
1331 return -ESRCH;
1332 make_it_fail = task->make_it_fail;
1333 put_task_struct(task);
1334
1335 len = snprintf(buffer, sizeof(buffer), "%i\n", make_it_fail);
Akinobu Mita0c28f282007-05-08 00:31:41 -07001336
1337 return simple_read_from_buffer(buf, count, ppos, buffer, len);
Akinobu Mitaf4f154f2006-12-08 02:39:47 -08001338}
1339
1340static ssize_t proc_fault_inject_write(struct file * file,
1341 const char __user * buf, size_t count, loff_t *ppos)
1342{
1343 struct task_struct *task;
Alexey Dobriyan774636e2015-09-09 15:36:59 -07001344 char buffer[PROC_NUMBUF];
Akinobu Mitaf4f154f2006-12-08 02:39:47 -08001345 int make_it_fail;
Alexey Dobriyan774636e2015-09-09 15:36:59 -07001346 int rv;
Akinobu Mitaf4f154f2006-12-08 02:39:47 -08001347
1348 if (!capable(CAP_SYS_RESOURCE))
1349 return -EPERM;
1350 memset(buffer, 0, sizeof(buffer));
1351 if (count > sizeof(buffer) - 1)
1352 count = sizeof(buffer) - 1;
1353 if (copy_from_user(buffer, buf, count))
1354 return -EFAULT;
Alexey Dobriyan774636e2015-09-09 15:36:59 -07001355 rv = kstrtoint(strstrip(buffer), 0, &make_it_fail);
1356 if (rv < 0)
1357 return rv;
Dave Jones16caed32014-04-07 15:39:15 -07001358 if (make_it_fail < 0 || make_it_fail > 1)
1359 return -EINVAL;
1360
Al Viro496ad9a2013-01-23 17:07:38 -05001361 task = get_proc_task(file_inode(file));
Akinobu Mitaf4f154f2006-12-08 02:39:47 -08001362 if (!task)
1363 return -ESRCH;
1364 task->make_it_fail = make_it_fail;
1365 put_task_struct(task);
Vincent Licba8aaf2009-09-22 16:45:38 -07001366
1367 return count;
Akinobu Mitaf4f154f2006-12-08 02:39:47 -08001368}
1369
Arjan van de Ven00977a52007-02-12 00:55:34 -08001370static const struct file_operations proc_fault_inject_operations = {
Akinobu Mitaf4f154f2006-12-08 02:39:47 -08001371 .read = proc_fault_inject_read,
1372 .write = proc_fault_inject_write,
Arnd Bergmann87df8422010-03-17 23:06:02 +01001373 .llseek = generic_file_llseek,
Akinobu Mitaf4f154f2006-12-08 02:39:47 -08001374};
Dmitry Vyukove41d58182017-07-12 14:34:35 -07001375
1376static ssize_t proc_fail_nth_write(struct file *file, const char __user *buf,
1377 size_t count, loff_t *ppos)
1378{
1379 struct task_struct *task;
Akinobu Mita9049f2f2017-07-14 14:49:52 -07001380 int err;
1381 unsigned int n;
Dmitry Vyukove41d58182017-07-12 14:34:35 -07001382
Akinobu Mita9049f2f2017-07-14 14:49:52 -07001383 err = kstrtouint_from_user(buf, count, 0, &n);
Dmitry Vyukove41d58182017-07-12 14:34:35 -07001384 if (err)
1385 return err;
Akinobu Mita1203c8e2017-07-14 14:49:57 -07001386
1387 task = get_proc_task(file_inode(file));
1388 if (!task)
1389 return -ESRCH;
Alexey Dobriyan9f7118b2018-02-06 15:36:55 -08001390 task->fail_nth = n;
Akinobu Mita1203c8e2017-07-14 14:49:57 -07001391 put_task_struct(task);
1392
Dmitry Vyukove41d58182017-07-12 14:34:35 -07001393 return count;
1394}
1395
1396static ssize_t proc_fail_nth_read(struct file *file, char __user *buf,
1397 size_t count, loff_t *ppos)
1398{
1399 struct task_struct *task;
Akinobu Mitabfc74092017-07-14 14:49:54 -07001400 char numbuf[PROC_NUMBUF];
1401 ssize_t len;
Dmitry Vyukove41d58182017-07-12 14:34:35 -07001402
1403 task = get_proc_task(file_inode(file));
1404 if (!task)
1405 return -ESRCH;
Alexey Dobriyan9f7118b2018-02-06 15:36:55 -08001406 len = snprintf(numbuf, sizeof(numbuf), "%u\n", task->fail_nth);
Akinobu Mitabfc74092017-07-14 14:49:54 -07001407 len = simple_read_from_buffer(buf, count, ppos, numbuf, len);
Akinobu Mita1203c8e2017-07-14 14:49:57 -07001408 put_task_struct(task);
Akinobu Mitabfc74092017-07-14 14:49:54 -07001409
1410 return len;
Dmitry Vyukove41d58182017-07-12 14:34:35 -07001411}
1412
1413static const struct file_operations proc_fail_nth_operations = {
1414 .read = proc_fail_nth_read,
1415 .write = proc_fail_nth_write,
1416};
Akinobu Mitaf4f154f2006-12-08 02:39:47 -08001417#endif
1418
Arjan van de Ven97455122008-01-25 21:08:34 +01001419
Ingo Molnar43ae34c2007-07-09 18:52:00 +02001420#ifdef CONFIG_SCHED_DEBUG
1421/*
1422 * Print out various scheduling related per-task fields:
1423 */
1424static int sched_show(struct seq_file *m, void *v)
1425{
1426 struct inode *inode = m->private;
Aleksa Sarai74dc3382017-08-06 14:41:41 +10001427 struct pid_namespace *ns = inode->i_sb->s_fs_info;
Ingo Molnar43ae34c2007-07-09 18:52:00 +02001428 struct task_struct *p;
1429
Ingo Molnar43ae34c2007-07-09 18:52:00 +02001430 p = get_proc_task(inode);
1431 if (!p)
1432 return -ESRCH;
Aleksa Sarai74dc3382017-08-06 14:41:41 +10001433 proc_sched_show_task(p, ns, m);
Ingo Molnar43ae34c2007-07-09 18:52:00 +02001434
1435 put_task_struct(p);
1436
1437 return 0;
1438}
1439
1440static ssize_t
1441sched_write(struct file *file, const char __user *buf,
1442 size_t count, loff_t *offset)
1443{
Al Viro496ad9a2013-01-23 17:07:38 -05001444 struct inode *inode = file_inode(file);
Ingo Molnar43ae34c2007-07-09 18:52:00 +02001445 struct task_struct *p;
1446
Ingo Molnar43ae34c2007-07-09 18:52:00 +02001447 p = get_proc_task(inode);
1448 if (!p)
1449 return -ESRCH;
1450 proc_sched_set_task(p);
1451
1452 put_task_struct(p);
1453
1454 return count;
1455}
1456
1457static int sched_open(struct inode *inode, struct file *filp)
1458{
Jovi Zhangc6a34052011-01-12 17:00:34 -08001459 return single_open(filp, sched_show, inode);
Ingo Molnar43ae34c2007-07-09 18:52:00 +02001460}
1461
1462static const struct file_operations proc_pid_sched_operations = {
1463 .open = sched_open,
1464 .read = seq_read,
1465 .write = sched_write,
1466 .llseek = seq_lseek,
Alexey Dobriyan5ea473a2007-07-31 00:38:50 -07001467 .release = single_release,
Ingo Molnar43ae34c2007-07-09 18:52:00 +02001468};
1469
1470#endif
1471
Mike Galbraith5091faa2010-11-30 14:18:03 +01001472#ifdef CONFIG_SCHED_AUTOGROUP
1473/*
1474 * Print out autogroup related information:
1475 */
1476static int sched_autogroup_show(struct seq_file *m, void *v)
1477{
1478 struct inode *inode = m->private;
1479 struct task_struct *p;
1480
1481 p = get_proc_task(inode);
1482 if (!p)
1483 return -ESRCH;
1484 proc_sched_autogroup_show_task(p, m);
1485
1486 put_task_struct(p);
1487
1488 return 0;
1489}
1490
1491static ssize_t
1492sched_autogroup_write(struct file *file, const char __user *buf,
1493 size_t count, loff_t *offset)
1494{
Al Viro496ad9a2013-01-23 17:07:38 -05001495 struct inode *inode = file_inode(file);
Mike Galbraith5091faa2010-11-30 14:18:03 +01001496 struct task_struct *p;
1497 char buffer[PROC_NUMBUF];
Alexey Dobriyan0a8cb8e2011-05-26 16:25:50 -07001498 int nice;
Mike Galbraith5091faa2010-11-30 14:18:03 +01001499 int err;
1500
1501 memset(buffer, 0, sizeof(buffer));
1502 if (count > sizeof(buffer) - 1)
1503 count = sizeof(buffer) - 1;
1504 if (copy_from_user(buffer, buf, count))
1505 return -EFAULT;
1506
Alexey Dobriyan0a8cb8e2011-05-26 16:25:50 -07001507 err = kstrtoint(strstrip(buffer), 0, &nice);
1508 if (err < 0)
1509 return err;
Mike Galbraith5091faa2010-11-30 14:18:03 +01001510
1511 p = get_proc_task(inode);
1512 if (!p)
1513 return -ESRCH;
1514
Hiroshi Shimamoto2e5b5b32012-02-23 17:41:27 +09001515 err = proc_sched_autogroup_set_nice(p, nice);
Mike Galbraith5091faa2010-11-30 14:18:03 +01001516 if (err)
1517 count = err;
1518
1519 put_task_struct(p);
1520
1521 return count;
1522}
1523
1524static int sched_autogroup_open(struct inode *inode, struct file *filp)
1525{
1526 int ret;
1527
1528 ret = single_open(filp, sched_autogroup_show, NULL);
1529 if (!ret) {
1530 struct seq_file *m = filp->private_data;
1531
1532 m->private = inode;
1533 }
1534 return ret;
1535}
1536
1537static const struct file_operations proc_pid_sched_autogroup_operations = {
1538 .open = sched_autogroup_open,
1539 .read = seq_read,
1540 .write = sched_autogroup_write,
1541 .llseek = seq_lseek,
1542 .release = single_release,
1543};
1544
1545#endif /* CONFIG_SCHED_AUTOGROUP */
1546
john stultz4614a696b2009-12-14 18:00:05 -08001547static ssize_t comm_write(struct file *file, const char __user *buf,
1548 size_t count, loff_t *offset)
1549{
Al Viro496ad9a2013-01-23 17:07:38 -05001550 struct inode *inode = file_inode(file);
john stultz4614a696b2009-12-14 18:00:05 -08001551 struct task_struct *p;
1552 char buffer[TASK_COMM_LEN];
David Rientjes830e0fc2013-04-30 15:28:18 -07001553 const size_t maxlen = sizeof(buffer) - 1;
john stultz4614a696b2009-12-14 18:00:05 -08001554
1555 memset(buffer, 0, sizeof(buffer));
David Rientjes830e0fc2013-04-30 15:28:18 -07001556 if (copy_from_user(buffer, buf, count > maxlen ? maxlen : count))
john stultz4614a696b2009-12-14 18:00:05 -08001557 return -EFAULT;
1558
1559 p = get_proc_task(inode);
1560 if (!p)
1561 return -ESRCH;
1562
1563 if (same_thread_group(current, p))
1564 set_task_comm(p, buffer);
1565 else
1566 count = -EINVAL;
1567
1568 put_task_struct(p);
1569
1570 return count;
1571}
1572
1573static int comm_show(struct seq_file *m, void *v)
1574{
1575 struct inode *inode = m->private;
1576 struct task_struct *p;
1577
1578 p = get_proc_task(inode);
1579 if (!p)
1580 return -ESRCH;
1581
1582 task_lock(p);
1583 seq_printf(m, "%s\n", p->comm);
1584 task_unlock(p);
1585
1586 put_task_struct(p);
1587
1588 return 0;
1589}
1590
1591static int comm_open(struct inode *inode, struct file *filp)
1592{
Jovi Zhangc6a34052011-01-12 17:00:34 -08001593 return single_open(filp, comm_show, inode);
john stultz4614a696b2009-12-14 18:00:05 -08001594}
1595
1596static const struct file_operations proc_pid_set_comm_operations = {
1597 .open = comm_open,
1598 .read = seq_read,
1599 .write = comm_write,
1600 .llseek = seq_lseek,
1601 .release = single_release,
1602};
1603
Cyrill Gorcunov7773fbc2012-01-10 15:11:20 -08001604static int proc_exe_link(struct dentry *dentry, struct path *exe_path)
Matt Helsley925d1c42008-04-29 01:01:36 -07001605{
1606 struct task_struct *task;
Matt Helsley925d1c42008-04-29 01:01:36 -07001607 struct file *exe_file;
1608
David Howells2b0143b2015-03-17 22:25:59 +00001609 task = get_proc_task(d_inode(dentry));
Matt Helsley925d1c42008-04-29 01:01:36 -07001610 if (!task)
1611 return -ENOENT;
Mateusz Guzikcd81a9172016-08-23 16:20:38 +02001612 exe_file = get_task_exe_file(task);
Matt Helsley925d1c42008-04-29 01:01:36 -07001613 put_task_struct(task);
Matt Helsley925d1c42008-04-29 01:01:36 -07001614 if (exe_file) {
1615 *exe_path = exe_file->f_path;
1616 path_get(&exe_file->f_path);
1617 fput(exe_file);
1618 return 0;
1619 } else
1620 return -ENOENT;
1621}
1622
Al Viro6b255392015-11-17 10:20:54 -05001623static const char *proc_pid_get_link(struct dentry *dentry,
Al Virofceef392015-12-29 15:58:39 -05001624 struct inode *inode,
1625 struct delayed_call *done)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001626{
Christoph Hellwig408ef012012-06-18 10:47:03 -04001627 struct path path;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001628 int error = -EACCES;
1629
Al Viro6b255392015-11-17 10:20:54 -05001630 if (!dentry)
1631 return ERR_PTR(-ECHILD);
1632
Eric W. Biederman778c1142006-06-26 00:25:58 -07001633 /* Are we allowed to snoop on the tasks file descriptors? */
1634 if (!proc_fd_access_allowed(inode))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001635 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001636
Christoph Hellwig408ef012012-06-18 10:47:03 -04001637 error = PROC_I(inode)->op.proc_get_link(dentry, &path);
1638 if (error)
1639 goto out;
1640
Al Viro6e77137b2015-05-02 13:37:52 -04001641 nd_jump_link(&path);
Christoph Hellwig408ef012012-06-18 10:47:03 -04001642 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001643out:
Al Viro008b1502005-08-20 00:17:39 +01001644 return ERR_PTR(error);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001645}
1646
Jan Blunck3dcd25f2008-02-14 19:38:35 -08001647static int do_proc_readlink(struct path *path, char __user *buffer, int buflen)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001648{
Michal Hocko0ee931c2017-09-13 16:28:29 -07001649 char *tmp = (char *)__get_free_page(GFP_KERNEL);
Jan Blunck3dcd25f2008-02-14 19:38:35 -08001650 char *pathname;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001651 int len;
1652
1653 if (!tmp)
1654 return -ENOMEM;
Akinobu Mita0c28f282007-05-08 00:31:41 -07001655
Eric W. Biederman7b2a69b2010-12-05 15:51:21 -08001656 pathname = d_path(path, tmp, PAGE_SIZE);
Jan Blunck3dcd25f2008-02-14 19:38:35 -08001657 len = PTR_ERR(pathname);
1658 if (IS_ERR(pathname))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001659 goto out;
Jan Blunck3dcd25f2008-02-14 19:38:35 -08001660 len = tmp + PAGE_SIZE - 1 - pathname;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001661
1662 if (len > buflen)
1663 len = buflen;
Jan Blunck3dcd25f2008-02-14 19:38:35 -08001664 if (copy_to_user(buffer, pathname, len))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001665 len = -EFAULT;
1666 out:
1667 free_page((unsigned long)tmp);
1668 return len;
1669}
1670
1671static int proc_pid_readlink(struct dentry * dentry, char __user * buffer, int buflen)
1672{
1673 int error = -EACCES;
David Howells2b0143b2015-03-17 22:25:59 +00001674 struct inode *inode = d_inode(dentry);
Jan Blunck3dcd25f2008-02-14 19:38:35 -08001675 struct path path;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001676
Eric W. Biederman778c1142006-06-26 00:25:58 -07001677 /* Are we allowed to snoop on the tasks file descriptors? */
1678 if (!proc_fd_access_allowed(inode))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001679 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001680
Cyrill Gorcunov7773fbc2012-01-10 15:11:20 -08001681 error = PROC_I(inode)->op.proc_get_link(dentry, &path);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001682 if (error)
1683 goto out;
1684
Jan Blunck3dcd25f2008-02-14 19:38:35 -08001685 error = do_proc_readlink(&path, buffer, buflen);
1686 path_put(&path);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001687out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001688 return error;
1689}
1690
Cyrill Gorcunovfaf60af2012-08-23 14:43:24 +04001691const struct inode_operations proc_pid_link_inode_operations = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001692 .readlink = proc_pid_readlink,
Al Viro6b255392015-11-17 10:20:54 -05001693 .get_link = proc_pid_get_link,
Linus Torvalds6d76fa52006-07-15 12:26:45 -07001694 .setattr = proc_setattr,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001695};
1696
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001697
1698/* building an inode */
1699
Al Viroc6eb50d2017-09-30 14:45:42 -04001700void task_dump_owner(struct task_struct *task, umode_t mode,
Eric W. Biederman68eb94f2017-01-03 10:23:11 +13001701 kuid_t *ruid, kgid_t *rgid)
1702{
1703 /* Depending on the state of dumpable compute who should own a
1704 * proc file for a task.
1705 */
1706 const struct cred *cred;
1707 kuid_t uid;
1708 kgid_t gid;
1709
Alexey Dobriyan2e0ad552018-04-20 14:56:03 -07001710 if (unlikely(task->flags & PF_KTHREAD)) {
1711 *ruid = GLOBAL_ROOT_UID;
1712 *rgid = GLOBAL_ROOT_GID;
1713 return;
1714 }
1715
Eric W. Biederman68eb94f2017-01-03 10:23:11 +13001716 /* Default to the tasks effective ownership */
1717 rcu_read_lock();
1718 cred = __task_cred(task);
1719 uid = cred->euid;
1720 gid = cred->egid;
1721 rcu_read_unlock();
1722
1723 /*
1724 * Before the /proc/pid/status file was created the only way to read
1725 * the effective uid of a /process was to stat /proc/pid. Reading
1726 * /proc/pid/status is slow enough that procps and other packages
1727 * kept stating /proc/pid. To keep the rules in /proc simple I have
1728 * made this apply to all per process world readable and executable
1729 * directories.
1730 */
1731 if (mode != (S_IFDIR|S_IRUGO|S_IXUGO)) {
1732 struct mm_struct *mm;
1733 task_lock(task);
1734 mm = task->mm;
1735 /* Make non-dumpable tasks owned by some root */
1736 if (mm) {
1737 if (get_dumpable(mm) != SUID_DUMP_USER) {
1738 struct user_namespace *user_ns = mm->user_ns;
1739
1740 uid = make_kuid(user_ns, 0);
1741 if (!uid_valid(uid))
1742 uid = GLOBAL_ROOT_UID;
1743
1744 gid = make_kgid(user_ns, 0);
1745 if (!gid_valid(gid))
1746 gid = GLOBAL_ROOT_GID;
1747 }
1748 } else {
1749 uid = GLOBAL_ROOT_UID;
1750 gid = GLOBAL_ROOT_GID;
1751 }
1752 task_unlock(task);
1753 }
1754 *ruid = uid;
1755 *rgid = gid;
1756}
1757
Andreas Gruenbacherdb978da2016-11-10 22:18:28 +01001758struct inode *proc_pid_make_inode(struct super_block * sb,
1759 struct task_struct *task, umode_t mode)
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001760{
1761 struct inode * inode;
1762 struct proc_inode *ei;
1763
1764 /* We need a new inode */
1765
1766 inode = new_inode(sb);
1767 if (!inode)
1768 goto out;
1769
1770 /* Common stuff */
1771 ei = PROC_I(inode);
Andreas Gruenbacherdb978da2016-11-10 22:18:28 +01001772 inode->i_mode = mode;
Christoph Hellwig85fe4022010-10-23 11:19:54 -04001773 inode->i_ino = get_next_ino();
Deepa Dinamani078cd822016-09-14 07:48:04 -07001774 inode->i_mtime = inode->i_atime = inode->i_ctime = current_time(inode);
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001775 inode->i_op = &proc_def_inode_operations;
1776
1777 /*
1778 * grab the reference to task.
1779 */
Oleg Nesterov1a657f782006-10-02 02:18:59 -07001780 ei->pid = get_task_pid(task, PIDTYPE_PID);
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001781 if (!ei->pid)
1782 goto out_unlock;
1783
Eric W. Biederman68eb94f2017-01-03 10:23:11 +13001784 task_dump_owner(task, 0, &inode->i_uid, &inode->i_gid);
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001785 security_task_to_inode(task, inode);
1786
1787out:
1788 return inode;
1789
1790out_unlock:
1791 iput(inode);
1792 return NULL;
1793}
1794
David Howellsa528d352017-01-31 16:46:22 +00001795int pid_getattr(const struct path *path, struct kstat *stat,
1796 u32 request_mask, unsigned int query_flags)
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001797{
David Howellsa528d352017-01-31 16:46:22 +00001798 struct inode *inode = d_inode(path->dentry);
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001799 struct task_struct *task;
David Howellsa528d352017-01-31 16:46:22 +00001800 struct pid_namespace *pid = path->dentry->d_sb->s_fs_info;
David Howellsc69e8d92008-11-14 10:39:19 +11001801
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001802 generic_fillattr(inode, stat);
1803
1804 rcu_read_lock();
Eric W. Biedermandcb0f222012-02-09 08:48:21 -08001805 stat->uid = GLOBAL_ROOT_UID;
1806 stat->gid = GLOBAL_ROOT_GID;
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001807 task = pid_task(proc_pid(inode), PIDTYPE_PID);
1808 if (task) {
Lafcadio Wluiki796f5712017-02-24 15:00:23 -08001809 if (!has_pid_permissions(pid, task, HIDEPID_INVISIBLE)) {
Vasiliy Kulikov04996802012-01-10 15:11:31 -08001810 rcu_read_unlock();
1811 /*
1812 * This doesn't prevent learning whether PID exists,
1813 * it only makes getattr() consistent with readdir().
1814 */
1815 return -ENOENT;
1816 }
Eric W. Biederman68eb94f2017-01-03 10:23:11 +13001817 task_dump_owner(task, inode->i_mode, &stat->uid, &stat->gid);
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001818 }
1819 rcu_read_unlock();
1820 return 0;
1821}
1822
1823/* dentry stuff */
1824
1825/*
1826 * Exceptional case: normally we are not allowed to unhash a busy
1827 * directory. In this case, however, we can do it - no aliasing problems
1828 * due to the way we treat inodes.
1829 *
1830 * Rewrite the inode's ownerships here because the owning task may have
1831 * performed a setuid(), etc.
1832 *
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001833 */
Al Viro0b728e12012-06-10 16:03:43 -04001834int pid_revalidate(struct dentry *dentry, unsigned int flags)
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001835{
Nick Piggin34286d62011-01-07 17:49:57 +11001836 struct inode *inode;
1837 struct task_struct *task;
David Howellsc69e8d92008-11-14 10:39:19 +11001838
Al Viro0b728e12012-06-10 16:03:43 -04001839 if (flags & LOOKUP_RCU)
Nick Piggin34286d62011-01-07 17:49:57 +11001840 return -ECHILD;
1841
David Howells2b0143b2015-03-17 22:25:59 +00001842 inode = d_inode(dentry);
Nick Piggin34286d62011-01-07 17:49:57 +11001843 task = get_proc_task(inode);
1844
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001845 if (task) {
Eric W. Biederman68eb94f2017-01-03 10:23:11 +13001846 task_dump_owner(task, inode->i_mode, &inode->i_uid, &inode->i_gid);
1847
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001848 inode->i_mode &= ~(S_ISUID | S_ISGID);
1849 security_task_to_inode(task, inode);
1850 put_task_struct(task);
1851 return 1;
1852 }
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001853 return 0;
1854}
1855
Oleg Nesterovd855a4b2014-01-23 15:55:39 -08001856static inline bool proc_inode_is_dead(struct inode *inode)
1857{
1858 return !proc_pid(inode)->tasks[PIDTYPE_PID].first;
1859}
1860
David Howells1dd704b2013-04-12 01:08:50 +01001861int pid_delete_dentry(const struct dentry *dentry)
1862{
1863 /* Is the task we represent dead?
1864 * If so, then don't put the dentry on the lru list,
1865 * kill it immediately.
1866 */
David Howells2b0143b2015-03-17 22:25:59 +00001867 return proc_inode_is_dead(d_inode(dentry));
David Howells1dd704b2013-04-12 01:08:50 +01001868}
1869
Eric W. Biederman6b4e3062010-03-07 16:41:34 -08001870const struct dentry_operations pid_dentry_operations =
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001871{
1872 .d_revalidate = pid_revalidate,
1873 .d_delete = pid_delete_dentry,
1874};
1875
1876/* Lookups */
1877
Eric W. Biederman1c0d04c2006-10-02 02:18:57 -07001878/*
1879 * Fill a directory entry.
1880 *
1881 * If possible create the dcache entry and derive our inode number and
1882 * file type from dcache entry.
1883 *
1884 * Since all of the proc inode numbers are dynamically generated, the inode
1885 * numbers do not exist until the inode is cache. This means creating the
1886 * the dcache entry in readdir is necessary to keep the inode numbers
1887 * reported by readdir in sync with the inode numbers reported
1888 * by stat.
1889 */
Al Virof0c3b502013-05-16 12:07:31 -04001890bool proc_fill_cache(struct file *file, struct dir_context *ctx,
Eric W. Biederman6b4e3062010-03-07 16:41:34 -08001891 const char *name, int len,
Eric Dumazetc5141e62007-05-08 00:26:15 -07001892 instantiate_t instantiate, struct task_struct *task, const void *ptr)
Eric W. Biederman61a28782006-10-02 02:18:49 -07001893{
Al Virof0c3b502013-05-16 12:07:31 -04001894 struct dentry *child, *dir = file->f_path.dentry;
Al Viro1df98b82013-06-15 11:33:10 +04001895 struct qstr qname = QSTR_INIT(name, len);
Eric W. Biederman61a28782006-10-02 02:18:49 -07001896 struct inode *inode;
Al Viro1df98b82013-06-15 11:33:10 +04001897 unsigned type;
1898 ino_t ino;
Eric W. Biederman61a28782006-10-02 02:18:49 -07001899
Al Viro1df98b82013-06-15 11:33:10 +04001900 child = d_hash_and_lookup(dir, &qname);
Eric W. Biederman61a28782006-10-02 02:18:49 -07001901 if (!child) {
Al Viro37817642016-04-20 16:31:31 -04001902 DECLARE_WAIT_QUEUE_HEAD_ONSTACK(wq);
1903 child = d_alloc_parallel(dir, &qname, &wq);
1904 if (IS_ERR(child))
Al Viro1df98b82013-06-15 11:33:10 +04001905 goto end_instantiate;
Al Viro37817642016-04-20 16:31:31 -04001906 if (d_in_lookup(child)) {
1907 int err = instantiate(d_inode(dir), child, task, ptr);
1908 d_lookup_done(child);
1909 if (err < 0) {
1910 dput(child);
1911 goto end_instantiate;
1912 }
Eric W. Biederman61a28782006-10-02 02:18:49 -07001913 }
1914 }
David Howells2b0143b2015-03-17 22:25:59 +00001915 inode = d_inode(child);
Al Viro147ce692013-06-15 10:26:35 +04001916 ino = inode->i_ino;
1917 type = inode->i_mode >> 12;
Eric W. Biederman61a28782006-10-02 02:18:49 -07001918 dput(child);
Al Virof0c3b502013-05-16 12:07:31 -04001919 return dir_emit(ctx, name, len, ino, type);
Al Viro1df98b82013-06-15 11:33:10 +04001920
1921end_instantiate:
1922 return dir_emit(ctx, name, len, 1, DT_UNKNOWN);
Eric W. Biederman61a28782006-10-02 02:18:49 -07001923}
1924
Pavel Emelyanov640708a2012-01-10 15:11:23 -08001925/*
1926 * dname_to_vma_addr - maps a dentry name into two unsigned longs
1927 * which represent vma start and end addresses.
1928 */
1929static int dname_to_vma_addr(struct dentry *dentry,
1930 unsigned long *start, unsigned long *end)
1931{
Alexey Dobriyanac7f1062018-02-06 15:36:59 -08001932 const char *str = dentry->d_name.name;
1933 unsigned long long sval, eval;
1934 unsigned int len;
1935
Alexey Dobriyan35318db2018-04-10 16:41:14 -07001936 if (str[0] == '0' && str[1] != '-')
1937 return -EINVAL;
Alexey Dobriyanac7f1062018-02-06 15:36:59 -08001938 len = _parse_integer(str, 16, &sval);
1939 if (len & KSTRTOX_OVERFLOW)
Pavel Emelyanov640708a2012-01-10 15:11:23 -08001940 return -EINVAL;
Alexey Dobriyanac7f1062018-02-06 15:36:59 -08001941 if (sval != (unsigned long)sval)
1942 return -EINVAL;
1943 str += len;
1944
1945 if (*str != '-')
1946 return -EINVAL;
1947 str++;
1948
Alexey Dobriyan35318db2018-04-10 16:41:14 -07001949 if (str[0] == '0' && str[1])
1950 return -EINVAL;
Alexey Dobriyanac7f1062018-02-06 15:36:59 -08001951 len = _parse_integer(str, 16, &eval);
1952 if (len & KSTRTOX_OVERFLOW)
1953 return -EINVAL;
1954 if (eval != (unsigned long)eval)
1955 return -EINVAL;
1956 str += len;
1957
1958 if (*str != '\0')
1959 return -EINVAL;
1960
1961 *start = sval;
1962 *end = eval;
Pavel Emelyanov640708a2012-01-10 15:11:23 -08001963
1964 return 0;
1965}
1966
Al Viro0b728e12012-06-10 16:03:43 -04001967static int map_files_d_revalidate(struct dentry *dentry, unsigned int flags)
Pavel Emelyanov640708a2012-01-10 15:11:23 -08001968{
1969 unsigned long vm_start, vm_end;
1970 bool exact_vma_exists = false;
1971 struct mm_struct *mm = NULL;
1972 struct task_struct *task;
Pavel Emelyanov640708a2012-01-10 15:11:23 -08001973 struct inode *inode;
1974 int status = 0;
1975
Al Viro0b728e12012-06-10 16:03:43 -04001976 if (flags & LOOKUP_RCU)
Pavel Emelyanov640708a2012-01-10 15:11:23 -08001977 return -ECHILD;
1978
David Howells2b0143b2015-03-17 22:25:59 +00001979 inode = d_inode(dentry);
Pavel Emelyanov640708a2012-01-10 15:11:23 -08001980 task = get_proc_task(inode);
1981 if (!task)
1982 goto out_notask;
1983
Jann Horncaaee622016-01-20 15:00:04 -08001984 mm = mm_access(task, PTRACE_MODE_READ_FSCREDS);
Cong Wang2344bec2012-05-31 16:26:18 -07001985 if (IS_ERR_OR_NULL(mm))
Pavel Emelyanov640708a2012-01-10 15:11:23 -08001986 goto out;
1987
1988 if (!dname_to_vma_addr(dentry, &vm_start, &vm_end)) {
1989 down_read(&mm->mmap_sem);
1990 exact_vma_exists = !!find_exact_vma(mm, vm_start, vm_end);
1991 up_read(&mm->mmap_sem);
1992 }
1993
1994 mmput(mm);
1995
1996 if (exact_vma_exists) {
Eric W. Biederman68eb94f2017-01-03 10:23:11 +13001997 task_dump_owner(task, 0, &inode->i_uid, &inode->i_gid);
1998
Pavel Emelyanov640708a2012-01-10 15:11:23 -08001999 security_task_to_inode(task, inode);
2000 status = 1;
2001 }
2002
2003out:
2004 put_task_struct(task);
2005
2006out_notask:
Pavel Emelyanov640708a2012-01-10 15:11:23 -08002007 return status;
2008}
2009
2010static const struct dentry_operations tid_map_files_dentry_operations = {
2011 .d_revalidate = map_files_d_revalidate,
2012 .d_delete = pid_delete_dentry,
2013};
2014
Al Viro6b255392015-11-17 10:20:54 -05002015static int map_files_get_link(struct dentry *dentry, struct path *path)
Pavel Emelyanov640708a2012-01-10 15:11:23 -08002016{
2017 unsigned long vm_start, vm_end;
2018 struct vm_area_struct *vma;
2019 struct task_struct *task;
2020 struct mm_struct *mm;
2021 int rc;
2022
2023 rc = -ENOENT;
David Howells2b0143b2015-03-17 22:25:59 +00002024 task = get_proc_task(d_inode(dentry));
Pavel Emelyanov640708a2012-01-10 15:11:23 -08002025 if (!task)
2026 goto out;
2027
2028 mm = get_task_mm(task);
2029 put_task_struct(task);
2030 if (!mm)
2031 goto out;
2032
2033 rc = dname_to_vma_addr(dentry, &vm_start, &vm_end);
2034 if (rc)
2035 goto out_mmput;
2036
Artem Fetishev70335ab2014-03-10 15:49:45 -07002037 rc = -ENOENT;
Pavel Emelyanov640708a2012-01-10 15:11:23 -08002038 down_read(&mm->mmap_sem);
2039 vma = find_exact_vma(mm, vm_start, vm_end);
2040 if (vma && vma->vm_file) {
2041 *path = vma->vm_file->f_path;
2042 path_get(path);
2043 rc = 0;
2044 }
2045 up_read(&mm->mmap_sem);
2046
2047out_mmput:
2048 mmput(mm);
2049out:
2050 return rc;
2051}
2052
2053struct map_files_info {
Alexey Dobriyan20d28cd2018-02-06 15:37:06 -08002054 unsigned long start;
2055 unsigned long end;
Al Viro7b540d02012-08-27 14:55:26 -04002056 fmode_t mode;
Pavel Emelyanov640708a2012-01-10 15:11:23 -08002057};
2058
Calvin Owensbdb4d102015-09-09 15:35:54 -07002059/*
2060 * Only allow CAP_SYS_ADMIN to follow the links, due to concerns about how the
2061 * symlinks may be used to bypass permissions on ancestor directories in the
2062 * path to the file in question.
2063 */
2064static const char *
Al Viro6b255392015-11-17 10:20:54 -05002065proc_map_files_get_link(struct dentry *dentry,
Al Virofceef392015-12-29 15:58:39 -05002066 struct inode *inode,
2067 struct delayed_call *done)
Calvin Owensbdb4d102015-09-09 15:35:54 -07002068{
2069 if (!capable(CAP_SYS_ADMIN))
2070 return ERR_PTR(-EPERM);
2071
Al Virofceef392015-12-29 15:58:39 -05002072 return proc_pid_get_link(dentry, inode, done);
Calvin Owensbdb4d102015-09-09 15:35:54 -07002073}
2074
2075/*
Al Viro6b255392015-11-17 10:20:54 -05002076 * Identical to proc_pid_link_inode_operations except for get_link()
Calvin Owensbdb4d102015-09-09 15:35:54 -07002077 */
2078static const struct inode_operations proc_map_files_link_inode_operations = {
2079 .readlink = proc_pid_readlink,
Al Viro6b255392015-11-17 10:20:54 -05002080 .get_link = proc_map_files_get_link,
Calvin Owensbdb4d102015-09-09 15:35:54 -07002081 .setattr = proc_setattr,
2082};
2083
Al Viroc52a47a2013-06-15 11:15:20 +04002084static int
Pavel Emelyanov640708a2012-01-10 15:11:23 -08002085proc_map_files_instantiate(struct inode *dir, struct dentry *dentry,
2086 struct task_struct *task, const void *ptr)
2087{
Al Viro7b540d02012-08-27 14:55:26 -04002088 fmode_t mode = (fmode_t)(unsigned long)ptr;
Pavel Emelyanov640708a2012-01-10 15:11:23 -08002089 struct proc_inode *ei;
2090 struct inode *inode;
2091
Andreas Gruenbacherdb978da2016-11-10 22:18:28 +01002092 inode = proc_pid_make_inode(dir->i_sb, task, S_IFLNK |
2093 ((mode & FMODE_READ ) ? S_IRUSR : 0) |
2094 ((mode & FMODE_WRITE) ? S_IWUSR : 0));
Pavel Emelyanov640708a2012-01-10 15:11:23 -08002095 if (!inode)
Al Viroc52a47a2013-06-15 11:15:20 +04002096 return -ENOENT;
Pavel Emelyanov640708a2012-01-10 15:11:23 -08002097
2098 ei = PROC_I(inode);
Al Viro6b255392015-11-17 10:20:54 -05002099 ei->op.proc_get_link = map_files_get_link;
Pavel Emelyanov640708a2012-01-10 15:11:23 -08002100
Calvin Owensbdb4d102015-09-09 15:35:54 -07002101 inode->i_op = &proc_map_files_link_inode_operations;
Pavel Emelyanov640708a2012-01-10 15:11:23 -08002102 inode->i_size = 64;
Pavel Emelyanov640708a2012-01-10 15:11:23 -08002103
2104 d_set_d_op(dentry, &tid_map_files_dentry_operations);
2105 d_add(dentry, inode);
2106
Al Viroc52a47a2013-06-15 11:15:20 +04002107 return 0;
Pavel Emelyanov640708a2012-01-10 15:11:23 -08002108}
2109
2110static struct dentry *proc_map_files_lookup(struct inode *dir,
Al Viro00cd8dd2012-06-10 17:13:09 -04002111 struct dentry *dentry, unsigned int flags)
Pavel Emelyanov640708a2012-01-10 15:11:23 -08002112{
2113 unsigned long vm_start, vm_end;
2114 struct vm_area_struct *vma;
2115 struct task_struct *task;
Al Viroc52a47a2013-06-15 11:15:20 +04002116 int result;
Pavel Emelyanov640708a2012-01-10 15:11:23 -08002117 struct mm_struct *mm;
2118
Al Viroc52a47a2013-06-15 11:15:20 +04002119 result = -ENOENT;
Pavel Emelyanov640708a2012-01-10 15:11:23 -08002120 task = get_proc_task(dir);
2121 if (!task)
2122 goto out;
2123
Al Viroc52a47a2013-06-15 11:15:20 +04002124 result = -EACCES;
Jann Horncaaee622016-01-20 15:00:04 -08002125 if (!ptrace_may_access(task, PTRACE_MODE_READ_FSCREDS))
Pavel Emelyanov640708a2012-01-10 15:11:23 -08002126 goto out_put_task;
2127
Al Viroc52a47a2013-06-15 11:15:20 +04002128 result = -ENOENT;
Pavel Emelyanov640708a2012-01-10 15:11:23 -08002129 if (dname_to_vma_addr(dentry, &vm_start, &vm_end))
Cyrill Gorcunoveb94cd92012-05-17 17:03:25 -07002130 goto out_put_task;
Pavel Emelyanov640708a2012-01-10 15:11:23 -08002131
2132 mm = get_task_mm(task);
2133 if (!mm)
Cyrill Gorcunoveb94cd92012-05-17 17:03:25 -07002134 goto out_put_task;
Pavel Emelyanov640708a2012-01-10 15:11:23 -08002135
2136 down_read(&mm->mmap_sem);
2137 vma = find_exact_vma(mm, vm_start, vm_end);
2138 if (!vma)
2139 goto out_no_vma;
2140
Stanislav Kinsbursky05f56482012-11-26 16:29:42 -08002141 if (vma->vm_file)
2142 result = proc_map_files_instantiate(dir, dentry, task,
2143 (void *)(unsigned long)vma->vm_file->f_mode);
Pavel Emelyanov640708a2012-01-10 15:11:23 -08002144
2145out_no_vma:
2146 up_read(&mm->mmap_sem);
2147 mmput(mm);
Pavel Emelyanov640708a2012-01-10 15:11:23 -08002148out_put_task:
2149 put_task_struct(task);
2150out:
Al Viroc52a47a2013-06-15 11:15:20 +04002151 return ERR_PTR(result);
Pavel Emelyanov640708a2012-01-10 15:11:23 -08002152}
2153
2154static const struct inode_operations proc_map_files_inode_operations = {
2155 .lookup = proc_map_files_lookup,
2156 .permission = proc_fd_permission,
2157 .setattr = proc_setattr,
2158};
2159
2160static int
Al Virof0c3b502013-05-16 12:07:31 -04002161proc_map_files_readdir(struct file *file, struct dir_context *ctx)
Pavel Emelyanov640708a2012-01-10 15:11:23 -08002162{
Pavel Emelyanov640708a2012-01-10 15:11:23 -08002163 struct vm_area_struct *vma;
2164 struct task_struct *task;
2165 struct mm_struct *mm;
Al Virof0c3b502013-05-16 12:07:31 -04002166 unsigned long nr_files, pos, i;
2167 struct flex_array *fa = NULL;
2168 struct map_files_info info;
2169 struct map_files_info *p;
Pavel Emelyanov640708a2012-01-10 15:11:23 -08002170 int ret;
2171
Pavel Emelyanov640708a2012-01-10 15:11:23 -08002172 ret = -ENOENT;
Al Virof0c3b502013-05-16 12:07:31 -04002173 task = get_proc_task(file_inode(file));
Pavel Emelyanov640708a2012-01-10 15:11:23 -08002174 if (!task)
2175 goto out;
2176
2177 ret = -EACCES;
Jann Horncaaee622016-01-20 15:00:04 -08002178 if (!ptrace_may_access(task, PTRACE_MODE_READ_FSCREDS))
Pavel Emelyanov640708a2012-01-10 15:11:23 -08002179 goto out_put_task;
2180
2181 ret = 0;
Al Virof0c3b502013-05-16 12:07:31 -04002182 if (!dir_emit_dots(file, ctx))
2183 goto out_put_task;
Pavel Emelyanov640708a2012-01-10 15:11:23 -08002184
Al Virof0c3b502013-05-16 12:07:31 -04002185 mm = get_task_mm(task);
2186 if (!mm)
2187 goto out_put_task;
2188 down_read(&mm->mmap_sem);
Pavel Emelyanov640708a2012-01-10 15:11:23 -08002189
Al Virof0c3b502013-05-16 12:07:31 -04002190 nr_files = 0;
Pavel Emelyanov640708a2012-01-10 15:11:23 -08002191
Al Virof0c3b502013-05-16 12:07:31 -04002192 /*
2193 * We need two passes here:
2194 *
2195 * 1) Collect vmas of mapped files with mmap_sem taken
2196 * 2) Release mmap_sem and instantiate entries
2197 *
2198 * otherwise we get lockdep complained, since filldir()
2199 * routine might require mmap_sem taken in might_fault().
2200 */
Pavel Emelyanov640708a2012-01-10 15:11:23 -08002201
Al Virof0c3b502013-05-16 12:07:31 -04002202 for (vma = mm->mmap, pos = 2; vma; vma = vma->vm_next) {
2203 if (vma->vm_file && ++pos > ctx->pos)
2204 nr_files++;
Pavel Emelyanov640708a2012-01-10 15:11:23 -08002205 }
Al Virof0c3b502013-05-16 12:07:31 -04002206
2207 if (nr_files) {
2208 fa = flex_array_alloc(sizeof(info), nr_files,
2209 GFP_KERNEL);
2210 if (!fa || flex_array_prealloc(fa, 0, nr_files,
2211 GFP_KERNEL)) {
2212 ret = -ENOMEM;
2213 if (fa)
2214 flex_array_free(fa);
2215 up_read(&mm->mmap_sem);
2216 mmput(mm);
2217 goto out_put_task;
2218 }
2219 for (i = 0, vma = mm->mmap, pos = 2; vma;
2220 vma = vma->vm_next) {
2221 if (!vma->vm_file)
2222 continue;
2223 if (++pos <= ctx->pos)
2224 continue;
2225
Alexey Dobriyan20d28cd2018-02-06 15:37:06 -08002226 info.start = vma->vm_start;
2227 info.end = vma->vm_end;
Al Virof0c3b502013-05-16 12:07:31 -04002228 info.mode = vma->vm_file->f_mode;
Al Virof0c3b502013-05-16 12:07:31 -04002229 if (flex_array_put(fa, i++, &info, GFP_KERNEL))
2230 BUG();
2231 }
Pavel Emelyanov640708a2012-01-10 15:11:23 -08002232 }
Al Virof0c3b502013-05-16 12:07:31 -04002233 up_read(&mm->mmap_sem);
Alexey Dobriyanfe079a52018-04-10 16:32:05 -07002234 mmput(mm);
Al Virof0c3b502013-05-16 12:07:31 -04002235
2236 for (i = 0; i < nr_files; i++) {
Alexey Dobriyan20d28cd2018-02-06 15:37:06 -08002237 char buf[4 * sizeof(long) + 2]; /* max: %lx-%lx\0 */
2238 unsigned int len;
2239
Al Virof0c3b502013-05-16 12:07:31 -04002240 p = flex_array_get(fa, i);
Alexey Dobriyan20d28cd2018-02-06 15:37:06 -08002241 len = snprintf(buf, sizeof(buf), "%lx-%lx", p->start, p->end);
Al Virof0c3b502013-05-16 12:07:31 -04002242 if (!proc_fill_cache(file, ctx,
Alexey Dobriyan20d28cd2018-02-06 15:37:06 -08002243 buf, len,
Al Virof0c3b502013-05-16 12:07:31 -04002244 proc_map_files_instantiate,
2245 task,
2246 (void *)(unsigned long)p->mode))
2247 break;
2248 ctx->pos++;
2249 }
2250 if (fa)
2251 flex_array_free(fa);
Pavel Emelyanov640708a2012-01-10 15:11:23 -08002252
Pavel Emelyanov640708a2012-01-10 15:11:23 -08002253out_put_task:
2254 put_task_struct(task);
2255out:
2256 return ret;
2257}
2258
2259static const struct file_operations proc_map_files_operations = {
2260 .read = generic_read_dir,
Al Virof50752e2016-04-20 17:13:54 -04002261 .iterate_shared = proc_map_files_readdir,
2262 .llseek = generic_file_llseek,
Pavel Emelyanov640708a2012-01-10 15:11:23 -08002263};
2264
Nicolas Pitreb18b6a92017-01-21 00:09:08 -05002265#if defined(CONFIG_CHECKPOINT_RESTORE) && defined(CONFIG_POSIX_TIMERS)
Pavel Emelyanov48f6a7a2013-03-11 13:12:45 +04002266struct timers_private {
2267 struct pid *pid;
2268 struct task_struct *task;
2269 struct sighand_struct *sighand;
Pavel Emelyanov57b80152013-03-11 13:13:08 +04002270 struct pid_namespace *ns;
Pavel Emelyanov48f6a7a2013-03-11 13:12:45 +04002271 unsigned long flags;
2272};
2273
2274static void *timers_start(struct seq_file *m, loff_t *pos)
2275{
2276 struct timers_private *tp = m->private;
2277
2278 tp->task = get_pid_task(tp->pid, PIDTYPE_PID);
2279 if (!tp->task)
2280 return ERR_PTR(-ESRCH);
2281
2282 tp->sighand = lock_task_sighand(tp->task, &tp->flags);
2283 if (!tp->sighand)
2284 return ERR_PTR(-ESRCH);
2285
2286 return seq_list_start(&tp->task->signal->posix_timers, *pos);
2287}
2288
2289static void *timers_next(struct seq_file *m, void *v, loff_t *pos)
2290{
2291 struct timers_private *tp = m->private;
2292 return seq_list_next(v, &tp->task->signal->posix_timers, pos);
2293}
2294
2295static void timers_stop(struct seq_file *m, void *v)
2296{
2297 struct timers_private *tp = m->private;
2298
2299 if (tp->sighand) {
2300 unlock_task_sighand(tp->task, &tp->flags);
2301 tp->sighand = NULL;
2302 }
2303
2304 if (tp->task) {
2305 put_task_struct(tp->task);
2306 tp->task = NULL;
2307 }
2308}
2309
2310static int show_timer(struct seq_file *m, void *v)
2311{
2312 struct k_itimer *timer;
Pavel Emelyanov57b80152013-03-11 13:13:08 +04002313 struct timers_private *tp = m->private;
2314 int notify;
Alexey Dobriyancedbcca2014-08-08 14:21:33 -07002315 static const char * const nstr[] = {
Pavel Emelyanov57b80152013-03-11 13:13:08 +04002316 [SIGEV_SIGNAL] = "signal",
2317 [SIGEV_NONE] = "none",
2318 [SIGEV_THREAD] = "thread",
2319 };
Pavel Emelyanov48f6a7a2013-03-11 13:12:45 +04002320
2321 timer = list_entry((struct list_head *)v, struct k_itimer, list);
Pavel Emelyanov57b80152013-03-11 13:13:08 +04002322 notify = timer->it_sigev_notify;
2323
Pavel Emelyanov48f6a7a2013-03-11 13:12:45 +04002324 seq_printf(m, "ID: %d\n", timer->it_id);
Linus Torvaldsba3edf1f2017-12-06 18:23:27 -08002325 seq_printf(m, "signal: %d/%px\n",
Joe Perches25ce3192015-04-15 16:18:17 -07002326 timer->sigq->info.si_signo,
2327 timer->sigq->info.si_value.sival_ptr);
Pavel Emelyanov57b80152013-03-11 13:13:08 +04002328 seq_printf(m, "notify: %s/%s.%d\n",
Joe Perches25ce3192015-04-15 16:18:17 -07002329 nstr[notify & ~SIGEV_THREAD_ID],
2330 (notify & SIGEV_THREAD_ID) ? "tid" : "pid",
2331 pid_nr_ns(timer->it_pid, tp->ns));
Pavel Tikhomirov15ef0292013-05-17 02:12:03 +04002332 seq_printf(m, "ClockID: %d\n", timer->it_clock);
Pavel Emelyanov48f6a7a2013-03-11 13:12:45 +04002333
2334 return 0;
2335}
2336
2337static const struct seq_operations proc_timers_seq_ops = {
2338 .start = timers_start,
2339 .next = timers_next,
2340 .stop = timers_stop,
2341 .show = show_timer,
2342};
2343
2344static int proc_timers_open(struct inode *inode, struct file *file)
2345{
2346 struct timers_private *tp;
2347
2348 tp = __seq_open_private(file, &proc_timers_seq_ops,
2349 sizeof(struct timers_private));
2350 if (!tp)
2351 return -ENOMEM;
2352
2353 tp->pid = proc_pid(inode);
Pavel Emelyanov57b80152013-03-11 13:13:08 +04002354 tp->ns = inode->i_sb->s_fs_info;
Pavel Emelyanov48f6a7a2013-03-11 13:12:45 +04002355 return 0;
2356}
2357
2358static const struct file_operations proc_timers_operations = {
2359 .open = proc_timers_open,
2360 .read = seq_read,
2361 .llseek = seq_lseek,
2362 .release = seq_release_private,
2363};
Eric Engestromb5946be2016-03-17 14:20:57 -07002364#endif
Pavel Emelyanov640708a2012-01-10 15:11:23 -08002365
John Stultz5de23d42016-03-17 14:20:54 -07002366static ssize_t timerslack_ns_write(struct file *file, const char __user *buf,
2367 size_t count, loff_t *offset)
2368{
2369 struct inode *inode = file_inode(file);
2370 struct task_struct *p;
2371 u64 slack_ns;
2372 int err;
2373
2374 err = kstrtoull_from_user(buf, count, 10, &slack_ns);
2375 if (err < 0)
2376 return err;
2377
2378 p = get_proc_task(inode);
2379 if (!p)
2380 return -ESRCH;
2381
John Stultz4b2bd5f2016-10-07 17:02:33 -07002382 if (p != current) {
2383 if (!capable(CAP_SYS_NICE)) {
2384 count = -EPERM;
2385 goto out;
2386 }
John Stultz5de23d42016-03-17 14:20:54 -07002387
John Stultz4b2bd5f2016-10-07 17:02:33 -07002388 err = security_task_setscheduler(p);
2389 if (err) {
2390 count = err;
2391 goto out;
2392 }
John Stultz904763e2016-10-07 17:02:29 -07002393 }
2394
John Stultz7abbaf92016-10-07 17:02:26 -07002395 task_lock(p);
2396 if (slack_ns == 0)
2397 p->timer_slack_ns = p->default_timer_slack_ns;
2398 else
2399 p->timer_slack_ns = slack_ns;
2400 task_unlock(p);
2401
2402out:
John Stultz5de23d42016-03-17 14:20:54 -07002403 put_task_struct(p);
2404
2405 return count;
2406}
2407
2408static int timerslack_ns_show(struct seq_file *m, void *v)
2409{
2410 struct inode *inode = m->private;
2411 struct task_struct *p;
John Stultz7abbaf92016-10-07 17:02:26 -07002412 int err = 0;
John Stultz5de23d42016-03-17 14:20:54 -07002413
2414 p = get_proc_task(inode);
2415 if (!p)
2416 return -ESRCH;
2417
John Stultz4b2bd5f2016-10-07 17:02:33 -07002418 if (p != current) {
John Stultz5de23d42016-03-17 14:20:54 -07002419
John Stultz4b2bd5f2016-10-07 17:02:33 -07002420 if (!capable(CAP_SYS_NICE)) {
2421 err = -EPERM;
2422 goto out;
2423 }
2424 err = security_task_getscheduler(p);
2425 if (err)
2426 goto out;
2427 }
John Stultz904763e2016-10-07 17:02:29 -07002428
John Stultz7abbaf92016-10-07 17:02:26 -07002429 task_lock(p);
2430 seq_printf(m, "%llu\n", p->timer_slack_ns);
2431 task_unlock(p);
2432
2433out:
John Stultz5de23d42016-03-17 14:20:54 -07002434 put_task_struct(p);
2435
2436 return err;
2437}
2438
2439static int timerslack_ns_open(struct inode *inode, struct file *filp)
2440{
2441 return single_open(filp, timerslack_ns_show, inode);
2442}
2443
2444static const struct file_operations proc_pid_set_timerslack_ns_operations = {
2445 .open = timerslack_ns_open,
2446 .read = seq_read,
2447 .write = timerslack_ns_write,
2448 .llseek = seq_lseek,
2449 .release = single_release,
2450};
2451
Al Viroc52a47a2013-06-15 11:15:20 +04002452static int proc_pident_instantiate(struct inode *dir,
Eric Dumazetc5141e62007-05-08 00:26:15 -07002453 struct dentry *dentry, struct task_struct *task, const void *ptr)
Eric W. Biederman444ceed2006-10-02 02:18:49 -07002454{
Eric Dumazetc5141e62007-05-08 00:26:15 -07002455 const struct pid_entry *p = ptr;
Eric W. Biederman444ceed2006-10-02 02:18:49 -07002456 struct inode *inode;
2457 struct proc_inode *ei;
Eric W. Biederman444ceed2006-10-02 02:18:49 -07002458
Andreas Gruenbacherdb978da2016-11-10 22:18:28 +01002459 inode = proc_pid_make_inode(dir->i_sb, task, p->mode);
Eric W. Biederman444ceed2006-10-02 02:18:49 -07002460 if (!inode)
2461 goto out;
2462
2463 ei = PROC_I(inode);
Eric W. Biederman444ceed2006-10-02 02:18:49 -07002464 if (S_ISDIR(inode->i_mode))
Miklos Szeredibfe86842011-10-28 14:13:29 +02002465 set_nlink(inode, 2); /* Use getattr to fix if necessary */
Eric W. Biederman444ceed2006-10-02 02:18:49 -07002466 if (p->iop)
2467 inode->i_op = p->iop;
2468 if (p->fop)
2469 inode->i_fop = p->fop;
2470 ei->op = p->op;
Nick Pigginfb045ad2011-01-07 17:49:55 +11002471 d_set_d_op(dentry, &pid_dentry_operations);
Eric W. Biederman444ceed2006-10-02 02:18:49 -07002472 d_add(dentry, inode);
2473 /* Close the race of the process dying before we return the dentry */
Al Viro0b728e12012-06-10 16:03:43 -04002474 if (pid_revalidate(dentry, 0))
Al Viroc52a47a2013-06-15 11:15:20 +04002475 return 0;
Eric W. Biederman444ceed2006-10-02 02:18:49 -07002476out:
Al Viroc52a47a2013-06-15 11:15:20 +04002477 return -ENOENT;
Eric W. Biederman444ceed2006-10-02 02:18:49 -07002478}
2479
Linus Torvalds1da177e2005-04-16 15:20:36 -07002480static struct dentry *proc_pident_lookup(struct inode *dir,
2481 struct dentry *dentry,
Eric Dumazetc5141e62007-05-08 00:26:15 -07002482 const struct pid_entry *ents,
Eric W. Biederman7bcd6b02006-10-02 02:18:56 -07002483 unsigned int nents)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002484{
Al Viroc52a47a2013-06-15 11:15:20 +04002485 int error;
Eric W. Biederman99f89552006-06-26 00:25:55 -07002486 struct task_struct *task = get_proc_task(dir);
Eric Dumazetc5141e62007-05-08 00:26:15 -07002487 const struct pid_entry *p, *last;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002488
Al Viroc52a47a2013-06-15 11:15:20 +04002489 error = -ENOENT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002490
Eric W. Biederman99f89552006-06-26 00:25:55 -07002491 if (!task)
2492 goto out_no_task;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002493
Eric W. Biederman20cdc892006-10-02 02:17:07 -07002494 /*
2495 * Yes, it does not scale. And it should not. Don't add
2496 * new entries into /proc/<tgid>/ without very good reasons.
2497 */
Alexey Dobriyanbac5f5d2016-12-12 16:45:28 -08002498 last = &ents[nents];
2499 for (p = ents; p < last; p++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002500 if (p->len != dentry->d_name.len)
2501 continue;
2502 if (!memcmp(dentry->d_name.name, p->name, p->len))
2503 break;
2504 }
Alexey Dobriyanbac5f5d2016-12-12 16:45:28 -08002505 if (p >= last)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002506 goto out;
2507
Eric W. Biederman444ceed2006-10-02 02:18:49 -07002508 error = proc_pident_instantiate(dir, dentry, task, p);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002509out:
Eric W. Biederman99f89552006-06-26 00:25:55 -07002510 put_task_struct(task);
2511out_no_task:
Al Viroc52a47a2013-06-15 11:15:20 +04002512 return ERR_PTR(error);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002513}
2514
Al Virof0c3b502013-05-16 12:07:31 -04002515static int proc_pident_readdir(struct file *file, struct dir_context *ctx,
Eric Dumazetc5141e62007-05-08 00:26:15 -07002516 const struct pid_entry *ents, unsigned int nents)
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002517{
Al Virof0c3b502013-05-16 12:07:31 -04002518 struct task_struct *task = get_proc_task(file_inode(file));
2519 const struct pid_entry *p;
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002520
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002521 if (!task)
Al Virof0c3b502013-05-16 12:07:31 -04002522 return -ENOENT;
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002523
Al Virof0c3b502013-05-16 12:07:31 -04002524 if (!dir_emit_dots(file, ctx))
2525 goto out;
2526
2527 if (ctx->pos >= nents + 2)
2528 goto out;
2529
Alexey Dobriyanbac5f5d2016-12-12 16:45:28 -08002530 for (p = ents + (ctx->pos - 2); p < ents + nents; p++) {
Al Virof0c3b502013-05-16 12:07:31 -04002531 if (!proc_fill_cache(file, ctx, p->name, p->len,
2532 proc_pident_instantiate, task, p))
2533 break;
2534 ctx->pos++;
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002535 }
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002536out:
Eric W. Biederman61a28782006-10-02 02:18:49 -07002537 put_task_struct(task);
Al Virof0c3b502013-05-16 12:07:31 -04002538 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002539}
2540
Linus Torvalds1da177e2005-04-16 15:20:36 -07002541#ifdef CONFIG_SECURITY
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002542static ssize_t proc_pid_attr_read(struct file * file, char __user * buf,
2543 size_t count, loff_t *ppos)
2544{
Al Viro496ad9a2013-01-23 17:07:38 -05002545 struct inode * inode = file_inode(file);
Al Viro04ff9702007-03-12 16:17:58 +00002546 char *p = NULL;
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002547 ssize_t length;
2548 struct task_struct *task = get_proc_task(inode);
2549
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002550 if (!task)
Al Viro04ff9702007-03-12 16:17:58 +00002551 return -ESRCH;
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002552
2553 length = security_getprocattr(task,
Josef "Jeff" Sipek2fddfee2006-12-08 02:36:36 -08002554 (char*)file->f_path.dentry->d_name.name,
Al Viro04ff9702007-03-12 16:17:58 +00002555 &p);
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002556 put_task_struct(task);
Al Viro04ff9702007-03-12 16:17:58 +00002557 if (length > 0)
2558 length = simple_read_from_buffer(buf, count, ppos, p, length);
2559 kfree(p);
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002560 return length;
2561}
2562
2563static ssize_t proc_pid_attr_write(struct file * file, const char __user * buf,
2564 size_t count, loff_t *ppos)
2565{
Al Viro496ad9a2013-01-23 17:07:38 -05002566 struct inode * inode = file_inode(file);
Al Virobb646cd2015-12-24 00:16:30 -05002567 void *page;
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002568 ssize_t length;
2569 struct task_struct *task = get_proc_task(inode);
2570
2571 length = -ESRCH;
2572 if (!task)
2573 goto out_no_task;
Stephen Smalleyb21507e2017-01-09 10:07:31 -05002574
2575 /* A task may only write its own attributes. */
2576 length = -EACCES;
2577 if (current != task)
2578 goto out;
2579
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002580 if (count > PAGE_SIZE)
2581 count = PAGE_SIZE;
2582
2583 /* No partial writes. */
2584 length = -EINVAL;
2585 if (*ppos != 0)
2586 goto out;
2587
Al Virobb646cd2015-12-24 00:16:30 -05002588 page = memdup_user(buf, count);
2589 if (IS_ERR(page)) {
2590 length = PTR_ERR(page);
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002591 goto out;
Al Virobb646cd2015-12-24 00:16:30 -05002592 }
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002593
David Howells107db7c2009-05-08 13:55:27 +01002594 /* Guard against adverse ptrace interaction */
Stephen Smalleyb21507e2017-01-09 10:07:31 -05002595 length = mutex_lock_interruptible(&current->signal->cred_guard_mutex);
David Howells107db7c2009-05-08 13:55:27 +01002596 if (length < 0)
2597 goto out_free;
2598
Stephen Smalleyb21507e2017-01-09 10:07:31 -05002599 length = security_setprocattr(file->f_path.dentry->d_name.name,
Al Virobb646cd2015-12-24 00:16:30 -05002600 page, count);
Stephen Smalleyb21507e2017-01-09 10:07:31 -05002601 mutex_unlock(&current->signal->cred_guard_mutex);
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002602out_free:
Al Virobb646cd2015-12-24 00:16:30 -05002603 kfree(page);
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002604out:
2605 put_task_struct(task);
2606out_no_task:
2607 return length;
2608}
2609
Arjan van de Ven00977a52007-02-12 00:55:34 -08002610static const struct file_operations proc_pid_attr_operations = {
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002611 .read = proc_pid_attr_read,
2612 .write = proc_pid_attr_write,
Arnd Bergmann87df8422010-03-17 23:06:02 +01002613 .llseek = generic_file_llseek,
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002614};
2615
Eric Dumazetc5141e62007-05-08 00:26:15 -07002616static const struct pid_entry attr_dir_stuff[] = {
Alexey Dobriyan631f9c12008-11-10 01:32:52 +03002617 REG("current", S_IRUGO|S_IWUGO, proc_pid_attr_operations),
2618 REG("prev", S_IRUGO, proc_pid_attr_operations),
2619 REG("exec", S_IRUGO|S_IWUGO, proc_pid_attr_operations),
2620 REG("fscreate", S_IRUGO|S_IWUGO, proc_pid_attr_operations),
2621 REG("keycreate", S_IRUGO|S_IWUGO, proc_pid_attr_operations),
2622 REG("sockcreate", S_IRUGO|S_IWUGO, proc_pid_attr_operations),
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002623};
2624
Al Virof0c3b502013-05-16 12:07:31 -04002625static int proc_attr_dir_readdir(struct file *file, struct dir_context *ctx)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002626{
Al Virof0c3b502013-05-16 12:07:31 -04002627 return proc_pident_readdir(file, ctx,
2628 attr_dir_stuff, ARRAY_SIZE(attr_dir_stuff));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002629}
2630
Arjan van de Ven00977a52007-02-12 00:55:34 -08002631static const struct file_operations proc_attr_dir_operations = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002632 .read = generic_read_dir,
Al Virof50752e2016-04-20 17:13:54 -04002633 .iterate_shared = proc_attr_dir_readdir,
2634 .llseek = generic_file_llseek,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002635};
2636
Eric W. Biederman72d9dcf2006-10-02 02:18:50 -07002637static struct dentry *proc_attr_dir_lookup(struct inode *dir,
Al Viro00cd8dd2012-06-10 17:13:09 -04002638 struct dentry *dentry, unsigned int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002639{
Eric W. Biederman7bcd6b02006-10-02 02:18:56 -07002640 return proc_pident_lookup(dir, dentry,
2641 attr_dir_stuff, ARRAY_SIZE(attr_dir_stuff));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002642}
2643
Arjan van de Venc5ef1c42007-02-12 00:55:40 -08002644static const struct inode_operations proc_attr_dir_inode_operations = {
Eric W. Biederman72d9dcf2006-10-02 02:18:50 -07002645 .lookup = proc_attr_dir_lookup,
Eric W. Biederman99f89552006-06-26 00:25:55 -07002646 .getattr = pid_getattr,
Linus Torvalds6d76fa52006-07-15 12:26:45 -07002647 .setattr = proc_setattr,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002648};
2649
Linus Torvalds1da177e2005-04-16 15:20:36 -07002650#endif
2651
Christoph Hellwig698ba7b2009-12-15 16:47:37 -08002652#ifdef CONFIG_ELF_CORE
Kawai, Hidehiro3cb4a0b2007-07-19 01:48:28 -07002653static ssize_t proc_coredump_filter_read(struct file *file, char __user *buf,
2654 size_t count, loff_t *ppos)
2655{
Al Viro496ad9a2013-01-23 17:07:38 -05002656 struct task_struct *task = get_proc_task(file_inode(file));
Kawai, Hidehiro3cb4a0b2007-07-19 01:48:28 -07002657 struct mm_struct *mm;
2658 char buffer[PROC_NUMBUF];
2659 size_t len;
2660 int ret;
2661
2662 if (!task)
2663 return -ESRCH;
2664
2665 ret = 0;
2666 mm = get_task_mm(task);
2667 if (mm) {
2668 len = snprintf(buffer, sizeof(buffer), "%08lx\n",
2669 ((mm->flags & MMF_DUMP_FILTER_MASK) >>
2670 MMF_DUMP_FILTER_SHIFT));
2671 mmput(mm);
2672 ret = simple_read_from_buffer(buf, count, ppos, buffer, len);
2673 }
2674
2675 put_task_struct(task);
2676
2677 return ret;
2678}
2679
2680static ssize_t proc_coredump_filter_write(struct file *file,
2681 const char __user *buf,
2682 size_t count,
2683 loff_t *ppos)
2684{
2685 struct task_struct *task;
2686 struct mm_struct *mm;
Kawai, Hidehiro3cb4a0b2007-07-19 01:48:28 -07002687 unsigned int val;
2688 int ret;
2689 int i;
2690 unsigned long mask;
2691
Alexey Dobriyan774636e2015-09-09 15:36:59 -07002692 ret = kstrtouint_from_user(buf, count, 0, &val);
2693 if (ret < 0)
2694 return ret;
Kawai, Hidehiro3cb4a0b2007-07-19 01:48:28 -07002695
2696 ret = -ESRCH;
Al Viro496ad9a2013-01-23 17:07:38 -05002697 task = get_proc_task(file_inode(file));
Kawai, Hidehiro3cb4a0b2007-07-19 01:48:28 -07002698 if (!task)
2699 goto out_no_task;
2700
Kawai, Hidehiro3cb4a0b2007-07-19 01:48:28 -07002701 mm = get_task_mm(task);
2702 if (!mm)
2703 goto out_no_mm;
Colin Ian King41a0c2492015-12-18 14:22:01 -08002704 ret = 0;
Kawai, Hidehiro3cb4a0b2007-07-19 01:48:28 -07002705
2706 for (i = 0, mask = 1; i < MMF_DUMP_FILTER_BITS; i++, mask <<= 1) {
2707 if (val & mask)
2708 set_bit(i + MMF_DUMP_FILTER_SHIFT, &mm->flags);
2709 else
2710 clear_bit(i + MMF_DUMP_FILTER_SHIFT, &mm->flags);
2711 }
2712
2713 mmput(mm);
2714 out_no_mm:
2715 put_task_struct(task);
2716 out_no_task:
Alexey Dobriyan774636e2015-09-09 15:36:59 -07002717 if (ret < 0)
2718 return ret;
2719 return count;
Kawai, Hidehiro3cb4a0b2007-07-19 01:48:28 -07002720}
2721
2722static const struct file_operations proc_coredump_filter_operations = {
2723 .read = proc_coredump_filter_read,
2724 .write = proc_coredump_filter_write,
Arnd Bergmann87df8422010-03-17 23:06:02 +01002725 .llseek = generic_file_llseek,
Kawai, Hidehiro3cb4a0b2007-07-19 01:48:28 -07002726};
2727#endif
2728
Andrew Mortonaba76fd2006-12-10 02:19:48 -08002729#ifdef CONFIG_TASK_IO_ACCOUNTING
Alexey Dobriyan19aadc92014-08-08 14:21:50 -07002730static int do_io_accounting(struct task_struct *task, struct seq_file *m, int whole)
Andrew Mortonaba76fd2006-12-10 02:19:48 -08002731{
Andrea Righi940389b2008-07-28 00:48:12 +02002732 struct task_io_accounting acct = task->ioac;
Andrea Righi59954772008-07-27 17:29:15 +02002733 unsigned long flags;
Vasiliy Kulikov293eb1e2011-07-26 16:08:38 -07002734 int result;
Andrea Righi297c5d92008-07-25 01:48:49 -07002735
Vasiliy Kulikov293eb1e2011-07-26 16:08:38 -07002736 result = mutex_lock_killable(&task->signal->cred_guard_mutex);
2737 if (result)
2738 return result;
2739
Jann Horncaaee622016-01-20 15:00:04 -08002740 if (!ptrace_may_access(task, PTRACE_MODE_READ_FSCREDS)) {
Vasiliy Kulikov293eb1e2011-07-26 16:08:38 -07002741 result = -EACCES;
2742 goto out_unlock;
2743 }
Vasiliy Kulikov1d1221f2011-06-24 16:08:38 +04002744
Andrea Righi59954772008-07-27 17:29:15 +02002745 if (whole && lock_task_sighand(task, &flags)) {
2746 struct task_struct *t = task;
Andrea Righib2d002d2008-07-26 15:22:27 -07002747
Andrea Righi59954772008-07-27 17:29:15 +02002748 task_io_accounting_add(&acct, &task->signal->ioac);
2749 while_each_thread(task, t)
2750 task_io_accounting_add(&acct, &t->ioac);
Andrea Righi297c5d92008-07-25 01:48:49 -07002751
Andrea Righi59954772008-07-27 17:29:15 +02002752 unlock_task_sighand(task, &flags);
Andrea Righi297c5d92008-07-25 01:48:49 -07002753 }
Joe Perches25ce3192015-04-15 16:18:17 -07002754 seq_printf(m,
2755 "rchar: %llu\n"
2756 "wchar: %llu\n"
2757 "syscr: %llu\n"
2758 "syscw: %llu\n"
2759 "read_bytes: %llu\n"
2760 "write_bytes: %llu\n"
2761 "cancelled_write_bytes: %llu\n",
2762 (unsigned long long)acct.rchar,
2763 (unsigned long long)acct.wchar,
2764 (unsigned long long)acct.syscr,
2765 (unsigned long long)acct.syscw,
2766 (unsigned long long)acct.read_bytes,
2767 (unsigned long long)acct.write_bytes,
2768 (unsigned long long)acct.cancelled_write_bytes);
2769 result = 0;
2770
Vasiliy Kulikov293eb1e2011-07-26 16:08:38 -07002771out_unlock:
2772 mutex_unlock(&task->signal->cred_guard_mutex);
2773 return result;
Andrew Mortonaba76fd2006-12-10 02:19:48 -08002774}
Andrea Righi297c5d92008-07-25 01:48:49 -07002775
Alexey Dobriyan19aadc92014-08-08 14:21:50 -07002776static int proc_tid_io_accounting(struct seq_file *m, struct pid_namespace *ns,
2777 struct pid *pid, struct task_struct *task)
Andrea Righi297c5d92008-07-25 01:48:49 -07002778{
Alexey Dobriyan19aadc92014-08-08 14:21:50 -07002779 return do_io_accounting(task, m, 0);
Andrea Righi297c5d92008-07-25 01:48:49 -07002780}
2781
Alexey Dobriyan19aadc92014-08-08 14:21:50 -07002782static int proc_tgid_io_accounting(struct seq_file *m, struct pid_namespace *ns,
2783 struct pid *pid, struct task_struct *task)
Andrea Righi297c5d92008-07-25 01:48:49 -07002784{
Alexey Dobriyan19aadc92014-08-08 14:21:50 -07002785 return do_io_accounting(task, m, 1);
Andrea Righi297c5d92008-07-25 01:48:49 -07002786}
2787#endif /* CONFIG_TASK_IO_ACCOUNTING */
Andrew Mortonaba76fd2006-12-10 02:19:48 -08002788
Eric W. Biederman22d917d2011-11-17 00:11:58 -08002789#ifdef CONFIG_USER_NS
2790static int proc_id_map_open(struct inode *inode, struct file *file,
Fabian Frederickccf94f12014-08-08 14:21:22 -07002791 const struct seq_operations *seq_ops)
Eric W. Biederman22d917d2011-11-17 00:11:58 -08002792{
2793 struct user_namespace *ns = NULL;
2794 struct task_struct *task;
2795 struct seq_file *seq;
2796 int ret = -EINVAL;
2797
2798 task = get_proc_task(inode);
2799 if (task) {
2800 rcu_read_lock();
2801 ns = get_user_ns(task_cred_xxx(task, user_ns));
2802 rcu_read_unlock();
2803 put_task_struct(task);
2804 }
2805 if (!ns)
2806 goto err;
2807
2808 ret = seq_open(file, seq_ops);
2809 if (ret)
2810 goto err_put_ns;
2811
2812 seq = file->private_data;
2813 seq->private = ns;
2814
2815 return 0;
2816err_put_ns:
2817 put_user_ns(ns);
2818err:
2819 return ret;
2820}
2821
2822static int proc_id_map_release(struct inode *inode, struct file *file)
2823{
2824 struct seq_file *seq = file->private_data;
2825 struct user_namespace *ns = seq->private;
2826 put_user_ns(ns);
2827 return seq_release(inode, file);
2828}
2829
2830static int proc_uid_map_open(struct inode *inode, struct file *file)
2831{
2832 return proc_id_map_open(inode, file, &proc_uid_seq_operations);
2833}
2834
2835static int proc_gid_map_open(struct inode *inode, struct file *file)
2836{
2837 return proc_id_map_open(inode, file, &proc_gid_seq_operations);
2838}
2839
Eric W. Biedermanf76d2072012-08-30 01:24:05 -07002840static int proc_projid_map_open(struct inode *inode, struct file *file)
2841{
2842 return proc_id_map_open(inode, file, &proc_projid_seq_operations);
2843}
2844
Eric W. Biederman22d917d2011-11-17 00:11:58 -08002845static const struct file_operations proc_uid_map_operations = {
2846 .open = proc_uid_map_open,
2847 .write = proc_uid_map_write,
2848 .read = seq_read,
2849 .llseek = seq_lseek,
2850 .release = proc_id_map_release,
2851};
2852
2853static const struct file_operations proc_gid_map_operations = {
2854 .open = proc_gid_map_open,
2855 .write = proc_gid_map_write,
2856 .read = seq_read,
2857 .llseek = seq_lseek,
2858 .release = proc_id_map_release,
2859};
Eric W. Biedermanf76d2072012-08-30 01:24:05 -07002860
2861static const struct file_operations proc_projid_map_operations = {
2862 .open = proc_projid_map_open,
2863 .write = proc_projid_map_write,
2864 .read = seq_read,
2865 .llseek = seq_lseek,
2866 .release = proc_id_map_release,
2867};
Eric W. Biederman9cc46512014-12-02 12:27:26 -06002868
2869static int proc_setgroups_open(struct inode *inode, struct file *file)
2870{
2871 struct user_namespace *ns = NULL;
2872 struct task_struct *task;
2873 int ret;
2874
2875 ret = -ESRCH;
2876 task = get_proc_task(inode);
2877 if (task) {
2878 rcu_read_lock();
2879 ns = get_user_ns(task_cred_xxx(task, user_ns));
2880 rcu_read_unlock();
2881 put_task_struct(task);
2882 }
2883 if (!ns)
2884 goto err;
2885
2886 if (file->f_mode & FMODE_WRITE) {
2887 ret = -EACCES;
2888 if (!ns_capable(ns, CAP_SYS_ADMIN))
2889 goto err_put_ns;
2890 }
2891
2892 ret = single_open(file, &proc_setgroups_show, ns);
2893 if (ret)
2894 goto err_put_ns;
2895
2896 return 0;
2897err_put_ns:
2898 put_user_ns(ns);
2899err:
2900 return ret;
2901}
2902
2903static int proc_setgroups_release(struct inode *inode, struct file *file)
2904{
2905 struct seq_file *seq = file->private_data;
2906 struct user_namespace *ns = seq->private;
2907 int ret = single_release(inode, file);
2908 put_user_ns(ns);
2909 return ret;
2910}
2911
2912static const struct file_operations proc_setgroups_operations = {
2913 .open = proc_setgroups_open,
2914 .write = proc_setgroups_write,
2915 .read = seq_read,
2916 .llseek = seq_lseek,
2917 .release = proc_setgroups_release,
2918};
Eric W. Biederman22d917d2011-11-17 00:11:58 -08002919#endif /* CONFIG_USER_NS */
2920
Kees Cook47830722008-10-06 03:11:58 +04002921static int proc_pid_personality(struct seq_file *m, struct pid_namespace *ns,
2922 struct pid *pid, struct task_struct *task)
2923{
Al Viroa9712bc2011-03-23 15:52:50 -04002924 int err = lock_trace(task);
2925 if (!err) {
2926 seq_printf(m, "%08x\n", task->personality);
2927 unlock_trace(task);
2928 }
2929 return err;
Kees Cook47830722008-10-06 03:11:58 +04002930}
2931
Josh Poimboeuf7c23b332017-02-13 19:42:41 -06002932#ifdef CONFIG_LIVEPATCH
2933static int proc_pid_patch_state(struct seq_file *m, struct pid_namespace *ns,
2934 struct pid *pid, struct task_struct *task)
2935{
2936 seq_printf(m, "%d\n", task->patch_state);
2937 return 0;
2938}
2939#endif /* CONFIG_LIVEPATCH */
2940
Eric W. Biederman801199c2006-10-02 02:18:48 -07002941/*
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002942 * Thread groups
2943 */
Arjan van de Ven00977a52007-02-12 00:55:34 -08002944static const struct file_operations proc_task_operations;
Arjan van de Venc5ef1c42007-02-12 00:55:40 -08002945static const struct inode_operations proc_task_inode_operations;
Eric W. Biederman20cdc892006-10-02 02:17:07 -07002946
Eric Dumazetc5141e62007-05-08 00:26:15 -07002947static const struct pid_entry tgid_base_stuff[] = {
Alexey Dobriyan631f9c12008-11-10 01:32:52 +03002948 DIR("task", S_IRUGO|S_IXUGO, proc_task_inode_operations, proc_task_operations),
2949 DIR("fd", S_IRUSR|S_IXUSR, proc_fd_inode_operations, proc_fd_operations),
Pavel Emelyanov640708a2012-01-10 15:11:23 -08002950 DIR("map_files", S_IRUSR|S_IXUSR, proc_map_files_inode_operations, proc_map_files_operations),
Alexey Dobriyan631f9c12008-11-10 01:32:52 +03002951 DIR("fdinfo", S_IRUSR|S_IXUSR, proc_fdinfo_inode_operations, proc_fdinfo_operations),
Eric W. Biederman6b4e3062010-03-07 16:41:34 -08002952 DIR("ns", S_IRUSR|S_IXUGO, proc_ns_dir_inode_operations, proc_ns_dir_operations),
Andrew Mortonb2211a32008-03-11 18:03:35 -07002953#ifdef CONFIG_NET
Alexey Dobriyan631f9c12008-11-10 01:32:52 +03002954 DIR("net", S_IRUGO|S_IXUGO, proc_net_inode_operations, proc_net_operations),
Andrew Mortonb2211a32008-03-11 18:03:35 -07002955#endif
Alexey Dobriyan631f9c12008-11-10 01:32:52 +03002956 REG("environ", S_IRUSR, proc_environ_operations),
Al Viroc5317162016-10-05 18:43:43 -04002957 REG("auxv", S_IRUSR, proc_auxv_operations),
Alexey Dobriyan631f9c12008-11-10 01:32:52 +03002958 ONE("status", S_IRUGO, proc_pid_status),
Djalal Harouni35a35042014-04-07 15:38:36 -07002959 ONE("personality", S_IRUSR, proc_pid_personality),
Alexey Dobriyan1c963eb2014-08-08 14:21:37 -07002960 ONE("limits", S_IRUGO, proc_pid_limits),
Ingo Molnar43ae34c2007-07-09 18:52:00 +02002961#ifdef CONFIG_SCHED_DEBUG
Alexey Dobriyan631f9c12008-11-10 01:32:52 +03002962 REG("sched", S_IRUGO|S_IWUSR, proc_pid_sched_operations),
Ingo Molnar43ae34c2007-07-09 18:52:00 +02002963#endif
Mike Galbraith5091faa2010-11-30 14:18:03 +01002964#ifdef CONFIG_SCHED_AUTOGROUP
2965 REG("autogroup", S_IRUGO|S_IWUSR, proc_pid_sched_autogroup_operations),
2966#endif
john stultz4614a696b2009-12-14 18:00:05 -08002967 REG("comm", S_IRUGO|S_IWUSR, proc_pid_set_comm_operations),
Roland McGrathebcb6732008-07-25 19:46:00 -07002968#ifdef CONFIG_HAVE_ARCH_TRACEHOOK
Alexey Dobriyan09d93bd2014-08-08 14:21:39 -07002969 ONE("syscall", S_IRUSR, proc_pid_syscall),
Roland McGrathebcb6732008-07-25 19:46:00 -07002970#endif
Alexey Dobriyanc2c0bb42015-06-25 15:00:54 -07002971 REG("cmdline", S_IRUGO, proc_pid_cmdline_ops),
Alexey Dobriyan631f9c12008-11-10 01:32:52 +03002972 ONE("stat", S_IRUGO, proc_tgid_stat),
2973 ONE("statm", S_IRUGO, proc_pid_statm),
Siddhesh Poyarekarb7643752012-03-21 16:34:04 -07002974 REG("maps", S_IRUGO, proc_pid_maps_operations),
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002975#ifdef CONFIG_NUMA
Siddhesh Poyarekarb7643752012-03-21 16:34:04 -07002976 REG("numa_maps", S_IRUGO, proc_pid_numa_maps_operations),
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002977#endif
Alexey Dobriyan631f9c12008-11-10 01:32:52 +03002978 REG("mem", S_IRUSR|S_IWUSR, proc_mem_operations),
2979 LNK("cwd", proc_cwd_link),
2980 LNK("root", proc_root_link),
2981 LNK("exe", proc_exe_link),
2982 REG("mounts", S_IRUGO, proc_mounts_operations),
2983 REG("mountinfo", S_IRUGO, proc_mountinfo_operations),
2984 REG("mountstats", S_IRUSR, proc_mountstats_operations),
Matt Mackall1e883282008-02-04 22:29:07 -08002985#ifdef CONFIG_PROC_PAGE_MONITOR
Alexey Dobriyan631f9c12008-11-10 01:32:52 +03002986 REG("clear_refs", S_IWUSR, proc_clear_refs_operations),
Siddhesh Poyarekarb7643752012-03-21 16:34:04 -07002987 REG("smaps", S_IRUGO, proc_pid_smaps_operations),
Daniel Colascione493b0e92017-09-06 16:25:08 -07002988 REG("smaps_rollup", S_IRUGO, proc_pid_smaps_rollup_operations),
Djalal Harouni32ed74a2014-04-07 15:38:38 -07002989 REG("pagemap", S_IRUSR, proc_pagemap_operations),
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002990#endif
2991#ifdef CONFIG_SECURITY
Alexey Dobriyan631f9c12008-11-10 01:32:52 +03002992 DIR("attr", S_IRUGO|S_IXUGO, proc_attr_dir_inode_operations, proc_attr_dir_operations),
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002993#endif
2994#ifdef CONFIG_KALLSYMS
Alexey Dobriyanedfcd602014-08-08 14:21:44 -07002995 ONE("wchan", S_IRUGO, proc_pid_wchan),
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002996#endif
Ken Chen2ec220e2008-11-10 11:26:08 +03002997#ifdef CONFIG_STACKTRACE
Djalal Harouni35a35042014-04-07 15:38:36 -07002998 ONE("stack", S_IRUSR, proc_pid_stack),
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002999#endif
Naveen N. Rao5968cec2015-06-30 14:36:03 +05303000#ifdef CONFIG_SCHED_INFO
Alexey Dobriyanf6e826c2014-08-08 14:21:46 -07003001 ONE("schedstat", S_IRUGO, proc_pid_schedstat),
Eric W. Biederman28a6d672006-10-02 02:17:05 -07003002#endif
Arjan van de Ven97455122008-01-25 21:08:34 +01003003#ifdef CONFIG_LATENCYTOP
Alexey Dobriyan631f9c12008-11-10 01:32:52 +03003004 REG("latency", S_IRUGO, proc_lstats_operations),
Arjan van de Ven97455122008-01-25 21:08:34 +01003005#endif
Paul Menage8793d852007-10-18 23:39:39 -07003006#ifdef CONFIG_PROC_PID_CPUSET
Zefan Li52de4772014-09-18 16:03:36 +08003007 ONE("cpuset", S_IRUGO, proc_cpuset_show),
Eric W. Biederman28a6d672006-10-02 02:17:05 -07003008#endif
Paul Menagea4243162007-10-18 23:39:35 -07003009#ifdef CONFIG_CGROUPS
Zefan Li006f4ac2014-09-18 16:03:15 +08003010 ONE("cgroup", S_IRUGO, proc_cgroup_show),
Paul Menagea4243162007-10-18 23:39:35 -07003011#endif
Alexey Dobriyan6ba51e32014-08-08 14:21:48 -07003012 ONE("oom_score", S_IRUGO, proc_oom_score),
David Rientjesfa0cbbf2012-11-12 17:53:04 -08003013 REG("oom_adj", S_IRUGO|S_IWUSR, proc_oom_adj_operations),
David Rientjesa63d83f2010-08-09 17:19:46 -07003014 REG("oom_score_adj", S_IRUGO|S_IWUSR, proc_oom_score_adj_operations),
Eric W. Biederman28a6d672006-10-02 02:17:05 -07003015#ifdef CONFIG_AUDITSYSCALL
Alexey Dobriyan631f9c12008-11-10 01:32:52 +03003016 REG("loginuid", S_IWUSR|S_IRUGO, proc_loginuid_operations),
3017 REG("sessionid", S_IRUGO, proc_sessionid_operations),
Eric W. Biederman28a6d672006-10-02 02:17:05 -07003018#endif
Akinobu Mitaf4f154f2006-12-08 02:39:47 -08003019#ifdef CONFIG_FAULT_INJECTION
Alexey Dobriyan631f9c12008-11-10 01:32:52 +03003020 REG("make-it-fail", S_IRUGO|S_IWUSR, proc_fault_inject_operations),
Akinobu Mita168c42b2017-07-14 14:50:00 -07003021 REG("fail-nth", 0644, proc_fail_nth_operations),
Akinobu Mitaf4f154f2006-12-08 02:39:47 -08003022#endif
Christoph Hellwig698ba7b2009-12-15 16:47:37 -08003023#ifdef CONFIG_ELF_CORE
Alexey Dobriyan631f9c12008-11-10 01:32:52 +03003024 REG("coredump_filter", S_IRUGO|S_IWUSR, proc_coredump_filter_operations),
Kawai, Hidehiro3cb4a0b2007-07-19 01:48:28 -07003025#endif
Andrew Mortonaba76fd2006-12-10 02:19:48 -08003026#ifdef CONFIG_TASK_IO_ACCOUNTING
Alexey Dobriyan19aadc92014-08-08 14:21:50 -07003027 ONE("io", S_IRUSR, proc_tgid_io_accounting),
Andrew Mortonaba76fd2006-12-10 02:19:48 -08003028#endif
Eric W. Biederman22d917d2011-11-17 00:11:58 -08003029#ifdef CONFIG_USER_NS
3030 REG("uid_map", S_IRUGO|S_IWUSR, proc_uid_map_operations),
3031 REG("gid_map", S_IRUGO|S_IWUSR, proc_gid_map_operations),
Eric W. Biedermanf76d2072012-08-30 01:24:05 -07003032 REG("projid_map", S_IRUGO|S_IWUSR, proc_projid_map_operations),
Eric W. Biederman9cc46512014-12-02 12:27:26 -06003033 REG("setgroups", S_IRUGO|S_IWUSR, proc_setgroups_operations),
Eric W. Biederman22d917d2011-11-17 00:11:58 -08003034#endif
Nicolas Pitreb18b6a92017-01-21 00:09:08 -05003035#if defined(CONFIG_CHECKPOINT_RESTORE) && defined(CONFIG_POSIX_TIMERS)
Pavel Emelyanov48f6a7a2013-03-11 13:12:45 +04003036 REG("timers", S_IRUGO, proc_timers_operations),
3037#endif
John Stultz5de23d42016-03-17 14:20:54 -07003038 REG("timerslack_ns", S_IRUGO|S_IWUGO, proc_pid_set_timerslack_ns_operations),
Josh Poimboeuf7c23b332017-02-13 19:42:41 -06003039#ifdef CONFIG_LIVEPATCH
3040 ONE("patch_state", S_IRUSR, proc_pid_patch_state),
3041#endif
Eric W. Biederman28a6d672006-10-02 02:17:05 -07003042};
3043
Al Virof0c3b502013-05-16 12:07:31 -04003044static int proc_tgid_base_readdir(struct file *file, struct dir_context *ctx)
Eric W. Biederman28a6d672006-10-02 02:17:05 -07003045{
Al Virof0c3b502013-05-16 12:07:31 -04003046 return proc_pident_readdir(file, ctx,
3047 tgid_base_stuff, ARRAY_SIZE(tgid_base_stuff));
Eric W. Biederman28a6d672006-10-02 02:17:05 -07003048}
3049
Arjan van de Ven00977a52007-02-12 00:55:34 -08003050static const struct file_operations proc_tgid_base_operations = {
Eric W. Biederman28a6d672006-10-02 02:17:05 -07003051 .read = generic_read_dir,
Al Virof50752e2016-04-20 17:13:54 -04003052 .iterate_shared = proc_tgid_base_readdir,
3053 .llseek = generic_file_llseek,
Eric W. Biederman28a6d672006-10-02 02:17:05 -07003054};
3055
Al Viro00cd8dd2012-06-10 17:13:09 -04003056static struct dentry *proc_tgid_base_lookup(struct inode *dir, struct dentry *dentry, unsigned int flags)
3057{
Eric W. Biederman7bcd6b02006-10-02 02:18:56 -07003058 return proc_pident_lookup(dir, dentry,
3059 tgid_base_stuff, ARRAY_SIZE(tgid_base_stuff));
Eric W. Biederman28a6d672006-10-02 02:17:05 -07003060}
3061
Arjan van de Venc5ef1c42007-02-12 00:55:40 -08003062static const struct inode_operations proc_tgid_base_inode_operations = {
Eric W. Biederman28a6d672006-10-02 02:17:05 -07003063 .lookup = proc_tgid_base_lookup,
3064 .getattr = pid_getattr,
3065 .setattr = proc_setattr,
Vasiliy Kulikov04996802012-01-10 15:11:31 -08003066 .permission = proc_pid_permission,
Eric W. Biederman28a6d672006-10-02 02:17:05 -07003067};
3068
Pavel Emelyanov60347f62007-10-18 23:40:03 -07003069static void proc_flush_task_mnt(struct vfsmount *mnt, pid_t pid, pid_t tgid)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003070{
Eric W. Biederman48e64842006-06-26 00:25:48 -07003071 struct dentry *dentry, *leader, *dir;
Alexey Dobriyane3912ac2018-02-06 15:36:51 -08003072 char buf[10 + 1];
Eric W. Biederman48e64842006-06-26 00:25:48 -07003073 struct qstr name;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003074
Eric W. Biederman48e64842006-06-26 00:25:48 -07003075 name.name = buf;
Alexey Dobriyane3912ac2018-02-06 15:36:51 -08003076 name.len = snprintf(buf, sizeof(buf), "%u", pid);
Al Viro4f522a22013-02-11 23:20:37 -05003077 /* no ->d_hash() rejects on procfs */
Pavel Emelyanov60347f62007-10-18 23:40:03 -07003078 dentry = d_hash_and_lookup(mnt->mnt_root, &name);
Eric W. Biederman48e64842006-06-26 00:25:48 -07003079 if (dentry) {
Eric W. Biedermanbbd51922014-02-13 10:24:23 -08003080 d_invalidate(dentry);
Eric W. Biederman48e64842006-06-26 00:25:48 -07003081 dput(dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003082 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003083
Oleg Nesterovc35a7f12014-12-10 15:54:56 -08003084 if (pid == tgid)
3085 return;
3086
Eric W. Biederman48e64842006-06-26 00:25:48 -07003087 name.name = buf;
Alexey Dobriyane3912ac2018-02-06 15:36:51 -08003088 name.len = snprintf(buf, sizeof(buf), "%u", tgid);
Pavel Emelyanov60347f62007-10-18 23:40:03 -07003089 leader = d_hash_and_lookup(mnt->mnt_root, &name);
Eric W. Biederman48e64842006-06-26 00:25:48 -07003090 if (!leader)
3091 goto out;
3092
3093 name.name = "task";
3094 name.len = strlen(name.name);
3095 dir = d_hash_and_lookup(leader, &name);
3096 if (!dir)
3097 goto out_put_leader;
3098
3099 name.name = buf;
Alexey Dobriyane3912ac2018-02-06 15:36:51 -08003100 name.len = snprintf(buf, sizeof(buf), "%u", pid);
Eric W. Biederman48e64842006-06-26 00:25:48 -07003101 dentry = d_hash_and_lookup(dir, &name);
3102 if (dentry) {
Eric W. Biedermanbbd51922014-02-13 10:24:23 -08003103 d_invalidate(dentry);
Eric W. Biederman48e64842006-06-26 00:25:48 -07003104 dput(dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003105 }
Eric W. Biederman48e64842006-06-26 00:25:48 -07003106
3107 dput(dir);
3108out_put_leader:
3109 dput(leader);
3110out:
3111 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003112}
3113
Randy Dunlap0895e912007-10-21 21:00:10 -07003114/**
3115 * proc_flush_task - Remove dcache entries for @task from the /proc dcache.
3116 * @task: task that should be flushed.
3117 *
3118 * When flushing dentries from proc, one needs to flush them from global
Pavel Emelyanov60347f62007-10-18 23:40:03 -07003119 * proc (proc_mnt) and from all the namespaces' procs this task was seen
Randy Dunlap0895e912007-10-21 21:00:10 -07003120 * in. This call is supposed to do all of this job.
3121 *
3122 * Looks in the dcache for
3123 * /proc/@pid
3124 * /proc/@tgid/task/@pid
3125 * if either directory is present flushes it and all of it'ts children
3126 * from the dcache.
3127 *
3128 * It is safe and reasonable to cache /proc entries for a task until
3129 * that task exits. After that they just clog up the dcache with
3130 * useless entries, possibly causing useful dcache entries to be
3131 * flushed instead. This routine is proved to flush those useless
3132 * dcache entries at process exit time.
3133 *
3134 * NOTE: This routine is just an optimization so it does not guarantee
3135 * that no dcache entries will exist at process exit time it
3136 * just makes it very unlikely that any will persist.
Pavel Emelyanov60347f62007-10-18 23:40:03 -07003137 */
3138
3139void proc_flush_task(struct task_struct *task)
3140{
Eric W. Biederman9fcc2d12007-11-14 17:00:07 -08003141 int i;
Oleg Nesterov9b4d1cbe2009-09-22 16:45:34 -07003142 struct pid *pid, *tgid;
Pavel Emelyanov130f77e2007-10-18 23:40:11 -07003143 struct upid *upid;
3144
Pavel Emelyanov130f77e2007-10-18 23:40:11 -07003145 pid = task_pid(task);
Oleg Nesterov9b4d1cbe2009-09-22 16:45:34 -07003146 tgid = task_tgid(task);
Pavel Emelyanov130f77e2007-10-18 23:40:11 -07003147
Eric W. Biederman9fcc2d12007-11-14 17:00:07 -08003148 for (i = 0; i <= pid->level; i++) {
Pavel Emelyanov130f77e2007-10-18 23:40:11 -07003149 upid = &pid->numbers[i];
3150 proc_flush_task_mnt(upid->ns->proc_mnt, upid->nr,
Oleg Nesterov9b4d1cbe2009-09-22 16:45:34 -07003151 tgid->numbers[i].nr);
Pavel Emelyanov130f77e2007-10-18 23:40:11 -07003152 }
Pavel Emelyanov60347f62007-10-18 23:40:03 -07003153}
3154
Al Viroc52a47a2013-06-15 11:15:20 +04003155static int proc_pid_instantiate(struct inode *dir,
3156 struct dentry * dentry,
3157 struct task_struct *task, const void *ptr)
Eric W. Biederman444ceed2006-10-02 02:18:49 -07003158{
Eric W. Biederman444ceed2006-10-02 02:18:49 -07003159 struct inode *inode;
3160
Andreas Gruenbacherdb978da2016-11-10 22:18:28 +01003161 inode = proc_pid_make_inode(dir->i_sb, task, S_IFDIR | S_IRUGO | S_IXUGO);
Eric W. Biederman444ceed2006-10-02 02:18:49 -07003162 if (!inode)
3163 goto out;
3164
Eric W. Biederman444ceed2006-10-02 02:18:49 -07003165 inode->i_op = &proc_tgid_base_inode_operations;
3166 inode->i_fop = &proc_tgid_base_operations;
3167 inode->i_flags|=S_IMMUTABLE;
Vegard Nossumaed54172008-06-05 22:46:53 -07003168
Alexey Dobriyan1270dd82016-12-12 16:45:32 -08003169 set_nlink(inode, nlink_tgid);
Eric W. Biederman444ceed2006-10-02 02:18:49 -07003170
Nick Pigginfb045ad2011-01-07 17:49:55 +11003171 d_set_d_op(dentry, &pid_dentry_operations);
Eric W. Biederman444ceed2006-10-02 02:18:49 -07003172
3173 d_add(dentry, inode);
3174 /* Close the race of the process dying before we return the dentry */
Al Viro0b728e12012-06-10 16:03:43 -04003175 if (pid_revalidate(dentry, 0))
Al Viroc52a47a2013-06-15 11:15:20 +04003176 return 0;
Eric W. Biederman444ceed2006-10-02 02:18:49 -07003177out:
Al Viroc52a47a2013-06-15 11:15:20 +04003178 return -ENOENT;
Eric W. Biederman444ceed2006-10-02 02:18:49 -07003179}
3180
Al Viro00cd8dd2012-06-10 17:13:09 -04003181struct dentry *proc_pid_lookup(struct inode *dir, struct dentry * dentry, unsigned int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003182{
Alexey Dobriyan335eb532014-08-08 14:21:27 -07003183 int result = -ENOENT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003184 struct task_struct *task;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003185 unsigned tgid;
Pavel Emelyanovb4888932007-10-18 23:40:14 -07003186 struct pid_namespace *ns;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003187
Alexey Dobriyandbcdb502014-08-08 14:21:25 -07003188 tgid = name_to_int(&dentry->d_name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003189 if (tgid == ~0U)
3190 goto out;
3191
Pavel Emelyanovb4888932007-10-18 23:40:14 -07003192 ns = dentry->d_sb->s_fs_info;
Eric W. Biedermande758732006-06-26 00:25:51 -07003193 rcu_read_lock();
Pavel Emelyanovb4888932007-10-18 23:40:14 -07003194 task = find_task_by_pid_ns(tgid, ns);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003195 if (task)
3196 get_task_struct(task);
Eric W. Biedermande758732006-06-26 00:25:51 -07003197 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07003198 if (!task)
3199 goto out;
3200
Eric W. Biederman444ceed2006-10-02 02:18:49 -07003201 result = proc_pid_instantiate(dir, dentry, task, NULL);
Eric W. Biederman48e64842006-06-26 00:25:48 -07003202 put_task_struct(task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003203out:
Al Viroc52a47a2013-06-15 11:15:20 +04003204 return ERR_PTR(result);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003205}
3206
Linus Torvalds1da177e2005-04-16 15:20:36 -07003207/*
Eric W. Biederman0804ef42006-10-02 02:17:04 -07003208 * Find the first task with tgid >= tgid
Eric W. Biederman0bc58a92006-06-26 00:25:50 -07003209 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07003210 */
Eric W. Biederman19fd4bb2007-11-28 16:21:26 -08003211struct tgid_iter {
3212 unsigned int tgid;
Eric W. Biederman0804ef42006-10-02 02:17:04 -07003213 struct task_struct *task;
Eric W. Biederman19fd4bb2007-11-28 16:21:26 -08003214};
3215static struct tgid_iter next_tgid(struct pid_namespace *ns, struct tgid_iter iter)
3216{
Eric W. Biederman0804ef42006-10-02 02:17:04 -07003217 struct pid *pid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003218
Eric W. Biederman19fd4bb2007-11-28 16:21:26 -08003219 if (iter.task)
3220 put_task_struct(iter.task);
Eric W. Biederman0804ef42006-10-02 02:17:04 -07003221 rcu_read_lock();
3222retry:
Eric W. Biederman19fd4bb2007-11-28 16:21:26 -08003223 iter.task = NULL;
3224 pid = find_ge_pid(iter.tgid, ns);
Eric W. Biederman0804ef42006-10-02 02:17:04 -07003225 if (pid) {
Eric W. Biederman19fd4bb2007-11-28 16:21:26 -08003226 iter.tgid = pid_nr_ns(pid, ns);
3227 iter.task = pid_task(pid, PIDTYPE_PID);
Eric W. Biederman0804ef42006-10-02 02:17:04 -07003228 /* What we to know is if the pid we have find is the
3229 * pid of a thread_group_leader. Testing for task
3230 * being a thread_group_leader is the obvious thing
3231 * todo but there is a window when it fails, due to
3232 * the pid transfer logic in de_thread.
3233 *
3234 * So we perform the straight forward test of seeing
3235 * if the pid we have found is the pid of a thread
3236 * group leader, and don't worry if the task we have
3237 * found doesn't happen to be a thread group leader.
3238 * As we don't care in the case of readdir.
3239 */
Eric W. Biederman19fd4bb2007-11-28 16:21:26 -08003240 if (!iter.task || !has_group_leader_pid(iter.task)) {
3241 iter.tgid += 1;
Eric W. Biederman0804ef42006-10-02 02:17:04 -07003242 goto retry;
Eric W. Biederman19fd4bb2007-11-28 16:21:26 -08003243 }
3244 get_task_struct(iter.task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003245 }
Eric W. Biederman454cc102006-06-26 00:25:51 -07003246 rcu_read_unlock();
Eric W. Biederman19fd4bb2007-11-28 16:21:26 -08003247 return iter;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003248}
3249
Eric W. Biederman00978752014-07-31 03:10:50 -07003250#define TGID_OFFSET (FIRST_PROCESS_ENTRY + 2)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003251
3252/* for the /proc/ directory itself, after non-process stuff has been done */
Al Virof0c3b502013-05-16 12:07:31 -04003253int proc_pid_readdir(struct file *file, struct dir_context *ctx)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003254{
Eric W. Biederman19fd4bb2007-11-28 16:21:26 -08003255 struct tgid_iter iter;
Al Viro3aa33772014-10-31 00:42:35 -04003256 struct pid_namespace *ns = file_inode(file)->i_sb->s_fs_info;
Al Virof0c3b502013-05-16 12:07:31 -04003257 loff_t pos = ctx->pos;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003258
Al Viro021ada72013-03-29 19:27:05 -04003259 if (pos >= PID_MAX_LIMIT + TGID_OFFSET)
Al Virof0c3b502013-05-16 12:07:31 -04003260 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003261
Eric W. Biederman00978752014-07-31 03:10:50 -07003262 if (pos == TGID_OFFSET - 2) {
David Howells2b0143b2015-03-17 22:25:59 +00003263 struct inode *inode = d_inode(ns->proc_self);
Al Virodb963162013-06-15 10:45:10 +04003264 if (!dir_emit(ctx, "self", 4, inode->i_ino, DT_LNK))
Al Virof0c3b502013-05-16 12:07:31 -04003265 return 0;
Eric W. Biederman00978752014-07-31 03:10:50 -07003266 ctx->pos = pos = pos + 1;
Al Viro021ada72013-03-29 19:27:05 -04003267 }
Eric W. Biederman00978752014-07-31 03:10:50 -07003268 if (pos == TGID_OFFSET - 1) {
David Howells2b0143b2015-03-17 22:25:59 +00003269 struct inode *inode = d_inode(ns->proc_thread_self);
Eric W. Biederman00978752014-07-31 03:10:50 -07003270 if (!dir_emit(ctx, "thread-self", 11, inode->i_ino, DT_LNK))
3271 return 0;
3272 ctx->pos = pos = pos + 1;
3273 }
3274 iter.tgid = pos - TGID_OFFSET;
Eric W. Biederman19fd4bb2007-11-28 16:21:26 -08003275 iter.task = NULL;
Eric W. Biederman19fd4bb2007-11-28 16:21:26 -08003276 for (iter = next_tgid(ns, iter);
3277 iter.task;
3278 iter.tgid += 1, iter = next_tgid(ns, iter)) {
Alexey Dobriyane3912ac2018-02-06 15:36:51 -08003279 char name[10 + 1];
Al Virof0c3b502013-05-16 12:07:31 -04003280 int len;
Eric Dumazet3ba4bce2017-01-24 15:18:07 -08003281
3282 cond_resched();
Lafcadio Wluiki796f5712017-02-24 15:00:23 -08003283 if (!has_pid_permissions(ns, iter.task, HIDEPID_INVISIBLE))
Al Virof0c3b502013-05-16 12:07:31 -04003284 continue;
Vasiliy Kulikov04996802012-01-10 15:11:31 -08003285
Alexey Dobriyane3912ac2018-02-06 15:36:51 -08003286 len = snprintf(name, sizeof(name), "%u", iter.tgid);
Al Virof0c3b502013-05-16 12:07:31 -04003287 ctx->pos = iter.tgid + TGID_OFFSET;
3288 if (!proc_fill_cache(file, ctx, name, len,
3289 proc_pid_instantiate, iter.task, NULL)) {
Eric W. Biederman19fd4bb2007-11-28 16:21:26 -08003290 put_task_struct(iter.task);
Al Virof0c3b502013-05-16 12:07:31 -04003291 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003292 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003293 }
Al Virof0c3b502013-05-16 12:07:31 -04003294 ctx->pos = PID_MAX_LIMIT + TGID_OFFSET;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003295 return 0;
3296}
3297
Eric W. Biederman0bc58a92006-06-26 00:25:50 -07003298/*
Janis Danisevskis1b3044e2016-05-20 17:00:08 -07003299 * proc_tid_comm_permission is a special permission function exclusively
3300 * used for the node /proc/<pid>/task/<tid>/comm.
3301 * It bypasses generic permission checks in the case where a task of the same
3302 * task group attempts to access the node.
3303 * The rationale behind this is that glibc and bionic access this node for
3304 * cross thread naming (pthread_set/getname_np(!self)). However, if
3305 * PR_SET_DUMPABLE gets set to 0 this node among others becomes uid=0 gid=0,
3306 * which locks out the cross thread naming implementation.
3307 * This function makes sure that the node is always accessible for members of
3308 * same thread group.
3309 */
3310static int proc_tid_comm_permission(struct inode *inode, int mask)
3311{
3312 bool is_same_tgroup;
3313 struct task_struct *task;
3314
3315 task = get_proc_task(inode);
3316 if (!task)
3317 return -ESRCH;
3318 is_same_tgroup = same_thread_group(current, task);
3319 put_task_struct(task);
3320
3321 if (likely(is_same_tgroup && !(mask & MAY_EXEC))) {
3322 /* This file (/proc/<pid>/task/<tid>/comm) can always be
3323 * read or written by the members of the corresponding
3324 * thread group.
3325 */
3326 return 0;
3327 }
3328
3329 return generic_permission(inode, mask);
3330}
3331
3332static const struct inode_operations proc_tid_comm_inode_operations = {
3333 .permission = proc_tid_comm_permission,
3334};
3335
3336/*
Eric W. Biederman28a6d672006-10-02 02:17:05 -07003337 * Tasks
3338 */
Eric Dumazetc5141e62007-05-08 00:26:15 -07003339static const struct pid_entry tid_base_stuff[] = {
Alexey Dobriyan631f9c12008-11-10 01:32:52 +03003340 DIR("fd", S_IRUSR|S_IXUSR, proc_fd_inode_operations, proc_fd_operations),
Jerome Marchand38355412010-04-27 13:13:06 -07003341 DIR("fdinfo", S_IRUSR|S_IXUSR, proc_fdinfo_inode_operations, proc_fdinfo_operations),
Eric W. Biederman6b4e3062010-03-07 16:41:34 -08003342 DIR("ns", S_IRUSR|S_IXUGO, proc_ns_dir_inode_operations, proc_ns_dir_operations),
Eric W. Biederman6ba8ed72014-07-31 16:27:08 -07003343#ifdef CONFIG_NET
3344 DIR("net", S_IRUGO|S_IXUGO, proc_net_inode_operations, proc_net_operations),
3345#endif
Alexey Dobriyan631f9c12008-11-10 01:32:52 +03003346 REG("environ", S_IRUSR, proc_environ_operations),
Al Viroc5317162016-10-05 18:43:43 -04003347 REG("auxv", S_IRUSR, proc_auxv_operations),
Alexey Dobriyan631f9c12008-11-10 01:32:52 +03003348 ONE("status", S_IRUGO, proc_pid_status),
Djalal Harouni35a35042014-04-07 15:38:36 -07003349 ONE("personality", S_IRUSR, proc_pid_personality),
Alexey Dobriyan1c963eb2014-08-08 14:21:37 -07003350 ONE("limits", S_IRUGO, proc_pid_limits),
Ingo Molnar43ae34c2007-07-09 18:52:00 +02003351#ifdef CONFIG_SCHED_DEBUG
Alexey Dobriyan631f9c12008-11-10 01:32:52 +03003352 REG("sched", S_IRUGO|S_IWUSR, proc_pid_sched_operations),
Ingo Molnar43ae34c2007-07-09 18:52:00 +02003353#endif
Janis Danisevskis1b3044e2016-05-20 17:00:08 -07003354 NOD("comm", S_IFREG|S_IRUGO|S_IWUSR,
3355 &proc_tid_comm_inode_operations,
3356 &proc_pid_set_comm_operations, {}),
Roland McGrathebcb6732008-07-25 19:46:00 -07003357#ifdef CONFIG_HAVE_ARCH_TRACEHOOK
Alexey Dobriyan09d93bd2014-08-08 14:21:39 -07003358 ONE("syscall", S_IRUSR, proc_pid_syscall),
Roland McGrathebcb6732008-07-25 19:46:00 -07003359#endif
Alexey Dobriyanc2c0bb42015-06-25 15:00:54 -07003360 REG("cmdline", S_IRUGO, proc_pid_cmdline_ops),
Alexey Dobriyan631f9c12008-11-10 01:32:52 +03003361 ONE("stat", S_IRUGO, proc_tid_stat),
3362 ONE("statm", S_IRUGO, proc_pid_statm),
Siddhesh Poyarekarb7643752012-03-21 16:34:04 -07003363 REG("maps", S_IRUGO, proc_tid_maps_operations),
Iago López Galeiras2e13ba52015-06-25 15:00:57 -07003364#ifdef CONFIG_PROC_CHILDREN
Cyrill Gorcunov818411612012-05-31 16:26:43 -07003365 REG("children", S_IRUGO, proc_tid_children_operations),
3366#endif
Eric W. Biederman28a6d672006-10-02 02:17:05 -07003367#ifdef CONFIG_NUMA
Siddhesh Poyarekarb7643752012-03-21 16:34:04 -07003368 REG("numa_maps", S_IRUGO, proc_tid_numa_maps_operations),
Eric W. Biederman28a6d672006-10-02 02:17:05 -07003369#endif
Alexey Dobriyan631f9c12008-11-10 01:32:52 +03003370 REG("mem", S_IRUSR|S_IWUSR, proc_mem_operations),
3371 LNK("cwd", proc_cwd_link),
3372 LNK("root", proc_root_link),
3373 LNK("exe", proc_exe_link),
3374 REG("mounts", S_IRUGO, proc_mounts_operations),
3375 REG("mountinfo", S_IRUGO, proc_mountinfo_operations),
Matt Mackall1e883282008-02-04 22:29:07 -08003376#ifdef CONFIG_PROC_PAGE_MONITOR
Alexey Dobriyan631f9c12008-11-10 01:32:52 +03003377 REG("clear_refs", S_IWUSR, proc_clear_refs_operations),
Siddhesh Poyarekarb7643752012-03-21 16:34:04 -07003378 REG("smaps", S_IRUGO, proc_tid_smaps_operations),
Daniel Colascione493b0e92017-09-06 16:25:08 -07003379 REG("smaps_rollup", S_IRUGO, proc_pid_smaps_rollup_operations),
Djalal Harouni32ed74a2014-04-07 15:38:38 -07003380 REG("pagemap", S_IRUSR, proc_pagemap_operations),
Eric W. Biederman28a6d672006-10-02 02:17:05 -07003381#endif
3382#ifdef CONFIG_SECURITY
Alexey Dobriyan631f9c12008-11-10 01:32:52 +03003383 DIR("attr", S_IRUGO|S_IXUGO, proc_attr_dir_inode_operations, proc_attr_dir_operations),
Eric W. Biederman28a6d672006-10-02 02:17:05 -07003384#endif
3385#ifdef CONFIG_KALLSYMS
Alexey Dobriyanedfcd602014-08-08 14:21:44 -07003386 ONE("wchan", S_IRUGO, proc_pid_wchan),
Eric W. Biederman28a6d672006-10-02 02:17:05 -07003387#endif
Ken Chen2ec220e2008-11-10 11:26:08 +03003388#ifdef CONFIG_STACKTRACE
Djalal Harouni35a35042014-04-07 15:38:36 -07003389 ONE("stack", S_IRUSR, proc_pid_stack),
Eric W. Biederman28a6d672006-10-02 02:17:05 -07003390#endif
Naveen N. Rao5968cec2015-06-30 14:36:03 +05303391#ifdef CONFIG_SCHED_INFO
Alexey Dobriyanf6e826c2014-08-08 14:21:46 -07003392 ONE("schedstat", S_IRUGO, proc_pid_schedstat),
Eric W. Biederman28a6d672006-10-02 02:17:05 -07003393#endif
Arjan van de Ven97455122008-01-25 21:08:34 +01003394#ifdef CONFIG_LATENCYTOP
Alexey Dobriyan631f9c12008-11-10 01:32:52 +03003395 REG("latency", S_IRUGO, proc_lstats_operations),
Arjan van de Ven97455122008-01-25 21:08:34 +01003396#endif
Paul Menage8793d852007-10-18 23:39:39 -07003397#ifdef CONFIG_PROC_PID_CPUSET
Zefan Li52de4772014-09-18 16:03:36 +08003398 ONE("cpuset", S_IRUGO, proc_cpuset_show),
Eric W. Biederman28a6d672006-10-02 02:17:05 -07003399#endif
Paul Menagea4243162007-10-18 23:39:35 -07003400#ifdef CONFIG_CGROUPS
Zefan Li006f4ac2014-09-18 16:03:15 +08003401 ONE("cgroup", S_IRUGO, proc_cgroup_show),
Paul Menagea4243162007-10-18 23:39:35 -07003402#endif
Alexey Dobriyan6ba51e32014-08-08 14:21:48 -07003403 ONE("oom_score", S_IRUGO, proc_oom_score),
David Rientjesfa0cbbf2012-11-12 17:53:04 -08003404 REG("oom_adj", S_IRUGO|S_IWUSR, proc_oom_adj_operations),
David Rientjesa63d83f2010-08-09 17:19:46 -07003405 REG("oom_score_adj", S_IRUGO|S_IWUSR, proc_oom_score_adj_operations),
Eric W. Biederman28a6d672006-10-02 02:17:05 -07003406#ifdef CONFIG_AUDITSYSCALL
Alexey Dobriyan631f9c12008-11-10 01:32:52 +03003407 REG("loginuid", S_IWUSR|S_IRUGO, proc_loginuid_operations),
Al Viro26ec3c62011-02-15 21:24:05 -05003408 REG("sessionid", S_IRUGO, proc_sessionid_operations),
Eric W. Biederman28a6d672006-10-02 02:17:05 -07003409#endif
Akinobu Mitaf4f154f2006-12-08 02:39:47 -08003410#ifdef CONFIG_FAULT_INJECTION
Alexey Dobriyan631f9c12008-11-10 01:32:52 +03003411 REG("make-it-fail", S_IRUGO|S_IWUSR, proc_fault_inject_operations),
Akinobu Mita1203c8e2017-07-14 14:49:57 -07003412 REG("fail-nth", 0644, proc_fail_nth_operations),
Akinobu Mitaf4f154f2006-12-08 02:39:47 -08003413#endif
Andrea Righi297c5d92008-07-25 01:48:49 -07003414#ifdef CONFIG_TASK_IO_ACCOUNTING
Alexey Dobriyan19aadc92014-08-08 14:21:50 -07003415 ONE("io", S_IRUSR, proc_tid_io_accounting),
Andrea Righi297c5d92008-07-25 01:48:49 -07003416#endif
Eric W. Biederman22d917d2011-11-17 00:11:58 -08003417#ifdef CONFIG_USER_NS
3418 REG("uid_map", S_IRUGO|S_IWUSR, proc_uid_map_operations),
3419 REG("gid_map", S_IRUGO|S_IWUSR, proc_gid_map_operations),
Eric W. Biedermanf76d2072012-08-30 01:24:05 -07003420 REG("projid_map", S_IRUGO|S_IWUSR, proc_projid_map_operations),
Eric W. Biederman9cc46512014-12-02 12:27:26 -06003421 REG("setgroups", S_IRUGO|S_IWUSR, proc_setgroups_operations),
Eric W. Biederman22d917d2011-11-17 00:11:58 -08003422#endif
Josh Poimboeuf7c23b332017-02-13 19:42:41 -06003423#ifdef CONFIG_LIVEPATCH
3424 ONE("patch_state", S_IRUSR, proc_pid_patch_state),
3425#endif
Eric W. Biederman28a6d672006-10-02 02:17:05 -07003426};
3427
Al Virof0c3b502013-05-16 12:07:31 -04003428static int proc_tid_base_readdir(struct file *file, struct dir_context *ctx)
Eric W. Biederman28a6d672006-10-02 02:17:05 -07003429{
Al Virof0c3b502013-05-16 12:07:31 -04003430 return proc_pident_readdir(file, ctx,
3431 tid_base_stuff, ARRAY_SIZE(tid_base_stuff));
Eric W. Biederman28a6d672006-10-02 02:17:05 -07003432}
3433
Al Viro00cd8dd2012-06-10 17:13:09 -04003434static struct dentry *proc_tid_base_lookup(struct inode *dir, struct dentry *dentry, unsigned int flags)
3435{
Eric W. Biederman7bcd6b02006-10-02 02:18:56 -07003436 return proc_pident_lookup(dir, dentry,
3437 tid_base_stuff, ARRAY_SIZE(tid_base_stuff));
Eric W. Biederman28a6d672006-10-02 02:17:05 -07003438}
3439
Arjan van de Ven00977a52007-02-12 00:55:34 -08003440static const struct file_operations proc_tid_base_operations = {
Eric W. Biederman28a6d672006-10-02 02:17:05 -07003441 .read = generic_read_dir,
Al Virof50752e2016-04-20 17:13:54 -04003442 .iterate_shared = proc_tid_base_readdir,
3443 .llseek = generic_file_llseek,
Eric W. Biederman28a6d672006-10-02 02:17:05 -07003444};
3445
Arjan van de Venc5ef1c42007-02-12 00:55:40 -08003446static const struct inode_operations proc_tid_base_inode_operations = {
Eric W. Biederman28a6d672006-10-02 02:17:05 -07003447 .lookup = proc_tid_base_lookup,
3448 .getattr = pid_getattr,
3449 .setattr = proc_setattr,
3450};
3451
Al Viroc52a47a2013-06-15 11:15:20 +04003452static int proc_task_instantiate(struct inode *dir,
Eric Dumazetc5141e62007-05-08 00:26:15 -07003453 struct dentry *dentry, struct task_struct *task, const void *ptr)
Eric W. Biederman444ceed2006-10-02 02:18:49 -07003454{
Eric W. Biederman444ceed2006-10-02 02:18:49 -07003455 struct inode *inode;
Andreas Gruenbacherdb978da2016-11-10 22:18:28 +01003456 inode = proc_pid_make_inode(dir->i_sb, task, S_IFDIR | S_IRUGO | S_IXUGO);
Eric W. Biederman444ceed2006-10-02 02:18:49 -07003457
3458 if (!inode)
3459 goto out;
Eric W. Biederman444ceed2006-10-02 02:18:49 -07003460 inode->i_op = &proc_tid_base_inode_operations;
3461 inode->i_fop = &proc_tid_base_operations;
3462 inode->i_flags|=S_IMMUTABLE;
Vegard Nossumaed54172008-06-05 22:46:53 -07003463
Alexey Dobriyan1270dd82016-12-12 16:45:32 -08003464 set_nlink(inode, nlink_tid);
Eric W. Biederman444ceed2006-10-02 02:18:49 -07003465
Nick Pigginfb045ad2011-01-07 17:49:55 +11003466 d_set_d_op(dentry, &pid_dentry_operations);
Eric W. Biederman444ceed2006-10-02 02:18:49 -07003467
3468 d_add(dentry, inode);
3469 /* Close the race of the process dying before we return the dentry */
Al Viro0b728e12012-06-10 16:03:43 -04003470 if (pid_revalidate(dentry, 0))
Al Viroc52a47a2013-06-15 11:15:20 +04003471 return 0;
Eric W. Biederman444ceed2006-10-02 02:18:49 -07003472out:
Al Viroc52a47a2013-06-15 11:15:20 +04003473 return -ENOENT;
Eric W. Biederman444ceed2006-10-02 02:18:49 -07003474}
3475
Al Viro00cd8dd2012-06-10 17:13:09 -04003476static struct dentry *proc_task_lookup(struct inode *dir, struct dentry * dentry, unsigned int flags)
Eric W. Biederman28a6d672006-10-02 02:17:05 -07003477{
Al Viroc52a47a2013-06-15 11:15:20 +04003478 int result = -ENOENT;
Eric W. Biederman28a6d672006-10-02 02:17:05 -07003479 struct task_struct *task;
3480 struct task_struct *leader = get_proc_task(dir);
Eric W. Biederman28a6d672006-10-02 02:17:05 -07003481 unsigned tid;
Pavel Emelyanovb4888932007-10-18 23:40:14 -07003482 struct pid_namespace *ns;
Eric W. Biederman28a6d672006-10-02 02:17:05 -07003483
3484 if (!leader)
3485 goto out_no_task;
3486
Alexey Dobriyandbcdb502014-08-08 14:21:25 -07003487 tid = name_to_int(&dentry->d_name);
Eric W. Biederman28a6d672006-10-02 02:17:05 -07003488 if (tid == ~0U)
3489 goto out;
3490
Pavel Emelyanovb4888932007-10-18 23:40:14 -07003491 ns = dentry->d_sb->s_fs_info;
Eric W. Biederman28a6d672006-10-02 02:17:05 -07003492 rcu_read_lock();
Pavel Emelyanovb4888932007-10-18 23:40:14 -07003493 task = find_task_by_pid_ns(tid, ns);
Eric W. Biederman28a6d672006-10-02 02:17:05 -07003494 if (task)
3495 get_task_struct(task);
3496 rcu_read_unlock();
3497 if (!task)
3498 goto out;
Pavel Emelyanovbac0abd2007-10-18 23:40:18 -07003499 if (!same_thread_group(leader, task))
Eric W. Biederman28a6d672006-10-02 02:17:05 -07003500 goto out_drop_task;
3501
Eric W. Biederman444ceed2006-10-02 02:18:49 -07003502 result = proc_task_instantiate(dir, dentry, task, NULL);
Eric W. Biederman28a6d672006-10-02 02:17:05 -07003503out_drop_task:
3504 put_task_struct(task);
3505out:
3506 put_task_struct(leader);
3507out_no_task:
Al Viroc52a47a2013-06-15 11:15:20 +04003508 return ERR_PTR(result);
Eric W. Biederman28a6d672006-10-02 02:17:05 -07003509}
3510
3511/*
Eric W. Biederman0bc58a92006-06-26 00:25:50 -07003512 * Find the first tid of a thread group to return to user space.
3513 *
3514 * Usually this is just the thread group leader, but if the users
3515 * buffer was too small or there was a seek into the middle of the
3516 * directory we have more work todo.
3517 *
3518 * In the case of a short read we start with find_task_by_pid.
3519 *
3520 * In the case of a seek we start with the leader and walk nr
3521 * threads past it.
3522 */
Oleg Nesterov9f6e9632014-01-23 15:55:40 -08003523static struct task_struct *first_tid(struct pid *pid, int tid, loff_t f_pos,
3524 struct pid_namespace *ns)
Eric W. Biederman0bc58a92006-06-26 00:25:50 -07003525{
Oleg Nesterovd855a4b2014-01-23 15:55:39 -08003526 struct task_struct *pos, *task;
Oleg Nesterov9f6e9632014-01-23 15:55:40 -08003527 unsigned long nr = f_pos;
3528
3529 if (nr != f_pos) /* 32bit overflow? */
3530 return NULL;
Eric W. Biederman0bc58a92006-06-26 00:25:50 -07003531
Eric W. Biedermancc288732006-06-26 00:26:01 -07003532 rcu_read_lock();
Oleg Nesterovd855a4b2014-01-23 15:55:39 -08003533 task = pid_task(pid, PIDTYPE_PID);
3534 if (!task)
3535 goto fail;
3536
3537 /* Attempt to start with the tid of a thread */
Oleg Nesterov9f6e9632014-01-23 15:55:40 -08003538 if (tid && nr) {
Pavel Emelyanovb4888932007-10-18 23:40:14 -07003539 pos = find_task_by_pid_ns(tid, ns);
Oleg Nesterovd855a4b2014-01-23 15:55:39 -08003540 if (pos && same_thread_group(pos, task))
Oleg Nesterova872ff02006-06-26 00:26:01 -07003541 goto found;
Eric W. Biederman0bc58a92006-06-26 00:25:50 -07003542 }
3543
3544 /* If nr exceeds the number of threads there is nothing todo */
Oleg Nesterov9f6e9632014-01-23 15:55:40 -08003545 if (nr >= get_nr_threads(task))
Oleg Nesterovc986c142014-01-23 15:55:38 -08003546 goto fail;
Oleg Nesterova872ff02006-06-26 00:26:01 -07003547
3548 /* If we haven't found our starting place yet start
3549 * with the leader and walk nr threads forward.
3550 */
Oleg Nesterovd855a4b2014-01-23 15:55:39 -08003551 pos = task = task->group_leader;
Oleg Nesterovc986c142014-01-23 15:55:38 -08003552 do {
Oleg Nesterov9f6e9632014-01-23 15:55:40 -08003553 if (!nr--)
Oleg Nesterovc986c142014-01-23 15:55:38 -08003554 goto found;
Oleg Nesterovd855a4b2014-01-23 15:55:39 -08003555 } while_each_thread(task, pos);
Oleg Nesterovc986c142014-01-23 15:55:38 -08003556fail:
3557 pos = NULL;
3558 goto out;
Oleg Nesterova872ff02006-06-26 00:26:01 -07003559found:
3560 get_task_struct(pos);
3561out:
Eric W. Biedermancc288732006-06-26 00:26:01 -07003562 rcu_read_unlock();
Eric W. Biederman0bc58a92006-06-26 00:25:50 -07003563 return pos;
3564}
3565
3566/*
3567 * Find the next thread in the thread list.
3568 * Return NULL if there is an error or no next thread.
3569 *
3570 * The reference to the input task_struct is released.
3571 */
3572static struct task_struct *next_tid(struct task_struct *start)
3573{
Oleg Nesterovc1df7fb2006-06-26 00:26:02 -07003574 struct task_struct *pos = NULL;
Eric W. Biedermancc288732006-06-26 00:26:01 -07003575 rcu_read_lock();
Oleg Nesterovc1df7fb2006-06-26 00:26:02 -07003576 if (pid_alive(start)) {
Eric W. Biederman0bc58a92006-06-26 00:25:50 -07003577 pos = next_thread(start);
Oleg Nesterovc1df7fb2006-06-26 00:26:02 -07003578 if (thread_group_leader(pos))
3579 pos = NULL;
3580 else
3581 get_task_struct(pos);
3582 }
Eric W. Biedermancc288732006-06-26 00:26:01 -07003583 rcu_read_unlock();
Eric W. Biederman0bc58a92006-06-26 00:25:50 -07003584 put_task_struct(start);
3585 return pos;
3586}
3587
Linus Torvalds1da177e2005-04-16 15:20:36 -07003588/* for the /proc/TGID/task/ directories */
Al Virof0c3b502013-05-16 12:07:31 -04003589static int proc_task_readdir(struct file *file, struct dir_context *ctx)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003590{
Oleg Nesterovd855a4b2014-01-23 15:55:39 -08003591 struct inode *inode = file_inode(file);
3592 struct task_struct *task;
Pavel Emelyanovb4888932007-10-18 23:40:14 -07003593 struct pid_namespace *ns;
Al Virof0c3b502013-05-16 12:07:31 -04003594 int tid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003595
Oleg Nesterovd855a4b2014-01-23 15:55:39 -08003596 if (proc_inode_is_dead(inode))
Al Virof0c3b502013-05-16 12:07:31 -04003597 return -ENOENT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003598
Al Virof0c3b502013-05-16 12:07:31 -04003599 if (!dir_emit_dots(file, ctx))
Oleg Nesterovd855a4b2014-01-23 15:55:39 -08003600 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003601
Eric W. Biederman0bc58a92006-06-26 00:25:50 -07003602 /* f_version caches the tgid value that the last readdir call couldn't
3603 * return. lseek aka telldir automagically resets f_version to 0.
3604 */
Al Viro3aa33772014-10-31 00:42:35 -04003605 ns = inode->i_sb->s_fs_info;
Al Virof0c3b502013-05-16 12:07:31 -04003606 tid = (int)file->f_version;
3607 file->f_version = 0;
Oleg Nesterovd855a4b2014-01-23 15:55:39 -08003608 for (task = first_tid(proc_pid(inode), tid, ctx->pos - 2, ns);
Eric W. Biederman0bc58a92006-06-26 00:25:50 -07003609 task;
Al Virof0c3b502013-05-16 12:07:31 -04003610 task = next_tid(task), ctx->pos++) {
Alexey Dobriyane3912ac2018-02-06 15:36:51 -08003611 char name[10 + 1];
Al Virof0c3b502013-05-16 12:07:31 -04003612 int len;
Pavel Emelyanovb4888932007-10-18 23:40:14 -07003613 tid = task_pid_nr_ns(task, ns);
Alexey Dobriyane3912ac2018-02-06 15:36:51 -08003614 len = snprintf(name, sizeof(name), "%u", tid);
Al Virof0c3b502013-05-16 12:07:31 -04003615 if (!proc_fill_cache(file, ctx, name, len,
3616 proc_task_instantiate, task, NULL)) {
Eric W. Biederman0bc58a92006-06-26 00:25:50 -07003617 /* returning this tgid failed, save it as the first
3618 * pid for the next readir call */
Al Virof0c3b502013-05-16 12:07:31 -04003619 file->f_version = (u64)tid;
Eric W. Biederman0bc58a92006-06-26 00:25:50 -07003620 put_task_struct(task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003621 break;
Eric W. Biederman0bc58a92006-06-26 00:25:50 -07003622 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003623 }
Oleg Nesterovd855a4b2014-01-23 15:55:39 -08003624
Al Virof0c3b502013-05-16 12:07:31 -04003625 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003626}
Eric W. Biederman6e66b522006-06-26 00:25:47 -07003627
David Howellsa528d352017-01-31 16:46:22 +00003628static int proc_task_getattr(const struct path *path, struct kstat *stat,
3629 u32 request_mask, unsigned int query_flags)
Eric W. Biederman6e66b522006-06-26 00:25:47 -07003630{
David Howellsa528d352017-01-31 16:46:22 +00003631 struct inode *inode = d_inode(path->dentry);
Eric W. Biederman99f89552006-06-26 00:25:55 -07003632 struct task_struct *p = get_proc_task(inode);
Eric W. Biederman6e66b522006-06-26 00:25:47 -07003633 generic_fillattr(inode, stat);
3634
Eric W. Biederman99f89552006-06-26 00:25:55 -07003635 if (p) {
Eric W. Biederman99f89552006-06-26 00:25:55 -07003636 stat->nlink += get_nr_threads(p);
Eric W. Biederman99f89552006-06-26 00:25:55 -07003637 put_task_struct(p);
Eric W. Biederman6e66b522006-06-26 00:25:47 -07003638 }
3639
3640 return 0;
3641}
Eric W. Biederman28a6d672006-10-02 02:17:05 -07003642
Arjan van de Venc5ef1c42007-02-12 00:55:40 -08003643static const struct inode_operations proc_task_inode_operations = {
Eric W. Biederman28a6d672006-10-02 02:17:05 -07003644 .lookup = proc_task_lookup,
3645 .getattr = proc_task_getattr,
3646 .setattr = proc_setattr,
Vasiliy Kulikov04996802012-01-10 15:11:31 -08003647 .permission = proc_pid_permission,
Eric W. Biederman28a6d672006-10-02 02:17:05 -07003648};
3649
Arjan van de Ven00977a52007-02-12 00:55:34 -08003650static const struct file_operations proc_task_operations = {
Eric W. Biederman28a6d672006-10-02 02:17:05 -07003651 .read = generic_read_dir,
Al Virof50752e2016-04-20 17:13:54 -04003652 .iterate_shared = proc_task_readdir,
3653 .llseek = generic_file_llseek,
Eric W. Biederman28a6d672006-10-02 02:17:05 -07003654};
Alexey Dobriyan1270dd82016-12-12 16:45:32 -08003655
3656void __init set_proc_pid_nlink(void)
3657{
3658 nlink_tid = pid_entry_nlink(tid_base_stuff, ARRAY_SIZE(tid_base_stuff));
3659 nlink_tgid = pid_entry_nlink(tgid_base_stuff, ARRAY_SIZE(tgid_base_stuff));
3660}