blob: 3fcca77d45f1e758cf3a9b14c801beb22a2a2f63 [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
Alexey Dobriyanc2c0bb42015-06-25 15:00:54 -0700208static ssize_t proc_pid_cmdline_read(struct file *file, 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 struct task_struct *tsk;
212 struct mm_struct *mm;
213 char *page;
214 unsigned long count = _count;
215 unsigned long arg_start, arg_end, env_start, env_end;
216 unsigned long len1, len2, len;
217 unsigned long p;
Alexey Dobriyan6a6cbe72018-06-07 17:09:59 -0700218 char __user *buf0 = buf;
Alexey Dobriyanc2c0bb42015-06-25 15:00:54 -0700219 char c;
Alexey Dobriyan6a6cbe72018-06-07 17:09:59 -0700220 int rv;
Alexey Dobriyanc2c0bb42015-06-25 15:00:54 -0700221
222 BUG_ON(*pos < 0);
223
224 tsk = get_proc_task(file_inode(file));
225 if (!tsk)
226 return -ESRCH;
227 mm = get_task_mm(tsk);
228 put_task_struct(tsk);
229 if (!mm)
230 return 0;
231 /* Check if process spawned far enough to have cmdline. */
232 if (!mm->env_end) {
233 rv = 0;
234 goto out_mmput;
235 }
236
Michal Hocko0ee931c2017-09-13 16:28:29 -0700237 page = (char *)__get_free_page(GFP_KERNEL);
Alexey Dobriyanc2c0bb42015-06-25 15:00:54 -0700238 if (!page) {
239 rv = -ENOMEM;
240 goto out_mmput;
241 }
242
Yang Shi88aa7cc2018-06-07 17:05:28 -0700243 spin_lock(&mm->arg_lock);
Alexey Dobriyanc2c0bb42015-06-25 15:00:54 -0700244 arg_start = mm->arg_start;
245 arg_end = mm->arg_end;
246 env_start = mm->env_start;
247 env_end = mm->env_end;
Yang Shi88aa7cc2018-06-07 17:05:28 -0700248 spin_unlock(&mm->arg_lock);
Alexey Dobriyanc2c0bb42015-06-25 15:00:54 -0700249
250 BUG_ON(arg_start > arg_end);
251 BUG_ON(env_start > env_end);
252
253 len1 = arg_end - arg_start;
254 len2 = env_end - env_start;
255
Alexey Dobriyan3581d452015-07-17 16:24:09 -0700256 /* Empty ARGV. */
Alexey Dobriyan6a6cbe72018-06-07 17:09:59 -0700257 if (len1 == 0)
258 goto end;
259
Alexey Dobriyan2ca66ff2014-08-08 14:21:41 -0700260 /*
Alexey Dobriyanc2c0bb42015-06-25 15:00:54 -0700261 * Inherently racy -- command line shares address space
262 * with code and data.
Alexey Dobriyan2ca66ff2014-08-08 14:21:41 -0700263 */
Alexey Dobriyan6a6cbe72018-06-07 17:09:59 -0700264 if (access_remote_vm(mm, arg_end - 1, &c, 1, FOLL_ANON) != 1)
265 goto end;
Alexey Dobriyanc2c0bb42015-06-25 15:00:54 -0700266
267 if (c == '\0') {
268 /* Command line (set of strings) occupies whole ARGV. */
269 if (len1 <= *pos)
Alexey Dobriyan6a6cbe72018-06-07 17:09:59 -0700270 goto end;
Alexey Dobriyanc2c0bb42015-06-25 15:00:54 -0700271
272 p = arg_start + *pos;
273 len = len1 - *pos;
274 while (count > 0 && len > 0) {
Alexey Dobriyanb42262a2018-06-07 17:09:52 -0700275 unsigned int nr_read;
Alexey Dobriyanc2c0bb42015-06-25 15:00:54 -0700276
Alexey Dobriyanb42262a2018-06-07 17:09:52 -0700277 nr_read = min3(count, len, PAGE_SIZE);
278 nr_read = access_remote_vm(mm, p, page, nr_read, FOLL_ANON);
279 if (nr_read == 0)
Alexey Dobriyan6a6cbe72018-06-07 17:09:59 -0700280 goto end;
Alexey Dobriyanc2c0bb42015-06-25 15:00:54 -0700281
282 if (copy_to_user(buf, page, nr_read)) {
283 rv = -EFAULT;
284 goto out_free_page;
285 }
286
287 p += nr_read;
288 len -= nr_read;
289 buf += nr_read;
290 count -= nr_read;
Alexey Dobriyanc2c0bb42015-06-25 15:00:54 -0700291 }
292 } else {
293 /*
Alexey Dobriyanc2c0bb42015-06-25 15:00:54 -0700294 * Command line (1 string) occupies ARGV and
295 * extends into ENVP.
296 */
Alexey Dobriyana0a07b82017-02-24 15:00:20 -0800297 struct {
298 unsigned long p;
299 unsigned long len;
300 } cmdline[2] = {
301 { .p = arg_start, .len = len1 },
302 { .p = env_start, .len = len2 },
303 };
304 loff_t pos1 = *pos;
305 unsigned int i;
306
307 i = 0;
308 while (i < 2 && pos1 >= cmdline[i].len) {
309 pos1 -= cmdline[i].len;
310 i++;
Alexey Dobriyanc2c0bb42015-06-25 15:00:54 -0700311 }
Alexey Dobriyana0a07b82017-02-24 15:00:20 -0800312 while (i < 2) {
313 p = cmdline[i].p + pos1;
314 len = cmdline[i].len - pos1;
315 while (count > 0 && len > 0) {
Alexey Dobriyan6a6b9c42018-06-07 17:09:55 -0700316 unsigned int nr_read, nr_write;
Alexey Dobriyanc2c0bb42015-06-25 15:00:54 -0700317
Alexey Dobriyanb42262a2018-06-07 17:09:52 -0700318 nr_read = min3(count, len, PAGE_SIZE);
319 nr_read = access_remote_vm(mm, p, page, nr_read, FOLL_ANON);
320 if (nr_read == 0)
Alexey Dobriyan6a6cbe72018-06-07 17:09:59 -0700321 goto end;
Alexey Dobriyanc2c0bb42015-06-25 15:00:54 -0700322
Alexey Dobriyana0a07b82017-02-24 15:00:20 -0800323 /*
324 * Command line can be shorter than whole ARGV
325 * even if last "marker" byte says it is not.
326 */
Alexey Dobriyan6a6b9c42018-06-07 17:09:55 -0700327 nr_write = strnlen(page, nr_read);
Alexey Dobriyana0a07b82017-02-24 15:00:20 -0800328
Alexey Dobriyan6a6b9c42018-06-07 17:09:55 -0700329 if (copy_to_user(buf, page, nr_write)) {
Alexey Dobriyana0a07b82017-02-24 15:00:20 -0800330 rv = -EFAULT;
331 goto out_free_page;
332 }
333
Alexey Dobriyan6a6b9c42018-06-07 17:09:55 -0700334 p += nr_write;
335 len -= nr_write;
336 buf += nr_write;
337 count -= nr_write;
Alexey Dobriyana0a07b82017-02-24 15:00:20 -0800338
Alexey Dobriyan6a6b9c42018-06-07 17:09:55 -0700339 if (nr_write < nr_read)
Alexey Dobriyan6a6cbe72018-06-07 17:09:59 -0700340 goto end;
Alexey Dobriyanc2c0bb42015-06-25 15:00:54 -0700341 }
342
Alexey Dobriyana0a07b82017-02-24 15:00:20 -0800343 /* Only first chunk can be read partially. */
344 pos1 = 0;
345 i++;
Alexey Dobriyanc2c0bb42015-06-25 15:00:54 -0700346 }
Alexey Dobriyanc2c0bb42015-06-25 15:00:54 -0700347 }
348
Alexey Dobriyan6a6cbe72018-06-07 17:09:59 -0700349end:
350 *pos += buf - buf0;
351 rv = buf - buf0;
Alexey Dobriyanc2c0bb42015-06-25 15:00:54 -0700352out_free_page:
353 free_page((unsigned long)page);
354out_mmput:
355 mmput(mm);
Alexey Dobriyanc2c0bb42015-06-25 15:00:54 -0700356 return rv;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700357}
358
Alexey Dobriyanc2c0bb42015-06-25 15:00:54 -0700359static const struct file_operations proc_pid_cmdline_ops = {
360 .read = proc_pid_cmdline_read,
361 .llseek = generic_file_llseek,
362};
363
Linus Torvalds1da177e2005-04-16 15:20:36 -0700364#ifdef CONFIG_KALLSYMS
365/*
366 * Provides a wchan file via kallsyms in a proper one-value-per-file format.
367 * Returns the resolved symbol. If that fails, simply return the address.
368 */
Alexey Dobriyanedfcd602014-08-08 14:21:44 -0700369static int proc_pid_wchan(struct seq_file *m, struct pid_namespace *ns,
370 struct pid *pid, struct task_struct *task)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700371{
Alexey Dobriyanffb45122007-05-08 00:28:41 -0700372 unsigned long wchan;
Tejun Heo9281ace2007-07-17 04:03:51 -0700373 char symname[KSYM_NAME_LEN];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700374
Alexey Dobriyan24b2ec22018-04-10 16:31:30 -0700375 if (!ptrace_may_access(task, PTRACE_MODE_READ_FSCREDS))
376 goto print0;
377
Linus Torvalds1da177e2005-04-16 15:20:36 -0700378 wchan = get_wchan(task);
Alexey Dobriyan24b2ec22018-04-10 16:31:30 -0700379 if (wchan && !lookup_symbol_name(wchan, symname)) {
Alexey Dobriyan21dae0a2018-04-10 16:31:34 -0700380 seq_puts(m, symname);
Alexey Dobriyan24b2ec22018-04-10 16:31:30 -0700381 return 0;
382 }
Joe Perches25ce3192015-04-15 16:18:17 -0700383
Alexey Dobriyan24b2ec22018-04-10 16:31:30 -0700384print0:
385 seq_putc(m, '0');
Joe Perches25ce3192015-04-15 16:18:17 -0700386 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700387}
388#endif /* CONFIG_KALLSYMS */
389
Al Viroa9712bc2011-03-23 15:52:50 -0400390static int lock_trace(struct task_struct *task)
391{
392 int err = mutex_lock_killable(&task->signal->cred_guard_mutex);
393 if (err)
394 return err;
Jann Horncaaee622016-01-20 15:00:04 -0800395 if (!ptrace_may_access(task, PTRACE_MODE_ATTACH_FSCREDS)) {
Al Viroa9712bc2011-03-23 15:52:50 -0400396 mutex_unlock(&task->signal->cred_guard_mutex);
397 return -EPERM;
398 }
399 return 0;
400}
401
402static void unlock_trace(struct task_struct *task)
403{
404 mutex_unlock(&task->signal->cred_guard_mutex);
405}
406
Ken Chen2ec220e2008-11-10 11:26:08 +0300407#ifdef CONFIG_STACKTRACE
408
409#define MAX_STACK_TRACE_DEPTH 64
410
411static int proc_pid_stack(struct seq_file *m, struct pid_namespace *ns,
412 struct pid *pid, struct task_struct *task)
413{
414 struct stack_trace trace;
415 unsigned long *entries;
Al Viroa9712bc2011-03-23 15:52:50 -0400416 int err;
Ken Chen2ec220e2008-11-10 11:26:08 +0300417 int i;
418
419 entries = kmalloc(MAX_STACK_TRACE_DEPTH * sizeof(*entries), GFP_KERNEL);
420 if (!entries)
421 return -ENOMEM;
422
423 trace.nr_entries = 0;
424 trace.max_entries = MAX_STACK_TRACE_DEPTH;
425 trace.entries = entries;
426 trace.skip = 0;
Ken Chen2ec220e2008-11-10 11:26:08 +0300427
Al Viroa9712bc2011-03-23 15:52:50 -0400428 err = lock_trace(task);
429 if (!err) {
430 save_stack_trace_tsk(task, &trace);
431
432 for (i = 0; i < trace.nr_entries; i++) {
Linus Torvalds8f5abe82017-11-27 16:45:56 -0800433 seq_printf(m, "[<0>] %pB\n", (void *)entries[i]);
Al Viroa9712bc2011-03-23 15:52:50 -0400434 }
435 unlock_trace(task);
Ken Chen2ec220e2008-11-10 11:26:08 +0300436 }
437 kfree(entries);
438
Al Viroa9712bc2011-03-23 15:52:50 -0400439 return err;
Ken Chen2ec220e2008-11-10 11:26:08 +0300440}
441#endif
442
Naveen N. Rao5968cec2015-06-30 14:36:03 +0530443#ifdef CONFIG_SCHED_INFO
Linus Torvalds1da177e2005-04-16 15:20:36 -0700444/*
445 * Provides /proc/PID/schedstat
446 */
Alexey Dobriyanf6e826c2014-08-08 14:21:46 -0700447static int proc_pid_schedstat(struct seq_file *m, struct pid_namespace *ns,
448 struct pid *pid, struct task_struct *task)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700449{
Naveen N. Rao5968cec2015-06-30 14:36:03 +0530450 if (unlikely(!sched_info_on()))
451 seq_printf(m, "0 0 0\n");
452 else
453 seq_printf(m, "%llu %llu %lu\n",
Joe Perches25ce3192015-04-15 16:18:17 -0700454 (unsigned long long)task->se.sum_exec_runtime,
455 (unsigned long long)task->sched_info.run_delay,
456 task->sched_info.pcount);
457
458 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700459}
460#endif
461
Arjan van de Ven97455122008-01-25 21:08:34 +0100462#ifdef CONFIG_LATENCYTOP
463static int lstats_show_proc(struct seq_file *m, void *v)
464{
465 int i;
Hiroshi Shimamoto13d77c32008-02-20 16:53:29 -0800466 struct inode *inode = m->private;
467 struct task_struct *task = get_proc_task(inode);
Arjan van de Ven97455122008-01-25 21:08:34 +0100468
Hiroshi Shimamoto13d77c32008-02-20 16:53:29 -0800469 if (!task)
470 return -ESRCH;
471 seq_puts(m, "Latency Top version : v0.1\n");
Arjan van de Ven97455122008-01-25 21:08:34 +0100472 for (i = 0; i < 32; i++) {
Joe Perches34e49d42011-01-12 17:00:30 -0800473 struct latency_record *lr = &task->latency_record[i];
474 if (lr->backtrace[0]) {
Arjan van de Ven97455122008-01-25 21:08:34 +0100475 int q;
Joe Perches34e49d42011-01-12 17:00:30 -0800476 seq_printf(m, "%i %li %li",
477 lr->count, lr->time, lr->max);
Arjan van de Ven97455122008-01-25 21:08:34 +0100478 for (q = 0; q < LT_BACKTRACEDEPTH; q++) {
Joe Perches34e49d42011-01-12 17:00:30 -0800479 unsigned long bt = lr->backtrace[q];
480 if (!bt)
Arjan van de Ven97455122008-01-25 21:08:34 +0100481 break;
Joe Perches34e49d42011-01-12 17:00:30 -0800482 if (bt == ULONG_MAX)
Arjan van de Ven97455122008-01-25 21:08:34 +0100483 break;
Joe Perches34e49d42011-01-12 17:00:30 -0800484 seq_printf(m, " %ps", (void *)bt);
Arjan van de Ven97455122008-01-25 21:08:34 +0100485 }
Alexey Dobriyan9d6de122011-01-12 17:00:32 -0800486 seq_putc(m, '\n');
Arjan van de Ven97455122008-01-25 21:08:34 +0100487 }
488
489 }
Hiroshi Shimamoto13d77c32008-02-20 16:53:29 -0800490 put_task_struct(task);
Arjan van de Ven97455122008-01-25 21:08:34 +0100491 return 0;
492}
493
494static int lstats_open(struct inode *inode, struct file *file)
495{
Hiroshi Shimamoto13d77c32008-02-20 16:53:29 -0800496 return single_open(file, lstats_show_proc, inode);
Hiroshi Shimamotod6643d12008-02-14 10:27:00 -0800497}
498
Arjan van de Ven97455122008-01-25 21:08:34 +0100499static ssize_t lstats_write(struct file *file, const char __user *buf,
500 size_t count, loff_t *offs)
501{
Al Viro496ad9a2013-01-23 17:07:38 -0500502 struct task_struct *task = get_proc_task(file_inode(file));
Arjan van de Ven97455122008-01-25 21:08:34 +0100503
Hiroshi Shimamoto13d77c32008-02-20 16:53:29 -0800504 if (!task)
505 return -ESRCH;
Arjan van de Ven97455122008-01-25 21:08:34 +0100506 clear_all_latency_tracing(task);
Hiroshi Shimamoto13d77c32008-02-20 16:53:29 -0800507 put_task_struct(task);
Arjan van de Ven97455122008-01-25 21:08:34 +0100508
509 return count;
510}
511
512static const struct file_operations proc_lstats_operations = {
513 .open = lstats_open,
514 .read = seq_read,
515 .write = lstats_write,
516 .llseek = seq_lseek,
Hiroshi Shimamoto13d77c32008-02-20 16:53:29 -0800517 .release = single_release,
Arjan van de Ven97455122008-01-25 21:08:34 +0100518};
519
520#endif
521
Alexey Dobriyan6ba51e32014-08-08 14:21:48 -0700522static int proc_oom_score(struct seq_file *m, struct pid_namespace *ns,
523 struct pid *pid, struct task_struct *task)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700524{
David Rientjesa7f638f2012-05-29 15:06:47 -0700525 unsigned long totalpages = totalram_pages + total_swap_pages;
Oleg Nesterovb95c35e2010-04-01 15:13:57 +0200526 unsigned long points = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700527
Oleg Nesterovef419392016-08-02 14:03:19 -0700528 points = oom_badness(task, NULL, NULL, totalpages) *
529 1000 / totalpages;
Joe Perches25ce3192015-04-15 16:18:17 -0700530 seq_printf(m, "%lu\n", points);
531
532 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700533}
534
Neil Hormand85f50d2007-10-18 23:40:37 -0700535struct limit_names {
Alexey Dobriyancedbcca2014-08-08 14:21:33 -0700536 const char *name;
537 const char *unit;
Neil Hormand85f50d2007-10-18 23:40:37 -0700538};
539
540static const struct limit_names lnames[RLIM_NLIMITS] = {
Kees Cookcff4edb2009-09-22 16:45:32 -0700541 [RLIMIT_CPU] = {"Max cpu time", "seconds"},
Neil Hormand85f50d2007-10-18 23:40:37 -0700542 [RLIMIT_FSIZE] = {"Max file size", "bytes"},
543 [RLIMIT_DATA] = {"Max data size", "bytes"},
544 [RLIMIT_STACK] = {"Max stack size", "bytes"},
545 [RLIMIT_CORE] = {"Max core file size", "bytes"},
546 [RLIMIT_RSS] = {"Max resident set", "bytes"},
547 [RLIMIT_NPROC] = {"Max processes", "processes"},
548 [RLIMIT_NOFILE] = {"Max open files", "files"},
549 [RLIMIT_MEMLOCK] = {"Max locked memory", "bytes"},
550 [RLIMIT_AS] = {"Max address space", "bytes"},
551 [RLIMIT_LOCKS] = {"Max file locks", "locks"},
552 [RLIMIT_SIGPENDING] = {"Max pending signals", "signals"},
553 [RLIMIT_MSGQUEUE] = {"Max msgqueue size", "bytes"},
554 [RLIMIT_NICE] = {"Max nice priority", NULL},
555 [RLIMIT_RTPRIO] = {"Max realtime priority", NULL},
Eugene Teo88081172008-02-23 15:23:52 -0800556 [RLIMIT_RTTIME] = {"Max realtime timeout", "us"},
Neil Hormand85f50d2007-10-18 23:40:37 -0700557};
558
559/* Display limits for a process */
Alexey Dobriyan1c963eb2014-08-08 14:21:37 -0700560static int proc_pid_limits(struct seq_file *m, struct pid_namespace *ns,
561 struct pid *pid, struct task_struct *task)
Neil Hormand85f50d2007-10-18 23:40:37 -0700562{
563 unsigned int i;
Neil Hormand85f50d2007-10-18 23:40:37 -0700564 unsigned long flags;
Neil Hormand85f50d2007-10-18 23:40:37 -0700565
566 struct rlimit rlim[RLIM_NLIMITS];
567
Lai Jiangshana6bebbc2008-10-05 00:51:15 +0400568 if (!lock_task_sighand(task, &flags))
Neil Hormand85f50d2007-10-18 23:40:37 -0700569 return 0;
Neil Hormand85f50d2007-10-18 23:40:37 -0700570 memcpy(rlim, task->signal->rlim, sizeof(struct rlimit) * RLIM_NLIMITS);
571 unlock_task_sighand(task, &flags);
Neil Hormand85f50d2007-10-18 23:40:37 -0700572
573 /*
574 * print the file header
575 */
Alexey Dobriyan1c963eb2014-08-08 14:21:37 -0700576 seq_printf(m, "%-25s %-20s %-20s %-10s\n",
Joe Perches25ce3192015-04-15 16:18:17 -0700577 "Limit", "Soft Limit", "Hard Limit", "Units");
Neil Hormand85f50d2007-10-18 23:40:37 -0700578
579 for (i = 0; i < RLIM_NLIMITS; i++) {
580 if (rlim[i].rlim_cur == RLIM_INFINITY)
Alexey Dobriyan1c963eb2014-08-08 14:21:37 -0700581 seq_printf(m, "%-25s %-20s ",
Joe Perches25ce3192015-04-15 16:18:17 -0700582 lnames[i].name, "unlimited");
Neil Hormand85f50d2007-10-18 23:40:37 -0700583 else
Alexey Dobriyan1c963eb2014-08-08 14:21:37 -0700584 seq_printf(m, "%-25s %-20lu ",
Joe Perches25ce3192015-04-15 16:18:17 -0700585 lnames[i].name, rlim[i].rlim_cur);
Neil Hormand85f50d2007-10-18 23:40:37 -0700586
587 if (rlim[i].rlim_max == RLIM_INFINITY)
Alexey Dobriyan1c963eb2014-08-08 14:21:37 -0700588 seq_printf(m, "%-20s ", "unlimited");
Neil Hormand85f50d2007-10-18 23:40:37 -0700589 else
Alexey Dobriyan1c963eb2014-08-08 14:21:37 -0700590 seq_printf(m, "%-20lu ", rlim[i].rlim_max);
Neil Hormand85f50d2007-10-18 23:40:37 -0700591
592 if (lnames[i].unit)
Alexey Dobriyan1c963eb2014-08-08 14:21:37 -0700593 seq_printf(m, "%-10s\n", lnames[i].unit);
Neil Hormand85f50d2007-10-18 23:40:37 -0700594 else
Alexey Dobriyan1c963eb2014-08-08 14:21:37 -0700595 seq_putc(m, '\n');
Neil Hormand85f50d2007-10-18 23:40:37 -0700596 }
597
Alexey Dobriyan1c963eb2014-08-08 14:21:37 -0700598 return 0;
Neil Hormand85f50d2007-10-18 23:40:37 -0700599}
600
Roland McGrathebcb6732008-07-25 19:46:00 -0700601#ifdef CONFIG_HAVE_ARCH_TRACEHOOK
Alexey Dobriyan09d93bd2014-08-08 14:21:39 -0700602static int proc_pid_syscall(struct seq_file *m, struct pid_namespace *ns,
603 struct pid *pid, struct task_struct *task)
Roland McGrathebcb6732008-07-25 19:46:00 -0700604{
605 long nr;
606 unsigned long args[6], sp, pc;
Joe Perches25ce3192015-04-15 16:18:17 -0700607 int res;
608
609 res = lock_trace(task);
Al Viroa9712bc2011-03-23 15:52:50 -0400610 if (res)
611 return res;
Roland McGrathebcb6732008-07-25 19:46:00 -0700612
613 if (task_current_syscall(task, &nr, args, 6, &sp, &pc))
Alexey Dobriyan09d93bd2014-08-08 14:21:39 -0700614 seq_puts(m, "running\n");
Al Viroa9712bc2011-03-23 15:52:50 -0400615 else if (nr < 0)
Alexey Dobriyan09d93bd2014-08-08 14:21:39 -0700616 seq_printf(m, "%ld 0x%lx 0x%lx\n", nr, sp, pc);
Al Viroa9712bc2011-03-23 15:52:50 -0400617 else
Alexey Dobriyan09d93bd2014-08-08 14:21:39 -0700618 seq_printf(m,
Roland McGrathebcb6732008-07-25 19:46:00 -0700619 "%ld 0x%lx 0x%lx 0x%lx 0x%lx 0x%lx 0x%lx 0x%lx 0x%lx\n",
620 nr,
621 args[0], args[1], args[2], args[3], args[4], args[5],
622 sp, pc);
Al Viroa9712bc2011-03-23 15:52:50 -0400623 unlock_trace(task);
Joe Perches25ce3192015-04-15 16:18:17 -0700624
625 return 0;
Roland McGrathebcb6732008-07-25 19:46:00 -0700626}
627#endif /* CONFIG_HAVE_ARCH_TRACEHOOK */
628
Linus Torvalds1da177e2005-04-16 15:20:36 -0700629/************************************************************************/
630/* Here the fs part begins */
631/************************************************************************/
632
633/* permission checks */
Eric W. Biederman778c1142006-06-26 00:25:58 -0700634static int proc_fd_access_allowed(struct inode *inode)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700635{
Eric W. Biederman778c1142006-06-26 00:25:58 -0700636 struct task_struct *task;
637 int allowed = 0;
Eric W. Biedermandf26c402006-06-26 00:25:59 -0700638 /* Allow access to a task's file descriptors if it is us or we
639 * may use ptrace attach to the process and find out that
640 * information.
Eric W. Biederman778c1142006-06-26 00:25:58 -0700641 */
642 task = get_proc_task(inode);
Eric W. Biedermandf26c402006-06-26 00:25:59 -0700643 if (task) {
Jann Horncaaee622016-01-20 15:00:04 -0800644 allowed = ptrace_may_access(task, PTRACE_MODE_READ_FSCREDS);
Eric W. Biederman778c1142006-06-26 00:25:58 -0700645 put_task_struct(task);
Eric W. Biedermandf26c402006-06-26 00:25:59 -0700646 }
Eric W. Biederman778c1142006-06-26 00:25:58 -0700647 return allowed;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700648}
649
Eric W. Biederman6b4e3062010-03-07 16:41:34 -0800650int proc_setattr(struct dentry *dentry, struct iattr *attr)
Linus Torvalds6d76fa52006-07-15 12:26:45 -0700651{
652 int error;
David Howells2b0143b2015-03-17 22:25:59 +0000653 struct inode *inode = d_inode(dentry);
Linus Torvalds6d76fa52006-07-15 12:26:45 -0700654
655 if (attr->ia_valid & ATTR_MODE)
656 return -EPERM;
657
Jan Kara31051c82016-05-26 16:55:18 +0200658 error = setattr_prepare(dentry, attr);
Christoph Hellwig10257742010-06-04 11:30:02 +0200659 if (error)
660 return error;
661
Christoph Hellwig10257742010-06-04 11:30:02 +0200662 setattr_copy(inode, attr);
663 mark_inode_dirty(inode);
664 return 0;
Linus Torvalds6d76fa52006-07-15 12:26:45 -0700665}
666
Vasiliy Kulikov04996802012-01-10 15:11:31 -0800667/*
668 * May current process learn task's sched/cmdline info (for hide_pid_min=1)
669 * or euid/egid (for hide_pid_min=2)?
670 */
671static bool has_pid_permissions(struct pid_namespace *pid,
672 struct task_struct *task,
673 int hide_pid_min)
674{
675 if (pid->hide_pid < hide_pid_min)
676 return true;
677 if (in_group_p(pid->pid_gid))
678 return true;
Jann Horncaaee622016-01-20 15:00:04 -0800679 return ptrace_may_access(task, PTRACE_MODE_READ_FSCREDS);
Vasiliy Kulikov04996802012-01-10 15:11:31 -0800680}
681
682
683static int proc_pid_permission(struct inode *inode, int mask)
684{
Christoph Hellwig76f668b2018-05-16 07:19:01 +0200685 struct pid_namespace *pid = proc_pid_ns(inode);
Vasiliy Kulikov04996802012-01-10 15:11:31 -0800686 struct task_struct *task;
687 bool has_perms;
688
689 task = get_proc_task(inode);
Xiaotian Fenga2ef9902012-01-12 17:17:08 -0800690 if (!task)
691 return -ESRCH;
Lafcadio Wluiki796f5712017-02-24 15:00:23 -0800692 has_perms = has_pid_permissions(pid, task, HIDEPID_NO_ACCESS);
Vasiliy Kulikov04996802012-01-10 15:11:31 -0800693 put_task_struct(task);
694
695 if (!has_perms) {
Lafcadio Wluiki796f5712017-02-24 15:00:23 -0800696 if (pid->hide_pid == HIDEPID_INVISIBLE) {
Vasiliy Kulikov04996802012-01-10 15:11:31 -0800697 /*
698 * Let's make getdents(), stat(), and open()
699 * consistent with each other. If a process
700 * may not stat() a file, it shouldn't be seen
701 * in procfs at all.
702 */
703 return -ENOENT;
704 }
705
706 return -EPERM;
707 }
708 return generic_permission(inode, mask);
709}
710
711
712
Arjan van de Venc5ef1c42007-02-12 00:55:40 -0800713static const struct inode_operations proc_def_inode_operations = {
Linus Torvalds6d76fa52006-07-15 12:26:45 -0700714 .setattr = proc_setattr,
715};
716
Eric W. Biedermanbe614082008-02-08 04:18:30 -0800717static int proc_single_show(struct seq_file *m, void *v)
718{
719 struct inode *inode = m->private;
Christoph Hellwig76f668b2018-05-16 07:19:01 +0200720 struct pid_namespace *ns = proc_pid_ns(inode);
721 struct pid *pid = proc_pid(inode);
Eric W. Biedermanbe614082008-02-08 04:18:30 -0800722 struct task_struct *task;
723 int ret;
724
Eric W. Biedermanbe614082008-02-08 04:18:30 -0800725 task = get_pid_task(pid, PIDTYPE_PID);
726 if (!task)
727 return -ESRCH;
728
729 ret = PROC_I(inode)->op.proc_show(m, ns, pid, task);
730
731 put_task_struct(task);
732 return ret;
733}
734
735static int proc_single_open(struct inode *inode, struct file *filp)
736{
Jovi Zhangc6a34052011-01-12 17:00:34 -0800737 return single_open(filp, proc_single_show, inode);
Eric W. Biedermanbe614082008-02-08 04:18:30 -0800738}
739
740static const struct file_operations proc_single_file_operations = {
741 .open = proc_single_open,
742 .read = seq_read,
743 .llseek = seq_lseek,
744 .release = single_release,
745};
746
Oleg Nesterov5381e162014-10-09 15:25:24 -0700747
748struct mm_struct *proc_mem_open(struct inode *inode, unsigned int mode)
749{
750 struct task_struct *task = get_proc_task(inode);
751 struct mm_struct *mm = ERR_PTR(-ESRCH);
752
753 if (task) {
Jann Horncaaee622016-01-20 15:00:04 -0800754 mm = mm_access(task, mode | PTRACE_MODE_FSCREDS);
Oleg Nesterov5381e162014-10-09 15:25:24 -0700755 put_task_struct(task);
756
757 if (!IS_ERR_OR_NULL(mm)) {
758 /* ensure this mm_struct can't be freed */
Vegard Nossumf1f10072017-02-27 14:30:07 -0800759 mmgrab(mm);
Oleg Nesterov5381e162014-10-09 15:25:24 -0700760 /* but do not pin its memory */
761 mmput(mm);
762 }
763 }
764
765 return mm;
766}
767
Cong Wangb409e572012-05-31 16:26:17 -0700768static int __mem_open(struct inode *inode, struct file *file, unsigned int mode)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700769{
Oleg Nesterov5381e162014-10-09 15:25:24 -0700770 struct mm_struct *mm = proc_mem_open(inode, mode);
Linus Torvaldse2683372012-01-17 15:21:19 -0800771
772 if (IS_ERR(mm))
773 return PTR_ERR(mm);
774
Linus Torvaldse2683372012-01-17 15:21:19 -0800775 file->private_data = mm;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700776 return 0;
777}
778
Cong Wangb409e572012-05-31 16:26:17 -0700779static int mem_open(struct inode *inode, struct file *file)
780{
Djalal Harounibc452b42012-07-30 14:42:28 -0700781 int ret = __mem_open(inode, file, PTRACE_MODE_ATTACH);
782
783 /* OK to pass negative loff_t, we can catch out-of-range */
784 file->f_mode |= FMODE_UNSIGNED_OFFSET;
785
786 return ret;
Cong Wangb409e572012-05-31 16:26:17 -0700787}
788
Oleg Nesterov572d34b2012-01-31 17:14:54 +0100789static ssize_t mem_rw(struct file *file, char __user *buf,
790 size_t count, loff_t *ppos, int write)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700791{
Linus Torvaldse2683372012-01-17 15:21:19 -0800792 struct mm_struct *mm = file->private_data;
Oleg Nesterov572d34b2012-01-31 17:14:54 +0100793 unsigned long addr = *ppos;
794 ssize_t copied;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700795 char *page;
Linus Torvalds272ddc82016-10-24 19:00:44 -0700796 unsigned int flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700797
Linus Torvaldse2683372012-01-17 15:21:19 -0800798 if (!mm)
799 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700800
Michal Hocko0ee931c2017-09-13 16:28:29 -0700801 page = (char *)__get_free_page(GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700802 if (!page)
Linus Torvaldse2683372012-01-17 15:21:19 -0800803 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700804
Frederik Deweerdtf7ca54f2006-09-29 02:01:02 -0700805 copied = 0;
Vegard Nossum388f7932017-02-27 14:30:13 -0800806 if (!mmget_not_zero(mm))
Oleg Nesterov6d08f2c2012-01-31 17:15:11 +0100807 goto free;
808
Linus Torvaldsf511c0b2017-05-30 12:38:59 -0700809 flags = FOLL_FORCE | (write ? FOLL_WRITE : 0);
Lorenzo Stoakes6347e8d2016-10-13 01:20:19 +0100810
Linus Torvalds1da177e2005-04-16 15:20:36 -0700811 while (count > 0) {
Oleg Nesterov572d34b2012-01-31 17:14:54 +0100812 int this_len = min_t(int, count, PAGE_SIZE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700813
Oleg Nesterov572d34b2012-01-31 17:14:54 +0100814 if (write && copy_from_user(page, buf, this_len)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700815 copied = -EFAULT;
816 break;
817 }
Oleg Nesterov572d34b2012-01-31 17:14:54 +0100818
Lorenzo Stoakes6347e8d2016-10-13 01:20:19 +0100819 this_len = access_remote_vm(mm, addr, page, this_len, flags);
Oleg Nesterov572d34b2012-01-31 17:14:54 +0100820 if (!this_len) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700821 if (!copied)
822 copied = -EIO;
823 break;
824 }
Oleg Nesterov572d34b2012-01-31 17:14:54 +0100825
826 if (!write && copy_to_user(buf, page, this_len)) {
827 copied = -EFAULT;
828 break;
829 }
830
831 buf += this_len;
832 addr += this_len;
833 copied += this_len;
834 count -= this_len;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700835 }
Oleg Nesterov572d34b2012-01-31 17:14:54 +0100836 *ppos = addr;
KOSAKI Motohiro30cd8902011-05-26 16:25:52 -0700837
Oleg Nesterov6d08f2c2012-01-31 17:15:11 +0100838 mmput(mm);
839free:
KOSAKI Motohiro30cd8902011-05-26 16:25:52 -0700840 free_page((unsigned long) page);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700841 return copied;
842}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700843
Oleg Nesterov572d34b2012-01-31 17:14:54 +0100844static ssize_t mem_read(struct file *file, char __user *buf,
845 size_t count, loff_t *ppos)
846{
847 return mem_rw(file, buf, count, ppos, 0);
848}
849
850static ssize_t mem_write(struct file *file, const char __user *buf,
851 size_t count, loff_t *ppos)
852{
853 return mem_rw(file, (char __user*)buf, count, ppos, 1);
854}
855
Matt Mackall85863e42008-02-04 22:29:04 -0800856loff_t mem_lseek(struct file *file, loff_t offset, int orig)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700857{
858 switch (orig) {
859 case 0:
860 file->f_pos = offset;
861 break;
862 case 1:
863 file->f_pos += offset;
864 break;
865 default:
866 return -EINVAL;
867 }
868 force_successful_syscall_return();
869 return file->f_pos;
870}
871
Linus Torvaldse2683372012-01-17 15:21:19 -0800872static int mem_release(struct inode *inode, struct file *file)
873{
874 struct mm_struct *mm = file->private_data;
Oleg Nesterov71879d32012-01-31 17:14:38 +0100875 if (mm)
Oleg Nesterov6d08f2c2012-01-31 17:15:11 +0100876 mmdrop(mm);
Linus Torvaldse2683372012-01-17 15:21:19 -0800877 return 0;
878}
879
Arjan van de Ven00977a52007-02-12 00:55:34 -0800880static const struct file_operations proc_mem_operations = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700881 .llseek = mem_lseek,
882 .read = mem_read,
883 .write = mem_write,
884 .open = mem_open,
Linus Torvaldse2683372012-01-17 15:21:19 -0800885 .release = mem_release,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700886};
887
Cong Wangb409e572012-05-31 16:26:17 -0700888static int environ_open(struct inode *inode, struct file *file)
889{
890 return __mem_open(inode, file, PTRACE_MODE_READ);
891}
892
James Pearson315e28c2007-10-16 23:30:17 -0700893static ssize_t environ_read(struct file *file, char __user *buf,
894 size_t count, loff_t *ppos)
895{
James Pearson315e28c2007-10-16 23:30:17 -0700896 char *page;
897 unsigned long src = *ppos;
Cong Wangb409e572012-05-31 16:26:17 -0700898 int ret = 0;
899 struct mm_struct *mm = file->private_data;
Mateusz Guzika3b609e2016-01-20 15:01:05 -0800900 unsigned long env_start, env_end;
James Pearson315e28c2007-10-16 23:30:17 -0700901
Mathias Krause8148a732016-05-05 16:22:26 -0700902 /* Ensure the process spawned far enough to have an environment. */
903 if (!mm || !mm->env_end)
Cong Wangb409e572012-05-31 16:26:17 -0700904 return 0;
James Pearson315e28c2007-10-16 23:30:17 -0700905
Michal Hocko0ee931c2017-09-13 16:28:29 -0700906 page = (char *)__get_free_page(GFP_KERNEL);
James Pearson315e28c2007-10-16 23:30:17 -0700907 if (!page)
Cong Wangb409e572012-05-31 16:26:17 -0700908 return -ENOMEM;
James Pearson315e28c2007-10-16 23:30:17 -0700909
Al Virod6f64b82011-02-15 22:26:01 -0500910 ret = 0;
Vegard Nossum388f7932017-02-27 14:30:13 -0800911 if (!mmget_not_zero(mm))
Cong Wangb409e572012-05-31 16:26:17 -0700912 goto free;
Mateusz Guzika3b609e2016-01-20 15:01:05 -0800913
Yang Shi88aa7cc2018-06-07 17:05:28 -0700914 spin_lock(&mm->arg_lock);
Mateusz Guzika3b609e2016-01-20 15:01:05 -0800915 env_start = mm->env_start;
916 env_end = mm->env_end;
Yang Shi88aa7cc2018-06-07 17:05:28 -0700917 spin_unlock(&mm->arg_lock);
Mateusz Guzika3b609e2016-01-20 15:01:05 -0800918
James Pearson315e28c2007-10-16 23:30:17 -0700919 while (count > 0) {
Djalal Harounie8905ec2012-07-30 14:42:26 -0700920 size_t this_len, max_len;
921 int retval;
922
Mateusz Guzika3b609e2016-01-20 15:01:05 -0800923 if (src >= (env_end - env_start))
Djalal Harounie8905ec2012-07-30 14:42:26 -0700924 break;
James Pearson315e28c2007-10-16 23:30:17 -0700925
Mateusz Guzika3b609e2016-01-20 15:01:05 -0800926 this_len = env_end - (env_start + src);
James Pearson315e28c2007-10-16 23:30:17 -0700927
Djalal Harounie8905ec2012-07-30 14:42:26 -0700928 max_len = min_t(size_t, PAGE_SIZE, count);
929 this_len = min(max_len, this_len);
James Pearson315e28c2007-10-16 23:30:17 -0700930
Willy Tarreau7f7ccc22018-05-11 08:11:44 +0200931 retval = access_remote_vm(mm, (env_start + src), page, this_len, FOLL_ANON);
James Pearson315e28c2007-10-16 23:30:17 -0700932
933 if (retval <= 0) {
934 ret = retval;
935 break;
936 }
937
938 if (copy_to_user(buf, page, retval)) {
939 ret = -EFAULT;
940 break;
941 }
942
943 ret += retval;
944 src += retval;
945 buf += retval;
946 count -= retval;
947 }
948 *ppos = src;
James Pearson315e28c2007-10-16 23:30:17 -0700949 mmput(mm);
Cong Wangb409e572012-05-31 16:26:17 -0700950
951free:
James Pearson315e28c2007-10-16 23:30:17 -0700952 free_page((unsigned long) page);
James Pearson315e28c2007-10-16 23:30:17 -0700953 return ret;
954}
955
956static const struct file_operations proc_environ_operations = {
Cong Wangb409e572012-05-31 16:26:17 -0700957 .open = environ_open,
James Pearson315e28c2007-10-16 23:30:17 -0700958 .read = environ_read,
Arnd Bergmann87df8422010-03-17 23:06:02 +0100959 .llseek = generic_file_llseek,
Cong Wangb409e572012-05-31 16:26:17 -0700960 .release = mem_release,
James Pearson315e28c2007-10-16 23:30:17 -0700961};
962
Al Viroc5317162016-10-05 18:43:43 -0400963static int auxv_open(struct inode *inode, struct file *file)
964{
965 return __mem_open(inode, file, PTRACE_MODE_READ_FSCREDS);
966}
967
968static ssize_t auxv_read(struct file *file, char __user *buf,
969 size_t count, loff_t *ppos)
970{
971 struct mm_struct *mm = file->private_data;
972 unsigned int nwords = 0;
Leon Yu06b28492016-10-27 17:46:50 -0700973
974 if (!mm)
975 return 0;
Al Viroc5317162016-10-05 18:43:43 -0400976 do {
977 nwords += 2;
978 } while (mm->saved_auxv[nwords - 2] != 0); /* AT_NULL */
979 return simple_read_from_buffer(buf, count, ppos, mm->saved_auxv,
980 nwords * sizeof(mm->saved_auxv[0]));
981}
982
983static const struct file_operations proc_auxv_operations = {
984 .open = auxv_open,
985 .read = auxv_read,
986 .llseek = generic_file_llseek,
987 .release = mem_release,
988};
989
David Rientjesfa0cbbf2012-11-12 17:53:04 -0800990static ssize_t oom_adj_read(struct file *file, char __user *buf, size_t count,
991 loff_t *ppos)
992{
Al Viro496ad9a2013-01-23 17:07:38 -0500993 struct task_struct *task = get_proc_task(file_inode(file));
David Rientjesfa0cbbf2012-11-12 17:53:04 -0800994 char buffer[PROC_NUMBUF];
995 int oom_adj = OOM_ADJUST_MIN;
996 size_t len;
David Rientjesfa0cbbf2012-11-12 17:53:04 -0800997
998 if (!task)
999 return -ESRCH;
Michal Hockof913da52016-07-28 15:44:37 -07001000 if (task->signal->oom_score_adj == OOM_SCORE_ADJ_MAX)
1001 oom_adj = OOM_ADJUST_MAX;
1002 else
1003 oom_adj = (task->signal->oom_score_adj * -OOM_DISABLE) /
1004 OOM_SCORE_ADJ_MAX;
David Rientjesfa0cbbf2012-11-12 17:53:04 -08001005 put_task_struct(task);
1006 len = snprintf(buffer, sizeof(buffer), "%d\n", oom_adj);
1007 return simple_read_from_buffer(buf, count, ppos, buffer, len);
1008}
1009
Michal Hocko1d5f0ac2016-07-28 15:44:40 -07001010static int __set_oom_adj(struct file *file, int oom_adj, bool legacy)
1011{
1012 static DEFINE_MUTEX(oom_adj_mutex);
Michal Hocko44a70ade2016-07-28 15:44:43 -07001013 struct mm_struct *mm = NULL;
Michal Hocko1d5f0ac2016-07-28 15:44:40 -07001014 struct task_struct *task;
1015 int err = 0;
1016
1017 task = get_proc_task(file_inode(file));
1018 if (!task)
1019 return -ESRCH;
1020
1021 mutex_lock(&oom_adj_mutex);
1022 if (legacy) {
1023 if (oom_adj < task->signal->oom_score_adj &&
1024 !capable(CAP_SYS_RESOURCE)) {
1025 err = -EACCES;
1026 goto err_unlock;
1027 }
1028 /*
1029 * /proc/pid/oom_adj is provided for legacy purposes, ask users to use
1030 * /proc/pid/oom_score_adj instead.
1031 */
1032 pr_warn_once("%s (%d): /proc/%d/oom_adj is deprecated, please use /proc/%d/oom_score_adj instead.\n",
1033 current->comm, task_pid_nr(current), task_pid_nr(task),
1034 task_pid_nr(task));
1035 } else {
1036 if ((short)oom_adj < task->signal->oom_score_adj_min &&
1037 !capable(CAP_SYS_RESOURCE)) {
1038 err = -EACCES;
1039 goto err_unlock;
1040 }
1041 }
1042
Michal Hocko44a70ade2016-07-28 15:44:43 -07001043 /*
1044 * Make sure we will check other processes sharing the mm if this is
1045 * not vfrok which wants its own oom_score_adj.
1046 * pin the mm so it doesn't go away and get reused after task_unlock
1047 */
1048 if (!task->vfork_done) {
1049 struct task_struct *p = find_lock_task_mm(task);
1050
1051 if (p) {
1052 if (atomic_read(&p->mm->mm_users) > 1) {
1053 mm = p->mm;
Vegard Nossumf1f10072017-02-27 14:30:07 -08001054 mmgrab(mm);
Michal Hocko44a70ade2016-07-28 15:44:43 -07001055 }
1056 task_unlock(p);
1057 }
1058 }
1059
Michal Hocko1d5f0ac2016-07-28 15:44:40 -07001060 task->signal->oom_score_adj = oom_adj;
1061 if (!legacy && has_capability_noaudit(current, CAP_SYS_RESOURCE))
1062 task->signal->oom_score_adj_min = (short)oom_adj;
1063 trace_oom_score_adj_update(task);
Michal Hocko44a70ade2016-07-28 15:44:43 -07001064
1065 if (mm) {
1066 struct task_struct *p;
1067
1068 rcu_read_lock();
1069 for_each_process(p) {
1070 if (same_thread_group(task, p))
1071 continue;
1072
1073 /* do not touch kernel threads or the global init */
1074 if (p->flags & PF_KTHREAD || is_global_init(p))
1075 continue;
1076
1077 task_lock(p);
1078 if (!p->vfork_done && process_shares_mm(p, mm)) {
1079 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",
1080 task_pid_nr(p), p->comm,
1081 p->signal->oom_score_adj, oom_adj,
1082 task_pid_nr(task), task->comm);
1083 p->signal->oom_score_adj = oom_adj;
1084 if (!legacy && has_capability_noaudit(current, CAP_SYS_RESOURCE))
1085 p->signal->oom_score_adj_min = (short)oom_adj;
1086 }
1087 task_unlock(p);
1088 }
1089 rcu_read_unlock();
1090 mmdrop(mm);
1091 }
Michal Hocko1d5f0ac2016-07-28 15:44:40 -07001092err_unlock:
1093 mutex_unlock(&oom_adj_mutex);
1094 put_task_struct(task);
1095 return err;
1096}
Michal Hockof913da52016-07-28 15:44:37 -07001097
David Rientjesb72bdfa2015-11-05 18:50:32 -08001098/*
1099 * /proc/pid/oom_adj exists solely for backwards compatibility with previous
1100 * kernels. The effective policy is defined by oom_score_adj, which has a
1101 * different scale: oom_adj grew exponentially and oom_score_adj grows linearly.
1102 * Values written to oom_adj are simply mapped linearly to oom_score_adj.
1103 * Processes that become oom disabled via oom_adj will still be oom disabled
1104 * with this implementation.
1105 *
1106 * oom_adj cannot be removed since existing userspace binaries use it.
1107 */
David Rientjesfa0cbbf2012-11-12 17:53:04 -08001108static ssize_t oom_adj_write(struct file *file, const char __user *buf,
1109 size_t count, loff_t *ppos)
1110{
David Rientjesfa0cbbf2012-11-12 17:53:04 -08001111 char buffer[PROC_NUMBUF];
1112 int oom_adj;
David Rientjesfa0cbbf2012-11-12 17:53:04 -08001113 int err;
1114
1115 memset(buffer, 0, sizeof(buffer));
1116 if (count > sizeof(buffer) - 1)
1117 count = sizeof(buffer) - 1;
1118 if (copy_from_user(buffer, buf, count)) {
1119 err = -EFAULT;
1120 goto out;
1121 }
1122
1123 err = kstrtoint(strstrip(buffer), 0, &oom_adj);
1124 if (err)
1125 goto out;
1126 if ((oom_adj < OOM_ADJUST_MIN || oom_adj > OOM_ADJUST_MAX) &&
1127 oom_adj != OOM_DISABLE) {
1128 err = -EINVAL;
1129 goto out;
1130 }
1131
David Rientjesfa0cbbf2012-11-12 17:53:04 -08001132 /*
1133 * Scale /proc/pid/oom_score_adj appropriately ensuring that a maximum
1134 * value is always attainable.
1135 */
1136 if (oom_adj == OOM_ADJUST_MAX)
1137 oom_adj = OOM_SCORE_ADJ_MAX;
1138 else
1139 oom_adj = (oom_adj * OOM_SCORE_ADJ_MAX) / -OOM_DISABLE;
1140
Michal Hocko1d5f0ac2016-07-28 15:44:40 -07001141 err = __set_oom_adj(file, oom_adj, true);
David Rientjesfa0cbbf2012-11-12 17:53:04 -08001142out:
1143 return err < 0 ? err : count;
1144}
1145
1146static const struct file_operations proc_oom_adj_operations = {
1147 .read = oom_adj_read,
1148 .write = oom_adj_write,
1149 .llseek = generic_file_llseek,
1150};
1151
David Rientjesa63d83f2010-08-09 17:19:46 -07001152static ssize_t oom_score_adj_read(struct file *file, char __user *buf,
1153 size_t count, loff_t *ppos)
1154{
Al Viro496ad9a2013-01-23 17:07:38 -05001155 struct task_struct *task = get_proc_task(file_inode(file));
David Rientjesa63d83f2010-08-09 17:19:46 -07001156 char buffer[PROC_NUMBUF];
David Rientjesa9c58b902012-12-11 16:02:54 -08001157 short oom_score_adj = OOM_SCORE_ADJ_MIN;
David Rientjesa63d83f2010-08-09 17:19:46 -07001158 size_t len;
1159
1160 if (!task)
1161 return -ESRCH;
Michal Hockof913da52016-07-28 15:44:37 -07001162 oom_score_adj = task->signal->oom_score_adj;
David Rientjesa63d83f2010-08-09 17:19:46 -07001163 put_task_struct(task);
David Rientjesa9c58b902012-12-11 16:02:54 -08001164 len = snprintf(buffer, sizeof(buffer), "%hd\n", oom_score_adj);
David Rientjesa63d83f2010-08-09 17:19:46 -07001165 return simple_read_from_buffer(buf, count, ppos, buffer, len);
1166}
1167
1168static ssize_t oom_score_adj_write(struct file *file, const char __user *buf,
1169 size_t count, loff_t *ppos)
1170{
David Rientjesa63d83f2010-08-09 17:19:46 -07001171 char buffer[PROC_NUMBUF];
Alexey Dobriyan0a8cb8e2011-05-26 16:25:50 -07001172 int oom_score_adj;
David Rientjesa63d83f2010-08-09 17:19:46 -07001173 int err;
1174
1175 memset(buffer, 0, sizeof(buffer));
1176 if (count > sizeof(buffer) - 1)
1177 count = sizeof(buffer) - 1;
David Rientjes723548b2010-10-26 14:21:25 -07001178 if (copy_from_user(buffer, buf, count)) {
1179 err = -EFAULT;
1180 goto out;
1181 }
David Rientjesa63d83f2010-08-09 17:19:46 -07001182
Alexey Dobriyan0a8cb8e2011-05-26 16:25:50 -07001183 err = kstrtoint(strstrip(buffer), 0, &oom_score_adj);
David Rientjesa63d83f2010-08-09 17:19:46 -07001184 if (err)
David Rientjes723548b2010-10-26 14:21:25 -07001185 goto out;
David Rientjesa63d83f2010-08-09 17:19:46 -07001186 if (oom_score_adj < OOM_SCORE_ADJ_MIN ||
David Rientjes723548b2010-10-26 14:21:25 -07001187 oom_score_adj > OOM_SCORE_ADJ_MAX) {
1188 err = -EINVAL;
1189 goto out;
1190 }
David Rientjesa63d83f2010-08-09 17:19:46 -07001191
Michal Hocko1d5f0ac2016-07-28 15:44:40 -07001192 err = __set_oom_adj(file, oom_score_adj, false);
David Rientjes723548b2010-10-26 14:21:25 -07001193out:
1194 return err < 0 ? err : count;
David Rientjesa63d83f2010-08-09 17:19:46 -07001195}
1196
1197static const struct file_operations proc_oom_score_adj_operations = {
1198 .read = oom_score_adj_read,
1199 .write = oom_score_adj_write,
Arnd Bergmann6038f372010-08-15 18:52:59 +02001200 .llseek = default_llseek,
David Rientjesa63d83f2010-08-09 17:19:46 -07001201};
1202
Linus Torvalds1da177e2005-04-16 15:20:36 -07001203#ifdef CONFIG_AUDITSYSCALL
Alexey Dobriyanb4eb4f72016-10-29 19:04:39 +03001204#define TMPBUFLEN 11
Linus Torvalds1da177e2005-04-16 15:20:36 -07001205static ssize_t proc_loginuid_read(struct file * file, char __user * buf,
1206 size_t count, loff_t *ppos)
1207{
Al Viro496ad9a2013-01-23 17:07:38 -05001208 struct inode * inode = file_inode(file);
Eric W. Biederman99f89552006-06-26 00:25:55 -07001209 struct task_struct *task = get_proc_task(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001210 ssize_t length;
1211 char tmpbuf[TMPBUFLEN];
1212
Eric W. Biederman99f89552006-06-26 00:25:55 -07001213 if (!task)
1214 return -ESRCH;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001215 length = scnprintf(tmpbuf, TMPBUFLEN, "%u",
Eric W. Biedermane1760bd2012-09-10 22:39:43 -07001216 from_kuid(file->f_cred->user_ns,
1217 audit_get_loginuid(task)));
Eric W. Biederman99f89552006-06-26 00:25:55 -07001218 put_task_struct(task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001219 return simple_read_from_buffer(buf, count, ppos, tmpbuf, length);
1220}
1221
1222static ssize_t proc_loginuid_write(struct file * file, const char __user * buf,
1223 size_t count, loff_t *ppos)
1224{
Al Viro496ad9a2013-01-23 17:07:38 -05001225 struct inode * inode = file_inode(file);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001226 uid_t loginuid;
Eric W. Biedermane1760bd2012-09-10 22:39:43 -07001227 kuid_t kloginuid;
Alexey Dobriyan774636e2015-09-09 15:36:59 -07001228 int rv;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001229
Paul E. McKenney7dc52152010-02-22 17:04:52 -08001230 rcu_read_lock();
1231 if (current != pid_task(proc_pid(inode), PIDTYPE_PID)) {
1232 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001233 return -EPERM;
Paul E. McKenney7dc52152010-02-22 17:04:52 -08001234 }
1235 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001236
Linus Torvalds1da177e2005-04-16 15:20:36 -07001237 if (*ppos != 0) {
1238 /* No partial writes. */
1239 return -EINVAL;
1240 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001241
Alexey Dobriyan774636e2015-09-09 15:36:59 -07001242 rv = kstrtou32_from_user(buf, count, 10, &loginuid);
1243 if (rv < 0)
1244 return rv;
Eric Paris81407c82013-05-24 09:49:14 -04001245
1246 /* is userspace tring to explicitly UNSET the loginuid? */
1247 if (loginuid == AUDIT_UID_UNSET) {
1248 kloginuid = INVALID_UID;
1249 } else {
1250 kloginuid = make_kuid(file->f_cred->user_ns, loginuid);
Alexey Dobriyan774636e2015-09-09 15:36:59 -07001251 if (!uid_valid(kloginuid))
1252 return -EINVAL;
Eric W. Biedermane1760bd2012-09-10 22:39:43 -07001253 }
1254
Alexey Dobriyan774636e2015-09-09 15:36:59 -07001255 rv = audit_set_loginuid(kloginuid);
1256 if (rv < 0)
1257 return rv;
1258 return count;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001259}
1260
Arjan van de Ven00977a52007-02-12 00:55:34 -08001261static const struct file_operations proc_loginuid_operations = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001262 .read = proc_loginuid_read,
1263 .write = proc_loginuid_write,
Arnd Bergmann87df8422010-03-17 23:06:02 +01001264 .llseek = generic_file_llseek,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001265};
Eric Paris1e0bd752008-03-13 08:15:31 -04001266
1267static ssize_t proc_sessionid_read(struct file * file, char __user * buf,
1268 size_t count, loff_t *ppos)
1269{
Al Viro496ad9a2013-01-23 17:07:38 -05001270 struct inode * inode = file_inode(file);
Eric Paris1e0bd752008-03-13 08:15:31 -04001271 struct task_struct *task = get_proc_task(inode);
1272 ssize_t length;
1273 char tmpbuf[TMPBUFLEN];
1274
1275 if (!task)
1276 return -ESRCH;
1277 length = scnprintf(tmpbuf, TMPBUFLEN, "%u",
1278 audit_get_sessionid(task));
1279 put_task_struct(task);
1280 return simple_read_from_buffer(buf, count, ppos, tmpbuf, length);
1281}
1282
1283static const struct file_operations proc_sessionid_operations = {
1284 .read = proc_sessionid_read,
Arnd Bergmann87df8422010-03-17 23:06:02 +01001285 .llseek = generic_file_llseek,
Eric Paris1e0bd752008-03-13 08:15:31 -04001286};
Linus Torvalds1da177e2005-04-16 15:20:36 -07001287#endif
1288
Akinobu Mitaf4f154f2006-12-08 02:39:47 -08001289#ifdef CONFIG_FAULT_INJECTION
1290static ssize_t proc_fault_inject_read(struct file * file, char __user * buf,
1291 size_t count, loff_t *ppos)
1292{
Al Viro496ad9a2013-01-23 17:07:38 -05001293 struct task_struct *task = get_proc_task(file_inode(file));
Akinobu Mitaf4f154f2006-12-08 02:39:47 -08001294 char buffer[PROC_NUMBUF];
1295 size_t len;
1296 int make_it_fail;
Akinobu Mitaf4f154f2006-12-08 02:39:47 -08001297
1298 if (!task)
1299 return -ESRCH;
1300 make_it_fail = task->make_it_fail;
1301 put_task_struct(task);
1302
1303 len = snprintf(buffer, sizeof(buffer), "%i\n", make_it_fail);
Akinobu Mita0c28f282007-05-08 00:31:41 -07001304
1305 return simple_read_from_buffer(buf, count, ppos, buffer, len);
Akinobu Mitaf4f154f2006-12-08 02:39:47 -08001306}
1307
1308static ssize_t proc_fault_inject_write(struct file * file,
1309 const char __user * buf, size_t count, loff_t *ppos)
1310{
1311 struct task_struct *task;
Alexey Dobriyan774636e2015-09-09 15:36:59 -07001312 char buffer[PROC_NUMBUF];
Akinobu Mitaf4f154f2006-12-08 02:39:47 -08001313 int make_it_fail;
Alexey Dobriyan774636e2015-09-09 15:36:59 -07001314 int rv;
Akinobu Mitaf4f154f2006-12-08 02:39:47 -08001315
1316 if (!capable(CAP_SYS_RESOURCE))
1317 return -EPERM;
1318 memset(buffer, 0, sizeof(buffer));
1319 if (count > sizeof(buffer) - 1)
1320 count = sizeof(buffer) - 1;
1321 if (copy_from_user(buffer, buf, count))
1322 return -EFAULT;
Alexey Dobriyan774636e2015-09-09 15:36:59 -07001323 rv = kstrtoint(strstrip(buffer), 0, &make_it_fail);
1324 if (rv < 0)
1325 return rv;
Dave Jones16caed32014-04-07 15:39:15 -07001326 if (make_it_fail < 0 || make_it_fail > 1)
1327 return -EINVAL;
1328
Al Viro496ad9a2013-01-23 17:07:38 -05001329 task = get_proc_task(file_inode(file));
Akinobu Mitaf4f154f2006-12-08 02:39:47 -08001330 if (!task)
1331 return -ESRCH;
1332 task->make_it_fail = make_it_fail;
1333 put_task_struct(task);
Vincent Licba8aaf2009-09-22 16:45:38 -07001334
1335 return count;
Akinobu Mitaf4f154f2006-12-08 02:39:47 -08001336}
1337
Arjan van de Ven00977a52007-02-12 00:55:34 -08001338static const struct file_operations proc_fault_inject_operations = {
Akinobu Mitaf4f154f2006-12-08 02:39:47 -08001339 .read = proc_fault_inject_read,
1340 .write = proc_fault_inject_write,
Arnd Bergmann87df8422010-03-17 23:06:02 +01001341 .llseek = generic_file_llseek,
Akinobu Mitaf4f154f2006-12-08 02:39:47 -08001342};
Dmitry Vyukove41d58182017-07-12 14:34:35 -07001343
1344static ssize_t proc_fail_nth_write(struct file *file, const char __user *buf,
1345 size_t count, loff_t *ppos)
1346{
1347 struct task_struct *task;
Akinobu Mita9049f2f2017-07-14 14:49:52 -07001348 int err;
1349 unsigned int n;
Dmitry Vyukove41d58182017-07-12 14:34:35 -07001350
Akinobu Mita9049f2f2017-07-14 14:49:52 -07001351 err = kstrtouint_from_user(buf, count, 0, &n);
Dmitry Vyukove41d58182017-07-12 14:34:35 -07001352 if (err)
1353 return err;
Akinobu Mita1203c8e2017-07-14 14:49:57 -07001354
1355 task = get_proc_task(file_inode(file));
1356 if (!task)
1357 return -ESRCH;
Alexey Dobriyan9f7118b2018-02-06 15:36:55 -08001358 task->fail_nth = n;
Akinobu Mita1203c8e2017-07-14 14:49:57 -07001359 put_task_struct(task);
1360
Dmitry Vyukove41d58182017-07-12 14:34:35 -07001361 return count;
1362}
1363
1364static ssize_t proc_fail_nth_read(struct file *file, char __user *buf,
1365 size_t count, loff_t *ppos)
1366{
1367 struct task_struct *task;
Akinobu Mitabfc74092017-07-14 14:49:54 -07001368 char numbuf[PROC_NUMBUF];
1369 ssize_t len;
Dmitry Vyukove41d58182017-07-12 14:34:35 -07001370
1371 task = get_proc_task(file_inode(file));
1372 if (!task)
1373 return -ESRCH;
Alexey Dobriyan9f7118b2018-02-06 15:36:55 -08001374 len = snprintf(numbuf, sizeof(numbuf), "%u\n", task->fail_nth);
Akinobu Mitabfc74092017-07-14 14:49:54 -07001375 len = simple_read_from_buffer(buf, count, ppos, numbuf, len);
Akinobu Mita1203c8e2017-07-14 14:49:57 -07001376 put_task_struct(task);
Akinobu Mitabfc74092017-07-14 14:49:54 -07001377
1378 return len;
Dmitry Vyukove41d58182017-07-12 14:34:35 -07001379}
1380
1381static const struct file_operations proc_fail_nth_operations = {
1382 .read = proc_fail_nth_read,
1383 .write = proc_fail_nth_write,
1384};
Akinobu Mitaf4f154f2006-12-08 02:39:47 -08001385#endif
1386
Arjan van de Ven97455122008-01-25 21:08:34 +01001387
Ingo Molnar43ae34c2007-07-09 18:52:00 +02001388#ifdef CONFIG_SCHED_DEBUG
1389/*
1390 * Print out various scheduling related per-task fields:
1391 */
1392static int sched_show(struct seq_file *m, void *v)
1393{
1394 struct inode *inode = m->private;
Christoph Hellwig76f668b2018-05-16 07:19:01 +02001395 struct pid_namespace *ns = proc_pid_ns(inode);
Ingo Molnar43ae34c2007-07-09 18:52:00 +02001396 struct task_struct *p;
1397
Ingo Molnar43ae34c2007-07-09 18:52:00 +02001398 p = get_proc_task(inode);
1399 if (!p)
1400 return -ESRCH;
Aleksa Sarai74dc3382017-08-06 14:41:41 +10001401 proc_sched_show_task(p, ns, m);
Ingo Molnar43ae34c2007-07-09 18:52:00 +02001402
1403 put_task_struct(p);
1404
1405 return 0;
1406}
1407
1408static ssize_t
1409sched_write(struct file *file, const char __user *buf,
1410 size_t count, loff_t *offset)
1411{
Al Viro496ad9a2013-01-23 17:07:38 -05001412 struct inode *inode = file_inode(file);
Ingo Molnar43ae34c2007-07-09 18:52:00 +02001413 struct task_struct *p;
1414
Ingo Molnar43ae34c2007-07-09 18:52:00 +02001415 p = get_proc_task(inode);
1416 if (!p)
1417 return -ESRCH;
1418 proc_sched_set_task(p);
1419
1420 put_task_struct(p);
1421
1422 return count;
1423}
1424
1425static int sched_open(struct inode *inode, struct file *filp)
1426{
Jovi Zhangc6a34052011-01-12 17:00:34 -08001427 return single_open(filp, sched_show, inode);
Ingo Molnar43ae34c2007-07-09 18:52:00 +02001428}
1429
1430static const struct file_operations proc_pid_sched_operations = {
1431 .open = sched_open,
1432 .read = seq_read,
1433 .write = sched_write,
1434 .llseek = seq_lseek,
Alexey Dobriyan5ea473a2007-07-31 00:38:50 -07001435 .release = single_release,
Ingo Molnar43ae34c2007-07-09 18:52:00 +02001436};
1437
1438#endif
1439
Mike Galbraith5091faa2010-11-30 14:18:03 +01001440#ifdef CONFIG_SCHED_AUTOGROUP
1441/*
1442 * Print out autogroup related information:
1443 */
1444static int sched_autogroup_show(struct seq_file *m, void *v)
1445{
1446 struct inode *inode = m->private;
1447 struct task_struct *p;
1448
1449 p = get_proc_task(inode);
1450 if (!p)
1451 return -ESRCH;
1452 proc_sched_autogroup_show_task(p, m);
1453
1454 put_task_struct(p);
1455
1456 return 0;
1457}
1458
1459static ssize_t
1460sched_autogroup_write(struct file *file, const char __user *buf,
1461 size_t count, loff_t *offset)
1462{
Al Viro496ad9a2013-01-23 17:07:38 -05001463 struct inode *inode = file_inode(file);
Mike Galbraith5091faa2010-11-30 14:18:03 +01001464 struct task_struct *p;
1465 char buffer[PROC_NUMBUF];
Alexey Dobriyan0a8cb8e2011-05-26 16:25:50 -07001466 int nice;
Mike Galbraith5091faa2010-11-30 14:18:03 +01001467 int err;
1468
1469 memset(buffer, 0, sizeof(buffer));
1470 if (count > sizeof(buffer) - 1)
1471 count = sizeof(buffer) - 1;
1472 if (copy_from_user(buffer, buf, count))
1473 return -EFAULT;
1474
Alexey Dobriyan0a8cb8e2011-05-26 16:25:50 -07001475 err = kstrtoint(strstrip(buffer), 0, &nice);
1476 if (err < 0)
1477 return err;
Mike Galbraith5091faa2010-11-30 14:18:03 +01001478
1479 p = get_proc_task(inode);
1480 if (!p)
1481 return -ESRCH;
1482
Hiroshi Shimamoto2e5b5b32012-02-23 17:41:27 +09001483 err = proc_sched_autogroup_set_nice(p, nice);
Mike Galbraith5091faa2010-11-30 14:18:03 +01001484 if (err)
1485 count = err;
1486
1487 put_task_struct(p);
1488
1489 return count;
1490}
1491
1492static int sched_autogroup_open(struct inode *inode, struct file *filp)
1493{
1494 int ret;
1495
1496 ret = single_open(filp, sched_autogroup_show, NULL);
1497 if (!ret) {
1498 struct seq_file *m = filp->private_data;
1499
1500 m->private = inode;
1501 }
1502 return ret;
1503}
1504
1505static const struct file_operations proc_pid_sched_autogroup_operations = {
1506 .open = sched_autogroup_open,
1507 .read = seq_read,
1508 .write = sched_autogroup_write,
1509 .llseek = seq_lseek,
1510 .release = single_release,
1511};
1512
1513#endif /* CONFIG_SCHED_AUTOGROUP */
1514
john stultz4614a696b2009-12-14 18:00:05 -08001515static ssize_t comm_write(struct file *file, const char __user *buf,
1516 size_t count, loff_t *offset)
1517{
Al Viro496ad9a2013-01-23 17:07:38 -05001518 struct inode *inode = file_inode(file);
john stultz4614a696b2009-12-14 18:00:05 -08001519 struct task_struct *p;
1520 char buffer[TASK_COMM_LEN];
David Rientjes830e0fc2013-04-30 15:28:18 -07001521 const size_t maxlen = sizeof(buffer) - 1;
john stultz4614a696b2009-12-14 18:00:05 -08001522
1523 memset(buffer, 0, sizeof(buffer));
David Rientjes830e0fc2013-04-30 15:28:18 -07001524 if (copy_from_user(buffer, buf, count > maxlen ? maxlen : count))
john stultz4614a696b2009-12-14 18:00:05 -08001525 return -EFAULT;
1526
1527 p = get_proc_task(inode);
1528 if (!p)
1529 return -ESRCH;
1530
1531 if (same_thread_group(current, p))
1532 set_task_comm(p, buffer);
1533 else
1534 count = -EINVAL;
1535
1536 put_task_struct(p);
1537
1538 return count;
1539}
1540
1541static int comm_show(struct seq_file *m, void *v)
1542{
1543 struct inode *inode = m->private;
1544 struct task_struct *p;
1545
1546 p = get_proc_task(inode);
1547 if (!p)
1548 return -ESRCH;
1549
Tejun Heo88b72b32018-05-18 08:47:13 -07001550 proc_task_name(m, p, false);
1551 seq_putc(m, '\n');
john stultz4614a696b2009-12-14 18:00:05 -08001552
1553 put_task_struct(p);
1554
1555 return 0;
1556}
1557
1558static int comm_open(struct inode *inode, struct file *filp)
1559{
Jovi Zhangc6a34052011-01-12 17:00:34 -08001560 return single_open(filp, comm_show, inode);
john stultz4614a696b2009-12-14 18:00:05 -08001561}
1562
1563static const struct file_operations proc_pid_set_comm_operations = {
1564 .open = comm_open,
1565 .read = seq_read,
1566 .write = comm_write,
1567 .llseek = seq_lseek,
1568 .release = single_release,
1569};
1570
Cyrill Gorcunov7773fbc2012-01-10 15:11:20 -08001571static int proc_exe_link(struct dentry *dentry, struct path *exe_path)
Matt Helsley925d1c42008-04-29 01:01:36 -07001572{
1573 struct task_struct *task;
Matt Helsley925d1c42008-04-29 01:01:36 -07001574 struct file *exe_file;
1575
David Howells2b0143b2015-03-17 22:25:59 +00001576 task = get_proc_task(d_inode(dentry));
Matt Helsley925d1c42008-04-29 01:01:36 -07001577 if (!task)
1578 return -ENOENT;
Mateusz Guzikcd81a9172016-08-23 16:20:38 +02001579 exe_file = get_task_exe_file(task);
Matt Helsley925d1c42008-04-29 01:01:36 -07001580 put_task_struct(task);
Matt Helsley925d1c42008-04-29 01:01:36 -07001581 if (exe_file) {
1582 *exe_path = exe_file->f_path;
1583 path_get(&exe_file->f_path);
1584 fput(exe_file);
1585 return 0;
1586 } else
1587 return -ENOENT;
1588}
1589
Al Viro6b255392015-11-17 10:20:54 -05001590static const char *proc_pid_get_link(struct dentry *dentry,
Al Virofceef392015-12-29 15:58:39 -05001591 struct inode *inode,
1592 struct delayed_call *done)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001593{
Christoph Hellwig408ef012012-06-18 10:47:03 -04001594 struct path path;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001595 int error = -EACCES;
1596
Al Viro6b255392015-11-17 10:20:54 -05001597 if (!dentry)
1598 return ERR_PTR(-ECHILD);
1599
Eric W. Biederman778c1142006-06-26 00:25:58 -07001600 /* Are we allowed to snoop on the tasks file descriptors? */
1601 if (!proc_fd_access_allowed(inode))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001602 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001603
Christoph Hellwig408ef012012-06-18 10:47:03 -04001604 error = PROC_I(inode)->op.proc_get_link(dentry, &path);
1605 if (error)
1606 goto out;
1607
Al Viro6e77137b2015-05-02 13:37:52 -04001608 nd_jump_link(&path);
Christoph Hellwig408ef012012-06-18 10:47:03 -04001609 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001610out:
Al Viro008b1502005-08-20 00:17:39 +01001611 return ERR_PTR(error);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001612}
1613
Jan Blunck3dcd25f2008-02-14 19:38:35 -08001614static int do_proc_readlink(struct path *path, char __user *buffer, int buflen)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001615{
Michal Hocko0ee931c2017-09-13 16:28:29 -07001616 char *tmp = (char *)__get_free_page(GFP_KERNEL);
Jan Blunck3dcd25f2008-02-14 19:38:35 -08001617 char *pathname;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001618 int len;
1619
1620 if (!tmp)
1621 return -ENOMEM;
Akinobu Mita0c28f282007-05-08 00:31:41 -07001622
Eric W. Biederman7b2a69b2010-12-05 15:51:21 -08001623 pathname = d_path(path, tmp, PAGE_SIZE);
Jan Blunck3dcd25f2008-02-14 19:38:35 -08001624 len = PTR_ERR(pathname);
1625 if (IS_ERR(pathname))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001626 goto out;
Jan Blunck3dcd25f2008-02-14 19:38:35 -08001627 len = tmp + PAGE_SIZE - 1 - pathname;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001628
1629 if (len > buflen)
1630 len = buflen;
Jan Blunck3dcd25f2008-02-14 19:38:35 -08001631 if (copy_to_user(buffer, pathname, len))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001632 len = -EFAULT;
1633 out:
1634 free_page((unsigned long)tmp);
1635 return len;
1636}
1637
1638static int proc_pid_readlink(struct dentry * dentry, char __user * buffer, int buflen)
1639{
1640 int error = -EACCES;
David Howells2b0143b2015-03-17 22:25:59 +00001641 struct inode *inode = d_inode(dentry);
Jan Blunck3dcd25f2008-02-14 19:38:35 -08001642 struct path path;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001643
Eric W. Biederman778c1142006-06-26 00:25:58 -07001644 /* Are we allowed to snoop on the tasks file descriptors? */
1645 if (!proc_fd_access_allowed(inode))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001646 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001647
Cyrill Gorcunov7773fbc2012-01-10 15:11:20 -08001648 error = PROC_I(inode)->op.proc_get_link(dentry, &path);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001649 if (error)
1650 goto out;
1651
Jan Blunck3dcd25f2008-02-14 19:38:35 -08001652 error = do_proc_readlink(&path, buffer, buflen);
1653 path_put(&path);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001654out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001655 return error;
1656}
1657
Cyrill Gorcunovfaf60af2012-08-23 14:43:24 +04001658const struct inode_operations proc_pid_link_inode_operations = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001659 .readlink = proc_pid_readlink,
Al Viro6b255392015-11-17 10:20:54 -05001660 .get_link = proc_pid_get_link,
Linus Torvalds6d76fa52006-07-15 12:26:45 -07001661 .setattr = proc_setattr,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001662};
1663
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001664
1665/* building an inode */
1666
Al Viroc6eb50d2017-09-30 14:45:42 -04001667void task_dump_owner(struct task_struct *task, umode_t mode,
Eric W. Biederman68eb94f2017-01-03 10:23:11 +13001668 kuid_t *ruid, kgid_t *rgid)
1669{
1670 /* Depending on the state of dumpable compute who should own a
1671 * proc file for a task.
1672 */
1673 const struct cred *cred;
1674 kuid_t uid;
1675 kgid_t gid;
1676
Alexey Dobriyan2e0ad552018-04-20 14:56:03 -07001677 if (unlikely(task->flags & PF_KTHREAD)) {
1678 *ruid = GLOBAL_ROOT_UID;
1679 *rgid = GLOBAL_ROOT_GID;
1680 return;
1681 }
1682
Eric W. Biederman68eb94f2017-01-03 10:23:11 +13001683 /* Default to the tasks effective ownership */
1684 rcu_read_lock();
1685 cred = __task_cred(task);
1686 uid = cred->euid;
1687 gid = cred->egid;
1688 rcu_read_unlock();
1689
1690 /*
1691 * Before the /proc/pid/status file was created the only way to read
1692 * the effective uid of a /process was to stat /proc/pid. Reading
1693 * /proc/pid/status is slow enough that procps and other packages
1694 * kept stating /proc/pid. To keep the rules in /proc simple I have
1695 * made this apply to all per process world readable and executable
1696 * directories.
1697 */
1698 if (mode != (S_IFDIR|S_IRUGO|S_IXUGO)) {
1699 struct mm_struct *mm;
1700 task_lock(task);
1701 mm = task->mm;
1702 /* Make non-dumpable tasks owned by some root */
1703 if (mm) {
1704 if (get_dumpable(mm) != SUID_DUMP_USER) {
1705 struct user_namespace *user_ns = mm->user_ns;
1706
1707 uid = make_kuid(user_ns, 0);
1708 if (!uid_valid(uid))
1709 uid = GLOBAL_ROOT_UID;
1710
1711 gid = make_kgid(user_ns, 0);
1712 if (!gid_valid(gid))
1713 gid = GLOBAL_ROOT_GID;
1714 }
1715 } else {
1716 uid = GLOBAL_ROOT_UID;
1717 gid = GLOBAL_ROOT_GID;
1718 }
1719 task_unlock(task);
1720 }
1721 *ruid = uid;
1722 *rgid = gid;
1723}
1724
Andreas Gruenbacherdb978da2016-11-10 22:18:28 +01001725struct inode *proc_pid_make_inode(struct super_block * sb,
1726 struct task_struct *task, umode_t mode)
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001727{
1728 struct inode * inode;
1729 struct proc_inode *ei;
1730
1731 /* We need a new inode */
1732
1733 inode = new_inode(sb);
1734 if (!inode)
1735 goto out;
1736
1737 /* Common stuff */
1738 ei = PROC_I(inode);
Andreas Gruenbacherdb978da2016-11-10 22:18:28 +01001739 inode->i_mode = mode;
Christoph Hellwig85fe4022010-10-23 11:19:54 -04001740 inode->i_ino = get_next_ino();
Deepa Dinamani078cd822016-09-14 07:48:04 -07001741 inode->i_mtime = inode->i_atime = inode->i_ctime = current_time(inode);
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001742 inode->i_op = &proc_def_inode_operations;
1743
1744 /*
1745 * grab the reference to task.
1746 */
Oleg Nesterov1a657f782006-10-02 02:18:59 -07001747 ei->pid = get_task_pid(task, PIDTYPE_PID);
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001748 if (!ei->pid)
1749 goto out_unlock;
1750
Eric W. Biederman68eb94f2017-01-03 10:23:11 +13001751 task_dump_owner(task, 0, &inode->i_uid, &inode->i_gid);
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001752 security_task_to_inode(task, inode);
1753
1754out:
1755 return inode;
1756
1757out_unlock:
1758 iput(inode);
1759 return NULL;
1760}
1761
David Howellsa528d352017-01-31 16:46:22 +00001762int pid_getattr(const struct path *path, struct kstat *stat,
1763 u32 request_mask, unsigned int query_flags)
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001764{
David Howellsa528d352017-01-31 16:46:22 +00001765 struct inode *inode = d_inode(path->dentry);
Christoph Hellwig76f668b2018-05-16 07:19:01 +02001766 struct pid_namespace *pid = proc_pid_ns(inode);
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001767 struct task_struct *task;
David Howellsc69e8d92008-11-14 10:39:19 +11001768
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001769 generic_fillattr(inode, stat);
1770
1771 rcu_read_lock();
Eric W. Biedermandcb0f222012-02-09 08:48:21 -08001772 stat->uid = GLOBAL_ROOT_UID;
1773 stat->gid = GLOBAL_ROOT_GID;
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001774 task = pid_task(proc_pid(inode), PIDTYPE_PID);
1775 if (task) {
Lafcadio Wluiki796f5712017-02-24 15:00:23 -08001776 if (!has_pid_permissions(pid, task, HIDEPID_INVISIBLE)) {
Vasiliy Kulikov04996802012-01-10 15:11:31 -08001777 rcu_read_unlock();
1778 /*
1779 * This doesn't prevent learning whether PID exists,
1780 * it only makes getattr() consistent with readdir().
1781 */
1782 return -ENOENT;
1783 }
Eric W. Biederman68eb94f2017-01-03 10:23:11 +13001784 task_dump_owner(task, inode->i_mode, &stat->uid, &stat->gid);
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001785 }
1786 rcu_read_unlock();
1787 return 0;
1788}
1789
1790/* dentry stuff */
1791
1792/*
Al Viro1bbc5512018-05-02 21:26:16 -04001793 * Set <pid>/... inode ownership (can change due to setuid(), etc.)
1794 */
1795void pid_update_inode(struct task_struct *task, struct inode *inode)
1796{
1797 task_dump_owner(task, inode->i_mode, &inode->i_uid, &inode->i_gid);
1798
1799 inode->i_mode &= ~(S_ISUID | S_ISGID);
1800 security_task_to_inode(task, inode);
1801}
1802
1803/*
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001804 * Rewrite the inode's ownerships here because the owning task may have
1805 * performed a setuid(), etc.
1806 *
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001807 */
Al Viro1bbc5512018-05-02 21:26:16 -04001808static int pid_revalidate(struct dentry *dentry, unsigned int flags)
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001809{
Nick Piggin34286d62011-01-07 17:49:57 +11001810 struct inode *inode;
1811 struct task_struct *task;
David Howellsc69e8d92008-11-14 10:39:19 +11001812
Al Viro0b728e12012-06-10 16:03:43 -04001813 if (flags & LOOKUP_RCU)
Nick Piggin34286d62011-01-07 17:49:57 +11001814 return -ECHILD;
1815
David Howells2b0143b2015-03-17 22:25:59 +00001816 inode = d_inode(dentry);
Nick Piggin34286d62011-01-07 17:49:57 +11001817 task = get_proc_task(inode);
1818
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001819 if (task) {
Al Viro1bbc5512018-05-02 21:26:16 -04001820 pid_update_inode(task, inode);
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001821 put_task_struct(task);
1822 return 1;
1823 }
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001824 return 0;
1825}
1826
Oleg Nesterovd855a4b2014-01-23 15:55:39 -08001827static inline bool proc_inode_is_dead(struct inode *inode)
1828{
1829 return !proc_pid(inode)->tasks[PIDTYPE_PID].first;
1830}
1831
David Howells1dd704b2013-04-12 01:08:50 +01001832int pid_delete_dentry(const struct dentry *dentry)
1833{
1834 /* Is the task we represent dead?
1835 * If so, then don't put the dentry on the lru list,
1836 * kill it immediately.
1837 */
David Howells2b0143b2015-03-17 22:25:59 +00001838 return proc_inode_is_dead(d_inode(dentry));
David Howells1dd704b2013-04-12 01:08:50 +01001839}
1840
Eric W. Biederman6b4e3062010-03-07 16:41:34 -08001841const struct dentry_operations pid_dentry_operations =
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001842{
1843 .d_revalidate = pid_revalidate,
1844 .d_delete = pid_delete_dentry,
1845};
1846
1847/* Lookups */
1848
Eric W. Biederman1c0d04c2006-10-02 02:18:57 -07001849/*
1850 * Fill a directory entry.
1851 *
1852 * If possible create the dcache entry and derive our inode number and
1853 * file type from dcache entry.
1854 *
1855 * Since all of the proc inode numbers are dynamically generated, the inode
1856 * numbers do not exist until the inode is cache. This means creating the
1857 * the dcache entry in readdir is necessary to keep the inode numbers
1858 * reported by readdir in sync with the inode numbers reported
1859 * by stat.
1860 */
Al Virof0c3b502013-05-16 12:07:31 -04001861bool proc_fill_cache(struct file *file, struct dir_context *ctx,
Eric W. Biederman6b4e3062010-03-07 16:41:34 -08001862 const char *name, int len,
Eric Dumazetc5141e62007-05-08 00:26:15 -07001863 instantiate_t instantiate, struct task_struct *task, const void *ptr)
Eric W. Biederman61a28782006-10-02 02:18:49 -07001864{
Al Virof0c3b502013-05-16 12:07:31 -04001865 struct dentry *child, *dir = file->f_path.dentry;
Al Viro1df98b82013-06-15 11:33:10 +04001866 struct qstr qname = QSTR_INIT(name, len);
Eric W. Biederman61a28782006-10-02 02:18:49 -07001867 struct inode *inode;
Al Viro0168b9e2018-05-03 09:21:05 -04001868 unsigned type = DT_UNKNOWN;
1869 ino_t ino = 1;
Eric W. Biederman61a28782006-10-02 02:18:49 -07001870
Al Viro1df98b82013-06-15 11:33:10 +04001871 child = d_hash_and_lookup(dir, &qname);
Eric W. Biederman61a28782006-10-02 02:18:49 -07001872 if (!child) {
Al Viro37817642016-04-20 16:31:31 -04001873 DECLARE_WAIT_QUEUE_HEAD_ONSTACK(wq);
1874 child = d_alloc_parallel(dir, &qname, &wq);
1875 if (IS_ERR(child))
Al Viro1df98b82013-06-15 11:33:10 +04001876 goto end_instantiate;
Al Viro37817642016-04-20 16:31:31 -04001877 if (d_in_lookup(child)) {
Al Viro0168b9e2018-05-03 09:21:05 -04001878 struct dentry *res;
1879 res = instantiate(child, task, ptr);
Al Viro37817642016-04-20 16:31:31 -04001880 d_lookup_done(child);
Al Viro0168b9e2018-05-03 09:21:05 -04001881 if (IS_ERR(res))
Al Viro37817642016-04-20 16:31:31 -04001882 goto end_instantiate;
Al Viro0168b9e2018-05-03 09:21:05 -04001883 if (unlikely(res)) {
1884 dput(child);
1885 child = res;
Al Viro37817642016-04-20 16:31:31 -04001886 }
Eric W. Biederman61a28782006-10-02 02:18:49 -07001887 }
1888 }
David Howells2b0143b2015-03-17 22:25:59 +00001889 inode = d_inode(child);
Al Viro147ce692013-06-15 10:26:35 +04001890 ino = inode->i_ino;
1891 type = inode->i_mode >> 12;
Al Viro0168b9e2018-05-03 09:21:05 -04001892end_instantiate:
Eric W. Biederman61a28782006-10-02 02:18:49 -07001893 dput(child);
Al Virof0c3b502013-05-16 12:07:31 -04001894 return dir_emit(ctx, name, len, ino, type);
Eric W. Biederman61a28782006-10-02 02:18:49 -07001895}
1896
Pavel Emelyanov640708a2012-01-10 15:11:23 -08001897/*
1898 * dname_to_vma_addr - maps a dentry name into two unsigned longs
1899 * which represent vma start and end addresses.
1900 */
1901static int dname_to_vma_addr(struct dentry *dentry,
1902 unsigned long *start, unsigned long *end)
1903{
Alexey Dobriyanac7f1062018-02-06 15:36:59 -08001904 const char *str = dentry->d_name.name;
1905 unsigned long long sval, eval;
1906 unsigned int len;
1907
Alexey Dobriyan35318db2018-04-10 16:41:14 -07001908 if (str[0] == '0' && str[1] != '-')
1909 return -EINVAL;
Alexey Dobriyanac7f1062018-02-06 15:36:59 -08001910 len = _parse_integer(str, 16, &sval);
1911 if (len & KSTRTOX_OVERFLOW)
Pavel Emelyanov640708a2012-01-10 15:11:23 -08001912 return -EINVAL;
Alexey Dobriyanac7f1062018-02-06 15:36:59 -08001913 if (sval != (unsigned long)sval)
1914 return -EINVAL;
1915 str += len;
1916
1917 if (*str != '-')
1918 return -EINVAL;
1919 str++;
1920
Alexey Dobriyan35318db2018-04-10 16:41:14 -07001921 if (str[0] == '0' && str[1])
1922 return -EINVAL;
Alexey Dobriyanac7f1062018-02-06 15:36:59 -08001923 len = _parse_integer(str, 16, &eval);
1924 if (len & KSTRTOX_OVERFLOW)
1925 return -EINVAL;
1926 if (eval != (unsigned long)eval)
1927 return -EINVAL;
1928 str += len;
1929
1930 if (*str != '\0')
1931 return -EINVAL;
1932
1933 *start = sval;
1934 *end = eval;
Pavel Emelyanov640708a2012-01-10 15:11:23 -08001935
1936 return 0;
1937}
1938
Al Viro0b728e12012-06-10 16:03:43 -04001939static int map_files_d_revalidate(struct dentry *dentry, unsigned int flags)
Pavel Emelyanov640708a2012-01-10 15:11:23 -08001940{
1941 unsigned long vm_start, vm_end;
1942 bool exact_vma_exists = false;
1943 struct mm_struct *mm = NULL;
1944 struct task_struct *task;
Pavel Emelyanov640708a2012-01-10 15:11:23 -08001945 struct inode *inode;
1946 int status = 0;
1947
Al Viro0b728e12012-06-10 16:03:43 -04001948 if (flags & LOOKUP_RCU)
Pavel Emelyanov640708a2012-01-10 15:11:23 -08001949 return -ECHILD;
1950
David Howells2b0143b2015-03-17 22:25:59 +00001951 inode = d_inode(dentry);
Pavel Emelyanov640708a2012-01-10 15:11:23 -08001952 task = get_proc_task(inode);
1953 if (!task)
1954 goto out_notask;
1955
Jann Horncaaee622016-01-20 15:00:04 -08001956 mm = mm_access(task, PTRACE_MODE_READ_FSCREDS);
Cong Wang2344bec2012-05-31 16:26:18 -07001957 if (IS_ERR_OR_NULL(mm))
Pavel Emelyanov640708a2012-01-10 15:11:23 -08001958 goto out;
1959
1960 if (!dname_to_vma_addr(dentry, &vm_start, &vm_end)) {
1961 down_read(&mm->mmap_sem);
1962 exact_vma_exists = !!find_exact_vma(mm, vm_start, vm_end);
1963 up_read(&mm->mmap_sem);
1964 }
1965
1966 mmput(mm);
1967
1968 if (exact_vma_exists) {
Eric W. Biederman68eb94f2017-01-03 10:23:11 +13001969 task_dump_owner(task, 0, &inode->i_uid, &inode->i_gid);
1970
Pavel Emelyanov640708a2012-01-10 15:11:23 -08001971 security_task_to_inode(task, inode);
1972 status = 1;
1973 }
1974
1975out:
1976 put_task_struct(task);
1977
1978out_notask:
Pavel Emelyanov640708a2012-01-10 15:11:23 -08001979 return status;
1980}
1981
1982static const struct dentry_operations tid_map_files_dentry_operations = {
1983 .d_revalidate = map_files_d_revalidate,
1984 .d_delete = pid_delete_dentry,
1985};
1986
Al Viro6b255392015-11-17 10:20:54 -05001987static int map_files_get_link(struct dentry *dentry, struct path *path)
Pavel Emelyanov640708a2012-01-10 15:11:23 -08001988{
1989 unsigned long vm_start, vm_end;
1990 struct vm_area_struct *vma;
1991 struct task_struct *task;
1992 struct mm_struct *mm;
1993 int rc;
1994
1995 rc = -ENOENT;
David Howells2b0143b2015-03-17 22:25:59 +00001996 task = get_proc_task(d_inode(dentry));
Pavel Emelyanov640708a2012-01-10 15:11:23 -08001997 if (!task)
1998 goto out;
1999
2000 mm = get_task_mm(task);
2001 put_task_struct(task);
2002 if (!mm)
2003 goto out;
2004
2005 rc = dname_to_vma_addr(dentry, &vm_start, &vm_end);
2006 if (rc)
2007 goto out_mmput;
2008
Artem Fetishev70335ab2014-03-10 15:49:45 -07002009 rc = -ENOENT;
Pavel Emelyanov640708a2012-01-10 15:11:23 -08002010 down_read(&mm->mmap_sem);
2011 vma = find_exact_vma(mm, vm_start, vm_end);
2012 if (vma && vma->vm_file) {
2013 *path = vma->vm_file->f_path;
2014 path_get(path);
2015 rc = 0;
2016 }
2017 up_read(&mm->mmap_sem);
2018
2019out_mmput:
2020 mmput(mm);
2021out:
2022 return rc;
2023}
2024
2025struct map_files_info {
Alexey Dobriyan20d28cd2018-02-06 15:37:06 -08002026 unsigned long start;
2027 unsigned long end;
Al Viro7b540d02012-08-27 14:55:26 -04002028 fmode_t mode;
Pavel Emelyanov640708a2012-01-10 15:11:23 -08002029};
2030
Calvin Owensbdb4d102015-09-09 15:35:54 -07002031/*
2032 * Only allow CAP_SYS_ADMIN to follow the links, due to concerns about how the
2033 * symlinks may be used to bypass permissions on ancestor directories in the
2034 * path to the file in question.
2035 */
2036static const char *
Al Viro6b255392015-11-17 10:20:54 -05002037proc_map_files_get_link(struct dentry *dentry,
Al Virofceef392015-12-29 15:58:39 -05002038 struct inode *inode,
2039 struct delayed_call *done)
Calvin Owensbdb4d102015-09-09 15:35:54 -07002040{
2041 if (!capable(CAP_SYS_ADMIN))
2042 return ERR_PTR(-EPERM);
2043
Al Virofceef392015-12-29 15:58:39 -05002044 return proc_pid_get_link(dentry, inode, done);
Calvin Owensbdb4d102015-09-09 15:35:54 -07002045}
2046
2047/*
Al Viro6b255392015-11-17 10:20:54 -05002048 * Identical to proc_pid_link_inode_operations except for get_link()
Calvin Owensbdb4d102015-09-09 15:35:54 -07002049 */
2050static const struct inode_operations proc_map_files_link_inode_operations = {
2051 .readlink = proc_pid_readlink,
Al Viro6b255392015-11-17 10:20:54 -05002052 .get_link = proc_map_files_get_link,
Calvin Owensbdb4d102015-09-09 15:35:54 -07002053 .setattr = proc_setattr,
2054};
2055
Al Viro0168b9e2018-05-03 09:21:05 -04002056static struct dentry *
2057proc_map_files_instantiate(struct dentry *dentry,
Pavel Emelyanov640708a2012-01-10 15:11:23 -08002058 struct task_struct *task, const void *ptr)
2059{
Al Viro7b540d02012-08-27 14:55:26 -04002060 fmode_t mode = (fmode_t)(unsigned long)ptr;
Pavel Emelyanov640708a2012-01-10 15:11:23 -08002061 struct proc_inode *ei;
2062 struct inode *inode;
2063
Al Viro0168b9e2018-05-03 09:21:05 -04002064 inode = proc_pid_make_inode(dentry->d_sb, task, S_IFLNK |
Andreas Gruenbacherdb978da2016-11-10 22:18:28 +01002065 ((mode & FMODE_READ ) ? S_IRUSR : 0) |
2066 ((mode & FMODE_WRITE) ? S_IWUSR : 0));
Pavel Emelyanov640708a2012-01-10 15:11:23 -08002067 if (!inode)
Al Viro0168b9e2018-05-03 09:21:05 -04002068 return ERR_PTR(-ENOENT);
Pavel Emelyanov640708a2012-01-10 15:11:23 -08002069
2070 ei = PROC_I(inode);
Al Viro6b255392015-11-17 10:20:54 -05002071 ei->op.proc_get_link = map_files_get_link;
Pavel Emelyanov640708a2012-01-10 15:11:23 -08002072
Calvin Owensbdb4d102015-09-09 15:35:54 -07002073 inode->i_op = &proc_map_files_link_inode_operations;
Pavel Emelyanov640708a2012-01-10 15:11:23 -08002074 inode->i_size = 64;
Pavel Emelyanov640708a2012-01-10 15:11:23 -08002075
2076 d_set_d_op(dentry, &tid_map_files_dentry_operations);
Al Viro0168b9e2018-05-03 09:21:05 -04002077 return d_splice_alias(inode, dentry);
Pavel Emelyanov640708a2012-01-10 15:11:23 -08002078}
2079
2080static struct dentry *proc_map_files_lookup(struct inode *dir,
Al Viro00cd8dd2012-06-10 17:13:09 -04002081 struct dentry *dentry, unsigned int flags)
Pavel Emelyanov640708a2012-01-10 15:11:23 -08002082{
2083 unsigned long vm_start, vm_end;
2084 struct vm_area_struct *vma;
2085 struct task_struct *task;
Al Viro0168b9e2018-05-03 09:21:05 -04002086 struct dentry *result;
Pavel Emelyanov640708a2012-01-10 15:11:23 -08002087 struct mm_struct *mm;
2088
Al Viro0168b9e2018-05-03 09:21:05 -04002089 result = ERR_PTR(-ENOENT);
Pavel Emelyanov640708a2012-01-10 15:11:23 -08002090 task = get_proc_task(dir);
2091 if (!task)
2092 goto out;
2093
Al Viro0168b9e2018-05-03 09:21:05 -04002094 result = ERR_PTR(-EACCES);
Jann Horncaaee622016-01-20 15:00:04 -08002095 if (!ptrace_may_access(task, PTRACE_MODE_READ_FSCREDS))
Pavel Emelyanov640708a2012-01-10 15:11:23 -08002096 goto out_put_task;
2097
Al Viro0168b9e2018-05-03 09:21:05 -04002098 result = ERR_PTR(-ENOENT);
Pavel Emelyanov640708a2012-01-10 15:11:23 -08002099 if (dname_to_vma_addr(dentry, &vm_start, &vm_end))
Cyrill Gorcunoveb94cd92012-05-17 17:03:25 -07002100 goto out_put_task;
Pavel Emelyanov640708a2012-01-10 15:11:23 -08002101
2102 mm = get_task_mm(task);
2103 if (!mm)
Cyrill Gorcunoveb94cd92012-05-17 17:03:25 -07002104 goto out_put_task;
Pavel Emelyanov640708a2012-01-10 15:11:23 -08002105
2106 down_read(&mm->mmap_sem);
2107 vma = find_exact_vma(mm, vm_start, vm_end);
2108 if (!vma)
2109 goto out_no_vma;
2110
Stanislav Kinsbursky05f56482012-11-26 16:29:42 -08002111 if (vma->vm_file)
Al Viro0168b9e2018-05-03 09:21:05 -04002112 result = proc_map_files_instantiate(dentry, task,
Stanislav Kinsbursky05f56482012-11-26 16:29:42 -08002113 (void *)(unsigned long)vma->vm_file->f_mode);
Pavel Emelyanov640708a2012-01-10 15:11:23 -08002114
2115out_no_vma:
2116 up_read(&mm->mmap_sem);
2117 mmput(mm);
Pavel Emelyanov640708a2012-01-10 15:11:23 -08002118out_put_task:
2119 put_task_struct(task);
2120out:
Al Viro0168b9e2018-05-03 09:21:05 -04002121 return result;
Pavel Emelyanov640708a2012-01-10 15:11:23 -08002122}
2123
2124static const struct inode_operations proc_map_files_inode_operations = {
2125 .lookup = proc_map_files_lookup,
2126 .permission = proc_fd_permission,
2127 .setattr = proc_setattr,
2128};
2129
2130static int
Al Virof0c3b502013-05-16 12:07:31 -04002131proc_map_files_readdir(struct file *file, struct dir_context *ctx)
Pavel Emelyanov640708a2012-01-10 15:11:23 -08002132{
Pavel Emelyanov640708a2012-01-10 15:11:23 -08002133 struct vm_area_struct *vma;
2134 struct task_struct *task;
2135 struct mm_struct *mm;
Al Virof0c3b502013-05-16 12:07:31 -04002136 unsigned long nr_files, pos, i;
2137 struct flex_array *fa = NULL;
2138 struct map_files_info info;
2139 struct map_files_info *p;
Pavel Emelyanov640708a2012-01-10 15:11:23 -08002140 int ret;
2141
Pavel Emelyanov640708a2012-01-10 15:11:23 -08002142 ret = -ENOENT;
Al Virof0c3b502013-05-16 12:07:31 -04002143 task = get_proc_task(file_inode(file));
Pavel Emelyanov640708a2012-01-10 15:11:23 -08002144 if (!task)
2145 goto out;
2146
2147 ret = -EACCES;
Jann Horncaaee622016-01-20 15:00:04 -08002148 if (!ptrace_may_access(task, PTRACE_MODE_READ_FSCREDS))
Pavel Emelyanov640708a2012-01-10 15:11:23 -08002149 goto out_put_task;
2150
2151 ret = 0;
Al Virof0c3b502013-05-16 12:07:31 -04002152 if (!dir_emit_dots(file, ctx))
2153 goto out_put_task;
Pavel Emelyanov640708a2012-01-10 15:11:23 -08002154
Al Virof0c3b502013-05-16 12:07:31 -04002155 mm = get_task_mm(task);
2156 if (!mm)
2157 goto out_put_task;
2158 down_read(&mm->mmap_sem);
Pavel Emelyanov640708a2012-01-10 15:11:23 -08002159
Al Virof0c3b502013-05-16 12:07:31 -04002160 nr_files = 0;
Pavel Emelyanov640708a2012-01-10 15:11:23 -08002161
Al Virof0c3b502013-05-16 12:07:31 -04002162 /*
2163 * We need two passes here:
2164 *
2165 * 1) Collect vmas of mapped files with mmap_sem taken
2166 * 2) Release mmap_sem and instantiate entries
2167 *
2168 * otherwise we get lockdep complained, since filldir()
2169 * routine might require mmap_sem taken in might_fault().
2170 */
Pavel Emelyanov640708a2012-01-10 15:11:23 -08002171
Al Virof0c3b502013-05-16 12:07:31 -04002172 for (vma = mm->mmap, pos = 2; vma; vma = vma->vm_next) {
2173 if (vma->vm_file && ++pos > ctx->pos)
2174 nr_files++;
Pavel Emelyanov640708a2012-01-10 15:11:23 -08002175 }
Al Virof0c3b502013-05-16 12:07:31 -04002176
2177 if (nr_files) {
2178 fa = flex_array_alloc(sizeof(info), nr_files,
2179 GFP_KERNEL);
2180 if (!fa || flex_array_prealloc(fa, 0, nr_files,
2181 GFP_KERNEL)) {
2182 ret = -ENOMEM;
2183 if (fa)
2184 flex_array_free(fa);
2185 up_read(&mm->mmap_sem);
2186 mmput(mm);
2187 goto out_put_task;
2188 }
2189 for (i = 0, vma = mm->mmap, pos = 2; vma;
2190 vma = vma->vm_next) {
2191 if (!vma->vm_file)
2192 continue;
2193 if (++pos <= ctx->pos)
2194 continue;
2195
Alexey Dobriyan20d28cd2018-02-06 15:37:06 -08002196 info.start = vma->vm_start;
2197 info.end = vma->vm_end;
Al Virof0c3b502013-05-16 12:07:31 -04002198 info.mode = vma->vm_file->f_mode;
Al Virof0c3b502013-05-16 12:07:31 -04002199 if (flex_array_put(fa, i++, &info, GFP_KERNEL))
2200 BUG();
2201 }
Pavel Emelyanov640708a2012-01-10 15:11:23 -08002202 }
Al Virof0c3b502013-05-16 12:07:31 -04002203 up_read(&mm->mmap_sem);
Alexey Dobriyanfe079a52018-04-10 16:32:05 -07002204 mmput(mm);
Al Virof0c3b502013-05-16 12:07:31 -04002205
2206 for (i = 0; i < nr_files; i++) {
Alexey Dobriyan20d28cd2018-02-06 15:37:06 -08002207 char buf[4 * sizeof(long) + 2]; /* max: %lx-%lx\0 */
2208 unsigned int len;
2209
Al Virof0c3b502013-05-16 12:07:31 -04002210 p = flex_array_get(fa, i);
Alexey Dobriyan20d28cd2018-02-06 15:37:06 -08002211 len = snprintf(buf, sizeof(buf), "%lx-%lx", p->start, p->end);
Al Virof0c3b502013-05-16 12:07:31 -04002212 if (!proc_fill_cache(file, ctx,
Alexey Dobriyan20d28cd2018-02-06 15:37:06 -08002213 buf, len,
Al Virof0c3b502013-05-16 12:07:31 -04002214 proc_map_files_instantiate,
2215 task,
2216 (void *)(unsigned long)p->mode))
2217 break;
2218 ctx->pos++;
2219 }
2220 if (fa)
2221 flex_array_free(fa);
Pavel Emelyanov640708a2012-01-10 15:11:23 -08002222
Pavel Emelyanov640708a2012-01-10 15:11:23 -08002223out_put_task:
2224 put_task_struct(task);
2225out:
2226 return ret;
2227}
2228
2229static const struct file_operations proc_map_files_operations = {
2230 .read = generic_read_dir,
Al Virof50752e2016-04-20 17:13:54 -04002231 .iterate_shared = proc_map_files_readdir,
2232 .llseek = generic_file_llseek,
Pavel Emelyanov640708a2012-01-10 15:11:23 -08002233};
2234
Nicolas Pitreb18b6a92017-01-21 00:09:08 -05002235#if defined(CONFIG_CHECKPOINT_RESTORE) && defined(CONFIG_POSIX_TIMERS)
Pavel Emelyanov48f6a7a2013-03-11 13:12:45 +04002236struct timers_private {
2237 struct pid *pid;
2238 struct task_struct *task;
2239 struct sighand_struct *sighand;
Pavel Emelyanov57b80152013-03-11 13:13:08 +04002240 struct pid_namespace *ns;
Pavel Emelyanov48f6a7a2013-03-11 13:12:45 +04002241 unsigned long flags;
2242};
2243
2244static void *timers_start(struct seq_file *m, loff_t *pos)
2245{
2246 struct timers_private *tp = m->private;
2247
2248 tp->task = get_pid_task(tp->pid, PIDTYPE_PID);
2249 if (!tp->task)
2250 return ERR_PTR(-ESRCH);
2251
2252 tp->sighand = lock_task_sighand(tp->task, &tp->flags);
2253 if (!tp->sighand)
2254 return ERR_PTR(-ESRCH);
2255
2256 return seq_list_start(&tp->task->signal->posix_timers, *pos);
2257}
2258
2259static void *timers_next(struct seq_file *m, void *v, loff_t *pos)
2260{
2261 struct timers_private *tp = m->private;
2262 return seq_list_next(v, &tp->task->signal->posix_timers, pos);
2263}
2264
2265static void timers_stop(struct seq_file *m, void *v)
2266{
2267 struct timers_private *tp = m->private;
2268
2269 if (tp->sighand) {
2270 unlock_task_sighand(tp->task, &tp->flags);
2271 tp->sighand = NULL;
2272 }
2273
2274 if (tp->task) {
2275 put_task_struct(tp->task);
2276 tp->task = NULL;
2277 }
2278}
2279
2280static int show_timer(struct seq_file *m, void *v)
2281{
2282 struct k_itimer *timer;
Pavel Emelyanov57b80152013-03-11 13:13:08 +04002283 struct timers_private *tp = m->private;
2284 int notify;
Alexey Dobriyancedbcca2014-08-08 14:21:33 -07002285 static const char * const nstr[] = {
Pavel Emelyanov57b80152013-03-11 13:13:08 +04002286 [SIGEV_SIGNAL] = "signal",
2287 [SIGEV_NONE] = "none",
2288 [SIGEV_THREAD] = "thread",
2289 };
Pavel Emelyanov48f6a7a2013-03-11 13:12:45 +04002290
2291 timer = list_entry((struct list_head *)v, struct k_itimer, list);
Pavel Emelyanov57b80152013-03-11 13:13:08 +04002292 notify = timer->it_sigev_notify;
2293
Pavel Emelyanov48f6a7a2013-03-11 13:12:45 +04002294 seq_printf(m, "ID: %d\n", timer->it_id);
Linus Torvaldsba3edf1f2017-12-06 18:23:27 -08002295 seq_printf(m, "signal: %d/%px\n",
Joe Perches25ce3192015-04-15 16:18:17 -07002296 timer->sigq->info.si_signo,
2297 timer->sigq->info.si_value.sival_ptr);
Pavel Emelyanov57b80152013-03-11 13:13:08 +04002298 seq_printf(m, "notify: %s/%s.%d\n",
Joe Perches25ce3192015-04-15 16:18:17 -07002299 nstr[notify & ~SIGEV_THREAD_ID],
2300 (notify & SIGEV_THREAD_ID) ? "tid" : "pid",
2301 pid_nr_ns(timer->it_pid, tp->ns));
Pavel Tikhomirov15ef0292013-05-17 02:12:03 +04002302 seq_printf(m, "ClockID: %d\n", timer->it_clock);
Pavel Emelyanov48f6a7a2013-03-11 13:12:45 +04002303
2304 return 0;
2305}
2306
2307static const struct seq_operations proc_timers_seq_ops = {
2308 .start = timers_start,
2309 .next = timers_next,
2310 .stop = timers_stop,
2311 .show = show_timer,
2312};
2313
2314static int proc_timers_open(struct inode *inode, struct file *file)
2315{
2316 struct timers_private *tp;
2317
2318 tp = __seq_open_private(file, &proc_timers_seq_ops,
2319 sizeof(struct timers_private));
2320 if (!tp)
2321 return -ENOMEM;
2322
2323 tp->pid = proc_pid(inode);
Christoph Hellwig76f668b2018-05-16 07:19:01 +02002324 tp->ns = proc_pid_ns(inode);
Pavel Emelyanov48f6a7a2013-03-11 13:12:45 +04002325 return 0;
2326}
2327
2328static const struct file_operations proc_timers_operations = {
2329 .open = proc_timers_open,
2330 .read = seq_read,
2331 .llseek = seq_lseek,
2332 .release = seq_release_private,
2333};
Eric Engestromb5946be2016-03-17 14:20:57 -07002334#endif
Pavel Emelyanov640708a2012-01-10 15:11:23 -08002335
John Stultz5de23d42016-03-17 14:20:54 -07002336static ssize_t timerslack_ns_write(struct file *file, const char __user *buf,
2337 size_t count, loff_t *offset)
2338{
2339 struct inode *inode = file_inode(file);
2340 struct task_struct *p;
2341 u64 slack_ns;
2342 int err;
2343
2344 err = kstrtoull_from_user(buf, count, 10, &slack_ns);
2345 if (err < 0)
2346 return err;
2347
2348 p = get_proc_task(inode);
2349 if (!p)
2350 return -ESRCH;
2351
John Stultz4b2bd5f2016-10-07 17:02:33 -07002352 if (p != current) {
2353 if (!capable(CAP_SYS_NICE)) {
2354 count = -EPERM;
2355 goto out;
2356 }
John Stultz5de23d42016-03-17 14:20:54 -07002357
John Stultz4b2bd5f2016-10-07 17:02:33 -07002358 err = security_task_setscheduler(p);
2359 if (err) {
2360 count = err;
2361 goto out;
2362 }
John Stultz904763e2016-10-07 17:02:29 -07002363 }
2364
John Stultz7abbaf92016-10-07 17:02:26 -07002365 task_lock(p);
2366 if (slack_ns == 0)
2367 p->timer_slack_ns = p->default_timer_slack_ns;
2368 else
2369 p->timer_slack_ns = slack_ns;
2370 task_unlock(p);
2371
2372out:
John Stultz5de23d42016-03-17 14:20:54 -07002373 put_task_struct(p);
2374
2375 return count;
2376}
2377
2378static int timerslack_ns_show(struct seq_file *m, void *v)
2379{
2380 struct inode *inode = m->private;
2381 struct task_struct *p;
John Stultz7abbaf92016-10-07 17:02:26 -07002382 int err = 0;
John Stultz5de23d42016-03-17 14:20:54 -07002383
2384 p = get_proc_task(inode);
2385 if (!p)
2386 return -ESRCH;
2387
John Stultz4b2bd5f2016-10-07 17:02:33 -07002388 if (p != current) {
John Stultz5de23d42016-03-17 14:20:54 -07002389
John Stultz4b2bd5f2016-10-07 17:02:33 -07002390 if (!capable(CAP_SYS_NICE)) {
2391 err = -EPERM;
2392 goto out;
2393 }
2394 err = security_task_getscheduler(p);
2395 if (err)
2396 goto out;
2397 }
John Stultz904763e2016-10-07 17:02:29 -07002398
John Stultz7abbaf92016-10-07 17:02:26 -07002399 task_lock(p);
2400 seq_printf(m, "%llu\n", p->timer_slack_ns);
2401 task_unlock(p);
2402
2403out:
John Stultz5de23d42016-03-17 14:20:54 -07002404 put_task_struct(p);
2405
2406 return err;
2407}
2408
2409static int timerslack_ns_open(struct inode *inode, struct file *filp)
2410{
2411 return single_open(filp, timerslack_ns_show, inode);
2412}
2413
2414static const struct file_operations proc_pid_set_timerslack_ns_operations = {
2415 .open = timerslack_ns_open,
2416 .read = seq_read,
2417 .write = timerslack_ns_write,
2418 .llseek = seq_lseek,
2419 .release = single_release,
2420};
2421
Al Viro0168b9e2018-05-03 09:21:05 -04002422static struct dentry *proc_pident_instantiate(struct dentry *dentry,
2423 struct task_struct *task, const void *ptr)
Eric W. Biederman444ceed2006-10-02 02:18:49 -07002424{
Eric Dumazetc5141e62007-05-08 00:26:15 -07002425 const struct pid_entry *p = ptr;
Eric W. Biederman444ceed2006-10-02 02:18:49 -07002426 struct inode *inode;
2427 struct proc_inode *ei;
Eric W. Biederman444ceed2006-10-02 02:18:49 -07002428
Al Viro0168b9e2018-05-03 09:21:05 -04002429 inode = proc_pid_make_inode(dentry->d_sb, task, p->mode);
Eric W. Biederman444ceed2006-10-02 02:18:49 -07002430 if (!inode)
Al Viro0168b9e2018-05-03 09:21:05 -04002431 return ERR_PTR(-ENOENT);
Eric W. Biederman444ceed2006-10-02 02:18:49 -07002432
2433 ei = PROC_I(inode);
Eric W. Biederman444ceed2006-10-02 02:18:49 -07002434 if (S_ISDIR(inode->i_mode))
Miklos Szeredibfe86842011-10-28 14:13:29 +02002435 set_nlink(inode, 2); /* Use getattr to fix if necessary */
Eric W. Biederman444ceed2006-10-02 02:18:49 -07002436 if (p->iop)
2437 inode->i_op = p->iop;
2438 if (p->fop)
2439 inode->i_fop = p->fop;
2440 ei->op = p->op;
Al Viro1bbc5512018-05-02 21:26:16 -04002441 pid_update_inode(task, inode);
Nick Pigginfb045ad2011-01-07 17:49:55 +11002442 d_set_d_op(dentry, &pid_dentry_operations);
Al Viro0168b9e2018-05-03 09:21:05 -04002443 return d_splice_alias(inode, dentry);
Eric W. Biederman444ceed2006-10-02 02:18:49 -07002444}
2445
Linus Torvalds1da177e2005-04-16 15:20:36 -07002446static struct dentry *proc_pident_lookup(struct inode *dir,
2447 struct dentry *dentry,
Eric Dumazetc5141e62007-05-08 00:26:15 -07002448 const struct pid_entry *ents,
Eric W. Biederman7bcd6b02006-10-02 02:18:56 -07002449 unsigned int nents)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002450{
Eric W. Biederman99f89552006-06-26 00:25:55 -07002451 struct task_struct *task = get_proc_task(dir);
Eric Dumazetc5141e62007-05-08 00:26:15 -07002452 const struct pid_entry *p, *last;
Al Viro0168b9e2018-05-03 09:21:05 -04002453 struct dentry *res = ERR_PTR(-ENOENT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002454
Eric W. Biederman99f89552006-06-26 00:25:55 -07002455 if (!task)
2456 goto out_no_task;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002457
Eric W. Biederman20cdc892006-10-02 02:17:07 -07002458 /*
2459 * Yes, it does not scale. And it should not. Don't add
2460 * new entries into /proc/<tgid>/ without very good reasons.
2461 */
Alexey Dobriyanbac5f5d2016-12-12 16:45:28 -08002462 last = &ents[nents];
2463 for (p = ents; p < last; p++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002464 if (p->len != dentry->d_name.len)
2465 continue;
2466 if (!memcmp(dentry->d_name.name, p->name, p->len))
2467 break;
2468 }
Alexey Dobriyanbac5f5d2016-12-12 16:45:28 -08002469 if (p >= last)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002470 goto out;
2471
Al Viro0168b9e2018-05-03 09:21:05 -04002472 res = proc_pident_instantiate(dentry, task, p);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002473out:
Eric W. Biederman99f89552006-06-26 00:25:55 -07002474 put_task_struct(task);
2475out_no_task:
Al Viro0168b9e2018-05-03 09:21:05 -04002476 return res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002477}
2478
Al Virof0c3b502013-05-16 12:07:31 -04002479static int proc_pident_readdir(struct file *file, struct dir_context *ctx,
Eric Dumazetc5141e62007-05-08 00:26:15 -07002480 const struct pid_entry *ents, unsigned int nents)
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002481{
Al Virof0c3b502013-05-16 12:07:31 -04002482 struct task_struct *task = get_proc_task(file_inode(file));
2483 const struct pid_entry *p;
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002484
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002485 if (!task)
Al Virof0c3b502013-05-16 12:07:31 -04002486 return -ENOENT;
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002487
Al Virof0c3b502013-05-16 12:07:31 -04002488 if (!dir_emit_dots(file, ctx))
2489 goto out;
2490
2491 if (ctx->pos >= nents + 2)
2492 goto out;
2493
Alexey Dobriyanbac5f5d2016-12-12 16:45:28 -08002494 for (p = ents + (ctx->pos - 2); p < ents + nents; p++) {
Al Virof0c3b502013-05-16 12:07:31 -04002495 if (!proc_fill_cache(file, ctx, p->name, p->len,
2496 proc_pident_instantiate, task, p))
2497 break;
2498 ctx->pos++;
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002499 }
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002500out:
Eric W. Biederman61a28782006-10-02 02:18:49 -07002501 put_task_struct(task);
Al Virof0c3b502013-05-16 12:07:31 -04002502 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002503}
2504
Linus Torvalds1da177e2005-04-16 15:20:36 -07002505#ifdef CONFIG_SECURITY
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002506static ssize_t proc_pid_attr_read(struct file * file, char __user * buf,
2507 size_t count, loff_t *ppos)
2508{
Al Viro496ad9a2013-01-23 17:07:38 -05002509 struct inode * inode = file_inode(file);
Al Viro04ff9702007-03-12 16:17:58 +00002510 char *p = NULL;
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002511 ssize_t length;
2512 struct task_struct *task = get_proc_task(inode);
2513
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002514 if (!task)
Al Viro04ff9702007-03-12 16:17:58 +00002515 return -ESRCH;
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002516
2517 length = security_getprocattr(task,
Josef "Jeff" Sipek2fddfee2006-12-08 02:36:36 -08002518 (char*)file->f_path.dentry->d_name.name,
Al Viro04ff9702007-03-12 16:17:58 +00002519 &p);
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002520 put_task_struct(task);
Al Viro04ff9702007-03-12 16:17:58 +00002521 if (length > 0)
2522 length = simple_read_from_buffer(buf, count, ppos, p, length);
2523 kfree(p);
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002524 return length;
2525}
2526
2527static ssize_t proc_pid_attr_write(struct file * file, const char __user * buf,
2528 size_t count, loff_t *ppos)
2529{
Al Viro496ad9a2013-01-23 17:07:38 -05002530 struct inode * inode = file_inode(file);
Al Virobb646cd2015-12-24 00:16:30 -05002531 void *page;
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002532 ssize_t length;
2533 struct task_struct *task = get_proc_task(inode);
2534
2535 length = -ESRCH;
2536 if (!task)
2537 goto out_no_task;
Stephen Smalleyb21507e2017-01-09 10:07:31 -05002538
2539 /* A task may only write its own attributes. */
2540 length = -EACCES;
2541 if (current != task)
2542 goto out;
2543
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002544 if (count > PAGE_SIZE)
2545 count = PAGE_SIZE;
2546
2547 /* No partial writes. */
2548 length = -EINVAL;
2549 if (*ppos != 0)
2550 goto out;
2551
Al Virobb646cd2015-12-24 00:16:30 -05002552 page = memdup_user(buf, count);
2553 if (IS_ERR(page)) {
2554 length = PTR_ERR(page);
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002555 goto out;
Al Virobb646cd2015-12-24 00:16:30 -05002556 }
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002557
David Howells107db7c2009-05-08 13:55:27 +01002558 /* Guard against adverse ptrace interaction */
Stephen Smalleyb21507e2017-01-09 10:07:31 -05002559 length = mutex_lock_interruptible(&current->signal->cred_guard_mutex);
David Howells107db7c2009-05-08 13:55:27 +01002560 if (length < 0)
2561 goto out_free;
2562
Stephen Smalleyb21507e2017-01-09 10:07:31 -05002563 length = security_setprocattr(file->f_path.dentry->d_name.name,
Al Virobb646cd2015-12-24 00:16:30 -05002564 page, count);
Stephen Smalleyb21507e2017-01-09 10:07:31 -05002565 mutex_unlock(&current->signal->cred_guard_mutex);
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002566out_free:
Al Virobb646cd2015-12-24 00:16:30 -05002567 kfree(page);
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002568out:
2569 put_task_struct(task);
2570out_no_task:
2571 return length;
2572}
2573
Arjan van de Ven00977a52007-02-12 00:55:34 -08002574static const struct file_operations proc_pid_attr_operations = {
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002575 .read = proc_pid_attr_read,
2576 .write = proc_pid_attr_write,
Arnd Bergmann87df8422010-03-17 23:06:02 +01002577 .llseek = generic_file_llseek,
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002578};
2579
Eric Dumazetc5141e62007-05-08 00:26:15 -07002580static const struct pid_entry attr_dir_stuff[] = {
Alexey Dobriyan631f9c12008-11-10 01:32:52 +03002581 REG("current", S_IRUGO|S_IWUGO, proc_pid_attr_operations),
2582 REG("prev", S_IRUGO, proc_pid_attr_operations),
2583 REG("exec", S_IRUGO|S_IWUGO, proc_pid_attr_operations),
2584 REG("fscreate", S_IRUGO|S_IWUGO, proc_pid_attr_operations),
2585 REG("keycreate", S_IRUGO|S_IWUGO, proc_pid_attr_operations),
2586 REG("sockcreate", S_IRUGO|S_IWUGO, proc_pid_attr_operations),
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002587};
2588
Al Virof0c3b502013-05-16 12:07:31 -04002589static int proc_attr_dir_readdir(struct file *file, struct dir_context *ctx)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002590{
Al Virof0c3b502013-05-16 12:07:31 -04002591 return proc_pident_readdir(file, ctx,
2592 attr_dir_stuff, ARRAY_SIZE(attr_dir_stuff));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002593}
2594
Arjan van de Ven00977a52007-02-12 00:55:34 -08002595static const struct file_operations proc_attr_dir_operations = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002596 .read = generic_read_dir,
Al Virof50752e2016-04-20 17:13:54 -04002597 .iterate_shared = proc_attr_dir_readdir,
2598 .llseek = generic_file_llseek,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002599};
2600
Eric W. Biederman72d9dcf2006-10-02 02:18:50 -07002601static struct dentry *proc_attr_dir_lookup(struct inode *dir,
Al Viro00cd8dd2012-06-10 17:13:09 -04002602 struct dentry *dentry, unsigned int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002603{
Eric W. Biederman7bcd6b02006-10-02 02:18:56 -07002604 return proc_pident_lookup(dir, dentry,
2605 attr_dir_stuff, ARRAY_SIZE(attr_dir_stuff));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002606}
2607
Arjan van de Venc5ef1c42007-02-12 00:55:40 -08002608static const struct inode_operations proc_attr_dir_inode_operations = {
Eric W. Biederman72d9dcf2006-10-02 02:18:50 -07002609 .lookup = proc_attr_dir_lookup,
Eric W. Biederman99f89552006-06-26 00:25:55 -07002610 .getattr = pid_getattr,
Linus Torvalds6d76fa52006-07-15 12:26:45 -07002611 .setattr = proc_setattr,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002612};
2613
Linus Torvalds1da177e2005-04-16 15:20:36 -07002614#endif
2615
Christoph Hellwig698ba7b2009-12-15 16:47:37 -08002616#ifdef CONFIG_ELF_CORE
Kawai, Hidehiro3cb4a0b2007-07-19 01:48:28 -07002617static ssize_t proc_coredump_filter_read(struct file *file, char __user *buf,
2618 size_t count, loff_t *ppos)
2619{
Al Viro496ad9a2013-01-23 17:07:38 -05002620 struct task_struct *task = get_proc_task(file_inode(file));
Kawai, Hidehiro3cb4a0b2007-07-19 01:48:28 -07002621 struct mm_struct *mm;
2622 char buffer[PROC_NUMBUF];
2623 size_t len;
2624 int ret;
2625
2626 if (!task)
2627 return -ESRCH;
2628
2629 ret = 0;
2630 mm = get_task_mm(task);
2631 if (mm) {
2632 len = snprintf(buffer, sizeof(buffer), "%08lx\n",
2633 ((mm->flags & MMF_DUMP_FILTER_MASK) >>
2634 MMF_DUMP_FILTER_SHIFT));
2635 mmput(mm);
2636 ret = simple_read_from_buffer(buf, count, ppos, buffer, len);
2637 }
2638
2639 put_task_struct(task);
2640
2641 return ret;
2642}
2643
2644static ssize_t proc_coredump_filter_write(struct file *file,
2645 const char __user *buf,
2646 size_t count,
2647 loff_t *ppos)
2648{
2649 struct task_struct *task;
2650 struct mm_struct *mm;
Kawai, Hidehiro3cb4a0b2007-07-19 01:48:28 -07002651 unsigned int val;
2652 int ret;
2653 int i;
2654 unsigned long mask;
2655
Alexey Dobriyan774636e2015-09-09 15:36:59 -07002656 ret = kstrtouint_from_user(buf, count, 0, &val);
2657 if (ret < 0)
2658 return ret;
Kawai, Hidehiro3cb4a0b2007-07-19 01:48:28 -07002659
2660 ret = -ESRCH;
Al Viro496ad9a2013-01-23 17:07:38 -05002661 task = get_proc_task(file_inode(file));
Kawai, Hidehiro3cb4a0b2007-07-19 01:48:28 -07002662 if (!task)
2663 goto out_no_task;
2664
Kawai, Hidehiro3cb4a0b2007-07-19 01:48:28 -07002665 mm = get_task_mm(task);
2666 if (!mm)
2667 goto out_no_mm;
Colin Ian King41a0c2492015-12-18 14:22:01 -08002668 ret = 0;
Kawai, Hidehiro3cb4a0b2007-07-19 01:48:28 -07002669
2670 for (i = 0, mask = 1; i < MMF_DUMP_FILTER_BITS; i++, mask <<= 1) {
2671 if (val & mask)
2672 set_bit(i + MMF_DUMP_FILTER_SHIFT, &mm->flags);
2673 else
2674 clear_bit(i + MMF_DUMP_FILTER_SHIFT, &mm->flags);
2675 }
2676
2677 mmput(mm);
2678 out_no_mm:
2679 put_task_struct(task);
2680 out_no_task:
Alexey Dobriyan774636e2015-09-09 15:36:59 -07002681 if (ret < 0)
2682 return ret;
2683 return count;
Kawai, Hidehiro3cb4a0b2007-07-19 01:48:28 -07002684}
2685
2686static const struct file_operations proc_coredump_filter_operations = {
2687 .read = proc_coredump_filter_read,
2688 .write = proc_coredump_filter_write,
Arnd Bergmann87df8422010-03-17 23:06:02 +01002689 .llseek = generic_file_llseek,
Kawai, Hidehiro3cb4a0b2007-07-19 01:48:28 -07002690};
2691#endif
2692
Andrew Mortonaba76fd2006-12-10 02:19:48 -08002693#ifdef CONFIG_TASK_IO_ACCOUNTING
Alexey Dobriyan19aadc92014-08-08 14:21:50 -07002694static int do_io_accounting(struct task_struct *task, struct seq_file *m, int whole)
Andrew Mortonaba76fd2006-12-10 02:19:48 -08002695{
Andrea Righi940389b2008-07-28 00:48:12 +02002696 struct task_io_accounting acct = task->ioac;
Andrea Righi59954772008-07-27 17:29:15 +02002697 unsigned long flags;
Vasiliy Kulikov293eb1e2011-07-26 16:08:38 -07002698 int result;
Andrea Righi297c5d92008-07-25 01:48:49 -07002699
Vasiliy Kulikov293eb1e2011-07-26 16:08:38 -07002700 result = mutex_lock_killable(&task->signal->cred_guard_mutex);
2701 if (result)
2702 return result;
2703
Jann Horncaaee622016-01-20 15:00:04 -08002704 if (!ptrace_may_access(task, PTRACE_MODE_READ_FSCREDS)) {
Vasiliy Kulikov293eb1e2011-07-26 16:08:38 -07002705 result = -EACCES;
2706 goto out_unlock;
2707 }
Vasiliy Kulikov1d1221f2011-06-24 16:08:38 +04002708
Andrea Righi59954772008-07-27 17:29:15 +02002709 if (whole && lock_task_sighand(task, &flags)) {
2710 struct task_struct *t = task;
Andrea Righib2d002d2008-07-26 15:22:27 -07002711
Andrea Righi59954772008-07-27 17:29:15 +02002712 task_io_accounting_add(&acct, &task->signal->ioac);
2713 while_each_thread(task, t)
2714 task_io_accounting_add(&acct, &t->ioac);
Andrea Righi297c5d92008-07-25 01:48:49 -07002715
Andrea Righi59954772008-07-27 17:29:15 +02002716 unlock_task_sighand(task, &flags);
Andrea Righi297c5d92008-07-25 01:48:49 -07002717 }
Joe Perches25ce3192015-04-15 16:18:17 -07002718 seq_printf(m,
2719 "rchar: %llu\n"
2720 "wchar: %llu\n"
2721 "syscr: %llu\n"
2722 "syscw: %llu\n"
2723 "read_bytes: %llu\n"
2724 "write_bytes: %llu\n"
2725 "cancelled_write_bytes: %llu\n",
2726 (unsigned long long)acct.rchar,
2727 (unsigned long long)acct.wchar,
2728 (unsigned long long)acct.syscr,
2729 (unsigned long long)acct.syscw,
2730 (unsigned long long)acct.read_bytes,
2731 (unsigned long long)acct.write_bytes,
2732 (unsigned long long)acct.cancelled_write_bytes);
2733 result = 0;
2734
Vasiliy Kulikov293eb1e2011-07-26 16:08:38 -07002735out_unlock:
2736 mutex_unlock(&task->signal->cred_guard_mutex);
2737 return result;
Andrew Mortonaba76fd2006-12-10 02:19:48 -08002738}
Andrea Righi297c5d92008-07-25 01:48:49 -07002739
Alexey Dobriyan19aadc92014-08-08 14:21:50 -07002740static int proc_tid_io_accounting(struct seq_file *m, struct pid_namespace *ns,
2741 struct pid *pid, struct task_struct *task)
Andrea Righi297c5d92008-07-25 01:48:49 -07002742{
Alexey Dobriyan19aadc92014-08-08 14:21:50 -07002743 return do_io_accounting(task, m, 0);
Andrea Righi297c5d92008-07-25 01:48:49 -07002744}
2745
Alexey Dobriyan19aadc92014-08-08 14:21:50 -07002746static int proc_tgid_io_accounting(struct seq_file *m, struct pid_namespace *ns,
2747 struct pid *pid, struct task_struct *task)
Andrea Righi297c5d92008-07-25 01:48:49 -07002748{
Alexey Dobriyan19aadc92014-08-08 14:21:50 -07002749 return do_io_accounting(task, m, 1);
Andrea Righi297c5d92008-07-25 01:48:49 -07002750}
2751#endif /* CONFIG_TASK_IO_ACCOUNTING */
Andrew Mortonaba76fd2006-12-10 02:19:48 -08002752
Eric W. Biederman22d917d2011-11-17 00:11:58 -08002753#ifdef CONFIG_USER_NS
2754static int proc_id_map_open(struct inode *inode, struct file *file,
Fabian Frederickccf94f12014-08-08 14:21:22 -07002755 const struct seq_operations *seq_ops)
Eric W. Biederman22d917d2011-11-17 00:11:58 -08002756{
2757 struct user_namespace *ns = NULL;
2758 struct task_struct *task;
2759 struct seq_file *seq;
2760 int ret = -EINVAL;
2761
2762 task = get_proc_task(inode);
2763 if (task) {
2764 rcu_read_lock();
2765 ns = get_user_ns(task_cred_xxx(task, user_ns));
2766 rcu_read_unlock();
2767 put_task_struct(task);
2768 }
2769 if (!ns)
2770 goto err;
2771
2772 ret = seq_open(file, seq_ops);
2773 if (ret)
2774 goto err_put_ns;
2775
2776 seq = file->private_data;
2777 seq->private = ns;
2778
2779 return 0;
2780err_put_ns:
2781 put_user_ns(ns);
2782err:
2783 return ret;
2784}
2785
2786static int proc_id_map_release(struct inode *inode, struct file *file)
2787{
2788 struct seq_file *seq = file->private_data;
2789 struct user_namespace *ns = seq->private;
2790 put_user_ns(ns);
2791 return seq_release(inode, file);
2792}
2793
2794static int proc_uid_map_open(struct inode *inode, struct file *file)
2795{
2796 return proc_id_map_open(inode, file, &proc_uid_seq_operations);
2797}
2798
2799static int proc_gid_map_open(struct inode *inode, struct file *file)
2800{
2801 return proc_id_map_open(inode, file, &proc_gid_seq_operations);
2802}
2803
Eric W. Biedermanf76d2072012-08-30 01:24:05 -07002804static int proc_projid_map_open(struct inode *inode, struct file *file)
2805{
2806 return proc_id_map_open(inode, file, &proc_projid_seq_operations);
2807}
2808
Eric W. Biederman22d917d2011-11-17 00:11:58 -08002809static const struct file_operations proc_uid_map_operations = {
2810 .open = proc_uid_map_open,
2811 .write = proc_uid_map_write,
2812 .read = seq_read,
2813 .llseek = seq_lseek,
2814 .release = proc_id_map_release,
2815};
2816
2817static const struct file_operations proc_gid_map_operations = {
2818 .open = proc_gid_map_open,
2819 .write = proc_gid_map_write,
2820 .read = seq_read,
2821 .llseek = seq_lseek,
2822 .release = proc_id_map_release,
2823};
Eric W. Biedermanf76d2072012-08-30 01:24:05 -07002824
2825static const struct file_operations proc_projid_map_operations = {
2826 .open = proc_projid_map_open,
2827 .write = proc_projid_map_write,
2828 .read = seq_read,
2829 .llseek = seq_lseek,
2830 .release = proc_id_map_release,
2831};
Eric W. Biederman9cc46512014-12-02 12:27:26 -06002832
2833static int proc_setgroups_open(struct inode *inode, struct file *file)
2834{
2835 struct user_namespace *ns = NULL;
2836 struct task_struct *task;
2837 int ret;
2838
2839 ret = -ESRCH;
2840 task = get_proc_task(inode);
2841 if (task) {
2842 rcu_read_lock();
2843 ns = get_user_ns(task_cred_xxx(task, user_ns));
2844 rcu_read_unlock();
2845 put_task_struct(task);
2846 }
2847 if (!ns)
2848 goto err;
2849
2850 if (file->f_mode & FMODE_WRITE) {
2851 ret = -EACCES;
2852 if (!ns_capable(ns, CAP_SYS_ADMIN))
2853 goto err_put_ns;
2854 }
2855
2856 ret = single_open(file, &proc_setgroups_show, ns);
2857 if (ret)
2858 goto err_put_ns;
2859
2860 return 0;
2861err_put_ns:
2862 put_user_ns(ns);
2863err:
2864 return ret;
2865}
2866
2867static int proc_setgroups_release(struct inode *inode, struct file *file)
2868{
2869 struct seq_file *seq = file->private_data;
2870 struct user_namespace *ns = seq->private;
2871 int ret = single_release(inode, file);
2872 put_user_ns(ns);
2873 return ret;
2874}
2875
2876static const struct file_operations proc_setgroups_operations = {
2877 .open = proc_setgroups_open,
2878 .write = proc_setgroups_write,
2879 .read = seq_read,
2880 .llseek = seq_lseek,
2881 .release = proc_setgroups_release,
2882};
Eric W. Biederman22d917d2011-11-17 00:11:58 -08002883#endif /* CONFIG_USER_NS */
2884
Kees Cook47830722008-10-06 03:11:58 +04002885static int proc_pid_personality(struct seq_file *m, struct pid_namespace *ns,
2886 struct pid *pid, struct task_struct *task)
2887{
Al Viroa9712bc2011-03-23 15:52:50 -04002888 int err = lock_trace(task);
2889 if (!err) {
2890 seq_printf(m, "%08x\n", task->personality);
2891 unlock_trace(task);
2892 }
2893 return err;
Kees Cook47830722008-10-06 03:11:58 +04002894}
2895
Josh Poimboeuf7c23b332017-02-13 19:42:41 -06002896#ifdef CONFIG_LIVEPATCH
2897static int proc_pid_patch_state(struct seq_file *m, struct pid_namespace *ns,
2898 struct pid *pid, struct task_struct *task)
2899{
2900 seq_printf(m, "%d\n", task->patch_state);
2901 return 0;
2902}
2903#endif /* CONFIG_LIVEPATCH */
2904
Eric W. Biederman801199c2006-10-02 02:18:48 -07002905/*
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002906 * Thread groups
2907 */
Arjan van de Ven00977a52007-02-12 00:55:34 -08002908static const struct file_operations proc_task_operations;
Arjan van de Venc5ef1c42007-02-12 00:55:40 -08002909static const struct inode_operations proc_task_inode_operations;
Eric W. Biederman20cdc892006-10-02 02:17:07 -07002910
Eric Dumazetc5141e62007-05-08 00:26:15 -07002911static const struct pid_entry tgid_base_stuff[] = {
Alexey Dobriyan631f9c12008-11-10 01:32:52 +03002912 DIR("task", S_IRUGO|S_IXUGO, proc_task_inode_operations, proc_task_operations),
2913 DIR("fd", S_IRUSR|S_IXUSR, proc_fd_inode_operations, proc_fd_operations),
Pavel Emelyanov640708a2012-01-10 15:11:23 -08002914 DIR("map_files", S_IRUSR|S_IXUSR, proc_map_files_inode_operations, proc_map_files_operations),
Alexey Dobriyan631f9c12008-11-10 01:32:52 +03002915 DIR("fdinfo", S_IRUSR|S_IXUSR, proc_fdinfo_inode_operations, proc_fdinfo_operations),
Eric W. Biederman6b4e3062010-03-07 16:41:34 -08002916 DIR("ns", S_IRUSR|S_IXUGO, proc_ns_dir_inode_operations, proc_ns_dir_operations),
Andrew Mortonb2211a32008-03-11 18:03:35 -07002917#ifdef CONFIG_NET
Alexey Dobriyan631f9c12008-11-10 01:32:52 +03002918 DIR("net", S_IRUGO|S_IXUGO, proc_net_inode_operations, proc_net_operations),
Andrew Mortonb2211a32008-03-11 18:03:35 -07002919#endif
Alexey Dobriyan631f9c12008-11-10 01:32:52 +03002920 REG("environ", S_IRUSR, proc_environ_operations),
Al Viroc5317162016-10-05 18:43:43 -04002921 REG("auxv", S_IRUSR, proc_auxv_operations),
Alexey Dobriyan631f9c12008-11-10 01:32:52 +03002922 ONE("status", S_IRUGO, proc_pid_status),
Djalal Harouni35a35042014-04-07 15:38:36 -07002923 ONE("personality", S_IRUSR, proc_pid_personality),
Alexey Dobriyan1c963eb2014-08-08 14:21:37 -07002924 ONE("limits", S_IRUGO, proc_pid_limits),
Ingo Molnar43ae34c2007-07-09 18:52:00 +02002925#ifdef CONFIG_SCHED_DEBUG
Alexey Dobriyan631f9c12008-11-10 01:32:52 +03002926 REG("sched", S_IRUGO|S_IWUSR, proc_pid_sched_operations),
Ingo Molnar43ae34c2007-07-09 18:52:00 +02002927#endif
Mike Galbraith5091faa2010-11-30 14:18:03 +01002928#ifdef CONFIG_SCHED_AUTOGROUP
2929 REG("autogroup", S_IRUGO|S_IWUSR, proc_pid_sched_autogroup_operations),
2930#endif
john stultz4614a696b2009-12-14 18:00:05 -08002931 REG("comm", S_IRUGO|S_IWUSR, proc_pid_set_comm_operations),
Roland McGrathebcb6732008-07-25 19:46:00 -07002932#ifdef CONFIG_HAVE_ARCH_TRACEHOOK
Alexey Dobriyan09d93bd2014-08-08 14:21:39 -07002933 ONE("syscall", S_IRUSR, proc_pid_syscall),
Roland McGrathebcb6732008-07-25 19:46:00 -07002934#endif
Alexey Dobriyanc2c0bb42015-06-25 15:00:54 -07002935 REG("cmdline", S_IRUGO, proc_pid_cmdline_ops),
Alexey Dobriyan631f9c12008-11-10 01:32:52 +03002936 ONE("stat", S_IRUGO, proc_tgid_stat),
2937 ONE("statm", S_IRUGO, proc_pid_statm),
Siddhesh Poyarekarb7643752012-03-21 16:34:04 -07002938 REG("maps", S_IRUGO, proc_pid_maps_operations),
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002939#ifdef CONFIG_NUMA
Siddhesh Poyarekarb7643752012-03-21 16:34:04 -07002940 REG("numa_maps", S_IRUGO, proc_pid_numa_maps_operations),
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002941#endif
Alexey Dobriyan631f9c12008-11-10 01:32:52 +03002942 REG("mem", S_IRUSR|S_IWUSR, proc_mem_operations),
2943 LNK("cwd", proc_cwd_link),
2944 LNK("root", proc_root_link),
2945 LNK("exe", proc_exe_link),
2946 REG("mounts", S_IRUGO, proc_mounts_operations),
2947 REG("mountinfo", S_IRUGO, proc_mountinfo_operations),
2948 REG("mountstats", S_IRUSR, proc_mountstats_operations),
Matt Mackall1e883282008-02-04 22:29:07 -08002949#ifdef CONFIG_PROC_PAGE_MONITOR
Alexey Dobriyan631f9c12008-11-10 01:32:52 +03002950 REG("clear_refs", S_IWUSR, proc_clear_refs_operations),
Siddhesh Poyarekarb7643752012-03-21 16:34:04 -07002951 REG("smaps", S_IRUGO, proc_pid_smaps_operations),
Daniel Colascione493b0e92017-09-06 16:25:08 -07002952 REG("smaps_rollup", S_IRUGO, proc_pid_smaps_rollup_operations),
Djalal Harouni32ed74a2014-04-07 15:38:38 -07002953 REG("pagemap", S_IRUSR, proc_pagemap_operations),
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002954#endif
2955#ifdef CONFIG_SECURITY
Alexey Dobriyan631f9c12008-11-10 01:32:52 +03002956 DIR("attr", S_IRUGO|S_IXUGO, proc_attr_dir_inode_operations, proc_attr_dir_operations),
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002957#endif
2958#ifdef CONFIG_KALLSYMS
Alexey Dobriyanedfcd602014-08-08 14:21:44 -07002959 ONE("wchan", S_IRUGO, proc_pid_wchan),
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002960#endif
Ken Chen2ec220e2008-11-10 11:26:08 +03002961#ifdef CONFIG_STACKTRACE
Djalal Harouni35a35042014-04-07 15:38:36 -07002962 ONE("stack", S_IRUSR, proc_pid_stack),
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002963#endif
Naveen N. Rao5968cec2015-06-30 14:36:03 +05302964#ifdef CONFIG_SCHED_INFO
Alexey Dobriyanf6e826c2014-08-08 14:21:46 -07002965 ONE("schedstat", S_IRUGO, proc_pid_schedstat),
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002966#endif
Arjan van de Ven97455122008-01-25 21:08:34 +01002967#ifdef CONFIG_LATENCYTOP
Alexey Dobriyan631f9c12008-11-10 01:32:52 +03002968 REG("latency", S_IRUGO, proc_lstats_operations),
Arjan van de Ven97455122008-01-25 21:08:34 +01002969#endif
Paul Menage8793d852007-10-18 23:39:39 -07002970#ifdef CONFIG_PROC_PID_CPUSET
Zefan Li52de4772014-09-18 16:03:36 +08002971 ONE("cpuset", S_IRUGO, proc_cpuset_show),
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002972#endif
Paul Menagea4243162007-10-18 23:39:35 -07002973#ifdef CONFIG_CGROUPS
Zefan Li006f4ac2014-09-18 16:03:15 +08002974 ONE("cgroup", S_IRUGO, proc_cgroup_show),
Paul Menagea4243162007-10-18 23:39:35 -07002975#endif
Alexey Dobriyan6ba51e32014-08-08 14:21:48 -07002976 ONE("oom_score", S_IRUGO, proc_oom_score),
David Rientjesfa0cbbf2012-11-12 17:53:04 -08002977 REG("oom_adj", S_IRUGO|S_IWUSR, proc_oom_adj_operations),
David Rientjesa63d83f2010-08-09 17:19:46 -07002978 REG("oom_score_adj", S_IRUGO|S_IWUSR, proc_oom_score_adj_operations),
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002979#ifdef CONFIG_AUDITSYSCALL
Alexey Dobriyan631f9c12008-11-10 01:32:52 +03002980 REG("loginuid", S_IWUSR|S_IRUGO, proc_loginuid_operations),
2981 REG("sessionid", S_IRUGO, proc_sessionid_operations),
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002982#endif
Akinobu Mitaf4f154f2006-12-08 02:39:47 -08002983#ifdef CONFIG_FAULT_INJECTION
Alexey Dobriyan631f9c12008-11-10 01:32:52 +03002984 REG("make-it-fail", S_IRUGO|S_IWUSR, proc_fault_inject_operations),
Akinobu Mita168c42b2017-07-14 14:50:00 -07002985 REG("fail-nth", 0644, proc_fail_nth_operations),
Akinobu Mitaf4f154f2006-12-08 02:39:47 -08002986#endif
Christoph Hellwig698ba7b2009-12-15 16:47:37 -08002987#ifdef CONFIG_ELF_CORE
Alexey Dobriyan631f9c12008-11-10 01:32:52 +03002988 REG("coredump_filter", S_IRUGO|S_IWUSR, proc_coredump_filter_operations),
Kawai, Hidehiro3cb4a0b2007-07-19 01:48:28 -07002989#endif
Andrew Mortonaba76fd2006-12-10 02:19:48 -08002990#ifdef CONFIG_TASK_IO_ACCOUNTING
Alexey Dobriyan19aadc92014-08-08 14:21:50 -07002991 ONE("io", S_IRUSR, proc_tgid_io_accounting),
Andrew Mortonaba76fd2006-12-10 02:19:48 -08002992#endif
Eric W. Biederman22d917d2011-11-17 00:11:58 -08002993#ifdef CONFIG_USER_NS
2994 REG("uid_map", S_IRUGO|S_IWUSR, proc_uid_map_operations),
2995 REG("gid_map", S_IRUGO|S_IWUSR, proc_gid_map_operations),
Eric W. Biedermanf76d2072012-08-30 01:24:05 -07002996 REG("projid_map", S_IRUGO|S_IWUSR, proc_projid_map_operations),
Eric W. Biederman9cc46512014-12-02 12:27:26 -06002997 REG("setgroups", S_IRUGO|S_IWUSR, proc_setgroups_operations),
Eric W. Biederman22d917d2011-11-17 00:11:58 -08002998#endif
Nicolas Pitreb18b6a92017-01-21 00:09:08 -05002999#if defined(CONFIG_CHECKPOINT_RESTORE) && defined(CONFIG_POSIX_TIMERS)
Pavel Emelyanov48f6a7a2013-03-11 13:12:45 +04003000 REG("timers", S_IRUGO, proc_timers_operations),
3001#endif
John Stultz5de23d42016-03-17 14:20:54 -07003002 REG("timerslack_ns", S_IRUGO|S_IWUGO, proc_pid_set_timerslack_ns_operations),
Josh Poimboeuf7c23b332017-02-13 19:42:41 -06003003#ifdef CONFIG_LIVEPATCH
3004 ONE("patch_state", S_IRUSR, proc_pid_patch_state),
3005#endif
Eric W. Biederman28a6d672006-10-02 02:17:05 -07003006};
3007
Al Virof0c3b502013-05-16 12:07:31 -04003008static int proc_tgid_base_readdir(struct file *file, struct dir_context *ctx)
Eric W. Biederman28a6d672006-10-02 02:17:05 -07003009{
Al Virof0c3b502013-05-16 12:07:31 -04003010 return proc_pident_readdir(file, ctx,
3011 tgid_base_stuff, ARRAY_SIZE(tgid_base_stuff));
Eric W. Biederman28a6d672006-10-02 02:17:05 -07003012}
3013
Arjan van de Ven00977a52007-02-12 00:55:34 -08003014static const struct file_operations proc_tgid_base_operations = {
Eric W. Biederman28a6d672006-10-02 02:17:05 -07003015 .read = generic_read_dir,
Al Virof50752e2016-04-20 17:13:54 -04003016 .iterate_shared = proc_tgid_base_readdir,
3017 .llseek = generic_file_llseek,
Eric W. Biederman28a6d672006-10-02 02:17:05 -07003018};
3019
Al Viro00cd8dd2012-06-10 17:13:09 -04003020static struct dentry *proc_tgid_base_lookup(struct inode *dir, struct dentry *dentry, unsigned int flags)
3021{
Eric W. Biederman7bcd6b02006-10-02 02:18:56 -07003022 return proc_pident_lookup(dir, dentry,
3023 tgid_base_stuff, ARRAY_SIZE(tgid_base_stuff));
Eric W. Biederman28a6d672006-10-02 02:17:05 -07003024}
3025
Arjan van de Venc5ef1c42007-02-12 00:55:40 -08003026static const struct inode_operations proc_tgid_base_inode_operations = {
Eric W. Biederman28a6d672006-10-02 02:17:05 -07003027 .lookup = proc_tgid_base_lookup,
3028 .getattr = pid_getattr,
3029 .setattr = proc_setattr,
Vasiliy Kulikov04996802012-01-10 15:11:31 -08003030 .permission = proc_pid_permission,
Eric W. Biederman28a6d672006-10-02 02:17:05 -07003031};
3032
Pavel Emelyanov60347f62007-10-18 23:40:03 -07003033static void proc_flush_task_mnt(struct vfsmount *mnt, pid_t pid, pid_t tgid)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003034{
Eric W. Biederman48e64842006-06-26 00:25:48 -07003035 struct dentry *dentry, *leader, *dir;
Alexey Dobriyane3912ac2018-02-06 15:36:51 -08003036 char buf[10 + 1];
Eric W. Biederman48e64842006-06-26 00:25:48 -07003037 struct qstr name;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003038
Eric W. Biederman48e64842006-06-26 00:25:48 -07003039 name.name = buf;
Alexey Dobriyane3912ac2018-02-06 15:36:51 -08003040 name.len = snprintf(buf, sizeof(buf), "%u", pid);
Al Viro4f522a22013-02-11 23:20:37 -05003041 /* no ->d_hash() rejects on procfs */
Pavel Emelyanov60347f62007-10-18 23:40:03 -07003042 dentry = d_hash_and_lookup(mnt->mnt_root, &name);
Eric W. Biederman48e64842006-06-26 00:25:48 -07003043 if (dentry) {
Eric W. Biedermanbbd51922014-02-13 10:24:23 -08003044 d_invalidate(dentry);
Eric W. Biederman48e64842006-06-26 00:25:48 -07003045 dput(dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003046 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003047
Oleg Nesterovc35a7f12014-12-10 15:54:56 -08003048 if (pid == tgid)
3049 return;
3050
Eric W. Biederman48e64842006-06-26 00:25:48 -07003051 name.name = buf;
Alexey Dobriyane3912ac2018-02-06 15:36:51 -08003052 name.len = snprintf(buf, sizeof(buf), "%u", tgid);
Pavel Emelyanov60347f62007-10-18 23:40:03 -07003053 leader = d_hash_and_lookup(mnt->mnt_root, &name);
Eric W. Biederman48e64842006-06-26 00:25:48 -07003054 if (!leader)
3055 goto out;
3056
3057 name.name = "task";
3058 name.len = strlen(name.name);
3059 dir = d_hash_and_lookup(leader, &name);
3060 if (!dir)
3061 goto out_put_leader;
3062
3063 name.name = buf;
Alexey Dobriyane3912ac2018-02-06 15:36:51 -08003064 name.len = snprintf(buf, sizeof(buf), "%u", pid);
Eric W. Biederman48e64842006-06-26 00:25:48 -07003065 dentry = d_hash_and_lookup(dir, &name);
3066 if (dentry) {
Eric W. Biedermanbbd51922014-02-13 10:24:23 -08003067 d_invalidate(dentry);
Eric W. Biederman48e64842006-06-26 00:25:48 -07003068 dput(dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003069 }
Eric W. Biederman48e64842006-06-26 00:25:48 -07003070
3071 dput(dir);
3072out_put_leader:
3073 dput(leader);
3074out:
3075 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003076}
3077
Randy Dunlap0895e912007-10-21 21:00:10 -07003078/**
3079 * proc_flush_task - Remove dcache entries for @task from the /proc dcache.
3080 * @task: task that should be flushed.
3081 *
3082 * When flushing dentries from proc, one needs to flush them from global
Pavel Emelyanov60347f62007-10-18 23:40:03 -07003083 * proc (proc_mnt) and from all the namespaces' procs this task was seen
Randy Dunlap0895e912007-10-21 21:00:10 -07003084 * in. This call is supposed to do all of this job.
3085 *
3086 * Looks in the dcache for
3087 * /proc/@pid
3088 * /proc/@tgid/task/@pid
3089 * if either directory is present flushes it and all of it'ts children
3090 * from the dcache.
3091 *
3092 * It is safe and reasonable to cache /proc entries for a task until
3093 * that task exits. After that they just clog up the dcache with
3094 * useless entries, possibly causing useful dcache entries to be
3095 * flushed instead. This routine is proved to flush those useless
3096 * dcache entries at process exit time.
3097 *
3098 * NOTE: This routine is just an optimization so it does not guarantee
3099 * that no dcache entries will exist at process exit time it
3100 * just makes it very unlikely that any will persist.
Pavel Emelyanov60347f62007-10-18 23:40:03 -07003101 */
3102
3103void proc_flush_task(struct task_struct *task)
3104{
Eric W. Biederman9fcc2d12007-11-14 17:00:07 -08003105 int i;
Oleg Nesterov9b4d1cbe2009-09-22 16:45:34 -07003106 struct pid *pid, *tgid;
Pavel Emelyanov130f77e2007-10-18 23:40:11 -07003107 struct upid *upid;
3108
Pavel Emelyanov130f77e2007-10-18 23:40:11 -07003109 pid = task_pid(task);
Oleg Nesterov9b4d1cbe2009-09-22 16:45:34 -07003110 tgid = task_tgid(task);
Pavel Emelyanov130f77e2007-10-18 23:40:11 -07003111
Eric W. Biederman9fcc2d12007-11-14 17:00:07 -08003112 for (i = 0; i <= pid->level; i++) {
Pavel Emelyanov130f77e2007-10-18 23:40:11 -07003113 upid = &pid->numbers[i];
3114 proc_flush_task_mnt(upid->ns->proc_mnt, upid->nr,
Oleg Nesterov9b4d1cbe2009-09-22 16:45:34 -07003115 tgid->numbers[i].nr);
Pavel Emelyanov130f77e2007-10-18 23:40:11 -07003116 }
Pavel Emelyanov60347f62007-10-18 23:40:03 -07003117}
3118
Al Viro0168b9e2018-05-03 09:21:05 -04003119static struct dentry *proc_pid_instantiate(struct dentry * dentry,
Al Viroc52a47a2013-06-15 11:15:20 +04003120 struct task_struct *task, const void *ptr)
Eric W. Biederman444ceed2006-10-02 02:18:49 -07003121{
Eric W. Biederman444ceed2006-10-02 02:18:49 -07003122 struct inode *inode;
3123
Al Viro0168b9e2018-05-03 09:21:05 -04003124 inode = proc_pid_make_inode(dentry->d_sb, task, S_IFDIR | S_IRUGO | S_IXUGO);
Eric W. Biederman444ceed2006-10-02 02:18:49 -07003125 if (!inode)
Al Viro0168b9e2018-05-03 09:21:05 -04003126 return ERR_PTR(-ENOENT);
Eric W. Biederman444ceed2006-10-02 02:18:49 -07003127
Eric W. Biederman444ceed2006-10-02 02:18:49 -07003128 inode->i_op = &proc_tgid_base_inode_operations;
3129 inode->i_fop = &proc_tgid_base_operations;
3130 inode->i_flags|=S_IMMUTABLE;
Vegard Nossumaed54172008-06-05 22:46:53 -07003131
Alexey Dobriyan1270dd82016-12-12 16:45:32 -08003132 set_nlink(inode, nlink_tgid);
Al Viro1bbc5512018-05-02 21:26:16 -04003133 pid_update_inode(task, inode);
Eric W. Biederman444ceed2006-10-02 02:18:49 -07003134
Nick Pigginfb045ad2011-01-07 17:49:55 +11003135 d_set_d_op(dentry, &pid_dentry_operations);
Al Viro0168b9e2018-05-03 09:21:05 -04003136 return d_splice_alias(inode, dentry);
Eric W. Biederman444ceed2006-10-02 02:18:49 -07003137}
3138
Al Viro00cd8dd2012-06-10 17:13:09 -04003139struct dentry *proc_pid_lookup(struct inode *dir, struct dentry * dentry, unsigned int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003140{
3141 struct task_struct *task;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003142 unsigned tgid;
Pavel Emelyanovb4888932007-10-18 23:40:14 -07003143 struct pid_namespace *ns;
Al Viro0168b9e2018-05-03 09:21:05 -04003144 struct dentry *result = ERR_PTR(-ENOENT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003145
Alexey Dobriyandbcdb502014-08-08 14:21:25 -07003146 tgid = name_to_int(&dentry->d_name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003147 if (tgid == ~0U)
3148 goto out;
3149
Pavel Emelyanovb4888932007-10-18 23:40:14 -07003150 ns = dentry->d_sb->s_fs_info;
Eric W. Biedermande758732006-06-26 00:25:51 -07003151 rcu_read_lock();
Pavel Emelyanovb4888932007-10-18 23:40:14 -07003152 task = find_task_by_pid_ns(tgid, ns);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003153 if (task)
3154 get_task_struct(task);
Eric W. Biedermande758732006-06-26 00:25:51 -07003155 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07003156 if (!task)
3157 goto out;
3158
Al Viro0168b9e2018-05-03 09:21:05 -04003159 result = proc_pid_instantiate(dentry, task, NULL);
Eric W. Biederman48e64842006-06-26 00:25:48 -07003160 put_task_struct(task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003161out:
Al Viro0168b9e2018-05-03 09:21:05 -04003162 return result;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003163}
3164
Linus Torvalds1da177e2005-04-16 15:20:36 -07003165/*
Eric W. Biederman0804ef42006-10-02 02:17:04 -07003166 * Find the first task with tgid >= tgid
Eric W. Biederman0bc58a92006-06-26 00:25:50 -07003167 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07003168 */
Eric W. Biederman19fd4bb2007-11-28 16:21:26 -08003169struct tgid_iter {
3170 unsigned int tgid;
Eric W. Biederman0804ef42006-10-02 02:17:04 -07003171 struct task_struct *task;
Eric W. Biederman19fd4bb2007-11-28 16:21:26 -08003172};
3173static struct tgid_iter next_tgid(struct pid_namespace *ns, struct tgid_iter iter)
3174{
Eric W. Biederman0804ef42006-10-02 02:17:04 -07003175 struct pid *pid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003176
Eric W. Biederman19fd4bb2007-11-28 16:21:26 -08003177 if (iter.task)
3178 put_task_struct(iter.task);
Eric W. Biederman0804ef42006-10-02 02:17:04 -07003179 rcu_read_lock();
3180retry:
Eric W. Biederman19fd4bb2007-11-28 16:21:26 -08003181 iter.task = NULL;
3182 pid = find_ge_pid(iter.tgid, ns);
Eric W. Biederman0804ef42006-10-02 02:17:04 -07003183 if (pid) {
Eric W. Biederman19fd4bb2007-11-28 16:21:26 -08003184 iter.tgid = pid_nr_ns(pid, ns);
3185 iter.task = pid_task(pid, PIDTYPE_PID);
Eric W. Biederman0804ef42006-10-02 02:17:04 -07003186 /* What we to know is if the pid we have find is the
3187 * pid of a thread_group_leader. Testing for task
3188 * being a thread_group_leader is the obvious thing
3189 * todo but there is a window when it fails, due to
3190 * the pid transfer logic in de_thread.
3191 *
3192 * So we perform the straight forward test of seeing
3193 * if the pid we have found is the pid of a thread
3194 * group leader, and don't worry if the task we have
3195 * found doesn't happen to be a thread group leader.
3196 * As we don't care in the case of readdir.
3197 */
Eric W. Biederman19fd4bb2007-11-28 16:21:26 -08003198 if (!iter.task || !has_group_leader_pid(iter.task)) {
3199 iter.tgid += 1;
Eric W. Biederman0804ef42006-10-02 02:17:04 -07003200 goto retry;
Eric W. Biederman19fd4bb2007-11-28 16:21:26 -08003201 }
3202 get_task_struct(iter.task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003203 }
Eric W. Biederman454cc102006-06-26 00:25:51 -07003204 rcu_read_unlock();
Eric W. Biederman19fd4bb2007-11-28 16:21:26 -08003205 return iter;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003206}
3207
Eric W. Biederman00978752014-07-31 03:10:50 -07003208#define TGID_OFFSET (FIRST_PROCESS_ENTRY + 2)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003209
3210/* for the /proc/ directory itself, after non-process stuff has been done */
Al Virof0c3b502013-05-16 12:07:31 -04003211int proc_pid_readdir(struct file *file, struct dir_context *ctx)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003212{
Eric W. Biederman19fd4bb2007-11-28 16:21:26 -08003213 struct tgid_iter iter;
Christoph Hellwig76f668b2018-05-16 07:19:01 +02003214 struct pid_namespace *ns = proc_pid_ns(file_inode(file));
Al Virof0c3b502013-05-16 12:07:31 -04003215 loff_t pos = ctx->pos;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003216
Al Viro021ada72013-03-29 19:27:05 -04003217 if (pos >= PID_MAX_LIMIT + TGID_OFFSET)
Al Virof0c3b502013-05-16 12:07:31 -04003218 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003219
Eric W. Biederman00978752014-07-31 03:10:50 -07003220 if (pos == TGID_OFFSET - 2) {
David Howells2b0143b2015-03-17 22:25:59 +00003221 struct inode *inode = d_inode(ns->proc_self);
Al Virodb963162013-06-15 10:45:10 +04003222 if (!dir_emit(ctx, "self", 4, inode->i_ino, DT_LNK))
Al Virof0c3b502013-05-16 12:07:31 -04003223 return 0;
Eric W. Biederman00978752014-07-31 03:10:50 -07003224 ctx->pos = pos = pos + 1;
Al Viro021ada72013-03-29 19:27:05 -04003225 }
Eric W. Biederman00978752014-07-31 03:10:50 -07003226 if (pos == TGID_OFFSET - 1) {
David Howells2b0143b2015-03-17 22:25:59 +00003227 struct inode *inode = d_inode(ns->proc_thread_self);
Eric W. Biederman00978752014-07-31 03:10:50 -07003228 if (!dir_emit(ctx, "thread-self", 11, inode->i_ino, DT_LNK))
3229 return 0;
3230 ctx->pos = pos = pos + 1;
3231 }
3232 iter.tgid = pos - TGID_OFFSET;
Eric W. Biederman19fd4bb2007-11-28 16:21:26 -08003233 iter.task = NULL;
Eric W. Biederman19fd4bb2007-11-28 16:21:26 -08003234 for (iter = next_tgid(ns, iter);
3235 iter.task;
3236 iter.tgid += 1, iter = next_tgid(ns, iter)) {
Alexey Dobriyane3912ac2018-02-06 15:36:51 -08003237 char name[10 + 1];
Al Virof0c3b502013-05-16 12:07:31 -04003238 int len;
Eric Dumazet3ba4bce2017-01-24 15:18:07 -08003239
3240 cond_resched();
Lafcadio Wluiki796f5712017-02-24 15:00:23 -08003241 if (!has_pid_permissions(ns, iter.task, HIDEPID_INVISIBLE))
Al Virof0c3b502013-05-16 12:07:31 -04003242 continue;
Vasiliy Kulikov04996802012-01-10 15:11:31 -08003243
Alexey Dobriyane3912ac2018-02-06 15:36:51 -08003244 len = snprintf(name, sizeof(name), "%u", iter.tgid);
Al Virof0c3b502013-05-16 12:07:31 -04003245 ctx->pos = iter.tgid + TGID_OFFSET;
3246 if (!proc_fill_cache(file, ctx, name, len,
3247 proc_pid_instantiate, iter.task, NULL)) {
Eric W. Biederman19fd4bb2007-11-28 16:21:26 -08003248 put_task_struct(iter.task);
Al Virof0c3b502013-05-16 12:07:31 -04003249 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003250 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003251 }
Al Virof0c3b502013-05-16 12:07:31 -04003252 ctx->pos = PID_MAX_LIMIT + TGID_OFFSET;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003253 return 0;
3254}
3255
Eric W. Biederman0bc58a92006-06-26 00:25:50 -07003256/*
Janis Danisevskis1b3044e2016-05-20 17:00:08 -07003257 * proc_tid_comm_permission is a special permission function exclusively
3258 * used for the node /proc/<pid>/task/<tid>/comm.
3259 * It bypasses generic permission checks in the case where a task of the same
3260 * task group attempts to access the node.
3261 * The rationale behind this is that glibc and bionic access this node for
3262 * cross thread naming (pthread_set/getname_np(!self)). However, if
3263 * PR_SET_DUMPABLE gets set to 0 this node among others becomes uid=0 gid=0,
3264 * which locks out the cross thread naming implementation.
3265 * This function makes sure that the node is always accessible for members of
3266 * same thread group.
3267 */
3268static int proc_tid_comm_permission(struct inode *inode, int mask)
3269{
3270 bool is_same_tgroup;
3271 struct task_struct *task;
3272
3273 task = get_proc_task(inode);
3274 if (!task)
3275 return -ESRCH;
3276 is_same_tgroup = same_thread_group(current, task);
3277 put_task_struct(task);
3278
3279 if (likely(is_same_tgroup && !(mask & MAY_EXEC))) {
3280 /* This file (/proc/<pid>/task/<tid>/comm) can always be
3281 * read or written by the members of the corresponding
3282 * thread group.
3283 */
3284 return 0;
3285 }
3286
3287 return generic_permission(inode, mask);
3288}
3289
3290static const struct inode_operations proc_tid_comm_inode_operations = {
3291 .permission = proc_tid_comm_permission,
3292};
3293
3294/*
Eric W. Biederman28a6d672006-10-02 02:17:05 -07003295 * Tasks
3296 */
Eric Dumazetc5141e62007-05-08 00:26:15 -07003297static const struct pid_entry tid_base_stuff[] = {
Alexey Dobriyan631f9c12008-11-10 01:32:52 +03003298 DIR("fd", S_IRUSR|S_IXUSR, proc_fd_inode_operations, proc_fd_operations),
Jerome Marchand38355412010-04-27 13:13:06 -07003299 DIR("fdinfo", S_IRUSR|S_IXUSR, proc_fdinfo_inode_operations, proc_fdinfo_operations),
Eric W. Biederman6b4e3062010-03-07 16:41:34 -08003300 DIR("ns", S_IRUSR|S_IXUGO, proc_ns_dir_inode_operations, proc_ns_dir_operations),
Eric W. Biederman6ba8ed72014-07-31 16:27:08 -07003301#ifdef CONFIG_NET
3302 DIR("net", S_IRUGO|S_IXUGO, proc_net_inode_operations, proc_net_operations),
3303#endif
Alexey Dobriyan631f9c12008-11-10 01:32:52 +03003304 REG("environ", S_IRUSR, proc_environ_operations),
Al Viroc5317162016-10-05 18:43:43 -04003305 REG("auxv", S_IRUSR, proc_auxv_operations),
Alexey Dobriyan631f9c12008-11-10 01:32:52 +03003306 ONE("status", S_IRUGO, proc_pid_status),
Djalal Harouni35a35042014-04-07 15:38:36 -07003307 ONE("personality", S_IRUSR, proc_pid_personality),
Alexey Dobriyan1c963eb2014-08-08 14:21:37 -07003308 ONE("limits", S_IRUGO, proc_pid_limits),
Ingo Molnar43ae34c2007-07-09 18:52:00 +02003309#ifdef CONFIG_SCHED_DEBUG
Alexey Dobriyan631f9c12008-11-10 01:32:52 +03003310 REG("sched", S_IRUGO|S_IWUSR, proc_pid_sched_operations),
Ingo Molnar43ae34c2007-07-09 18:52:00 +02003311#endif
Janis Danisevskis1b3044e2016-05-20 17:00:08 -07003312 NOD("comm", S_IFREG|S_IRUGO|S_IWUSR,
3313 &proc_tid_comm_inode_operations,
3314 &proc_pid_set_comm_operations, {}),
Roland McGrathebcb6732008-07-25 19:46:00 -07003315#ifdef CONFIG_HAVE_ARCH_TRACEHOOK
Alexey Dobriyan09d93bd2014-08-08 14:21:39 -07003316 ONE("syscall", S_IRUSR, proc_pid_syscall),
Roland McGrathebcb6732008-07-25 19:46:00 -07003317#endif
Alexey Dobriyanc2c0bb42015-06-25 15:00:54 -07003318 REG("cmdline", S_IRUGO, proc_pid_cmdline_ops),
Alexey Dobriyan631f9c12008-11-10 01:32:52 +03003319 ONE("stat", S_IRUGO, proc_tid_stat),
3320 ONE("statm", S_IRUGO, proc_pid_statm),
Siddhesh Poyarekarb7643752012-03-21 16:34:04 -07003321 REG("maps", S_IRUGO, proc_tid_maps_operations),
Iago López Galeiras2e13ba52015-06-25 15:00:57 -07003322#ifdef CONFIG_PROC_CHILDREN
Cyrill Gorcunov818411612012-05-31 16:26:43 -07003323 REG("children", S_IRUGO, proc_tid_children_operations),
3324#endif
Eric W. Biederman28a6d672006-10-02 02:17:05 -07003325#ifdef CONFIG_NUMA
Siddhesh Poyarekarb7643752012-03-21 16:34:04 -07003326 REG("numa_maps", S_IRUGO, proc_tid_numa_maps_operations),
Eric W. Biederman28a6d672006-10-02 02:17:05 -07003327#endif
Alexey Dobriyan631f9c12008-11-10 01:32:52 +03003328 REG("mem", S_IRUSR|S_IWUSR, proc_mem_operations),
3329 LNK("cwd", proc_cwd_link),
3330 LNK("root", proc_root_link),
3331 LNK("exe", proc_exe_link),
3332 REG("mounts", S_IRUGO, proc_mounts_operations),
3333 REG("mountinfo", S_IRUGO, proc_mountinfo_operations),
Matt Mackall1e883282008-02-04 22:29:07 -08003334#ifdef CONFIG_PROC_PAGE_MONITOR
Alexey Dobriyan631f9c12008-11-10 01:32:52 +03003335 REG("clear_refs", S_IWUSR, proc_clear_refs_operations),
Siddhesh Poyarekarb7643752012-03-21 16:34:04 -07003336 REG("smaps", S_IRUGO, proc_tid_smaps_operations),
Daniel Colascione493b0e92017-09-06 16:25:08 -07003337 REG("smaps_rollup", S_IRUGO, proc_pid_smaps_rollup_operations),
Djalal Harouni32ed74a2014-04-07 15:38:38 -07003338 REG("pagemap", S_IRUSR, proc_pagemap_operations),
Eric W. Biederman28a6d672006-10-02 02:17:05 -07003339#endif
3340#ifdef CONFIG_SECURITY
Alexey Dobriyan631f9c12008-11-10 01:32:52 +03003341 DIR("attr", S_IRUGO|S_IXUGO, proc_attr_dir_inode_operations, proc_attr_dir_operations),
Eric W. Biederman28a6d672006-10-02 02:17:05 -07003342#endif
3343#ifdef CONFIG_KALLSYMS
Alexey Dobriyanedfcd602014-08-08 14:21:44 -07003344 ONE("wchan", S_IRUGO, proc_pid_wchan),
Eric W. Biederman28a6d672006-10-02 02:17:05 -07003345#endif
Ken Chen2ec220e2008-11-10 11:26:08 +03003346#ifdef CONFIG_STACKTRACE
Djalal Harouni35a35042014-04-07 15:38:36 -07003347 ONE("stack", S_IRUSR, proc_pid_stack),
Eric W. Biederman28a6d672006-10-02 02:17:05 -07003348#endif
Naveen N. Rao5968cec2015-06-30 14:36:03 +05303349#ifdef CONFIG_SCHED_INFO
Alexey Dobriyanf6e826c2014-08-08 14:21:46 -07003350 ONE("schedstat", S_IRUGO, proc_pid_schedstat),
Eric W. Biederman28a6d672006-10-02 02:17:05 -07003351#endif
Arjan van de Ven97455122008-01-25 21:08:34 +01003352#ifdef CONFIG_LATENCYTOP
Alexey Dobriyan631f9c12008-11-10 01:32:52 +03003353 REG("latency", S_IRUGO, proc_lstats_operations),
Arjan van de Ven97455122008-01-25 21:08:34 +01003354#endif
Paul Menage8793d852007-10-18 23:39:39 -07003355#ifdef CONFIG_PROC_PID_CPUSET
Zefan Li52de4772014-09-18 16:03:36 +08003356 ONE("cpuset", S_IRUGO, proc_cpuset_show),
Eric W. Biederman28a6d672006-10-02 02:17:05 -07003357#endif
Paul Menagea4243162007-10-18 23:39:35 -07003358#ifdef CONFIG_CGROUPS
Zefan Li006f4ac2014-09-18 16:03:15 +08003359 ONE("cgroup", S_IRUGO, proc_cgroup_show),
Paul Menagea4243162007-10-18 23:39:35 -07003360#endif
Alexey Dobriyan6ba51e32014-08-08 14:21:48 -07003361 ONE("oom_score", S_IRUGO, proc_oom_score),
David Rientjesfa0cbbf2012-11-12 17:53:04 -08003362 REG("oom_adj", S_IRUGO|S_IWUSR, proc_oom_adj_operations),
David Rientjesa63d83f2010-08-09 17:19:46 -07003363 REG("oom_score_adj", S_IRUGO|S_IWUSR, proc_oom_score_adj_operations),
Eric W. Biederman28a6d672006-10-02 02:17:05 -07003364#ifdef CONFIG_AUDITSYSCALL
Alexey Dobriyan631f9c12008-11-10 01:32:52 +03003365 REG("loginuid", S_IWUSR|S_IRUGO, proc_loginuid_operations),
Al Viro26ec3c62011-02-15 21:24:05 -05003366 REG("sessionid", S_IRUGO, proc_sessionid_operations),
Eric W. Biederman28a6d672006-10-02 02:17:05 -07003367#endif
Akinobu Mitaf4f154f2006-12-08 02:39:47 -08003368#ifdef CONFIG_FAULT_INJECTION
Alexey Dobriyan631f9c12008-11-10 01:32:52 +03003369 REG("make-it-fail", S_IRUGO|S_IWUSR, proc_fault_inject_operations),
Akinobu Mita1203c8e2017-07-14 14:49:57 -07003370 REG("fail-nth", 0644, proc_fail_nth_operations),
Akinobu Mitaf4f154f2006-12-08 02:39:47 -08003371#endif
Andrea Righi297c5d92008-07-25 01:48:49 -07003372#ifdef CONFIG_TASK_IO_ACCOUNTING
Alexey Dobriyan19aadc92014-08-08 14:21:50 -07003373 ONE("io", S_IRUSR, proc_tid_io_accounting),
Andrea Righi297c5d92008-07-25 01:48:49 -07003374#endif
Eric W. Biederman22d917d2011-11-17 00:11:58 -08003375#ifdef CONFIG_USER_NS
3376 REG("uid_map", S_IRUGO|S_IWUSR, proc_uid_map_operations),
3377 REG("gid_map", S_IRUGO|S_IWUSR, proc_gid_map_operations),
Eric W. Biedermanf76d2072012-08-30 01:24:05 -07003378 REG("projid_map", S_IRUGO|S_IWUSR, proc_projid_map_operations),
Eric W. Biederman9cc46512014-12-02 12:27:26 -06003379 REG("setgroups", S_IRUGO|S_IWUSR, proc_setgroups_operations),
Eric W. Biederman22d917d2011-11-17 00:11:58 -08003380#endif
Josh Poimboeuf7c23b332017-02-13 19:42:41 -06003381#ifdef CONFIG_LIVEPATCH
3382 ONE("patch_state", S_IRUSR, proc_pid_patch_state),
3383#endif
Eric W. Biederman28a6d672006-10-02 02:17:05 -07003384};
3385
Al Virof0c3b502013-05-16 12:07:31 -04003386static int proc_tid_base_readdir(struct file *file, struct dir_context *ctx)
Eric W. Biederman28a6d672006-10-02 02:17:05 -07003387{
Al Virof0c3b502013-05-16 12:07:31 -04003388 return proc_pident_readdir(file, ctx,
3389 tid_base_stuff, ARRAY_SIZE(tid_base_stuff));
Eric W. Biederman28a6d672006-10-02 02:17:05 -07003390}
3391
Al Viro00cd8dd2012-06-10 17:13:09 -04003392static struct dentry *proc_tid_base_lookup(struct inode *dir, struct dentry *dentry, unsigned int flags)
3393{
Eric W. Biederman7bcd6b02006-10-02 02:18:56 -07003394 return proc_pident_lookup(dir, dentry,
3395 tid_base_stuff, ARRAY_SIZE(tid_base_stuff));
Eric W. Biederman28a6d672006-10-02 02:17:05 -07003396}
3397
Arjan van de Ven00977a52007-02-12 00:55:34 -08003398static const struct file_operations proc_tid_base_operations = {
Eric W. Biederman28a6d672006-10-02 02:17:05 -07003399 .read = generic_read_dir,
Al Virof50752e2016-04-20 17:13:54 -04003400 .iterate_shared = proc_tid_base_readdir,
3401 .llseek = generic_file_llseek,
Eric W. Biederman28a6d672006-10-02 02:17:05 -07003402};
3403
Arjan van de Venc5ef1c42007-02-12 00:55:40 -08003404static const struct inode_operations proc_tid_base_inode_operations = {
Eric W. Biederman28a6d672006-10-02 02:17:05 -07003405 .lookup = proc_tid_base_lookup,
3406 .getattr = pid_getattr,
3407 .setattr = proc_setattr,
3408};
3409
Al Viro0168b9e2018-05-03 09:21:05 -04003410static struct dentry *proc_task_instantiate(struct dentry *dentry,
3411 struct task_struct *task, const void *ptr)
Eric W. Biederman444ceed2006-10-02 02:18:49 -07003412{
Eric W. Biederman444ceed2006-10-02 02:18:49 -07003413 struct inode *inode;
Al Viro0168b9e2018-05-03 09:21:05 -04003414 inode = proc_pid_make_inode(dentry->d_sb, task, S_IFDIR | S_IRUGO | S_IXUGO);
Eric W. Biederman444ceed2006-10-02 02:18:49 -07003415 if (!inode)
Al Viro0168b9e2018-05-03 09:21:05 -04003416 return ERR_PTR(-ENOENT);
Al Viro1bbc5512018-05-02 21:26:16 -04003417
Eric W. Biederman444ceed2006-10-02 02:18:49 -07003418 inode->i_op = &proc_tid_base_inode_operations;
3419 inode->i_fop = &proc_tid_base_operations;
Al Viro1bbc5512018-05-02 21:26:16 -04003420 inode->i_flags |= S_IMMUTABLE;
Vegard Nossumaed54172008-06-05 22:46:53 -07003421
Alexey Dobriyan1270dd82016-12-12 16:45:32 -08003422 set_nlink(inode, nlink_tid);
Al Viro1bbc5512018-05-02 21:26:16 -04003423 pid_update_inode(task, inode);
Eric W. Biederman444ceed2006-10-02 02:18:49 -07003424
Nick Pigginfb045ad2011-01-07 17:49:55 +11003425 d_set_d_op(dentry, &pid_dentry_operations);
Al Viro0168b9e2018-05-03 09:21:05 -04003426 return d_splice_alias(inode, dentry);
Eric W. Biederman444ceed2006-10-02 02:18:49 -07003427}
3428
Al Viro00cd8dd2012-06-10 17:13:09 -04003429static struct dentry *proc_task_lookup(struct inode *dir, struct dentry * dentry, unsigned int flags)
Eric W. Biederman28a6d672006-10-02 02:17:05 -07003430{
Eric W. Biederman28a6d672006-10-02 02:17:05 -07003431 struct task_struct *task;
3432 struct task_struct *leader = get_proc_task(dir);
Eric W. Biederman28a6d672006-10-02 02:17:05 -07003433 unsigned tid;
Pavel Emelyanovb4888932007-10-18 23:40:14 -07003434 struct pid_namespace *ns;
Al Viro0168b9e2018-05-03 09:21:05 -04003435 struct dentry *result = ERR_PTR(-ENOENT);
Eric W. Biederman28a6d672006-10-02 02:17:05 -07003436
3437 if (!leader)
3438 goto out_no_task;
3439
Alexey Dobriyandbcdb502014-08-08 14:21:25 -07003440 tid = name_to_int(&dentry->d_name);
Eric W. Biederman28a6d672006-10-02 02:17:05 -07003441 if (tid == ~0U)
3442 goto out;
3443
Pavel Emelyanovb4888932007-10-18 23:40:14 -07003444 ns = dentry->d_sb->s_fs_info;
Eric W. Biederman28a6d672006-10-02 02:17:05 -07003445 rcu_read_lock();
Pavel Emelyanovb4888932007-10-18 23:40:14 -07003446 task = find_task_by_pid_ns(tid, ns);
Eric W. Biederman28a6d672006-10-02 02:17:05 -07003447 if (task)
3448 get_task_struct(task);
3449 rcu_read_unlock();
3450 if (!task)
3451 goto out;
Pavel Emelyanovbac0abd2007-10-18 23:40:18 -07003452 if (!same_thread_group(leader, task))
Eric W. Biederman28a6d672006-10-02 02:17:05 -07003453 goto out_drop_task;
3454
Al Viro0168b9e2018-05-03 09:21:05 -04003455 result = proc_task_instantiate(dentry, task, NULL);
Eric W. Biederman28a6d672006-10-02 02:17:05 -07003456out_drop_task:
3457 put_task_struct(task);
3458out:
3459 put_task_struct(leader);
3460out_no_task:
Al Viro0168b9e2018-05-03 09:21:05 -04003461 return result;
Eric W. Biederman28a6d672006-10-02 02:17:05 -07003462}
3463
3464/*
Eric W. Biederman0bc58a92006-06-26 00:25:50 -07003465 * Find the first tid of a thread group to return to user space.
3466 *
3467 * Usually this is just the thread group leader, but if the users
3468 * buffer was too small or there was a seek into the middle of the
3469 * directory we have more work todo.
3470 *
3471 * In the case of a short read we start with find_task_by_pid.
3472 *
3473 * In the case of a seek we start with the leader and walk nr
3474 * threads past it.
3475 */
Oleg Nesterov9f6e9632014-01-23 15:55:40 -08003476static struct task_struct *first_tid(struct pid *pid, int tid, loff_t f_pos,
3477 struct pid_namespace *ns)
Eric W. Biederman0bc58a92006-06-26 00:25:50 -07003478{
Oleg Nesterovd855a4b2014-01-23 15:55:39 -08003479 struct task_struct *pos, *task;
Oleg Nesterov9f6e9632014-01-23 15:55:40 -08003480 unsigned long nr = f_pos;
3481
3482 if (nr != f_pos) /* 32bit overflow? */
3483 return NULL;
Eric W. Biederman0bc58a92006-06-26 00:25:50 -07003484
Eric W. Biedermancc288732006-06-26 00:26:01 -07003485 rcu_read_lock();
Oleg Nesterovd855a4b2014-01-23 15:55:39 -08003486 task = pid_task(pid, PIDTYPE_PID);
3487 if (!task)
3488 goto fail;
3489
3490 /* Attempt to start with the tid of a thread */
Oleg Nesterov9f6e9632014-01-23 15:55:40 -08003491 if (tid && nr) {
Pavel Emelyanovb4888932007-10-18 23:40:14 -07003492 pos = find_task_by_pid_ns(tid, ns);
Oleg Nesterovd855a4b2014-01-23 15:55:39 -08003493 if (pos && same_thread_group(pos, task))
Oleg Nesterova872ff02006-06-26 00:26:01 -07003494 goto found;
Eric W. Biederman0bc58a92006-06-26 00:25:50 -07003495 }
3496
3497 /* If nr exceeds the number of threads there is nothing todo */
Oleg Nesterov9f6e9632014-01-23 15:55:40 -08003498 if (nr >= get_nr_threads(task))
Oleg Nesterovc986c142014-01-23 15:55:38 -08003499 goto fail;
Oleg Nesterova872ff02006-06-26 00:26:01 -07003500
3501 /* If we haven't found our starting place yet start
3502 * with the leader and walk nr threads forward.
3503 */
Oleg Nesterovd855a4b2014-01-23 15:55:39 -08003504 pos = task = task->group_leader;
Oleg Nesterovc986c142014-01-23 15:55:38 -08003505 do {
Oleg Nesterov9f6e9632014-01-23 15:55:40 -08003506 if (!nr--)
Oleg Nesterovc986c142014-01-23 15:55:38 -08003507 goto found;
Oleg Nesterovd855a4b2014-01-23 15:55:39 -08003508 } while_each_thread(task, pos);
Oleg Nesterovc986c142014-01-23 15:55:38 -08003509fail:
3510 pos = NULL;
3511 goto out;
Oleg Nesterova872ff02006-06-26 00:26:01 -07003512found:
3513 get_task_struct(pos);
3514out:
Eric W. Biedermancc288732006-06-26 00:26:01 -07003515 rcu_read_unlock();
Eric W. Biederman0bc58a92006-06-26 00:25:50 -07003516 return pos;
3517}
3518
3519/*
3520 * Find the next thread in the thread list.
3521 * Return NULL if there is an error or no next thread.
3522 *
3523 * The reference to the input task_struct is released.
3524 */
3525static struct task_struct *next_tid(struct task_struct *start)
3526{
Oleg Nesterovc1df7fb2006-06-26 00:26:02 -07003527 struct task_struct *pos = NULL;
Eric W. Biedermancc288732006-06-26 00:26:01 -07003528 rcu_read_lock();
Oleg Nesterovc1df7fb2006-06-26 00:26:02 -07003529 if (pid_alive(start)) {
Eric W. Biederman0bc58a92006-06-26 00:25:50 -07003530 pos = next_thread(start);
Oleg Nesterovc1df7fb2006-06-26 00:26:02 -07003531 if (thread_group_leader(pos))
3532 pos = NULL;
3533 else
3534 get_task_struct(pos);
3535 }
Eric W. Biedermancc288732006-06-26 00:26:01 -07003536 rcu_read_unlock();
Eric W. Biederman0bc58a92006-06-26 00:25:50 -07003537 put_task_struct(start);
3538 return pos;
3539}
3540
Linus Torvalds1da177e2005-04-16 15:20:36 -07003541/* for the /proc/TGID/task/ directories */
Al Virof0c3b502013-05-16 12:07:31 -04003542static int proc_task_readdir(struct file *file, struct dir_context *ctx)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003543{
Oleg Nesterovd855a4b2014-01-23 15:55:39 -08003544 struct inode *inode = file_inode(file);
3545 struct task_struct *task;
Pavel Emelyanovb4888932007-10-18 23:40:14 -07003546 struct pid_namespace *ns;
Al Virof0c3b502013-05-16 12:07:31 -04003547 int tid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003548
Oleg Nesterovd855a4b2014-01-23 15:55:39 -08003549 if (proc_inode_is_dead(inode))
Al Virof0c3b502013-05-16 12:07:31 -04003550 return -ENOENT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003551
Al Virof0c3b502013-05-16 12:07:31 -04003552 if (!dir_emit_dots(file, ctx))
Oleg Nesterovd855a4b2014-01-23 15:55:39 -08003553 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003554
Eric W. Biederman0bc58a92006-06-26 00:25:50 -07003555 /* f_version caches the tgid value that the last readdir call couldn't
3556 * return. lseek aka telldir automagically resets f_version to 0.
3557 */
Christoph Hellwig76f668b2018-05-16 07:19:01 +02003558 ns = proc_pid_ns(inode);
Al Virof0c3b502013-05-16 12:07:31 -04003559 tid = (int)file->f_version;
3560 file->f_version = 0;
Oleg Nesterovd855a4b2014-01-23 15:55:39 -08003561 for (task = first_tid(proc_pid(inode), tid, ctx->pos - 2, ns);
Eric W. Biederman0bc58a92006-06-26 00:25:50 -07003562 task;
Al Virof0c3b502013-05-16 12:07:31 -04003563 task = next_tid(task), ctx->pos++) {
Alexey Dobriyane3912ac2018-02-06 15:36:51 -08003564 char name[10 + 1];
Al Virof0c3b502013-05-16 12:07:31 -04003565 int len;
Pavel Emelyanovb4888932007-10-18 23:40:14 -07003566 tid = task_pid_nr_ns(task, ns);
Alexey Dobriyane3912ac2018-02-06 15:36:51 -08003567 len = snprintf(name, sizeof(name), "%u", tid);
Al Virof0c3b502013-05-16 12:07:31 -04003568 if (!proc_fill_cache(file, ctx, name, len,
3569 proc_task_instantiate, task, NULL)) {
Eric W. Biederman0bc58a92006-06-26 00:25:50 -07003570 /* returning this tgid failed, save it as the first
3571 * pid for the next readir call */
Al Virof0c3b502013-05-16 12:07:31 -04003572 file->f_version = (u64)tid;
Eric W. Biederman0bc58a92006-06-26 00:25:50 -07003573 put_task_struct(task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003574 break;
Eric W. Biederman0bc58a92006-06-26 00:25:50 -07003575 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003576 }
Oleg Nesterovd855a4b2014-01-23 15:55:39 -08003577
Al Virof0c3b502013-05-16 12:07:31 -04003578 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003579}
Eric W. Biederman6e66b522006-06-26 00:25:47 -07003580
David Howellsa528d352017-01-31 16:46:22 +00003581static int proc_task_getattr(const struct path *path, struct kstat *stat,
3582 u32 request_mask, unsigned int query_flags)
Eric W. Biederman6e66b522006-06-26 00:25:47 -07003583{
David Howellsa528d352017-01-31 16:46:22 +00003584 struct inode *inode = d_inode(path->dentry);
Eric W. Biederman99f89552006-06-26 00:25:55 -07003585 struct task_struct *p = get_proc_task(inode);
Eric W. Biederman6e66b522006-06-26 00:25:47 -07003586 generic_fillattr(inode, stat);
3587
Eric W. Biederman99f89552006-06-26 00:25:55 -07003588 if (p) {
Eric W. Biederman99f89552006-06-26 00:25:55 -07003589 stat->nlink += get_nr_threads(p);
Eric W. Biederman99f89552006-06-26 00:25:55 -07003590 put_task_struct(p);
Eric W. Biederman6e66b522006-06-26 00:25:47 -07003591 }
3592
3593 return 0;
3594}
Eric W. Biederman28a6d672006-10-02 02:17:05 -07003595
Arjan van de Venc5ef1c42007-02-12 00:55:40 -08003596static const struct inode_operations proc_task_inode_operations = {
Eric W. Biederman28a6d672006-10-02 02:17:05 -07003597 .lookup = proc_task_lookup,
3598 .getattr = proc_task_getattr,
3599 .setattr = proc_setattr,
Vasiliy Kulikov04996802012-01-10 15:11:31 -08003600 .permission = proc_pid_permission,
Eric W. Biederman28a6d672006-10-02 02:17:05 -07003601};
3602
Arjan van de Ven00977a52007-02-12 00:55:34 -08003603static const struct file_operations proc_task_operations = {
Eric W. Biederman28a6d672006-10-02 02:17:05 -07003604 .read = generic_read_dir,
Al Virof50752e2016-04-20 17:13:54 -04003605 .iterate_shared = proc_task_readdir,
3606 .llseek = generic_file_llseek,
Eric W. Biederman28a6d672006-10-02 02:17:05 -07003607};
Alexey Dobriyan1270dd82016-12-12 16:45:32 -08003608
3609void __init set_proc_pid_nlink(void)
3610{
3611 nlink_tid = pid_entry_nlink(tid_base_stuff, ARRAY_SIZE(tid_base_stuff));
3612 nlink_tgid = pid_entry_nlink(tgid_base_stuff, ARRAY_SIZE(tgid_base_stuff));
3613}