blob: 97af285c63e152b13334e9fb40ad4966e6ec08c7 [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;
Alexey Dobriyan3cb4e162018-06-07 17:10:02 -0700216 unsigned long len1, len2;
Alexey Dobriyan6a6cbe72018-06-07 17:09:59 -0700217 char __user *buf0 = buf;
Alexey Dobriyan3cb4e162018-06-07 17:10:02 -0700218 struct {
219 unsigned long p;
220 unsigned long len;
221 } cmdline[2];
Alexey Dobriyanc2c0bb42015-06-25 15:00:54 -0700222 char c;
Alexey Dobriyan6a6cbe72018-06-07 17:09:59 -0700223 int rv;
Alexey Dobriyanc2c0bb42015-06-25 15:00:54 -0700224
225 BUG_ON(*pos < 0);
226
227 tsk = get_proc_task(file_inode(file));
228 if (!tsk)
229 return -ESRCH;
230 mm = get_task_mm(tsk);
231 put_task_struct(tsk);
232 if (!mm)
233 return 0;
234 /* Check if process spawned far enough to have cmdline. */
235 if (!mm->env_end) {
236 rv = 0;
237 goto out_mmput;
238 }
239
Michal Hocko0ee931c2017-09-13 16:28:29 -0700240 page = (char *)__get_free_page(GFP_KERNEL);
Alexey Dobriyanc2c0bb42015-06-25 15:00:54 -0700241 if (!page) {
242 rv = -ENOMEM;
243 goto out_mmput;
244 }
245
Yang Shi88aa7cc2018-06-07 17:05:28 -0700246 spin_lock(&mm->arg_lock);
Alexey Dobriyanc2c0bb42015-06-25 15:00:54 -0700247 arg_start = mm->arg_start;
248 arg_end = mm->arg_end;
249 env_start = mm->env_start;
250 env_end = mm->env_end;
Yang Shi88aa7cc2018-06-07 17:05:28 -0700251 spin_unlock(&mm->arg_lock);
Alexey Dobriyanc2c0bb42015-06-25 15:00:54 -0700252
253 BUG_ON(arg_start > arg_end);
254 BUG_ON(env_start > env_end);
255
256 len1 = arg_end - arg_start;
257 len2 = env_end - env_start;
258
Alexey Dobriyan3581d452015-07-17 16:24:09 -0700259 /* Empty ARGV. */
Alexey Dobriyan6a6cbe72018-06-07 17:09:59 -0700260 if (len1 == 0)
261 goto end;
262
Alexey Dobriyan2ca66ff2014-08-08 14:21:41 -0700263 /*
Alexey Dobriyanc2c0bb42015-06-25 15:00:54 -0700264 * Inherently racy -- command line shares address space
265 * with code and data.
Alexey Dobriyan2ca66ff2014-08-08 14:21:41 -0700266 */
Alexey Dobriyan6a6cbe72018-06-07 17:09:59 -0700267 if (access_remote_vm(mm, arg_end - 1, &c, 1, FOLL_ANON) != 1)
268 goto end;
Alexey Dobriyanc2c0bb42015-06-25 15:00:54 -0700269
Alexey Dobriyan3cb4e162018-06-07 17:10:02 -0700270 cmdline[0].p = arg_start;
271 cmdline[0].len = len1;
Alexey Dobriyanc2c0bb42015-06-25 15:00:54 -0700272 if (c == '\0') {
273 /* Command line (set of strings) occupies whole ARGV. */
Alexey Dobriyan3cb4e162018-06-07 17:10:02 -0700274 cmdline[1].len = 0;
Alexey Dobriyanc2c0bb42015-06-25 15:00:54 -0700275 } else {
276 /*
Alexey Dobriyanc2c0bb42015-06-25 15:00:54 -0700277 * Command line (1 string) occupies ARGV and
278 * extends into ENVP.
279 */
Alexey Dobriyan3cb4e162018-06-07 17:10:02 -0700280 cmdline[1].p = env_start;
281 cmdline[1].len = len2;
282 }
283
284 {
Alexey Dobriyana0a07b82017-02-24 15:00:20 -0800285 loff_t pos1 = *pos;
286 unsigned int i;
287
288 i = 0;
289 while (i < 2 && pos1 >= cmdline[i].len) {
290 pos1 -= cmdline[i].len;
291 i++;
Alexey Dobriyanc2c0bb42015-06-25 15:00:54 -0700292 }
Alexey Dobriyana0a07b82017-02-24 15:00:20 -0800293 while (i < 2) {
Alexey Dobriyan3cb4e162018-06-07 17:10:02 -0700294 unsigned long p;
295 unsigned long len;
296
Alexey Dobriyana0a07b82017-02-24 15:00:20 -0800297 p = cmdline[i].p + pos1;
298 len = cmdline[i].len - pos1;
299 while (count > 0 && len > 0) {
Alexey Dobriyan6a6b9c42018-06-07 17:09:55 -0700300 unsigned int nr_read, nr_write;
Alexey Dobriyanc2c0bb42015-06-25 15:00:54 -0700301
Alexey Dobriyanb42262a2018-06-07 17:09:52 -0700302 nr_read = min3(count, len, PAGE_SIZE);
303 nr_read = access_remote_vm(mm, p, page, nr_read, FOLL_ANON);
304 if (nr_read == 0)
Alexey Dobriyan6a6cbe72018-06-07 17:09:59 -0700305 goto end;
Alexey Dobriyanc2c0bb42015-06-25 15:00:54 -0700306
Alexey Dobriyana0a07b82017-02-24 15:00:20 -0800307 /*
308 * Command line can be shorter than whole ARGV
309 * even if last "marker" byte says it is not.
310 */
Alexey Dobriyan3cb4e162018-06-07 17:10:02 -0700311 if (c == '\0')
312 nr_write = nr_read;
313 else
314 nr_write = strnlen(page, nr_read);
Alexey Dobriyana0a07b82017-02-24 15:00:20 -0800315
Alexey Dobriyan6a6b9c42018-06-07 17:09:55 -0700316 if (copy_to_user(buf, page, nr_write)) {
Alexey Dobriyana0a07b82017-02-24 15:00:20 -0800317 rv = -EFAULT;
318 goto out_free_page;
319 }
320
Alexey Dobriyan6a6b9c42018-06-07 17:09:55 -0700321 p += nr_write;
322 len -= nr_write;
323 buf += nr_write;
324 count -= nr_write;
Alexey Dobriyana0a07b82017-02-24 15:00:20 -0800325
Alexey Dobriyan6a6b9c42018-06-07 17:09:55 -0700326 if (nr_write < nr_read)
Alexey Dobriyan6a6cbe72018-06-07 17:09:59 -0700327 goto end;
Alexey Dobriyanc2c0bb42015-06-25 15:00:54 -0700328 }
329
Alexey Dobriyana0a07b82017-02-24 15:00:20 -0800330 /* Only first chunk can be read partially. */
331 pos1 = 0;
332 i++;
Alexey Dobriyanc2c0bb42015-06-25 15:00:54 -0700333 }
Alexey Dobriyanc2c0bb42015-06-25 15:00:54 -0700334 }
335
Alexey Dobriyan6a6cbe72018-06-07 17:09:59 -0700336end:
337 *pos += buf - buf0;
338 rv = buf - buf0;
Alexey Dobriyanc2c0bb42015-06-25 15:00:54 -0700339out_free_page:
340 free_page((unsigned long)page);
341out_mmput:
342 mmput(mm);
Alexey Dobriyanc2c0bb42015-06-25 15:00:54 -0700343 return rv;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700344}
345
Alexey Dobriyanc2c0bb42015-06-25 15:00:54 -0700346static const struct file_operations proc_pid_cmdline_ops = {
347 .read = proc_pid_cmdline_read,
348 .llseek = generic_file_llseek,
349};
350
Linus Torvalds1da177e2005-04-16 15:20:36 -0700351#ifdef CONFIG_KALLSYMS
352/*
353 * Provides a wchan file via kallsyms in a proper one-value-per-file format.
354 * Returns the resolved symbol. If that fails, simply return the address.
355 */
Alexey Dobriyanedfcd602014-08-08 14:21:44 -0700356static int proc_pid_wchan(struct seq_file *m, struct pid_namespace *ns,
357 struct pid *pid, struct task_struct *task)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700358{
Alexey Dobriyanffb45122007-05-08 00:28:41 -0700359 unsigned long wchan;
Tejun Heo9281ace2007-07-17 04:03:51 -0700360 char symname[KSYM_NAME_LEN];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700361
Alexey Dobriyan24b2ec22018-04-10 16:31:30 -0700362 if (!ptrace_may_access(task, PTRACE_MODE_READ_FSCREDS))
363 goto print0;
364
Linus Torvalds1da177e2005-04-16 15:20:36 -0700365 wchan = get_wchan(task);
Alexey Dobriyan24b2ec22018-04-10 16:31:30 -0700366 if (wchan && !lookup_symbol_name(wchan, symname)) {
Alexey Dobriyan21dae0a2018-04-10 16:31:34 -0700367 seq_puts(m, symname);
Alexey Dobriyan24b2ec22018-04-10 16:31:30 -0700368 return 0;
369 }
Joe Perches25ce3192015-04-15 16:18:17 -0700370
Alexey Dobriyan24b2ec22018-04-10 16:31:30 -0700371print0:
372 seq_putc(m, '0');
Joe Perches25ce3192015-04-15 16:18:17 -0700373 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700374}
375#endif /* CONFIG_KALLSYMS */
376
Al Viroa9712bc2011-03-23 15:52:50 -0400377static int lock_trace(struct task_struct *task)
378{
379 int err = mutex_lock_killable(&task->signal->cred_guard_mutex);
380 if (err)
381 return err;
Jann Horncaaee622016-01-20 15:00:04 -0800382 if (!ptrace_may_access(task, PTRACE_MODE_ATTACH_FSCREDS)) {
Al Viroa9712bc2011-03-23 15:52:50 -0400383 mutex_unlock(&task->signal->cred_guard_mutex);
384 return -EPERM;
385 }
386 return 0;
387}
388
389static void unlock_trace(struct task_struct *task)
390{
391 mutex_unlock(&task->signal->cred_guard_mutex);
392}
393
Ken Chen2ec220e2008-11-10 11:26:08 +0300394#ifdef CONFIG_STACKTRACE
395
396#define MAX_STACK_TRACE_DEPTH 64
397
398static int proc_pid_stack(struct seq_file *m, struct pid_namespace *ns,
399 struct pid *pid, struct task_struct *task)
400{
401 struct stack_trace trace;
402 unsigned long *entries;
Al Viroa9712bc2011-03-23 15:52:50 -0400403 int err;
Ken Chen2ec220e2008-11-10 11:26:08 +0300404 int i;
405
406 entries = kmalloc(MAX_STACK_TRACE_DEPTH * sizeof(*entries), GFP_KERNEL);
407 if (!entries)
408 return -ENOMEM;
409
410 trace.nr_entries = 0;
411 trace.max_entries = MAX_STACK_TRACE_DEPTH;
412 trace.entries = entries;
413 trace.skip = 0;
Ken Chen2ec220e2008-11-10 11:26:08 +0300414
Al Viroa9712bc2011-03-23 15:52:50 -0400415 err = lock_trace(task);
416 if (!err) {
417 save_stack_trace_tsk(task, &trace);
418
419 for (i = 0; i < trace.nr_entries; i++) {
Linus Torvalds8f5abe82017-11-27 16:45:56 -0800420 seq_printf(m, "[<0>] %pB\n", (void *)entries[i]);
Al Viroa9712bc2011-03-23 15:52:50 -0400421 }
422 unlock_trace(task);
Ken Chen2ec220e2008-11-10 11:26:08 +0300423 }
424 kfree(entries);
425
Al Viroa9712bc2011-03-23 15:52:50 -0400426 return err;
Ken Chen2ec220e2008-11-10 11:26:08 +0300427}
428#endif
429
Naveen N. Rao5968cec2015-06-30 14:36:03 +0530430#ifdef CONFIG_SCHED_INFO
Linus Torvalds1da177e2005-04-16 15:20:36 -0700431/*
432 * Provides /proc/PID/schedstat
433 */
Alexey Dobriyanf6e826c2014-08-08 14:21:46 -0700434static int proc_pid_schedstat(struct seq_file *m, struct pid_namespace *ns,
435 struct pid *pid, struct task_struct *task)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700436{
Naveen N. Rao5968cec2015-06-30 14:36:03 +0530437 if (unlikely(!sched_info_on()))
438 seq_printf(m, "0 0 0\n");
439 else
440 seq_printf(m, "%llu %llu %lu\n",
Joe Perches25ce3192015-04-15 16:18:17 -0700441 (unsigned long long)task->se.sum_exec_runtime,
442 (unsigned long long)task->sched_info.run_delay,
443 task->sched_info.pcount);
444
445 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700446}
447#endif
448
Arjan van de Ven97455122008-01-25 21:08:34 +0100449#ifdef CONFIG_LATENCYTOP
450static int lstats_show_proc(struct seq_file *m, void *v)
451{
452 int i;
Hiroshi Shimamoto13d77c32008-02-20 16:53:29 -0800453 struct inode *inode = m->private;
454 struct task_struct *task = get_proc_task(inode);
Arjan van de Ven97455122008-01-25 21:08:34 +0100455
Hiroshi Shimamoto13d77c32008-02-20 16:53:29 -0800456 if (!task)
457 return -ESRCH;
458 seq_puts(m, "Latency Top version : v0.1\n");
Arjan van de Ven97455122008-01-25 21:08:34 +0100459 for (i = 0; i < 32; i++) {
Joe Perches34e49d42011-01-12 17:00:30 -0800460 struct latency_record *lr = &task->latency_record[i];
461 if (lr->backtrace[0]) {
Arjan van de Ven97455122008-01-25 21:08:34 +0100462 int q;
Joe Perches34e49d42011-01-12 17:00:30 -0800463 seq_printf(m, "%i %li %li",
464 lr->count, lr->time, lr->max);
Arjan van de Ven97455122008-01-25 21:08:34 +0100465 for (q = 0; q < LT_BACKTRACEDEPTH; q++) {
Joe Perches34e49d42011-01-12 17:00:30 -0800466 unsigned long bt = lr->backtrace[q];
467 if (!bt)
Arjan van de Ven97455122008-01-25 21:08:34 +0100468 break;
Joe Perches34e49d42011-01-12 17:00:30 -0800469 if (bt == ULONG_MAX)
Arjan van de Ven97455122008-01-25 21:08:34 +0100470 break;
Joe Perches34e49d42011-01-12 17:00:30 -0800471 seq_printf(m, " %ps", (void *)bt);
Arjan van de Ven97455122008-01-25 21:08:34 +0100472 }
Alexey Dobriyan9d6de122011-01-12 17:00:32 -0800473 seq_putc(m, '\n');
Arjan van de Ven97455122008-01-25 21:08:34 +0100474 }
475
476 }
Hiroshi Shimamoto13d77c32008-02-20 16:53:29 -0800477 put_task_struct(task);
Arjan van de Ven97455122008-01-25 21:08:34 +0100478 return 0;
479}
480
481static int lstats_open(struct inode *inode, struct file *file)
482{
Hiroshi Shimamoto13d77c32008-02-20 16:53:29 -0800483 return single_open(file, lstats_show_proc, inode);
Hiroshi Shimamotod6643d12008-02-14 10:27:00 -0800484}
485
Arjan van de Ven97455122008-01-25 21:08:34 +0100486static ssize_t lstats_write(struct file *file, const char __user *buf,
487 size_t count, loff_t *offs)
488{
Al Viro496ad9a2013-01-23 17:07:38 -0500489 struct task_struct *task = get_proc_task(file_inode(file));
Arjan van de Ven97455122008-01-25 21:08:34 +0100490
Hiroshi Shimamoto13d77c32008-02-20 16:53:29 -0800491 if (!task)
492 return -ESRCH;
Arjan van de Ven97455122008-01-25 21:08:34 +0100493 clear_all_latency_tracing(task);
Hiroshi Shimamoto13d77c32008-02-20 16:53:29 -0800494 put_task_struct(task);
Arjan van de Ven97455122008-01-25 21:08:34 +0100495
496 return count;
497}
498
499static const struct file_operations proc_lstats_operations = {
500 .open = lstats_open,
501 .read = seq_read,
502 .write = lstats_write,
503 .llseek = seq_lseek,
Hiroshi Shimamoto13d77c32008-02-20 16:53:29 -0800504 .release = single_release,
Arjan van de Ven97455122008-01-25 21:08:34 +0100505};
506
507#endif
508
Alexey Dobriyan6ba51e32014-08-08 14:21:48 -0700509static int proc_oom_score(struct seq_file *m, struct pid_namespace *ns,
510 struct pid *pid, struct task_struct *task)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700511{
David Rientjesa7f638f2012-05-29 15:06:47 -0700512 unsigned long totalpages = totalram_pages + total_swap_pages;
Oleg Nesterovb95c35e2010-04-01 15:13:57 +0200513 unsigned long points = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700514
Oleg Nesterovef419392016-08-02 14:03:19 -0700515 points = oom_badness(task, NULL, NULL, totalpages) *
516 1000 / totalpages;
Joe Perches25ce3192015-04-15 16:18:17 -0700517 seq_printf(m, "%lu\n", points);
518
519 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700520}
521
Neil Hormand85f50d2007-10-18 23:40:37 -0700522struct limit_names {
Alexey Dobriyancedbcca2014-08-08 14:21:33 -0700523 const char *name;
524 const char *unit;
Neil Hormand85f50d2007-10-18 23:40:37 -0700525};
526
527static const struct limit_names lnames[RLIM_NLIMITS] = {
Kees Cookcff4edb2009-09-22 16:45:32 -0700528 [RLIMIT_CPU] = {"Max cpu time", "seconds"},
Neil Hormand85f50d2007-10-18 23:40:37 -0700529 [RLIMIT_FSIZE] = {"Max file size", "bytes"},
530 [RLIMIT_DATA] = {"Max data size", "bytes"},
531 [RLIMIT_STACK] = {"Max stack size", "bytes"},
532 [RLIMIT_CORE] = {"Max core file size", "bytes"},
533 [RLIMIT_RSS] = {"Max resident set", "bytes"},
534 [RLIMIT_NPROC] = {"Max processes", "processes"},
535 [RLIMIT_NOFILE] = {"Max open files", "files"},
536 [RLIMIT_MEMLOCK] = {"Max locked memory", "bytes"},
537 [RLIMIT_AS] = {"Max address space", "bytes"},
538 [RLIMIT_LOCKS] = {"Max file locks", "locks"},
539 [RLIMIT_SIGPENDING] = {"Max pending signals", "signals"},
540 [RLIMIT_MSGQUEUE] = {"Max msgqueue size", "bytes"},
541 [RLIMIT_NICE] = {"Max nice priority", NULL},
542 [RLIMIT_RTPRIO] = {"Max realtime priority", NULL},
Eugene Teo88081172008-02-23 15:23:52 -0800543 [RLIMIT_RTTIME] = {"Max realtime timeout", "us"},
Neil Hormand85f50d2007-10-18 23:40:37 -0700544};
545
546/* Display limits for a process */
Alexey Dobriyan1c963eb2014-08-08 14:21:37 -0700547static int proc_pid_limits(struct seq_file *m, struct pid_namespace *ns,
548 struct pid *pid, struct task_struct *task)
Neil Hormand85f50d2007-10-18 23:40:37 -0700549{
550 unsigned int i;
Neil Hormand85f50d2007-10-18 23:40:37 -0700551 unsigned long flags;
Neil Hormand85f50d2007-10-18 23:40:37 -0700552
553 struct rlimit rlim[RLIM_NLIMITS];
554
Lai Jiangshana6bebbc2008-10-05 00:51:15 +0400555 if (!lock_task_sighand(task, &flags))
Neil Hormand85f50d2007-10-18 23:40:37 -0700556 return 0;
Neil Hormand85f50d2007-10-18 23:40:37 -0700557 memcpy(rlim, task->signal->rlim, sizeof(struct rlimit) * RLIM_NLIMITS);
558 unlock_task_sighand(task, &flags);
Neil Hormand85f50d2007-10-18 23:40:37 -0700559
560 /*
561 * print the file header
562 */
Alexey Dobriyan1c963eb2014-08-08 14:21:37 -0700563 seq_printf(m, "%-25s %-20s %-20s %-10s\n",
Joe Perches25ce3192015-04-15 16:18:17 -0700564 "Limit", "Soft Limit", "Hard Limit", "Units");
Neil Hormand85f50d2007-10-18 23:40:37 -0700565
566 for (i = 0; i < RLIM_NLIMITS; i++) {
567 if (rlim[i].rlim_cur == RLIM_INFINITY)
Alexey Dobriyan1c963eb2014-08-08 14:21:37 -0700568 seq_printf(m, "%-25s %-20s ",
Joe Perches25ce3192015-04-15 16:18:17 -0700569 lnames[i].name, "unlimited");
Neil Hormand85f50d2007-10-18 23:40:37 -0700570 else
Alexey Dobriyan1c963eb2014-08-08 14:21:37 -0700571 seq_printf(m, "%-25s %-20lu ",
Joe Perches25ce3192015-04-15 16:18:17 -0700572 lnames[i].name, rlim[i].rlim_cur);
Neil Hormand85f50d2007-10-18 23:40:37 -0700573
574 if (rlim[i].rlim_max == RLIM_INFINITY)
Alexey Dobriyan1c963eb2014-08-08 14:21:37 -0700575 seq_printf(m, "%-20s ", "unlimited");
Neil Hormand85f50d2007-10-18 23:40:37 -0700576 else
Alexey Dobriyan1c963eb2014-08-08 14:21:37 -0700577 seq_printf(m, "%-20lu ", rlim[i].rlim_max);
Neil Hormand85f50d2007-10-18 23:40:37 -0700578
579 if (lnames[i].unit)
Alexey Dobriyan1c963eb2014-08-08 14:21:37 -0700580 seq_printf(m, "%-10s\n", lnames[i].unit);
Neil Hormand85f50d2007-10-18 23:40:37 -0700581 else
Alexey Dobriyan1c963eb2014-08-08 14:21:37 -0700582 seq_putc(m, '\n');
Neil Hormand85f50d2007-10-18 23:40:37 -0700583 }
584
Alexey Dobriyan1c963eb2014-08-08 14:21:37 -0700585 return 0;
Neil Hormand85f50d2007-10-18 23:40:37 -0700586}
587
Roland McGrathebcb6732008-07-25 19:46:00 -0700588#ifdef CONFIG_HAVE_ARCH_TRACEHOOK
Alexey Dobriyan09d93bd2014-08-08 14:21:39 -0700589static int proc_pid_syscall(struct seq_file *m, struct pid_namespace *ns,
590 struct pid *pid, struct task_struct *task)
Roland McGrathebcb6732008-07-25 19:46:00 -0700591{
592 long nr;
593 unsigned long args[6], sp, pc;
Joe Perches25ce3192015-04-15 16:18:17 -0700594 int res;
595
596 res = lock_trace(task);
Al Viroa9712bc2011-03-23 15:52:50 -0400597 if (res)
598 return res;
Roland McGrathebcb6732008-07-25 19:46:00 -0700599
600 if (task_current_syscall(task, &nr, args, 6, &sp, &pc))
Alexey Dobriyan09d93bd2014-08-08 14:21:39 -0700601 seq_puts(m, "running\n");
Al Viroa9712bc2011-03-23 15:52:50 -0400602 else if (nr < 0)
Alexey Dobriyan09d93bd2014-08-08 14:21:39 -0700603 seq_printf(m, "%ld 0x%lx 0x%lx\n", nr, sp, pc);
Al Viroa9712bc2011-03-23 15:52:50 -0400604 else
Alexey Dobriyan09d93bd2014-08-08 14:21:39 -0700605 seq_printf(m,
Roland McGrathebcb6732008-07-25 19:46:00 -0700606 "%ld 0x%lx 0x%lx 0x%lx 0x%lx 0x%lx 0x%lx 0x%lx 0x%lx\n",
607 nr,
608 args[0], args[1], args[2], args[3], args[4], args[5],
609 sp, pc);
Al Viroa9712bc2011-03-23 15:52:50 -0400610 unlock_trace(task);
Joe Perches25ce3192015-04-15 16:18:17 -0700611
612 return 0;
Roland McGrathebcb6732008-07-25 19:46:00 -0700613}
614#endif /* CONFIG_HAVE_ARCH_TRACEHOOK */
615
Linus Torvalds1da177e2005-04-16 15:20:36 -0700616/************************************************************************/
617/* Here the fs part begins */
618/************************************************************************/
619
620/* permission checks */
Eric W. Biederman778c1142006-06-26 00:25:58 -0700621static int proc_fd_access_allowed(struct inode *inode)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700622{
Eric W. Biederman778c1142006-06-26 00:25:58 -0700623 struct task_struct *task;
624 int allowed = 0;
Eric W. Biedermandf26c402006-06-26 00:25:59 -0700625 /* Allow access to a task's file descriptors if it is us or we
626 * may use ptrace attach to the process and find out that
627 * information.
Eric W. Biederman778c1142006-06-26 00:25:58 -0700628 */
629 task = get_proc_task(inode);
Eric W. Biedermandf26c402006-06-26 00:25:59 -0700630 if (task) {
Jann Horncaaee622016-01-20 15:00:04 -0800631 allowed = ptrace_may_access(task, PTRACE_MODE_READ_FSCREDS);
Eric W. Biederman778c1142006-06-26 00:25:58 -0700632 put_task_struct(task);
Eric W. Biedermandf26c402006-06-26 00:25:59 -0700633 }
Eric W. Biederman778c1142006-06-26 00:25:58 -0700634 return allowed;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700635}
636
Eric W. Biederman6b4e3062010-03-07 16:41:34 -0800637int proc_setattr(struct dentry *dentry, struct iattr *attr)
Linus Torvalds6d76fa52006-07-15 12:26:45 -0700638{
639 int error;
David Howells2b0143b2015-03-17 22:25:59 +0000640 struct inode *inode = d_inode(dentry);
Linus Torvalds6d76fa52006-07-15 12:26:45 -0700641
642 if (attr->ia_valid & ATTR_MODE)
643 return -EPERM;
644
Jan Kara31051c82016-05-26 16:55:18 +0200645 error = setattr_prepare(dentry, attr);
Christoph Hellwig10257742010-06-04 11:30:02 +0200646 if (error)
647 return error;
648
Christoph Hellwig10257742010-06-04 11:30:02 +0200649 setattr_copy(inode, attr);
650 mark_inode_dirty(inode);
651 return 0;
Linus Torvalds6d76fa52006-07-15 12:26:45 -0700652}
653
Vasiliy Kulikov04996802012-01-10 15:11:31 -0800654/*
655 * May current process learn task's sched/cmdline info (for hide_pid_min=1)
656 * or euid/egid (for hide_pid_min=2)?
657 */
658static bool has_pid_permissions(struct pid_namespace *pid,
659 struct task_struct *task,
660 int hide_pid_min)
661{
662 if (pid->hide_pid < hide_pid_min)
663 return true;
664 if (in_group_p(pid->pid_gid))
665 return true;
Jann Horncaaee622016-01-20 15:00:04 -0800666 return ptrace_may_access(task, PTRACE_MODE_READ_FSCREDS);
Vasiliy Kulikov04996802012-01-10 15:11:31 -0800667}
668
669
670static int proc_pid_permission(struct inode *inode, int mask)
671{
Christoph Hellwig76f668b2018-05-16 07:19:01 +0200672 struct pid_namespace *pid = proc_pid_ns(inode);
Vasiliy Kulikov04996802012-01-10 15:11:31 -0800673 struct task_struct *task;
674 bool has_perms;
675
676 task = get_proc_task(inode);
Xiaotian Fenga2ef9902012-01-12 17:17:08 -0800677 if (!task)
678 return -ESRCH;
Lafcadio Wluiki796f5712017-02-24 15:00:23 -0800679 has_perms = has_pid_permissions(pid, task, HIDEPID_NO_ACCESS);
Vasiliy Kulikov04996802012-01-10 15:11:31 -0800680 put_task_struct(task);
681
682 if (!has_perms) {
Lafcadio Wluiki796f5712017-02-24 15:00:23 -0800683 if (pid->hide_pid == HIDEPID_INVISIBLE) {
Vasiliy Kulikov04996802012-01-10 15:11:31 -0800684 /*
685 * Let's make getdents(), stat(), and open()
686 * consistent with each other. If a process
687 * may not stat() a file, it shouldn't be seen
688 * in procfs at all.
689 */
690 return -ENOENT;
691 }
692
693 return -EPERM;
694 }
695 return generic_permission(inode, mask);
696}
697
698
699
Arjan van de Venc5ef1c42007-02-12 00:55:40 -0800700static const struct inode_operations proc_def_inode_operations = {
Linus Torvalds6d76fa52006-07-15 12:26:45 -0700701 .setattr = proc_setattr,
702};
703
Eric W. Biedermanbe614082008-02-08 04:18:30 -0800704static int proc_single_show(struct seq_file *m, void *v)
705{
706 struct inode *inode = m->private;
Christoph Hellwig76f668b2018-05-16 07:19:01 +0200707 struct pid_namespace *ns = proc_pid_ns(inode);
708 struct pid *pid = proc_pid(inode);
Eric W. Biedermanbe614082008-02-08 04:18:30 -0800709 struct task_struct *task;
710 int ret;
711
Eric W. Biedermanbe614082008-02-08 04:18:30 -0800712 task = get_pid_task(pid, PIDTYPE_PID);
713 if (!task)
714 return -ESRCH;
715
716 ret = PROC_I(inode)->op.proc_show(m, ns, pid, task);
717
718 put_task_struct(task);
719 return ret;
720}
721
722static int proc_single_open(struct inode *inode, struct file *filp)
723{
Jovi Zhangc6a34052011-01-12 17:00:34 -0800724 return single_open(filp, proc_single_show, inode);
Eric W. Biedermanbe614082008-02-08 04:18:30 -0800725}
726
727static const struct file_operations proc_single_file_operations = {
728 .open = proc_single_open,
729 .read = seq_read,
730 .llseek = seq_lseek,
731 .release = single_release,
732};
733
Oleg Nesterov5381e162014-10-09 15:25:24 -0700734
735struct mm_struct *proc_mem_open(struct inode *inode, unsigned int mode)
736{
737 struct task_struct *task = get_proc_task(inode);
738 struct mm_struct *mm = ERR_PTR(-ESRCH);
739
740 if (task) {
Jann Horncaaee622016-01-20 15:00:04 -0800741 mm = mm_access(task, mode | PTRACE_MODE_FSCREDS);
Oleg Nesterov5381e162014-10-09 15:25:24 -0700742 put_task_struct(task);
743
744 if (!IS_ERR_OR_NULL(mm)) {
745 /* ensure this mm_struct can't be freed */
Vegard Nossumf1f10072017-02-27 14:30:07 -0800746 mmgrab(mm);
Oleg Nesterov5381e162014-10-09 15:25:24 -0700747 /* but do not pin its memory */
748 mmput(mm);
749 }
750 }
751
752 return mm;
753}
754
Cong Wangb409e572012-05-31 16:26:17 -0700755static int __mem_open(struct inode *inode, struct file *file, unsigned int mode)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700756{
Oleg Nesterov5381e162014-10-09 15:25:24 -0700757 struct mm_struct *mm = proc_mem_open(inode, mode);
Linus Torvaldse2683372012-01-17 15:21:19 -0800758
759 if (IS_ERR(mm))
760 return PTR_ERR(mm);
761
Linus Torvaldse2683372012-01-17 15:21:19 -0800762 file->private_data = mm;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700763 return 0;
764}
765
Cong Wangb409e572012-05-31 16:26:17 -0700766static int mem_open(struct inode *inode, struct file *file)
767{
Djalal Harounibc452b42012-07-30 14:42:28 -0700768 int ret = __mem_open(inode, file, PTRACE_MODE_ATTACH);
769
770 /* OK to pass negative loff_t, we can catch out-of-range */
771 file->f_mode |= FMODE_UNSIGNED_OFFSET;
772
773 return ret;
Cong Wangb409e572012-05-31 16:26:17 -0700774}
775
Oleg Nesterov572d34b2012-01-31 17:14:54 +0100776static ssize_t mem_rw(struct file *file, char __user *buf,
777 size_t count, loff_t *ppos, int write)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700778{
Linus Torvaldse2683372012-01-17 15:21:19 -0800779 struct mm_struct *mm = file->private_data;
Oleg Nesterov572d34b2012-01-31 17:14:54 +0100780 unsigned long addr = *ppos;
781 ssize_t copied;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700782 char *page;
Linus Torvalds272ddc82016-10-24 19:00:44 -0700783 unsigned int flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700784
Linus Torvaldse2683372012-01-17 15:21:19 -0800785 if (!mm)
786 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700787
Michal Hocko0ee931c2017-09-13 16:28:29 -0700788 page = (char *)__get_free_page(GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700789 if (!page)
Linus Torvaldse2683372012-01-17 15:21:19 -0800790 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700791
Frederik Deweerdtf7ca54f2006-09-29 02:01:02 -0700792 copied = 0;
Vegard Nossum388f7932017-02-27 14:30:13 -0800793 if (!mmget_not_zero(mm))
Oleg Nesterov6d08f2c72012-01-31 17:15:11 +0100794 goto free;
795
Linus Torvaldsf511c0b2017-05-30 12:38:59 -0700796 flags = FOLL_FORCE | (write ? FOLL_WRITE : 0);
Lorenzo Stoakes6347e8d2016-10-13 01:20:19 +0100797
Linus Torvalds1da177e2005-04-16 15:20:36 -0700798 while (count > 0) {
Oleg Nesterov572d34b2012-01-31 17:14:54 +0100799 int this_len = min_t(int, count, PAGE_SIZE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700800
Oleg Nesterov572d34b2012-01-31 17:14:54 +0100801 if (write && copy_from_user(page, buf, this_len)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700802 copied = -EFAULT;
803 break;
804 }
Oleg Nesterov572d34b2012-01-31 17:14:54 +0100805
Lorenzo Stoakes6347e8d2016-10-13 01:20:19 +0100806 this_len = access_remote_vm(mm, addr, page, this_len, flags);
Oleg Nesterov572d34b2012-01-31 17:14:54 +0100807 if (!this_len) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700808 if (!copied)
809 copied = -EIO;
810 break;
811 }
Oleg Nesterov572d34b2012-01-31 17:14:54 +0100812
813 if (!write && copy_to_user(buf, page, this_len)) {
814 copied = -EFAULT;
815 break;
816 }
817
818 buf += this_len;
819 addr += this_len;
820 copied += this_len;
821 count -= this_len;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700822 }
Oleg Nesterov572d34b2012-01-31 17:14:54 +0100823 *ppos = addr;
KOSAKI Motohiro30cd8902011-05-26 16:25:52 -0700824
Oleg Nesterov6d08f2c72012-01-31 17:15:11 +0100825 mmput(mm);
826free:
KOSAKI Motohiro30cd8902011-05-26 16:25:52 -0700827 free_page((unsigned long) page);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700828 return copied;
829}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700830
Oleg Nesterov572d34b2012-01-31 17:14:54 +0100831static ssize_t mem_read(struct file *file, char __user *buf,
832 size_t count, loff_t *ppos)
833{
834 return mem_rw(file, buf, count, ppos, 0);
835}
836
837static ssize_t mem_write(struct file *file, const char __user *buf,
838 size_t count, loff_t *ppos)
839{
840 return mem_rw(file, (char __user*)buf, count, ppos, 1);
841}
842
Matt Mackall85863e42008-02-04 22:29:04 -0800843loff_t mem_lseek(struct file *file, loff_t offset, int orig)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700844{
845 switch (orig) {
846 case 0:
847 file->f_pos = offset;
848 break;
849 case 1:
850 file->f_pos += offset;
851 break;
852 default:
853 return -EINVAL;
854 }
855 force_successful_syscall_return();
856 return file->f_pos;
857}
858
Linus Torvaldse2683372012-01-17 15:21:19 -0800859static int mem_release(struct inode *inode, struct file *file)
860{
861 struct mm_struct *mm = file->private_data;
Oleg Nesterov71879d32012-01-31 17:14:38 +0100862 if (mm)
Oleg Nesterov6d08f2c72012-01-31 17:15:11 +0100863 mmdrop(mm);
Linus Torvaldse2683372012-01-17 15:21:19 -0800864 return 0;
865}
866
Arjan van de Ven00977a52007-02-12 00:55:34 -0800867static const struct file_operations proc_mem_operations = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700868 .llseek = mem_lseek,
869 .read = mem_read,
870 .write = mem_write,
871 .open = mem_open,
Linus Torvaldse2683372012-01-17 15:21:19 -0800872 .release = mem_release,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700873};
874
Cong Wangb409e572012-05-31 16:26:17 -0700875static int environ_open(struct inode *inode, struct file *file)
876{
877 return __mem_open(inode, file, PTRACE_MODE_READ);
878}
879
James Pearson315e28c2007-10-16 23:30:17 -0700880static ssize_t environ_read(struct file *file, char __user *buf,
881 size_t count, loff_t *ppos)
882{
James Pearson315e28c2007-10-16 23:30:17 -0700883 char *page;
884 unsigned long src = *ppos;
Cong Wangb409e572012-05-31 16:26:17 -0700885 int ret = 0;
886 struct mm_struct *mm = file->private_data;
Mateusz Guzika3b609e2016-01-20 15:01:05 -0800887 unsigned long env_start, env_end;
James Pearson315e28c2007-10-16 23:30:17 -0700888
Mathias Krause8148a732016-05-05 16:22:26 -0700889 /* Ensure the process spawned far enough to have an environment. */
890 if (!mm || !mm->env_end)
Cong Wangb409e572012-05-31 16:26:17 -0700891 return 0;
James Pearson315e28c2007-10-16 23:30:17 -0700892
Michal Hocko0ee931c2017-09-13 16:28:29 -0700893 page = (char *)__get_free_page(GFP_KERNEL);
James Pearson315e28c2007-10-16 23:30:17 -0700894 if (!page)
Cong Wangb409e572012-05-31 16:26:17 -0700895 return -ENOMEM;
James Pearson315e28c2007-10-16 23:30:17 -0700896
Al Virod6f64b82011-02-15 22:26:01 -0500897 ret = 0;
Vegard Nossum388f7932017-02-27 14:30:13 -0800898 if (!mmget_not_zero(mm))
Cong Wangb409e572012-05-31 16:26:17 -0700899 goto free;
Mateusz Guzika3b609e2016-01-20 15:01:05 -0800900
Yang Shi88aa7cc2018-06-07 17:05:28 -0700901 spin_lock(&mm->arg_lock);
Mateusz Guzika3b609e2016-01-20 15:01:05 -0800902 env_start = mm->env_start;
903 env_end = mm->env_end;
Yang Shi88aa7cc2018-06-07 17:05:28 -0700904 spin_unlock(&mm->arg_lock);
Mateusz Guzika3b609e2016-01-20 15:01:05 -0800905
James Pearson315e28c2007-10-16 23:30:17 -0700906 while (count > 0) {
Djalal Harounie8905ec2012-07-30 14:42:26 -0700907 size_t this_len, max_len;
908 int retval;
909
Mateusz Guzika3b609e2016-01-20 15:01:05 -0800910 if (src >= (env_end - env_start))
Djalal Harounie8905ec2012-07-30 14:42:26 -0700911 break;
James Pearson315e28c2007-10-16 23:30:17 -0700912
Mateusz Guzika3b609e2016-01-20 15:01:05 -0800913 this_len = env_end - (env_start + src);
James Pearson315e28c2007-10-16 23:30:17 -0700914
Djalal Harounie8905ec2012-07-30 14:42:26 -0700915 max_len = min_t(size_t, PAGE_SIZE, count);
916 this_len = min(max_len, this_len);
James Pearson315e28c2007-10-16 23:30:17 -0700917
Willy Tarreau7f7ccc22018-05-11 08:11:44 +0200918 retval = access_remote_vm(mm, (env_start + src), page, this_len, FOLL_ANON);
James Pearson315e28c2007-10-16 23:30:17 -0700919
920 if (retval <= 0) {
921 ret = retval;
922 break;
923 }
924
925 if (copy_to_user(buf, page, retval)) {
926 ret = -EFAULT;
927 break;
928 }
929
930 ret += retval;
931 src += retval;
932 buf += retval;
933 count -= retval;
934 }
935 *ppos = src;
James Pearson315e28c2007-10-16 23:30:17 -0700936 mmput(mm);
Cong Wangb409e572012-05-31 16:26:17 -0700937
938free:
James Pearson315e28c2007-10-16 23:30:17 -0700939 free_page((unsigned long) page);
James Pearson315e28c2007-10-16 23:30:17 -0700940 return ret;
941}
942
943static const struct file_operations proc_environ_operations = {
Cong Wangb409e572012-05-31 16:26:17 -0700944 .open = environ_open,
James Pearson315e28c2007-10-16 23:30:17 -0700945 .read = environ_read,
Arnd Bergmann87df8422010-03-17 23:06:02 +0100946 .llseek = generic_file_llseek,
Cong Wangb409e572012-05-31 16:26:17 -0700947 .release = mem_release,
James Pearson315e28c2007-10-16 23:30:17 -0700948};
949
Al Viroc5317162016-10-05 18:43:43 -0400950static int auxv_open(struct inode *inode, struct file *file)
951{
952 return __mem_open(inode, file, PTRACE_MODE_READ_FSCREDS);
953}
954
955static ssize_t auxv_read(struct file *file, char __user *buf,
956 size_t count, loff_t *ppos)
957{
958 struct mm_struct *mm = file->private_data;
959 unsigned int nwords = 0;
Leon Yu06b28492016-10-27 17:46:50 -0700960
961 if (!mm)
962 return 0;
Al Viroc5317162016-10-05 18:43:43 -0400963 do {
964 nwords += 2;
965 } while (mm->saved_auxv[nwords - 2] != 0); /* AT_NULL */
966 return simple_read_from_buffer(buf, count, ppos, mm->saved_auxv,
967 nwords * sizeof(mm->saved_auxv[0]));
968}
969
970static const struct file_operations proc_auxv_operations = {
971 .open = auxv_open,
972 .read = auxv_read,
973 .llseek = generic_file_llseek,
974 .release = mem_release,
975};
976
David Rientjesfa0cbbf2012-11-12 17:53:04 -0800977static ssize_t oom_adj_read(struct file *file, char __user *buf, size_t count,
978 loff_t *ppos)
979{
Al Viro496ad9a2013-01-23 17:07:38 -0500980 struct task_struct *task = get_proc_task(file_inode(file));
David Rientjesfa0cbbf2012-11-12 17:53:04 -0800981 char buffer[PROC_NUMBUF];
982 int oom_adj = OOM_ADJUST_MIN;
983 size_t len;
David Rientjesfa0cbbf2012-11-12 17:53:04 -0800984
985 if (!task)
986 return -ESRCH;
Michal Hockof913da52016-07-28 15:44:37 -0700987 if (task->signal->oom_score_adj == OOM_SCORE_ADJ_MAX)
988 oom_adj = OOM_ADJUST_MAX;
989 else
990 oom_adj = (task->signal->oom_score_adj * -OOM_DISABLE) /
991 OOM_SCORE_ADJ_MAX;
David Rientjesfa0cbbf2012-11-12 17:53:04 -0800992 put_task_struct(task);
993 len = snprintf(buffer, sizeof(buffer), "%d\n", oom_adj);
994 return simple_read_from_buffer(buf, count, ppos, buffer, len);
995}
996
Michal Hocko1d5f0ac2016-07-28 15:44:40 -0700997static int __set_oom_adj(struct file *file, int oom_adj, bool legacy)
998{
999 static DEFINE_MUTEX(oom_adj_mutex);
Michal Hocko44a70ade2016-07-28 15:44:43 -07001000 struct mm_struct *mm = NULL;
Michal Hocko1d5f0ac2016-07-28 15:44:40 -07001001 struct task_struct *task;
1002 int err = 0;
1003
1004 task = get_proc_task(file_inode(file));
1005 if (!task)
1006 return -ESRCH;
1007
1008 mutex_lock(&oom_adj_mutex);
1009 if (legacy) {
1010 if (oom_adj < task->signal->oom_score_adj &&
1011 !capable(CAP_SYS_RESOURCE)) {
1012 err = -EACCES;
1013 goto err_unlock;
1014 }
1015 /*
1016 * /proc/pid/oom_adj is provided for legacy purposes, ask users to use
1017 * /proc/pid/oom_score_adj instead.
1018 */
1019 pr_warn_once("%s (%d): /proc/%d/oom_adj is deprecated, please use /proc/%d/oom_score_adj instead.\n",
1020 current->comm, task_pid_nr(current), task_pid_nr(task),
1021 task_pid_nr(task));
1022 } else {
1023 if ((short)oom_adj < task->signal->oom_score_adj_min &&
1024 !capable(CAP_SYS_RESOURCE)) {
1025 err = -EACCES;
1026 goto err_unlock;
1027 }
1028 }
1029
Michal Hocko44a70ade2016-07-28 15:44:43 -07001030 /*
1031 * Make sure we will check other processes sharing the mm if this is
1032 * not vfrok which wants its own oom_score_adj.
1033 * pin the mm so it doesn't go away and get reused after task_unlock
1034 */
1035 if (!task->vfork_done) {
1036 struct task_struct *p = find_lock_task_mm(task);
1037
1038 if (p) {
1039 if (atomic_read(&p->mm->mm_users) > 1) {
1040 mm = p->mm;
Vegard Nossumf1f10072017-02-27 14:30:07 -08001041 mmgrab(mm);
Michal Hocko44a70ade2016-07-28 15:44:43 -07001042 }
1043 task_unlock(p);
1044 }
1045 }
1046
Michal Hocko1d5f0ac2016-07-28 15:44:40 -07001047 task->signal->oom_score_adj = oom_adj;
1048 if (!legacy && has_capability_noaudit(current, CAP_SYS_RESOURCE))
1049 task->signal->oom_score_adj_min = (short)oom_adj;
1050 trace_oom_score_adj_update(task);
Michal Hocko44a70ade2016-07-28 15:44:43 -07001051
1052 if (mm) {
1053 struct task_struct *p;
1054
1055 rcu_read_lock();
1056 for_each_process(p) {
1057 if (same_thread_group(task, p))
1058 continue;
1059
1060 /* do not touch kernel threads or the global init */
1061 if (p->flags & PF_KTHREAD || is_global_init(p))
1062 continue;
1063
1064 task_lock(p);
1065 if (!p->vfork_done && process_shares_mm(p, mm)) {
1066 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",
1067 task_pid_nr(p), p->comm,
1068 p->signal->oom_score_adj, oom_adj,
1069 task_pid_nr(task), task->comm);
1070 p->signal->oom_score_adj = oom_adj;
1071 if (!legacy && has_capability_noaudit(current, CAP_SYS_RESOURCE))
1072 p->signal->oom_score_adj_min = (short)oom_adj;
1073 }
1074 task_unlock(p);
1075 }
1076 rcu_read_unlock();
1077 mmdrop(mm);
1078 }
Michal Hocko1d5f0ac2016-07-28 15:44:40 -07001079err_unlock:
1080 mutex_unlock(&oom_adj_mutex);
1081 put_task_struct(task);
1082 return err;
1083}
Michal Hockof913da52016-07-28 15:44:37 -07001084
David Rientjesb72bdfa2015-11-05 18:50:32 -08001085/*
1086 * /proc/pid/oom_adj exists solely for backwards compatibility with previous
1087 * kernels. The effective policy is defined by oom_score_adj, which has a
1088 * different scale: oom_adj grew exponentially and oom_score_adj grows linearly.
1089 * Values written to oom_adj are simply mapped linearly to oom_score_adj.
1090 * Processes that become oom disabled via oom_adj will still be oom disabled
1091 * with this implementation.
1092 *
1093 * oom_adj cannot be removed since existing userspace binaries use it.
1094 */
David Rientjesfa0cbbf2012-11-12 17:53:04 -08001095static ssize_t oom_adj_write(struct file *file, const char __user *buf,
1096 size_t count, loff_t *ppos)
1097{
David Rientjesfa0cbbf2012-11-12 17:53:04 -08001098 char buffer[PROC_NUMBUF];
1099 int oom_adj;
David Rientjesfa0cbbf2012-11-12 17:53:04 -08001100 int err;
1101
1102 memset(buffer, 0, sizeof(buffer));
1103 if (count > sizeof(buffer) - 1)
1104 count = sizeof(buffer) - 1;
1105 if (copy_from_user(buffer, buf, count)) {
1106 err = -EFAULT;
1107 goto out;
1108 }
1109
1110 err = kstrtoint(strstrip(buffer), 0, &oom_adj);
1111 if (err)
1112 goto out;
1113 if ((oom_adj < OOM_ADJUST_MIN || oom_adj > OOM_ADJUST_MAX) &&
1114 oom_adj != OOM_DISABLE) {
1115 err = -EINVAL;
1116 goto out;
1117 }
1118
David Rientjesfa0cbbf2012-11-12 17:53:04 -08001119 /*
1120 * Scale /proc/pid/oom_score_adj appropriately ensuring that a maximum
1121 * value is always attainable.
1122 */
1123 if (oom_adj == OOM_ADJUST_MAX)
1124 oom_adj = OOM_SCORE_ADJ_MAX;
1125 else
1126 oom_adj = (oom_adj * OOM_SCORE_ADJ_MAX) / -OOM_DISABLE;
1127
Michal Hocko1d5f0ac2016-07-28 15:44:40 -07001128 err = __set_oom_adj(file, oom_adj, true);
David Rientjesfa0cbbf2012-11-12 17:53:04 -08001129out:
1130 return err < 0 ? err : count;
1131}
1132
1133static const struct file_operations proc_oom_adj_operations = {
1134 .read = oom_adj_read,
1135 .write = oom_adj_write,
1136 .llseek = generic_file_llseek,
1137};
1138
David Rientjesa63d83f2010-08-09 17:19:46 -07001139static ssize_t oom_score_adj_read(struct file *file, char __user *buf,
1140 size_t count, loff_t *ppos)
1141{
Al Viro496ad9a2013-01-23 17:07:38 -05001142 struct task_struct *task = get_proc_task(file_inode(file));
David Rientjesa63d83f2010-08-09 17:19:46 -07001143 char buffer[PROC_NUMBUF];
David Rientjesa9c58b902012-12-11 16:02:54 -08001144 short oom_score_adj = OOM_SCORE_ADJ_MIN;
David Rientjesa63d83f2010-08-09 17:19:46 -07001145 size_t len;
1146
1147 if (!task)
1148 return -ESRCH;
Michal Hockof913da52016-07-28 15:44:37 -07001149 oom_score_adj = task->signal->oom_score_adj;
David Rientjesa63d83f2010-08-09 17:19:46 -07001150 put_task_struct(task);
David Rientjesa9c58b902012-12-11 16:02:54 -08001151 len = snprintf(buffer, sizeof(buffer), "%hd\n", oom_score_adj);
David Rientjesa63d83f2010-08-09 17:19:46 -07001152 return simple_read_from_buffer(buf, count, ppos, buffer, len);
1153}
1154
1155static ssize_t oom_score_adj_write(struct file *file, const char __user *buf,
1156 size_t count, loff_t *ppos)
1157{
David Rientjesa63d83f2010-08-09 17:19:46 -07001158 char buffer[PROC_NUMBUF];
Alexey Dobriyan0a8cb8e2011-05-26 16:25:50 -07001159 int oom_score_adj;
David Rientjesa63d83f2010-08-09 17:19:46 -07001160 int err;
1161
1162 memset(buffer, 0, sizeof(buffer));
1163 if (count > sizeof(buffer) - 1)
1164 count = sizeof(buffer) - 1;
David Rientjes723548b2010-10-26 14:21:25 -07001165 if (copy_from_user(buffer, buf, count)) {
1166 err = -EFAULT;
1167 goto out;
1168 }
David Rientjesa63d83f2010-08-09 17:19:46 -07001169
Alexey Dobriyan0a8cb8e2011-05-26 16:25:50 -07001170 err = kstrtoint(strstrip(buffer), 0, &oom_score_adj);
David Rientjesa63d83f2010-08-09 17:19:46 -07001171 if (err)
David Rientjes723548b2010-10-26 14:21:25 -07001172 goto out;
David Rientjesa63d83f2010-08-09 17:19:46 -07001173 if (oom_score_adj < OOM_SCORE_ADJ_MIN ||
David Rientjes723548b2010-10-26 14:21:25 -07001174 oom_score_adj > OOM_SCORE_ADJ_MAX) {
1175 err = -EINVAL;
1176 goto out;
1177 }
David Rientjesa63d83f2010-08-09 17:19:46 -07001178
Michal Hocko1d5f0ac2016-07-28 15:44:40 -07001179 err = __set_oom_adj(file, oom_score_adj, false);
David Rientjes723548b2010-10-26 14:21:25 -07001180out:
1181 return err < 0 ? err : count;
David Rientjesa63d83f2010-08-09 17:19:46 -07001182}
1183
1184static const struct file_operations proc_oom_score_adj_operations = {
1185 .read = oom_score_adj_read,
1186 .write = oom_score_adj_write,
Arnd Bergmann6038f372010-08-15 18:52:59 +02001187 .llseek = default_llseek,
David Rientjesa63d83f2010-08-09 17:19:46 -07001188};
1189
Linus Torvalds1da177e2005-04-16 15:20:36 -07001190#ifdef CONFIG_AUDITSYSCALL
Alexey Dobriyanb4eb4f72016-10-29 19:04:39 +03001191#define TMPBUFLEN 11
Linus Torvalds1da177e2005-04-16 15:20:36 -07001192static ssize_t proc_loginuid_read(struct file * file, char __user * buf,
1193 size_t count, loff_t *ppos)
1194{
Al Viro496ad9a2013-01-23 17:07:38 -05001195 struct inode * inode = file_inode(file);
Eric W. Biederman99f89552006-06-26 00:25:55 -07001196 struct task_struct *task = get_proc_task(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001197 ssize_t length;
1198 char tmpbuf[TMPBUFLEN];
1199
Eric W. Biederman99f89552006-06-26 00:25:55 -07001200 if (!task)
1201 return -ESRCH;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001202 length = scnprintf(tmpbuf, TMPBUFLEN, "%u",
Eric W. Biedermane1760bd2012-09-10 22:39:43 -07001203 from_kuid(file->f_cred->user_ns,
1204 audit_get_loginuid(task)));
Eric W. Biederman99f89552006-06-26 00:25:55 -07001205 put_task_struct(task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001206 return simple_read_from_buffer(buf, count, ppos, tmpbuf, length);
1207}
1208
1209static ssize_t proc_loginuid_write(struct file * file, const char __user * buf,
1210 size_t count, loff_t *ppos)
1211{
Al Viro496ad9a2013-01-23 17:07:38 -05001212 struct inode * inode = file_inode(file);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001213 uid_t loginuid;
Eric W. Biedermane1760bd2012-09-10 22:39:43 -07001214 kuid_t kloginuid;
Alexey Dobriyan774636e2015-09-09 15:36:59 -07001215 int rv;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001216
Paul E. McKenney7dc52152010-02-22 17:04:52 -08001217 rcu_read_lock();
1218 if (current != pid_task(proc_pid(inode), PIDTYPE_PID)) {
1219 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001220 return -EPERM;
Paul E. McKenney7dc52152010-02-22 17:04:52 -08001221 }
1222 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001223
Linus Torvalds1da177e2005-04-16 15:20:36 -07001224 if (*ppos != 0) {
1225 /* No partial writes. */
1226 return -EINVAL;
1227 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001228
Alexey Dobriyan774636e2015-09-09 15:36:59 -07001229 rv = kstrtou32_from_user(buf, count, 10, &loginuid);
1230 if (rv < 0)
1231 return rv;
Eric Paris81407c82013-05-24 09:49:14 -04001232
1233 /* is userspace tring to explicitly UNSET the loginuid? */
1234 if (loginuid == AUDIT_UID_UNSET) {
1235 kloginuid = INVALID_UID;
1236 } else {
1237 kloginuid = make_kuid(file->f_cred->user_ns, loginuid);
Alexey Dobriyan774636e2015-09-09 15:36:59 -07001238 if (!uid_valid(kloginuid))
1239 return -EINVAL;
Eric W. Biedermane1760bd2012-09-10 22:39:43 -07001240 }
1241
Alexey Dobriyan774636e2015-09-09 15:36:59 -07001242 rv = audit_set_loginuid(kloginuid);
1243 if (rv < 0)
1244 return rv;
1245 return count;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001246}
1247
Arjan van de Ven00977a52007-02-12 00:55:34 -08001248static const struct file_operations proc_loginuid_operations = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001249 .read = proc_loginuid_read,
1250 .write = proc_loginuid_write,
Arnd Bergmann87df8422010-03-17 23:06:02 +01001251 .llseek = generic_file_llseek,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001252};
Eric Paris1e0bd752008-03-13 08:15:31 -04001253
1254static ssize_t proc_sessionid_read(struct file * file, char __user * buf,
1255 size_t count, loff_t *ppos)
1256{
Al Viro496ad9a2013-01-23 17:07:38 -05001257 struct inode * inode = file_inode(file);
Eric Paris1e0bd752008-03-13 08:15:31 -04001258 struct task_struct *task = get_proc_task(inode);
1259 ssize_t length;
1260 char tmpbuf[TMPBUFLEN];
1261
1262 if (!task)
1263 return -ESRCH;
1264 length = scnprintf(tmpbuf, TMPBUFLEN, "%u",
1265 audit_get_sessionid(task));
1266 put_task_struct(task);
1267 return simple_read_from_buffer(buf, count, ppos, tmpbuf, length);
1268}
1269
1270static const struct file_operations proc_sessionid_operations = {
1271 .read = proc_sessionid_read,
Arnd Bergmann87df8422010-03-17 23:06:02 +01001272 .llseek = generic_file_llseek,
Eric Paris1e0bd752008-03-13 08:15:31 -04001273};
Linus Torvalds1da177e2005-04-16 15:20:36 -07001274#endif
1275
Akinobu Mitaf4f154f2006-12-08 02:39:47 -08001276#ifdef CONFIG_FAULT_INJECTION
1277static ssize_t proc_fault_inject_read(struct file * file, char __user * buf,
1278 size_t count, loff_t *ppos)
1279{
Al Viro496ad9a2013-01-23 17:07:38 -05001280 struct task_struct *task = get_proc_task(file_inode(file));
Akinobu Mitaf4f154f2006-12-08 02:39:47 -08001281 char buffer[PROC_NUMBUF];
1282 size_t len;
1283 int make_it_fail;
Akinobu Mitaf4f154f2006-12-08 02:39:47 -08001284
1285 if (!task)
1286 return -ESRCH;
1287 make_it_fail = task->make_it_fail;
1288 put_task_struct(task);
1289
1290 len = snprintf(buffer, sizeof(buffer), "%i\n", make_it_fail);
Akinobu Mita0c28f282007-05-08 00:31:41 -07001291
1292 return simple_read_from_buffer(buf, count, ppos, buffer, len);
Akinobu Mitaf4f154f2006-12-08 02:39:47 -08001293}
1294
1295static ssize_t proc_fault_inject_write(struct file * file,
1296 const char __user * buf, size_t count, loff_t *ppos)
1297{
1298 struct task_struct *task;
Alexey Dobriyan774636e2015-09-09 15:36:59 -07001299 char buffer[PROC_NUMBUF];
Akinobu Mitaf4f154f2006-12-08 02:39:47 -08001300 int make_it_fail;
Alexey Dobriyan774636e2015-09-09 15:36:59 -07001301 int rv;
Akinobu Mitaf4f154f2006-12-08 02:39:47 -08001302
1303 if (!capable(CAP_SYS_RESOURCE))
1304 return -EPERM;
1305 memset(buffer, 0, sizeof(buffer));
1306 if (count > sizeof(buffer) - 1)
1307 count = sizeof(buffer) - 1;
1308 if (copy_from_user(buffer, buf, count))
1309 return -EFAULT;
Alexey Dobriyan774636e2015-09-09 15:36:59 -07001310 rv = kstrtoint(strstrip(buffer), 0, &make_it_fail);
1311 if (rv < 0)
1312 return rv;
Dave Jones16caed32014-04-07 15:39:15 -07001313 if (make_it_fail < 0 || make_it_fail > 1)
1314 return -EINVAL;
1315
Al Viro496ad9a2013-01-23 17:07:38 -05001316 task = get_proc_task(file_inode(file));
Akinobu Mitaf4f154f2006-12-08 02:39:47 -08001317 if (!task)
1318 return -ESRCH;
1319 task->make_it_fail = make_it_fail;
1320 put_task_struct(task);
Vincent Licba8aaf2009-09-22 16:45:38 -07001321
1322 return count;
Akinobu Mitaf4f154f2006-12-08 02:39:47 -08001323}
1324
Arjan van de Ven00977a52007-02-12 00:55:34 -08001325static const struct file_operations proc_fault_inject_operations = {
Akinobu Mitaf4f154f2006-12-08 02:39:47 -08001326 .read = proc_fault_inject_read,
1327 .write = proc_fault_inject_write,
Arnd Bergmann87df8422010-03-17 23:06:02 +01001328 .llseek = generic_file_llseek,
Akinobu Mitaf4f154f2006-12-08 02:39:47 -08001329};
Dmitry Vyukove41d58182017-07-12 14:34:35 -07001330
1331static ssize_t proc_fail_nth_write(struct file *file, const char __user *buf,
1332 size_t count, loff_t *ppos)
1333{
1334 struct task_struct *task;
Akinobu Mita9049f2f2017-07-14 14:49:52 -07001335 int err;
1336 unsigned int n;
Dmitry Vyukove41d58182017-07-12 14:34:35 -07001337
Akinobu Mita9049f2f2017-07-14 14:49:52 -07001338 err = kstrtouint_from_user(buf, count, 0, &n);
Dmitry Vyukove41d58182017-07-12 14:34:35 -07001339 if (err)
1340 return err;
Akinobu Mita1203c8e2017-07-14 14:49:57 -07001341
1342 task = get_proc_task(file_inode(file));
1343 if (!task)
1344 return -ESRCH;
Alexey Dobriyan9f7118b2018-02-06 15:36:55 -08001345 task->fail_nth = n;
Akinobu Mita1203c8e2017-07-14 14:49:57 -07001346 put_task_struct(task);
1347
Dmitry Vyukove41d58182017-07-12 14:34:35 -07001348 return count;
1349}
1350
1351static ssize_t proc_fail_nth_read(struct file *file, char __user *buf,
1352 size_t count, loff_t *ppos)
1353{
1354 struct task_struct *task;
Akinobu Mitabfc74092017-07-14 14:49:54 -07001355 char numbuf[PROC_NUMBUF];
1356 ssize_t len;
Dmitry Vyukove41d58182017-07-12 14:34:35 -07001357
1358 task = get_proc_task(file_inode(file));
1359 if (!task)
1360 return -ESRCH;
Alexey Dobriyan9f7118b2018-02-06 15:36:55 -08001361 len = snprintf(numbuf, sizeof(numbuf), "%u\n", task->fail_nth);
Akinobu Mitabfc74092017-07-14 14:49:54 -07001362 len = simple_read_from_buffer(buf, count, ppos, numbuf, len);
Akinobu Mita1203c8e2017-07-14 14:49:57 -07001363 put_task_struct(task);
Akinobu Mitabfc74092017-07-14 14:49:54 -07001364
1365 return len;
Dmitry Vyukove41d58182017-07-12 14:34:35 -07001366}
1367
1368static const struct file_operations proc_fail_nth_operations = {
1369 .read = proc_fail_nth_read,
1370 .write = proc_fail_nth_write,
1371};
Akinobu Mitaf4f154f2006-12-08 02:39:47 -08001372#endif
1373
Arjan van de Ven97455122008-01-25 21:08:34 +01001374
Ingo Molnar43ae34c2007-07-09 18:52:00 +02001375#ifdef CONFIG_SCHED_DEBUG
1376/*
1377 * Print out various scheduling related per-task fields:
1378 */
1379static int sched_show(struct seq_file *m, void *v)
1380{
1381 struct inode *inode = m->private;
Christoph Hellwig76f668b2018-05-16 07:19:01 +02001382 struct pid_namespace *ns = proc_pid_ns(inode);
Ingo Molnar43ae34c2007-07-09 18:52:00 +02001383 struct task_struct *p;
1384
Ingo Molnar43ae34c2007-07-09 18:52:00 +02001385 p = get_proc_task(inode);
1386 if (!p)
1387 return -ESRCH;
Aleksa Sarai74dc3382017-08-06 14:41:41 +10001388 proc_sched_show_task(p, ns, m);
Ingo Molnar43ae34c2007-07-09 18:52:00 +02001389
1390 put_task_struct(p);
1391
1392 return 0;
1393}
1394
1395static ssize_t
1396sched_write(struct file *file, const char __user *buf,
1397 size_t count, loff_t *offset)
1398{
Al Viro496ad9a2013-01-23 17:07:38 -05001399 struct inode *inode = file_inode(file);
Ingo Molnar43ae34c2007-07-09 18:52:00 +02001400 struct task_struct *p;
1401
Ingo Molnar43ae34c2007-07-09 18:52:00 +02001402 p = get_proc_task(inode);
1403 if (!p)
1404 return -ESRCH;
1405 proc_sched_set_task(p);
1406
1407 put_task_struct(p);
1408
1409 return count;
1410}
1411
1412static int sched_open(struct inode *inode, struct file *filp)
1413{
Jovi Zhangc6a34052011-01-12 17:00:34 -08001414 return single_open(filp, sched_show, inode);
Ingo Molnar43ae34c2007-07-09 18:52:00 +02001415}
1416
1417static const struct file_operations proc_pid_sched_operations = {
1418 .open = sched_open,
1419 .read = seq_read,
1420 .write = sched_write,
1421 .llseek = seq_lseek,
Alexey Dobriyan5ea473a2007-07-31 00:38:50 -07001422 .release = single_release,
Ingo Molnar43ae34c2007-07-09 18:52:00 +02001423};
1424
1425#endif
1426
Mike Galbraith5091faa2010-11-30 14:18:03 +01001427#ifdef CONFIG_SCHED_AUTOGROUP
1428/*
1429 * Print out autogroup related information:
1430 */
1431static int sched_autogroup_show(struct seq_file *m, void *v)
1432{
1433 struct inode *inode = m->private;
1434 struct task_struct *p;
1435
1436 p = get_proc_task(inode);
1437 if (!p)
1438 return -ESRCH;
1439 proc_sched_autogroup_show_task(p, m);
1440
1441 put_task_struct(p);
1442
1443 return 0;
1444}
1445
1446static ssize_t
1447sched_autogroup_write(struct file *file, const char __user *buf,
1448 size_t count, loff_t *offset)
1449{
Al Viro496ad9a2013-01-23 17:07:38 -05001450 struct inode *inode = file_inode(file);
Mike Galbraith5091faa2010-11-30 14:18:03 +01001451 struct task_struct *p;
1452 char buffer[PROC_NUMBUF];
Alexey Dobriyan0a8cb8e2011-05-26 16:25:50 -07001453 int nice;
Mike Galbraith5091faa2010-11-30 14:18:03 +01001454 int err;
1455
1456 memset(buffer, 0, sizeof(buffer));
1457 if (count > sizeof(buffer) - 1)
1458 count = sizeof(buffer) - 1;
1459 if (copy_from_user(buffer, buf, count))
1460 return -EFAULT;
1461
Alexey Dobriyan0a8cb8e2011-05-26 16:25:50 -07001462 err = kstrtoint(strstrip(buffer), 0, &nice);
1463 if (err < 0)
1464 return err;
Mike Galbraith5091faa2010-11-30 14:18:03 +01001465
1466 p = get_proc_task(inode);
1467 if (!p)
1468 return -ESRCH;
1469
Hiroshi Shimamoto2e5b5b32012-02-23 17:41:27 +09001470 err = proc_sched_autogroup_set_nice(p, nice);
Mike Galbraith5091faa2010-11-30 14:18:03 +01001471 if (err)
1472 count = err;
1473
1474 put_task_struct(p);
1475
1476 return count;
1477}
1478
1479static int sched_autogroup_open(struct inode *inode, struct file *filp)
1480{
1481 int ret;
1482
1483 ret = single_open(filp, sched_autogroup_show, NULL);
1484 if (!ret) {
1485 struct seq_file *m = filp->private_data;
1486
1487 m->private = inode;
1488 }
1489 return ret;
1490}
1491
1492static const struct file_operations proc_pid_sched_autogroup_operations = {
1493 .open = sched_autogroup_open,
1494 .read = seq_read,
1495 .write = sched_autogroup_write,
1496 .llseek = seq_lseek,
1497 .release = single_release,
1498};
1499
1500#endif /* CONFIG_SCHED_AUTOGROUP */
1501
john stultz4614a696b2009-12-14 18:00:05 -08001502static ssize_t comm_write(struct file *file, const char __user *buf,
1503 size_t count, loff_t *offset)
1504{
Al Viro496ad9a2013-01-23 17:07:38 -05001505 struct inode *inode = file_inode(file);
john stultz4614a696b2009-12-14 18:00:05 -08001506 struct task_struct *p;
1507 char buffer[TASK_COMM_LEN];
David Rientjes830e0fc2013-04-30 15:28:18 -07001508 const size_t maxlen = sizeof(buffer) - 1;
john stultz4614a696b2009-12-14 18:00:05 -08001509
1510 memset(buffer, 0, sizeof(buffer));
David Rientjes830e0fc2013-04-30 15:28:18 -07001511 if (copy_from_user(buffer, buf, count > maxlen ? maxlen : count))
john stultz4614a696b2009-12-14 18:00:05 -08001512 return -EFAULT;
1513
1514 p = get_proc_task(inode);
1515 if (!p)
1516 return -ESRCH;
1517
1518 if (same_thread_group(current, p))
1519 set_task_comm(p, buffer);
1520 else
1521 count = -EINVAL;
1522
1523 put_task_struct(p);
1524
1525 return count;
1526}
1527
1528static int comm_show(struct seq_file *m, void *v)
1529{
1530 struct inode *inode = m->private;
1531 struct task_struct *p;
1532
1533 p = get_proc_task(inode);
1534 if (!p)
1535 return -ESRCH;
1536
Tejun Heo88b72b32018-05-18 08:47:13 -07001537 proc_task_name(m, p, false);
1538 seq_putc(m, '\n');
john stultz4614a696b2009-12-14 18:00:05 -08001539
1540 put_task_struct(p);
1541
1542 return 0;
1543}
1544
1545static int comm_open(struct inode *inode, struct file *filp)
1546{
Jovi Zhangc6a34052011-01-12 17:00:34 -08001547 return single_open(filp, comm_show, inode);
john stultz4614a696b2009-12-14 18:00:05 -08001548}
1549
1550static const struct file_operations proc_pid_set_comm_operations = {
1551 .open = comm_open,
1552 .read = seq_read,
1553 .write = comm_write,
1554 .llseek = seq_lseek,
1555 .release = single_release,
1556};
1557
Cyrill Gorcunov7773fbc2012-01-10 15:11:20 -08001558static int proc_exe_link(struct dentry *dentry, struct path *exe_path)
Matt Helsley925d1c42008-04-29 01:01:36 -07001559{
1560 struct task_struct *task;
Matt Helsley925d1c42008-04-29 01:01:36 -07001561 struct file *exe_file;
1562
David Howells2b0143b2015-03-17 22:25:59 +00001563 task = get_proc_task(d_inode(dentry));
Matt Helsley925d1c42008-04-29 01:01:36 -07001564 if (!task)
1565 return -ENOENT;
Mateusz Guzikcd81a9172016-08-23 16:20:38 +02001566 exe_file = get_task_exe_file(task);
Matt Helsley925d1c42008-04-29 01:01:36 -07001567 put_task_struct(task);
Matt Helsley925d1c42008-04-29 01:01:36 -07001568 if (exe_file) {
1569 *exe_path = exe_file->f_path;
1570 path_get(&exe_file->f_path);
1571 fput(exe_file);
1572 return 0;
1573 } else
1574 return -ENOENT;
1575}
1576
Al Viro6b255392015-11-17 10:20:54 -05001577static const char *proc_pid_get_link(struct dentry *dentry,
Al Virofceef392015-12-29 15:58:39 -05001578 struct inode *inode,
1579 struct delayed_call *done)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001580{
Christoph Hellwig408ef012012-06-18 10:47:03 -04001581 struct path path;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001582 int error = -EACCES;
1583
Al Viro6b255392015-11-17 10:20:54 -05001584 if (!dentry)
1585 return ERR_PTR(-ECHILD);
1586
Eric W. Biederman778c1142006-06-26 00:25:58 -07001587 /* Are we allowed to snoop on the tasks file descriptors? */
1588 if (!proc_fd_access_allowed(inode))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001589 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001590
Christoph Hellwig408ef012012-06-18 10:47:03 -04001591 error = PROC_I(inode)->op.proc_get_link(dentry, &path);
1592 if (error)
1593 goto out;
1594
Al Viro6e77137b2015-05-02 13:37:52 -04001595 nd_jump_link(&path);
Christoph Hellwig408ef012012-06-18 10:47:03 -04001596 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001597out:
Al Viro008b1502005-08-20 00:17:39 +01001598 return ERR_PTR(error);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001599}
1600
Jan Blunck3dcd25f2008-02-14 19:38:35 -08001601static int do_proc_readlink(struct path *path, char __user *buffer, int buflen)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001602{
Michal Hocko0ee931c2017-09-13 16:28:29 -07001603 char *tmp = (char *)__get_free_page(GFP_KERNEL);
Jan Blunck3dcd25f2008-02-14 19:38:35 -08001604 char *pathname;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001605 int len;
1606
1607 if (!tmp)
1608 return -ENOMEM;
Akinobu Mita0c28f282007-05-08 00:31:41 -07001609
Eric W. Biederman7b2a69b2010-12-05 15:51:21 -08001610 pathname = d_path(path, tmp, PAGE_SIZE);
Jan Blunck3dcd25f2008-02-14 19:38:35 -08001611 len = PTR_ERR(pathname);
1612 if (IS_ERR(pathname))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001613 goto out;
Jan Blunck3dcd25f2008-02-14 19:38:35 -08001614 len = tmp + PAGE_SIZE - 1 - pathname;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001615
1616 if (len > buflen)
1617 len = buflen;
Jan Blunck3dcd25f2008-02-14 19:38:35 -08001618 if (copy_to_user(buffer, pathname, len))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001619 len = -EFAULT;
1620 out:
1621 free_page((unsigned long)tmp);
1622 return len;
1623}
1624
1625static int proc_pid_readlink(struct dentry * dentry, char __user * buffer, int buflen)
1626{
1627 int error = -EACCES;
David Howells2b0143b2015-03-17 22:25:59 +00001628 struct inode *inode = d_inode(dentry);
Jan Blunck3dcd25f2008-02-14 19:38:35 -08001629 struct path path;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001630
Eric W. Biederman778c1142006-06-26 00:25:58 -07001631 /* Are we allowed to snoop on the tasks file descriptors? */
1632 if (!proc_fd_access_allowed(inode))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001633 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001634
Cyrill Gorcunov7773fbc2012-01-10 15:11:20 -08001635 error = PROC_I(inode)->op.proc_get_link(dentry, &path);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001636 if (error)
1637 goto out;
1638
Jan Blunck3dcd25f2008-02-14 19:38:35 -08001639 error = do_proc_readlink(&path, buffer, buflen);
1640 path_put(&path);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001641out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001642 return error;
1643}
1644
Cyrill Gorcunovfaf60af2012-08-23 14:43:24 +04001645const struct inode_operations proc_pid_link_inode_operations = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001646 .readlink = proc_pid_readlink,
Al Viro6b255392015-11-17 10:20:54 -05001647 .get_link = proc_pid_get_link,
Linus Torvalds6d76fa52006-07-15 12:26:45 -07001648 .setattr = proc_setattr,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001649};
1650
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001651
1652/* building an inode */
1653
Al Viroc6eb50d2017-09-30 14:45:42 -04001654void task_dump_owner(struct task_struct *task, umode_t mode,
Eric W. Biederman68eb94f2017-01-03 10:23:11 +13001655 kuid_t *ruid, kgid_t *rgid)
1656{
1657 /* Depending on the state of dumpable compute who should own a
1658 * proc file for a task.
1659 */
1660 const struct cred *cred;
1661 kuid_t uid;
1662 kgid_t gid;
1663
Alexey Dobriyan2e0ad552018-04-20 14:56:03 -07001664 if (unlikely(task->flags & PF_KTHREAD)) {
1665 *ruid = GLOBAL_ROOT_UID;
1666 *rgid = GLOBAL_ROOT_GID;
1667 return;
1668 }
1669
Eric W. Biederman68eb94f2017-01-03 10:23:11 +13001670 /* Default to the tasks effective ownership */
1671 rcu_read_lock();
1672 cred = __task_cred(task);
1673 uid = cred->euid;
1674 gid = cred->egid;
1675 rcu_read_unlock();
1676
1677 /*
1678 * Before the /proc/pid/status file was created the only way to read
1679 * the effective uid of a /process was to stat /proc/pid. Reading
1680 * /proc/pid/status is slow enough that procps and other packages
1681 * kept stating /proc/pid. To keep the rules in /proc simple I have
1682 * made this apply to all per process world readable and executable
1683 * directories.
1684 */
1685 if (mode != (S_IFDIR|S_IRUGO|S_IXUGO)) {
1686 struct mm_struct *mm;
1687 task_lock(task);
1688 mm = task->mm;
1689 /* Make non-dumpable tasks owned by some root */
1690 if (mm) {
1691 if (get_dumpable(mm) != SUID_DUMP_USER) {
1692 struct user_namespace *user_ns = mm->user_ns;
1693
1694 uid = make_kuid(user_ns, 0);
1695 if (!uid_valid(uid))
1696 uid = GLOBAL_ROOT_UID;
1697
1698 gid = make_kgid(user_ns, 0);
1699 if (!gid_valid(gid))
1700 gid = GLOBAL_ROOT_GID;
1701 }
1702 } else {
1703 uid = GLOBAL_ROOT_UID;
1704 gid = GLOBAL_ROOT_GID;
1705 }
1706 task_unlock(task);
1707 }
1708 *ruid = uid;
1709 *rgid = gid;
1710}
1711
Andreas Gruenbacherdb978da2016-11-10 22:18:28 +01001712struct inode *proc_pid_make_inode(struct super_block * sb,
1713 struct task_struct *task, umode_t mode)
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001714{
1715 struct inode * inode;
1716 struct proc_inode *ei;
1717
1718 /* We need a new inode */
1719
1720 inode = new_inode(sb);
1721 if (!inode)
1722 goto out;
1723
1724 /* Common stuff */
1725 ei = PROC_I(inode);
Andreas Gruenbacherdb978da2016-11-10 22:18:28 +01001726 inode->i_mode = mode;
Christoph Hellwig85fe4022010-10-23 11:19:54 -04001727 inode->i_ino = get_next_ino();
Deepa Dinamani078cd822016-09-14 07:48:04 -07001728 inode->i_mtime = inode->i_atime = inode->i_ctime = current_time(inode);
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001729 inode->i_op = &proc_def_inode_operations;
1730
1731 /*
1732 * grab the reference to task.
1733 */
Oleg Nesterov1a657f782006-10-02 02:18:59 -07001734 ei->pid = get_task_pid(task, PIDTYPE_PID);
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001735 if (!ei->pid)
1736 goto out_unlock;
1737
Eric W. Biederman68eb94f2017-01-03 10:23:11 +13001738 task_dump_owner(task, 0, &inode->i_uid, &inode->i_gid);
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001739 security_task_to_inode(task, inode);
1740
1741out:
1742 return inode;
1743
1744out_unlock:
1745 iput(inode);
1746 return NULL;
1747}
1748
David Howellsa528d352017-01-31 16:46:22 +00001749int pid_getattr(const struct path *path, struct kstat *stat,
1750 u32 request_mask, unsigned int query_flags)
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001751{
David Howellsa528d352017-01-31 16:46:22 +00001752 struct inode *inode = d_inode(path->dentry);
Christoph Hellwig76f668b2018-05-16 07:19:01 +02001753 struct pid_namespace *pid = proc_pid_ns(inode);
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001754 struct task_struct *task;
David Howellsc69e8d92008-11-14 10:39:19 +11001755
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001756 generic_fillattr(inode, stat);
1757
Eric W. Biedermandcb0f222012-02-09 08:48:21 -08001758 stat->uid = GLOBAL_ROOT_UID;
1759 stat->gid = GLOBAL_ROOT_GID;
Alexey Dobriyan94116922018-06-07 17:10:07 -07001760 rcu_read_lock();
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001761 task = pid_task(proc_pid(inode), PIDTYPE_PID);
1762 if (task) {
Lafcadio Wluiki796f5712017-02-24 15:00:23 -08001763 if (!has_pid_permissions(pid, task, HIDEPID_INVISIBLE)) {
Vasiliy Kulikov04996802012-01-10 15:11:31 -08001764 rcu_read_unlock();
1765 /*
1766 * This doesn't prevent learning whether PID exists,
1767 * it only makes getattr() consistent with readdir().
1768 */
1769 return -ENOENT;
1770 }
Eric W. Biederman68eb94f2017-01-03 10:23:11 +13001771 task_dump_owner(task, inode->i_mode, &stat->uid, &stat->gid);
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001772 }
1773 rcu_read_unlock();
1774 return 0;
1775}
1776
1777/* dentry stuff */
1778
1779/*
Al Viro1bbc5512018-05-02 21:26:16 -04001780 * Set <pid>/... inode ownership (can change due to setuid(), etc.)
1781 */
1782void pid_update_inode(struct task_struct *task, struct inode *inode)
1783{
1784 task_dump_owner(task, inode->i_mode, &inode->i_uid, &inode->i_gid);
1785
1786 inode->i_mode &= ~(S_ISUID | S_ISGID);
1787 security_task_to_inode(task, inode);
1788}
1789
1790/*
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001791 * Rewrite the inode's ownerships here because the owning task may have
1792 * performed a setuid(), etc.
1793 *
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001794 */
Al Viro1bbc5512018-05-02 21:26:16 -04001795static int pid_revalidate(struct dentry *dentry, unsigned int flags)
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001796{
Nick Piggin34286d62011-01-07 17:49:57 +11001797 struct inode *inode;
1798 struct task_struct *task;
David Howellsc69e8d92008-11-14 10:39:19 +11001799
Al Viro0b728e12012-06-10 16:03:43 -04001800 if (flags & LOOKUP_RCU)
Nick Piggin34286d62011-01-07 17:49:57 +11001801 return -ECHILD;
1802
David Howells2b0143b2015-03-17 22:25:59 +00001803 inode = d_inode(dentry);
Nick Piggin34286d62011-01-07 17:49:57 +11001804 task = get_proc_task(inode);
1805
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001806 if (task) {
Al Viro1bbc5512018-05-02 21:26:16 -04001807 pid_update_inode(task, inode);
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001808 put_task_struct(task);
1809 return 1;
1810 }
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001811 return 0;
1812}
1813
Oleg Nesterovd855a4b2014-01-23 15:55:39 -08001814static inline bool proc_inode_is_dead(struct inode *inode)
1815{
1816 return !proc_pid(inode)->tasks[PIDTYPE_PID].first;
1817}
1818
David Howells1dd704b2013-04-12 01:08:50 +01001819int pid_delete_dentry(const struct dentry *dentry)
1820{
1821 /* Is the task we represent dead?
1822 * If so, then don't put the dentry on the lru list,
1823 * kill it immediately.
1824 */
David Howells2b0143b2015-03-17 22:25:59 +00001825 return proc_inode_is_dead(d_inode(dentry));
David Howells1dd704b2013-04-12 01:08:50 +01001826}
1827
Eric W. Biederman6b4e3062010-03-07 16:41:34 -08001828const struct dentry_operations pid_dentry_operations =
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001829{
1830 .d_revalidate = pid_revalidate,
1831 .d_delete = pid_delete_dentry,
1832};
1833
1834/* Lookups */
1835
Eric W. Biederman1c0d04c2006-10-02 02:18:57 -07001836/*
1837 * Fill a directory entry.
1838 *
1839 * If possible create the dcache entry and derive our inode number and
1840 * file type from dcache entry.
1841 *
1842 * Since all of the proc inode numbers are dynamically generated, the inode
1843 * numbers do not exist until the inode is cache. This means creating the
1844 * the dcache entry in readdir is necessary to keep the inode numbers
1845 * reported by readdir in sync with the inode numbers reported
1846 * by stat.
1847 */
Al Virof0c3b502013-05-16 12:07:31 -04001848bool proc_fill_cache(struct file *file, struct dir_context *ctx,
Eric W. Biederman6b4e3062010-03-07 16:41:34 -08001849 const char *name, int len,
Eric Dumazetc5141e62007-05-08 00:26:15 -07001850 instantiate_t instantiate, struct task_struct *task, const void *ptr)
Eric W. Biederman61a28782006-10-02 02:18:49 -07001851{
Al Virof0c3b502013-05-16 12:07:31 -04001852 struct dentry *child, *dir = file->f_path.dentry;
Al Viro1df98b82013-06-15 11:33:10 +04001853 struct qstr qname = QSTR_INIT(name, len);
Eric W. Biederman61a28782006-10-02 02:18:49 -07001854 struct inode *inode;
Al Viro0168b9e2018-05-03 09:21:05 -04001855 unsigned type = DT_UNKNOWN;
1856 ino_t ino = 1;
Eric W. Biederman61a28782006-10-02 02:18:49 -07001857
Al Viro1df98b82013-06-15 11:33:10 +04001858 child = d_hash_and_lookup(dir, &qname);
Eric W. Biederman61a28782006-10-02 02:18:49 -07001859 if (!child) {
Al Viro37817642016-04-20 16:31:31 -04001860 DECLARE_WAIT_QUEUE_HEAD_ONSTACK(wq);
1861 child = d_alloc_parallel(dir, &qname, &wq);
1862 if (IS_ERR(child))
Al Viro1df98b82013-06-15 11:33:10 +04001863 goto end_instantiate;
Al Viro37817642016-04-20 16:31:31 -04001864 if (d_in_lookup(child)) {
Al Viro0168b9e2018-05-03 09:21:05 -04001865 struct dentry *res;
1866 res = instantiate(child, task, ptr);
Al Viro37817642016-04-20 16:31:31 -04001867 d_lookup_done(child);
Al Viro0168b9e2018-05-03 09:21:05 -04001868 if (IS_ERR(res))
Al Viro37817642016-04-20 16:31:31 -04001869 goto end_instantiate;
Al Viro0168b9e2018-05-03 09:21:05 -04001870 if (unlikely(res)) {
1871 dput(child);
1872 child = res;
Al Viro37817642016-04-20 16:31:31 -04001873 }
Eric W. Biederman61a28782006-10-02 02:18:49 -07001874 }
1875 }
David Howells2b0143b2015-03-17 22:25:59 +00001876 inode = d_inode(child);
Al Viro147ce692013-06-15 10:26:35 +04001877 ino = inode->i_ino;
1878 type = inode->i_mode >> 12;
Al Viro0168b9e2018-05-03 09:21:05 -04001879end_instantiate:
Eric W. Biederman61a28782006-10-02 02:18:49 -07001880 dput(child);
Al Virof0c3b502013-05-16 12:07:31 -04001881 return dir_emit(ctx, name, len, ino, type);
Eric W. Biederman61a28782006-10-02 02:18:49 -07001882}
1883
Pavel Emelyanov640708a2012-01-10 15:11:23 -08001884/*
1885 * dname_to_vma_addr - maps a dentry name into two unsigned longs
1886 * which represent vma start and end addresses.
1887 */
1888static int dname_to_vma_addr(struct dentry *dentry,
1889 unsigned long *start, unsigned long *end)
1890{
Alexey Dobriyanac7f1062018-02-06 15:36:59 -08001891 const char *str = dentry->d_name.name;
1892 unsigned long long sval, eval;
1893 unsigned int len;
1894
Alexey Dobriyan35318db2018-04-10 16:41:14 -07001895 if (str[0] == '0' && str[1] != '-')
1896 return -EINVAL;
Alexey Dobriyanac7f1062018-02-06 15:36:59 -08001897 len = _parse_integer(str, 16, &sval);
1898 if (len & KSTRTOX_OVERFLOW)
Pavel Emelyanov640708a2012-01-10 15:11:23 -08001899 return -EINVAL;
Alexey Dobriyanac7f1062018-02-06 15:36:59 -08001900 if (sval != (unsigned long)sval)
1901 return -EINVAL;
1902 str += len;
1903
1904 if (*str != '-')
1905 return -EINVAL;
1906 str++;
1907
Alexey Dobriyan35318db2018-04-10 16:41:14 -07001908 if (str[0] == '0' && str[1])
1909 return -EINVAL;
Alexey Dobriyanac7f1062018-02-06 15:36:59 -08001910 len = _parse_integer(str, 16, &eval);
1911 if (len & KSTRTOX_OVERFLOW)
1912 return -EINVAL;
1913 if (eval != (unsigned long)eval)
1914 return -EINVAL;
1915 str += len;
1916
1917 if (*str != '\0')
1918 return -EINVAL;
1919
1920 *start = sval;
1921 *end = eval;
Pavel Emelyanov640708a2012-01-10 15:11:23 -08001922
1923 return 0;
1924}
1925
Al Viro0b728e12012-06-10 16:03:43 -04001926static int map_files_d_revalidate(struct dentry *dentry, unsigned int flags)
Pavel Emelyanov640708a2012-01-10 15:11:23 -08001927{
1928 unsigned long vm_start, vm_end;
1929 bool exact_vma_exists = false;
1930 struct mm_struct *mm = NULL;
1931 struct task_struct *task;
Pavel Emelyanov640708a2012-01-10 15:11:23 -08001932 struct inode *inode;
1933 int status = 0;
1934
Al Viro0b728e12012-06-10 16:03:43 -04001935 if (flags & LOOKUP_RCU)
Pavel Emelyanov640708a2012-01-10 15:11:23 -08001936 return -ECHILD;
1937
David Howells2b0143b2015-03-17 22:25:59 +00001938 inode = d_inode(dentry);
Pavel Emelyanov640708a2012-01-10 15:11:23 -08001939 task = get_proc_task(inode);
1940 if (!task)
1941 goto out_notask;
1942
Jann Horncaaee622016-01-20 15:00:04 -08001943 mm = mm_access(task, PTRACE_MODE_READ_FSCREDS);
Cong Wang2344bec2012-05-31 16:26:18 -07001944 if (IS_ERR_OR_NULL(mm))
Pavel Emelyanov640708a2012-01-10 15:11:23 -08001945 goto out;
1946
1947 if (!dname_to_vma_addr(dentry, &vm_start, &vm_end)) {
1948 down_read(&mm->mmap_sem);
1949 exact_vma_exists = !!find_exact_vma(mm, vm_start, vm_end);
1950 up_read(&mm->mmap_sem);
1951 }
1952
1953 mmput(mm);
1954
1955 if (exact_vma_exists) {
Eric W. Biederman68eb94f2017-01-03 10:23:11 +13001956 task_dump_owner(task, 0, &inode->i_uid, &inode->i_gid);
1957
Pavel Emelyanov640708a2012-01-10 15:11:23 -08001958 security_task_to_inode(task, inode);
1959 status = 1;
1960 }
1961
1962out:
1963 put_task_struct(task);
1964
1965out_notask:
Pavel Emelyanov640708a2012-01-10 15:11:23 -08001966 return status;
1967}
1968
1969static const struct dentry_operations tid_map_files_dentry_operations = {
1970 .d_revalidate = map_files_d_revalidate,
1971 .d_delete = pid_delete_dentry,
1972};
1973
Al Viro6b255392015-11-17 10:20:54 -05001974static int map_files_get_link(struct dentry *dentry, struct path *path)
Pavel Emelyanov640708a2012-01-10 15:11:23 -08001975{
1976 unsigned long vm_start, vm_end;
1977 struct vm_area_struct *vma;
1978 struct task_struct *task;
1979 struct mm_struct *mm;
1980 int rc;
1981
1982 rc = -ENOENT;
David Howells2b0143b2015-03-17 22:25:59 +00001983 task = get_proc_task(d_inode(dentry));
Pavel Emelyanov640708a2012-01-10 15:11:23 -08001984 if (!task)
1985 goto out;
1986
1987 mm = get_task_mm(task);
1988 put_task_struct(task);
1989 if (!mm)
1990 goto out;
1991
1992 rc = dname_to_vma_addr(dentry, &vm_start, &vm_end);
1993 if (rc)
1994 goto out_mmput;
1995
Artem Fetishev70335ab2014-03-10 15:49:45 -07001996 rc = -ENOENT;
Pavel Emelyanov640708a2012-01-10 15:11:23 -08001997 down_read(&mm->mmap_sem);
1998 vma = find_exact_vma(mm, vm_start, vm_end);
1999 if (vma && vma->vm_file) {
2000 *path = vma->vm_file->f_path;
2001 path_get(path);
2002 rc = 0;
2003 }
2004 up_read(&mm->mmap_sem);
2005
2006out_mmput:
2007 mmput(mm);
2008out:
2009 return rc;
2010}
2011
2012struct map_files_info {
Alexey Dobriyan20d28cd2018-02-06 15:37:06 -08002013 unsigned long start;
2014 unsigned long end;
Al Viro7b540d02012-08-27 14:55:26 -04002015 fmode_t mode;
Pavel Emelyanov640708a2012-01-10 15:11:23 -08002016};
2017
Calvin Owensbdb4d102015-09-09 15:35:54 -07002018/*
2019 * Only allow CAP_SYS_ADMIN to follow the links, due to concerns about how the
2020 * symlinks may be used to bypass permissions on ancestor directories in the
2021 * path to the file in question.
2022 */
2023static const char *
Al Viro6b255392015-11-17 10:20:54 -05002024proc_map_files_get_link(struct dentry *dentry,
Al Virofceef392015-12-29 15:58:39 -05002025 struct inode *inode,
2026 struct delayed_call *done)
Calvin Owensbdb4d102015-09-09 15:35:54 -07002027{
2028 if (!capable(CAP_SYS_ADMIN))
2029 return ERR_PTR(-EPERM);
2030
Al Virofceef392015-12-29 15:58:39 -05002031 return proc_pid_get_link(dentry, inode, done);
Calvin Owensbdb4d102015-09-09 15:35:54 -07002032}
2033
2034/*
Al Viro6b255392015-11-17 10:20:54 -05002035 * Identical to proc_pid_link_inode_operations except for get_link()
Calvin Owensbdb4d102015-09-09 15:35:54 -07002036 */
2037static const struct inode_operations proc_map_files_link_inode_operations = {
2038 .readlink = proc_pid_readlink,
Al Viro6b255392015-11-17 10:20:54 -05002039 .get_link = proc_map_files_get_link,
Calvin Owensbdb4d102015-09-09 15:35:54 -07002040 .setattr = proc_setattr,
2041};
2042
Al Viro0168b9e2018-05-03 09:21:05 -04002043static struct dentry *
2044proc_map_files_instantiate(struct dentry *dentry,
Pavel Emelyanov640708a2012-01-10 15:11:23 -08002045 struct task_struct *task, const void *ptr)
2046{
Al Viro7b540d02012-08-27 14:55:26 -04002047 fmode_t mode = (fmode_t)(unsigned long)ptr;
Pavel Emelyanov640708a2012-01-10 15:11:23 -08002048 struct proc_inode *ei;
2049 struct inode *inode;
2050
Al Viro0168b9e2018-05-03 09:21:05 -04002051 inode = proc_pid_make_inode(dentry->d_sb, task, S_IFLNK |
Andreas Gruenbacherdb978da2016-11-10 22:18:28 +01002052 ((mode & FMODE_READ ) ? S_IRUSR : 0) |
2053 ((mode & FMODE_WRITE) ? S_IWUSR : 0));
Pavel Emelyanov640708a2012-01-10 15:11:23 -08002054 if (!inode)
Al Viro0168b9e2018-05-03 09:21:05 -04002055 return ERR_PTR(-ENOENT);
Pavel Emelyanov640708a2012-01-10 15:11:23 -08002056
2057 ei = PROC_I(inode);
Al Viro6b255392015-11-17 10:20:54 -05002058 ei->op.proc_get_link = map_files_get_link;
Pavel Emelyanov640708a2012-01-10 15:11:23 -08002059
Calvin Owensbdb4d102015-09-09 15:35:54 -07002060 inode->i_op = &proc_map_files_link_inode_operations;
Pavel Emelyanov640708a2012-01-10 15:11:23 -08002061 inode->i_size = 64;
Pavel Emelyanov640708a2012-01-10 15:11:23 -08002062
2063 d_set_d_op(dentry, &tid_map_files_dentry_operations);
Al Viro0168b9e2018-05-03 09:21:05 -04002064 return d_splice_alias(inode, dentry);
Pavel Emelyanov640708a2012-01-10 15:11:23 -08002065}
2066
2067static struct dentry *proc_map_files_lookup(struct inode *dir,
Al Viro00cd8dd2012-06-10 17:13:09 -04002068 struct dentry *dentry, unsigned int flags)
Pavel Emelyanov640708a2012-01-10 15:11:23 -08002069{
2070 unsigned long vm_start, vm_end;
2071 struct vm_area_struct *vma;
2072 struct task_struct *task;
Al Viro0168b9e2018-05-03 09:21:05 -04002073 struct dentry *result;
Pavel Emelyanov640708a2012-01-10 15:11:23 -08002074 struct mm_struct *mm;
2075
Al Viro0168b9e2018-05-03 09:21:05 -04002076 result = ERR_PTR(-ENOENT);
Pavel Emelyanov640708a2012-01-10 15:11:23 -08002077 task = get_proc_task(dir);
2078 if (!task)
2079 goto out;
2080
Al Viro0168b9e2018-05-03 09:21:05 -04002081 result = ERR_PTR(-EACCES);
Jann Horncaaee622016-01-20 15:00:04 -08002082 if (!ptrace_may_access(task, PTRACE_MODE_READ_FSCREDS))
Pavel Emelyanov640708a2012-01-10 15:11:23 -08002083 goto out_put_task;
2084
Al Viro0168b9e2018-05-03 09:21:05 -04002085 result = ERR_PTR(-ENOENT);
Pavel Emelyanov640708a2012-01-10 15:11:23 -08002086 if (dname_to_vma_addr(dentry, &vm_start, &vm_end))
Cyrill Gorcunoveb94cd92012-05-17 17:03:25 -07002087 goto out_put_task;
Pavel Emelyanov640708a2012-01-10 15:11:23 -08002088
2089 mm = get_task_mm(task);
2090 if (!mm)
Cyrill Gorcunoveb94cd92012-05-17 17:03:25 -07002091 goto out_put_task;
Pavel Emelyanov640708a2012-01-10 15:11:23 -08002092
2093 down_read(&mm->mmap_sem);
2094 vma = find_exact_vma(mm, vm_start, vm_end);
2095 if (!vma)
2096 goto out_no_vma;
2097
Stanislav Kinsbursky05f56482012-11-26 16:29:42 -08002098 if (vma->vm_file)
Al Viro0168b9e2018-05-03 09:21:05 -04002099 result = proc_map_files_instantiate(dentry, task,
Stanislav Kinsbursky05f56482012-11-26 16:29:42 -08002100 (void *)(unsigned long)vma->vm_file->f_mode);
Pavel Emelyanov640708a2012-01-10 15:11:23 -08002101
2102out_no_vma:
2103 up_read(&mm->mmap_sem);
2104 mmput(mm);
Pavel Emelyanov640708a2012-01-10 15:11:23 -08002105out_put_task:
2106 put_task_struct(task);
2107out:
Al Viro0168b9e2018-05-03 09:21:05 -04002108 return result;
Pavel Emelyanov640708a2012-01-10 15:11:23 -08002109}
2110
2111static const struct inode_operations proc_map_files_inode_operations = {
2112 .lookup = proc_map_files_lookup,
2113 .permission = proc_fd_permission,
2114 .setattr = proc_setattr,
2115};
2116
2117static int
Al Virof0c3b502013-05-16 12:07:31 -04002118proc_map_files_readdir(struct file *file, struct dir_context *ctx)
Pavel Emelyanov640708a2012-01-10 15:11:23 -08002119{
Pavel Emelyanov640708a2012-01-10 15:11:23 -08002120 struct vm_area_struct *vma;
2121 struct task_struct *task;
2122 struct mm_struct *mm;
Al Virof0c3b502013-05-16 12:07:31 -04002123 unsigned long nr_files, pos, i;
2124 struct flex_array *fa = NULL;
2125 struct map_files_info info;
2126 struct map_files_info *p;
Pavel Emelyanov640708a2012-01-10 15:11:23 -08002127 int ret;
2128
Pavel Emelyanov640708a2012-01-10 15:11:23 -08002129 ret = -ENOENT;
Al Virof0c3b502013-05-16 12:07:31 -04002130 task = get_proc_task(file_inode(file));
Pavel Emelyanov640708a2012-01-10 15:11:23 -08002131 if (!task)
2132 goto out;
2133
2134 ret = -EACCES;
Jann Horncaaee622016-01-20 15:00:04 -08002135 if (!ptrace_may_access(task, PTRACE_MODE_READ_FSCREDS))
Pavel Emelyanov640708a2012-01-10 15:11:23 -08002136 goto out_put_task;
2137
2138 ret = 0;
Al Virof0c3b502013-05-16 12:07:31 -04002139 if (!dir_emit_dots(file, ctx))
2140 goto out_put_task;
Pavel Emelyanov640708a2012-01-10 15:11:23 -08002141
Al Virof0c3b502013-05-16 12:07:31 -04002142 mm = get_task_mm(task);
2143 if (!mm)
2144 goto out_put_task;
2145 down_read(&mm->mmap_sem);
Pavel Emelyanov640708a2012-01-10 15:11:23 -08002146
Al Virof0c3b502013-05-16 12:07:31 -04002147 nr_files = 0;
Pavel Emelyanov640708a2012-01-10 15:11:23 -08002148
Al Virof0c3b502013-05-16 12:07:31 -04002149 /*
2150 * We need two passes here:
2151 *
2152 * 1) Collect vmas of mapped files with mmap_sem taken
2153 * 2) Release mmap_sem and instantiate entries
2154 *
2155 * otherwise we get lockdep complained, since filldir()
2156 * routine might require mmap_sem taken in might_fault().
2157 */
Pavel Emelyanov640708a2012-01-10 15:11:23 -08002158
Al Virof0c3b502013-05-16 12:07:31 -04002159 for (vma = mm->mmap, pos = 2; vma; vma = vma->vm_next) {
2160 if (vma->vm_file && ++pos > ctx->pos)
2161 nr_files++;
Pavel Emelyanov640708a2012-01-10 15:11:23 -08002162 }
Al Virof0c3b502013-05-16 12:07:31 -04002163
2164 if (nr_files) {
2165 fa = flex_array_alloc(sizeof(info), nr_files,
2166 GFP_KERNEL);
2167 if (!fa || flex_array_prealloc(fa, 0, nr_files,
2168 GFP_KERNEL)) {
2169 ret = -ENOMEM;
2170 if (fa)
2171 flex_array_free(fa);
2172 up_read(&mm->mmap_sem);
2173 mmput(mm);
2174 goto out_put_task;
2175 }
2176 for (i = 0, vma = mm->mmap, pos = 2; vma;
2177 vma = vma->vm_next) {
2178 if (!vma->vm_file)
2179 continue;
2180 if (++pos <= ctx->pos)
2181 continue;
2182
Alexey Dobriyan20d28cd2018-02-06 15:37:06 -08002183 info.start = vma->vm_start;
2184 info.end = vma->vm_end;
Al Virof0c3b502013-05-16 12:07:31 -04002185 info.mode = vma->vm_file->f_mode;
Al Virof0c3b502013-05-16 12:07:31 -04002186 if (flex_array_put(fa, i++, &info, GFP_KERNEL))
2187 BUG();
2188 }
Pavel Emelyanov640708a2012-01-10 15:11:23 -08002189 }
Al Virof0c3b502013-05-16 12:07:31 -04002190 up_read(&mm->mmap_sem);
Alexey Dobriyanfe079a52018-04-10 16:32:05 -07002191 mmput(mm);
Al Virof0c3b502013-05-16 12:07:31 -04002192
2193 for (i = 0; i < nr_files; i++) {
Alexey Dobriyan20d28cd2018-02-06 15:37:06 -08002194 char buf[4 * sizeof(long) + 2]; /* max: %lx-%lx\0 */
2195 unsigned int len;
2196
Al Virof0c3b502013-05-16 12:07:31 -04002197 p = flex_array_get(fa, i);
Alexey Dobriyan20d28cd2018-02-06 15:37:06 -08002198 len = snprintf(buf, sizeof(buf), "%lx-%lx", p->start, p->end);
Al Virof0c3b502013-05-16 12:07:31 -04002199 if (!proc_fill_cache(file, ctx,
Alexey Dobriyan20d28cd2018-02-06 15:37:06 -08002200 buf, len,
Al Virof0c3b502013-05-16 12:07:31 -04002201 proc_map_files_instantiate,
2202 task,
2203 (void *)(unsigned long)p->mode))
2204 break;
2205 ctx->pos++;
2206 }
2207 if (fa)
2208 flex_array_free(fa);
Pavel Emelyanov640708a2012-01-10 15:11:23 -08002209
Pavel Emelyanov640708a2012-01-10 15:11:23 -08002210out_put_task:
2211 put_task_struct(task);
2212out:
2213 return ret;
2214}
2215
2216static const struct file_operations proc_map_files_operations = {
2217 .read = generic_read_dir,
Al Virof50752e2016-04-20 17:13:54 -04002218 .iterate_shared = proc_map_files_readdir,
2219 .llseek = generic_file_llseek,
Pavel Emelyanov640708a2012-01-10 15:11:23 -08002220};
2221
Nicolas Pitreb18b6a92017-01-21 00:09:08 -05002222#if defined(CONFIG_CHECKPOINT_RESTORE) && defined(CONFIG_POSIX_TIMERS)
Pavel Emelyanov48f6a7a2013-03-11 13:12:45 +04002223struct timers_private {
2224 struct pid *pid;
2225 struct task_struct *task;
2226 struct sighand_struct *sighand;
Pavel Emelyanov57b80152013-03-11 13:13:08 +04002227 struct pid_namespace *ns;
Pavel Emelyanov48f6a7a2013-03-11 13:12:45 +04002228 unsigned long flags;
2229};
2230
2231static void *timers_start(struct seq_file *m, loff_t *pos)
2232{
2233 struct timers_private *tp = m->private;
2234
2235 tp->task = get_pid_task(tp->pid, PIDTYPE_PID);
2236 if (!tp->task)
2237 return ERR_PTR(-ESRCH);
2238
2239 tp->sighand = lock_task_sighand(tp->task, &tp->flags);
2240 if (!tp->sighand)
2241 return ERR_PTR(-ESRCH);
2242
2243 return seq_list_start(&tp->task->signal->posix_timers, *pos);
2244}
2245
2246static void *timers_next(struct seq_file *m, void *v, loff_t *pos)
2247{
2248 struct timers_private *tp = m->private;
2249 return seq_list_next(v, &tp->task->signal->posix_timers, pos);
2250}
2251
2252static void timers_stop(struct seq_file *m, void *v)
2253{
2254 struct timers_private *tp = m->private;
2255
2256 if (tp->sighand) {
2257 unlock_task_sighand(tp->task, &tp->flags);
2258 tp->sighand = NULL;
2259 }
2260
2261 if (tp->task) {
2262 put_task_struct(tp->task);
2263 tp->task = NULL;
2264 }
2265}
2266
2267static int show_timer(struct seq_file *m, void *v)
2268{
2269 struct k_itimer *timer;
Pavel Emelyanov57b80152013-03-11 13:13:08 +04002270 struct timers_private *tp = m->private;
2271 int notify;
Alexey Dobriyancedbcca2014-08-08 14:21:33 -07002272 static const char * const nstr[] = {
Pavel Emelyanov57b80152013-03-11 13:13:08 +04002273 [SIGEV_SIGNAL] = "signal",
2274 [SIGEV_NONE] = "none",
2275 [SIGEV_THREAD] = "thread",
2276 };
Pavel Emelyanov48f6a7a2013-03-11 13:12:45 +04002277
2278 timer = list_entry((struct list_head *)v, struct k_itimer, list);
Pavel Emelyanov57b80152013-03-11 13:13:08 +04002279 notify = timer->it_sigev_notify;
2280
Pavel Emelyanov48f6a7a2013-03-11 13:12:45 +04002281 seq_printf(m, "ID: %d\n", timer->it_id);
Linus Torvaldsba3edf1f2017-12-06 18:23:27 -08002282 seq_printf(m, "signal: %d/%px\n",
Joe Perches25ce3192015-04-15 16:18:17 -07002283 timer->sigq->info.si_signo,
2284 timer->sigq->info.si_value.sival_ptr);
Pavel Emelyanov57b80152013-03-11 13:13:08 +04002285 seq_printf(m, "notify: %s/%s.%d\n",
Joe Perches25ce3192015-04-15 16:18:17 -07002286 nstr[notify & ~SIGEV_THREAD_ID],
2287 (notify & SIGEV_THREAD_ID) ? "tid" : "pid",
2288 pid_nr_ns(timer->it_pid, tp->ns));
Pavel Tikhomirov15ef0292013-05-17 02:12:03 +04002289 seq_printf(m, "ClockID: %d\n", timer->it_clock);
Pavel Emelyanov48f6a7a2013-03-11 13:12:45 +04002290
2291 return 0;
2292}
2293
2294static const struct seq_operations proc_timers_seq_ops = {
2295 .start = timers_start,
2296 .next = timers_next,
2297 .stop = timers_stop,
2298 .show = show_timer,
2299};
2300
2301static int proc_timers_open(struct inode *inode, struct file *file)
2302{
2303 struct timers_private *tp;
2304
2305 tp = __seq_open_private(file, &proc_timers_seq_ops,
2306 sizeof(struct timers_private));
2307 if (!tp)
2308 return -ENOMEM;
2309
2310 tp->pid = proc_pid(inode);
Christoph Hellwig76f668b2018-05-16 07:19:01 +02002311 tp->ns = proc_pid_ns(inode);
Pavel Emelyanov48f6a7a2013-03-11 13:12:45 +04002312 return 0;
2313}
2314
2315static const struct file_operations proc_timers_operations = {
2316 .open = proc_timers_open,
2317 .read = seq_read,
2318 .llseek = seq_lseek,
2319 .release = seq_release_private,
2320};
Eric Engestromb5946be2016-03-17 14:20:57 -07002321#endif
Pavel Emelyanov640708a2012-01-10 15:11:23 -08002322
John Stultz5de23d42016-03-17 14:20:54 -07002323static ssize_t timerslack_ns_write(struct file *file, const char __user *buf,
2324 size_t count, loff_t *offset)
2325{
2326 struct inode *inode = file_inode(file);
2327 struct task_struct *p;
2328 u64 slack_ns;
2329 int err;
2330
2331 err = kstrtoull_from_user(buf, count, 10, &slack_ns);
2332 if (err < 0)
2333 return err;
2334
2335 p = get_proc_task(inode);
2336 if (!p)
2337 return -ESRCH;
2338
John Stultz4b2bd5f2016-10-07 17:02:33 -07002339 if (p != current) {
2340 if (!capable(CAP_SYS_NICE)) {
2341 count = -EPERM;
2342 goto out;
2343 }
John Stultz5de23d42016-03-17 14:20:54 -07002344
John Stultz4b2bd5f2016-10-07 17:02:33 -07002345 err = security_task_setscheduler(p);
2346 if (err) {
2347 count = err;
2348 goto out;
2349 }
John Stultz904763e2016-10-07 17:02:29 -07002350 }
2351
John Stultz7abbaf92016-10-07 17:02:26 -07002352 task_lock(p);
2353 if (slack_ns == 0)
2354 p->timer_slack_ns = p->default_timer_slack_ns;
2355 else
2356 p->timer_slack_ns = slack_ns;
2357 task_unlock(p);
2358
2359out:
John Stultz5de23d42016-03-17 14:20:54 -07002360 put_task_struct(p);
2361
2362 return count;
2363}
2364
2365static int timerslack_ns_show(struct seq_file *m, void *v)
2366{
2367 struct inode *inode = m->private;
2368 struct task_struct *p;
John Stultz7abbaf92016-10-07 17:02:26 -07002369 int err = 0;
John Stultz5de23d42016-03-17 14:20:54 -07002370
2371 p = get_proc_task(inode);
2372 if (!p)
2373 return -ESRCH;
2374
John Stultz4b2bd5f2016-10-07 17:02:33 -07002375 if (p != current) {
John Stultz5de23d42016-03-17 14:20:54 -07002376
John Stultz4b2bd5f2016-10-07 17:02:33 -07002377 if (!capable(CAP_SYS_NICE)) {
2378 err = -EPERM;
2379 goto out;
2380 }
2381 err = security_task_getscheduler(p);
2382 if (err)
2383 goto out;
2384 }
John Stultz904763e2016-10-07 17:02:29 -07002385
John Stultz7abbaf92016-10-07 17:02:26 -07002386 task_lock(p);
2387 seq_printf(m, "%llu\n", p->timer_slack_ns);
2388 task_unlock(p);
2389
2390out:
John Stultz5de23d42016-03-17 14:20:54 -07002391 put_task_struct(p);
2392
2393 return err;
2394}
2395
2396static int timerslack_ns_open(struct inode *inode, struct file *filp)
2397{
2398 return single_open(filp, timerslack_ns_show, inode);
2399}
2400
2401static const struct file_operations proc_pid_set_timerslack_ns_operations = {
2402 .open = timerslack_ns_open,
2403 .read = seq_read,
2404 .write = timerslack_ns_write,
2405 .llseek = seq_lseek,
2406 .release = single_release,
2407};
2408
Al Viro0168b9e2018-05-03 09:21:05 -04002409static struct dentry *proc_pident_instantiate(struct dentry *dentry,
2410 struct task_struct *task, const void *ptr)
Eric W. Biederman444ceed2006-10-02 02:18:49 -07002411{
Eric Dumazetc5141e62007-05-08 00:26:15 -07002412 const struct pid_entry *p = ptr;
Eric W. Biederman444ceed2006-10-02 02:18:49 -07002413 struct inode *inode;
2414 struct proc_inode *ei;
Eric W. Biederman444ceed2006-10-02 02:18:49 -07002415
Al Viro0168b9e2018-05-03 09:21:05 -04002416 inode = proc_pid_make_inode(dentry->d_sb, task, p->mode);
Eric W. Biederman444ceed2006-10-02 02:18:49 -07002417 if (!inode)
Al Viro0168b9e2018-05-03 09:21:05 -04002418 return ERR_PTR(-ENOENT);
Eric W. Biederman444ceed2006-10-02 02:18:49 -07002419
2420 ei = PROC_I(inode);
Eric W. Biederman444ceed2006-10-02 02:18:49 -07002421 if (S_ISDIR(inode->i_mode))
Miklos Szeredibfe86842011-10-28 14:13:29 +02002422 set_nlink(inode, 2); /* Use getattr to fix if necessary */
Eric W. Biederman444ceed2006-10-02 02:18:49 -07002423 if (p->iop)
2424 inode->i_op = p->iop;
2425 if (p->fop)
2426 inode->i_fop = p->fop;
2427 ei->op = p->op;
Al Viro1bbc5512018-05-02 21:26:16 -04002428 pid_update_inode(task, inode);
Nick Pigginfb045ad2011-01-07 17:49:55 +11002429 d_set_d_op(dentry, &pid_dentry_operations);
Al Viro0168b9e2018-05-03 09:21:05 -04002430 return d_splice_alias(inode, dentry);
Eric W. Biederman444ceed2006-10-02 02:18:49 -07002431}
2432
Linus Torvalds1da177e2005-04-16 15:20:36 -07002433static struct dentry *proc_pident_lookup(struct inode *dir,
2434 struct dentry *dentry,
Eric Dumazetc5141e62007-05-08 00:26:15 -07002435 const struct pid_entry *ents,
Eric W. Biederman7bcd6b02006-10-02 02:18:56 -07002436 unsigned int nents)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002437{
Eric W. Biederman99f89552006-06-26 00:25:55 -07002438 struct task_struct *task = get_proc_task(dir);
Eric Dumazetc5141e62007-05-08 00:26:15 -07002439 const struct pid_entry *p, *last;
Al Viro0168b9e2018-05-03 09:21:05 -04002440 struct dentry *res = ERR_PTR(-ENOENT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002441
Eric W. Biederman99f89552006-06-26 00:25:55 -07002442 if (!task)
2443 goto out_no_task;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002444
Eric W. Biederman20cdc892006-10-02 02:17:07 -07002445 /*
2446 * Yes, it does not scale. And it should not. Don't add
2447 * new entries into /proc/<tgid>/ without very good reasons.
2448 */
Alexey Dobriyanbac5f5d2016-12-12 16:45:28 -08002449 last = &ents[nents];
2450 for (p = ents; p < last; p++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002451 if (p->len != dentry->d_name.len)
2452 continue;
2453 if (!memcmp(dentry->d_name.name, p->name, p->len))
2454 break;
2455 }
Alexey Dobriyanbac5f5d2016-12-12 16:45:28 -08002456 if (p >= last)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002457 goto out;
2458
Al Viro0168b9e2018-05-03 09:21:05 -04002459 res = proc_pident_instantiate(dentry, task, p);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002460out:
Eric W. Biederman99f89552006-06-26 00:25:55 -07002461 put_task_struct(task);
2462out_no_task:
Al Viro0168b9e2018-05-03 09:21:05 -04002463 return res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002464}
2465
Al Virof0c3b502013-05-16 12:07:31 -04002466static int proc_pident_readdir(struct file *file, struct dir_context *ctx,
Eric Dumazetc5141e62007-05-08 00:26:15 -07002467 const struct pid_entry *ents, unsigned int nents)
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002468{
Al Virof0c3b502013-05-16 12:07:31 -04002469 struct task_struct *task = get_proc_task(file_inode(file));
2470 const struct pid_entry *p;
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002471
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002472 if (!task)
Al Virof0c3b502013-05-16 12:07:31 -04002473 return -ENOENT;
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002474
Al Virof0c3b502013-05-16 12:07:31 -04002475 if (!dir_emit_dots(file, ctx))
2476 goto out;
2477
2478 if (ctx->pos >= nents + 2)
2479 goto out;
2480
Alexey Dobriyanbac5f5d2016-12-12 16:45:28 -08002481 for (p = ents + (ctx->pos - 2); p < ents + nents; p++) {
Al Virof0c3b502013-05-16 12:07:31 -04002482 if (!proc_fill_cache(file, ctx, p->name, p->len,
2483 proc_pident_instantiate, task, p))
2484 break;
2485 ctx->pos++;
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002486 }
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002487out:
Eric W. Biederman61a28782006-10-02 02:18:49 -07002488 put_task_struct(task);
Al Virof0c3b502013-05-16 12:07:31 -04002489 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002490}
2491
Linus Torvalds1da177e2005-04-16 15:20:36 -07002492#ifdef CONFIG_SECURITY
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002493static ssize_t proc_pid_attr_read(struct file * file, char __user * buf,
2494 size_t count, loff_t *ppos)
2495{
Al Viro496ad9a2013-01-23 17:07:38 -05002496 struct inode * inode = file_inode(file);
Al Viro04ff9702007-03-12 16:17:58 +00002497 char *p = NULL;
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002498 ssize_t length;
2499 struct task_struct *task = get_proc_task(inode);
2500
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002501 if (!task)
Al Viro04ff9702007-03-12 16:17:58 +00002502 return -ESRCH;
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002503
2504 length = security_getprocattr(task,
Josef "Jeff" Sipek2fddfee2006-12-08 02:36:36 -08002505 (char*)file->f_path.dentry->d_name.name,
Al Viro04ff9702007-03-12 16:17:58 +00002506 &p);
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002507 put_task_struct(task);
Al Viro04ff9702007-03-12 16:17:58 +00002508 if (length > 0)
2509 length = simple_read_from_buffer(buf, count, ppos, p, length);
2510 kfree(p);
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002511 return length;
2512}
2513
2514static ssize_t proc_pid_attr_write(struct file * file, const char __user * buf,
2515 size_t count, loff_t *ppos)
2516{
Al Viro496ad9a2013-01-23 17:07:38 -05002517 struct inode * inode = file_inode(file);
Al Virobb646cd2015-12-24 00:16:30 -05002518 void *page;
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002519 ssize_t length;
2520 struct task_struct *task = get_proc_task(inode);
2521
2522 length = -ESRCH;
2523 if (!task)
2524 goto out_no_task;
Stephen Smalleyb21507e2017-01-09 10:07:31 -05002525
2526 /* A task may only write its own attributes. */
2527 length = -EACCES;
2528 if (current != task)
2529 goto out;
2530
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002531 if (count > PAGE_SIZE)
2532 count = PAGE_SIZE;
2533
2534 /* No partial writes. */
2535 length = -EINVAL;
2536 if (*ppos != 0)
2537 goto out;
2538
Al Virobb646cd2015-12-24 00:16:30 -05002539 page = memdup_user(buf, count);
2540 if (IS_ERR(page)) {
2541 length = PTR_ERR(page);
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002542 goto out;
Al Virobb646cd2015-12-24 00:16:30 -05002543 }
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002544
David Howells107db7c2009-05-08 13:55:27 +01002545 /* Guard against adverse ptrace interaction */
Stephen Smalleyb21507e2017-01-09 10:07:31 -05002546 length = mutex_lock_interruptible(&current->signal->cred_guard_mutex);
David Howells107db7c2009-05-08 13:55:27 +01002547 if (length < 0)
2548 goto out_free;
2549
Stephen Smalleyb21507e2017-01-09 10:07:31 -05002550 length = security_setprocattr(file->f_path.dentry->d_name.name,
Al Virobb646cd2015-12-24 00:16:30 -05002551 page, count);
Stephen Smalleyb21507e2017-01-09 10:07:31 -05002552 mutex_unlock(&current->signal->cred_guard_mutex);
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002553out_free:
Al Virobb646cd2015-12-24 00:16:30 -05002554 kfree(page);
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002555out:
2556 put_task_struct(task);
2557out_no_task:
2558 return length;
2559}
2560
Arjan van de Ven00977a52007-02-12 00:55:34 -08002561static const struct file_operations proc_pid_attr_operations = {
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002562 .read = proc_pid_attr_read,
2563 .write = proc_pid_attr_write,
Arnd Bergmann87df8422010-03-17 23:06:02 +01002564 .llseek = generic_file_llseek,
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002565};
2566
Eric Dumazetc5141e62007-05-08 00:26:15 -07002567static const struct pid_entry attr_dir_stuff[] = {
Alexey Dobriyan631f9c12008-11-10 01:32:52 +03002568 REG("current", S_IRUGO|S_IWUGO, proc_pid_attr_operations),
2569 REG("prev", S_IRUGO, proc_pid_attr_operations),
2570 REG("exec", S_IRUGO|S_IWUGO, proc_pid_attr_operations),
2571 REG("fscreate", S_IRUGO|S_IWUGO, proc_pid_attr_operations),
2572 REG("keycreate", S_IRUGO|S_IWUGO, proc_pid_attr_operations),
2573 REG("sockcreate", S_IRUGO|S_IWUGO, proc_pid_attr_operations),
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002574};
2575
Al Virof0c3b502013-05-16 12:07:31 -04002576static int proc_attr_dir_readdir(struct file *file, struct dir_context *ctx)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002577{
Al Virof0c3b502013-05-16 12:07:31 -04002578 return proc_pident_readdir(file, ctx,
2579 attr_dir_stuff, ARRAY_SIZE(attr_dir_stuff));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002580}
2581
Arjan van de Ven00977a52007-02-12 00:55:34 -08002582static const struct file_operations proc_attr_dir_operations = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002583 .read = generic_read_dir,
Al Virof50752e2016-04-20 17:13:54 -04002584 .iterate_shared = proc_attr_dir_readdir,
2585 .llseek = generic_file_llseek,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002586};
2587
Eric W. Biederman72d9dcf2006-10-02 02:18:50 -07002588static struct dentry *proc_attr_dir_lookup(struct inode *dir,
Al Viro00cd8dd2012-06-10 17:13:09 -04002589 struct dentry *dentry, unsigned int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002590{
Eric W. Biederman7bcd6b02006-10-02 02:18:56 -07002591 return proc_pident_lookup(dir, dentry,
2592 attr_dir_stuff, ARRAY_SIZE(attr_dir_stuff));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002593}
2594
Arjan van de Venc5ef1c42007-02-12 00:55:40 -08002595static const struct inode_operations proc_attr_dir_inode_operations = {
Eric W. Biederman72d9dcf2006-10-02 02:18:50 -07002596 .lookup = proc_attr_dir_lookup,
Eric W. Biederman99f89552006-06-26 00:25:55 -07002597 .getattr = pid_getattr,
Linus Torvalds6d76fa52006-07-15 12:26:45 -07002598 .setattr = proc_setattr,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002599};
2600
Linus Torvalds1da177e2005-04-16 15:20:36 -07002601#endif
2602
Christoph Hellwig698ba7b2009-12-15 16:47:37 -08002603#ifdef CONFIG_ELF_CORE
Kawai, Hidehiro3cb4a0b2007-07-19 01:48:28 -07002604static ssize_t proc_coredump_filter_read(struct file *file, char __user *buf,
2605 size_t count, loff_t *ppos)
2606{
Al Viro496ad9a2013-01-23 17:07:38 -05002607 struct task_struct *task = get_proc_task(file_inode(file));
Kawai, Hidehiro3cb4a0b2007-07-19 01:48:28 -07002608 struct mm_struct *mm;
2609 char buffer[PROC_NUMBUF];
2610 size_t len;
2611 int ret;
2612
2613 if (!task)
2614 return -ESRCH;
2615
2616 ret = 0;
2617 mm = get_task_mm(task);
2618 if (mm) {
2619 len = snprintf(buffer, sizeof(buffer), "%08lx\n",
2620 ((mm->flags & MMF_DUMP_FILTER_MASK) >>
2621 MMF_DUMP_FILTER_SHIFT));
2622 mmput(mm);
2623 ret = simple_read_from_buffer(buf, count, ppos, buffer, len);
2624 }
2625
2626 put_task_struct(task);
2627
2628 return ret;
2629}
2630
2631static ssize_t proc_coredump_filter_write(struct file *file,
2632 const char __user *buf,
2633 size_t count,
2634 loff_t *ppos)
2635{
2636 struct task_struct *task;
2637 struct mm_struct *mm;
Kawai, Hidehiro3cb4a0b2007-07-19 01:48:28 -07002638 unsigned int val;
2639 int ret;
2640 int i;
2641 unsigned long mask;
2642
Alexey Dobriyan774636e2015-09-09 15:36:59 -07002643 ret = kstrtouint_from_user(buf, count, 0, &val);
2644 if (ret < 0)
2645 return ret;
Kawai, Hidehiro3cb4a0b2007-07-19 01:48:28 -07002646
2647 ret = -ESRCH;
Al Viro496ad9a2013-01-23 17:07:38 -05002648 task = get_proc_task(file_inode(file));
Kawai, Hidehiro3cb4a0b2007-07-19 01:48:28 -07002649 if (!task)
2650 goto out_no_task;
2651
Kawai, Hidehiro3cb4a0b2007-07-19 01:48:28 -07002652 mm = get_task_mm(task);
2653 if (!mm)
2654 goto out_no_mm;
Colin Ian King41a0c2492015-12-18 14:22:01 -08002655 ret = 0;
Kawai, Hidehiro3cb4a0b2007-07-19 01:48:28 -07002656
2657 for (i = 0, mask = 1; i < MMF_DUMP_FILTER_BITS; i++, mask <<= 1) {
2658 if (val & mask)
2659 set_bit(i + MMF_DUMP_FILTER_SHIFT, &mm->flags);
2660 else
2661 clear_bit(i + MMF_DUMP_FILTER_SHIFT, &mm->flags);
2662 }
2663
2664 mmput(mm);
2665 out_no_mm:
2666 put_task_struct(task);
2667 out_no_task:
Alexey Dobriyan774636e2015-09-09 15:36:59 -07002668 if (ret < 0)
2669 return ret;
2670 return count;
Kawai, Hidehiro3cb4a0b2007-07-19 01:48:28 -07002671}
2672
2673static const struct file_operations proc_coredump_filter_operations = {
2674 .read = proc_coredump_filter_read,
2675 .write = proc_coredump_filter_write,
Arnd Bergmann87df8422010-03-17 23:06:02 +01002676 .llseek = generic_file_llseek,
Kawai, Hidehiro3cb4a0b2007-07-19 01:48:28 -07002677};
2678#endif
2679
Andrew Mortonaba76fd2006-12-10 02:19:48 -08002680#ifdef CONFIG_TASK_IO_ACCOUNTING
Alexey Dobriyan19aadc92014-08-08 14:21:50 -07002681static int do_io_accounting(struct task_struct *task, struct seq_file *m, int whole)
Andrew Mortonaba76fd2006-12-10 02:19:48 -08002682{
Andrea Righi940389b2008-07-28 00:48:12 +02002683 struct task_io_accounting acct = task->ioac;
Andrea Righi59954772008-07-27 17:29:15 +02002684 unsigned long flags;
Vasiliy Kulikov293eb1e2011-07-26 16:08:38 -07002685 int result;
Andrea Righi297c5d92008-07-25 01:48:49 -07002686
Vasiliy Kulikov293eb1e2011-07-26 16:08:38 -07002687 result = mutex_lock_killable(&task->signal->cred_guard_mutex);
2688 if (result)
2689 return result;
2690
Jann Horncaaee622016-01-20 15:00:04 -08002691 if (!ptrace_may_access(task, PTRACE_MODE_READ_FSCREDS)) {
Vasiliy Kulikov293eb1e2011-07-26 16:08:38 -07002692 result = -EACCES;
2693 goto out_unlock;
2694 }
Vasiliy Kulikov1d1221f2011-06-24 16:08:38 +04002695
Andrea Righi59954772008-07-27 17:29:15 +02002696 if (whole && lock_task_sighand(task, &flags)) {
2697 struct task_struct *t = task;
Andrea Righib2d002d2008-07-26 15:22:27 -07002698
Andrea Righi59954772008-07-27 17:29:15 +02002699 task_io_accounting_add(&acct, &task->signal->ioac);
2700 while_each_thread(task, t)
2701 task_io_accounting_add(&acct, &t->ioac);
Andrea Righi297c5d92008-07-25 01:48:49 -07002702
Andrea Righi59954772008-07-27 17:29:15 +02002703 unlock_task_sighand(task, &flags);
Andrea Righi297c5d92008-07-25 01:48:49 -07002704 }
Joe Perches25ce3192015-04-15 16:18:17 -07002705 seq_printf(m,
2706 "rchar: %llu\n"
2707 "wchar: %llu\n"
2708 "syscr: %llu\n"
2709 "syscw: %llu\n"
2710 "read_bytes: %llu\n"
2711 "write_bytes: %llu\n"
2712 "cancelled_write_bytes: %llu\n",
2713 (unsigned long long)acct.rchar,
2714 (unsigned long long)acct.wchar,
2715 (unsigned long long)acct.syscr,
2716 (unsigned long long)acct.syscw,
2717 (unsigned long long)acct.read_bytes,
2718 (unsigned long long)acct.write_bytes,
2719 (unsigned long long)acct.cancelled_write_bytes);
2720 result = 0;
2721
Vasiliy Kulikov293eb1e2011-07-26 16:08:38 -07002722out_unlock:
2723 mutex_unlock(&task->signal->cred_guard_mutex);
2724 return result;
Andrew Mortonaba76fd2006-12-10 02:19:48 -08002725}
Andrea Righi297c5d92008-07-25 01:48:49 -07002726
Alexey Dobriyan19aadc92014-08-08 14:21:50 -07002727static int proc_tid_io_accounting(struct seq_file *m, struct pid_namespace *ns,
2728 struct pid *pid, struct task_struct *task)
Andrea Righi297c5d92008-07-25 01:48:49 -07002729{
Alexey Dobriyan19aadc92014-08-08 14:21:50 -07002730 return do_io_accounting(task, m, 0);
Andrea Righi297c5d92008-07-25 01:48:49 -07002731}
2732
Alexey Dobriyan19aadc92014-08-08 14:21:50 -07002733static int proc_tgid_io_accounting(struct seq_file *m, struct pid_namespace *ns,
2734 struct pid *pid, struct task_struct *task)
Andrea Righi297c5d92008-07-25 01:48:49 -07002735{
Alexey Dobriyan19aadc92014-08-08 14:21:50 -07002736 return do_io_accounting(task, m, 1);
Andrea Righi297c5d92008-07-25 01:48:49 -07002737}
2738#endif /* CONFIG_TASK_IO_ACCOUNTING */
Andrew Mortonaba76fd2006-12-10 02:19:48 -08002739
Eric W. Biederman22d917d2011-11-17 00:11:58 -08002740#ifdef CONFIG_USER_NS
2741static int proc_id_map_open(struct inode *inode, struct file *file,
Fabian Frederickccf94f12014-08-08 14:21:22 -07002742 const struct seq_operations *seq_ops)
Eric W. Biederman22d917d2011-11-17 00:11:58 -08002743{
2744 struct user_namespace *ns = NULL;
2745 struct task_struct *task;
2746 struct seq_file *seq;
2747 int ret = -EINVAL;
2748
2749 task = get_proc_task(inode);
2750 if (task) {
2751 rcu_read_lock();
2752 ns = get_user_ns(task_cred_xxx(task, user_ns));
2753 rcu_read_unlock();
2754 put_task_struct(task);
2755 }
2756 if (!ns)
2757 goto err;
2758
2759 ret = seq_open(file, seq_ops);
2760 if (ret)
2761 goto err_put_ns;
2762
2763 seq = file->private_data;
2764 seq->private = ns;
2765
2766 return 0;
2767err_put_ns:
2768 put_user_ns(ns);
2769err:
2770 return ret;
2771}
2772
2773static int proc_id_map_release(struct inode *inode, struct file *file)
2774{
2775 struct seq_file *seq = file->private_data;
2776 struct user_namespace *ns = seq->private;
2777 put_user_ns(ns);
2778 return seq_release(inode, file);
2779}
2780
2781static int proc_uid_map_open(struct inode *inode, struct file *file)
2782{
2783 return proc_id_map_open(inode, file, &proc_uid_seq_operations);
2784}
2785
2786static int proc_gid_map_open(struct inode *inode, struct file *file)
2787{
2788 return proc_id_map_open(inode, file, &proc_gid_seq_operations);
2789}
2790
Eric W. Biedermanf76d2072012-08-30 01:24:05 -07002791static int proc_projid_map_open(struct inode *inode, struct file *file)
2792{
2793 return proc_id_map_open(inode, file, &proc_projid_seq_operations);
2794}
2795
Eric W. Biederman22d917d2011-11-17 00:11:58 -08002796static const struct file_operations proc_uid_map_operations = {
2797 .open = proc_uid_map_open,
2798 .write = proc_uid_map_write,
2799 .read = seq_read,
2800 .llseek = seq_lseek,
2801 .release = proc_id_map_release,
2802};
2803
2804static const struct file_operations proc_gid_map_operations = {
2805 .open = proc_gid_map_open,
2806 .write = proc_gid_map_write,
2807 .read = seq_read,
2808 .llseek = seq_lseek,
2809 .release = proc_id_map_release,
2810};
Eric W. Biedermanf76d2072012-08-30 01:24:05 -07002811
2812static const struct file_operations proc_projid_map_operations = {
2813 .open = proc_projid_map_open,
2814 .write = proc_projid_map_write,
2815 .read = seq_read,
2816 .llseek = seq_lseek,
2817 .release = proc_id_map_release,
2818};
Eric W. Biederman9cc46512014-12-02 12:27:26 -06002819
2820static int proc_setgroups_open(struct inode *inode, struct file *file)
2821{
2822 struct user_namespace *ns = NULL;
2823 struct task_struct *task;
2824 int ret;
2825
2826 ret = -ESRCH;
2827 task = get_proc_task(inode);
2828 if (task) {
2829 rcu_read_lock();
2830 ns = get_user_ns(task_cred_xxx(task, user_ns));
2831 rcu_read_unlock();
2832 put_task_struct(task);
2833 }
2834 if (!ns)
2835 goto err;
2836
2837 if (file->f_mode & FMODE_WRITE) {
2838 ret = -EACCES;
2839 if (!ns_capable(ns, CAP_SYS_ADMIN))
2840 goto err_put_ns;
2841 }
2842
2843 ret = single_open(file, &proc_setgroups_show, ns);
2844 if (ret)
2845 goto err_put_ns;
2846
2847 return 0;
2848err_put_ns:
2849 put_user_ns(ns);
2850err:
2851 return ret;
2852}
2853
2854static int proc_setgroups_release(struct inode *inode, struct file *file)
2855{
2856 struct seq_file *seq = file->private_data;
2857 struct user_namespace *ns = seq->private;
2858 int ret = single_release(inode, file);
2859 put_user_ns(ns);
2860 return ret;
2861}
2862
2863static const struct file_operations proc_setgroups_operations = {
2864 .open = proc_setgroups_open,
2865 .write = proc_setgroups_write,
2866 .read = seq_read,
2867 .llseek = seq_lseek,
2868 .release = proc_setgroups_release,
2869};
Eric W. Biederman22d917d2011-11-17 00:11:58 -08002870#endif /* CONFIG_USER_NS */
2871
Kees Cook47830722008-10-06 03:11:58 +04002872static int proc_pid_personality(struct seq_file *m, struct pid_namespace *ns,
2873 struct pid *pid, struct task_struct *task)
2874{
Al Viroa9712bc2011-03-23 15:52:50 -04002875 int err = lock_trace(task);
2876 if (!err) {
2877 seq_printf(m, "%08x\n", task->personality);
2878 unlock_trace(task);
2879 }
2880 return err;
Kees Cook47830722008-10-06 03:11:58 +04002881}
2882
Josh Poimboeuf7c23b332017-02-13 19:42:41 -06002883#ifdef CONFIG_LIVEPATCH
2884static int proc_pid_patch_state(struct seq_file *m, struct pid_namespace *ns,
2885 struct pid *pid, struct task_struct *task)
2886{
2887 seq_printf(m, "%d\n", task->patch_state);
2888 return 0;
2889}
2890#endif /* CONFIG_LIVEPATCH */
2891
Eric W. Biederman801199c2006-10-02 02:18:48 -07002892/*
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002893 * Thread groups
2894 */
Arjan van de Ven00977a52007-02-12 00:55:34 -08002895static const struct file_operations proc_task_operations;
Arjan van de Venc5ef1c42007-02-12 00:55:40 -08002896static const struct inode_operations proc_task_inode_operations;
Eric W. Biederman20cdc892006-10-02 02:17:07 -07002897
Eric Dumazetc5141e62007-05-08 00:26:15 -07002898static const struct pid_entry tgid_base_stuff[] = {
Alexey Dobriyan631f9c12008-11-10 01:32:52 +03002899 DIR("task", S_IRUGO|S_IXUGO, proc_task_inode_operations, proc_task_operations),
2900 DIR("fd", S_IRUSR|S_IXUSR, proc_fd_inode_operations, proc_fd_operations),
Pavel Emelyanov640708a2012-01-10 15:11:23 -08002901 DIR("map_files", S_IRUSR|S_IXUSR, proc_map_files_inode_operations, proc_map_files_operations),
Alexey Dobriyan631f9c12008-11-10 01:32:52 +03002902 DIR("fdinfo", S_IRUSR|S_IXUSR, proc_fdinfo_inode_operations, proc_fdinfo_operations),
Eric W. Biederman6b4e3062010-03-07 16:41:34 -08002903 DIR("ns", S_IRUSR|S_IXUGO, proc_ns_dir_inode_operations, proc_ns_dir_operations),
Andrew Mortonb2211a32008-03-11 18:03:35 -07002904#ifdef CONFIG_NET
Alexey Dobriyan631f9c12008-11-10 01:32:52 +03002905 DIR("net", S_IRUGO|S_IXUGO, proc_net_inode_operations, proc_net_operations),
Andrew Mortonb2211a32008-03-11 18:03:35 -07002906#endif
Alexey Dobriyan631f9c12008-11-10 01:32:52 +03002907 REG("environ", S_IRUSR, proc_environ_operations),
Al Viroc5317162016-10-05 18:43:43 -04002908 REG("auxv", S_IRUSR, proc_auxv_operations),
Alexey Dobriyan631f9c12008-11-10 01:32:52 +03002909 ONE("status", S_IRUGO, proc_pid_status),
Djalal Harouni35a35042014-04-07 15:38:36 -07002910 ONE("personality", S_IRUSR, proc_pid_personality),
Alexey Dobriyan1c963eb2014-08-08 14:21:37 -07002911 ONE("limits", S_IRUGO, proc_pid_limits),
Ingo Molnar43ae34c2007-07-09 18:52:00 +02002912#ifdef CONFIG_SCHED_DEBUG
Alexey Dobriyan631f9c12008-11-10 01:32:52 +03002913 REG("sched", S_IRUGO|S_IWUSR, proc_pid_sched_operations),
Ingo Molnar43ae34c2007-07-09 18:52:00 +02002914#endif
Mike Galbraith5091faa2010-11-30 14:18:03 +01002915#ifdef CONFIG_SCHED_AUTOGROUP
2916 REG("autogroup", S_IRUGO|S_IWUSR, proc_pid_sched_autogroup_operations),
2917#endif
john stultz4614a696b2009-12-14 18:00:05 -08002918 REG("comm", S_IRUGO|S_IWUSR, proc_pid_set_comm_operations),
Roland McGrathebcb6732008-07-25 19:46:00 -07002919#ifdef CONFIG_HAVE_ARCH_TRACEHOOK
Alexey Dobriyan09d93bd2014-08-08 14:21:39 -07002920 ONE("syscall", S_IRUSR, proc_pid_syscall),
Roland McGrathebcb6732008-07-25 19:46:00 -07002921#endif
Alexey Dobriyanc2c0bb42015-06-25 15:00:54 -07002922 REG("cmdline", S_IRUGO, proc_pid_cmdline_ops),
Alexey Dobriyan631f9c12008-11-10 01:32:52 +03002923 ONE("stat", S_IRUGO, proc_tgid_stat),
2924 ONE("statm", S_IRUGO, proc_pid_statm),
Siddhesh Poyarekarb7643752012-03-21 16:34:04 -07002925 REG("maps", S_IRUGO, proc_pid_maps_operations),
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002926#ifdef CONFIG_NUMA
Siddhesh Poyarekarb7643752012-03-21 16:34:04 -07002927 REG("numa_maps", S_IRUGO, proc_pid_numa_maps_operations),
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002928#endif
Alexey Dobriyan631f9c12008-11-10 01:32:52 +03002929 REG("mem", S_IRUSR|S_IWUSR, proc_mem_operations),
2930 LNK("cwd", proc_cwd_link),
2931 LNK("root", proc_root_link),
2932 LNK("exe", proc_exe_link),
2933 REG("mounts", S_IRUGO, proc_mounts_operations),
2934 REG("mountinfo", S_IRUGO, proc_mountinfo_operations),
2935 REG("mountstats", S_IRUSR, proc_mountstats_operations),
Matt Mackall1e883282008-02-04 22:29:07 -08002936#ifdef CONFIG_PROC_PAGE_MONITOR
Alexey Dobriyan631f9c12008-11-10 01:32:52 +03002937 REG("clear_refs", S_IWUSR, proc_clear_refs_operations),
Siddhesh Poyarekarb7643752012-03-21 16:34:04 -07002938 REG("smaps", S_IRUGO, proc_pid_smaps_operations),
Daniel Colascione493b0e92017-09-06 16:25:08 -07002939 REG("smaps_rollup", S_IRUGO, proc_pid_smaps_rollup_operations),
Djalal Harouni32ed74a2014-04-07 15:38:38 -07002940 REG("pagemap", S_IRUSR, proc_pagemap_operations),
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002941#endif
2942#ifdef CONFIG_SECURITY
Alexey Dobriyan631f9c12008-11-10 01:32:52 +03002943 DIR("attr", S_IRUGO|S_IXUGO, proc_attr_dir_inode_operations, proc_attr_dir_operations),
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002944#endif
2945#ifdef CONFIG_KALLSYMS
Alexey Dobriyanedfcd602014-08-08 14:21:44 -07002946 ONE("wchan", S_IRUGO, proc_pid_wchan),
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002947#endif
Ken Chen2ec220e2008-11-10 11:26:08 +03002948#ifdef CONFIG_STACKTRACE
Djalal Harouni35a35042014-04-07 15:38:36 -07002949 ONE("stack", S_IRUSR, proc_pid_stack),
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002950#endif
Naveen N. Rao5968cec2015-06-30 14:36:03 +05302951#ifdef CONFIG_SCHED_INFO
Alexey Dobriyanf6e826c2014-08-08 14:21:46 -07002952 ONE("schedstat", S_IRUGO, proc_pid_schedstat),
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002953#endif
Arjan van de Ven97455122008-01-25 21:08:34 +01002954#ifdef CONFIG_LATENCYTOP
Alexey Dobriyan631f9c12008-11-10 01:32:52 +03002955 REG("latency", S_IRUGO, proc_lstats_operations),
Arjan van de Ven97455122008-01-25 21:08:34 +01002956#endif
Paul Menage8793d852007-10-18 23:39:39 -07002957#ifdef CONFIG_PROC_PID_CPUSET
Zefan Li52de4772014-09-18 16:03:36 +08002958 ONE("cpuset", S_IRUGO, proc_cpuset_show),
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002959#endif
Paul Menagea4243162007-10-18 23:39:35 -07002960#ifdef CONFIG_CGROUPS
Zefan Li006f4ac2014-09-18 16:03:15 +08002961 ONE("cgroup", S_IRUGO, proc_cgroup_show),
Paul Menagea4243162007-10-18 23:39:35 -07002962#endif
Alexey Dobriyan6ba51e32014-08-08 14:21:48 -07002963 ONE("oom_score", S_IRUGO, proc_oom_score),
David Rientjesfa0cbbf2012-11-12 17:53:04 -08002964 REG("oom_adj", S_IRUGO|S_IWUSR, proc_oom_adj_operations),
David Rientjesa63d83f2010-08-09 17:19:46 -07002965 REG("oom_score_adj", S_IRUGO|S_IWUSR, proc_oom_score_adj_operations),
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002966#ifdef CONFIG_AUDITSYSCALL
Alexey Dobriyan631f9c12008-11-10 01:32:52 +03002967 REG("loginuid", S_IWUSR|S_IRUGO, proc_loginuid_operations),
2968 REG("sessionid", S_IRUGO, proc_sessionid_operations),
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002969#endif
Akinobu Mitaf4f154f2006-12-08 02:39:47 -08002970#ifdef CONFIG_FAULT_INJECTION
Alexey Dobriyan631f9c12008-11-10 01:32:52 +03002971 REG("make-it-fail", S_IRUGO|S_IWUSR, proc_fault_inject_operations),
Akinobu Mita168c42b2017-07-14 14:50:00 -07002972 REG("fail-nth", 0644, proc_fail_nth_operations),
Akinobu Mitaf4f154f2006-12-08 02:39:47 -08002973#endif
Christoph Hellwig698ba7b2009-12-15 16:47:37 -08002974#ifdef CONFIG_ELF_CORE
Alexey Dobriyan631f9c12008-11-10 01:32:52 +03002975 REG("coredump_filter", S_IRUGO|S_IWUSR, proc_coredump_filter_operations),
Kawai, Hidehiro3cb4a0b2007-07-19 01:48:28 -07002976#endif
Andrew Mortonaba76fd2006-12-10 02:19:48 -08002977#ifdef CONFIG_TASK_IO_ACCOUNTING
Alexey Dobriyan19aadc92014-08-08 14:21:50 -07002978 ONE("io", S_IRUSR, proc_tgid_io_accounting),
Andrew Mortonaba76fd2006-12-10 02:19:48 -08002979#endif
Eric W. Biederman22d917d2011-11-17 00:11:58 -08002980#ifdef CONFIG_USER_NS
2981 REG("uid_map", S_IRUGO|S_IWUSR, proc_uid_map_operations),
2982 REG("gid_map", S_IRUGO|S_IWUSR, proc_gid_map_operations),
Eric W. Biedermanf76d2072012-08-30 01:24:05 -07002983 REG("projid_map", S_IRUGO|S_IWUSR, proc_projid_map_operations),
Eric W. Biederman9cc46512014-12-02 12:27:26 -06002984 REG("setgroups", S_IRUGO|S_IWUSR, proc_setgroups_operations),
Eric W. Biederman22d917d2011-11-17 00:11:58 -08002985#endif
Nicolas Pitreb18b6a92017-01-21 00:09:08 -05002986#if defined(CONFIG_CHECKPOINT_RESTORE) && defined(CONFIG_POSIX_TIMERS)
Pavel Emelyanov48f6a7a2013-03-11 13:12:45 +04002987 REG("timers", S_IRUGO, proc_timers_operations),
2988#endif
John Stultz5de23d42016-03-17 14:20:54 -07002989 REG("timerslack_ns", S_IRUGO|S_IWUGO, proc_pid_set_timerslack_ns_operations),
Josh Poimboeuf7c23b332017-02-13 19:42:41 -06002990#ifdef CONFIG_LIVEPATCH
2991 ONE("patch_state", S_IRUSR, proc_pid_patch_state),
2992#endif
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002993};
2994
Al Virof0c3b502013-05-16 12:07:31 -04002995static int proc_tgid_base_readdir(struct file *file, struct dir_context *ctx)
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002996{
Al Virof0c3b502013-05-16 12:07:31 -04002997 return proc_pident_readdir(file, ctx,
2998 tgid_base_stuff, ARRAY_SIZE(tgid_base_stuff));
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002999}
3000
Arjan van de Ven00977a52007-02-12 00:55:34 -08003001static const struct file_operations proc_tgid_base_operations = {
Eric W. Biederman28a6d672006-10-02 02:17:05 -07003002 .read = generic_read_dir,
Al Virof50752e2016-04-20 17:13:54 -04003003 .iterate_shared = proc_tgid_base_readdir,
3004 .llseek = generic_file_llseek,
Eric W. Biederman28a6d672006-10-02 02:17:05 -07003005};
3006
Al Viro00cd8dd2012-06-10 17:13:09 -04003007static struct dentry *proc_tgid_base_lookup(struct inode *dir, struct dentry *dentry, unsigned int flags)
3008{
Eric W. Biederman7bcd6b02006-10-02 02:18:56 -07003009 return proc_pident_lookup(dir, dentry,
3010 tgid_base_stuff, ARRAY_SIZE(tgid_base_stuff));
Eric W. Biederman28a6d672006-10-02 02:17:05 -07003011}
3012
Arjan van de Venc5ef1c42007-02-12 00:55:40 -08003013static const struct inode_operations proc_tgid_base_inode_operations = {
Eric W. Biederman28a6d672006-10-02 02:17:05 -07003014 .lookup = proc_tgid_base_lookup,
3015 .getattr = pid_getattr,
3016 .setattr = proc_setattr,
Vasiliy Kulikov04996802012-01-10 15:11:31 -08003017 .permission = proc_pid_permission,
Eric W. Biederman28a6d672006-10-02 02:17:05 -07003018};
3019
Pavel Emelyanov60347f62007-10-18 23:40:03 -07003020static void proc_flush_task_mnt(struct vfsmount *mnt, pid_t pid, pid_t tgid)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003021{
Eric W. Biederman48e64842006-06-26 00:25:48 -07003022 struct dentry *dentry, *leader, *dir;
Alexey Dobriyane3912ac2018-02-06 15:36:51 -08003023 char buf[10 + 1];
Eric W. Biederman48e64842006-06-26 00:25:48 -07003024 struct qstr name;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003025
Eric W. Biederman48e64842006-06-26 00:25:48 -07003026 name.name = buf;
Alexey Dobriyane3912ac2018-02-06 15:36:51 -08003027 name.len = snprintf(buf, sizeof(buf), "%u", pid);
Al Viro4f522a22013-02-11 23:20:37 -05003028 /* no ->d_hash() rejects on procfs */
Pavel Emelyanov60347f62007-10-18 23:40:03 -07003029 dentry = d_hash_and_lookup(mnt->mnt_root, &name);
Eric W. Biederman48e64842006-06-26 00:25:48 -07003030 if (dentry) {
Eric W. Biedermanbbd51922014-02-13 10:24:23 -08003031 d_invalidate(dentry);
Eric W. Biederman48e64842006-06-26 00:25:48 -07003032 dput(dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003033 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003034
Oleg Nesterovc35a7f12014-12-10 15:54:56 -08003035 if (pid == tgid)
3036 return;
3037
Eric W. Biederman48e64842006-06-26 00:25:48 -07003038 name.name = buf;
Alexey Dobriyane3912ac2018-02-06 15:36:51 -08003039 name.len = snprintf(buf, sizeof(buf), "%u", tgid);
Pavel Emelyanov60347f62007-10-18 23:40:03 -07003040 leader = d_hash_and_lookup(mnt->mnt_root, &name);
Eric W. Biederman48e64842006-06-26 00:25:48 -07003041 if (!leader)
3042 goto out;
3043
3044 name.name = "task";
3045 name.len = strlen(name.name);
3046 dir = d_hash_and_lookup(leader, &name);
3047 if (!dir)
3048 goto out_put_leader;
3049
3050 name.name = buf;
Alexey Dobriyane3912ac2018-02-06 15:36:51 -08003051 name.len = snprintf(buf, sizeof(buf), "%u", pid);
Eric W. Biederman48e64842006-06-26 00:25:48 -07003052 dentry = d_hash_and_lookup(dir, &name);
3053 if (dentry) {
Eric W. Biedermanbbd51922014-02-13 10:24:23 -08003054 d_invalidate(dentry);
Eric W. Biederman48e64842006-06-26 00:25:48 -07003055 dput(dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003056 }
Eric W. Biederman48e64842006-06-26 00:25:48 -07003057
3058 dput(dir);
3059out_put_leader:
3060 dput(leader);
3061out:
3062 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003063}
3064
Randy Dunlap0895e912007-10-21 21:00:10 -07003065/**
3066 * proc_flush_task - Remove dcache entries for @task from the /proc dcache.
3067 * @task: task that should be flushed.
3068 *
3069 * When flushing dentries from proc, one needs to flush them from global
Pavel Emelyanov60347f62007-10-18 23:40:03 -07003070 * proc (proc_mnt) and from all the namespaces' procs this task was seen
Randy Dunlap0895e912007-10-21 21:00:10 -07003071 * in. This call is supposed to do all of this job.
3072 *
3073 * Looks in the dcache for
3074 * /proc/@pid
3075 * /proc/@tgid/task/@pid
3076 * if either directory is present flushes it and all of it'ts children
3077 * from the dcache.
3078 *
3079 * It is safe and reasonable to cache /proc entries for a task until
3080 * that task exits. After that they just clog up the dcache with
3081 * useless entries, possibly causing useful dcache entries to be
3082 * flushed instead. This routine is proved to flush those useless
3083 * dcache entries at process exit time.
3084 *
3085 * NOTE: This routine is just an optimization so it does not guarantee
3086 * that no dcache entries will exist at process exit time it
3087 * just makes it very unlikely that any will persist.
Pavel Emelyanov60347f62007-10-18 23:40:03 -07003088 */
3089
3090void proc_flush_task(struct task_struct *task)
3091{
Eric W. Biederman9fcc2d12007-11-14 17:00:07 -08003092 int i;
Oleg Nesterov9b4d1cbe2009-09-22 16:45:34 -07003093 struct pid *pid, *tgid;
Pavel Emelyanov130f77e2007-10-18 23:40:11 -07003094 struct upid *upid;
3095
Pavel Emelyanov130f77e2007-10-18 23:40:11 -07003096 pid = task_pid(task);
Oleg Nesterov9b4d1cbe2009-09-22 16:45:34 -07003097 tgid = task_tgid(task);
Pavel Emelyanov130f77e2007-10-18 23:40:11 -07003098
Eric W. Biederman9fcc2d12007-11-14 17:00:07 -08003099 for (i = 0; i <= pid->level; i++) {
Pavel Emelyanov130f77e2007-10-18 23:40:11 -07003100 upid = &pid->numbers[i];
3101 proc_flush_task_mnt(upid->ns->proc_mnt, upid->nr,
Oleg Nesterov9b4d1cbe2009-09-22 16:45:34 -07003102 tgid->numbers[i].nr);
Pavel Emelyanov130f77e2007-10-18 23:40:11 -07003103 }
Pavel Emelyanov60347f62007-10-18 23:40:03 -07003104}
3105
Al Viro0168b9e2018-05-03 09:21:05 -04003106static struct dentry *proc_pid_instantiate(struct dentry * dentry,
Al Viroc52a47a2013-06-15 11:15:20 +04003107 struct task_struct *task, const void *ptr)
Eric W. Biederman444ceed2006-10-02 02:18:49 -07003108{
Eric W. Biederman444ceed2006-10-02 02:18:49 -07003109 struct inode *inode;
3110
Al Viro0168b9e2018-05-03 09:21:05 -04003111 inode = proc_pid_make_inode(dentry->d_sb, task, S_IFDIR | S_IRUGO | S_IXUGO);
Eric W. Biederman444ceed2006-10-02 02:18:49 -07003112 if (!inode)
Al Viro0168b9e2018-05-03 09:21:05 -04003113 return ERR_PTR(-ENOENT);
Eric W. Biederman444ceed2006-10-02 02:18:49 -07003114
Eric W. Biederman444ceed2006-10-02 02:18:49 -07003115 inode->i_op = &proc_tgid_base_inode_operations;
3116 inode->i_fop = &proc_tgid_base_operations;
3117 inode->i_flags|=S_IMMUTABLE;
Vegard Nossumaed54172008-06-05 22:46:53 -07003118
Alexey Dobriyan1270dd82016-12-12 16:45:32 -08003119 set_nlink(inode, nlink_tgid);
Al Viro1bbc5512018-05-02 21:26:16 -04003120 pid_update_inode(task, inode);
Eric W. Biederman444ceed2006-10-02 02:18:49 -07003121
Nick Pigginfb045ad2011-01-07 17:49:55 +11003122 d_set_d_op(dentry, &pid_dentry_operations);
Al Viro0168b9e2018-05-03 09:21:05 -04003123 return d_splice_alias(inode, dentry);
Eric W. Biederman444ceed2006-10-02 02:18:49 -07003124}
3125
Al Viro00cd8dd2012-06-10 17:13:09 -04003126struct dentry *proc_pid_lookup(struct inode *dir, struct dentry * dentry, unsigned int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003127{
3128 struct task_struct *task;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003129 unsigned tgid;
Pavel Emelyanovb4888932007-10-18 23:40:14 -07003130 struct pid_namespace *ns;
Al Viro0168b9e2018-05-03 09:21:05 -04003131 struct dentry *result = ERR_PTR(-ENOENT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003132
Alexey Dobriyandbcdb502014-08-08 14:21:25 -07003133 tgid = name_to_int(&dentry->d_name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003134 if (tgid == ~0U)
3135 goto out;
3136
Pavel Emelyanovb4888932007-10-18 23:40:14 -07003137 ns = dentry->d_sb->s_fs_info;
Eric W. Biedermande758732006-06-26 00:25:51 -07003138 rcu_read_lock();
Pavel Emelyanovb4888932007-10-18 23:40:14 -07003139 task = find_task_by_pid_ns(tgid, ns);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003140 if (task)
3141 get_task_struct(task);
Eric W. Biedermande758732006-06-26 00:25:51 -07003142 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07003143 if (!task)
3144 goto out;
3145
Al Viro0168b9e2018-05-03 09:21:05 -04003146 result = proc_pid_instantiate(dentry, task, NULL);
Eric W. Biederman48e64842006-06-26 00:25:48 -07003147 put_task_struct(task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003148out:
Al Viro0168b9e2018-05-03 09:21:05 -04003149 return result;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003150}
3151
Linus Torvalds1da177e2005-04-16 15:20:36 -07003152/*
Eric W. Biederman0804ef42006-10-02 02:17:04 -07003153 * Find the first task with tgid >= tgid
Eric W. Biederman0bc58a92006-06-26 00:25:50 -07003154 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07003155 */
Eric W. Biederman19fd4bb2007-11-28 16:21:26 -08003156struct tgid_iter {
3157 unsigned int tgid;
Eric W. Biederman0804ef42006-10-02 02:17:04 -07003158 struct task_struct *task;
Eric W. Biederman19fd4bb2007-11-28 16:21:26 -08003159};
3160static struct tgid_iter next_tgid(struct pid_namespace *ns, struct tgid_iter iter)
3161{
Eric W. Biederman0804ef42006-10-02 02:17:04 -07003162 struct pid *pid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003163
Eric W. Biederman19fd4bb2007-11-28 16:21:26 -08003164 if (iter.task)
3165 put_task_struct(iter.task);
Eric W. Biederman0804ef42006-10-02 02:17:04 -07003166 rcu_read_lock();
3167retry:
Eric W. Biederman19fd4bb2007-11-28 16:21:26 -08003168 iter.task = NULL;
3169 pid = find_ge_pid(iter.tgid, ns);
Eric W. Biederman0804ef42006-10-02 02:17:04 -07003170 if (pid) {
Eric W. Biederman19fd4bb2007-11-28 16:21:26 -08003171 iter.tgid = pid_nr_ns(pid, ns);
3172 iter.task = pid_task(pid, PIDTYPE_PID);
Eric W. Biederman0804ef42006-10-02 02:17:04 -07003173 /* What we to know is if the pid we have find is the
3174 * pid of a thread_group_leader. Testing for task
3175 * being a thread_group_leader is the obvious thing
3176 * todo but there is a window when it fails, due to
3177 * the pid transfer logic in de_thread.
3178 *
3179 * So we perform the straight forward test of seeing
3180 * if the pid we have found is the pid of a thread
3181 * group leader, and don't worry if the task we have
3182 * found doesn't happen to be a thread group leader.
3183 * As we don't care in the case of readdir.
3184 */
Eric W. Biederman19fd4bb2007-11-28 16:21:26 -08003185 if (!iter.task || !has_group_leader_pid(iter.task)) {
3186 iter.tgid += 1;
Eric W. Biederman0804ef42006-10-02 02:17:04 -07003187 goto retry;
Eric W. Biederman19fd4bb2007-11-28 16:21:26 -08003188 }
3189 get_task_struct(iter.task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003190 }
Eric W. Biederman454cc102006-06-26 00:25:51 -07003191 rcu_read_unlock();
Eric W. Biederman19fd4bb2007-11-28 16:21:26 -08003192 return iter;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003193}
3194
Eric W. Biederman00978752014-07-31 03:10:50 -07003195#define TGID_OFFSET (FIRST_PROCESS_ENTRY + 2)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003196
3197/* for the /proc/ directory itself, after non-process stuff has been done */
Al Virof0c3b502013-05-16 12:07:31 -04003198int proc_pid_readdir(struct file *file, struct dir_context *ctx)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003199{
Eric W. Biederman19fd4bb2007-11-28 16:21:26 -08003200 struct tgid_iter iter;
Christoph Hellwig76f668b2018-05-16 07:19:01 +02003201 struct pid_namespace *ns = proc_pid_ns(file_inode(file));
Al Virof0c3b502013-05-16 12:07:31 -04003202 loff_t pos = ctx->pos;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003203
Al Viro021ada72013-03-29 19:27:05 -04003204 if (pos >= PID_MAX_LIMIT + TGID_OFFSET)
Al Virof0c3b502013-05-16 12:07:31 -04003205 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003206
Eric W. Biederman00978752014-07-31 03:10:50 -07003207 if (pos == TGID_OFFSET - 2) {
David Howells2b0143b2015-03-17 22:25:59 +00003208 struct inode *inode = d_inode(ns->proc_self);
Al Virodb963162013-06-15 10:45:10 +04003209 if (!dir_emit(ctx, "self", 4, inode->i_ino, DT_LNK))
Al Virof0c3b502013-05-16 12:07:31 -04003210 return 0;
Eric W. Biederman00978752014-07-31 03:10:50 -07003211 ctx->pos = pos = pos + 1;
Al Viro021ada72013-03-29 19:27:05 -04003212 }
Eric W. Biederman00978752014-07-31 03:10:50 -07003213 if (pos == TGID_OFFSET - 1) {
David Howells2b0143b2015-03-17 22:25:59 +00003214 struct inode *inode = d_inode(ns->proc_thread_self);
Eric W. Biederman00978752014-07-31 03:10:50 -07003215 if (!dir_emit(ctx, "thread-self", 11, inode->i_ino, DT_LNK))
3216 return 0;
3217 ctx->pos = pos = pos + 1;
3218 }
3219 iter.tgid = pos - TGID_OFFSET;
Eric W. Biederman19fd4bb2007-11-28 16:21:26 -08003220 iter.task = NULL;
Eric W. Biederman19fd4bb2007-11-28 16:21:26 -08003221 for (iter = next_tgid(ns, iter);
3222 iter.task;
3223 iter.tgid += 1, iter = next_tgid(ns, iter)) {
Alexey Dobriyane3912ac2018-02-06 15:36:51 -08003224 char name[10 + 1];
Al Virof0c3b502013-05-16 12:07:31 -04003225 int len;
Eric Dumazet3ba4bce2017-01-24 15:18:07 -08003226
3227 cond_resched();
Lafcadio Wluiki796f5712017-02-24 15:00:23 -08003228 if (!has_pid_permissions(ns, iter.task, HIDEPID_INVISIBLE))
Al Virof0c3b502013-05-16 12:07:31 -04003229 continue;
Vasiliy Kulikov04996802012-01-10 15:11:31 -08003230
Alexey Dobriyane3912ac2018-02-06 15:36:51 -08003231 len = snprintf(name, sizeof(name), "%u", iter.tgid);
Al Virof0c3b502013-05-16 12:07:31 -04003232 ctx->pos = iter.tgid + TGID_OFFSET;
3233 if (!proc_fill_cache(file, ctx, name, len,
3234 proc_pid_instantiate, iter.task, NULL)) {
Eric W. Biederman19fd4bb2007-11-28 16:21:26 -08003235 put_task_struct(iter.task);
Al Virof0c3b502013-05-16 12:07:31 -04003236 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003237 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003238 }
Al Virof0c3b502013-05-16 12:07:31 -04003239 ctx->pos = PID_MAX_LIMIT + TGID_OFFSET;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003240 return 0;
3241}
3242
Eric W. Biederman0bc58a92006-06-26 00:25:50 -07003243/*
Janis Danisevskis1b3044e2016-05-20 17:00:08 -07003244 * proc_tid_comm_permission is a special permission function exclusively
3245 * used for the node /proc/<pid>/task/<tid>/comm.
3246 * It bypasses generic permission checks in the case where a task of the same
3247 * task group attempts to access the node.
3248 * The rationale behind this is that glibc and bionic access this node for
3249 * cross thread naming (pthread_set/getname_np(!self)). However, if
3250 * PR_SET_DUMPABLE gets set to 0 this node among others becomes uid=0 gid=0,
3251 * which locks out the cross thread naming implementation.
3252 * This function makes sure that the node is always accessible for members of
3253 * same thread group.
3254 */
3255static int proc_tid_comm_permission(struct inode *inode, int mask)
3256{
3257 bool is_same_tgroup;
3258 struct task_struct *task;
3259
3260 task = get_proc_task(inode);
3261 if (!task)
3262 return -ESRCH;
3263 is_same_tgroup = same_thread_group(current, task);
3264 put_task_struct(task);
3265
3266 if (likely(is_same_tgroup && !(mask & MAY_EXEC))) {
3267 /* This file (/proc/<pid>/task/<tid>/comm) can always be
3268 * read or written by the members of the corresponding
3269 * thread group.
3270 */
3271 return 0;
3272 }
3273
3274 return generic_permission(inode, mask);
3275}
3276
3277static const struct inode_operations proc_tid_comm_inode_operations = {
3278 .permission = proc_tid_comm_permission,
3279};
3280
3281/*
Eric W. Biederman28a6d672006-10-02 02:17:05 -07003282 * Tasks
3283 */
Eric Dumazetc5141e62007-05-08 00:26:15 -07003284static const struct pid_entry tid_base_stuff[] = {
Alexey Dobriyan631f9c12008-11-10 01:32:52 +03003285 DIR("fd", S_IRUSR|S_IXUSR, proc_fd_inode_operations, proc_fd_operations),
Jerome Marchand38355412010-04-27 13:13:06 -07003286 DIR("fdinfo", S_IRUSR|S_IXUSR, proc_fdinfo_inode_operations, proc_fdinfo_operations),
Eric W. Biederman6b4e3062010-03-07 16:41:34 -08003287 DIR("ns", S_IRUSR|S_IXUGO, proc_ns_dir_inode_operations, proc_ns_dir_operations),
Eric W. Biederman6ba8ed72014-07-31 16:27:08 -07003288#ifdef CONFIG_NET
3289 DIR("net", S_IRUGO|S_IXUGO, proc_net_inode_operations, proc_net_operations),
3290#endif
Alexey Dobriyan631f9c12008-11-10 01:32:52 +03003291 REG("environ", S_IRUSR, proc_environ_operations),
Al Viroc5317162016-10-05 18:43:43 -04003292 REG("auxv", S_IRUSR, proc_auxv_operations),
Alexey Dobriyan631f9c12008-11-10 01:32:52 +03003293 ONE("status", S_IRUGO, proc_pid_status),
Djalal Harouni35a35042014-04-07 15:38:36 -07003294 ONE("personality", S_IRUSR, proc_pid_personality),
Alexey Dobriyan1c963eb2014-08-08 14:21:37 -07003295 ONE("limits", S_IRUGO, proc_pid_limits),
Ingo Molnar43ae34c2007-07-09 18:52:00 +02003296#ifdef CONFIG_SCHED_DEBUG
Alexey Dobriyan631f9c12008-11-10 01:32:52 +03003297 REG("sched", S_IRUGO|S_IWUSR, proc_pid_sched_operations),
Ingo Molnar43ae34c2007-07-09 18:52:00 +02003298#endif
Janis Danisevskis1b3044e2016-05-20 17:00:08 -07003299 NOD("comm", S_IFREG|S_IRUGO|S_IWUSR,
3300 &proc_tid_comm_inode_operations,
3301 &proc_pid_set_comm_operations, {}),
Roland McGrathebcb6732008-07-25 19:46:00 -07003302#ifdef CONFIG_HAVE_ARCH_TRACEHOOK
Alexey Dobriyan09d93bd2014-08-08 14:21:39 -07003303 ONE("syscall", S_IRUSR, proc_pid_syscall),
Roland McGrathebcb6732008-07-25 19:46:00 -07003304#endif
Alexey Dobriyanc2c0bb42015-06-25 15:00:54 -07003305 REG("cmdline", S_IRUGO, proc_pid_cmdline_ops),
Alexey Dobriyan631f9c12008-11-10 01:32:52 +03003306 ONE("stat", S_IRUGO, proc_tid_stat),
3307 ONE("statm", S_IRUGO, proc_pid_statm),
Siddhesh Poyarekarb7643752012-03-21 16:34:04 -07003308 REG("maps", S_IRUGO, proc_tid_maps_operations),
Iago López Galeiras2e13ba52015-06-25 15:00:57 -07003309#ifdef CONFIG_PROC_CHILDREN
Cyrill Gorcunov818411612012-05-31 16:26:43 -07003310 REG("children", S_IRUGO, proc_tid_children_operations),
3311#endif
Eric W. Biederman28a6d672006-10-02 02:17:05 -07003312#ifdef CONFIG_NUMA
Siddhesh Poyarekarb7643752012-03-21 16:34:04 -07003313 REG("numa_maps", S_IRUGO, proc_tid_numa_maps_operations),
Eric W. Biederman28a6d672006-10-02 02:17:05 -07003314#endif
Alexey Dobriyan631f9c12008-11-10 01:32:52 +03003315 REG("mem", S_IRUSR|S_IWUSR, proc_mem_operations),
3316 LNK("cwd", proc_cwd_link),
3317 LNK("root", proc_root_link),
3318 LNK("exe", proc_exe_link),
3319 REG("mounts", S_IRUGO, proc_mounts_operations),
3320 REG("mountinfo", S_IRUGO, proc_mountinfo_operations),
Matt Mackall1e883282008-02-04 22:29:07 -08003321#ifdef CONFIG_PROC_PAGE_MONITOR
Alexey Dobriyan631f9c12008-11-10 01:32:52 +03003322 REG("clear_refs", S_IWUSR, proc_clear_refs_operations),
Siddhesh Poyarekarb7643752012-03-21 16:34:04 -07003323 REG("smaps", S_IRUGO, proc_tid_smaps_operations),
Daniel Colascione493b0e92017-09-06 16:25:08 -07003324 REG("smaps_rollup", S_IRUGO, proc_pid_smaps_rollup_operations),
Djalal Harouni32ed74a2014-04-07 15:38:38 -07003325 REG("pagemap", S_IRUSR, proc_pagemap_operations),
Eric W. Biederman28a6d672006-10-02 02:17:05 -07003326#endif
3327#ifdef CONFIG_SECURITY
Alexey Dobriyan631f9c12008-11-10 01:32:52 +03003328 DIR("attr", S_IRUGO|S_IXUGO, proc_attr_dir_inode_operations, proc_attr_dir_operations),
Eric W. Biederman28a6d672006-10-02 02:17:05 -07003329#endif
3330#ifdef CONFIG_KALLSYMS
Alexey Dobriyanedfcd602014-08-08 14:21:44 -07003331 ONE("wchan", S_IRUGO, proc_pid_wchan),
Eric W. Biederman28a6d672006-10-02 02:17:05 -07003332#endif
Ken Chen2ec220e2008-11-10 11:26:08 +03003333#ifdef CONFIG_STACKTRACE
Djalal Harouni35a35042014-04-07 15:38:36 -07003334 ONE("stack", S_IRUSR, proc_pid_stack),
Eric W. Biederman28a6d672006-10-02 02:17:05 -07003335#endif
Naveen N. Rao5968cec2015-06-30 14:36:03 +05303336#ifdef CONFIG_SCHED_INFO
Alexey Dobriyanf6e826c2014-08-08 14:21:46 -07003337 ONE("schedstat", S_IRUGO, proc_pid_schedstat),
Eric W. Biederman28a6d672006-10-02 02:17:05 -07003338#endif
Arjan van de Ven97455122008-01-25 21:08:34 +01003339#ifdef CONFIG_LATENCYTOP
Alexey Dobriyan631f9c12008-11-10 01:32:52 +03003340 REG("latency", S_IRUGO, proc_lstats_operations),
Arjan van de Ven97455122008-01-25 21:08:34 +01003341#endif
Paul Menage8793d852007-10-18 23:39:39 -07003342#ifdef CONFIG_PROC_PID_CPUSET
Zefan Li52de4772014-09-18 16:03:36 +08003343 ONE("cpuset", S_IRUGO, proc_cpuset_show),
Eric W. Biederman28a6d672006-10-02 02:17:05 -07003344#endif
Paul Menagea4243162007-10-18 23:39:35 -07003345#ifdef CONFIG_CGROUPS
Zefan Li006f4ac2014-09-18 16:03:15 +08003346 ONE("cgroup", S_IRUGO, proc_cgroup_show),
Paul Menagea4243162007-10-18 23:39:35 -07003347#endif
Alexey Dobriyan6ba51e32014-08-08 14:21:48 -07003348 ONE("oom_score", S_IRUGO, proc_oom_score),
David Rientjesfa0cbbf2012-11-12 17:53:04 -08003349 REG("oom_adj", S_IRUGO|S_IWUSR, proc_oom_adj_operations),
David Rientjesa63d83f2010-08-09 17:19:46 -07003350 REG("oom_score_adj", S_IRUGO|S_IWUSR, proc_oom_score_adj_operations),
Eric W. Biederman28a6d672006-10-02 02:17:05 -07003351#ifdef CONFIG_AUDITSYSCALL
Alexey Dobriyan631f9c12008-11-10 01:32:52 +03003352 REG("loginuid", S_IWUSR|S_IRUGO, proc_loginuid_operations),
Al Viro26ec3c62011-02-15 21:24:05 -05003353 REG("sessionid", S_IRUGO, proc_sessionid_operations),
Eric W. Biederman28a6d672006-10-02 02:17:05 -07003354#endif
Akinobu Mitaf4f154f2006-12-08 02:39:47 -08003355#ifdef CONFIG_FAULT_INJECTION
Alexey Dobriyan631f9c12008-11-10 01:32:52 +03003356 REG("make-it-fail", S_IRUGO|S_IWUSR, proc_fault_inject_operations),
Akinobu Mita1203c8e2017-07-14 14:49:57 -07003357 REG("fail-nth", 0644, proc_fail_nth_operations),
Akinobu Mitaf4f154f2006-12-08 02:39:47 -08003358#endif
Andrea Righi297c5d92008-07-25 01:48:49 -07003359#ifdef CONFIG_TASK_IO_ACCOUNTING
Alexey Dobriyan19aadc92014-08-08 14:21:50 -07003360 ONE("io", S_IRUSR, proc_tid_io_accounting),
Andrea Righi297c5d92008-07-25 01:48:49 -07003361#endif
Eric W. Biederman22d917d2011-11-17 00:11:58 -08003362#ifdef CONFIG_USER_NS
3363 REG("uid_map", S_IRUGO|S_IWUSR, proc_uid_map_operations),
3364 REG("gid_map", S_IRUGO|S_IWUSR, proc_gid_map_operations),
Eric W. Biedermanf76d2072012-08-30 01:24:05 -07003365 REG("projid_map", S_IRUGO|S_IWUSR, proc_projid_map_operations),
Eric W. Biederman9cc46512014-12-02 12:27:26 -06003366 REG("setgroups", S_IRUGO|S_IWUSR, proc_setgroups_operations),
Eric W. Biederman22d917d2011-11-17 00:11:58 -08003367#endif
Josh Poimboeuf7c23b332017-02-13 19:42:41 -06003368#ifdef CONFIG_LIVEPATCH
3369 ONE("patch_state", S_IRUSR, proc_pid_patch_state),
3370#endif
Eric W. Biederman28a6d672006-10-02 02:17:05 -07003371};
3372
Al Virof0c3b502013-05-16 12:07:31 -04003373static int proc_tid_base_readdir(struct file *file, struct dir_context *ctx)
Eric W. Biederman28a6d672006-10-02 02:17:05 -07003374{
Al Virof0c3b502013-05-16 12:07:31 -04003375 return proc_pident_readdir(file, ctx,
3376 tid_base_stuff, ARRAY_SIZE(tid_base_stuff));
Eric W. Biederman28a6d672006-10-02 02:17:05 -07003377}
3378
Al Viro00cd8dd2012-06-10 17:13:09 -04003379static struct dentry *proc_tid_base_lookup(struct inode *dir, struct dentry *dentry, unsigned int flags)
3380{
Eric W. Biederman7bcd6b02006-10-02 02:18:56 -07003381 return proc_pident_lookup(dir, dentry,
3382 tid_base_stuff, ARRAY_SIZE(tid_base_stuff));
Eric W. Biederman28a6d672006-10-02 02:17:05 -07003383}
3384
Arjan van de Ven00977a52007-02-12 00:55:34 -08003385static const struct file_operations proc_tid_base_operations = {
Eric W. Biederman28a6d672006-10-02 02:17:05 -07003386 .read = generic_read_dir,
Al Virof50752e2016-04-20 17:13:54 -04003387 .iterate_shared = proc_tid_base_readdir,
3388 .llseek = generic_file_llseek,
Eric W. Biederman28a6d672006-10-02 02:17:05 -07003389};
3390
Arjan van de Venc5ef1c42007-02-12 00:55:40 -08003391static const struct inode_operations proc_tid_base_inode_operations = {
Eric W. Biederman28a6d672006-10-02 02:17:05 -07003392 .lookup = proc_tid_base_lookup,
3393 .getattr = pid_getattr,
3394 .setattr = proc_setattr,
3395};
3396
Al Viro0168b9e2018-05-03 09:21:05 -04003397static struct dentry *proc_task_instantiate(struct dentry *dentry,
3398 struct task_struct *task, const void *ptr)
Eric W. Biederman444ceed2006-10-02 02:18:49 -07003399{
Eric W. Biederman444ceed2006-10-02 02:18:49 -07003400 struct inode *inode;
Al Viro0168b9e2018-05-03 09:21:05 -04003401 inode = proc_pid_make_inode(dentry->d_sb, task, S_IFDIR | S_IRUGO | S_IXUGO);
Eric W. Biederman444ceed2006-10-02 02:18:49 -07003402 if (!inode)
Al Viro0168b9e2018-05-03 09:21:05 -04003403 return ERR_PTR(-ENOENT);
Al Viro1bbc5512018-05-02 21:26:16 -04003404
Eric W. Biederman444ceed2006-10-02 02:18:49 -07003405 inode->i_op = &proc_tid_base_inode_operations;
3406 inode->i_fop = &proc_tid_base_operations;
Al Viro1bbc5512018-05-02 21:26:16 -04003407 inode->i_flags |= S_IMMUTABLE;
Vegard Nossumaed54172008-06-05 22:46:53 -07003408
Alexey Dobriyan1270dd82016-12-12 16:45:32 -08003409 set_nlink(inode, nlink_tid);
Al Viro1bbc5512018-05-02 21:26:16 -04003410 pid_update_inode(task, inode);
Eric W. Biederman444ceed2006-10-02 02:18:49 -07003411
Nick Pigginfb045ad2011-01-07 17:49:55 +11003412 d_set_d_op(dentry, &pid_dentry_operations);
Al Viro0168b9e2018-05-03 09:21:05 -04003413 return d_splice_alias(inode, dentry);
Eric W. Biederman444ceed2006-10-02 02:18:49 -07003414}
3415
Al Viro00cd8dd2012-06-10 17:13:09 -04003416static struct dentry *proc_task_lookup(struct inode *dir, struct dentry * dentry, unsigned int flags)
Eric W. Biederman28a6d672006-10-02 02:17:05 -07003417{
Eric W. Biederman28a6d672006-10-02 02:17:05 -07003418 struct task_struct *task;
3419 struct task_struct *leader = get_proc_task(dir);
Eric W. Biederman28a6d672006-10-02 02:17:05 -07003420 unsigned tid;
Pavel Emelyanovb4888932007-10-18 23:40:14 -07003421 struct pid_namespace *ns;
Al Viro0168b9e2018-05-03 09:21:05 -04003422 struct dentry *result = ERR_PTR(-ENOENT);
Eric W. Biederman28a6d672006-10-02 02:17:05 -07003423
3424 if (!leader)
3425 goto out_no_task;
3426
Alexey Dobriyandbcdb502014-08-08 14:21:25 -07003427 tid = name_to_int(&dentry->d_name);
Eric W. Biederman28a6d672006-10-02 02:17:05 -07003428 if (tid == ~0U)
3429 goto out;
3430
Pavel Emelyanovb4888932007-10-18 23:40:14 -07003431 ns = dentry->d_sb->s_fs_info;
Eric W. Biederman28a6d672006-10-02 02:17:05 -07003432 rcu_read_lock();
Pavel Emelyanovb4888932007-10-18 23:40:14 -07003433 task = find_task_by_pid_ns(tid, ns);
Eric W. Biederman28a6d672006-10-02 02:17:05 -07003434 if (task)
3435 get_task_struct(task);
3436 rcu_read_unlock();
3437 if (!task)
3438 goto out;
Pavel Emelyanovbac0abd2007-10-18 23:40:18 -07003439 if (!same_thread_group(leader, task))
Eric W. Biederman28a6d672006-10-02 02:17:05 -07003440 goto out_drop_task;
3441
Al Viro0168b9e2018-05-03 09:21:05 -04003442 result = proc_task_instantiate(dentry, task, NULL);
Eric W. Biederman28a6d672006-10-02 02:17:05 -07003443out_drop_task:
3444 put_task_struct(task);
3445out:
3446 put_task_struct(leader);
3447out_no_task:
Al Viro0168b9e2018-05-03 09:21:05 -04003448 return result;
Eric W. Biederman28a6d672006-10-02 02:17:05 -07003449}
3450
3451/*
Eric W. Biederman0bc58a92006-06-26 00:25:50 -07003452 * Find the first tid of a thread group to return to user space.
3453 *
3454 * Usually this is just the thread group leader, but if the users
3455 * buffer was too small or there was a seek into the middle of the
3456 * directory we have more work todo.
3457 *
3458 * In the case of a short read we start with find_task_by_pid.
3459 *
3460 * In the case of a seek we start with the leader and walk nr
3461 * threads past it.
3462 */
Oleg Nesterov9f6e9632014-01-23 15:55:40 -08003463static struct task_struct *first_tid(struct pid *pid, int tid, loff_t f_pos,
3464 struct pid_namespace *ns)
Eric W. Biederman0bc58a92006-06-26 00:25:50 -07003465{
Oleg Nesterovd855a4b2014-01-23 15:55:39 -08003466 struct task_struct *pos, *task;
Oleg Nesterov9f6e9632014-01-23 15:55:40 -08003467 unsigned long nr = f_pos;
3468
3469 if (nr != f_pos) /* 32bit overflow? */
3470 return NULL;
Eric W. Biederman0bc58a92006-06-26 00:25:50 -07003471
Eric W. Biedermancc288732006-06-26 00:26:01 -07003472 rcu_read_lock();
Oleg Nesterovd855a4b2014-01-23 15:55:39 -08003473 task = pid_task(pid, PIDTYPE_PID);
3474 if (!task)
3475 goto fail;
3476
3477 /* Attempt to start with the tid of a thread */
Oleg Nesterov9f6e9632014-01-23 15:55:40 -08003478 if (tid && nr) {
Pavel Emelyanovb4888932007-10-18 23:40:14 -07003479 pos = find_task_by_pid_ns(tid, ns);
Oleg Nesterovd855a4b2014-01-23 15:55:39 -08003480 if (pos && same_thread_group(pos, task))
Oleg Nesterova872ff02006-06-26 00:26:01 -07003481 goto found;
Eric W. Biederman0bc58a92006-06-26 00:25:50 -07003482 }
3483
3484 /* If nr exceeds the number of threads there is nothing todo */
Oleg Nesterov9f6e9632014-01-23 15:55:40 -08003485 if (nr >= get_nr_threads(task))
Oleg Nesterovc986c142014-01-23 15:55:38 -08003486 goto fail;
Oleg Nesterova872ff02006-06-26 00:26:01 -07003487
3488 /* If we haven't found our starting place yet start
3489 * with the leader and walk nr threads forward.
3490 */
Oleg Nesterovd855a4b2014-01-23 15:55:39 -08003491 pos = task = task->group_leader;
Oleg Nesterovc986c142014-01-23 15:55:38 -08003492 do {
Oleg Nesterov9f6e9632014-01-23 15:55:40 -08003493 if (!nr--)
Oleg Nesterovc986c142014-01-23 15:55:38 -08003494 goto found;
Oleg Nesterovd855a4b2014-01-23 15:55:39 -08003495 } while_each_thread(task, pos);
Oleg Nesterovc986c142014-01-23 15:55:38 -08003496fail:
3497 pos = NULL;
3498 goto out;
Oleg Nesterova872ff02006-06-26 00:26:01 -07003499found:
3500 get_task_struct(pos);
3501out:
Eric W. Biedermancc288732006-06-26 00:26:01 -07003502 rcu_read_unlock();
Eric W. Biederman0bc58a92006-06-26 00:25:50 -07003503 return pos;
3504}
3505
3506/*
3507 * Find the next thread in the thread list.
3508 * Return NULL if there is an error or no next thread.
3509 *
3510 * The reference to the input task_struct is released.
3511 */
3512static struct task_struct *next_tid(struct task_struct *start)
3513{
Oleg Nesterovc1df7fb2006-06-26 00:26:02 -07003514 struct task_struct *pos = NULL;
Eric W. Biedermancc288732006-06-26 00:26:01 -07003515 rcu_read_lock();
Oleg Nesterovc1df7fb2006-06-26 00:26:02 -07003516 if (pid_alive(start)) {
Eric W. Biederman0bc58a92006-06-26 00:25:50 -07003517 pos = next_thread(start);
Oleg Nesterovc1df7fb2006-06-26 00:26:02 -07003518 if (thread_group_leader(pos))
3519 pos = NULL;
3520 else
3521 get_task_struct(pos);
3522 }
Eric W. Biedermancc288732006-06-26 00:26:01 -07003523 rcu_read_unlock();
Eric W. Biederman0bc58a92006-06-26 00:25:50 -07003524 put_task_struct(start);
3525 return pos;
3526}
3527
Linus Torvalds1da177e2005-04-16 15:20:36 -07003528/* for the /proc/TGID/task/ directories */
Al Virof0c3b502013-05-16 12:07:31 -04003529static int proc_task_readdir(struct file *file, struct dir_context *ctx)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003530{
Oleg Nesterovd855a4b2014-01-23 15:55:39 -08003531 struct inode *inode = file_inode(file);
3532 struct task_struct *task;
Pavel Emelyanovb4888932007-10-18 23:40:14 -07003533 struct pid_namespace *ns;
Al Virof0c3b502013-05-16 12:07:31 -04003534 int tid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003535
Oleg Nesterovd855a4b2014-01-23 15:55:39 -08003536 if (proc_inode_is_dead(inode))
Al Virof0c3b502013-05-16 12:07:31 -04003537 return -ENOENT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003538
Al Virof0c3b502013-05-16 12:07:31 -04003539 if (!dir_emit_dots(file, ctx))
Oleg Nesterovd855a4b2014-01-23 15:55:39 -08003540 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003541
Eric W. Biederman0bc58a92006-06-26 00:25:50 -07003542 /* f_version caches the tgid value that the last readdir call couldn't
3543 * return. lseek aka telldir automagically resets f_version to 0.
3544 */
Christoph Hellwig76f668b2018-05-16 07:19:01 +02003545 ns = proc_pid_ns(inode);
Al Virof0c3b502013-05-16 12:07:31 -04003546 tid = (int)file->f_version;
3547 file->f_version = 0;
Oleg Nesterovd855a4b2014-01-23 15:55:39 -08003548 for (task = first_tid(proc_pid(inode), tid, ctx->pos - 2, ns);
Eric W. Biederman0bc58a92006-06-26 00:25:50 -07003549 task;
Al Virof0c3b502013-05-16 12:07:31 -04003550 task = next_tid(task), ctx->pos++) {
Alexey Dobriyane3912ac2018-02-06 15:36:51 -08003551 char name[10 + 1];
Al Virof0c3b502013-05-16 12:07:31 -04003552 int len;
Pavel Emelyanovb4888932007-10-18 23:40:14 -07003553 tid = task_pid_nr_ns(task, ns);
Alexey Dobriyane3912ac2018-02-06 15:36:51 -08003554 len = snprintf(name, sizeof(name), "%u", tid);
Al Virof0c3b502013-05-16 12:07:31 -04003555 if (!proc_fill_cache(file, ctx, name, len,
3556 proc_task_instantiate, task, NULL)) {
Eric W. Biederman0bc58a92006-06-26 00:25:50 -07003557 /* returning this tgid failed, save it as the first
3558 * pid for the next readir call */
Al Virof0c3b502013-05-16 12:07:31 -04003559 file->f_version = (u64)tid;
Eric W. Biederman0bc58a92006-06-26 00:25:50 -07003560 put_task_struct(task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003561 break;
Eric W. Biederman0bc58a92006-06-26 00:25:50 -07003562 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003563 }
Oleg Nesterovd855a4b2014-01-23 15:55:39 -08003564
Al Virof0c3b502013-05-16 12:07:31 -04003565 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003566}
Eric W. Biederman6e66b522006-06-26 00:25:47 -07003567
David Howellsa528d352017-01-31 16:46:22 +00003568static int proc_task_getattr(const struct path *path, struct kstat *stat,
3569 u32 request_mask, unsigned int query_flags)
Eric W. Biederman6e66b522006-06-26 00:25:47 -07003570{
David Howellsa528d352017-01-31 16:46:22 +00003571 struct inode *inode = d_inode(path->dentry);
Eric W. Biederman99f89552006-06-26 00:25:55 -07003572 struct task_struct *p = get_proc_task(inode);
Eric W. Biederman6e66b522006-06-26 00:25:47 -07003573 generic_fillattr(inode, stat);
3574
Eric W. Biederman99f89552006-06-26 00:25:55 -07003575 if (p) {
Eric W. Biederman99f89552006-06-26 00:25:55 -07003576 stat->nlink += get_nr_threads(p);
Eric W. Biederman99f89552006-06-26 00:25:55 -07003577 put_task_struct(p);
Eric W. Biederman6e66b522006-06-26 00:25:47 -07003578 }
3579
3580 return 0;
3581}
Eric W. Biederman28a6d672006-10-02 02:17:05 -07003582
Arjan van de Venc5ef1c42007-02-12 00:55:40 -08003583static const struct inode_operations proc_task_inode_operations = {
Eric W. Biederman28a6d672006-10-02 02:17:05 -07003584 .lookup = proc_task_lookup,
3585 .getattr = proc_task_getattr,
3586 .setattr = proc_setattr,
Vasiliy Kulikov04996802012-01-10 15:11:31 -08003587 .permission = proc_pid_permission,
Eric W. Biederman28a6d672006-10-02 02:17:05 -07003588};
3589
Arjan van de Ven00977a52007-02-12 00:55:34 -08003590static const struct file_operations proc_task_operations = {
Eric W. Biederman28a6d672006-10-02 02:17:05 -07003591 .read = generic_read_dir,
Al Virof50752e2016-04-20 17:13:54 -04003592 .iterate_shared = proc_task_readdir,
3593 .llseek = generic_file_llseek,
Eric W. Biederman28a6d672006-10-02 02:17:05 -07003594};
Alexey Dobriyan1270dd82016-12-12 16:45:32 -08003595
3596void __init set_proc_pid_nlink(void)
3597{
3598 nlink_tid = pid_entry_nlink(tid_base_stuff, ARRAY_SIZE(tid_base_stuff));
3599 nlink_tgid = pid_entry_nlink(tgid_base_stuff, ARRAY_SIZE(tgid_base_stuff));
3600}