blob: 159abd09b411fc841ebac4dc30ebe799b7ac8ee4 [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;
218 char c;
219 ssize_t rv;
220
221 BUG_ON(*pos < 0);
222
223 tsk = get_proc_task(file_inode(file));
224 if (!tsk)
225 return -ESRCH;
226 mm = get_task_mm(tsk);
227 put_task_struct(tsk);
228 if (!mm)
229 return 0;
230 /* Check if process spawned far enough to have cmdline. */
231 if (!mm->env_end) {
232 rv = 0;
233 goto out_mmput;
234 }
235
Michal Hocko0ee931c2017-09-13 16:28:29 -0700236 page = (char *)__get_free_page(GFP_KERNEL);
Alexey Dobriyanc2c0bb42015-06-25 15:00:54 -0700237 if (!page) {
238 rv = -ENOMEM;
239 goto out_mmput;
240 }
241
Yang Shi88aa7cc2018-06-07 17:05:28 -0700242 spin_lock(&mm->arg_lock);
Alexey Dobriyanc2c0bb42015-06-25 15:00:54 -0700243 arg_start = mm->arg_start;
244 arg_end = mm->arg_end;
245 env_start = mm->env_start;
246 env_end = mm->env_end;
Yang Shi88aa7cc2018-06-07 17:05:28 -0700247 spin_unlock(&mm->arg_lock);
Alexey Dobriyanc2c0bb42015-06-25 15:00:54 -0700248
249 BUG_ON(arg_start > arg_end);
250 BUG_ON(env_start > env_end);
251
252 len1 = arg_end - arg_start;
253 len2 = env_end - env_start;
254
Alexey Dobriyan3581d452015-07-17 16:24:09 -0700255 /* Empty ARGV. */
256 if (len1 == 0) {
257 rv = 0;
258 goto out_free_page;
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 Dobriyanb42262a2018-06-07 17:09:52 -0700264 if (access_remote_vm(mm, arg_end - 1, &c, 1, FOLL_ANON) != 1) {
265 rv = 0;
Alexey Dobriyanc2c0bb42015-06-25 15:00:54 -0700266 goto out_free_page;
Alexey Dobriyanb42262a2018-06-07 17:09:52 -0700267 }
Alexey Dobriyanc2c0bb42015-06-25 15:00:54 -0700268
269 rv = 0;
270
271 if (c == '\0') {
272 /* Command line (set of strings) occupies whole ARGV. */
273 if (len1 <= *pos)
274 goto out_free_page;
275
276 p = arg_start + *pos;
277 len = len1 - *pos;
278 while (count > 0 && len > 0) {
Alexey Dobriyanb42262a2018-06-07 17:09:52 -0700279 unsigned int nr_read;
Alexey Dobriyanc2c0bb42015-06-25 15:00:54 -0700280
Alexey Dobriyanb42262a2018-06-07 17:09:52 -0700281 nr_read = min3(count, len, PAGE_SIZE);
282 nr_read = access_remote_vm(mm, p, page, nr_read, FOLL_ANON);
283 if (nr_read == 0)
Alexey Dobriyanc2c0bb42015-06-25 15:00:54 -0700284 goto out_free_page;
285
286 if (copy_to_user(buf, page, nr_read)) {
287 rv = -EFAULT;
288 goto out_free_page;
289 }
290
291 p += nr_read;
292 len -= nr_read;
293 buf += nr_read;
294 count -= nr_read;
295 rv += nr_read;
296 }
297 } else {
298 /*
Alexey Dobriyanc2c0bb42015-06-25 15:00:54 -0700299 * Command line (1 string) occupies ARGV and
300 * extends into ENVP.
301 */
Alexey Dobriyana0a07b82017-02-24 15:00:20 -0800302 struct {
303 unsigned long p;
304 unsigned long len;
305 } cmdline[2] = {
306 { .p = arg_start, .len = len1 },
307 { .p = env_start, .len = len2 },
308 };
309 loff_t pos1 = *pos;
310 unsigned int i;
311
312 i = 0;
313 while (i < 2 && pos1 >= cmdline[i].len) {
314 pos1 -= cmdline[i].len;
315 i++;
Alexey Dobriyanc2c0bb42015-06-25 15:00:54 -0700316 }
Alexey Dobriyana0a07b82017-02-24 15:00:20 -0800317 while (i < 2) {
318 p = cmdline[i].p + pos1;
319 len = cmdline[i].len - pos1;
320 while (count > 0 && len > 0) {
Alexey Dobriyanb42262a2018-06-07 17:09:52 -0700321 unsigned int nr_read, l;
Alexey Dobriyana0a07b82017-02-24 15:00:20 -0800322 bool final;
Alexey Dobriyanc2c0bb42015-06-25 15:00:54 -0700323
Alexey Dobriyanb42262a2018-06-07 17:09:52 -0700324 nr_read = min3(count, len, PAGE_SIZE);
325 nr_read = access_remote_vm(mm, p, page, nr_read, FOLL_ANON);
326 if (nr_read == 0)
Alexey Dobriyana0a07b82017-02-24 15:00:20 -0800327 goto out_free_page;
Alexey Dobriyanc2c0bb42015-06-25 15:00:54 -0700328
Alexey Dobriyana0a07b82017-02-24 15:00:20 -0800329 /*
330 * Command line can be shorter than whole ARGV
331 * even if last "marker" byte says it is not.
332 */
333 final = false;
334 l = strnlen(page, nr_read);
335 if (l < nr_read) {
336 nr_read = l;
337 final = true;
338 }
339
340 if (copy_to_user(buf, page, nr_read)) {
341 rv = -EFAULT;
342 goto out_free_page;
343 }
344
345 p += nr_read;
346 len -= nr_read;
347 buf += nr_read;
348 count -= nr_read;
349 rv += nr_read;
350
351 if (final)
352 goto out_free_page;
Alexey Dobriyanc2c0bb42015-06-25 15:00:54 -0700353 }
354
Alexey Dobriyana0a07b82017-02-24 15:00:20 -0800355 /* Only first chunk can be read partially. */
356 pos1 = 0;
357 i++;
Alexey Dobriyanc2c0bb42015-06-25 15:00:54 -0700358 }
Alexey Dobriyanc2c0bb42015-06-25 15:00:54 -0700359 }
360
361out_free_page:
362 free_page((unsigned long)page);
363out_mmput:
364 mmput(mm);
365 if (rv > 0)
366 *pos += rv;
367 return rv;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700368}
369
Alexey Dobriyanc2c0bb42015-06-25 15:00:54 -0700370static const struct file_operations proc_pid_cmdline_ops = {
371 .read = proc_pid_cmdline_read,
372 .llseek = generic_file_llseek,
373};
374
Linus Torvalds1da177e2005-04-16 15:20:36 -0700375#ifdef CONFIG_KALLSYMS
376/*
377 * Provides a wchan file via kallsyms in a proper one-value-per-file format.
378 * Returns the resolved symbol. If that fails, simply return the address.
379 */
Alexey Dobriyanedfcd602014-08-08 14:21:44 -0700380static int proc_pid_wchan(struct seq_file *m, struct pid_namespace *ns,
381 struct pid *pid, struct task_struct *task)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700382{
Alexey Dobriyanffb45122007-05-08 00:28:41 -0700383 unsigned long wchan;
Tejun Heo9281ace2007-07-17 04:03:51 -0700384 char symname[KSYM_NAME_LEN];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700385
Alexey Dobriyan24b2ec22018-04-10 16:31:30 -0700386 if (!ptrace_may_access(task, PTRACE_MODE_READ_FSCREDS))
387 goto print0;
388
Linus Torvalds1da177e2005-04-16 15:20:36 -0700389 wchan = get_wchan(task);
Alexey Dobriyan24b2ec22018-04-10 16:31:30 -0700390 if (wchan && !lookup_symbol_name(wchan, symname)) {
Alexey Dobriyan21dae0a2018-04-10 16:31:34 -0700391 seq_puts(m, symname);
Alexey Dobriyan24b2ec22018-04-10 16:31:30 -0700392 return 0;
393 }
Joe Perches25ce3192015-04-15 16:18:17 -0700394
Alexey Dobriyan24b2ec22018-04-10 16:31:30 -0700395print0:
396 seq_putc(m, '0');
Joe Perches25ce3192015-04-15 16:18:17 -0700397 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700398}
399#endif /* CONFIG_KALLSYMS */
400
Al Viroa9712bc2011-03-23 15:52:50 -0400401static int lock_trace(struct task_struct *task)
402{
403 int err = mutex_lock_killable(&task->signal->cred_guard_mutex);
404 if (err)
405 return err;
Jann Horncaaee622016-01-20 15:00:04 -0800406 if (!ptrace_may_access(task, PTRACE_MODE_ATTACH_FSCREDS)) {
Al Viroa9712bc2011-03-23 15:52:50 -0400407 mutex_unlock(&task->signal->cred_guard_mutex);
408 return -EPERM;
409 }
410 return 0;
411}
412
413static void unlock_trace(struct task_struct *task)
414{
415 mutex_unlock(&task->signal->cred_guard_mutex);
416}
417
Ken Chen2ec220e2008-11-10 11:26:08 +0300418#ifdef CONFIG_STACKTRACE
419
420#define MAX_STACK_TRACE_DEPTH 64
421
422static int proc_pid_stack(struct seq_file *m, struct pid_namespace *ns,
423 struct pid *pid, struct task_struct *task)
424{
425 struct stack_trace trace;
426 unsigned long *entries;
Al Viroa9712bc2011-03-23 15:52:50 -0400427 int err;
Ken Chen2ec220e2008-11-10 11:26:08 +0300428 int i;
429
430 entries = kmalloc(MAX_STACK_TRACE_DEPTH * sizeof(*entries), GFP_KERNEL);
431 if (!entries)
432 return -ENOMEM;
433
434 trace.nr_entries = 0;
435 trace.max_entries = MAX_STACK_TRACE_DEPTH;
436 trace.entries = entries;
437 trace.skip = 0;
Ken Chen2ec220e2008-11-10 11:26:08 +0300438
Al Viroa9712bc2011-03-23 15:52:50 -0400439 err = lock_trace(task);
440 if (!err) {
441 save_stack_trace_tsk(task, &trace);
442
443 for (i = 0; i < trace.nr_entries; i++) {
Linus Torvalds8f5abe82017-11-27 16:45:56 -0800444 seq_printf(m, "[<0>] %pB\n", (void *)entries[i]);
Al Viroa9712bc2011-03-23 15:52:50 -0400445 }
446 unlock_trace(task);
Ken Chen2ec220e2008-11-10 11:26:08 +0300447 }
448 kfree(entries);
449
Al Viroa9712bc2011-03-23 15:52:50 -0400450 return err;
Ken Chen2ec220e2008-11-10 11:26:08 +0300451}
452#endif
453
Naveen N. Rao5968cec2015-06-30 14:36:03 +0530454#ifdef CONFIG_SCHED_INFO
Linus Torvalds1da177e2005-04-16 15:20:36 -0700455/*
456 * Provides /proc/PID/schedstat
457 */
Alexey Dobriyanf6e826c2014-08-08 14:21:46 -0700458static int proc_pid_schedstat(struct seq_file *m, struct pid_namespace *ns,
459 struct pid *pid, struct task_struct *task)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700460{
Naveen N. Rao5968cec2015-06-30 14:36:03 +0530461 if (unlikely(!sched_info_on()))
462 seq_printf(m, "0 0 0\n");
463 else
464 seq_printf(m, "%llu %llu %lu\n",
Joe Perches25ce3192015-04-15 16:18:17 -0700465 (unsigned long long)task->se.sum_exec_runtime,
466 (unsigned long long)task->sched_info.run_delay,
467 task->sched_info.pcount);
468
469 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700470}
471#endif
472
Arjan van de Ven97455122008-01-25 21:08:34 +0100473#ifdef CONFIG_LATENCYTOP
474static int lstats_show_proc(struct seq_file *m, void *v)
475{
476 int i;
Hiroshi Shimamoto13d77c32008-02-20 16:53:29 -0800477 struct inode *inode = m->private;
478 struct task_struct *task = get_proc_task(inode);
Arjan van de Ven97455122008-01-25 21:08:34 +0100479
Hiroshi Shimamoto13d77c32008-02-20 16:53:29 -0800480 if (!task)
481 return -ESRCH;
482 seq_puts(m, "Latency Top version : v0.1\n");
Arjan van de Ven97455122008-01-25 21:08:34 +0100483 for (i = 0; i < 32; i++) {
Joe Perches34e49d42011-01-12 17:00:30 -0800484 struct latency_record *lr = &task->latency_record[i];
485 if (lr->backtrace[0]) {
Arjan van de Ven97455122008-01-25 21:08:34 +0100486 int q;
Joe Perches34e49d42011-01-12 17:00:30 -0800487 seq_printf(m, "%i %li %li",
488 lr->count, lr->time, lr->max);
Arjan van de Ven97455122008-01-25 21:08:34 +0100489 for (q = 0; q < LT_BACKTRACEDEPTH; q++) {
Joe Perches34e49d42011-01-12 17:00:30 -0800490 unsigned long bt = lr->backtrace[q];
491 if (!bt)
Arjan van de Ven97455122008-01-25 21:08:34 +0100492 break;
Joe Perches34e49d42011-01-12 17:00:30 -0800493 if (bt == ULONG_MAX)
Arjan van de Ven97455122008-01-25 21:08:34 +0100494 break;
Joe Perches34e49d42011-01-12 17:00:30 -0800495 seq_printf(m, " %ps", (void *)bt);
Arjan van de Ven97455122008-01-25 21:08:34 +0100496 }
Alexey Dobriyan9d6de122011-01-12 17:00:32 -0800497 seq_putc(m, '\n');
Arjan van de Ven97455122008-01-25 21:08:34 +0100498 }
499
500 }
Hiroshi Shimamoto13d77c32008-02-20 16:53:29 -0800501 put_task_struct(task);
Arjan van de Ven97455122008-01-25 21:08:34 +0100502 return 0;
503}
504
505static int lstats_open(struct inode *inode, struct file *file)
506{
Hiroshi Shimamoto13d77c32008-02-20 16:53:29 -0800507 return single_open(file, lstats_show_proc, inode);
Hiroshi Shimamotod6643d12008-02-14 10:27:00 -0800508}
509
Arjan van de Ven97455122008-01-25 21:08:34 +0100510static ssize_t lstats_write(struct file *file, const char __user *buf,
511 size_t count, loff_t *offs)
512{
Al Viro496ad9a2013-01-23 17:07:38 -0500513 struct task_struct *task = get_proc_task(file_inode(file));
Arjan van de Ven97455122008-01-25 21:08:34 +0100514
Hiroshi Shimamoto13d77c32008-02-20 16:53:29 -0800515 if (!task)
516 return -ESRCH;
Arjan van de Ven97455122008-01-25 21:08:34 +0100517 clear_all_latency_tracing(task);
Hiroshi Shimamoto13d77c32008-02-20 16:53:29 -0800518 put_task_struct(task);
Arjan van de Ven97455122008-01-25 21:08:34 +0100519
520 return count;
521}
522
523static const struct file_operations proc_lstats_operations = {
524 .open = lstats_open,
525 .read = seq_read,
526 .write = lstats_write,
527 .llseek = seq_lseek,
Hiroshi Shimamoto13d77c32008-02-20 16:53:29 -0800528 .release = single_release,
Arjan van de Ven97455122008-01-25 21:08:34 +0100529};
530
531#endif
532
Alexey Dobriyan6ba51e32014-08-08 14:21:48 -0700533static int proc_oom_score(struct seq_file *m, struct pid_namespace *ns,
534 struct pid *pid, struct task_struct *task)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700535{
David Rientjesa7f638f2012-05-29 15:06:47 -0700536 unsigned long totalpages = totalram_pages + total_swap_pages;
Oleg Nesterovb95c35e2010-04-01 15:13:57 +0200537 unsigned long points = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700538
Oleg Nesterovef419392016-08-02 14:03:19 -0700539 points = oom_badness(task, NULL, NULL, totalpages) *
540 1000 / totalpages;
Joe Perches25ce3192015-04-15 16:18:17 -0700541 seq_printf(m, "%lu\n", points);
542
543 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700544}
545
Neil Hormand85f50d2007-10-18 23:40:37 -0700546struct limit_names {
Alexey Dobriyancedbcca2014-08-08 14:21:33 -0700547 const char *name;
548 const char *unit;
Neil Hormand85f50d2007-10-18 23:40:37 -0700549};
550
551static const struct limit_names lnames[RLIM_NLIMITS] = {
Kees Cookcff4edb2009-09-22 16:45:32 -0700552 [RLIMIT_CPU] = {"Max cpu time", "seconds"},
Neil Hormand85f50d2007-10-18 23:40:37 -0700553 [RLIMIT_FSIZE] = {"Max file size", "bytes"},
554 [RLIMIT_DATA] = {"Max data size", "bytes"},
555 [RLIMIT_STACK] = {"Max stack size", "bytes"},
556 [RLIMIT_CORE] = {"Max core file size", "bytes"},
557 [RLIMIT_RSS] = {"Max resident set", "bytes"},
558 [RLIMIT_NPROC] = {"Max processes", "processes"},
559 [RLIMIT_NOFILE] = {"Max open files", "files"},
560 [RLIMIT_MEMLOCK] = {"Max locked memory", "bytes"},
561 [RLIMIT_AS] = {"Max address space", "bytes"},
562 [RLIMIT_LOCKS] = {"Max file locks", "locks"},
563 [RLIMIT_SIGPENDING] = {"Max pending signals", "signals"},
564 [RLIMIT_MSGQUEUE] = {"Max msgqueue size", "bytes"},
565 [RLIMIT_NICE] = {"Max nice priority", NULL},
566 [RLIMIT_RTPRIO] = {"Max realtime priority", NULL},
Eugene Teo88081172008-02-23 15:23:52 -0800567 [RLIMIT_RTTIME] = {"Max realtime timeout", "us"},
Neil Hormand85f50d2007-10-18 23:40:37 -0700568};
569
570/* Display limits for a process */
Alexey Dobriyan1c963eb2014-08-08 14:21:37 -0700571static int proc_pid_limits(struct seq_file *m, struct pid_namespace *ns,
572 struct pid *pid, struct task_struct *task)
Neil Hormand85f50d2007-10-18 23:40:37 -0700573{
574 unsigned int i;
Neil Hormand85f50d2007-10-18 23:40:37 -0700575 unsigned long flags;
Neil Hormand85f50d2007-10-18 23:40:37 -0700576
577 struct rlimit rlim[RLIM_NLIMITS];
578
Lai Jiangshana6bebbc2008-10-05 00:51:15 +0400579 if (!lock_task_sighand(task, &flags))
Neil Hormand85f50d2007-10-18 23:40:37 -0700580 return 0;
Neil Hormand85f50d2007-10-18 23:40:37 -0700581 memcpy(rlim, task->signal->rlim, sizeof(struct rlimit) * RLIM_NLIMITS);
582 unlock_task_sighand(task, &flags);
Neil Hormand85f50d2007-10-18 23:40:37 -0700583
584 /*
585 * print the file header
586 */
Alexey Dobriyan1c963eb2014-08-08 14:21:37 -0700587 seq_printf(m, "%-25s %-20s %-20s %-10s\n",
Joe Perches25ce3192015-04-15 16:18:17 -0700588 "Limit", "Soft Limit", "Hard Limit", "Units");
Neil Hormand85f50d2007-10-18 23:40:37 -0700589
590 for (i = 0; i < RLIM_NLIMITS; i++) {
591 if (rlim[i].rlim_cur == RLIM_INFINITY)
Alexey Dobriyan1c963eb2014-08-08 14:21:37 -0700592 seq_printf(m, "%-25s %-20s ",
Joe Perches25ce3192015-04-15 16:18:17 -0700593 lnames[i].name, "unlimited");
Neil Hormand85f50d2007-10-18 23:40:37 -0700594 else
Alexey Dobriyan1c963eb2014-08-08 14:21:37 -0700595 seq_printf(m, "%-25s %-20lu ",
Joe Perches25ce3192015-04-15 16:18:17 -0700596 lnames[i].name, rlim[i].rlim_cur);
Neil Hormand85f50d2007-10-18 23:40:37 -0700597
598 if (rlim[i].rlim_max == RLIM_INFINITY)
Alexey Dobriyan1c963eb2014-08-08 14:21:37 -0700599 seq_printf(m, "%-20s ", "unlimited");
Neil Hormand85f50d2007-10-18 23:40:37 -0700600 else
Alexey Dobriyan1c963eb2014-08-08 14:21:37 -0700601 seq_printf(m, "%-20lu ", rlim[i].rlim_max);
Neil Hormand85f50d2007-10-18 23:40:37 -0700602
603 if (lnames[i].unit)
Alexey Dobriyan1c963eb2014-08-08 14:21:37 -0700604 seq_printf(m, "%-10s\n", lnames[i].unit);
Neil Hormand85f50d2007-10-18 23:40:37 -0700605 else
Alexey Dobriyan1c963eb2014-08-08 14:21:37 -0700606 seq_putc(m, '\n');
Neil Hormand85f50d2007-10-18 23:40:37 -0700607 }
608
Alexey Dobriyan1c963eb2014-08-08 14:21:37 -0700609 return 0;
Neil Hormand85f50d2007-10-18 23:40:37 -0700610}
611
Roland McGrathebcb6732008-07-25 19:46:00 -0700612#ifdef CONFIG_HAVE_ARCH_TRACEHOOK
Alexey Dobriyan09d93bd2014-08-08 14:21:39 -0700613static int proc_pid_syscall(struct seq_file *m, struct pid_namespace *ns,
614 struct pid *pid, struct task_struct *task)
Roland McGrathebcb6732008-07-25 19:46:00 -0700615{
616 long nr;
617 unsigned long args[6], sp, pc;
Joe Perches25ce3192015-04-15 16:18:17 -0700618 int res;
619
620 res = lock_trace(task);
Al Viroa9712bc2011-03-23 15:52:50 -0400621 if (res)
622 return res;
Roland McGrathebcb6732008-07-25 19:46:00 -0700623
624 if (task_current_syscall(task, &nr, args, 6, &sp, &pc))
Alexey Dobriyan09d93bd2014-08-08 14:21:39 -0700625 seq_puts(m, "running\n");
Al Viroa9712bc2011-03-23 15:52:50 -0400626 else if (nr < 0)
Alexey Dobriyan09d93bd2014-08-08 14:21:39 -0700627 seq_printf(m, "%ld 0x%lx 0x%lx\n", nr, sp, pc);
Al Viroa9712bc2011-03-23 15:52:50 -0400628 else
Alexey Dobriyan09d93bd2014-08-08 14:21:39 -0700629 seq_printf(m,
Roland McGrathebcb6732008-07-25 19:46:00 -0700630 "%ld 0x%lx 0x%lx 0x%lx 0x%lx 0x%lx 0x%lx 0x%lx 0x%lx\n",
631 nr,
632 args[0], args[1], args[2], args[3], args[4], args[5],
633 sp, pc);
Al Viroa9712bc2011-03-23 15:52:50 -0400634 unlock_trace(task);
Joe Perches25ce3192015-04-15 16:18:17 -0700635
636 return 0;
Roland McGrathebcb6732008-07-25 19:46:00 -0700637}
638#endif /* CONFIG_HAVE_ARCH_TRACEHOOK */
639
Linus Torvalds1da177e2005-04-16 15:20:36 -0700640/************************************************************************/
641/* Here the fs part begins */
642/************************************************************************/
643
644/* permission checks */
Eric W. Biederman778c1142006-06-26 00:25:58 -0700645static int proc_fd_access_allowed(struct inode *inode)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700646{
Eric W. Biederman778c1142006-06-26 00:25:58 -0700647 struct task_struct *task;
648 int allowed = 0;
Eric W. Biedermandf26c402006-06-26 00:25:59 -0700649 /* Allow access to a task's file descriptors if it is us or we
650 * may use ptrace attach to the process and find out that
651 * information.
Eric W. Biederman778c1142006-06-26 00:25:58 -0700652 */
653 task = get_proc_task(inode);
Eric W. Biedermandf26c402006-06-26 00:25:59 -0700654 if (task) {
Jann Horncaaee622016-01-20 15:00:04 -0800655 allowed = ptrace_may_access(task, PTRACE_MODE_READ_FSCREDS);
Eric W. Biederman778c1142006-06-26 00:25:58 -0700656 put_task_struct(task);
Eric W. Biedermandf26c402006-06-26 00:25:59 -0700657 }
Eric W. Biederman778c1142006-06-26 00:25:58 -0700658 return allowed;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700659}
660
Eric W. Biederman6b4e3062010-03-07 16:41:34 -0800661int proc_setattr(struct dentry *dentry, struct iattr *attr)
Linus Torvalds6d76fa52006-07-15 12:26:45 -0700662{
663 int error;
David Howells2b0143b2015-03-17 22:25:59 +0000664 struct inode *inode = d_inode(dentry);
Linus Torvalds6d76fa52006-07-15 12:26:45 -0700665
666 if (attr->ia_valid & ATTR_MODE)
667 return -EPERM;
668
Jan Kara31051c82016-05-26 16:55:18 +0200669 error = setattr_prepare(dentry, attr);
Christoph Hellwig10257742010-06-04 11:30:02 +0200670 if (error)
671 return error;
672
Christoph Hellwig10257742010-06-04 11:30:02 +0200673 setattr_copy(inode, attr);
674 mark_inode_dirty(inode);
675 return 0;
Linus Torvalds6d76fa52006-07-15 12:26:45 -0700676}
677
Vasiliy Kulikov04996802012-01-10 15:11:31 -0800678/*
679 * May current process learn task's sched/cmdline info (for hide_pid_min=1)
680 * or euid/egid (for hide_pid_min=2)?
681 */
682static bool has_pid_permissions(struct pid_namespace *pid,
683 struct task_struct *task,
684 int hide_pid_min)
685{
686 if (pid->hide_pid < hide_pid_min)
687 return true;
688 if (in_group_p(pid->pid_gid))
689 return true;
Jann Horncaaee622016-01-20 15:00:04 -0800690 return ptrace_may_access(task, PTRACE_MODE_READ_FSCREDS);
Vasiliy Kulikov04996802012-01-10 15:11:31 -0800691}
692
693
694static int proc_pid_permission(struct inode *inode, int mask)
695{
Christoph Hellwig76f668b2018-05-16 07:19:01 +0200696 struct pid_namespace *pid = proc_pid_ns(inode);
Vasiliy Kulikov04996802012-01-10 15:11:31 -0800697 struct task_struct *task;
698 bool has_perms;
699
700 task = get_proc_task(inode);
Xiaotian Fenga2ef9902012-01-12 17:17:08 -0800701 if (!task)
702 return -ESRCH;
Lafcadio Wluiki796f5712017-02-24 15:00:23 -0800703 has_perms = has_pid_permissions(pid, task, HIDEPID_NO_ACCESS);
Vasiliy Kulikov04996802012-01-10 15:11:31 -0800704 put_task_struct(task);
705
706 if (!has_perms) {
Lafcadio Wluiki796f5712017-02-24 15:00:23 -0800707 if (pid->hide_pid == HIDEPID_INVISIBLE) {
Vasiliy Kulikov04996802012-01-10 15:11:31 -0800708 /*
709 * Let's make getdents(), stat(), and open()
710 * consistent with each other. If a process
711 * may not stat() a file, it shouldn't be seen
712 * in procfs at all.
713 */
714 return -ENOENT;
715 }
716
717 return -EPERM;
718 }
719 return generic_permission(inode, mask);
720}
721
722
723
Arjan van de Venc5ef1c42007-02-12 00:55:40 -0800724static const struct inode_operations proc_def_inode_operations = {
Linus Torvalds6d76fa52006-07-15 12:26:45 -0700725 .setattr = proc_setattr,
726};
727
Eric W. Biedermanbe614082008-02-08 04:18:30 -0800728static int proc_single_show(struct seq_file *m, void *v)
729{
730 struct inode *inode = m->private;
Christoph Hellwig76f668b2018-05-16 07:19:01 +0200731 struct pid_namespace *ns = proc_pid_ns(inode);
732 struct pid *pid = proc_pid(inode);
Eric W. Biedermanbe614082008-02-08 04:18:30 -0800733 struct task_struct *task;
734 int ret;
735
Eric W. Biedermanbe614082008-02-08 04:18:30 -0800736 task = get_pid_task(pid, PIDTYPE_PID);
737 if (!task)
738 return -ESRCH;
739
740 ret = PROC_I(inode)->op.proc_show(m, ns, pid, task);
741
742 put_task_struct(task);
743 return ret;
744}
745
746static int proc_single_open(struct inode *inode, struct file *filp)
747{
Jovi Zhangc6a34052011-01-12 17:00:34 -0800748 return single_open(filp, proc_single_show, inode);
Eric W. Biedermanbe614082008-02-08 04:18:30 -0800749}
750
751static const struct file_operations proc_single_file_operations = {
752 .open = proc_single_open,
753 .read = seq_read,
754 .llseek = seq_lseek,
755 .release = single_release,
756};
757
Oleg Nesterov5381e162014-10-09 15:25:24 -0700758
759struct mm_struct *proc_mem_open(struct inode *inode, unsigned int mode)
760{
761 struct task_struct *task = get_proc_task(inode);
762 struct mm_struct *mm = ERR_PTR(-ESRCH);
763
764 if (task) {
Jann Horncaaee622016-01-20 15:00:04 -0800765 mm = mm_access(task, mode | PTRACE_MODE_FSCREDS);
Oleg Nesterov5381e162014-10-09 15:25:24 -0700766 put_task_struct(task);
767
768 if (!IS_ERR_OR_NULL(mm)) {
769 /* ensure this mm_struct can't be freed */
Vegard Nossumf1f10072017-02-27 14:30:07 -0800770 mmgrab(mm);
Oleg Nesterov5381e162014-10-09 15:25:24 -0700771 /* but do not pin its memory */
772 mmput(mm);
773 }
774 }
775
776 return mm;
777}
778
Cong Wangb409e572012-05-31 16:26:17 -0700779static int __mem_open(struct inode *inode, struct file *file, unsigned int mode)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700780{
Oleg Nesterov5381e162014-10-09 15:25:24 -0700781 struct mm_struct *mm = proc_mem_open(inode, mode);
Linus Torvaldse2683372012-01-17 15:21:19 -0800782
783 if (IS_ERR(mm))
784 return PTR_ERR(mm);
785
Linus Torvaldse2683372012-01-17 15:21:19 -0800786 file->private_data = mm;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700787 return 0;
788}
789
Cong Wangb409e572012-05-31 16:26:17 -0700790static int mem_open(struct inode *inode, struct file *file)
791{
Djalal Harounibc452b42012-07-30 14:42:28 -0700792 int ret = __mem_open(inode, file, PTRACE_MODE_ATTACH);
793
794 /* OK to pass negative loff_t, we can catch out-of-range */
795 file->f_mode |= FMODE_UNSIGNED_OFFSET;
796
797 return ret;
Cong Wangb409e572012-05-31 16:26:17 -0700798}
799
Oleg Nesterov572d34b2012-01-31 17:14:54 +0100800static ssize_t mem_rw(struct file *file, char __user *buf,
801 size_t count, loff_t *ppos, int write)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700802{
Linus Torvaldse2683372012-01-17 15:21:19 -0800803 struct mm_struct *mm = file->private_data;
Oleg Nesterov572d34b2012-01-31 17:14:54 +0100804 unsigned long addr = *ppos;
805 ssize_t copied;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700806 char *page;
Linus Torvalds272ddc82016-10-24 19:00:44 -0700807 unsigned int flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700808
Linus Torvaldse2683372012-01-17 15:21:19 -0800809 if (!mm)
810 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700811
Michal Hocko0ee931c2017-09-13 16:28:29 -0700812 page = (char *)__get_free_page(GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700813 if (!page)
Linus Torvaldse2683372012-01-17 15:21:19 -0800814 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700815
Frederik Deweerdtf7ca54f2006-09-29 02:01:02 -0700816 copied = 0;
Vegard Nossum388f7932017-02-27 14:30:13 -0800817 if (!mmget_not_zero(mm))
Oleg Nesterov6d08f2c2012-01-31 17:15:11 +0100818 goto free;
819
Linus Torvaldsf511c0b2017-05-30 12:38:59 -0700820 flags = FOLL_FORCE | (write ? FOLL_WRITE : 0);
Lorenzo Stoakes6347e8d2016-10-13 01:20:19 +0100821
Linus Torvalds1da177e2005-04-16 15:20:36 -0700822 while (count > 0) {
Oleg Nesterov572d34b2012-01-31 17:14:54 +0100823 int this_len = min_t(int, count, PAGE_SIZE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700824
Oleg Nesterov572d34b2012-01-31 17:14:54 +0100825 if (write && copy_from_user(page, buf, this_len)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700826 copied = -EFAULT;
827 break;
828 }
Oleg Nesterov572d34b2012-01-31 17:14:54 +0100829
Lorenzo Stoakes6347e8d2016-10-13 01:20:19 +0100830 this_len = access_remote_vm(mm, addr, page, this_len, flags);
Oleg Nesterov572d34b2012-01-31 17:14:54 +0100831 if (!this_len) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700832 if (!copied)
833 copied = -EIO;
834 break;
835 }
Oleg Nesterov572d34b2012-01-31 17:14:54 +0100836
837 if (!write && copy_to_user(buf, page, this_len)) {
838 copied = -EFAULT;
839 break;
840 }
841
842 buf += this_len;
843 addr += this_len;
844 copied += this_len;
845 count -= this_len;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700846 }
Oleg Nesterov572d34b2012-01-31 17:14:54 +0100847 *ppos = addr;
KOSAKI Motohiro30cd8902011-05-26 16:25:52 -0700848
Oleg Nesterov6d08f2c2012-01-31 17:15:11 +0100849 mmput(mm);
850free:
KOSAKI Motohiro30cd8902011-05-26 16:25:52 -0700851 free_page((unsigned long) page);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700852 return copied;
853}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700854
Oleg Nesterov572d34b2012-01-31 17:14:54 +0100855static ssize_t mem_read(struct file *file, char __user *buf,
856 size_t count, loff_t *ppos)
857{
858 return mem_rw(file, buf, count, ppos, 0);
859}
860
861static ssize_t mem_write(struct file *file, const char __user *buf,
862 size_t count, loff_t *ppos)
863{
864 return mem_rw(file, (char __user*)buf, count, ppos, 1);
865}
866
Matt Mackall85863e42008-02-04 22:29:04 -0800867loff_t mem_lseek(struct file *file, loff_t offset, int orig)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700868{
869 switch (orig) {
870 case 0:
871 file->f_pos = offset;
872 break;
873 case 1:
874 file->f_pos += offset;
875 break;
876 default:
877 return -EINVAL;
878 }
879 force_successful_syscall_return();
880 return file->f_pos;
881}
882
Linus Torvaldse2683372012-01-17 15:21:19 -0800883static int mem_release(struct inode *inode, struct file *file)
884{
885 struct mm_struct *mm = file->private_data;
Oleg Nesterov71879d32012-01-31 17:14:38 +0100886 if (mm)
Oleg Nesterov6d08f2c2012-01-31 17:15:11 +0100887 mmdrop(mm);
Linus Torvaldse2683372012-01-17 15:21:19 -0800888 return 0;
889}
890
Arjan van de Ven00977a52007-02-12 00:55:34 -0800891static const struct file_operations proc_mem_operations = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700892 .llseek = mem_lseek,
893 .read = mem_read,
894 .write = mem_write,
895 .open = mem_open,
Linus Torvaldse2683372012-01-17 15:21:19 -0800896 .release = mem_release,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700897};
898
Cong Wangb409e572012-05-31 16:26:17 -0700899static int environ_open(struct inode *inode, struct file *file)
900{
901 return __mem_open(inode, file, PTRACE_MODE_READ);
902}
903
James Pearson315e28c2007-10-16 23:30:17 -0700904static ssize_t environ_read(struct file *file, char __user *buf,
905 size_t count, loff_t *ppos)
906{
James Pearson315e28c2007-10-16 23:30:17 -0700907 char *page;
908 unsigned long src = *ppos;
Cong Wangb409e572012-05-31 16:26:17 -0700909 int ret = 0;
910 struct mm_struct *mm = file->private_data;
Mateusz Guzika3b609e2016-01-20 15:01:05 -0800911 unsigned long env_start, env_end;
James Pearson315e28c2007-10-16 23:30:17 -0700912
Mathias Krause8148a732016-05-05 16:22:26 -0700913 /* Ensure the process spawned far enough to have an environment. */
914 if (!mm || !mm->env_end)
Cong Wangb409e572012-05-31 16:26:17 -0700915 return 0;
James Pearson315e28c2007-10-16 23:30:17 -0700916
Michal Hocko0ee931c2017-09-13 16:28:29 -0700917 page = (char *)__get_free_page(GFP_KERNEL);
James Pearson315e28c2007-10-16 23:30:17 -0700918 if (!page)
Cong Wangb409e572012-05-31 16:26:17 -0700919 return -ENOMEM;
James Pearson315e28c2007-10-16 23:30:17 -0700920
Al Virod6f64b82011-02-15 22:26:01 -0500921 ret = 0;
Vegard Nossum388f7932017-02-27 14:30:13 -0800922 if (!mmget_not_zero(mm))
Cong Wangb409e572012-05-31 16:26:17 -0700923 goto free;
Mateusz Guzika3b609e2016-01-20 15:01:05 -0800924
Yang Shi88aa7cc2018-06-07 17:05:28 -0700925 spin_lock(&mm->arg_lock);
Mateusz Guzika3b609e2016-01-20 15:01:05 -0800926 env_start = mm->env_start;
927 env_end = mm->env_end;
Yang Shi88aa7cc2018-06-07 17:05:28 -0700928 spin_unlock(&mm->arg_lock);
Mateusz Guzika3b609e2016-01-20 15:01:05 -0800929
James Pearson315e28c2007-10-16 23:30:17 -0700930 while (count > 0) {
Djalal Harounie8905ec2012-07-30 14:42:26 -0700931 size_t this_len, max_len;
932 int retval;
933
Mateusz Guzika3b609e2016-01-20 15:01:05 -0800934 if (src >= (env_end - env_start))
Djalal Harounie8905ec2012-07-30 14:42:26 -0700935 break;
James Pearson315e28c2007-10-16 23:30:17 -0700936
Mateusz Guzika3b609e2016-01-20 15:01:05 -0800937 this_len = env_end - (env_start + src);
James Pearson315e28c2007-10-16 23:30:17 -0700938
Djalal Harounie8905ec2012-07-30 14:42:26 -0700939 max_len = min_t(size_t, PAGE_SIZE, count);
940 this_len = min(max_len, this_len);
James Pearson315e28c2007-10-16 23:30:17 -0700941
Willy Tarreau7f7ccc22018-05-11 08:11:44 +0200942 retval = access_remote_vm(mm, (env_start + src), page, this_len, FOLL_ANON);
James Pearson315e28c2007-10-16 23:30:17 -0700943
944 if (retval <= 0) {
945 ret = retval;
946 break;
947 }
948
949 if (copy_to_user(buf, page, retval)) {
950 ret = -EFAULT;
951 break;
952 }
953
954 ret += retval;
955 src += retval;
956 buf += retval;
957 count -= retval;
958 }
959 *ppos = src;
James Pearson315e28c2007-10-16 23:30:17 -0700960 mmput(mm);
Cong Wangb409e572012-05-31 16:26:17 -0700961
962free:
James Pearson315e28c2007-10-16 23:30:17 -0700963 free_page((unsigned long) page);
James Pearson315e28c2007-10-16 23:30:17 -0700964 return ret;
965}
966
967static const struct file_operations proc_environ_operations = {
Cong Wangb409e572012-05-31 16:26:17 -0700968 .open = environ_open,
James Pearson315e28c2007-10-16 23:30:17 -0700969 .read = environ_read,
Arnd Bergmann87df8422010-03-17 23:06:02 +0100970 .llseek = generic_file_llseek,
Cong Wangb409e572012-05-31 16:26:17 -0700971 .release = mem_release,
James Pearson315e28c2007-10-16 23:30:17 -0700972};
973
Al Viroc5317162016-10-05 18:43:43 -0400974static int auxv_open(struct inode *inode, struct file *file)
975{
976 return __mem_open(inode, file, PTRACE_MODE_READ_FSCREDS);
977}
978
979static ssize_t auxv_read(struct file *file, char __user *buf,
980 size_t count, loff_t *ppos)
981{
982 struct mm_struct *mm = file->private_data;
983 unsigned int nwords = 0;
Leon Yu06b28492016-10-27 17:46:50 -0700984
985 if (!mm)
986 return 0;
Al Viroc5317162016-10-05 18:43:43 -0400987 do {
988 nwords += 2;
989 } while (mm->saved_auxv[nwords - 2] != 0); /* AT_NULL */
990 return simple_read_from_buffer(buf, count, ppos, mm->saved_auxv,
991 nwords * sizeof(mm->saved_auxv[0]));
992}
993
994static const struct file_operations proc_auxv_operations = {
995 .open = auxv_open,
996 .read = auxv_read,
997 .llseek = generic_file_llseek,
998 .release = mem_release,
999};
1000
David Rientjesfa0cbbf2012-11-12 17:53:04 -08001001static ssize_t oom_adj_read(struct file *file, char __user *buf, size_t count,
1002 loff_t *ppos)
1003{
Al Viro496ad9a2013-01-23 17:07:38 -05001004 struct task_struct *task = get_proc_task(file_inode(file));
David Rientjesfa0cbbf2012-11-12 17:53:04 -08001005 char buffer[PROC_NUMBUF];
1006 int oom_adj = OOM_ADJUST_MIN;
1007 size_t len;
David Rientjesfa0cbbf2012-11-12 17:53:04 -08001008
1009 if (!task)
1010 return -ESRCH;
Michal Hockof913da52016-07-28 15:44:37 -07001011 if (task->signal->oom_score_adj == OOM_SCORE_ADJ_MAX)
1012 oom_adj = OOM_ADJUST_MAX;
1013 else
1014 oom_adj = (task->signal->oom_score_adj * -OOM_DISABLE) /
1015 OOM_SCORE_ADJ_MAX;
David Rientjesfa0cbbf2012-11-12 17:53:04 -08001016 put_task_struct(task);
1017 len = snprintf(buffer, sizeof(buffer), "%d\n", oom_adj);
1018 return simple_read_from_buffer(buf, count, ppos, buffer, len);
1019}
1020
Michal Hocko1d5f0ac2016-07-28 15:44:40 -07001021static int __set_oom_adj(struct file *file, int oom_adj, bool legacy)
1022{
1023 static DEFINE_MUTEX(oom_adj_mutex);
Michal Hocko44a70ade2016-07-28 15:44:43 -07001024 struct mm_struct *mm = NULL;
Michal Hocko1d5f0ac2016-07-28 15:44:40 -07001025 struct task_struct *task;
1026 int err = 0;
1027
1028 task = get_proc_task(file_inode(file));
1029 if (!task)
1030 return -ESRCH;
1031
1032 mutex_lock(&oom_adj_mutex);
1033 if (legacy) {
1034 if (oom_adj < task->signal->oom_score_adj &&
1035 !capable(CAP_SYS_RESOURCE)) {
1036 err = -EACCES;
1037 goto err_unlock;
1038 }
1039 /*
1040 * /proc/pid/oom_adj is provided for legacy purposes, ask users to use
1041 * /proc/pid/oom_score_adj instead.
1042 */
1043 pr_warn_once("%s (%d): /proc/%d/oom_adj is deprecated, please use /proc/%d/oom_score_adj instead.\n",
1044 current->comm, task_pid_nr(current), task_pid_nr(task),
1045 task_pid_nr(task));
1046 } else {
1047 if ((short)oom_adj < task->signal->oom_score_adj_min &&
1048 !capable(CAP_SYS_RESOURCE)) {
1049 err = -EACCES;
1050 goto err_unlock;
1051 }
1052 }
1053
Michal Hocko44a70ade2016-07-28 15:44:43 -07001054 /*
1055 * Make sure we will check other processes sharing the mm if this is
1056 * not vfrok which wants its own oom_score_adj.
1057 * pin the mm so it doesn't go away and get reused after task_unlock
1058 */
1059 if (!task->vfork_done) {
1060 struct task_struct *p = find_lock_task_mm(task);
1061
1062 if (p) {
1063 if (atomic_read(&p->mm->mm_users) > 1) {
1064 mm = p->mm;
Vegard Nossumf1f10072017-02-27 14:30:07 -08001065 mmgrab(mm);
Michal Hocko44a70ade2016-07-28 15:44:43 -07001066 }
1067 task_unlock(p);
1068 }
1069 }
1070
Michal Hocko1d5f0ac2016-07-28 15:44:40 -07001071 task->signal->oom_score_adj = oom_adj;
1072 if (!legacy && has_capability_noaudit(current, CAP_SYS_RESOURCE))
1073 task->signal->oom_score_adj_min = (short)oom_adj;
1074 trace_oom_score_adj_update(task);
Michal Hocko44a70ade2016-07-28 15:44:43 -07001075
1076 if (mm) {
1077 struct task_struct *p;
1078
1079 rcu_read_lock();
1080 for_each_process(p) {
1081 if (same_thread_group(task, p))
1082 continue;
1083
1084 /* do not touch kernel threads or the global init */
1085 if (p->flags & PF_KTHREAD || is_global_init(p))
1086 continue;
1087
1088 task_lock(p);
1089 if (!p->vfork_done && process_shares_mm(p, mm)) {
1090 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",
1091 task_pid_nr(p), p->comm,
1092 p->signal->oom_score_adj, oom_adj,
1093 task_pid_nr(task), task->comm);
1094 p->signal->oom_score_adj = oom_adj;
1095 if (!legacy && has_capability_noaudit(current, CAP_SYS_RESOURCE))
1096 p->signal->oom_score_adj_min = (short)oom_adj;
1097 }
1098 task_unlock(p);
1099 }
1100 rcu_read_unlock();
1101 mmdrop(mm);
1102 }
Michal Hocko1d5f0ac2016-07-28 15:44:40 -07001103err_unlock:
1104 mutex_unlock(&oom_adj_mutex);
1105 put_task_struct(task);
1106 return err;
1107}
Michal Hockof913da52016-07-28 15:44:37 -07001108
David Rientjesb72bdfa2015-11-05 18:50:32 -08001109/*
1110 * /proc/pid/oom_adj exists solely for backwards compatibility with previous
1111 * kernels. The effective policy is defined by oom_score_adj, which has a
1112 * different scale: oom_adj grew exponentially and oom_score_adj grows linearly.
1113 * Values written to oom_adj are simply mapped linearly to oom_score_adj.
1114 * Processes that become oom disabled via oom_adj will still be oom disabled
1115 * with this implementation.
1116 *
1117 * oom_adj cannot be removed since existing userspace binaries use it.
1118 */
David Rientjesfa0cbbf2012-11-12 17:53:04 -08001119static ssize_t oom_adj_write(struct file *file, const char __user *buf,
1120 size_t count, loff_t *ppos)
1121{
David Rientjesfa0cbbf2012-11-12 17:53:04 -08001122 char buffer[PROC_NUMBUF];
1123 int oom_adj;
David Rientjesfa0cbbf2012-11-12 17:53:04 -08001124 int err;
1125
1126 memset(buffer, 0, sizeof(buffer));
1127 if (count > sizeof(buffer) - 1)
1128 count = sizeof(buffer) - 1;
1129 if (copy_from_user(buffer, buf, count)) {
1130 err = -EFAULT;
1131 goto out;
1132 }
1133
1134 err = kstrtoint(strstrip(buffer), 0, &oom_adj);
1135 if (err)
1136 goto out;
1137 if ((oom_adj < OOM_ADJUST_MIN || oom_adj > OOM_ADJUST_MAX) &&
1138 oom_adj != OOM_DISABLE) {
1139 err = -EINVAL;
1140 goto out;
1141 }
1142
David Rientjesfa0cbbf2012-11-12 17:53:04 -08001143 /*
1144 * Scale /proc/pid/oom_score_adj appropriately ensuring that a maximum
1145 * value is always attainable.
1146 */
1147 if (oom_adj == OOM_ADJUST_MAX)
1148 oom_adj = OOM_SCORE_ADJ_MAX;
1149 else
1150 oom_adj = (oom_adj * OOM_SCORE_ADJ_MAX) / -OOM_DISABLE;
1151
Michal Hocko1d5f0ac2016-07-28 15:44:40 -07001152 err = __set_oom_adj(file, oom_adj, true);
David Rientjesfa0cbbf2012-11-12 17:53:04 -08001153out:
1154 return err < 0 ? err : count;
1155}
1156
1157static const struct file_operations proc_oom_adj_operations = {
1158 .read = oom_adj_read,
1159 .write = oom_adj_write,
1160 .llseek = generic_file_llseek,
1161};
1162
David Rientjesa63d83f2010-08-09 17:19:46 -07001163static ssize_t oom_score_adj_read(struct file *file, char __user *buf,
1164 size_t count, loff_t *ppos)
1165{
Al Viro496ad9a2013-01-23 17:07:38 -05001166 struct task_struct *task = get_proc_task(file_inode(file));
David Rientjesa63d83f2010-08-09 17:19:46 -07001167 char buffer[PROC_NUMBUF];
David Rientjesa9c58b902012-12-11 16:02:54 -08001168 short oom_score_adj = OOM_SCORE_ADJ_MIN;
David Rientjesa63d83f2010-08-09 17:19:46 -07001169 size_t len;
1170
1171 if (!task)
1172 return -ESRCH;
Michal Hockof913da52016-07-28 15:44:37 -07001173 oom_score_adj = task->signal->oom_score_adj;
David Rientjesa63d83f2010-08-09 17:19:46 -07001174 put_task_struct(task);
David Rientjesa9c58b902012-12-11 16:02:54 -08001175 len = snprintf(buffer, sizeof(buffer), "%hd\n", oom_score_adj);
David Rientjesa63d83f2010-08-09 17:19:46 -07001176 return simple_read_from_buffer(buf, count, ppos, buffer, len);
1177}
1178
1179static ssize_t oom_score_adj_write(struct file *file, const char __user *buf,
1180 size_t count, loff_t *ppos)
1181{
David Rientjesa63d83f2010-08-09 17:19:46 -07001182 char buffer[PROC_NUMBUF];
Alexey Dobriyan0a8cb8e2011-05-26 16:25:50 -07001183 int oom_score_adj;
David Rientjesa63d83f2010-08-09 17:19:46 -07001184 int err;
1185
1186 memset(buffer, 0, sizeof(buffer));
1187 if (count > sizeof(buffer) - 1)
1188 count = sizeof(buffer) - 1;
David Rientjes723548b2010-10-26 14:21:25 -07001189 if (copy_from_user(buffer, buf, count)) {
1190 err = -EFAULT;
1191 goto out;
1192 }
David Rientjesa63d83f2010-08-09 17:19:46 -07001193
Alexey Dobriyan0a8cb8e2011-05-26 16:25:50 -07001194 err = kstrtoint(strstrip(buffer), 0, &oom_score_adj);
David Rientjesa63d83f2010-08-09 17:19:46 -07001195 if (err)
David Rientjes723548b2010-10-26 14:21:25 -07001196 goto out;
David Rientjesa63d83f2010-08-09 17:19:46 -07001197 if (oom_score_adj < OOM_SCORE_ADJ_MIN ||
David Rientjes723548b2010-10-26 14:21:25 -07001198 oom_score_adj > OOM_SCORE_ADJ_MAX) {
1199 err = -EINVAL;
1200 goto out;
1201 }
David Rientjesa63d83f2010-08-09 17:19:46 -07001202
Michal Hocko1d5f0ac2016-07-28 15:44:40 -07001203 err = __set_oom_adj(file, oom_score_adj, false);
David Rientjes723548b2010-10-26 14:21:25 -07001204out:
1205 return err < 0 ? err : count;
David Rientjesa63d83f2010-08-09 17:19:46 -07001206}
1207
1208static const struct file_operations proc_oom_score_adj_operations = {
1209 .read = oom_score_adj_read,
1210 .write = oom_score_adj_write,
Arnd Bergmann6038f372010-08-15 18:52:59 +02001211 .llseek = default_llseek,
David Rientjesa63d83f2010-08-09 17:19:46 -07001212};
1213
Linus Torvalds1da177e2005-04-16 15:20:36 -07001214#ifdef CONFIG_AUDITSYSCALL
Alexey Dobriyanb4eb4f72016-10-29 19:04:39 +03001215#define TMPBUFLEN 11
Linus Torvalds1da177e2005-04-16 15:20:36 -07001216static ssize_t proc_loginuid_read(struct file * file, char __user * buf,
1217 size_t count, loff_t *ppos)
1218{
Al Viro496ad9a2013-01-23 17:07:38 -05001219 struct inode * inode = file_inode(file);
Eric W. Biederman99f89552006-06-26 00:25:55 -07001220 struct task_struct *task = get_proc_task(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001221 ssize_t length;
1222 char tmpbuf[TMPBUFLEN];
1223
Eric W. Biederman99f89552006-06-26 00:25:55 -07001224 if (!task)
1225 return -ESRCH;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001226 length = scnprintf(tmpbuf, TMPBUFLEN, "%u",
Eric W. Biedermane1760bd2012-09-10 22:39:43 -07001227 from_kuid(file->f_cred->user_ns,
1228 audit_get_loginuid(task)));
Eric W. Biederman99f89552006-06-26 00:25:55 -07001229 put_task_struct(task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001230 return simple_read_from_buffer(buf, count, ppos, tmpbuf, length);
1231}
1232
1233static ssize_t proc_loginuid_write(struct file * file, const char __user * buf,
1234 size_t count, loff_t *ppos)
1235{
Al Viro496ad9a2013-01-23 17:07:38 -05001236 struct inode * inode = file_inode(file);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001237 uid_t loginuid;
Eric W. Biedermane1760bd2012-09-10 22:39:43 -07001238 kuid_t kloginuid;
Alexey Dobriyan774636e2015-09-09 15:36:59 -07001239 int rv;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001240
Paul E. McKenney7dc52152010-02-22 17:04:52 -08001241 rcu_read_lock();
1242 if (current != pid_task(proc_pid(inode), PIDTYPE_PID)) {
1243 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001244 return -EPERM;
Paul E. McKenney7dc52152010-02-22 17:04:52 -08001245 }
1246 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001247
Linus Torvalds1da177e2005-04-16 15:20:36 -07001248 if (*ppos != 0) {
1249 /* No partial writes. */
1250 return -EINVAL;
1251 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001252
Alexey Dobriyan774636e2015-09-09 15:36:59 -07001253 rv = kstrtou32_from_user(buf, count, 10, &loginuid);
1254 if (rv < 0)
1255 return rv;
Eric Paris81407c82013-05-24 09:49:14 -04001256
1257 /* is userspace tring to explicitly UNSET the loginuid? */
1258 if (loginuid == AUDIT_UID_UNSET) {
1259 kloginuid = INVALID_UID;
1260 } else {
1261 kloginuid = make_kuid(file->f_cred->user_ns, loginuid);
Alexey Dobriyan774636e2015-09-09 15:36:59 -07001262 if (!uid_valid(kloginuid))
1263 return -EINVAL;
Eric W. Biedermane1760bd2012-09-10 22:39:43 -07001264 }
1265
Alexey Dobriyan774636e2015-09-09 15:36:59 -07001266 rv = audit_set_loginuid(kloginuid);
1267 if (rv < 0)
1268 return rv;
1269 return count;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001270}
1271
Arjan van de Ven00977a52007-02-12 00:55:34 -08001272static const struct file_operations proc_loginuid_operations = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001273 .read = proc_loginuid_read,
1274 .write = proc_loginuid_write,
Arnd Bergmann87df8422010-03-17 23:06:02 +01001275 .llseek = generic_file_llseek,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001276};
Eric Paris1e0bd752008-03-13 08:15:31 -04001277
1278static ssize_t proc_sessionid_read(struct file * file, char __user * buf,
1279 size_t count, loff_t *ppos)
1280{
Al Viro496ad9a2013-01-23 17:07:38 -05001281 struct inode * inode = file_inode(file);
Eric Paris1e0bd752008-03-13 08:15:31 -04001282 struct task_struct *task = get_proc_task(inode);
1283 ssize_t length;
1284 char tmpbuf[TMPBUFLEN];
1285
1286 if (!task)
1287 return -ESRCH;
1288 length = scnprintf(tmpbuf, TMPBUFLEN, "%u",
1289 audit_get_sessionid(task));
1290 put_task_struct(task);
1291 return simple_read_from_buffer(buf, count, ppos, tmpbuf, length);
1292}
1293
1294static const struct file_operations proc_sessionid_operations = {
1295 .read = proc_sessionid_read,
Arnd Bergmann87df8422010-03-17 23:06:02 +01001296 .llseek = generic_file_llseek,
Eric Paris1e0bd752008-03-13 08:15:31 -04001297};
Linus Torvalds1da177e2005-04-16 15:20:36 -07001298#endif
1299
Akinobu Mitaf4f154f2006-12-08 02:39:47 -08001300#ifdef CONFIG_FAULT_INJECTION
1301static ssize_t proc_fault_inject_read(struct file * file, char __user * buf,
1302 size_t count, loff_t *ppos)
1303{
Al Viro496ad9a2013-01-23 17:07:38 -05001304 struct task_struct *task = get_proc_task(file_inode(file));
Akinobu Mitaf4f154f2006-12-08 02:39:47 -08001305 char buffer[PROC_NUMBUF];
1306 size_t len;
1307 int make_it_fail;
Akinobu Mitaf4f154f2006-12-08 02:39:47 -08001308
1309 if (!task)
1310 return -ESRCH;
1311 make_it_fail = task->make_it_fail;
1312 put_task_struct(task);
1313
1314 len = snprintf(buffer, sizeof(buffer), "%i\n", make_it_fail);
Akinobu Mita0c28f282007-05-08 00:31:41 -07001315
1316 return simple_read_from_buffer(buf, count, ppos, buffer, len);
Akinobu Mitaf4f154f2006-12-08 02:39:47 -08001317}
1318
1319static ssize_t proc_fault_inject_write(struct file * file,
1320 const char __user * buf, size_t count, loff_t *ppos)
1321{
1322 struct task_struct *task;
Alexey Dobriyan774636e2015-09-09 15:36:59 -07001323 char buffer[PROC_NUMBUF];
Akinobu Mitaf4f154f2006-12-08 02:39:47 -08001324 int make_it_fail;
Alexey Dobriyan774636e2015-09-09 15:36:59 -07001325 int rv;
Akinobu Mitaf4f154f2006-12-08 02:39:47 -08001326
1327 if (!capable(CAP_SYS_RESOURCE))
1328 return -EPERM;
1329 memset(buffer, 0, sizeof(buffer));
1330 if (count > sizeof(buffer) - 1)
1331 count = sizeof(buffer) - 1;
1332 if (copy_from_user(buffer, buf, count))
1333 return -EFAULT;
Alexey Dobriyan774636e2015-09-09 15:36:59 -07001334 rv = kstrtoint(strstrip(buffer), 0, &make_it_fail);
1335 if (rv < 0)
1336 return rv;
Dave Jones16caed32014-04-07 15:39:15 -07001337 if (make_it_fail < 0 || make_it_fail > 1)
1338 return -EINVAL;
1339
Al Viro496ad9a2013-01-23 17:07:38 -05001340 task = get_proc_task(file_inode(file));
Akinobu Mitaf4f154f2006-12-08 02:39:47 -08001341 if (!task)
1342 return -ESRCH;
1343 task->make_it_fail = make_it_fail;
1344 put_task_struct(task);
Vincent Licba8aaf2009-09-22 16:45:38 -07001345
1346 return count;
Akinobu Mitaf4f154f2006-12-08 02:39:47 -08001347}
1348
Arjan van de Ven00977a52007-02-12 00:55:34 -08001349static const struct file_operations proc_fault_inject_operations = {
Akinobu Mitaf4f154f2006-12-08 02:39:47 -08001350 .read = proc_fault_inject_read,
1351 .write = proc_fault_inject_write,
Arnd Bergmann87df8422010-03-17 23:06:02 +01001352 .llseek = generic_file_llseek,
Akinobu Mitaf4f154f2006-12-08 02:39:47 -08001353};
Dmitry Vyukove41d58182017-07-12 14:34:35 -07001354
1355static ssize_t proc_fail_nth_write(struct file *file, const char __user *buf,
1356 size_t count, loff_t *ppos)
1357{
1358 struct task_struct *task;
Akinobu Mita9049f2f2017-07-14 14:49:52 -07001359 int err;
1360 unsigned int n;
Dmitry Vyukove41d58182017-07-12 14:34:35 -07001361
Akinobu Mita9049f2f2017-07-14 14:49:52 -07001362 err = kstrtouint_from_user(buf, count, 0, &n);
Dmitry Vyukove41d58182017-07-12 14:34:35 -07001363 if (err)
1364 return err;
Akinobu Mita1203c8e2017-07-14 14:49:57 -07001365
1366 task = get_proc_task(file_inode(file));
1367 if (!task)
1368 return -ESRCH;
Alexey Dobriyan9f7118b2018-02-06 15:36:55 -08001369 task->fail_nth = n;
Akinobu Mita1203c8e2017-07-14 14:49:57 -07001370 put_task_struct(task);
1371
Dmitry Vyukove41d58182017-07-12 14:34:35 -07001372 return count;
1373}
1374
1375static ssize_t proc_fail_nth_read(struct file *file, char __user *buf,
1376 size_t count, loff_t *ppos)
1377{
1378 struct task_struct *task;
Akinobu Mitabfc74092017-07-14 14:49:54 -07001379 char numbuf[PROC_NUMBUF];
1380 ssize_t len;
Dmitry Vyukove41d58182017-07-12 14:34:35 -07001381
1382 task = get_proc_task(file_inode(file));
1383 if (!task)
1384 return -ESRCH;
Alexey Dobriyan9f7118b2018-02-06 15:36:55 -08001385 len = snprintf(numbuf, sizeof(numbuf), "%u\n", task->fail_nth);
Akinobu Mitabfc74092017-07-14 14:49:54 -07001386 len = simple_read_from_buffer(buf, count, ppos, numbuf, len);
Akinobu Mita1203c8e2017-07-14 14:49:57 -07001387 put_task_struct(task);
Akinobu Mitabfc74092017-07-14 14:49:54 -07001388
1389 return len;
Dmitry Vyukove41d58182017-07-12 14:34:35 -07001390}
1391
1392static const struct file_operations proc_fail_nth_operations = {
1393 .read = proc_fail_nth_read,
1394 .write = proc_fail_nth_write,
1395};
Akinobu Mitaf4f154f2006-12-08 02:39:47 -08001396#endif
1397
Arjan van de Ven97455122008-01-25 21:08:34 +01001398
Ingo Molnar43ae34c2007-07-09 18:52:00 +02001399#ifdef CONFIG_SCHED_DEBUG
1400/*
1401 * Print out various scheduling related per-task fields:
1402 */
1403static int sched_show(struct seq_file *m, void *v)
1404{
1405 struct inode *inode = m->private;
Christoph Hellwig76f668b2018-05-16 07:19:01 +02001406 struct pid_namespace *ns = proc_pid_ns(inode);
Ingo Molnar43ae34c2007-07-09 18:52:00 +02001407 struct task_struct *p;
1408
Ingo Molnar43ae34c2007-07-09 18:52:00 +02001409 p = get_proc_task(inode);
1410 if (!p)
1411 return -ESRCH;
Aleksa Sarai74dc3382017-08-06 14:41:41 +10001412 proc_sched_show_task(p, ns, m);
Ingo Molnar43ae34c2007-07-09 18:52:00 +02001413
1414 put_task_struct(p);
1415
1416 return 0;
1417}
1418
1419static ssize_t
1420sched_write(struct file *file, const char __user *buf,
1421 size_t count, loff_t *offset)
1422{
Al Viro496ad9a2013-01-23 17:07:38 -05001423 struct inode *inode = file_inode(file);
Ingo Molnar43ae34c2007-07-09 18:52:00 +02001424 struct task_struct *p;
1425
Ingo Molnar43ae34c2007-07-09 18:52:00 +02001426 p = get_proc_task(inode);
1427 if (!p)
1428 return -ESRCH;
1429 proc_sched_set_task(p);
1430
1431 put_task_struct(p);
1432
1433 return count;
1434}
1435
1436static int sched_open(struct inode *inode, struct file *filp)
1437{
Jovi Zhangc6a34052011-01-12 17:00:34 -08001438 return single_open(filp, sched_show, inode);
Ingo Molnar43ae34c2007-07-09 18:52:00 +02001439}
1440
1441static const struct file_operations proc_pid_sched_operations = {
1442 .open = sched_open,
1443 .read = seq_read,
1444 .write = sched_write,
1445 .llseek = seq_lseek,
Alexey Dobriyan5ea473a2007-07-31 00:38:50 -07001446 .release = single_release,
Ingo Molnar43ae34c2007-07-09 18:52:00 +02001447};
1448
1449#endif
1450
Mike Galbraith5091faa2010-11-30 14:18:03 +01001451#ifdef CONFIG_SCHED_AUTOGROUP
1452/*
1453 * Print out autogroup related information:
1454 */
1455static int sched_autogroup_show(struct seq_file *m, void *v)
1456{
1457 struct inode *inode = m->private;
1458 struct task_struct *p;
1459
1460 p = get_proc_task(inode);
1461 if (!p)
1462 return -ESRCH;
1463 proc_sched_autogroup_show_task(p, m);
1464
1465 put_task_struct(p);
1466
1467 return 0;
1468}
1469
1470static ssize_t
1471sched_autogroup_write(struct file *file, const char __user *buf,
1472 size_t count, loff_t *offset)
1473{
Al Viro496ad9a2013-01-23 17:07:38 -05001474 struct inode *inode = file_inode(file);
Mike Galbraith5091faa2010-11-30 14:18:03 +01001475 struct task_struct *p;
1476 char buffer[PROC_NUMBUF];
Alexey Dobriyan0a8cb8e2011-05-26 16:25:50 -07001477 int nice;
Mike Galbraith5091faa2010-11-30 14:18:03 +01001478 int err;
1479
1480 memset(buffer, 0, sizeof(buffer));
1481 if (count > sizeof(buffer) - 1)
1482 count = sizeof(buffer) - 1;
1483 if (copy_from_user(buffer, buf, count))
1484 return -EFAULT;
1485
Alexey Dobriyan0a8cb8e2011-05-26 16:25:50 -07001486 err = kstrtoint(strstrip(buffer), 0, &nice);
1487 if (err < 0)
1488 return err;
Mike Galbraith5091faa2010-11-30 14:18:03 +01001489
1490 p = get_proc_task(inode);
1491 if (!p)
1492 return -ESRCH;
1493
Hiroshi Shimamoto2e5b5b32012-02-23 17:41:27 +09001494 err = proc_sched_autogroup_set_nice(p, nice);
Mike Galbraith5091faa2010-11-30 14:18:03 +01001495 if (err)
1496 count = err;
1497
1498 put_task_struct(p);
1499
1500 return count;
1501}
1502
1503static int sched_autogroup_open(struct inode *inode, struct file *filp)
1504{
1505 int ret;
1506
1507 ret = single_open(filp, sched_autogroup_show, NULL);
1508 if (!ret) {
1509 struct seq_file *m = filp->private_data;
1510
1511 m->private = inode;
1512 }
1513 return ret;
1514}
1515
1516static const struct file_operations proc_pid_sched_autogroup_operations = {
1517 .open = sched_autogroup_open,
1518 .read = seq_read,
1519 .write = sched_autogroup_write,
1520 .llseek = seq_lseek,
1521 .release = single_release,
1522};
1523
1524#endif /* CONFIG_SCHED_AUTOGROUP */
1525
john stultz4614a696b2009-12-14 18:00:05 -08001526static ssize_t comm_write(struct file *file, const char __user *buf,
1527 size_t count, loff_t *offset)
1528{
Al Viro496ad9a2013-01-23 17:07:38 -05001529 struct inode *inode = file_inode(file);
john stultz4614a696b2009-12-14 18:00:05 -08001530 struct task_struct *p;
1531 char buffer[TASK_COMM_LEN];
David Rientjes830e0fc2013-04-30 15:28:18 -07001532 const size_t maxlen = sizeof(buffer) - 1;
john stultz4614a696b2009-12-14 18:00:05 -08001533
1534 memset(buffer, 0, sizeof(buffer));
David Rientjes830e0fc2013-04-30 15:28:18 -07001535 if (copy_from_user(buffer, buf, count > maxlen ? maxlen : count))
john stultz4614a696b2009-12-14 18:00:05 -08001536 return -EFAULT;
1537
1538 p = get_proc_task(inode);
1539 if (!p)
1540 return -ESRCH;
1541
1542 if (same_thread_group(current, p))
1543 set_task_comm(p, buffer);
1544 else
1545 count = -EINVAL;
1546
1547 put_task_struct(p);
1548
1549 return count;
1550}
1551
1552static int comm_show(struct seq_file *m, void *v)
1553{
1554 struct inode *inode = m->private;
1555 struct task_struct *p;
1556
1557 p = get_proc_task(inode);
1558 if (!p)
1559 return -ESRCH;
1560
Tejun Heo88b72b32018-05-18 08:47:13 -07001561 proc_task_name(m, p, false);
1562 seq_putc(m, '\n');
john stultz4614a696b2009-12-14 18:00:05 -08001563
1564 put_task_struct(p);
1565
1566 return 0;
1567}
1568
1569static int comm_open(struct inode *inode, struct file *filp)
1570{
Jovi Zhangc6a34052011-01-12 17:00:34 -08001571 return single_open(filp, comm_show, inode);
john stultz4614a696b2009-12-14 18:00:05 -08001572}
1573
1574static const struct file_operations proc_pid_set_comm_operations = {
1575 .open = comm_open,
1576 .read = seq_read,
1577 .write = comm_write,
1578 .llseek = seq_lseek,
1579 .release = single_release,
1580};
1581
Cyrill Gorcunov7773fbc2012-01-10 15:11:20 -08001582static int proc_exe_link(struct dentry *dentry, struct path *exe_path)
Matt Helsley925d1c42008-04-29 01:01:36 -07001583{
1584 struct task_struct *task;
Matt Helsley925d1c42008-04-29 01:01:36 -07001585 struct file *exe_file;
1586
David Howells2b0143b2015-03-17 22:25:59 +00001587 task = get_proc_task(d_inode(dentry));
Matt Helsley925d1c42008-04-29 01:01:36 -07001588 if (!task)
1589 return -ENOENT;
Mateusz Guzikcd81a9172016-08-23 16:20:38 +02001590 exe_file = get_task_exe_file(task);
Matt Helsley925d1c42008-04-29 01:01:36 -07001591 put_task_struct(task);
Matt Helsley925d1c42008-04-29 01:01:36 -07001592 if (exe_file) {
1593 *exe_path = exe_file->f_path;
1594 path_get(&exe_file->f_path);
1595 fput(exe_file);
1596 return 0;
1597 } else
1598 return -ENOENT;
1599}
1600
Al Viro6b255392015-11-17 10:20:54 -05001601static const char *proc_pid_get_link(struct dentry *dentry,
Al Virofceef392015-12-29 15:58:39 -05001602 struct inode *inode,
1603 struct delayed_call *done)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001604{
Christoph Hellwig408ef012012-06-18 10:47:03 -04001605 struct path path;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001606 int error = -EACCES;
1607
Al Viro6b255392015-11-17 10:20:54 -05001608 if (!dentry)
1609 return ERR_PTR(-ECHILD);
1610
Eric W. Biederman778c1142006-06-26 00:25:58 -07001611 /* Are we allowed to snoop on the tasks file descriptors? */
1612 if (!proc_fd_access_allowed(inode))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001613 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001614
Christoph Hellwig408ef012012-06-18 10:47:03 -04001615 error = PROC_I(inode)->op.proc_get_link(dentry, &path);
1616 if (error)
1617 goto out;
1618
Al Viro6e77137b2015-05-02 13:37:52 -04001619 nd_jump_link(&path);
Christoph Hellwig408ef012012-06-18 10:47:03 -04001620 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001621out:
Al Viro008b1502005-08-20 00:17:39 +01001622 return ERR_PTR(error);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001623}
1624
Jan Blunck3dcd25f2008-02-14 19:38:35 -08001625static int do_proc_readlink(struct path *path, char __user *buffer, int buflen)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001626{
Michal Hocko0ee931c2017-09-13 16:28:29 -07001627 char *tmp = (char *)__get_free_page(GFP_KERNEL);
Jan Blunck3dcd25f2008-02-14 19:38:35 -08001628 char *pathname;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001629 int len;
1630
1631 if (!tmp)
1632 return -ENOMEM;
Akinobu Mita0c28f282007-05-08 00:31:41 -07001633
Eric W. Biederman7b2a69b2010-12-05 15:51:21 -08001634 pathname = d_path(path, tmp, PAGE_SIZE);
Jan Blunck3dcd25f2008-02-14 19:38:35 -08001635 len = PTR_ERR(pathname);
1636 if (IS_ERR(pathname))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001637 goto out;
Jan Blunck3dcd25f2008-02-14 19:38:35 -08001638 len = tmp + PAGE_SIZE - 1 - pathname;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001639
1640 if (len > buflen)
1641 len = buflen;
Jan Blunck3dcd25f2008-02-14 19:38:35 -08001642 if (copy_to_user(buffer, pathname, len))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001643 len = -EFAULT;
1644 out:
1645 free_page((unsigned long)tmp);
1646 return len;
1647}
1648
1649static int proc_pid_readlink(struct dentry * dentry, char __user * buffer, int buflen)
1650{
1651 int error = -EACCES;
David Howells2b0143b2015-03-17 22:25:59 +00001652 struct inode *inode = d_inode(dentry);
Jan Blunck3dcd25f2008-02-14 19:38:35 -08001653 struct path path;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001654
Eric W. Biederman778c1142006-06-26 00:25:58 -07001655 /* Are we allowed to snoop on the tasks file descriptors? */
1656 if (!proc_fd_access_allowed(inode))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001657 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001658
Cyrill Gorcunov7773fbc2012-01-10 15:11:20 -08001659 error = PROC_I(inode)->op.proc_get_link(dentry, &path);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001660 if (error)
1661 goto out;
1662
Jan Blunck3dcd25f2008-02-14 19:38:35 -08001663 error = do_proc_readlink(&path, buffer, buflen);
1664 path_put(&path);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001665out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001666 return error;
1667}
1668
Cyrill Gorcunovfaf60af2012-08-23 14:43:24 +04001669const struct inode_operations proc_pid_link_inode_operations = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001670 .readlink = proc_pid_readlink,
Al Viro6b255392015-11-17 10:20:54 -05001671 .get_link = proc_pid_get_link,
Linus Torvalds6d76fa52006-07-15 12:26:45 -07001672 .setattr = proc_setattr,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001673};
1674
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001675
1676/* building an inode */
1677
Al Viroc6eb50d2017-09-30 14:45:42 -04001678void task_dump_owner(struct task_struct *task, umode_t mode,
Eric W. Biederman68eb94f2017-01-03 10:23:11 +13001679 kuid_t *ruid, kgid_t *rgid)
1680{
1681 /* Depending on the state of dumpable compute who should own a
1682 * proc file for a task.
1683 */
1684 const struct cred *cred;
1685 kuid_t uid;
1686 kgid_t gid;
1687
Alexey Dobriyan2e0ad552018-04-20 14:56:03 -07001688 if (unlikely(task->flags & PF_KTHREAD)) {
1689 *ruid = GLOBAL_ROOT_UID;
1690 *rgid = GLOBAL_ROOT_GID;
1691 return;
1692 }
1693
Eric W. Biederman68eb94f2017-01-03 10:23:11 +13001694 /* Default to the tasks effective ownership */
1695 rcu_read_lock();
1696 cred = __task_cred(task);
1697 uid = cred->euid;
1698 gid = cred->egid;
1699 rcu_read_unlock();
1700
1701 /*
1702 * Before the /proc/pid/status file was created the only way to read
1703 * the effective uid of a /process was to stat /proc/pid. Reading
1704 * /proc/pid/status is slow enough that procps and other packages
1705 * kept stating /proc/pid. To keep the rules in /proc simple I have
1706 * made this apply to all per process world readable and executable
1707 * directories.
1708 */
1709 if (mode != (S_IFDIR|S_IRUGO|S_IXUGO)) {
1710 struct mm_struct *mm;
1711 task_lock(task);
1712 mm = task->mm;
1713 /* Make non-dumpable tasks owned by some root */
1714 if (mm) {
1715 if (get_dumpable(mm) != SUID_DUMP_USER) {
1716 struct user_namespace *user_ns = mm->user_ns;
1717
1718 uid = make_kuid(user_ns, 0);
1719 if (!uid_valid(uid))
1720 uid = GLOBAL_ROOT_UID;
1721
1722 gid = make_kgid(user_ns, 0);
1723 if (!gid_valid(gid))
1724 gid = GLOBAL_ROOT_GID;
1725 }
1726 } else {
1727 uid = GLOBAL_ROOT_UID;
1728 gid = GLOBAL_ROOT_GID;
1729 }
1730 task_unlock(task);
1731 }
1732 *ruid = uid;
1733 *rgid = gid;
1734}
1735
Andreas Gruenbacherdb978da2016-11-10 22:18:28 +01001736struct inode *proc_pid_make_inode(struct super_block * sb,
1737 struct task_struct *task, umode_t mode)
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001738{
1739 struct inode * inode;
1740 struct proc_inode *ei;
1741
1742 /* We need a new inode */
1743
1744 inode = new_inode(sb);
1745 if (!inode)
1746 goto out;
1747
1748 /* Common stuff */
1749 ei = PROC_I(inode);
Andreas Gruenbacherdb978da2016-11-10 22:18:28 +01001750 inode->i_mode = mode;
Christoph Hellwig85fe4022010-10-23 11:19:54 -04001751 inode->i_ino = get_next_ino();
Deepa Dinamani078cd822016-09-14 07:48:04 -07001752 inode->i_mtime = inode->i_atime = inode->i_ctime = current_time(inode);
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001753 inode->i_op = &proc_def_inode_operations;
1754
1755 /*
1756 * grab the reference to task.
1757 */
Oleg Nesterov1a657f782006-10-02 02:18:59 -07001758 ei->pid = get_task_pid(task, PIDTYPE_PID);
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001759 if (!ei->pid)
1760 goto out_unlock;
1761
Eric W. Biederman68eb94f2017-01-03 10:23:11 +13001762 task_dump_owner(task, 0, &inode->i_uid, &inode->i_gid);
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001763 security_task_to_inode(task, inode);
1764
1765out:
1766 return inode;
1767
1768out_unlock:
1769 iput(inode);
1770 return NULL;
1771}
1772
David Howellsa528d352017-01-31 16:46:22 +00001773int pid_getattr(const struct path *path, struct kstat *stat,
1774 u32 request_mask, unsigned int query_flags)
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001775{
David Howellsa528d352017-01-31 16:46:22 +00001776 struct inode *inode = d_inode(path->dentry);
Christoph Hellwig76f668b2018-05-16 07:19:01 +02001777 struct pid_namespace *pid = proc_pid_ns(inode);
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001778 struct task_struct *task;
David Howellsc69e8d92008-11-14 10:39:19 +11001779
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001780 generic_fillattr(inode, stat);
1781
1782 rcu_read_lock();
Eric W. Biedermandcb0f222012-02-09 08:48:21 -08001783 stat->uid = GLOBAL_ROOT_UID;
1784 stat->gid = GLOBAL_ROOT_GID;
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001785 task = pid_task(proc_pid(inode), PIDTYPE_PID);
1786 if (task) {
Lafcadio Wluiki796f5712017-02-24 15:00:23 -08001787 if (!has_pid_permissions(pid, task, HIDEPID_INVISIBLE)) {
Vasiliy Kulikov04996802012-01-10 15:11:31 -08001788 rcu_read_unlock();
1789 /*
1790 * This doesn't prevent learning whether PID exists,
1791 * it only makes getattr() consistent with readdir().
1792 */
1793 return -ENOENT;
1794 }
Eric W. Biederman68eb94f2017-01-03 10:23:11 +13001795 task_dump_owner(task, inode->i_mode, &stat->uid, &stat->gid);
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001796 }
1797 rcu_read_unlock();
1798 return 0;
1799}
1800
1801/* dentry stuff */
1802
1803/*
Al Viro1bbc5512018-05-02 21:26:16 -04001804 * Set <pid>/... inode ownership (can change due to setuid(), etc.)
1805 */
1806void pid_update_inode(struct task_struct *task, struct inode *inode)
1807{
1808 task_dump_owner(task, inode->i_mode, &inode->i_uid, &inode->i_gid);
1809
1810 inode->i_mode &= ~(S_ISUID | S_ISGID);
1811 security_task_to_inode(task, inode);
1812}
1813
1814/*
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001815 * Rewrite the inode's ownerships here because the owning task may have
1816 * performed a setuid(), etc.
1817 *
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001818 */
Al Viro1bbc5512018-05-02 21:26:16 -04001819static int pid_revalidate(struct dentry *dentry, unsigned int flags)
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001820{
Nick Piggin34286d62011-01-07 17:49:57 +11001821 struct inode *inode;
1822 struct task_struct *task;
David Howellsc69e8d92008-11-14 10:39:19 +11001823
Al Viro0b728e12012-06-10 16:03:43 -04001824 if (flags & LOOKUP_RCU)
Nick Piggin34286d62011-01-07 17:49:57 +11001825 return -ECHILD;
1826
David Howells2b0143b2015-03-17 22:25:59 +00001827 inode = d_inode(dentry);
Nick Piggin34286d62011-01-07 17:49:57 +11001828 task = get_proc_task(inode);
1829
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001830 if (task) {
Al Viro1bbc5512018-05-02 21:26:16 -04001831 pid_update_inode(task, inode);
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001832 put_task_struct(task);
1833 return 1;
1834 }
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001835 return 0;
1836}
1837
Oleg Nesterovd855a4b2014-01-23 15:55:39 -08001838static inline bool proc_inode_is_dead(struct inode *inode)
1839{
1840 return !proc_pid(inode)->tasks[PIDTYPE_PID].first;
1841}
1842
David Howells1dd704b2013-04-12 01:08:50 +01001843int pid_delete_dentry(const struct dentry *dentry)
1844{
1845 /* Is the task we represent dead?
1846 * If so, then don't put the dentry on the lru list,
1847 * kill it immediately.
1848 */
David Howells2b0143b2015-03-17 22:25:59 +00001849 return proc_inode_is_dead(d_inode(dentry));
David Howells1dd704b2013-04-12 01:08:50 +01001850}
1851
Eric W. Biederman6b4e3062010-03-07 16:41:34 -08001852const struct dentry_operations pid_dentry_operations =
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001853{
1854 .d_revalidate = pid_revalidate,
1855 .d_delete = pid_delete_dentry,
1856};
1857
1858/* Lookups */
1859
Eric W. Biederman1c0d04c2006-10-02 02:18:57 -07001860/*
1861 * Fill a directory entry.
1862 *
1863 * If possible create the dcache entry and derive our inode number and
1864 * file type from dcache entry.
1865 *
1866 * Since all of the proc inode numbers are dynamically generated, the inode
1867 * numbers do not exist until the inode is cache. This means creating the
1868 * the dcache entry in readdir is necessary to keep the inode numbers
1869 * reported by readdir in sync with the inode numbers reported
1870 * by stat.
1871 */
Al Virof0c3b502013-05-16 12:07:31 -04001872bool proc_fill_cache(struct file *file, struct dir_context *ctx,
Eric W. Biederman6b4e3062010-03-07 16:41:34 -08001873 const char *name, int len,
Eric Dumazetc5141e62007-05-08 00:26:15 -07001874 instantiate_t instantiate, struct task_struct *task, const void *ptr)
Eric W. Biederman61a28782006-10-02 02:18:49 -07001875{
Al Virof0c3b502013-05-16 12:07:31 -04001876 struct dentry *child, *dir = file->f_path.dentry;
Al Viro1df98b82013-06-15 11:33:10 +04001877 struct qstr qname = QSTR_INIT(name, len);
Eric W. Biederman61a28782006-10-02 02:18:49 -07001878 struct inode *inode;
Al Viro0168b9e2018-05-03 09:21:05 -04001879 unsigned type = DT_UNKNOWN;
1880 ino_t ino = 1;
Eric W. Biederman61a28782006-10-02 02:18:49 -07001881
Al Viro1df98b82013-06-15 11:33:10 +04001882 child = d_hash_and_lookup(dir, &qname);
Eric W. Biederman61a28782006-10-02 02:18:49 -07001883 if (!child) {
Al Viro37817642016-04-20 16:31:31 -04001884 DECLARE_WAIT_QUEUE_HEAD_ONSTACK(wq);
1885 child = d_alloc_parallel(dir, &qname, &wq);
1886 if (IS_ERR(child))
Al Viro1df98b82013-06-15 11:33:10 +04001887 goto end_instantiate;
Al Viro37817642016-04-20 16:31:31 -04001888 if (d_in_lookup(child)) {
Al Viro0168b9e2018-05-03 09:21:05 -04001889 struct dentry *res;
1890 res = instantiate(child, task, ptr);
Al Viro37817642016-04-20 16:31:31 -04001891 d_lookup_done(child);
Al Viro0168b9e2018-05-03 09:21:05 -04001892 if (IS_ERR(res))
Al Viro37817642016-04-20 16:31:31 -04001893 goto end_instantiate;
Al Viro0168b9e2018-05-03 09:21:05 -04001894 if (unlikely(res)) {
1895 dput(child);
1896 child = res;
Al Viro37817642016-04-20 16:31:31 -04001897 }
Eric W. Biederman61a28782006-10-02 02:18:49 -07001898 }
1899 }
David Howells2b0143b2015-03-17 22:25:59 +00001900 inode = d_inode(child);
Al Viro147ce692013-06-15 10:26:35 +04001901 ino = inode->i_ino;
1902 type = inode->i_mode >> 12;
Al Viro0168b9e2018-05-03 09:21:05 -04001903end_instantiate:
Eric W. Biederman61a28782006-10-02 02:18:49 -07001904 dput(child);
Al Virof0c3b502013-05-16 12:07:31 -04001905 return dir_emit(ctx, name, len, ino, type);
Eric W. Biederman61a28782006-10-02 02:18:49 -07001906}
1907
Pavel Emelyanov640708a2012-01-10 15:11:23 -08001908/*
1909 * dname_to_vma_addr - maps a dentry name into two unsigned longs
1910 * which represent vma start and end addresses.
1911 */
1912static int dname_to_vma_addr(struct dentry *dentry,
1913 unsigned long *start, unsigned long *end)
1914{
Alexey Dobriyanac7f1062018-02-06 15:36:59 -08001915 const char *str = dentry->d_name.name;
1916 unsigned long long sval, eval;
1917 unsigned int len;
1918
Alexey Dobriyan35318db2018-04-10 16:41:14 -07001919 if (str[0] == '0' && str[1] != '-')
1920 return -EINVAL;
Alexey Dobriyanac7f1062018-02-06 15:36:59 -08001921 len = _parse_integer(str, 16, &sval);
1922 if (len & KSTRTOX_OVERFLOW)
Pavel Emelyanov640708a2012-01-10 15:11:23 -08001923 return -EINVAL;
Alexey Dobriyanac7f1062018-02-06 15:36:59 -08001924 if (sval != (unsigned long)sval)
1925 return -EINVAL;
1926 str += len;
1927
1928 if (*str != '-')
1929 return -EINVAL;
1930 str++;
1931
Alexey Dobriyan35318db2018-04-10 16:41:14 -07001932 if (str[0] == '0' && str[1])
1933 return -EINVAL;
Alexey Dobriyanac7f1062018-02-06 15:36:59 -08001934 len = _parse_integer(str, 16, &eval);
1935 if (len & KSTRTOX_OVERFLOW)
1936 return -EINVAL;
1937 if (eval != (unsigned long)eval)
1938 return -EINVAL;
1939 str += len;
1940
1941 if (*str != '\0')
1942 return -EINVAL;
1943
1944 *start = sval;
1945 *end = eval;
Pavel Emelyanov640708a2012-01-10 15:11:23 -08001946
1947 return 0;
1948}
1949
Al Viro0b728e12012-06-10 16:03:43 -04001950static int map_files_d_revalidate(struct dentry *dentry, unsigned int flags)
Pavel Emelyanov640708a2012-01-10 15:11:23 -08001951{
1952 unsigned long vm_start, vm_end;
1953 bool exact_vma_exists = false;
1954 struct mm_struct *mm = NULL;
1955 struct task_struct *task;
Pavel Emelyanov640708a2012-01-10 15:11:23 -08001956 struct inode *inode;
1957 int status = 0;
1958
Al Viro0b728e12012-06-10 16:03:43 -04001959 if (flags & LOOKUP_RCU)
Pavel Emelyanov640708a2012-01-10 15:11:23 -08001960 return -ECHILD;
1961
David Howells2b0143b2015-03-17 22:25:59 +00001962 inode = d_inode(dentry);
Pavel Emelyanov640708a2012-01-10 15:11:23 -08001963 task = get_proc_task(inode);
1964 if (!task)
1965 goto out_notask;
1966
Jann Horncaaee622016-01-20 15:00:04 -08001967 mm = mm_access(task, PTRACE_MODE_READ_FSCREDS);
Cong Wang2344bec2012-05-31 16:26:18 -07001968 if (IS_ERR_OR_NULL(mm))
Pavel Emelyanov640708a2012-01-10 15:11:23 -08001969 goto out;
1970
1971 if (!dname_to_vma_addr(dentry, &vm_start, &vm_end)) {
1972 down_read(&mm->mmap_sem);
1973 exact_vma_exists = !!find_exact_vma(mm, vm_start, vm_end);
1974 up_read(&mm->mmap_sem);
1975 }
1976
1977 mmput(mm);
1978
1979 if (exact_vma_exists) {
Eric W. Biederman68eb94f2017-01-03 10:23:11 +13001980 task_dump_owner(task, 0, &inode->i_uid, &inode->i_gid);
1981
Pavel Emelyanov640708a2012-01-10 15:11:23 -08001982 security_task_to_inode(task, inode);
1983 status = 1;
1984 }
1985
1986out:
1987 put_task_struct(task);
1988
1989out_notask:
Pavel Emelyanov640708a2012-01-10 15:11:23 -08001990 return status;
1991}
1992
1993static const struct dentry_operations tid_map_files_dentry_operations = {
1994 .d_revalidate = map_files_d_revalidate,
1995 .d_delete = pid_delete_dentry,
1996};
1997
Al Viro6b255392015-11-17 10:20:54 -05001998static int map_files_get_link(struct dentry *dentry, struct path *path)
Pavel Emelyanov640708a2012-01-10 15:11:23 -08001999{
2000 unsigned long vm_start, vm_end;
2001 struct vm_area_struct *vma;
2002 struct task_struct *task;
2003 struct mm_struct *mm;
2004 int rc;
2005
2006 rc = -ENOENT;
David Howells2b0143b2015-03-17 22:25:59 +00002007 task = get_proc_task(d_inode(dentry));
Pavel Emelyanov640708a2012-01-10 15:11:23 -08002008 if (!task)
2009 goto out;
2010
2011 mm = get_task_mm(task);
2012 put_task_struct(task);
2013 if (!mm)
2014 goto out;
2015
2016 rc = dname_to_vma_addr(dentry, &vm_start, &vm_end);
2017 if (rc)
2018 goto out_mmput;
2019
Artem Fetishev70335ab2014-03-10 15:49:45 -07002020 rc = -ENOENT;
Pavel Emelyanov640708a2012-01-10 15:11:23 -08002021 down_read(&mm->mmap_sem);
2022 vma = find_exact_vma(mm, vm_start, vm_end);
2023 if (vma && vma->vm_file) {
2024 *path = vma->vm_file->f_path;
2025 path_get(path);
2026 rc = 0;
2027 }
2028 up_read(&mm->mmap_sem);
2029
2030out_mmput:
2031 mmput(mm);
2032out:
2033 return rc;
2034}
2035
2036struct map_files_info {
Alexey Dobriyan20d28cd2018-02-06 15:37:06 -08002037 unsigned long start;
2038 unsigned long end;
Al Viro7b540d02012-08-27 14:55:26 -04002039 fmode_t mode;
Pavel Emelyanov640708a2012-01-10 15:11:23 -08002040};
2041
Calvin Owensbdb4d102015-09-09 15:35:54 -07002042/*
2043 * Only allow CAP_SYS_ADMIN to follow the links, due to concerns about how the
2044 * symlinks may be used to bypass permissions on ancestor directories in the
2045 * path to the file in question.
2046 */
2047static const char *
Al Viro6b255392015-11-17 10:20:54 -05002048proc_map_files_get_link(struct dentry *dentry,
Al Virofceef392015-12-29 15:58:39 -05002049 struct inode *inode,
2050 struct delayed_call *done)
Calvin Owensbdb4d102015-09-09 15:35:54 -07002051{
2052 if (!capable(CAP_SYS_ADMIN))
2053 return ERR_PTR(-EPERM);
2054
Al Virofceef392015-12-29 15:58:39 -05002055 return proc_pid_get_link(dentry, inode, done);
Calvin Owensbdb4d102015-09-09 15:35:54 -07002056}
2057
2058/*
Al Viro6b255392015-11-17 10:20:54 -05002059 * Identical to proc_pid_link_inode_operations except for get_link()
Calvin Owensbdb4d102015-09-09 15:35:54 -07002060 */
2061static const struct inode_operations proc_map_files_link_inode_operations = {
2062 .readlink = proc_pid_readlink,
Al Viro6b255392015-11-17 10:20:54 -05002063 .get_link = proc_map_files_get_link,
Calvin Owensbdb4d102015-09-09 15:35:54 -07002064 .setattr = proc_setattr,
2065};
2066
Al Viro0168b9e2018-05-03 09:21:05 -04002067static struct dentry *
2068proc_map_files_instantiate(struct dentry *dentry,
Pavel Emelyanov640708a2012-01-10 15:11:23 -08002069 struct task_struct *task, const void *ptr)
2070{
Al Viro7b540d02012-08-27 14:55:26 -04002071 fmode_t mode = (fmode_t)(unsigned long)ptr;
Pavel Emelyanov640708a2012-01-10 15:11:23 -08002072 struct proc_inode *ei;
2073 struct inode *inode;
2074
Al Viro0168b9e2018-05-03 09:21:05 -04002075 inode = proc_pid_make_inode(dentry->d_sb, task, S_IFLNK |
Andreas Gruenbacherdb978da2016-11-10 22:18:28 +01002076 ((mode & FMODE_READ ) ? S_IRUSR : 0) |
2077 ((mode & FMODE_WRITE) ? S_IWUSR : 0));
Pavel Emelyanov640708a2012-01-10 15:11:23 -08002078 if (!inode)
Al Viro0168b9e2018-05-03 09:21:05 -04002079 return ERR_PTR(-ENOENT);
Pavel Emelyanov640708a2012-01-10 15:11:23 -08002080
2081 ei = PROC_I(inode);
Al Viro6b255392015-11-17 10:20:54 -05002082 ei->op.proc_get_link = map_files_get_link;
Pavel Emelyanov640708a2012-01-10 15:11:23 -08002083
Calvin Owensbdb4d102015-09-09 15:35:54 -07002084 inode->i_op = &proc_map_files_link_inode_operations;
Pavel Emelyanov640708a2012-01-10 15:11:23 -08002085 inode->i_size = 64;
Pavel Emelyanov640708a2012-01-10 15:11:23 -08002086
2087 d_set_d_op(dentry, &tid_map_files_dentry_operations);
Al Viro0168b9e2018-05-03 09:21:05 -04002088 return d_splice_alias(inode, dentry);
Pavel Emelyanov640708a2012-01-10 15:11:23 -08002089}
2090
2091static struct dentry *proc_map_files_lookup(struct inode *dir,
Al Viro00cd8dd2012-06-10 17:13:09 -04002092 struct dentry *dentry, unsigned int flags)
Pavel Emelyanov640708a2012-01-10 15:11:23 -08002093{
2094 unsigned long vm_start, vm_end;
2095 struct vm_area_struct *vma;
2096 struct task_struct *task;
Al Viro0168b9e2018-05-03 09:21:05 -04002097 struct dentry *result;
Pavel Emelyanov640708a2012-01-10 15:11:23 -08002098 struct mm_struct *mm;
2099
Al Viro0168b9e2018-05-03 09:21:05 -04002100 result = ERR_PTR(-ENOENT);
Pavel Emelyanov640708a2012-01-10 15:11:23 -08002101 task = get_proc_task(dir);
2102 if (!task)
2103 goto out;
2104
Al Viro0168b9e2018-05-03 09:21:05 -04002105 result = ERR_PTR(-EACCES);
Jann Horncaaee622016-01-20 15:00:04 -08002106 if (!ptrace_may_access(task, PTRACE_MODE_READ_FSCREDS))
Pavel Emelyanov640708a2012-01-10 15:11:23 -08002107 goto out_put_task;
2108
Al Viro0168b9e2018-05-03 09:21:05 -04002109 result = ERR_PTR(-ENOENT);
Pavel Emelyanov640708a2012-01-10 15:11:23 -08002110 if (dname_to_vma_addr(dentry, &vm_start, &vm_end))
Cyrill Gorcunoveb94cd92012-05-17 17:03:25 -07002111 goto out_put_task;
Pavel Emelyanov640708a2012-01-10 15:11:23 -08002112
2113 mm = get_task_mm(task);
2114 if (!mm)
Cyrill Gorcunoveb94cd92012-05-17 17:03:25 -07002115 goto out_put_task;
Pavel Emelyanov640708a2012-01-10 15:11:23 -08002116
2117 down_read(&mm->mmap_sem);
2118 vma = find_exact_vma(mm, vm_start, vm_end);
2119 if (!vma)
2120 goto out_no_vma;
2121
Stanislav Kinsbursky05f56482012-11-26 16:29:42 -08002122 if (vma->vm_file)
Al Viro0168b9e2018-05-03 09:21:05 -04002123 result = proc_map_files_instantiate(dentry, task,
Stanislav Kinsbursky05f56482012-11-26 16:29:42 -08002124 (void *)(unsigned long)vma->vm_file->f_mode);
Pavel Emelyanov640708a2012-01-10 15:11:23 -08002125
2126out_no_vma:
2127 up_read(&mm->mmap_sem);
2128 mmput(mm);
Pavel Emelyanov640708a2012-01-10 15:11:23 -08002129out_put_task:
2130 put_task_struct(task);
2131out:
Al Viro0168b9e2018-05-03 09:21:05 -04002132 return result;
Pavel Emelyanov640708a2012-01-10 15:11:23 -08002133}
2134
2135static const struct inode_operations proc_map_files_inode_operations = {
2136 .lookup = proc_map_files_lookup,
2137 .permission = proc_fd_permission,
2138 .setattr = proc_setattr,
2139};
2140
2141static int
Al Virof0c3b502013-05-16 12:07:31 -04002142proc_map_files_readdir(struct file *file, struct dir_context *ctx)
Pavel Emelyanov640708a2012-01-10 15:11:23 -08002143{
Pavel Emelyanov640708a2012-01-10 15:11:23 -08002144 struct vm_area_struct *vma;
2145 struct task_struct *task;
2146 struct mm_struct *mm;
Al Virof0c3b502013-05-16 12:07:31 -04002147 unsigned long nr_files, pos, i;
2148 struct flex_array *fa = NULL;
2149 struct map_files_info info;
2150 struct map_files_info *p;
Pavel Emelyanov640708a2012-01-10 15:11:23 -08002151 int ret;
2152
Pavel Emelyanov640708a2012-01-10 15:11:23 -08002153 ret = -ENOENT;
Al Virof0c3b502013-05-16 12:07:31 -04002154 task = get_proc_task(file_inode(file));
Pavel Emelyanov640708a2012-01-10 15:11:23 -08002155 if (!task)
2156 goto out;
2157
2158 ret = -EACCES;
Jann Horncaaee622016-01-20 15:00:04 -08002159 if (!ptrace_may_access(task, PTRACE_MODE_READ_FSCREDS))
Pavel Emelyanov640708a2012-01-10 15:11:23 -08002160 goto out_put_task;
2161
2162 ret = 0;
Al Virof0c3b502013-05-16 12:07:31 -04002163 if (!dir_emit_dots(file, ctx))
2164 goto out_put_task;
Pavel Emelyanov640708a2012-01-10 15:11:23 -08002165
Al Virof0c3b502013-05-16 12:07:31 -04002166 mm = get_task_mm(task);
2167 if (!mm)
2168 goto out_put_task;
2169 down_read(&mm->mmap_sem);
Pavel Emelyanov640708a2012-01-10 15:11:23 -08002170
Al Virof0c3b502013-05-16 12:07:31 -04002171 nr_files = 0;
Pavel Emelyanov640708a2012-01-10 15:11:23 -08002172
Al Virof0c3b502013-05-16 12:07:31 -04002173 /*
2174 * We need two passes here:
2175 *
2176 * 1) Collect vmas of mapped files with mmap_sem taken
2177 * 2) Release mmap_sem and instantiate entries
2178 *
2179 * otherwise we get lockdep complained, since filldir()
2180 * routine might require mmap_sem taken in might_fault().
2181 */
Pavel Emelyanov640708a2012-01-10 15:11:23 -08002182
Al Virof0c3b502013-05-16 12:07:31 -04002183 for (vma = mm->mmap, pos = 2; vma; vma = vma->vm_next) {
2184 if (vma->vm_file && ++pos > ctx->pos)
2185 nr_files++;
Pavel Emelyanov640708a2012-01-10 15:11:23 -08002186 }
Al Virof0c3b502013-05-16 12:07:31 -04002187
2188 if (nr_files) {
2189 fa = flex_array_alloc(sizeof(info), nr_files,
2190 GFP_KERNEL);
2191 if (!fa || flex_array_prealloc(fa, 0, nr_files,
2192 GFP_KERNEL)) {
2193 ret = -ENOMEM;
2194 if (fa)
2195 flex_array_free(fa);
2196 up_read(&mm->mmap_sem);
2197 mmput(mm);
2198 goto out_put_task;
2199 }
2200 for (i = 0, vma = mm->mmap, pos = 2; vma;
2201 vma = vma->vm_next) {
2202 if (!vma->vm_file)
2203 continue;
2204 if (++pos <= ctx->pos)
2205 continue;
2206
Alexey Dobriyan20d28cd2018-02-06 15:37:06 -08002207 info.start = vma->vm_start;
2208 info.end = vma->vm_end;
Al Virof0c3b502013-05-16 12:07:31 -04002209 info.mode = vma->vm_file->f_mode;
Al Virof0c3b502013-05-16 12:07:31 -04002210 if (flex_array_put(fa, i++, &info, GFP_KERNEL))
2211 BUG();
2212 }
Pavel Emelyanov640708a2012-01-10 15:11:23 -08002213 }
Al Virof0c3b502013-05-16 12:07:31 -04002214 up_read(&mm->mmap_sem);
Alexey Dobriyanfe079a52018-04-10 16:32:05 -07002215 mmput(mm);
Al Virof0c3b502013-05-16 12:07:31 -04002216
2217 for (i = 0; i < nr_files; i++) {
Alexey Dobriyan20d28cd2018-02-06 15:37:06 -08002218 char buf[4 * sizeof(long) + 2]; /* max: %lx-%lx\0 */
2219 unsigned int len;
2220
Al Virof0c3b502013-05-16 12:07:31 -04002221 p = flex_array_get(fa, i);
Alexey Dobriyan20d28cd2018-02-06 15:37:06 -08002222 len = snprintf(buf, sizeof(buf), "%lx-%lx", p->start, p->end);
Al Virof0c3b502013-05-16 12:07:31 -04002223 if (!proc_fill_cache(file, ctx,
Alexey Dobriyan20d28cd2018-02-06 15:37:06 -08002224 buf, len,
Al Virof0c3b502013-05-16 12:07:31 -04002225 proc_map_files_instantiate,
2226 task,
2227 (void *)(unsigned long)p->mode))
2228 break;
2229 ctx->pos++;
2230 }
2231 if (fa)
2232 flex_array_free(fa);
Pavel Emelyanov640708a2012-01-10 15:11:23 -08002233
Pavel Emelyanov640708a2012-01-10 15:11:23 -08002234out_put_task:
2235 put_task_struct(task);
2236out:
2237 return ret;
2238}
2239
2240static const struct file_operations proc_map_files_operations = {
2241 .read = generic_read_dir,
Al Virof50752e2016-04-20 17:13:54 -04002242 .iterate_shared = proc_map_files_readdir,
2243 .llseek = generic_file_llseek,
Pavel Emelyanov640708a2012-01-10 15:11:23 -08002244};
2245
Nicolas Pitreb18b6a92017-01-21 00:09:08 -05002246#if defined(CONFIG_CHECKPOINT_RESTORE) && defined(CONFIG_POSIX_TIMERS)
Pavel Emelyanov48f6a7a2013-03-11 13:12:45 +04002247struct timers_private {
2248 struct pid *pid;
2249 struct task_struct *task;
2250 struct sighand_struct *sighand;
Pavel Emelyanov57b80152013-03-11 13:13:08 +04002251 struct pid_namespace *ns;
Pavel Emelyanov48f6a7a2013-03-11 13:12:45 +04002252 unsigned long flags;
2253};
2254
2255static void *timers_start(struct seq_file *m, loff_t *pos)
2256{
2257 struct timers_private *tp = m->private;
2258
2259 tp->task = get_pid_task(tp->pid, PIDTYPE_PID);
2260 if (!tp->task)
2261 return ERR_PTR(-ESRCH);
2262
2263 tp->sighand = lock_task_sighand(tp->task, &tp->flags);
2264 if (!tp->sighand)
2265 return ERR_PTR(-ESRCH);
2266
2267 return seq_list_start(&tp->task->signal->posix_timers, *pos);
2268}
2269
2270static void *timers_next(struct seq_file *m, void *v, loff_t *pos)
2271{
2272 struct timers_private *tp = m->private;
2273 return seq_list_next(v, &tp->task->signal->posix_timers, pos);
2274}
2275
2276static void timers_stop(struct seq_file *m, void *v)
2277{
2278 struct timers_private *tp = m->private;
2279
2280 if (tp->sighand) {
2281 unlock_task_sighand(tp->task, &tp->flags);
2282 tp->sighand = NULL;
2283 }
2284
2285 if (tp->task) {
2286 put_task_struct(tp->task);
2287 tp->task = NULL;
2288 }
2289}
2290
2291static int show_timer(struct seq_file *m, void *v)
2292{
2293 struct k_itimer *timer;
Pavel Emelyanov57b80152013-03-11 13:13:08 +04002294 struct timers_private *tp = m->private;
2295 int notify;
Alexey Dobriyancedbcca2014-08-08 14:21:33 -07002296 static const char * const nstr[] = {
Pavel Emelyanov57b80152013-03-11 13:13:08 +04002297 [SIGEV_SIGNAL] = "signal",
2298 [SIGEV_NONE] = "none",
2299 [SIGEV_THREAD] = "thread",
2300 };
Pavel Emelyanov48f6a7a2013-03-11 13:12:45 +04002301
2302 timer = list_entry((struct list_head *)v, struct k_itimer, list);
Pavel Emelyanov57b80152013-03-11 13:13:08 +04002303 notify = timer->it_sigev_notify;
2304
Pavel Emelyanov48f6a7a2013-03-11 13:12:45 +04002305 seq_printf(m, "ID: %d\n", timer->it_id);
Linus Torvaldsba3edf1f2017-12-06 18:23:27 -08002306 seq_printf(m, "signal: %d/%px\n",
Joe Perches25ce3192015-04-15 16:18:17 -07002307 timer->sigq->info.si_signo,
2308 timer->sigq->info.si_value.sival_ptr);
Pavel Emelyanov57b80152013-03-11 13:13:08 +04002309 seq_printf(m, "notify: %s/%s.%d\n",
Joe Perches25ce3192015-04-15 16:18:17 -07002310 nstr[notify & ~SIGEV_THREAD_ID],
2311 (notify & SIGEV_THREAD_ID) ? "tid" : "pid",
2312 pid_nr_ns(timer->it_pid, tp->ns));
Pavel Tikhomirov15ef0292013-05-17 02:12:03 +04002313 seq_printf(m, "ClockID: %d\n", timer->it_clock);
Pavel Emelyanov48f6a7a2013-03-11 13:12:45 +04002314
2315 return 0;
2316}
2317
2318static const struct seq_operations proc_timers_seq_ops = {
2319 .start = timers_start,
2320 .next = timers_next,
2321 .stop = timers_stop,
2322 .show = show_timer,
2323};
2324
2325static int proc_timers_open(struct inode *inode, struct file *file)
2326{
2327 struct timers_private *tp;
2328
2329 tp = __seq_open_private(file, &proc_timers_seq_ops,
2330 sizeof(struct timers_private));
2331 if (!tp)
2332 return -ENOMEM;
2333
2334 tp->pid = proc_pid(inode);
Christoph Hellwig76f668b2018-05-16 07:19:01 +02002335 tp->ns = proc_pid_ns(inode);
Pavel Emelyanov48f6a7a2013-03-11 13:12:45 +04002336 return 0;
2337}
2338
2339static const struct file_operations proc_timers_operations = {
2340 .open = proc_timers_open,
2341 .read = seq_read,
2342 .llseek = seq_lseek,
2343 .release = seq_release_private,
2344};
Eric Engestromb5946be2016-03-17 14:20:57 -07002345#endif
Pavel Emelyanov640708a2012-01-10 15:11:23 -08002346
John Stultz5de23d42016-03-17 14:20:54 -07002347static ssize_t timerslack_ns_write(struct file *file, const char __user *buf,
2348 size_t count, loff_t *offset)
2349{
2350 struct inode *inode = file_inode(file);
2351 struct task_struct *p;
2352 u64 slack_ns;
2353 int err;
2354
2355 err = kstrtoull_from_user(buf, count, 10, &slack_ns);
2356 if (err < 0)
2357 return err;
2358
2359 p = get_proc_task(inode);
2360 if (!p)
2361 return -ESRCH;
2362
John Stultz4b2bd5f2016-10-07 17:02:33 -07002363 if (p != current) {
2364 if (!capable(CAP_SYS_NICE)) {
2365 count = -EPERM;
2366 goto out;
2367 }
John Stultz5de23d42016-03-17 14:20:54 -07002368
John Stultz4b2bd5f2016-10-07 17:02:33 -07002369 err = security_task_setscheduler(p);
2370 if (err) {
2371 count = err;
2372 goto out;
2373 }
John Stultz904763e2016-10-07 17:02:29 -07002374 }
2375
John Stultz7abbaf92016-10-07 17:02:26 -07002376 task_lock(p);
2377 if (slack_ns == 0)
2378 p->timer_slack_ns = p->default_timer_slack_ns;
2379 else
2380 p->timer_slack_ns = slack_ns;
2381 task_unlock(p);
2382
2383out:
John Stultz5de23d42016-03-17 14:20:54 -07002384 put_task_struct(p);
2385
2386 return count;
2387}
2388
2389static int timerslack_ns_show(struct seq_file *m, void *v)
2390{
2391 struct inode *inode = m->private;
2392 struct task_struct *p;
John Stultz7abbaf92016-10-07 17:02:26 -07002393 int err = 0;
John Stultz5de23d42016-03-17 14:20:54 -07002394
2395 p = get_proc_task(inode);
2396 if (!p)
2397 return -ESRCH;
2398
John Stultz4b2bd5f2016-10-07 17:02:33 -07002399 if (p != current) {
John Stultz5de23d42016-03-17 14:20:54 -07002400
John Stultz4b2bd5f2016-10-07 17:02:33 -07002401 if (!capable(CAP_SYS_NICE)) {
2402 err = -EPERM;
2403 goto out;
2404 }
2405 err = security_task_getscheduler(p);
2406 if (err)
2407 goto out;
2408 }
John Stultz904763e2016-10-07 17:02:29 -07002409
John Stultz7abbaf92016-10-07 17:02:26 -07002410 task_lock(p);
2411 seq_printf(m, "%llu\n", p->timer_slack_ns);
2412 task_unlock(p);
2413
2414out:
John Stultz5de23d42016-03-17 14:20:54 -07002415 put_task_struct(p);
2416
2417 return err;
2418}
2419
2420static int timerslack_ns_open(struct inode *inode, struct file *filp)
2421{
2422 return single_open(filp, timerslack_ns_show, inode);
2423}
2424
2425static const struct file_operations proc_pid_set_timerslack_ns_operations = {
2426 .open = timerslack_ns_open,
2427 .read = seq_read,
2428 .write = timerslack_ns_write,
2429 .llseek = seq_lseek,
2430 .release = single_release,
2431};
2432
Al Viro0168b9e2018-05-03 09:21:05 -04002433static struct dentry *proc_pident_instantiate(struct dentry *dentry,
2434 struct task_struct *task, const void *ptr)
Eric W. Biederman444ceed2006-10-02 02:18:49 -07002435{
Eric Dumazetc5141e62007-05-08 00:26:15 -07002436 const struct pid_entry *p = ptr;
Eric W. Biederman444ceed2006-10-02 02:18:49 -07002437 struct inode *inode;
2438 struct proc_inode *ei;
Eric W. Biederman444ceed2006-10-02 02:18:49 -07002439
Al Viro0168b9e2018-05-03 09:21:05 -04002440 inode = proc_pid_make_inode(dentry->d_sb, task, p->mode);
Eric W. Biederman444ceed2006-10-02 02:18:49 -07002441 if (!inode)
Al Viro0168b9e2018-05-03 09:21:05 -04002442 return ERR_PTR(-ENOENT);
Eric W. Biederman444ceed2006-10-02 02:18:49 -07002443
2444 ei = PROC_I(inode);
Eric W. Biederman444ceed2006-10-02 02:18:49 -07002445 if (S_ISDIR(inode->i_mode))
Miklos Szeredibfe86842011-10-28 14:13:29 +02002446 set_nlink(inode, 2); /* Use getattr to fix if necessary */
Eric W. Biederman444ceed2006-10-02 02:18:49 -07002447 if (p->iop)
2448 inode->i_op = p->iop;
2449 if (p->fop)
2450 inode->i_fop = p->fop;
2451 ei->op = p->op;
Al Viro1bbc5512018-05-02 21:26:16 -04002452 pid_update_inode(task, inode);
Nick Pigginfb045ad2011-01-07 17:49:55 +11002453 d_set_d_op(dentry, &pid_dentry_operations);
Al Viro0168b9e2018-05-03 09:21:05 -04002454 return d_splice_alias(inode, dentry);
Eric W. Biederman444ceed2006-10-02 02:18:49 -07002455}
2456
Linus Torvalds1da177e2005-04-16 15:20:36 -07002457static struct dentry *proc_pident_lookup(struct inode *dir,
2458 struct dentry *dentry,
Eric Dumazetc5141e62007-05-08 00:26:15 -07002459 const struct pid_entry *ents,
Eric W. Biederman7bcd6b02006-10-02 02:18:56 -07002460 unsigned int nents)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002461{
Eric W. Biederman99f89552006-06-26 00:25:55 -07002462 struct task_struct *task = get_proc_task(dir);
Eric Dumazetc5141e62007-05-08 00:26:15 -07002463 const struct pid_entry *p, *last;
Al Viro0168b9e2018-05-03 09:21:05 -04002464 struct dentry *res = ERR_PTR(-ENOENT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002465
Eric W. Biederman99f89552006-06-26 00:25:55 -07002466 if (!task)
2467 goto out_no_task;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002468
Eric W. Biederman20cdc892006-10-02 02:17:07 -07002469 /*
2470 * Yes, it does not scale. And it should not. Don't add
2471 * new entries into /proc/<tgid>/ without very good reasons.
2472 */
Alexey Dobriyanbac5f5d2016-12-12 16:45:28 -08002473 last = &ents[nents];
2474 for (p = ents; p < last; p++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002475 if (p->len != dentry->d_name.len)
2476 continue;
2477 if (!memcmp(dentry->d_name.name, p->name, p->len))
2478 break;
2479 }
Alexey Dobriyanbac5f5d2016-12-12 16:45:28 -08002480 if (p >= last)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002481 goto out;
2482
Al Viro0168b9e2018-05-03 09:21:05 -04002483 res = proc_pident_instantiate(dentry, task, p);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002484out:
Eric W. Biederman99f89552006-06-26 00:25:55 -07002485 put_task_struct(task);
2486out_no_task:
Al Viro0168b9e2018-05-03 09:21:05 -04002487 return res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002488}
2489
Al Virof0c3b502013-05-16 12:07:31 -04002490static int proc_pident_readdir(struct file *file, struct dir_context *ctx,
Eric Dumazetc5141e62007-05-08 00:26:15 -07002491 const struct pid_entry *ents, unsigned int nents)
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002492{
Al Virof0c3b502013-05-16 12:07:31 -04002493 struct task_struct *task = get_proc_task(file_inode(file));
2494 const struct pid_entry *p;
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002495
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002496 if (!task)
Al Virof0c3b502013-05-16 12:07:31 -04002497 return -ENOENT;
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002498
Al Virof0c3b502013-05-16 12:07:31 -04002499 if (!dir_emit_dots(file, ctx))
2500 goto out;
2501
2502 if (ctx->pos >= nents + 2)
2503 goto out;
2504
Alexey Dobriyanbac5f5d2016-12-12 16:45:28 -08002505 for (p = ents + (ctx->pos - 2); p < ents + nents; p++) {
Al Virof0c3b502013-05-16 12:07:31 -04002506 if (!proc_fill_cache(file, ctx, p->name, p->len,
2507 proc_pident_instantiate, task, p))
2508 break;
2509 ctx->pos++;
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002510 }
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002511out:
Eric W. Biederman61a28782006-10-02 02:18:49 -07002512 put_task_struct(task);
Al Virof0c3b502013-05-16 12:07:31 -04002513 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002514}
2515
Linus Torvalds1da177e2005-04-16 15:20:36 -07002516#ifdef CONFIG_SECURITY
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002517static ssize_t proc_pid_attr_read(struct file * file, char __user * buf,
2518 size_t count, loff_t *ppos)
2519{
Al Viro496ad9a2013-01-23 17:07:38 -05002520 struct inode * inode = file_inode(file);
Al Viro04ff9702007-03-12 16:17:58 +00002521 char *p = NULL;
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002522 ssize_t length;
2523 struct task_struct *task = get_proc_task(inode);
2524
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002525 if (!task)
Al Viro04ff9702007-03-12 16:17:58 +00002526 return -ESRCH;
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002527
2528 length = security_getprocattr(task,
Josef "Jeff" Sipek2fddfee2006-12-08 02:36:36 -08002529 (char*)file->f_path.dentry->d_name.name,
Al Viro04ff9702007-03-12 16:17:58 +00002530 &p);
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002531 put_task_struct(task);
Al Viro04ff9702007-03-12 16:17:58 +00002532 if (length > 0)
2533 length = simple_read_from_buffer(buf, count, ppos, p, length);
2534 kfree(p);
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002535 return length;
2536}
2537
2538static ssize_t proc_pid_attr_write(struct file * file, const char __user * buf,
2539 size_t count, loff_t *ppos)
2540{
Al Viro496ad9a2013-01-23 17:07:38 -05002541 struct inode * inode = file_inode(file);
Al Virobb646cd2015-12-24 00:16:30 -05002542 void *page;
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002543 ssize_t length;
2544 struct task_struct *task = get_proc_task(inode);
2545
2546 length = -ESRCH;
2547 if (!task)
2548 goto out_no_task;
Stephen Smalleyb21507e2017-01-09 10:07:31 -05002549
2550 /* A task may only write its own attributes. */
2551 length = -EACCES;
2552 if (current != task)
2553 goto out;
2554
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002555 if (count > PAGE_SIZE)
2556 count = PAGE_SIZE;
2557
2558 /* No partial writes. */
2559 length = -EINVAL;
2560 if (*ppos != 0)
2561 goto out;
2562
Al Virobb646cd2015-12-24 00:16:30 -05002563 page = memdup_user(buf, count);
2564 if (IS_ERR(page)) {
2565 length = PTR_ERR(page);
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002566 goto out;
Al Virobb646cd2015-12-24 00:16:30 -05002567 }
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002568
David Howells107db7c2009-05-08 13:55:27 +01002569 /* Guard against adverse ptrace interaction */
Stephen Smalleyb21507e2017-01-09 10:07:31 -05002570 length = mutex_lock_interruptible(&current->signal->cred_guard_mutex);
David Howells107db7c2009-05-08 13:55:27 +01002571 if (length < 0)
2572 goto out_free;
2573
Stephen Smalleyb21507e2017-01-09 10:07:31 -05002574 length = security_setprocattr(file->f_path.dentry->d_name.name,
Al Virobb646cd2015-12-24 00:16:30 -05002575 page, count);
Stephen Smalleyb21507e2017-01-09 10:07:31 -05002576 mutex_unlock(&current->signal->cred_guard_mutex);
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002577out_free:
Al Virobb646cd2015-12-24 00:16:30 -05002578 kfree(page);
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002579out:
2580 put_task_struct(task);
2581out_no_task:
2582 return length;
2583}
2584
Arjan van de Ven00977a52007-02-12 00:55:34 -08002585static const struct file_operations proc_pid_attr_operations = {
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002586 .read = proc_pid_attr_read,
2587 .write = proc_pid_attr_write,
Arnd Bergmann87df8422010-03-17 23:06:02 +01002588 .llseek = generic_file_llseek,
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002589};
2590
Eric Dumazetc5141e62007-05-08 00:26:15 -07002591static const struct pid_entry attr_dir_stuff[] = {
Alexey Dobriyan631f9c12008-11-10 01:32:52 +03002592 REG("current", S_IRUGO|S_IWUGO, proc_pid_attr_operations),
2593 REG("prev", S_IRUGO, proc_pid_attr_operations),
2594 REG("exec", S_IRUGO|S_IWUGO, proc_pid_attr_operations),
2595 REG("fscreate", S_IRUGO|S_IWUGO, proc_pid_attr_operations),
2596 REG("keycreate", S_IRUGO|S_IWUGO, proc_pid_attr_operations),
2597 REG("sockcreate", S_IRUGO|S_IWUGO, proc_pid_attr_operations),
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002598};
2599
Al Virof0c3b502013-05-16 12:07:31 -04002600static int proc_attr_dir_readdir(struct file *file, struct dir_context *ctx)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002601{
Al Virof0c3b502013-05-16 12:07:31 -04002602 return proc_pident_readdir(file, ctx,
2603 attr_dir_stuff, ARRAY_SIZE(attr_dir_stuff));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002604}
2605
Arjan van de Ven00977a52007-02-12 00:55:34 -08002606static const struct file_operations proc_attr_dir_operations = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002607 .read = generic_read_dir,
Al Virof50752e2016-04-20 17:13:54 -04002608 .iterate_shared = proc_attr_dir_readdir,
2609 .llseek = generic_file_llseek,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002610};
2611
Eric W. Biederman72d9dcf2006-10-02 02:18:50 -07002612static struct dentry *proc_attr_dir_lookup(struct inode *dir,
Al Viro00cd8dd2012-06-10 17:13:09 -04002613 struct dentry *dentry, unsigned int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002614{
Eric W. Biederman7bcd6b02006-10-02 02:18:56 -07002615 return proc_pident_lookup(dir, dentry,
2616 attr_dir_stuff, ARRAY_SIZE(attr_dir_stuff));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002617}
2618
Arjan van de Venc5ef1c42007-02-12 00:55:40 -08002619static const struct inode_operations proc_attr_dir_inode_operations = {
Eric W. Biederman72d9dcf2006-10-02 02:18:50 -07002620 .lookup = proc_attr_dir_lookup,
Eric W. Biederman99f89552006-06-26 00:25:55 -07002621 .getattr = pid_getattr,
Linus Torvalds6d76fa52006-07-15 12:26:45 -07002622 .setattr = proc_setattr,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002623};
2624
Linus Torvalds1da177e2005-04-16 15:20:36 -07002625#endif
2626
Christoph Hellwig698ba7b2009-12-15 16:47:37 -08002627#ifdef CONFIG_ELF_CORE
Kawai, Hidehiro3cb4a0b2007-07-19 01:48:28 -07002628static ssize_t proc_coredump_filter_read(struct file *file, char __user *buf,
2629 size_t count, loff_t *ppos)
2630{
Al Viro496ad9a2013-01-23 17:07:38 -05002631 struct task_struct *task = get_proc_task(file_inode(file));
Kawai, Hidehiro3cb4a0b2007-07-19 01:48:28 -07002632 struct mm_struct *mm;
2633 char buffer[PROC_NUMBUF];
2634 size_t len;
2635 int ret;
2636
2637 if (!task)
2638 return -ESRCH;
2639
2640 ret = 0;
2641 mm = get_task_mm(task);
2642 if (mm) {
2643 len = snprintf(buffer, sizeof(buffer), "%08lx\n",
2644 ((mm->flags & MMF_DUMP_FILTER_MASK) >>
2645 MMF_DUMP_FILTER_SHIFT));
2646 mmput(mm);
2647 ret = simple_read_from_buffer(buf, count, ppos, buffer, len);
2648 }
2649
2650 put_task_struct(task);
2651
2652 return ret;
2653}
2654
2655static ssize_t proc_coredump_filter_write(struct file *file,
2656 const char __user *buf,
2657 size_t count,
2658 loff_t *ppos)
2659{
2660 struct task_struct *task;
2661 struct mm_struct *mm;
Kawai, Hidehiro3cb4a0b2007-07-19 01:48:28 -07002662 unsigned int val;
2663 int ret;
2664 int i;
2665 unsigned long mask;
2666
Alexey Dobriyan774636e2015-09-09 15:36:59 -07002667 ret = kstrtouint_from_user(buf, count, 0, &val);
2668 if (ret < 0)
2669 return ret;
Kawai, Hidehiro3cb4a0b2007-07-19 01:48:28 -07002670
2671 ret = -ESRCH;
Al Viro496ad9a2013-01-23 17:07:38 -05002672 task = get_proc_task(file_inode(file));
Kawai, Hidehiro3cb4a0b2007-07-19 01:48:28 -07002673 if (!task)
2674 goto out_no_task;
2675
Kawai, Hidehiro3cb4a0b2007-07-19 01:48:28 -07002676 mm = get_task_mm(task);
2677 if (!mm)
2678 goto out_no_mm;
Colin Ian King41a0c2492015-12-18 14:22:01 -08002679 ret = 0;
Kawai, Hidehiro3cb4a0b2007-07-19 01:48:28 -07002680
2681 for (i = 0, mask = 1; i < MMF_DUMP_FILTER_BITS; i++, mask <<= 1) {
2682 if (val & mask)
2683 set_bit(i + MMF_DUMP_FILTER_SHIFT, &mm->flags);
2684 else
2685 clear_bit(i + MMF_DUMP_FILTER_SHIFT, &mm->flags);
2686 }
2687
2688 mmput(mm);
2689 out_no_mm:
2690 put_task_struct(task);
2691 out_no_task:
Alexey Dobriyan774636e2015-09-09 15:36:59 -07002692 if (ret < 0)
2693 return ret;
2694 return count;
Kawai, Hidehiro3cb4a0b2007-07-19 01:48:28 -07002695}
2696
2697static const struct file_operations proc_coredump_filter_operations = {
2698 .read = proc_coredump_filter_read,
2699 .write = proc_coredump_filter_write,
Arnd Bergmann87df8422010-03-17 23:06:02 +01002700 .llseek = generic_file_llseek,
Kawai, Hidehiro3cb4a0b2007-07-19 01:48:28 -07002701};
2702#endif
2703
Andrew Mortonaba76fd2006-12-10 02:19:48 -08002704#ifdef CONFIG_TASK_IO_ACCOUNTING
Alexey Dobriyan19aadc92014-08-08 14:21:50 -07002705static int do_io_accounting(struct task_struct *task, struct seq_file *m, int whole)
Andrew Mortonaba76fd2006-12-10 02:19:48 -08002706{
Andrea Righi940389b2008-07-28 00:48:12 +02002707 struct task_io_accounting acct = task->ioac;
Andrea Righi59954772008-07-27 17:29:15 +02002708 unsigned long flags;
Vasiliy Kulikov293eb1e2011-07-26 16:08:38 -07002709 int result;
Andrea Righi297c5d92008-07-25 01:48:49 -07002710
Vasiliy Kulikov293eb1e2011-07-26 16:08:38 -07002711 result = mutex_lock_killable(&task->signal->cred_guard_mutex);
2712 if (result)
2713 return result;
2714
Jann Horncaaee622016-01-20 15:00:04 -08002715 if (!ptrace_may_access(task, PTRACE_MODE_READ_FSCREDS)) {
Vasiliy Kulikov293eb1e2011-07-26 16:08:38 -07002716 result = -EACCES;
2717 goto out_unlock;
2718 }
Vasiliy Kulikov1d1221f2011-06-24 16:08:38 +04002719
Andrea Righi59954772008-07-27 17:29:15 +02002720 if (whole && lock_task_sighand(task, &flags)) {
2721 struct task_struct *t = task;
Andrea Righib2d002d2008-07-26 15:22:27 -07002722
Andrea Righi59954772008-07-27 17:29:15 +02002723 task_io_accounting_add(&acct, &task->signal->ioac);
2724 while_each_thread(task, t)
2725 task_io_accounting_add(&acct, &t->ioac);
Andrea Righi297c5d92008-07-25 01:48:49 -07002726
Andrea Righi59954772008-07-27 17:29:15 +02002727 unlock_task_sighand(task, &flags);
Andrea Righi297c5d92008-07-25 01:48:49 -07002728 }
Joe Perches25ce3192015-04-15 16:18:17 -07002729 seq_printf(m,
2730 "rchar: %llu\n"
2731 "wchar: %llu\n"
2732 "syscr: %llu\n"
2733 "syscw: %llu\n"
2734 "read_bytes: %llu\n"
2735 "write_bytes: %llu\n"
2736 "cancelled_write_bytes: %llu\n",
2737 (unsigned long long)acct.rchar,
2738 (unsigned long long)acct.wchar,
2739 (unsigned long long)acct.syscr,
2740 (unsigned long long)acct.syscw,
2741 (unsigned long long)acct.read_bytes,
2742 (unsigned long long)acct.write_bytes,
2743 (unsigned long long)acct.cancelled_write_bytes);
2744 result = 0;
2745
Vasiliy Kulikov293eb1e2011-07-26 16:08:38 -07002746out_unlock:
2747 mutex_unlock(&task->signal->cred_guard_mutex);
2748 return result;
Andrew Mortonaba76fd2006-12-10 02:19:48 -08002749}
Andrea Righi297c5d92008-07-25 01:48:49 -07002750
Alexey Dobriyan19aadc92014-08-08 14:21:50 -07002751static int proc_tid_io_accounting(struct seq_file *m, struct pid_namespace *ns,
2752 struct pid *pid, struct task_struct *task)
Andrea Righi297c5d92008-07-25 01:48:49 -07002753{
Alexey Dobriyan19aadc92014-08-08 14:21:50 -07002754 return do_io_accounting(task, m, 0);
Andrea Righi297c5d92008-07-25 01:48:49 -07002755}
2756
Alexey Dobriyan19aadc92014-08-08 14:21:50 -07002757static int proc_tgid_io_accounting(struct seq_file *m, struct pid_namespace *ns,
2758 struct pid *pid, struct task_struct *task)
Andrea Righi297c5d92008-07-25 01:48:49 -07002759{
Alexey Dobriyan19aadc92014-08-08 14:21:50 -07002760 return do_io_accounting(task, m, 1);
Andrea Righi297c5d92008-07-25 01:48:49 -07002761}
2762#endif /* CONFIG_TASK_IO_ACCOUNTING */
Andrew Mortonaba76fd2006-12-10 02:19:48 -08002763
Eric W. Biederman22d917d2011-11-17 00:11:58 -08002764#ifdef CONFIG_USER_NS
2765static int proc_id_map_open(struct inode *inode, struct file *file,
Fabian Frederickccf94f12014-08-08 14:21:22 -07002766 const struct seq_operations *seq_ops)
Eric W. Biederman22d917d2011-11-17 00:11:58 -08002767{
2768 struct user_namespace *ns = NULL;
2769 struct task_struct *task;
2770 struct seq_file *seq;
2771 int ret = -EINVAL;
2772
2773 task = get_proc_task(inode);
2774 if (task) {
2775 rcu_read_lock();
2776 ns = get_user_ns(task_cred_xxx(task, user_ns));
2777 rcu_read_unlock();
2778 put_task_struct(task);
2779 }
2780 if (!ns)
2781 goto err;
2782
2783 ret = seq_open(file, seq_ops);
2784 if (ret)
2785 goto err_put_ns;
2786
2787 seq = file->private_data;
2788 seq->private = ns;
2789
2790 return 0;
2791err_put_ns:
2792 put_user_ns(ns);
2793err:
2794 return ret;
2795}
2796
2797static int proc_id_map_release(struct inode *inode, struct file *file)
2798{
2799 struct seq_file *seq = file->private_data;
2800 struct user_namespace *ns = seq->private;
2801 put_user_ns(ns);
2802 return seq_release(inode, file);
2803}
2804
2805static int proc_uid_map_open(struct inode *inode, struct file *file)
2806{
2807 return proc_id_map_open(inode, file, &proc_uid_seq_operations);
2808}
2809
2810static int proc_gid_map_open(struct inode *inode, struct file *file)
2811{
2812 return proc_id_map_open(inode, file, &proc_gid_seq_operations);
2813}
2814
Eric W. Biedermanf76d2072012-08-30 01:24:05 -07002815static int proc_projid_map_open(struct inode *inode, struct file *file)
2816{
2817 return proc_id_map_open(inode, file, &proc_projid_seq_operations);
2818}
2819
Eric W. Biederman22d917d2011-11-17 00:11:58 -08002820static const struct file_operations proc_uid_map_operations = {
2821 .open = proc_uid_map_open,
2822 .write = proc_uid_map_write,
2823 .read = seq_read,
2824 .llseek = seq_lseek,
2825 .release = proc_id_map_release,
2826};
2827
2828static const struct file_operations proc_gid_map_operations = {
2829 .open = proc_gid_map_open,
2830 .write = proc_gid_map_write,
2831 .read = seq_read,
2832 .llseek = seq_lseek,
2833 .release = proc_id_map_release,
2834};
Eric W. Biedermanf76d2072012-08-30 01:24:05 -07002835
2836static const struct file_operations proc_projid_map_operations = {
2837 .open = proc_projid_map_open,
2838 .write = proc_projid_map_write,
2839 .read = seq_read,
2840 .llseek = seq_lseek,
2841 .release = proc_id_map_release,
2842};
Eric W. Biederman9cc46512014-12-02 12:27:26 -06002843
2844static int proc_setgroups_open(struct inode *inode, struct file *file)
2845{
2846 struct user_namespace *ns = NULL;
2847 struct task_struct *task;
2848 int ret;
2849
2850 ret = -ESRCH;
2851 task = get_proc_task(inode);
2852 if (task) {
2853 rcu_read_lock();
2854 ns = get_user_ns(task_cred_xxx(task, user_ns));
2855 rcu_read_unlock();
2856 put_task_struct(task);
2857 }
2858 if (!ns)
2859 goto err;
2860
2861 if (file->f_mode & FMODE_WRITE) {
2862 ret = -EACCES;
2863 if (!ns_capable(ns, CAP_SYS_ADMIN))
2864 goto err_put_ns;
2865 }
2866
2867 ret = single_open(file, &proc_setgroups_show, ns);
2868 if (ret)
2869 goto err_put_ns;
2870
2871 return 0;
2872err_put_ns:
2873 put_user_ns(ns);
2874err:
2875 return ret;
2876}
2877
2878static int proc_setgroups_release(struct inode *inode, struct file *file)
2879{
2880 struct seq_file *seq = file->private_data;
2881 struct user_namespace *ns = seq->private;
2882 int ret = single_release(inode, file);
2883 put_user_ns(ns);
2884 return ret;
2885}
2886
2887static const struct file_operations proc_setgroups_operations = {
2888 .open = proc_setgroups_open,
2889 .write = proc_setgroups_write,
2890 .read = seq_read,
2891 .llseek = seq_lseek,
2892 .release = proc_setgroups_release,
2893};
Eric W. Biederman22d917d2011-11-17 00:11:58 -08002894#endif /* CONFIG_USER_NS */
2895
Kees Cook47830722008-10-06 03:11:58 +04002896static int proc_pid_personality(struct seq_file *m, struct pid_namespace *ns,
2897 struct pid *pid, struct task_struct *task)
2898{
Al Viroa9712bc2011-03-23 15:52:50 -04002899 int err = lock_trace(task);
2900 if (!err) {
2901 seq_printf(m, "%08x\n", task->personality);
2902 unlock_trace(task);
2903 }
2904 return err;
Kees Cook47830722008-10-06 03:11:58 +04002905}
2906
Josh Poimboeuf7c23b332017-02-13 19:42:41 -06002907#ifdef CONFIG_LIVEPATCH
2908static int proc_pid_patch_state(struct seq_file *m, struct pid_namespace *ns,
2909 struct pid *pid, struct task_struct *task)
2910{
2911 seq_printf(m, "%d\n", task->patch_state);
2912 return 0;
2913}
2914#endif /* CONFIG_LIVEPATCH */
2915
Eric W. Biederman801199c2006-10-02 02:18:48 -07002916/*
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002917 * Thread groups
2918 */
Arjan van de Ven00977a52007-02-12 00:55:34 -08002919static const struct file_operations proc_task_operations;
Arjan van de Venc5ef1c42007-02-12 00:55:40 -08002920static const struct inode_operations proc_task_inode_operations;
Eric W. Biederman20cdc892006-10-02 02:17:07 -07002921
Eric Dumazetc5141e62007-05-08 00:26:15 -07002922static const struct pid_entry tgid_base_stuff[] = {
Alexey Dobriyan631f9c12008-11-10 01:32:52 +03002923 DIR("task", S_IRUGO|S_IXUGO, proc_task_inode_operations, proc_task_operations),
2924 DIR("fd", S_IRUSR|S_IXUSR, proc_fd_inode_operations, proc_fd_operations),
Pavel Emelyanov640708a2012-01-10 15:11:23 -08002925 DIR("map_files", S_IRUSR|S_IXUSR, proc_map_files_inode_operations, proc_map_files_operations),
Alexey Dobriyan631f9c12008-11-10 01:32:52 +03002926 DIR("fdinfo", S_IRUSR|S_IXUSR, proc_fdinfo_inode_operations, proc_fdinfo_operations),
Eric W. Biederman6b4e3062010-03-07 16:41:34 -08002927 DIR("ns", S_IRUSR|S_IXUGO, proc_ns_dir_inode_operations, proc_ns_dir_operations),
Andrew Mortonb2211a32008-03-11 18:03:35 -07002928#ifdef CONFIG_NET
Alexey Dobriyan631f9c12008-11-10 01:32:52 +03002929 DIR("net", S_IRUGO|S_IXUGO, proc_net_inode_operations, proc_net_operations),
Andrew Mortonb2211a32008-03-11 18:03:35 -07002930#endif
Alexey Dobriyan631f9c12008-11-10 01:32:52 +03002931 REG("environ", S_IRUSR, proc_environ_operations),
Al Viroc5317162016-10-05 18:43:43 -04002932 REG("auxv", S_IRUSR, proc_auxv_operations),
Alexey Dobriyan631f9c12008-11-10 01:32:52 +03002933 ONE("status", S_IRUGO, proc_pid_status),
Djalal Harouni35a35042014-04-07 15:38:36 -07002934 ONE("personality", S_IRUSR, proc_pid_personality),
Alexey Dobriyan1c963eb2014-08-08 14:21:37 -07002935 ONE("limits", S_IRUGO, proc_pid_limits),
Ingo Molnar43ae34c2007-07-09 18:52:00 +02002936#ifdef CONFIG_SCHED_DEBUG
Alexey Dobriyan631f9c12008-11-10 01:32:52 +03002937 REG("sched", S_IRUGO|S_IWUSR, proc_pid_sched_operations),
Ingo Molnar43ae34c2007-07-09 18:52:00 +02002938#endif
Mike Galbraith5091faa2010-11-30 14:18:03 +01002939#ifdef CONFIG_SCHED_AUTOGROUP
2940 REG("autogroup", S_IRUGO|S_IWUSR, proc_pid_sched_autogroup_operations),
2941#endif
john stultz4614a696b2009-12-14 18:00:05 -08002942 REG("comm", S_IRUGO|S_IWUSR, proc_pid_set_comm_operations),
Roland McGrathebcb6732008-07-25 19:46:00 -07002943#ifdef CONFIG_HAVE_ARCH_TRACEHOOK
Alexey Dobriyan09d93bd2014-08-08 14:21:39 -07002944 ONE("syscall", S_IRUSR, proc_pid_syscall),
Roland McGrathebcb6732008-07-25 19:46:00 -07002945#endif
Alexey Dobriyanc2c0bb42015-06-25 15:00:54 -07002946 REG("cmdline", S_IRUGO, proc_pid_cmdline_ops),
Alexey Dobriyan631f9c12008-11-10 01:32:52 +03002947 ONE("stat", S_IRUGO, proc_tgid_stat),
2948 ONE("statm", S_IRUGO, proc_pid_statm),
Siddhesh Poyarekarb7643752012-03-21 16:34:04 -07002949 REG("maps", S_IRUGO, proc_pid_maps_operations),
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002950#ifdef CONFIG_NUMA
Siddhesh Poyarekarb7643752012-03-21 16:34:04 -07002951 REG("numa_maps", S_IRUGO, proc_pid_numa_maps_operations),
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002952#endif
Alexey Dobriyan631f9c12008-11-10 01:32:52 +03002953 REG("mem", S_IRUSR|S_IWUSR, proc_mem_operations),
2954 LNK("cwd", proc_cwd_link),
2955 LNK("root", proc_root_link),
2956 LNK("exe", proc_exe_link),
2957 REG("mounts", S_IRUGO, proc_mounts_operations),
2958 REG("mountinfo", S_IRUGO, proc_mountinfo_operations),
2959 REG("mountstats", S_IRUSR, proc_mountstats_operations),
Matt Mackall1e883282008-02-04 22:29:07 -08002960#ifdef CONFIG_PROC_PAGE_MONITOR
Alexey Dobriyan631f9c12008-11-10 01:32:52 +03002961 REG("clear_refs", S_IWUSR, proc_clear_refs_operations),
Siddhesh Poyarekarb7643752012-03-21 16:34:04 -07002962 REG("smaps", S_IRUGO, proc_pid_smaps_operations),
Daniel Colascione493b0e92017-09-06 16:25:08 -07002963 REG("smaps_rollup", S_IRUGO, proc_pid_smaps_rollup_operations),
Djalal Harouni32ed74a2014-04-07 15:38:38 -07002964 REG("pagemap", S_IRUSR, proc_pagemap_operations),
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002965#endif
2966#ifdef CONFIG_SECURITY
Alexey Dobriyan631f9c12008-11-10 01:32:52 +03002967 DIR("attr", S_IRUGO|S_IXUGO, proc_attr_dir_inode_operations, proc_attr_dir_operations),
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002968#endif
2969#ifdef CONFIG_KALLSYMS
Alexey Dobriyanedfcd602014-08-08 14:21:44 -07002970 ONE("wchan", S_IRUGO, proc_pid_wchan),
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002971#endif
Ken Chen2ec220e2008-11-10 11:26:08 +03002972#ifdef CONFIG_STACKTRACE
Djalal Harouni35a35042014-04-07 15:38:36 -07002973 ONE("stack", S_IRUSR, proc_pid_stack),
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002974#endif
Naveen N. Rao5968cec2015-06-30 14:36:03 +05302975#ifdef CONFIG_SCHED_INFO
Alexey Dobriyanf6e826c2014-08-08 14:21:46 -07002976 ONE("schedstat", S_IRUGO, proc_pid_schedstat),
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002977#endif
Arjan van de Ven97455122008-01-25 21:08:34 +01002978#ifdef CONFIG_LATENCYTOP
Alexey Dobriyan631f9c12008-11-10 01:32:52 +03002979 REG("latency", S_IRUGO, proc_lstats_operations),
Arjan van de Ven97455122008-01-25 21:08:34 +01002980#endif
Paul Menage8793d852007-10-18 23:39:39 -07002981#ifdef CONFIG_PROC_PID_CPUSET
Zefan Li52de4772014-09-18 16:03:36 +08002982 ONE("cpuset", S_IRUGO, proc_cpuset_show),
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002983#endif
Paul Menagea4243162007-10-18 23:39:35 -07002984#ifdef CONFIG_CGROUPS
Zefan Li006f4ac2014-09-18 16:03:15 +08002985 ONE("cgroup", S_IRUGO, proc_cgroup_show),
Paul Menagea4243162007-10-18 23:39:35 -07002986#endif
Alexey Dobriyan6ba51e32014-08-08 14:21:48 -07002987 ONE("oom_score", S_IRUGO, proc_oom_score),
David Rientjesfa0cbbf2012-11-12 17:53:04 -08002988 REG("oom_adj", S_IRUGO|S_IWUSR, proc_oom_adj_operations),
David Rientjesa63d83f2010-08-09 17:19:46 -07002989 REG("oom_score_adj", S_IRUGO|S_IWUSR, proc_oom_score_adj_operations),
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002990#ifdef CONFIG_AUDITSYSCALL
Alexey Dobriyan631f9c12008-11-10 01:32:52 +03002991 REG("loginuid", S_IWUSR|S_IRUGO, proc_loginuid_operations),
2992 REG("sessionid", S_IRUGO, proc_sessionid_operations),
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002993#endif
Akinobu Mitaf4f154f2006-12-08 02:39:47 -08002994#ifdef CONFIG_FAULT_INJECTION
Alexey Dobriyan631f9c12008-11-10 01:32:52 +03002995 REG("make-it-fail", S_IRUGO|S_IWUSR, proc_fault_inject_operations),
Akinobu Mita168c42b2017-07-14 14:50:00 -07002996 REG("fail-nth", 0644, proc_fail_nth_operations),
Akinobu Mitaf4f154f2006-12-08 02:39:47 -08002997#endif
Christoph Hellwig698ba7b2009-12-15 16:47:37 -08002998#ifdef CONFIG_ELF_CORE
Alexey Dobriyan631f9c12008-11-10 01:32:52 +03002999 REG("coredump_filter", S_IRUGO|S_IWUSR, proc_coredump_filter_operations),
Kawai, Hidehiro3cb4a0b2007-07-19 01:48:28 -07003000#endif
Andrew Mortonaba76fd2006-12-10 02:19:48 -08003001#ifdef CONFIG_TASK_IO_ACCOUNTING
Alexey Dobriyan19aadc92014-08-08 14:21:50 -07003002 ONE("io", S_IRUSR, proc_tgid_io_accounting),
Andrew Mortonaba76fd2006-12-10 02:19:48 -08003003#endif
Eric W. Biederman22d917d2011-11-17 00:11:58 -08003004#ifdef CONFIG_USER_NS
3005 REG("uid_map", S_IRUGO|S_IWUSR, proc_uid_map_operations),
3006 REG("gid_map", S_IRUGO|S_IWUSR, proc_gid_map_operations),
Eric W. Biedermanf76d2072012-08-30 01:24:05 -07003007 REG("projid_map", S_IRUGO|S_IWUSR, proc_projid_map_operations),
Eric W. Biederman9cc46512014-12-02 12:27:26 -06003008 REG("setgroups", S_IRUGO|S_IWUSR, proc_setgroups_operations),
Eric W. Biederman22d917d2011-11-17 00:11:58 -08003009#endif
Nicolas Pitreb18b6a92017-01-21 00:09:08 -05003010#if defined(CONFIG_CHECKPOINT_RESTORE) && defined(CONFIG_POSIX_TIMERS)
Pavel Emelyanov48f6a7a2013-03-11 13:12:45 +04003011 REG("timers", S_IRUGO, proc_timers_operations),
3012#endif
John Stultz5de23d42016-03-17 14:20:54 -07003013 REG("timerslack_ns", S_IRUGO|S_IWUGO, proc_pid_set_timerslack_ns_operations),
Josh Poimboeuf7c23b332017-02-13 19:42:41 -06003014#ifdef CONFIG_LIVEPATCH
3015 ONE("patch_state", S_IRUSR, proc_pid_patch_state),
3016#endif
Eric W. Biederman28a6d672006-10-02 02:17:05 -07003017};
3018
Al Virof0c3b502013-05-16 12:07:31 -04003019static int proc_tgid_base_readdir(struct file *file, struct dir_context *ctx)
Eric W. Biederman28a6d672006-10-02 02:17:05 -07003020{
Al Virof0c3b502013-05-16 12:07:31 -04003021 return proc_pident_readdir(file, ctx,
3022 tgid_base_stuff, ARRAY_SIZE(tgid_base_stuff));
Eric W. Biederman28a6d672006-10-02 02:17:05 -07003023}
3024
Arjan van de Ven00977a52007-02-12 00:55:34 -08003025static const struct file_operations proc_tgid_base_operations = {
Eric W. Biederman28a6d672006-10-02 02:17:05 -07003026 .read = generic_read_dir,
Al Virof50752e2016-04-20 17:13:54 -04003027 .iterate_shared = proc_tgid_base_readdir,
3028 .llseek = generic_file_llseek,
Eric W. Biederman28a6d672006-10-02 02:17:05 -07003029};
3030
Al Viro00cd8dd2012-06-10 17:13:09 -04003031static struct dentry *proc_tgid_base_lookup(struct inode *dir, struct dentry *dentry, unsigned int flags)
3032{
Eric W. Biederman7bcd6b02006-10-02 02:18:56 -07003033 return proc_pident_lookup(dir, dentry,
3034 tgid_base_stuff, ARRAY_SIZE(tgid_base_stuff));
Eric W. Biederman28a6d672006-10-02 02:17:05 -07003035}
3036
Arjan van de Venc5ef1c42007-02-12 00:55:40 -08003037static const struct inode_operations proc_tgid_base_inode_operations = {
Eric W. Biederman28a6d672006-10-02 02:17:05 -07003038 .lookup = proc_tgid_base_lookup,
3039 .getattr = pid_getattr,
3040 .setattr = proc_setattr,
Vasiliy Kulikov04996802012-01-10 15:11:31 -08003041 .permission = proc_pid_permission,
Eric W. Biederman28a6d672006-10-02 02:17:05 -07003042};
3043
Pavel Emelyanov60347f62007-10-18 23:40:03 -07003044static void proc_flush_task_mnt(struct vfsmount *mnt, pid_t pid, pid_t tgid)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003045{
Eric W. Biederman48e64842006-06-26 00:25:48 -07003046 struct dentry *dentry, *leader, *dir;
Alexey Dobriyane3912ac2018-02-06 15:36:51 -08003047 char buf[10 + 1];
Eric W. Biederman48e64842006-06-26 00:25:48 -07003048 struct qstr name;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003049
Eric W. Biederman48e64842006-06-26 00:25:48 -07003050 name.name = buf;
Alexey Dobriyane3912ac2018-02-06 15:36:51 -08003051 name.len = snprintf(buf, sizeof(buf), "%u", pid);
Al Viro4f522a22013-02-11 23:20:37 -05003052 /* no ->d_hash() rejects on procfs */
Pavel Emelyanov60347f62007-10-18 23:40:03 -07003053 dentry = d_hash_and_lookup(mnt->mnt_root, &name);
Eric W. Biederman48e64842006-06-26 00:25:48 -07003054 if (dentry) {
Eric W. Biedermanbbd51922014-02-13 10:24:23 -08003055 d_invalidate(dentry);
Eric W. Biederman48e64842006-06-26 00:25:48 -07003056 dput(dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003057 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003058
Oleg Nesterovc35a7f12014-12-10 15:54:56 -08003059 if (pid == tgid)
3060 return;
3061
Eric W. Biederman48e64842006-06-26 00:25:48 -07003062 name.name = buf;
Alexey Dobriyane3912ac2018-02-06 15:36:51 -08003063 name.len = snprintf(buf, sizeof(buf), "%u", tgid);
Pavel Emelyanov60347f62007-10-18 23:40:03 -07003064 leader = d_hash_and_lookup(mnt->mnt_root, &name);
Eric W. Biederman48e64842006-06-26 00:25:48 -07003065 if (!leader)
3066 goto out;
3067
3068 name.name = "task";
3069 name.len = strlen(name.name);
3070 dir = d_hash_and_lookup(leader, &name);
3071 if (!dir)
3072 goto out_put_leader;
3073
3074 name.name = buf;
Alexey Dobriyane3912ac2018-02-06 15:36:51 -08003075 name.len = snprintf(buf, sizeof(buf), "%u", pid);
Eric W. Biederman48e64842006-06-26 00:25:48 -07003076 dentry = d_hash_and_lookup(dir, &name);
3077 if (dentry) {
Eric W. Biedermanbbd51922014-02-13 10:24:23 -08003078 d_invalidate(dentry);
Eric W. Biederman48e64842006-06-26 00:25:48 -07003079 dput(dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003080 }
Eric W. Biederman48e64842006-06-26 00:25:48 -07003081
3082 dput(dir);
3083out_put_leader:
3084 dput(leader);
3085out:
3086 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003087}
3088
Randy Dunlap0895e912007-10-21 21:00:10 -07003089/**
3090 * proc_flush_task - Remove dcache entries for @task from the /proc dcache.
3091 * @task: task that should be flushed.
3092 *
3093 * When flushing dentries from proc, one needs to flush them from global
Pavel Emelyanov60347f62007-10-18 23:40:03 -07003094 * proc (proc_mnt) and from all the namespaces' procs this task was seen
Randy Dunlap0895e912007-10-21 21:00:10 -07003095 * in. This call is supposed to do all of this job.
3096 *
3097 * Looks in the dcache for
3098 * /proc/@pid
3099 * /proc/@tgid/task/@pid
3100 * if either directory is present flushes it and all of it'ts children
3101 * from the dcache.
3102 *
3103 * It is safe and reasonable to cache /proc entries for a task until
3104 * that task exits. After that they just clog up the dcache with
3105 * useless entries, possibly causing useful dcache entries to be
3106 * flushed instead. This routine is proved to flush those useless
3107 * dcache entries at process exit time.
3108 *
3109 * NOTE: This routine is just an optimization so it does not guarantee
3110 * that no dcache entries will exist at process exit time it
3111 * just makes it very unlikely that any will persist.
Pavel Emelyanov60347f62007-10-18 23:40:03 -07003112 */
3113
3114void proc_flush_task(struct task_struct *task)
3115{
Eric W. Biederman9fcc2d12007-11-14 17:00:07 -08003116 int i;
Oleg Nesterov9b4d1cbe2009-09-22 16:45:34 -07003117 struct pid *pid, *tgid;
Pavel Emelyanov130f77e2007-10-18 23:40:11 -07003118 struct upid *upid;
3119
Pavel Emelyanov130f77e2007-10-18 23:40:11 -07003120 pid = task_pid(task);
Oleg Nesterov9b4d1cbe2009-09-22 16:45:34 -07003121 tgid = task_tgid(task);
Pavel Emelyanov130f77e2007-10-18 23:40:11 -07003122
Eric W. Biederman9fcc2d12007-11-14 17:00:07 -08003123 for (i = 0; i <= pid->level; i++) {
Pavel Emelyanov130f77e2007-10-18 23:40:11 -07003124 upid = &pid->numbers[i];
3125 proc_flush_task_mnt(upid->ns->proc_mnt, upid->nr,
Oleg Nesterov9b4d1cbe2009-09-22 16:45:34 -07003126 tgid->numbers[i].nr);
Pavel Emelyanov130f77e2007-10-18 23:40:11 -07003127 }
Pavel Emelyanov60347f62007-10-18 23:40:03 -07003128}
3129
Al Viro0168b9e2018-05-03 09:21:05 -04003130static struct dentry *proc_pid_instantiate(struct dentry * dentry,
Al Viroc52a47a2013-06-15 11:15:20 +04003131 struct task_struct *task, const void *ptr)
Eric W. Biederman444ceed2006-10-02 02:18:49 -07003132{
Eric W. Biederman444ceed2006-10-02 02:18:49 -07003133 struct inode *inode;
3134
Al Viro0168b9e2018-05-03 09:21:05 -04003135 inode = proc_pid_make_inode(dentry->d_sb, task, S_IFDIR | S_IRUGO | S_IXUGO);
Eric W. Biederman444ceed2006-10-02 02:18:49 -07003136 if (!inode)
Al Viro0168b9e2018-05-03 09:21:05 -04003137 return ERR_PTR(-ENOENT);
Eric W. Biederman444ceed2006-10-02 02:18:49 -07003138
Eric W. Biederman444ceed2006-10-02 02:18:49 -07003139 inode->i_op = &proc_tgid_base_inode_operations;
3140 inode->i_fop = &proc_tgid_base_operations;
3141 inode->i_flags|=S_IMMUTABLE;
Vegard Nossumaed54172008-06-05 22:46:53 -07003142
Alexey Dobriyan1270dd82016-12-12 16:45:32 -08003143 set_nlink(inode, nlink_tgid);
Al Viro1bbc5512018-05-02 21:26:16 -04003144 pid_update_inode(task, inode);
Eric W. Biederman444ceed2006-10-02 02:18:49 -07003145
Nick Pigginfb045ad2011-01-07 17:49:55 +11003146 d_set_d_op(dentry, &pid_dentry_operations);
Al Viro0168b9e2018-05-03 09:21:05 -04003147 return d_splice_alias(inode, dentry);
Eric W. Biederman444ceed2006-10-02 02:18:49 -07003148}
3149
Al Viro00cd8dd2012-06-10 17:13:09 -04003150struct dentry *proc_pid_lookup(struct inode *dir, struct dentry * dentry, unsigned int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003151{
3152 struct task_struct *task;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003153 unsigned tgid;
Pavel Emelyanovb4888932007-10-18 23:40:14 -07003154 struct pid_namespace *ns;
Al Viro0168b9e2018-05-03 09:21:05 -04003155 struct dentry *result = ERR_PTR(-ENOENT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003156
Alexey Dobriyandbcdb502014-08-08 14:21:25 -07003157 tgid = name_to_int(&dentry->d_name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003158 if (tgid == ~0U)
3159 goto out;
3160
Pavel Emelyanovb4888932007-10-18 23:40:14 -07003161 ns = dentry->d_sb->s_fs_info;
Eric W. Biedermande758732006-06-26 00:25:51 -07003162 rcu_read_lock();
Pavel Emelyanovb4888932007-10-18 23:40:14 -07003163 task = find_task_by_pid_ns(tgid, ns);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003164 if (task)
3165 get_task_struct(task);
Eric W. Biedermande758732006-06-26 00:25:51 -07003166 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07003167 if (!task)
3168 goto out;
3169
Al Viro0168b9e2018-05-03 09:21:05 -04003170 result = proc_pid_instantiate(dentry, task, NULL);
Eric W. Biederman48e64842006-06-26 00:25:48 -07003171 put_task_struct(task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003172out:
Al Viro0168b9e2018-05-03 09:21:05 -04003173 return result;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003174}
3175
Linus Torvalds1da177e2005-04-16 15:20:36 -07003176/*
Eric W. Biederman0804ef42006-10-02 02:17:04 -07003177 * Find the first task with tgid >= tgid
Eric W. Biederman0bc58a92006-06-26 00:25:50 -07003178 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07003179 */
Eric W. Biederman19fd4bb2007-11-28 16:21:26 -08003180struct tgid_iter {
3181 unsigned int tgid;
Eric W. Biederman0804ef42006-10-02 02:17:04 -07003182 struct task_struct *task;
Eric W. Biederman19fd4bb2007-11-28 16:21:26 -08003183};
3184static struct tgid_iter next_tgid(struct pid_namespace *ns, struct tgid_iter iter)
3185{
Eric W. Biederman0804ef42006-10-02 02:17:04 -07003186 struct pid *pid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003187
Eric W. Biederman19fd4bb2007-11-28 16:21:26 -08003188 if (iter.task)
3189 put_task_struct(iter.task);
Eric W. Biederman0804ef42006-10-02 02:17:04 -07003190 rcu_read_lock();
3191retry:
Eric W. Biederman19fd4bb2007-11-28 16:21:26 -08003192 iter.task = NULL;
3193 pid = find_ge_pid(iter.tgid, ns);
Eric W. Biederman0804ef42006-10-02 02:17:04 -07003194 if (pid) {
Eric W. Biederman19fd4bb2007-11-28 16:21:26 -08003195 iter.tgid = pid_nr_ns(pid, ns);
3196 iter.task = pid_task(pid, PIDTYPE_PID);
Eric W. Biederman0804ef42006-10-02 02:17:04 -07003197 /* What we to know is if the pid we have find is the
3198 * pid of a thread_group_leader. Testing for task
3199 * being a thread_group_leader is the obvious thing
3200 * todo but there is a window when it fails, due to
3201 * the pid transfer logic in de_thread.
3202 *
3203 * So we perform the straight forward test of seeing
3204 * if the pid we have found is the pid of a thread
3205 * group leader, and don't worry if the task we have
3206 * found doesn't happen to be a thread group leader.
3207 * As we don't care in the case of readdir.
3208 */
Eric W. Biederman19fd4bb2007-11-28 16:21:26 -08003209 if (!iter.task || !has_group_leader_pid(iter.task)) {
3210 iter.tgid += 1;
Eric W. Biederman0804ef42006-10-02 02:17:04 -07003211 goto retry;
Eric W. Biederman19fd4bb2007-11-28 16:21:26 -08003212 }
3213 get_task_struct(iter.task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003214 }
Eric W. Biederman454cc102006-06-26 00:25:51 -07003215 rcu_read_unlock();
Eric W. Biederman19fd4bb2007-11-28 16:21:26 -08003216 return iter;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003217}
3218
Eric W. Biederman00978752014-07-31 03:10:50 -07003219#define TGID_OFFSET (FIRST_PROCESS_ENTRY + 2)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003220
3221/* for the /proc/ directory itself, after non-process stuff has been done */
Al Virof0c3b502013-05-16 12:07:31 -04003222int proc_pid_readdir(struct file *file, struct dir_context *ctx)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003223{
Eric W. Biederman19fd4bb2007-11-28 16:21:26 -08003224 struct tgid_iter iter;
Christoph Hellwig76f668b2018-05-16 07:19:01 +02003225 struct pid_namespace *ns = proc_pid_ns(file_inode(file));
Al Virof0c3b502013-05-16 12:07:31 -04003226 loff_t pos = ctx->pos;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003227
Al Viro021ada72013-03-29 19:27:05 -04003228 if (pos >= PID_MAX_LIMIT + TGID_OFFSET)
Al Virof0c3b502013-05-16 12:07:31 -04003229 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003230
Eric W. Biederman00978752014-07-31 03:10:50 -07003231 if (pos == TGID_OFFSET - 2) {
David Howells2b0143b2015-03-17 22:25:59 +00003232 struct inode *inode = d_inode(ns->proc_self);
Al Virodb963162013-06-15 10:45:10 +04003233 if (!dir_emit(ctx, "self", 4, inode->i_ino, DT_LNK))
Al Virof0c3b502013-05-16 12:07:31 -04003234 return 0;
Eric W. Biederman00978752014-07-31 03:10:50 -07003235 ctx->pos = pos = pos + 1;
Al Viro021ada72013-03-29 19:27:05 -04003236 }
Eric W. Biederman00978752014-07-31 03:10:50 -07003237 if (pos == TGID_OFFSET - 1) {
David Howells2b0143b2015-03-17 22:25:59 +00003238 struct inode *inode = d_inode(ns->proc_thread_self);
Eric W. Biederman00978752014-07-31 03:10:50 -07003239 if (!dir_emit(ctx, "thread-self", 11, inode->i_ino, DT_LNK))
3240 return 0;
3241 ctx->pos = pos = pos + 1;
3242 }
3243 iter.tgid = pos - TGID_OFFSET;
Eric W. Biederman19fd4bb2007-11-28 16:21:26 -08003244 iter.task = NULL;
Eric W. Biederman19fd4bb2007-11-28 16:21:26 -08003245 for (iter = next_tgid(ns, iter);
3246 iter.task;
3247 iter.tgid += 1, iter = next_tgid(ns, iter)) {
Alexey Dobriyane3912ac2018-02-06 15:36:51 -08003248 char name[10 + 1];
Al Virof0c3b502013-05-16 12:07:31 -04003249 int len;
Eric Dumazet3ba4bce2017-01-24 15:18:07 -08003250
3251 cond_resched();
Lafcadio Wluiki796f5712017-02-24 15:00:23 -08003252 if (!has_pid_permissions(ns, iter.task, HIDEPID_INVISIBLE))
Al Virof0c3b502013-05-16 12:07:31 -04003253 continue;
Vasiliy Kulikov04996802012-01-10 15:11:31 -08003254
Alexey Dobriyane3912ac2018-02-06 15:36:51 -08003255 len = snprintf(name, sizeof(name), "%u", iter.tgid);
Al Virof0c3b502013-05-16 12:07:31 -04003256 ctx->pos = iter.tgid + TGID_OFFSET;
3257 if (!proc_fill_cache(file, ctx, name, len,
3258 proc_pid_instantiate, iter.task, NULL)) {
Eric W. Biederman19fd4bb2007-11-28 16:21:26 -08003259 put_task_struct(iter.task);
Al Virof0c3b502013-05-16 12:07:31 -04003260 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003261 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003262 }
Al Virof0c3b502013-05-16 12:07:31 -04003263 ctx->pos = PID_MAX_LIMIT + TGID_OFFSET;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003264 return 0;
3265}
3266
Eric W. Biederman0bc58a92006-06-26 00:25:50 -07003267/*
Janis Danisevskis1b3044e2016-05-20 17:00:08 -07003268 * proc_tid_comm_permission is a special permission function exclusively
3269 * used for the node /proc/<pid>/task/<tid>/comm.
3270 * It bypasses generic permission checks in the case where a task of the same
3271 * task group attempts to access the node.
3272 * The rationale behind this is that glibc and bionic access this node for
3273 * cross thread naming (pthread_set/getname_np(!self)). However, if
3274 * PR_SET_DUMPABLE gets set to 0 this node among others becomes uid=0 gid=0,
3275 * which locks out the cross thread naming implementation.
3276 * This function makes sure that the node is always accessible for members of
3277 * same thread group.
3278 */
3279static int proc_tid_comm_permission(struct inode *inode, int mask)
3280{
3281 bool is_same_tgroup;
3282 struct task_struct *task;
3283
3284 task = get_proc_task(inode);
3285 if (!task)
3286 return -ESRCH;
3287 is_same_tgroup = same_thread_group(current, task);
3288 put_task_struct(task);
3289
3290 if (likely(is_same_tgroup && !(mask & MAY_EXEC))) {
3291 /* This file (/proc/<pid>/task/<tid>/comm) can always be
3292 * read or written by the members of the corresponding
3293 * thread group.
3294 */
3295 return 0;
3296 }
3297
3298 return generic_permission(inode, mask);
3299}
3300
3301static const struct inode_operations proc_tid_comm_inode_operations = {
3302 .permission = proc_tid_comm_permission,
3303};
3304
3305/*
Eric W. Biederman28a6d672006-10-02 02:17:05 -07003306 * Tasks
3307 */
Eric Dumazetc5141e62007-05-08 00:26:15 -07003308static const struct pid_entry tid_base_stuff[] = {
Alexey Dobriyan631f9c12008-11-10 01:32:52 +03003309 DIR("fd", S_IRUSR|S_IXUSR, proc_fd_inode_operations, proc_fd_operations),
Jerome Marchand38355412010-04-27 13:13:06 -07003310 DIR("fdinfo", S_IRUSR|S_IXUSR, proc_fdinfo_inode_operations, proc_fdinfo_operations),
Eric W. Biederman6b4e3062010-03-07 16:41:34 -08003311 DIR("ns", S_IRUSR|S_IXUGO, proc_ns_dir_inode_operations, proc_ns_dir_operations),
Eric W. Biederman6ba8ed72014-07-31 16:27:08 -07003312#ifdef CONFIG_NET
3313 DIR("net", S_IRUGO|S_IXUGO, proc_net_inode_operations, proc_net_operations),
3314#endif
Alexey Dobriyan631f9c12008-11-10 01:32:52 +03003315 REG("environ", S_IRUSR, proc_environ_operations),
Al Viroc5317162016-10-05 18:43:43 -04003316 REG("auxv", S_IRUSR, proc_auxv_operations),
Alexey Dobriyan631f9c12008-11-10 01:32:52 +03003317 ONE("status", S_IRUGO, proc_pid_status),
Djalal Harouni35a35042014-04-07 15:38:36 -07003318 ONE("personality", S_IRUSR, proc_pid_personality),
Alexey Dobriyan1c963eb2014-08-08 14:21:37 -07003319 ONE("limits", S_IRUGO, proc_pid_limits),
Ingo Molnar43ae34c2007-07-09 18:52:00 +02003320#ifdef CONFIG_SCHED_DEBUG
Alexey Dobriyan631f9c12008-11-10 01:32:52 +03003321 REG("sched", S_IRUGO|S_IWUSR, proc_pid_sched_operations),
Ingo Molnar43ae34c2007-07-09 18:52:00 +02003322#endif
Janis Danisevskis1b3044e2016-05-20 17:00:08 -07003323 NOD("comm", S_IFREG|S_IRUGO|S_IWUSR,
3324 &proc_tid_comm_inode_operations,
3325 &proc_pid_set_comm_operations, {}),
Roland McGrathebcb6732008-07-25 19:46:00 -07003326#ifdef CONFIG_HAVE_ARCH_TRACEHOOK
Alexey Dobriyan09d93bd2014-08-08 14:21:39 -07003327 ONE("syscall", S_IRUSR, proc_pid_syscall),
Roland McGrathebcb6732008-07-25 19:46:00 -07003328#endif
Alexey Dobriyanc2c0bb42015-06-25 15:00:54 -07003329 REG("cmdline", S_IRUGO, proc_pid_cmdline_ops),
Alexey Dobriyan631f9c12008-11-10 01:32:52 +03003330 ONE("stat", S_IRUGO, proc_tid_stat),
3331 ONE("statm", S_IRUGO, proc_pid_statm),
Siddhesh Poyarekarb7643752012-03-21 16:34:04 -07003332 REG("maps", S_IRUGO, proc_tid_maps_operations),
Iago López Galeiras2e13ba52015-06-25 15:00:57 -07003333#ifdef CONFIG_PROC_CHILDREN
Cyrill Gorcunov818411612012-05-31 16:26:43 -07003334 REG("children", S_IRUGO, proc_tid_children_operations),
3335#endif
Eric W. Biederman28a6d672006-10-02 02:17:05 -07003336#ifdef CONFIG_NUMA
Siddhesh Poyarekarb7643752012-03-21 16:34:04 -07003337 REG("numa_maps", S_IRUGO, proc_tid_numa_maps_operations),
Eric W. Biederman28a6d672006-10-02 02:17:05 -07003338#endif
Alexey Dobriyan631f9c12008-11-10 01:32:52 +03003339 REG("mem", S_IRUSR|S_IWUSR, proc_mem_operations),
3340 LNK("cwd", proc_cwd_link),
3341 LNK("root", proc_root_link),
3342 LNK("exe", proc_exe_link),
3343 REG("mounts", S_IRUGO, proc_mounts_operations),
3344 REG("mountinfo", S_IRUGO, proc_mountinfo_operations),
Matt Mackall1e883282008-02-04 22:29:07 -08003345#ifdef CONFIG_PROC_PAGE_MONITOR
Alexey Dobriyan631f9c12008-11-10 01:32:52 +03003346 REG("clear_refs", S_IWUSR, proc_clear_refs_operations),
Siddhesh Poyarekarb7643752012-03-21 16:34:04 -07003347 REG("smaps", S_IRUGO, proc_tid_smaps_operations),
Daniel Colascione493b0e92017-09-06 16:25:08 -07003348 REG("smaps_rollup", S_IRUGO, proc_pid_smaps_rollup_operations),
Djalal Harouni32ed74a2014-04-07 15:38:38 -07003349 REG("pagemap", S_IRUSR, proc_pagemap_operations),
Eric W. Biederman28a6d672006-10-02 02:17:05 -07003350#endif
3351#ifdef CONFIG_SECURITY
Alexey Dobriyan631f9c12008-11-10 01:32:52 +03003352 DIR("attr", S_IRUGO|S_IXUGO, proc_attr_dir_inode_operations, proc_attr_dir_operations),
Eric W. Biederman28a6d672006-10-02 02:17:05 -07003353#endif
3354#ifdef CONFIG_KALLSYMS
Alexey Dobriyanedfcd602014-08-08 14:21:44 -07003355 ONE("wchan", S_IRUGO, proc_pid_wchan),
Eric W. Biederman28a6d672006-10-02 02:17:05 -07003356#endif
Ken Chen2ec220e2008-11-10 11:26:08 +03003357#ifdef CONFIG_STACKTRACE
Djalal Harouni35a35042014-04-07 15:38:36 -07003358 ONE("stack", S_IRUSR, proc_pid_stack),
Eric W. Biederman28a6d672006-10-02 02:17:05 -07003359#endif
Naveen N. Rao5968cec2015-06-30 14:36:03 +05303360#ifdef CONFIG_SCHED_INFO
Alexey Dobriyanf6e826c2014-08-08 14:21:46 -07003361 ONE("schedstat", S_IRUGO, proc_pid_schedstat),
Eric W. Biederman28a6d672006-10-02 02:17:05 -07003362#endif
Arjan van de Ven97455122008-01-25 21:08:34 +01003363#ifdef CONFIG_LATENCYTOP
Alexey Dobriyan631f9c12008-11-10 01:32:52 +03003364 REG("latency", S_IRUGO, proc_lstats_operations),
Arjan van de Ven97455122008-01-25 21:08:34 +01003365#endif
Paul Menage8793d852007-10-18 23:39:39 -07003366#ifdef CONFIG_PROC_PID_CPUSET
Zefan Li52de4772014-09-18 16:03:36 +08003367 ONE("cpuset", S_IRUGO, proc_cpuset_show),
Eric W. Biederman28a6d672006-10-02 02:17:05 -07003368#endif
Paul Menagea4243162007-10-18 23:39:35 -07003369#ifdef CONFIG_CGROUPS
Zefan Li006f4ac2014-09-18 16:03:15 +08003370 ONE("cgroup", S_IRUGO, proc_cgroup_show),
Paul Menagea4243162007-10-18 23:39:35 -07003371#endif
Alexey Dobriyan6ba51e32014-08-08 14:21:48 -07003372 ONE("oom_score", S_IRUGO, proc_oom_score),
David Rientjesfa0cbbf2012-11-12 17:53:04 -08003373 REG("oom_adj", S_IRUGO|S_IWUSR, proc_oom_adj_operations),
David Rientjesa63d83f2010-08-09 17:19:46 -07003374 REG("oom_score_adj", S_IRUGO|S_IWUSR, proc_oom_score_adj_operations),
Eric W. Biederman28a6d672006-10-02 02:17:05 -07003375#ifdef CONFIG_AUDITSYSCALL
Alexey Dobriyan631f9c12008-11-10 01:32:52 +03003376 REG("loginuid", S_IWUSR|S_IRUGO, proc_loginuid_operations),
Al Viro26ec3c62011-02-15 21:24:05 -05003377 REG("sessionid", S_IRUGO, proc_sessionid_operations),
Eric W. Biederman28a6d672006-10-02 02:17:05 -07003378#endif
Akinobu Mitaf4f154f2006-12-08 02:39:47 -08003379#ifdef CONFIG_FAULT_INJECTION
Alexey Dobriyan631f9c12008-11-10 01:32:52 +03003380 REG("make-it-fail", S_IRUGO|S_IWUSR, proc_fault_inject_operations),
Akinobu Mita1203c8e2017-07-14 14:49:57 -07003381 REG("fail-nth", 0644, proc_fail_nth_operations),
Akinobu Mitaf4f154f2006-12-08 02:39:47 -08003382#endif
Andrea Righi297c5d92008-07-25 01:48:49 -07003383#ifdef CONFIG_TASK_IO_ACCOUNTING
Alexey Dobriyan19aadc92014-08-08 14:21:50 -07003384 ONE("io", S_IRUSR, proc_tid_io_accounting),
Andrea Righi297c5d92008-07-25 01:48:49 -07003385#endif
Eric W. Biederman22d917d2011-11-17 00:11:58 -08003386#ifdef CONFIG_USER_NS
3387 REG("uid_map", S_IRUGO|S_IWUSR, proc_uid_map_operations),
3388 REG("gid_map", S_IRUGO|S_IWUSR, proc_gid_map_operations),
Eric W. Biedermanf76d2072012-08-30 01:24:05 -07003389 REG("projid_map", S_IRUGO|S_IWUSR, proc_projid_map_operations),
Eric W. Biederman9cc46512014-12-02 12:27:26 -06003390 REG("setgroups", S_IRUGO|S_IWUSR, proc_setgroups_operations),
Eric W. Biederman22d917d2011-11-17 00:11:58 -08003391#endif
Josh Poimboeuf7c23b332017-02-13 19:42:41 -06003392#ifdef CONFIG_LIVEPATCH
3393 ONE("patch_state", S_IRUSR, proc_pid_patch_state),
3394#endif
Eric W. Biederman28a6d672006-10-02 02:17:05 -07003395};
3396
Al Virof0c3b502013-05-16 12:07:31 -04003397static int proc_tid_base_readdir(struct file *file, struct dir_context *ctx)
Eric W. Biederman28a6d672006-10-02 02:17:05 -07003398{
Al Virof0c3b502013-05-16 12:07:31 -04003399 return proc_pident_readdir(file, ctx,
3400 tid_base_stuff, ARRAY_SIZE(tid_base_stuff));
Eric W. Biederman28a6d672006-10-02 02:17:05 -07003401}
3402
Al Viro00cd8dd2012-06-10 17:13:09 -04003403static struct dentry *proc_tid_base_lookup(struct inode *dir, struct dentry *dentry, unsigned int flags)
3404{
Eric W. Biederman7bcd6b02006-10-02 02:18:56 -07003405 return proc_pident_lookup(dir, dentry,
3406 tid_base_stuff, ARRAY_SIZE(tid_base_stuff));
Eric W. Biederman28a6d672006-10-02 02:17:05 -07003407}
3408
Arjan van de Ven00977a52007-02-12 00:55:34 -08003409static const struct file_operations proc_tid_base_operations = {
Eric W. Biederman28a6d672006-10-02 02:17:05 -07003410 .read = generic_read_dir,
Al Virof50752e2016-04-20 17:13:54 -04003411 .iterate_shared = proc_tid_base_readdir,
3412 .llseek = generic_file_llseek,
Eric W. Biederman28a6d672006-10-02 02:17:05 -07003413};
3414
Arjan van de Venc5ef1c42007-02-12 00:55:40 -08003415static const struct inode_operations proc_tid_base_inode_operations = {
Eric W. Biederman28a6d672006-10-02 02:17:05 -07003416 .lookup = proc_tid_base_lookup,
3417 .getattr = pid_getattr,
3418 .setattr = proc_setattr,
3419};
3420
Al Viro0168b9e2018-05-03 09:21:05 -04003421static struct dentry *proc_task_instantiate(struct dentry *dentry,
3422 struct task_struct *task, const void *ptr)
Eric W. Biederman444ceed2006-10-02 02:18:49 -07003423{
Eric W. Biederman444ceed2006-10-02 02:18:49 -07003424 struct inode *inode;
Al Viro0168b9e2018-05-03 09:21:05 -04003425 inode = proc_pid_make_inode(dentry->d_sb, task, S_IFDIR | S_IRUGO | S_IXUGO);
Eric W. Biederman444ceed2006-10-02 02:18:49 -07003426 if (!inode)
Al Viro0168b9e2018-05-03 09:21:05 -04003427 return ERR_PTR(-ENOENT);
Al Viro1bbc5512018-05-02 21:26:16 -04003428
Eric W. Biederman444ceed2006-10-02 02:18:49 -07003429 inode->i_op = &proc_tid_base_inode_operations;
3430 inode->i_fop = &proc_tid_base_operations;
Al Viro1bbc5512018-05-02 21:26:16 -04003431 inode->i_flags |= S_IMMUTABLE;
Vegard Nossumaed54172008-06-05 22:46:53 -07003432
Alexey Dobriyan1270dd82016-12-12 16:45:32 -08003433 set_nlink(inode, nlink_tid);
Al Viro1bbc5512018-05-02 21:26:16 -04003434 pid_update_inode(task, inode);
Eric W. Biederman444ceed2006-10-02 02:18:49 -07003435
Nick Pigginfb045ad2011-01-07 17:49:55 +11003436 d_set_d_op(dentry, &pid_dentry_operations);
Al Viro0168b9e2018-05-03 09:21:05 -04003437 return d_splice_alias(inode, dentry);
Eric W. Biederman444ceed2006-10-02 02:18:49 -07003438}
3439
Al Viro00cd8dd2012-06-10 17:13:09 -04003440static struct dentry *proc_task_lookup(struct inode *dir, struct dentry * dentry, unsigned int flags)
Eric W. Biederman28a6d672006-10-02 02:17:05 -07003441{
Eric W. Biederman28a6d672006-10-02 02:17:05 -07003442 struct task_struct *task;
3443 struct task_struct *leader = get_proc_task(dir);
Eric W. Biederman28a6d672006-10-02 02:17:05 -07003444 unsigned tid;
Pavel Emelyanovb4888932007-10-18 23:40:14 -07003445 struct pid_namespace *ns;
Al Viro0168b9e2018-05-03 09:21:05 -04003446 struct dentry *result = ERR_PTR(-ENOENT);
Eric W. Biederman28a6d672006-10-02 02:17:05 -07003447
3448 if (!leader)
3449 goto out_no_task;
3450
Alexey Dobriyandbcdb502014-08-08 14:21:25 -07003451 tid = name_to_int(&dentry->d_name);
Eric W. Biederman28a6d672006-10-02 02:17:05 -07003452 if (tid == ~0U)
3453 goto out;
3454
Pavel Emelyanovb4888932007-10-18 23:40:14 -07003455 ns = dentry->d_sb->s_fs_info;
Eric W. Biederman28a6d672006-10-02 02:17:05 -07003456 rcu_read_lock();
Pavel Emelyanovb4888932007-10-18 23:40:14 -07003457 task = find_task_by_pid_ns(tid, ns);
Eric W. Biederman28a6d672006-10-02 02:17:05 -07003458 if (task)
3459 get_task_struct(task);
3460 rcu_read_unlock();
3461 if (!task)
3462 goto out;
Pavel Emelyanovbac0abd2007-10-18 23:40:18 -07003463 if (!same_thread_group(leader, task))
Eric W. Biederman28a6d672006-10-02 02:17:05 -07003464 goto out_drop_task;
3465
Al Viro0168b9e2018-05-03 09:21:05 -04003466 result = proc_task_instantiate(dentry, task, NULL);
Eric W. Biederman28a6d672006-10-02 02:17:05 -07003467out_drop_task:
3468 put_task_struct(task);
3469out:
3470 put_task_struct(leader);
3471out_no_task:
Al Viro0168b9e2018-05-03 09:21:05 -04003472 return result;
Eric W. Biederman28a6d672006-10-02 02:17:05 -07003473}
3474
3475/*
Eric W. Biederman0bc58a92006-06-26 00:25:50 -07003476 * Find the first tid of a thread group to return to user space.
3477 *
3478 * Usually this is just the thread group leader, but if the users
3479 * buffer was too small or there was a seek into the middle of the
3480 * directory we have more work todo.
3481 *
3482 * In the case of a short read we start with find_task_by_pid.
3483 *
3484 * In the case of a seek we start with the leader and walk nr
3485 * threads past it.
3486 */
Oleg Nesterov9f6e9632014-01-23 15:55:40 -08003487static struct task_struct *first_tid(struct pid *pid, int tid, loff_t f_pos,
3488 struct pid_namespace *ns)
Eric W. Biederman0bc58a92006-06-26 00:25:50 -07003489{
Oleg Nesterovd855a4b2014-01-23 15:55:39 -08003490 struct task_struct *pos, *task;
Oleg Nesterov9f6e9632014-01-23 15:55:40 -08003491 unsigned long nr = f_pos;
3492
3493 if (nr != f_pos) /* 32bit overflow? */
3494 return NULL;
Eric W. Biederman0bc58a92006-06-26 00:25:50 -07003495
Eric W. Biedermancc288732006-06-26 00:26:01 -07003496 rcu_read_lock();
Oleg Nesterovd855a4b2014-01-23 15:55:39 -08003497 task = pid_task(pid, PIDTYPE_PID);
3498 if (!task)
3499 goto fail;
3500
3501 /* Attempt to start with the tid of a thread */
Oleg Nesterov9f6e9632014-01-23 15:55:40 -08003502 if (tid && nr) {
Pavel Emelyanovb4888932007-10-18 23:40:14 -07003503 pos = find_task_by_pid_ns(tid, ns);
Oleg Nesterovd855a4b2014-01-23 15:55:39 -08003504 if (pos && same_thread_group(pos, task))
Oleg Nesterova872ff02006-06-26 00:26:01 -07003505 goto found;
Eric W. Biederman0bc58a92006-06-26 00:25:50 -07003506 }
3507
3508 /* If nr exceeds the number of threads there is nothing todo */
Oleg Nesterov9f6e9632014-01-23 15:55:40 -08003509 if (nr >= get_nr_threads(task))
Oleg Nesterovc986c142014-01-23 15:55:38 -08003510 goto fail;
Oleg Nesterova872ff02006-06-26 00:26:01 -07003511
3512 /* If we haven't found our starting place yet start
3513 * with the leader and walk nr threads forward.
3514 */
Oleg Nesterovd855a4b2014-01-23 15:55:39 -08003515 pos = task = task->group_leader;
Oleg Nesterovc986c142014-01-23 15:55:38 -08003516 do {
Oleg Nesterov9f6e9632014-01-23 15:55:40 -08003517 if (!nr--)
Oleg Nesterovc986c142014-01-23 15:55:38 -08003518 goto found;
Oleg Nesterovd855a4b2014-01-23 15:55:39 -08003519 } while_each_thread(task, pos);
Oleg Nesterovc986c142014-01-23 15:55:38 -08003520fail:
3521 pos = NULL;
3522 goto out;
Oleg Nesterova872ff02006-06-26 00:26:01 -07003523found:
3524 get_task_struct(pos);
3525out:
Eric W. Biedermancc288732006-06-26 00:26:01 -07003526 rcu_read_unlock();
Eric W. Biederman0bc58a92006-06-26 00:25:50 -07003527 return pos;
3528}
3529
3530/*
3531 * Find the next thread in the thread list.
3532 * Return NULL if there is an error or no next thread.
3533 *
3534 * The reference to the input task_struct is released.
3535 */
3536static struct task_struct *next_tid(struct task_struct *start)
3537{
Oleg Nesterovc1df7fb2006-06-26 00:26:02 -07003538 struct task_struct *pos = NULL;
Eric W. Biedermancc288732006-06-26 00:26:01 -07003539 rcu_read_lock();
Oleg Nesterovc1df7fb2006-06-26 00:26:02 -07003540 if (pid_alive(start)) {
Eric W. Biederman0bc58a92006-06-26 00:25:50 -07003541 pos = next_thread(start);
Oleg Nesterovc1df7fb2006-06-26 00:26:02 -07003542 if (thread_group_leader(pos))
3543 pos = NULL;
3544 else
3545 get_task_struct(pos);
3546 }
Eric W. Biedermancc288732006-06-26 00:26:01 -07003547 rcu_read_unlock();
Eric W. Biederman0bc58a92006-06-26 00:25:50 -07003548 put_task_struct(start);
3549 return pos;
3550}
3551
Linus Torvalds1da177e2005-04-16 15:20:36 -07003552/* for the /proc/TGID/task/ directories */
Al Virof0c3b502013-05-16 12:07:31 -04003553static int proc_task_readdir(struct file *file, struct dir_context *ctx)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003554{
Oleg Nesterovd855a4b2014-01-23 15:55:39 -08003555 struct inode *inode = file_inode(file);
3556 struct task_struct *task;
Pavel Emelyanovb4888932007-10-18 23:40:14 -07003557 struct pid_namespace *ns;
Al Virof0c3b502013-05-16 12:07:31 -04003558 int tid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003559
Oleg Nesterovd855a4b2014-01-23 15:55:39 -08003560 if (proc_inode_is_dead(inode))
Al Virof0c3b502013-05-16 12:07:31 -04003561 return -ENOENT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003562
Al Virof0c3b502013-05-16 12:07:31 -04003563 if (!dir_emit_dots(file, ctx))
Oleg Nesterovd855a4b2014-01-23 15:55:39 -08003564 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003565
Eric W. Biederman0bc58a92006-06-26 00:25:50 -07003566 /* f_version caches the tgid value that the last readdir call couldn't
3567 * return. lseek aka telldir automagically resets f_version to 0.
3568 */
Christoph Hellwig76f668b2018-05-16 07:19:01 +02003569 ns = proc_pid_ns(inode);
Al Virof0c3b502013-05-16 12:07:31 -04003570 tid = (int)file->f_version;
3571 file->f_version = 0;
Oleg Nesterovd855a4b2014-01-23 15:55:39 -08003572 for (task = first_tid(proc_pid(inode), tid, ctx->pos - 2, ns);
Eric W. Biederman0bc58a92006-06-26 00:25:50 -07003573 task;
Al Virof0c3b502013-05-16 12:07:31 -04003574 task = next_tid(task), ctx->pos++) {
Alexey Dobriyane3912ac2018-02-06 15:36:51 -08003575 char name[10 + 1];
Al Virof0c3b502013-05-16 12:07:31 -04003576 int len;
Pavel Emelyanovb4888932007-10-18 23:40:14 -07003577 tid = task_pid_nr_ns(task, ns);
Alexey Dobriyane3912ac2018-02-06 15:36:51 -08003578 len = snprintf(name, sizeof(name), "%u", tid);
Al Virof0c3b502013-05-16 12:07:31 -04003579 if (!proc_fill_cache(file, ctx, name, len,
3580 proc_task_instantiate, task, NULL)) {
Eric W. Biederman0bc58a92006-06-26 00:25:50 -07003581 /* returning this tgid failed, save it as the first
3582 * pid for the next readir call */
Al Virof0c3b502013-05-16 12:07:31 -04003583 file->f_version = (u64)tid;
Eric W. Biederman0bc58a92006-06-26 00:25:50 -07003584 put_task_struct(task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003585 break;
Eric W. Biederman0bc58a92006-06-26 00:25:50 -07003586 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003587 }
Oleg Nesterovd855a4b2014-01-23 15:55:39 -08003588
Al Virof0c3b502013-05-16 12:07:31 -04003589 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003590}
Eric W. Biederman6e66b522006-06-26 00:25:47 -07003591
David Howellsa528d352017-01-31 16:46:22 +00003592static int proc_task_getattr(const struct path *path, struct kstat *stat,
3593 u32 request_mask, unsigned int query_flags)
Eric W. Biederman6e66b522006-06-26 00:25:47 -07003594{
David Howellsa528d352017-01-31 16:46:22 +00003595 struct inode *inode = d_inode(path->dentry);
Eric W. Biederman99f89552006-06-26 00:25:55 -07003596 struct task_struct *p = get_proc_task(inode);
Eric W. Biederman6e66b522006-06-26 00:25:47 -07003597 generic_fillattr(inode, stat);
3598
Eric W. Biederman99f89552006-06-26 00:25:55 -07003599 if (p) {
Eric W. Biederman99f89552006-06-26 00:25:55 -07003600 stat->nlink += get_nr_threads(p);
Eric W. Biederman99f89552006-06-26 00:25:55 -07003601 put_task_struct(p);
Eric W. Biederman6e66b522006-06-26 00:25:47 -07003602 }
3603
3604 return 0;
3605}
Eric W. Biederman28a6d672006-10-02 02:17:05 -07003606
Arjan van de Venc5ef1c42007-02-12 00:55:40 -08003607static const struct inode_operations proc_task_inode_operations = {
Eric W. Biederman28a6d672006-10-02 02:17:05 -07003608 .lookup = proc_task_lookup,
3609 .getattr = proc_task_getattr,
3610 .setattr = proc_setattr,
Vasiliy Kulikov04996802012-01-10 15:11:31 -08003611 .permission = proc_pid_permission,
Eric W. Biederman28a6d672006-10-02 02:17:05 -07003612};
3613
Arjan van de Ven00977a52007-02-12 00:55:34 -08003614static const struct file_operations proc_task_operations = {
Eric W. Biederman28a6d672006-10-02 02:17:05 -07003615 .read = generic_read_dir,
Al Virof50752e2016-04-20 17:13:54 -04003616 .iterate_shared = proc_task_readdir,
3617 .llseek = generic_file_llseek,
Eric W. Biederman28a6d672006-10-02 02:17:05 -07003618};
Alexey Dobriyan1270dd82016-12-12 16:45:32 -08003619
3620void __init set_proc_pid_nlink(void)
3621{
3622 nlink_tid = pid_entry_nlink(tid_base_stuff, ARRAY_SIZE(tid_base_stuff));
3623 nlink_tgid = pid_entry_nlink(tgid_base_stuff, ARRAY_SIZE(tgid_base_stuff));
3624}