blob: 8fbab527bd241aec8def87d9c1b4acadd086496e [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>
Kent Overstreet94f8f3b2019-03-11 23:31:18 -070062#include <linux/generic-radix-tree.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070063#include <linux/string.h>
64#include <linux/seq_file.h>
65#include <linux/namei.h>
Kirill Korotaev6b3286e2006-12-08 02:37:56 -080066#include <linux/mnt_namespace.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070067#include <linux/mm.h>
David Rientjesa63d83f2010-08-09 17:19:46 -070068#include <linux/swap.h>
Dipankar Sarmab8359962005-09-09 13:04:14 -070069#include <linux/rcupdate.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070070#include <linux/kallsyms.h>
Ken Chen2ec220e2008-11-10 11:26:08 +030071#include <linux/stacktrace.h>
Neil Hormand85f50d2007-10-18 23:40:37 -070072#include <linux/resource.h>
Kees Cook5096add2007-05-08 00:26:04 -070073#include <linux/module.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070074#include <linux/mount.h>
75#include <linux/security.h>
76#include <linux/ptrace.h>
Roland McGrath0d094ef2008-07-25 19:45:49 -070077#include <linux/tracehook.h>
Andrew Morton87ebdc02013-02-27 17:03:16 -080078#include <linux/printk.h>
Alexey Dobriyanefb1a572018-02-06 15:37:24 -080079#include <linux/cache.h>
Paul Menagea4243162007-10-18 23:39:35 -070080#include <linux/cgroup.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070081#include <linux/cpuset.h>
82#include <linux/audit.h>
Al Viro5addc5d2005-11-07 17:15:49 -050083#include <linux/poll.h>
Serge E. Hallyn1651e142006-10-02 02:18:08 -070084#include <linux/nsproxy.h>
Alexey Dobriyan8ac773b2006-10-19 23:28:32 -070085#include <linux/oom.h>
Kawai, Hidehiro3cb4a0b2007-07-19 01:48:28 -070086#include <linux/elf.h>
Pavel Emelyanov60347f62007-10-18 23:40:03 -070087#include <linux/pid_namespace.h>
Eric W. Biederman22d917d2011-11-17 00:11:58 -080088#include <linux/user_namespace.h>
Al Viro5ad4e532009-03-29 19:50:06 -040089#include <linux/fs_struct.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090090#include <linux/slab.h>
Ingo Molnar4eb5aaa2017-02-08 18:51:29 +010091#include <linux/sched/autogroup.h>
Ingo Molnar6e84f312017-02-08 18:51:29 +010092#include <linux/sched/mm.h>
Ingo Molnarf7ccbae2017-02-08 18:51:30 +010093#include <linux/sched/coredump.h>
Ingo Molnarb17b0152017-02-08 18:51:35 +010094#include <linux/sched/debug.h>
Ingo Molnar3905f9a2017-02-05 12:07:04 +010095#include <linux/sched/stat.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) \
Casey Schaufler6d9c9392018-09-21 17:16:59 -0700143 NOD(NAME, (S_IFREG|(MODE)), \
Eric W. Biedermanbe614082008-02-08 04:18:30 -0800144 NULL, &proc_single_file_operations, \
Alexey Dobriyan631f9c12008-11-10 01:32:52 +0300145 { .proc_show = show } )
Casey Schaufler6d9c9392018-09-21 17:16:59 -0700146#define ATTR(LSM, NAME, MODE) \
147 NOD(NAME, (S_IFREG|(MODE)), \
148 NULL, &proc_pid_attr_operations, \
149 { .lsm = LSM })
Linus Torvalds1da177e2005-04-16 15:20:36 -0700150
Vegard Nossumaed54172008-06-05 22:46:53 -0700151/*
152 * Count the number of hardlinks for the pid_entry table, excluding the .
153 * and .. links.
154 */
Alexey Dobriyan1270dd82016-12-12 16:45:32 -0800155static unsigned int __init pid_entry_nlink(const struct pid_entry *entries,
Vegard Nossumaed54172008-06-05 22:46:53 -0700156 unsigned int n)
157{
158 unsigned int i;
159 unsigned int count;
160
Alexey Dobriyan1270dd82016-12-12 16:45:32 -0800161 count = 2;
Vegard Nossumaed54172008-06-05 22:46:53 -0700162 for (i = 0; i < n; ++i) {
163 if (S_ISDIR(entries[i].mode))
164 ++count;
165 }
166
167 return count;
168}
169
Miklos Szeredif7ad3c62010-08-10 11:41:36 +0200170static int get_task_root(struct task_struct *task, struct path *root)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700171{
Hugh Dickins7c2c7d92009-03-28 23:21:27 +0000172 int result = -ENOENT;
173
Miklos Szeredi0494f6e2005-09-06 15:18:22 -0700174 task_lock(task);
Miklos Szeredif7ad3c62010-08-10 11:41:36 +0200175 if (task->fs) {
176 get_fs_root(task->fs, root);
Hugh Dickins7c2c7d92009-03-28 23:21:27 +0000177 result = 0;
178 }
Miklos Szeredi0494f6e2005-09-06 15:18:22 -0700179 task_unlock(task);
Hugh Dickins7c2c7d92009-03-28 23:21:27 +0000180 return result;
Miklos Szeredi0494f6e2005-09-06 15:18:22 -0700181}
182
Cyrill Gorcunov7773fbc2012-01-10 15:11:20 -0800183static int proc_cwd_link(struct dentry *dentry, struct path *path)
Miklos Szeredi0494f6e2005-09-06 15:18:22 -0700184{
David Howells2b0143b2015-03-17 22:25:59 +0000185 struct task_struct *task = get_proc_task(d_inode(dentry));
Miklos Szeredi0494f6e2005-09-06 15:18:22 -0700186 int result = -ENOENT;
Eric W. Biederman99f89552006-06-26 00:25:55 -0700187
188 if (task) {
Miklos Szeredif7ad3c62010-08-10 11:41:36 +0200189 task_lock(task);
190 if (task->fs) {
191 get_fs_pwd(task->fs, path);
192 result = 0;
193 }
194 task_unlock(task);
Eric W. Biederman99f89552006-06-26 00:25:55 -0700195 put_task_struct(task);
196 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700197 return result;
198}
199
Cyrill Gorcunov7773fbc2012-01-10 15:11:20 -0800200static int proc_root_link(struct dentry *dentry, struct path *path)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700201{
David Howells2b0143b2015-03-17 22:25:59 +0000202 struct task_struct *task = get_proc_task(d_inode(dentry));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700203 int result = -ENOENT;
Eric W. Biederman99f89552006-06-26 00:25:55 -0700204
205 if (task) {
Miklos Szeredif7ad3c62010-08-10 11:41:36 +0200206 result = get_task_root(task, path);
Eric W. Biederman99f89552006-06-26 00:25:55 -0700207 put_task_struct(task);
208 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700209 return result;
210}
211
Linus Torvaldsd26d0cd92019-07-13 14:27:14 -0700212/*
213 * If the user used setproctitle(), we just get the string from
214 * user space at arg_start, and limit it to a maximum of one page.
215 */
216static ssize_t get_mm_proctitle(struct mm_struct *mm, char __user *buf,
217 size_t count, unsigned long pos,
218 unsigned long arg_start)
219{
220 char *page;
221 int ret, got;
222
223 if (pos >= PAGE_SIZE)
224 return 0;
225
226 page = (char *)__get_free_page(GFP_KERNEL);
227 if (!page)
228 return -ENOMEM;
229
230 ret = 0;
231 got = access_remote_vm(mm, arg_start, page, PAGE_SIZE, FOLL_ANON);
232 if (got > 0) {
233 int len = strnlen(page, got);
234
235 /* Include the NUL character if it was found */
236 if (len < got)
237 len++;
238
239 if (len > pos) {
240 len -= pos;
241 if (len > count)
242 len = count;
243 len -= copy_to_user(buf, page+pos, len);
244 if (!len)
245 len = -EFAULT;
246 ret = len;
247 }
248 }
249 free_page((unsigned long)page);
250 return ret;
251}
252
Linus Torvaldse4b4e442018-05-17 13:04:17 -0700253static ssize_t get_mm_cmdline(struct mm_struct *mm, char __user *buf,
Linus Torvalds5ab82712018-05-17 15:17:33 -0700254 size_t count, loff_t *ppos)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700255{
Linus Torvaldsd26d0cd92019-07-13 14:27:14 -0700256 unsigned long arg_start, arg_end, env_start, env_end;
Linus Torvalds5ab82712018-05-17 15:17:33 -0700257 unsigned long pos, len;
Linus Torvaldsd26d0cd92019-07-13 14:27:14 -0700258 char *page, c;
Alexey Dobriyanc2c0bb42015-06-25 15:00:54 -0700259
Alexey Dobriyanc2c0bb42015-06-25 15:00:54 -0700260 /* Check if process spawned far enough to have cmdline. */
Linus Torvaldse4b4e442018-05-17 13:04:17 -0700261 if (!mm->env_end)
262 return 0;
Alexey Dobriyanc2c0bb42015-06-25 15:00:54 -0700263
Yang Shi88aa7cc2018-06-07 17:05:28 -0700264 spin_lock(&mm->arg_lock);
Alexey Dobriyanc2c0bb42015-06-25 15:00:54 -0700265 arg_start = mm->arg_start;
266 arg_end = mm->arg_end;
Linus Torvaldsd26d0cd92019-07-13 14:27:14 -0700267 env_start = mm->env_start;
268 env_end = mm->env_end;
Yang Shi88aa7cc2018-06-07 17:05:28 -0700269 spin_unlock(&mm->arg_lock);
Alexey Dobriyanc2c0bb42015-06-25 15:00:54 -0700270
Linus Torvalds5ab82712018-05-17 15:17:33 -0700271 if (arg_start >= arg_end)
272 return 0;
Alexey Dobriyan6a6cbe72018-06-07 17:09:59 -0700273
Linus Torvaldsd26d0cd92019-07-13 14:27:14 -0700274 /*
275 * We allow setproctitle() to overwrite the argument
276 * strings, and overflow past the original end. But
277 * only when it overflows into the environment area.
278 */
279 if (env_start != arg_end || env_end < env_start)
280 env_start = env_end = arg_end;
281 len = env_end - arg_start;
282
Linus Torvalds5ab82712018-05-17 15:17:33 -0700283 /* We're not going to care if "*ppos" has high bits set */
Linus Torvaldsd26d0cd92019-07-13 14:27:14 -0700284 pos = *ppos;
285 if (pos >= len)
286 return 0;
287 if (count > len - pos)
288 count = len - pos;
289 if (!count)
290 return 0;
291
292 /*
293 * Magical special case: if the argv[] end byte is not
294 * zero, the user has overwritten it with setproctitle(3).
295 *
296 * Possible future enhancement: do this only once when
297 * pos is 0, and set a flag in the 'struct file'.
298 */
299 if (access_remote_vm(mm, arg_end-1, &c, 1, FOLL_ANON) == 1 && c)
300 return get_mm_proctitle(mm, buf, count, pos, arg_start);
301
302 /*
303 * For the non-setproctitle() case we limit things strictly
304 * to the [arg_start, arg_end[ range.
305 */
306 pos += arg_start;
Linus Torvalds3d712542019-07-13 13:40:13 -0700307 if (pos < arg_start || pos >= arg_end)
Linus Torvalds5ab82712018-05-17 15:17:33 -0700308 return 0;
Linus Torvalds3d712542019-07-13 13:40:13 -0700309 if (count > arg_end - pos)
310 count = arg_end - pos;
Alexey Dobriyanc2c0bb42015-06-25 15:00:54 -0700311
Linus Torvalds5ab82712018-05-17 15:17:33 -0700312 page = (char *)__get_free_page(GFP_KERNEL);
313 if (!page)
314 return -ENOMEM;
Alexey Dobriyanc2c0bb42015-06-25 15:00:54 -0700315
Linus Torvalds5ab82712018-05-17 15:17:33 -0700316 len = 0;
317 while (count) {
318 int got;
319 size_t size = min_t(size_t, PAGE_SIZE, count);
Alexey Dobriyanc2c0bb42015-06-25 15:00:54 -0700320
Linus Torvalds3d712542019-07-13 13:40:13 -0700321 got = access_remote_vm(mm, pos, page, size, FOLL_ANON);
322 if (got <= 0)
Linus Torvalds5ab82712018-05-17 15:17:33 -0700323 break;
Linus Torvalds3d712542019-07-13 13:40:13 -0700324 got -= copy_to_user(buf, page, got);
Linus Torvalds5ab82712018-05-17 15:17:33 -0700325 if (unlikely(!got)) {
326 if (!len)
327 len = -EFAULT;
328 break;
Alexey Dobriyanc2c0bb42015-06-25 15:00:54 -0700329 }
Linus Torvalds5ab82712018-05-17 15:17:33 -0700330 pos += got;
331 buf += got;
332 len += got;
333 count -= got;
Alexey Dobriyan3cb4e162018-06-07 17:10:02 -0700334 }
335
Alexey Dobriyanc2c0bb42015-06-25 15:00:54 -0700336 free_page((unsigned long)page);
Linus Torvalds5ab82712018-05-17 15:17:33 -0700337 return len;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700338}
339
Linus Torvaldse4b4e442018-05-17 13:04:17 -0700340static ssize_t get_task_cmdline(struct task_struct *tsk, char __user *buf,
341 size_t count, loff_t *pos)
342{
343 struct mm_struct *mm;
344 ssize_t ret;
345
346 mm = get_task_mm(tsk);
347 if (!mm)
348 return 0;
349
350 ret = get_mm_cmdline(mm, buf, count, pos);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700351 mmput(mm);
Linus Torvaldse4b4e442018-05-17 13:04:17 -0700352 return ret;
353}
354
355static ssize_t proc_pid_cmdline_read(struct file *file, char __user *buf,
356 size_t count, loff_t *pos)
357{
358 struct task_struct *tsk;
359 ssize_t ret;
360
361 BUG_ON(*pos < 0);
362
363 tsk = get_proc_task(file_inode(file));
364 if (!tsk)
365 return -ESRCH;
366 ret = get_task_cmdline(tsk, buf, count, pos);
367 put_task_struct(tsk);
368 if (ret > 0)
369 *pos += ret;
370 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700371}
372
Alexey Dobriyanc2c0bb42015-06-25 15:00:54 -0700373static const struct file_operations proc_pid_cmdline_ops = {
374 .read = proc_pid_cmdline_read,
375 .llseek = generic_file_llseek,
376};
377
Linus Torvalds1da177e2005-04-16 15:20:36 -0700378#ifdef CONFIG_KALLSYMS
379/*
380 * Provides a wchan file via kallsyms in a proper one-value-per-file format.
381 * Returns the resolved symbol. If that fails, simply return the address.
382 */
Alexey Dobriyanedfcd602014-08-08 14:21:44 -0700383static int proc_pid_wchan(struct seq_file *m, struct pid_namespace *ns,
384 struct pid *pid, struct task_struct *task)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700385{
Alexey Dobriyanffb45122007-05-08 00:28:41 -0700386 unsigned long wchan;
Tejun Heo9281ace2007-07-17 04:03:51 -0700387 char symname[KSYM_NAME_LEN];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700388
Alexey Dobriyan24b2ec22018-04-10 16:31:30 -0700389 if (!ptrace_may_access(task, PTRACE_MODE_READ_FSCREDS))
390 goto print0;
391
Linus Torvalds1da177e2005-04-16 15:20:36 -0700392 wchan = get_wchan(task);
Alexey Dobriyan24b2ec22018-04-10 16:31:30 -0700393 if (wchan && !lookup_symbol_name(wchan, symname)) {
Alexey Dobriyan21dae0a2018-04-10 16:31:34 -0700394 seq_puts(m, symname);
Alexey Dobriyan24b2ec22018-04-10 16:31:30 -0700395 return 0;
396 }
Joe Perches25ce3192015-04-15 16:18:17 -0700397
Alexey Dobriyan24b2ec22018-04-10 16:31:30 -0700398print0:
399 seq_putc(m, '0');
Joe Perches25ce3192015-04-15 16:18:17 -0700400 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700401}
402#endif /* CONFIG_KALLSYMS */
403
Al Viroa9712bc2011-03-23 15:52:50 -0400404static int lock_trace(struct task_struct *task)
405{
406 int err = mutex_lock_killable(&task->signal->cred_guard_mutex);
407 if (err)
408 return err;
Jann Horncaaee622016-01-20 15:00:04 -0800409 if (!ptrace_may_access(task, PTRACE_MODE_ATTACH_FSCREDS)) {
Al Viroa9712bc2011-03-23 15:52:50 -0400410 mutex_unlock(&task->signal->cred_guard_mutex);
411 return -EPERM;
412 }
413 return 0;
414}
415
416static void unlock_trace(struct task_struct *task)
417{
418 mutex_unlock(&task->signal->cred_guard_mutex);
419}
420
Ken Chen2ec220e2008-11-10 11:26:08 +0300421#ifdef CONFIG_STACKTRACE
422
423#define MAX_STACK_TRACE_DEPTH 64
424
425static int proc_pid_stack(struct seq_file *m, struct pid_namespace *ns,
426 struct pid *pid, struct task_struct *task)
427{
Ken Chen2ec220e2008-11-10 11:26:08 +0300428 unsigned long *entries;
Al Viroa9712bc2011-03-23 15:52:50 -0400429 int err;
Ken Chen2ec220e2008-11-10 11:26:08 +0300430
Jann Hornf8a00ce2018-10-05 15:51:58 -0700431 /*
432 * The ability to racily run the kernel stack unwinder on a running task
433 * and then observe the unwinder output is scary; while it is useful for
434 * debugging kernel issues, it can also allow an attacker to leak kernel
435 * stack contents.
436 * Doing this in a manner that is at least safe from races would require
437 * some work to ensure that the remote task can not be scheduled; and
438 * even then, this would still expose the unwinder as local attack
439 * surface.
440 * Therefore, this interface is restricted to root.
441 */
442 if (!file_ns_capable(m->file, &init_user_ns, CAP_SYS_ADMIN))
443 return -EACCES;
444
Kees Cook6da2ec52018-06-12 13:55:00 -0700445 entries = kmalloc_array(MAX_STACK_TRACE_DEPTH, sizeof(*entries),
446 GFP_KERNEL);
Ken Chen2ec220e2008-11-10 11:26:08 +0300447 if (!entries)
448 return -ENOMEM;
449
Al Viroa9712bc2011-03-23 15:52:50 -0400450 err = lock_trace(task);
451 if (!err) {
Thomas Gleixnere988e5e2019-04-25 11:44:58 +0200452 unsigned int i, nr_entries;
Alexey Dobriyan5d008fb2018-06-07 17:10:17 -0700453
Thomas Gleixnere988e5e2019-04-25 11:44:58 +0200454 nr_entries = stack_trace_save_tsk(task, entries,
455 MAX_STACK_TRACE_DEPTH, 0);
Al Viroa9712bc2011-03-23 15:52:50 -0400456
Thomas Gleixnere988e5e2019-04-25 11:44:58 +0200457 for (i = 0; i < nr_entries; i++) {
Linus Torvalds8f5abe82017-11-27 16:45:56 -0800458 seq_printf(m, "[<0>] %pB\n", (void *)entries[i]);
Al Viroa9712bc2011-03-23 15:52:50 -0400459 }
Thomas Gleixnere988e5e2019-04-25 11:44:58 +0200460
Al Viroa9712bc2011-03-23 15:52:50 -0400461 unlock_trace(task);
Ken Chen2ec220e2008-11-10 11:26:08 +0300462 }
463 kfree(entries);
464
Al Viroa9712bc2011-03-23 15:52:50 -0400465 return err;
Ken Chen2ec220e2008-11-10 11:26:08 +0300466}
467#endif
468
Naveen N. Rao5968cec2015-06-30 14:36:03 +0530469#ifdef CONFIG_SCHED_INFO
Linus Torvalds1da177e2005-04-16 15:20:36 -0700470/*
471 * Provides /proc/PID/schedstat
472 */
Alexey Dobriyanf6e826c2014-08-08 14:21:46 -0700473static int proc_pid_schedstat(struct seq_file *m, struct pid_namespace *ns,
474 struct pid *pid, struct task_struct *task)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700475{
Naveen N. Rao5968cec2015-06-30 14:36:03 +0530476 if (unlikely(!sched_info_on()))
Alexey Dobriyan08b55772019-03-05 15:50:35 -0800477 seq_puts(m, "0 0 0\n");
Naveen N. Rao5968cec2015-06-30 14:36:03 +0530478 else
479 seq_printf(m, "%llu %llu %lu\n",
Joe Perches25ce3192015-04-15 16:18:17 -0700480 (unsigned long long)task->se.sum_exec_runtime,
481 (unsigned long long)task->sched_info.run_delay,
482 task->sched_info.pcount);
483
484 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700485}
486#endif
487
Arjan van de Ven97455122008-01-25 21:08:34 +0100488#ifdef CONFIG_LATENCYTOP
489static int lstats_show_proc(struct seq_file *m, void *v)
490{
491 int i;
Hiroshi Shimamoto13d77c32008-02-20 16:53:29 -0800492 struct inode *inode = m->private;
493 struct task_struct *task = get_proc_task(inode);
Arjan van de Ven97455122008-01-25 21:08:34 +0100494
Hiroshi Shimamoto13d77c32008-02-20 16:53:29 -0800495 if (!task)
496 return -ESRCH;
497 seq_puts(m, "Latency Top version : v0.1\n");
Alexey Dobriyanf6d2f582018-08-21 21:54:34 -0700498 for (i = 0; i < LT_SAVECOUNT; i++) {
Joe Perches34e49d42011-01-12 17:00:30 -0800499 struct latency_record *lr = &task->latency_record[i];
500 if (lr->backtrace[0]) {
Arjan van de Ven97455122008-01-25 21:08:34 +0100501 int q;
Joe Perches34e49d42011-01-12 17:00:30 -0800502 seq_printf(m, "%i %li %li",
503 lr->count, lr->time, lr->max);
Arjan van de Ven97455122008-01-25 21:08:34 +0100504 for (q = 0; q < LT_BACKTRACEDEPTH; q++) {
Joe Perches34e49d42011-01-12 17:00:30 -0800505 unsigned long bt = lr->backtrace[q];
Thomas Gleixneraccddc42019-04-10 12:28:08 +0200506
Joe Perches34e49d42011-01-12 17:00:30 -0800507 if (!bt)
Arjan van de Ven97455122008-01-25 21:08:34 +0100508 break;
Joe Perches34e49d42011-01-12 17:00:30 -0800509 seq_printf(m, " %ps", (void *)bt);
Arjan van de Ven97455122008-01-25 21:08:34 +0100510 }
Alexey Dobriyan9d6de122011-01-12 17:00:32 -0800511 seq_putc(m, '\n');
Arjan van de Ven97455122008-01-25 21:08:34 +0100512 }
513
514 }
Hiroshi Shimamoto13d77c32008-02-20 16:53:29 -0800515 put_task_struct(task);
Arjan van de Ven97455122008-01-25 21:08:34 +0100516 return 0;
517}
518
519static int lstats_open(struct inode *inode, struct file *file)
520{
Hiroshi Shimamoto13d77c32008-02-20 16:53:29 -0800521 return single_open(file, lstats_show_proc, inode);
Hiroshi Shimamotod6643d12008-02-14 10:27:00 -0800522}
523
Arjan van de Ven97455122008-01-25 21:08:34 +0100524static ssize_t lstats_write(struct file *file, const char __user *buf,
525 size_t count, loff_t *offs)
526{
Al Viro496ad9a2013-01-23 17:07:38 -0500527 struct task_struct *task = get_proc_task(file_inode(file));
Arjan van de Ven97455122008-01-25 21:08:34 +0100528
Hiroshi Shimamoto13d77c32008-02-20 16:53:29 -0800529 if (!task)
530 return -ESRCH;
Lin Fenge02c9b02019-05-14 15:42:34 -0700531 clear_tsk_latency_tracing(task);
Hiroshi Shimamoto13d77c32008-02-20 16:53:29 -0800532 put_task_struct(task);
Arjan van de Ven97455122008-01-25 21:08:34 +0100533
534 return count;
535}
536
537static const struct file_operations proc_lstats_operations = {
538 .open = lstats_open,
539 .read = seq_read,
540 .write = lstats_write,
541 .llseek = seq_lseek,
Hiroshi Shimamoto13d77c32008-02-20 16:53:29 -0800542 .release = single_release,
Arjan van de Ven97455122008-01-25 21:08:34 +0100543};
544
545#endif
546
Alexey Dobriyan6ba51e32014-08-08 14:21:48 -0700547static int proc_oom_score(struct seq_file *m, struct pid_namespace *ns,
548 struct pid *pid, struct task_struct *task)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700549{
Arun KSca79b0c2018-12-28 00:34:29 -0800550 unsigned long totalpages = totalram_pages() + total_swap_pages;
Oleg Nesterovb95c35e2010-04-01 15:13:57 +0200551 unsigned long points = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700552
Oleg Nesterovef419392016-08-02 14:03:19 -0700553 points = oom_badness(task, NULL, NULL, totalpages) *
554 1000 / totalpages;
Joe Perches25ce3192015-04-15 16:18:17 -0700555 seq_printf(m, "%lu\n", points);
556
557 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700558}
559
Neil Hormand85f50d2007-10-18 23:40:37 -0700560struct limit_names {
Alexey Dobriyancedbcca2014-08-08 14:21:33 -0700561 const char *name;
562 const char *unit;
Neil Hormand85f50d2007-10-18 23:40:37 -0700563};
564
565static const struct limit_names lnames[RLIM_NLIMITS] = {
Kees Cookcff4edb2009-09-22 16:45:32 -0700566 [RLIMIT_CPU] = {"Max cpu time", "seconds"},
Neil Hormand85f50d2007-10-18 23:40:37 -0700567 [RLIMIT_FSIZE] = {"Max file size", "bytes"},
568 [RLIMIT_DATA] = {"Max data size", "bytes"},
569 [RLIMIT_STACK] = {"Max stack size", "bytes"},
570 [RLIMIT_CORE] = {"Max core file size", "bytes"},
571 [RLIMIT_RSS] = {"Max resident set", "bytes"},
572 [RLIMIT_NPROC] = {"Max processes", "processes"},
573 [RLIMIT_NOFILE] = {"Max open files", "files"},
574 [RLIMIT_MEMLOCK] = {"Max locked memory", "bytes"},
575 [RLIMIT_AS] = {"Max address space", "bytes"},
576 [RLIMIT_LOCKS] = {"Max file locks", "locks"},
577 [RLIMIT_SIGPENDING] = {"Max pending signals", "signals"},
578 [RLIMIT_MSGQUEUE] = {"Max msgqueue size", "bytes"},
579 [RLIMIT_NICE] = {"Max nice priority", NULL},
580 [RLIMIT_RTPRIO] = {"Max realtime priority", NULL},
Eugene Teo88081172008-02-23 15:23:52 -0800581 [RLIMIT_RTTIME] = {"Max realtime timeout", "us"},
Neil Hormand85f50d2007-10-18 23:40:37 -0700582};
583
584/* Display limits for a process */
Alexey Dobriyan1c963eb2014-08-08 14:21:37 -0700585static int proc_pid_limits(struct seq_file *m, struct pid_namespace *ns,
586 struct pid *pid, struct task_struct *task)
Neil Hormand85f50d2007-10-18 23:40:37 -0700587{
588 unsigned int i;
Neil Hormand85f50d2007-10-18 23:40:37 -0700589 unsigned long flags;
Neil Hormand85f50d2007-10-18 23:40:37 -0700590
591 struct rlimit rlim[RLIM_NLIMITS];
592
Lai Jiangshana6bebbc2008-10-05 00:51:15 +0400593 if (!lock_task_sighand(task, &flags))
Neil Hormand85f50d2007-10-18 23:40:37 -0700594 return 0;
Neil Hormand85f50d2007-10-18 23:40:37 -0700595 memcpy(rlim, task->signal->rlim, sizeof(struct rlimit) * RLIM_NLIMITS);
596 unlock_task_sighand(task, &flags);
Neil Hormand85f50d2007-10-18 23:40:37 -0700597
598 /*
599 * print the file header
600 */
Alexey Dobriyanafe922c2019-01-03 15:26:09 -0800601 seq_puts(m, "Limit "
602 "Soft Limit "
603 "Hard Limit "
604 "Units \n");
Neil Hormand85f50d2007-10-18 23:40:37 -0700605
606 for (i = 0; i < RLIM_NLIMITS; i++) {
607 if (rlim[i].rlim_cur == RLIM_INFINITY)
Alexey Dobriyan1c963eb2014-08-08 14:21:37 -0700608 seq_printf(m, "%-25s %-20s ",
Joe Perches25ce3192015-04-15 16:18:17 -0700609 lnames[i].name, "unlimited");
Neil Hormand85f50d2007-10-18 23:40:37 -0700610 else
Alexey Dobriyan1c963eb2014-08-08 14:21:37 -0700611 seq_printf(m, "%-25s %-20lu ",
Joe Perches25ce3192015-04-15 16:18:17 -0700612 lnames[i].name, rlim[i].rlim_cur);
Neil Hormand85f50d2007-10-18 23:40:37 -0700613
614 if (rlim[i].rlim_max == RLIM_INFINITY)
Alexey Dobriyan1c963eb2014-08-08 14:21:37 -0700615 seq_printf(m, "%-20s ", "unlimited");
Neil Hormand85f50d2007-10-18 23:40:37 -0700616 else
Alexey Dobriyan1c963eb2014-08-08 14:21:37 -0700617 seq_printf(m, "%-20lu ", rlim[i].rlim_max);
Neil Hormand85f50d2007-10-18 23:40:37 -0700618
619 if (lnames[i].unit)
Alexey Dobriyan1c963eb2014-08-08 14:21:37 -0700620 seq_printf(m, "%-10s\n", lnames[i].unit);
Neil Hormand85f50d2007-10-18 23:40:37 -0700621 else
Alexey Dobriyan1c963eb2014-08-08 14:21:37 -0700622 seq_putc(m, '\n');
Neil Hormand85f50d2007-10-18 23:40:37 -0700623 }
624
Alexey Dobriyan1c963eb2014-08-08 14:21:37 -0700625 return 0;
Neil Hormand85f50d2007-10-18 23:40:37 -0700626}
627
Roland McGrathebcb6732008-07-25 19:46:00 -0700628#ifdef CONFIG_HAVE_ARCH_TRACEHOOK
Alexey Dobriyan09d93bd2014-08-08 14:21:39 -0700629static int proc_pid_syscall(struct seq_file *m, struct pid_namespace *ns,
630 struct pid *pid, struct task_struct *task)
Roland McGrathebcb6732008-07-25 19:46:00 -0700631{
Steven Rostedt (Red Hat)631b7ab2016-11-07 16:26:35 -0500632 struct syscall_info info;
633 u64 *args = &info.data.args[0];
Joe Perches25ce3192015-04-15 16:18:17 -0700634 int res;
635
636 res = lock_trace(task);
Al Viroa9712bc2011-03-23 15:52:50 -0400637 if (res)
638 return res;
Roland McGrathebcb6732008-07-25 19:46:00 -0700639
Steven Rostedt (Red Hat)631b7ab2016-11-07 16:26:35 -0500640 if (task_current_syscall(task, &info))
Alexey Dobriyan09d93bd2014-08-08 14:21:39 -0700641 seq_puts(m, "running\n");
Steven Rostedt (Red Hat)631b7ab2016-11-07 16:26:35 -0500642 else if (info.data.nr < 0)
643 seq_printf(m, "%d 0x%llx 0x%llx\n",
644 info.data.nr, info.sp, info.data.instruction_pointer);
Al Viroa9712bc2011-03-23 15:52:50 -0400645 else
Alexey Dobriyan09d93bd2014-08-08 14:21:39 -0700646 seq_printf(m,
Steven Rostedt (Red Hat)631b7ab2016-11-07 16:26:35 -0500647 "%d 0x%llx 0x%llx 0x%llx 0x%llx 0x%llx 0x%llx 0x%llx 0x%llx\n",
648 info.data.nr,
Roland McGrathebcb6732008-07-25 19:46:00 -0700649 args[0], args[1], args[2], args[3], args[4], args[5],
Steven Rostedt (Red Hat)631b7ab2016-11-07 16:26:35 -0500650 info.sp, info.data.instruction_pointer);
Al Viroa9712bc2011-03-23 15:52:50 -0400651 unlock_trace(task);
Joe Perches25ce3192015-04-15 16:18:17 -0700652
653 return 0;
Roland McGrathebcb6732008-07-25 19:46:00 -0700654}
655#endif /* CONFIG_HAVE_ARCH_TRACEHOOK */
656
Linus Torvalds1da177e2005-04-16 15:20:36 -0700657/************************************************************************/
658/* Here the fs part begins */
659/************************************************************************/
660
661/* permission checks */
Eric W. Biederman778c1142006-06-26 00:25:58 -0700662static int proc_fd_access_allowed(struct inode *inode)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700663{
Eric W. Biederman778c1142006-06-26 00:25:58 -0700664 struct task_struct *task;
665 int allowed = 0;
Eric W. Biedermandf26c402006-06-26 00:25:59 -0700666 /* Allow access to a task's file descriptors if it is us or we
667 * may use ptrace attach to the process and find out that
668 * information.
Eric W. Biederman778c1142006-06-26 00:25:58 -0700669 */
670 task = get_proc_task(inode);
Eric W. Biedermandf26c402006-06-26 00:25:59 -0700671 if (task) {
Jann Horncaaee622016-01-20 15:00:04 -0800672 allowed = ptrace_may_access(task, PTRACE_MODE_READ_FSCREDS);
Eric W. Biederman778c1142006-06-26 00:25:58 -0700673 put_task_struct(task);
Eric W. Biedermandf26c402006-06-26 00:25:59 -0700674 }
Eric W. Biederman778c1142006-06-26 00:25:58 -0700675 return allowed;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700676}
677
Eric W. Biederman6b4e3062010-03-07 16:41:34 -0800678int proc_setattr(struct dentry *dentry, struct iattr *attr)
Linus Torvalds6d76fa52006-07-15 12:26:45 -0700679{
680 int error;
David Howells2b0143b2015-03-17 22:25:59 +0000681 struct inode *inode = d_inode(dentry);
Linus Torvalds6d76fa52006-07-15 12:26:45 -0700682
683 if (attr->ia_valid & ATTR_MODE)
684 return -EPERM;
685
Jan Kara31051c82016-05-26 16:55:18 +0200686 error = setattr_prepare(dentry, attr);
Christoph Hellwig10257742010-06-04 11:30:02 +0200687 if (error)
688 return error;
689
Christoph Hellwig10257742010-06-04 11:30:02 +0200690 setattr_copy(inode, attr);
691 mark_inode_dirty(inode);
692 return 0;
Linus Torvalds6d76fa52006-07-15 12:26:45 -0700693}
694
Vasiliy Kulikov04996802012-01-10 15:11:31 -0800695/*
696 * May current process learn task's sched/cmdline info (for hide_pid_min=1)
697 * or euid/egid (for hide_pid_min=2)?
698 */
699static bool has_pid_permissions(struct pid_namespace *pid,
700 struct task_struct *task,
701 int hide_pid_min)
702{
703 if (pid->hide_pid < hide_pid_min)
704 return true;
705 if (in_group_p(pid->pid_gid))
706 return true;
Jann Horncaaee622016-01-20 15:00:04 -0800707 return ptrace_may_access(task, PTRACE_MODE_READ_FSCREDS);
Vasiliy Kulikov04996802012-01-10 15:11:31 -0800708}
709
710
711static int proc_pid_permission(struct inode *inode, int mask)
712{
Christoph Hellwig76f668b2018-05-16 07:19:01 +0200713 struct pid_namespace *pid = proc_pid_ns(inode);
Vasiliy Kulikov04996802012-01-10 15:11:31 -0800714 struct task_struct *task;
715 bool has_perms;
716
717 task = get_proc_task(inode);
Xiaotian Fenga2ef9902012-01-12 17:17:08 -0800718 if (!task)
719 return -ESRCH;
Lafcadio Wluiki796f5712017-02-24 15:00:23 -0800720 has_perms = has_pid_permissions(pid, task, HIDEPID_NO_ACCESS);
Vasiliy Kulikov04996802012-01-10 15:11:31 -0800721 put_task_struct(task);
722
723 if (!has_perms) {
Lafcadio Wluiki796f5712017-02-24 15:00:23 -0800724 if (pid->hide_pid == HIDEPID_INVISIBLE) {
Vasiliy Kulikov04996802012-01-10 15:11:31 -0800725 /*
726 * Let's make getdents(), stat(), and open()
727 * consistent with each other. If a process
728 * may not stat() a file, it shouldn't be seen
729 * in procfs at all.
730 */
731 return -ENOENT;
732 }
733
734 return -EPERM;
735 }
736 return generic_permission(inode, mask);
737}
738
739
740
Arjan van de Venc5ef1c42007-02-12 00:55:40 -0800741static const struct inode_operations proc_def_inode_operations = {
Linus Torvalds6d76fa52006-07-15 12:26:45 -0700742 .setattr = proc_setattr,
743};
744
Eric W. Biedermanbe614082008-02-08 04:18:30 -0800745static int proc_single_show(struct seq_file *m, void *v)
746{
747 struct inode *inode = m->private;
Christoph Hellwig76f668b2018-05-16 07:19:01 +0200748 struct pid_namespace *ns = proc_pid_ns(inode);
749 struct pid *pid = proc_pid(inode);
Eric W. Biedermanbe614082008-02-08 04:18:30 -0800750 struct task_struct *task;
751 int ret;
752
Eric W. Biedermanbe614082008-02-08 04:18:30 -0800753 task = get_pid_task(pid, PIDTYPE_PID);
754 if (!task)
755 return -ESRCH;
756
757 ret = PROC_I(inode)->op.proc_show(m, ns, pid, task);
758
759 put_task_struct(task);
760 return ret;
761}
762
763static int proc_single_open(struct inode *inode, struct file *filp)
764{
Jovi Zhangc6a34052011-01-12 17:00:34 -0800765 return single_open(filp, proc_single_show, inode);
Eric W. Biedermanbe614082008-02-08 04:18:30 -0800766}
767
768static const struct file_operations proc_single_file_operations = {
769 .open = proc_single_open,
770 .read = seq_read,
771 .llseek = seq_lseek,
772 .release = single_release,
773};
774
Oleg Nesterov5381e162014-10-09 15:25:24 -0700775
776struct mm_struct *proc_mem_open(struct inode *inode, unsigned int mode)
777{
778 struct task_struct *task = get_proc_task(inode);
779 struct mm_struct *mm = ERR_PTR(-ESRCH);
780
781 if (task) {
Jann Horncaaee622016-01-20 15:00:04 -0800782 mm = mm_access(task, mode | PTRACE_MODE_FSCREDS);
Oleg Nesterov5381e162014-10-09 15:25:24 -0700783 put_task_struct(task);
784
785 if (!IS_ERR_OR_NULL(mm)) {
786 /* ensure this mm_struct can't be freed */
Vegard Nossumf1f10072017-02-27 14:30:07 -0800787 mmgrab(mm);
Oleg Nesterov5381e162014-10-09 15:25:24 -0700788 /* but do not pin its memory */
789 mmput(mm);
790 }
791 }
792
793 return mm;
794}
795
Cong Wangb409e572012-05-31 16:26:17 -0700796static int __mem_open(struct inode *inode, struct file *file, unsigned int mode)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700797{
Oleg Nesterov5381e162014-10-09 15:25:24 -0700798 struct mm_struct *mm = proc_mem_open(inode, mode);
Linus Torvaldse2683372012-01-17 15:21:19 -0800799
800 if (IS_ERR(mm))
801 return PTR_ERR(mm);
802
Linus Torvaldse2683372012-01-17 15:21:19 -0800803 file->private_data = mm;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700804 return 0;
805}
806
Cong Wangb409e572012-05-31 16:26:17 -0700807static int mem_open(struct inode *inode, struct file *file)
808{
Djalal Harounibc452b42012-07-30 14:42:28 -0700809 int ret = __mem_open(inode, file, PTRACE_MODE_ATTACH);
810
811 /* OK to pass negative loff_t, we can catch out-of-range */
812 file->f_mode |= FMODE_UNSIGNED_OFFSET;
813
814 return ret;
Cong Wangb409e572012-05-31 16:26:17 -0700815}
816
Oleg Nesterov572d34b2012-01-31 17:14:54 +0100817static ssize_t mem_rw(struct file *file, char __user *buf,
818 size_t count, loff_t *ppos, int write)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700819{
Linus Torvaldse2683372012-01-17 15:21:19 -0800820 struct mm_struct *mm = file->private_data;
Oleg Nesterov572d34b2012-01-31 17:14:54 +0100821 unsigned long addr = *ppos;
822 ssize_t copied;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700823 char *page;
Linus Torvalds272ddc82016-10-24 19:00:44 -0700824 unsigned int flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700825
Linus Torvaldse2683372012-01-17 15:21:19 -0800826 if (!mm)
827 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700828
Michal Hocko0ee931c2017-09-13 16:28:29 -0700829 page = (char *)__get_free_page(GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700830 if (!page)
Linus Torvaldse2683372012-01-17 15:21:19 -0800831 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700832
Frederik Deweerdtf7ca54f2006-09-29 02:01:02 -0700833 copied = 0;
Vegard Nossum388f7932017-02-27 14:30:13 -0800834 if (!mmget_not_zero(mm))
Oleg Nesterov6d08f2c2012-01-31 17:15:11 +0100835 goto free;
836
Linus Torvaldsf511c0b2017-05-30 12:38:59 -0700837 flags = FOLL_FORCE | (write ? FOLL_WRITE : 0);
Lorenzo Stoakes6347e8d2016-10-13 01:20:19 +0100838
Linus Torvalds1da177e2005-04-16 15:20:36 -0700839 while (count > 0) {
Oleg Nesterov572d34b2012-01-31 17:14:54 +0100840 int this_len = min_t(int, count, PAGE_SIZE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700841
Oleg Nesterov572d34b2012-01-31 17:14:54 +0100842 if (write && copy_from_user(page, buf, this_len)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700843 copied = -EFAULT;
844 break;
845 }
Oleg Nesterov572d34b2012-01-31 17:14:54 +0100846
Lorenzo Stoakes6347e8d2016-10-13 01:20:19 +0100847 this_len = access_remote_vm(mm, addr, page, this_len, flags);
Oleg Nesterov572d34b2012-01-31 17:14:54 +0100848 if (!this_len) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700849 if (!copied)
850 copied = -EIO;
851 break;
852 }
Oleg Nesterov572d34b2012-01-31 17:14:54 +0100853
854 if (!write && copy_to_user(buf, page, this_len)) {
855 copied = -EFAULT;
856 break;
857 }
858
859 buf += this_len;
860 addr += this_len;
861 copied += this_len;
862 count -= this_len;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700863 }
Oleg Nesterov572d34b2012-01-31 17:14:54 +0100864 *ppos = addr;
KOSAKI Motohiro30cd8902011-05-26 16:25:52 -0700865
Oleg Nesterov6d08f2c2012-01-31 17:15:11 +0100866 mmput(mm);
867free:
KOSAKI Motohiro30cd8902011-05-26 16:25:52 -0700868 free_page((unsigned long) page);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700869 return copied;
870}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700871
Oleg Nesterov572d34b2012-01-31 17:14:54 +0100872static ssize_t mem_read(struct file *file, char __user *buf,
873 size_t count, loff_t *ppos)
874{
875 return mem_rw(file, buf, count, ppos, 0);
876}
877
878static ssize_t mem_write(struct file *file, const char __user *buf,
879 size_t count, loff_t *ppos)
880{
881 return mem_rw(file, (char __user*)buf, count, ppos, 1);
882}
883
Matt Mackall85863e42008-02-04 22:29:04 -0800884loff_t mem_lseek(struct file *file, loff_t offset, int orig)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700885{
886 switch (orig) {
887 case 0:
888 file->f_pos = offset;
889 break;
890 case 1:
891 file->f_pos += offset;
892 break;
893 default:
894 return -EINVAL;
895 }
896 force_successful_syscall_return();
897 return file->f_pos;
898}
899
Linus Torvaldse2683372012-01-17 15:21:19 -0800900static int mem_release(struct inode *inode, struct file *file)
901{
902 struct mm_struct *mm = file->private_data;
Oleg Nesterov71879d32012-01-31 17:14:38 +0100903 if (mm)
Oleg Nesterov6d08f2c2012-01-31 17:15:11 +0100904 mmdrop(mm);
Linus Torvaldse2683372012-01-17 15:21:19 -0800905 return 0;
906}
907
Arjan van de Ven00977a52007-02-12 00:55:34 -0800908static const struct file_operations proc_mem_operations = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700909 .llseek = mem_lseek,
910 .read = mem_read,
911 .write = mem_write,
912 .open = mem_open,
Linus Torvaldse2683372012-01-17 15:21:19 -0800913 .release = mem_release,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700914};
915
Cong Wangb409e572012-05-31 16:26:17 -0700916static int environ_open(struct inode *inode, struct file *file)
917{
918 return __mem_open(inode, file, PTRACE_MODE_READ);
919}
920
James Pearson315e28c2007-10-16 23:30:17 -0700921static ssize_t environ_read(struct file *file, char __user *buf,
922 size_t count, loff_t *ppos)
923{
James Pearson315e28c2007-10-16 23:30:17 -0700924 char *page;
925 unsigned long src = *ppos;
Cong Wangb409e572012-05-31 16:26:17 -0700926 int ret = 0;
927 struct mm_struct *mm = file->private_data;
Mateusz Guzika3b609e2016-01-20 15:01:05 -0800928 unsigned long env_start, env_end;
James Pearson315e28c2007-10-16 23:30:17 -0700929
Mathias Krause8148a732016-05-05 16:22:26 -0700930 /* Ensure the process spawned far enough to have an environment. */
931 if (!mm || !mm->env_end)
Cong Wangb409e572012-05-31 16:26:17 -0700932 return 0;
James Pearson315e28c2007-10-16 23:30:17 -0700933
Michal Hocko0ee931c2017-09-13 16:28:29 -0700934 page = (char *)__get_free_page(GFP_KERNEL);
James Pearson315e28c2007-10-16 23:30:17 -0700935 if (!page)
Cong Wangb409e572012-05-31 16:26:17 -0700936 return -ENOMEM;
James Pearson315e28c2007-10-16 23:30:17 -0700937
Al Virod6f64b82011-02-15 22:26:01 -0500938 ret = 0;
Vegard Nossum388f7932017-02-27 14:30:13 -0800939 if (!mmget_not_zero(mm))
Cong Wangb409e572012-05-31 16:26:17 -0700940 goto free;
Mateusz Guzika3b609e2016-01-20 15:01:05 -0800941
Yang Shi88aa7cc2018-06-07 17:05:28 -0700942 spin_lock(&mm->arg_lock);
Mateusz Guzika3b609e2016-01-20 15:01:05 -0800943 env_start = mm->env_start;
944 env_end = mm->env_end;
Yang Shi88aa7cc2018-06-07 17:05:28 -0700945 spin_unlock(&mm->arg_lock);
Mateusz Guzika3b609e2016-01-20 15:01:05 -0800946
James Pearson315e28c2007-10-16 23:30:17 -0700947 while (count > 0) {
Djalal Harounie8905ec2012-07-30 14:42:26 -0700948 size_t this_len, max_len;
949 int retval;
950
Mateusz Guzika3b609e2016-01-20 15:01:05 -0800951 if (src >= (env_end - env_start))
Djalal Harounie8905ec2012-07-30 14:42:26 -0700952 break;
James Pearson315e28c2007-10-16 23:30:17 -0700953
Mateusz Guzika3b609e2016-01-20 15:01:05 -0800954 this_len = env_end - (env_start + src);
James Pearson315e28c2007-10-16 23:30:17 -0700955
Djalal Harounie8905ec2012-07-30 14:42:26 -0700956 max_len = min_t(size_t, PAGE_SIZE, count);
957 this_len = min(max_len, this_len);
James Pearson315e28c2007-10-16 23:30:17 -0700958
Willy Tarreau7f7ccc22018-05-11 08:11:44 +0200959 retval = access_remote_vm(mm, (env_start + src), page, this_len, FOLL_ANON);
James Pearson315e28c2007-10-16 23:30:17 -0700960
961 if (retval <= 0) {
962 ret = retval;
963 break;
964 }
965
966 if (copy_to_user(buf, page, retval)) {
967 ret = -EFAULT;
968 break;
969 }
970
971 ret += retval;
972 src += retval;
973 buf += retval;
974 count -= retval;
975 }
976 *ppos = src;
James Pearson315e28c2007-10-16 23:30:17 -0700977 mmput(mm);
Cong Wangb409e572012-05-31 16:26:17 -0700978
979free:
James Pearson315e28c2007-10-16 23:30:17 -0700980 free_page((unsigned long) page);
James Pearson315e28c2007-10-16 23:30:17 -0700981 return ret;
982}
983
984static const struct file_operations proc_environ_operations = {
Cong Wangb409e572012-05-31 16:26:17 -0700985 .open = environ_open,
James Pearson315e28c2007-10-16 23:30:17 -0700986 .read = environ_read,
Arnd Bergmann87df8422010-03-17 23:06:02 +0100987 .llseek = generic_file_llseek,
Cong Wangb409e572012-05-31 16:26:17 -0700988 .release = mem_release,
James Pearson315e28c2007-10-16 23:30:17 -0700989};
990
Al Viroc5317162016-10-05 18:43:43 -0400991static int auxv_open(struct inode *inode, struct file *file)
992{
993 return __mem_open(inode, file, PTRACE_MODE_READ_FSCREDS);
994}
995
996static ssize_t auxv_read(struct file *file, char __user *buf,
997 size_t count, loff_t *ppos)
998{
999 struct mm_struct *mm = file->private_data;
1000 unsigned int nwords = 0;
Leon Yu06b28492016-10-27 17:46:50 -07001001
1002 if (!mm)
1003 return 0;
Al Viroc5317162016-10-05 18:43:43 -04001004 do {
1005 nwords += 2;
1006 } while (mm->saved_auxv[nwords - 2] != 0); /* AT_NULL */
1007 return simple_read_from_buffer(buf, count, ppos, mm->saved_auxv,
1008 nwords * sizeof(mm->saved_auxv[0]));
1009}
1010
1011static const struct file_operations proc_auxv_operations = {
1012 .open = auxv_open,
1013 .read = auxv_read,
1014 .llseek = generic_file_llseek,
1015 .release = mem_release,
1016};
1017
David Rientjesfa0cbbf2012-11-12 17:53:04 -08001018static ssize_t oom_adj_read(struct file *file, char __user *buf, size_t count,
1019 loff_t *ppos)
1020{
Al Viro496ad9a2013-01-23 17:07:38 -05001021 struct task_struct *task = get_proc_task(file_inode(file));
David Rientjesfa0cbbf2012-11-12 17:53:04 -08001022 char buffer[PROC_NUMBUF];
1023 int oom_adj = OOM_ADJUST_MIN;
1024 size_t len;
David Rientjesfa0cbbf2012-11-12 17:53:04 -08001025
1026 if (!task)
1027 return -ESRCH;
Michal Hockof913da52016-07-28 15:44:37 -07001028 if (task->signal->oom_score_adj == OOM_SCORE_ADJ_MAX)
1029 oom_adj = OOM_ADJUST_MAX;
1030 else
1031 oom_adj = (task->signal->oom_score_adj * -OOM_DISABLE) /
1032 OOM_SCORE_ADJ_MAX;
David Rientjesfa0cbbf2012-11-12 17:53:04 -08001033 put_task_struct(task);
1034 len = snprintf(buffer, sizeof(buffer), "%d\n", oom_adj);
1035 return simple_read_from_buffer(buf, count, ppos, buffer, len);
1036}
1037
Michal Hocko1d5f0ac2016-07-28 15:44:40 -07001038static int __set_oom_adj(struct file *file, int oom_adj, bool legacy)
1039{
1040 static DEFINE_MUTEX(oom_adj_mutex);
Michal Hocko44a70ade2016-07-28 15:44:43 -07001041 struct mm_struct *mm = NULL;
Michal Hocko1d5f0ac2016-07-28 15:44:40 -07001042 struct task_struct *task;
1043 int err = 0;
1044
1045 task = get_proc_task(file_inode(file));
1046 if (!task)
1047 return -ESRCH;
1048
1049 mutex_lock(&oom_adj_mutex);
1050 if (legacy) {
1051 if (oom_adj < task->signal->oom_score_adj &&
1052 !capable(CAP_SYS_RESOURCE)) {
1053 err = -EACCES;
1054 goto err_unlock;
1055 }
1056 /*
1057 * /proc/pid/oom_adj is provided for legacy purposes, ask users to use
1058 * /proc/pid/oom_score_adj instead.
1059 */
1060 pr_warn_once("%s (%d): /proc/%d/oom_adj is deprecated, please use /proc/%d/oom_score_adj instead.\n",
1061 current->comm, task_pid_nr(current), task_pid_nr(task),
1062 task_pid_nr(task));
1063 } else {
1064 if ((short)oom_adj < task->signal->oom_score_adj_min &&
1065 !capable(CAP_SYS_RESOURCE)) {
1066 err = -EACCES;
1067 goto err_unlock;
1068 }
1069 }
1070
Michal Hocko44a70ade2016-07-28 15:44:43 -07001071 /*
1072 * Make sure we will check other processes sharing the mm if this is
1073 * not vfrok which wants its own oom_score_adj.
1074 * pin the mm so it doesn't go away and get reused after task_unlock
1075 */
1076 if (!task->vfork_done) {
1077 struct task_struct *p = find_lock_task_mm(task);
1078
1079 if (p) {
1080 if (atomic_read(&p->mm->mm_users) > 1) {
1081 mm = p->mm;
Vegard Nossumf1f10072017-02-27 14:30:07 -08001082 mmgrab(mm);
Michal Hocko44a70ade2016-07-28 15:44:43 -07001083 }
1084 task_unlock(p);
1085 }
1086 }
1087
Michal Hocko1d5f0ac2016-07-28 15:44:40 -07001088 task->signal->oom_score_adj = oom_adj;
1089 if (!legacy && has_capability_noaudit(current, CAP_SYS_RESOURCE))
1090 task->signal->oom_score_adj_min = (short)oom_adj;
1091 trace_oom_score_adj_update(task);
Michal Hocko44a70ade2016-07-28 15:44:43 -07001092
1093 if (mm) {
1094 struct task_struct *p;
1095
1096 rcu_read_lock();
1097 for_each_process(p) {
1098 if (same_thread_group(task, p))
1099 continue;
1100
1101 /* do not touch kernel threads or the global init */
1102 if (p->flags & PF_KTHREAD || is_global_init(p))
1103 continue;
1104
1105 task_lock(p);
1106 if (!p->vfork_done && process_shares_mm(p, mm)) {
Michal Hocko44a70ade2016-07-28 15:44:43 -07001107 p->signal->oom_score_adj = oom_adj;
1108 if (!legacy && has_capability_noaudit(current, CAP_SYS_RESOURCE))
1109 p->signal->oom_score_adj_min = (short)oom_adj;
1110 }
1111 task_unlock(p);
1112 }
1113 rcu_read_unlock();
1114 mmdrop(mm);
1115 }
Michal Hocko1d5f0ac2016-07-28 15:44:40 -07001116err_unlock:
1117 mutex_unlock(&oom_adj_mutex);
1118 put_task_struct(task);
1119 return err;
1120}
Michal Hockof913da52016-07-28 15:44:37 -07001121
David Rientjesb72bdfa2015-11-05 18:50:32 -08001122/*
1123 * /proc/pid/oom_adj exists solely for backwards compatibility with previous
1124 * kernels. The effective policy is defined by oom_score_adj, which has a
1125 * different scale: oom_adj grew exponentially and oom_score_adj grows linearly.
1126 * Values written to oom_adj are simply mapped linearly to oom_score_adj.
1127 * Processes that become oom disabled via oom_adj will still be oom disabled
1128 * with this implementation.
1129 *
1130 * oom_adj cannot be removed since existing userspace binaries use it.
1131 */
David Rientjesfa0cbbf2012-11-12 17:53:04 -08001132static ssize_t oom_adj_write(struct file *file, const char __user *buf,
1133 size_t count, loff_t *ppos)
1134{
David Rientjesfa0cbbf2012-11-12 17:53:04 -08001135 char buffer[PROC_NUMBUF];
1136 int oom_adj;
David Rientjesfa0cbbf2012-11-12 17:53:04 -08001137 int err;
1138
1139 memset(buffer, 0, sizeof(buffer));
1140 if (count > sizeof(buffer) - 1)
1141 count = sizeof(buffer) - 1;
1142 if (copy_from_user(buffer, buf, count)) {
1143 err = -EFAULT;
1144 goto out;
1145 }
1146
1147 err = kstrtoint(strstrip(buffer), 0, &oom_adj);
1148 if (err)
1149 goto out;
1150 if ((oom_adj < OOM_ADJUST_MIN || oom_adj > OOM_ADJUST_MAX) &&
1151 oom_adj != OOM_DISABLE) {
1152 err = -EINVAL;
1153 goto out;
1154 }
1155
David Rientjesfa0cbbf2012-11-12 17:53:04 -08001156 /*
1157 * Scale /proc/pid/oom_score_adj appropriately ensuring that a maximum
1158 * value is always attainable.
1159 */
1160 if (oom_adj == OOM_ADJUST_MAX)
1161 oom_adj = OOM_SCORE_ADJ_MAX;
1162 else
1163 oom_adj = (oom_adj * OOM_SCORE_ADJ_MAX) / -OOM_DISABLE;
1164
Michal Hocko1d5f0ac2016-07-28 15:44:40 -07001165 err = __set_oom_adj(file, oom_adj, true);
David Rientjesfa0cbbf2012-11-12 17:53:04 -08001166out:
1167 return err < 0 ? err : count;
1168}
1169
1170static const struct file_operations proc_oom_adj_operations = {
1171 .read = oom_adj_read,
1172 .write = oom_adj_write,
1173 .llseek = generic_file_llseek,
1174};
1175
David Rientjesa63d83f2010-08-09 17:19:46 -07001176static ssize_t oom_score_adj_read(struct file *file, char __user *buf,
1177 size_t count, loff_t *ppos)
1178{
Al Viro496ad9a2013-01-23 17:07:38 -05001179 struct task_struct *task = get_proc_task(file_inode(file));
David Rientjesa63d83f2010-08-09 17:19:46 -07001180 char buffer[PROC_NUMBUF];
David Rientjesa9c58b902012-12-11 16:02:54 -08001181 short oom_score_adj = OOM_SCORE_ADJ_MIN;
David Rientjesa63d83f2010-08-09 17:19:46 -07001182 size_t len;
1183
1184 if (!task)
1185 return -ESRCH;
Michal Hockof913da52016-07-28 15:44:37 -07001186 oom_score_adj = task->signal->oom_score_adj;
David Rientjesa63d83f2010-08-09 17:19:46 -07001187 put_task_struct(task);
David Rientjesa9c58b902012-12-11 16:02:54 -08001188 len = snprintf(buffer, sizeof(buffer), "%hd\n", oom_score_adj);
David Rientjesa63d83f2010-08-09 17:19:46 -07001189 return simple_read_from_buffer(buf, count, ppos, buffer, len);
1190}
1191
1192static ssize_t oom_score_adj_write(struct file *file, const char __user *buf,
1193 size_t count, loff_t *ppos)
1194{
David Rientjesa63d83f2010-08-09 17:19:46 -07001195 char buffer[PROC_NUMBUF];
Alexey Dobriyan0a8cb8e2011-05-26 16:25:50 -07001196 int oom_score_adj;
David Rientjesa63d83f2010-08-09 17:19:46 -07001197 int err;
1198
1199 memset(buffer, 0, sizeof(buffer));
1200 if (count > sizeof(buffer) - 1)
1201 count = sizeof(buffer) - 1;
David Rientjes723548b2010-10-26 14:21:25 -07001202 if (copy_from_user(buffer, buf, count)) {
1203 err = -EFAULT;
1204 goto out;
1205 }
David Rientjesa63d83f2010-08-09 17:19:46 -07001206
Alexey Dobriyan0a8cb8e2011-05-26 16:25:50 -07001207 err = kstrtoint(strstrip(buffer), 0, &oom_score_adj);
David Rientjesa63d83f2010-08-09 17:19:46 -07001208 if (err)
David Rientjes723548b2010-10-26 14:21:25 -07001209 goto out;
David Rientjesa63d83f2010-08-09 17:19:46 -07001210 if (oom_score_adj < OOM_SCORE_ADJ_MIN ||
David Rientjes723548b2010-10-26 14:21:25 -07001211 oom_score_adj > OOM_SCORE_ADJ_MAX) {
1212 err = -EINVAL;
1213 goto out;
1214 }
David Rientjesa63d83f2010-08-09 17:19:46 -07001215
Michal Hocko1d5f0ac2016-07-28 15:44:40 -07001216 err = __set_oom_adj(file, oom_score_adj, false);
David Rientjes723548b2010-10-26 14:21:25 -07001217out:
1218 return err < 0 ? err : count;
David Rientjesa63d83f2010-08-09 17:19:46 -07001219}
1220
1221static const struct file_operations proc_oom_score_adj_operations = {
1222 .read = oom_score_adj_read,
1223 .write = oom_score_adj_write,
Arnd Bergmann6038f372010-08-15 18:52:59 +02001224 .llseek = default_llseek,
David Rientjesa63d83f2010-08-09 17:19:46 -07001225};
1226
Richard Guy Briggs4b7d2482019-01-22 17:06:39 -05001227#ifdef CONFIG_AUDIT
Alexey Dobriyanb4eb4f72016-10-29 19:04:39 +03001228#define TMPBUFLEN 11
Linus Torvalds1da177e2005-04-16 15:20:36 -07001229static ssize_t proc_loginuid_read(struct file * file, char __user * buf,
1230 size_t count, loff_t *ppos)
1231{
Al Viro496ad9a2013-01-23 17:07:38 -05001232 struct inode * inode = file_inode(file);
Eric W. Biederman99f89552006-06-26 00:25:55 -07001233 struct task_struct *task = get_proc_task(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001234 ssize_t length;
1235 char tmpbuf[TMPBUFLEN];
1236
Eric W. Biederman99f89552006-06-26 00:25:55 -07001237 if (!task)
1238 return -ESRCH;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001239 length = scnprintf(tmpbuf, TMPBUFLEN, "%u",
Eric W. Biedermane1760bd2012-09-10 22:39:43 -07001240 from_kuid(file->f_cred->user_ns,
1241 audit_get_loginuid(task)));
Eric W. Biederman99f89552006-06-26 00:25:55 -07001242 put_task_struct(task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001243 return simple_read_from_buffer(buf, count, ppos, tmpbuf, length);
1244}
1245
1246static ssize_t proc_loginuid_write(struct file * file, const char __user * buf,
1247 size_t count, loff_t *ppos)
1248{
Al Viro496ad9a2013-01-23 17:07:38 -05001249 struct inode * inode = file_inode(file);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001250 uid_t loginuid;
Eric W. Biedermane1760bd2012-09-10 22:39:43 -07001251 kuid_t kloginuid;
Alexey Dobriyan774636e2015-09-09 15:36:59 -07001252 int rv;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001253
Paul E. McKenney7dc52152010-02-22 17:04:52 -08001254 rcu_read_lock();
1255 if (current != pid_task(proc_pid(inode), PIDTYPE_PID)) {
1256 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001257 return -EPERM;
Paul E. McKenney7dc52152010-02-22 17:04:52 -08001258 }
1259 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001260
Linus Torvalds1da177e2005-04-16 15:20:36 -07001261 if (*ppos != 0) {
1262 /* No partial writes. */
1263 return -EINVAL;
1264 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001265
Alexey Dobriyan774636e2015-09-09 15:36:59 -07001266 rv = kstrtou32_from_user(buf, count, 10, &loginuid);
1267 if (rv < 0)
1268 return rv;
Eric Paris81407c82013-05-24 09:49:14 -04001269
1270 /* is userspace tring to explicitly UNSET the loginuid? */
1271 if (loginuid == AUDIT_UID_UNSET) {
1272 kloginuid = INVALID_UID;
1273 } else {
1274 kloginuid = make_kuid(file->f_cred->user_ns, loginuid);
Alexey Dobriyan774636e2015-09-09 15:36:59 -07001275 if (!uid_valid(kloginuid))
1276 return -EINVAL;
Eric W. Biedermane1760bd2012-09-10 22:39:43 -07001277 }
1278
Alexey Dobriyan774636e2015-09-09 15:36:59 -07001279 rv = audit_set_loginuid(kloginuid);
1280 if (rv < 0)
1281 return rv;
1282 return count;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001283}
1284
Arjan van de Ven00977a52007-02-12 00:55:34 -08001285static const struct file_operations proc_loginuid_operations = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001286 .read = proc_loginuid_read,
1287 .write = proc_loginuid_write,
Arnd Bergmann87df8422010-03-17 23:06:02 +01001288 .llseek = generic_file_llseek,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001289};
Eric Paris1e0bd752008-03-13 08:15:31 -04001290
1291static ssize_t proc_sessionid_read(struct file * file, char __user * buf,
1292 size_t count, loff_t *ppos)
1293{
Al Viro496ad9a2013-01-23 17:07:38 -05001294 struct inode * inode = file_inode(file);
Eric Paris1e0bd752008-03-13 08:15:31 -04001295 struct task_struct *task = get_proc_task(inode);
1296 ssize_t length;
1297 char tmpbuf[TMPBUFLEN];
1298
1299 if (!task)
1300 return -ESRCH;
1301 length = scnprintf(tmpbuf, TMPBUFLEN, "%u",
1302 audit_get_sessionid(task));
1303 put_task_struct(task);
1304 return simple_read_from_buffer(buf, count, ppos, tmpbuf, length);
1305}
1306
1307static const struct file_operations proc_sessionid_operations = {
1308 .read = proc_sessionid_read,
Arnd Bergmann87df8422010-03-17 23:06:02 +01001309 .llseek = generic_file_llseek,
Eric Paris1e0bd752008-03-13 08:15:31 -04001310};
Linus Torvalds1da177e2005-04-16 15:20:36 -07001311#endif
1312
Akinobu Mitaf4f154f2006-12-08 02:39:47 -08001313#ifdef CONFIG_FAULT_INJECTION
1314static ssize_t proc_fault_inject_read(struct file * file, char __user * buf,
1315 size_t count, loff_t *ppos)
1316{
Al Viro496ad9a2013-01-23 17:07:38 -05001317 struct task_struct *task = get_proc_task(file_inode(file));
Akinobu Mitaf4f154f2006-12-08 02:39:47 -08001318 char buffer[PROC_NUMBUF];
1319 size_t len;
1320 int make_it_fail;
Akinobu Mitaf4f154f2006-12-08 02:39:47 -08001321
1322 if (!task)
1323 return -ESRCH;
1324 make_it_fail = task->make_it_fail;
1325 put_task_struct(task);
1326
1327 len = snprintf(buffer, sizeof(buffer), "%i\n", make_it_fail);
Akinobu Mita0c28f282007-05-08 00:31:41 -07001328
1329 return simple_read_from_buffer(buf, count, ppos, buffer, len);
Akinobu Mitaf4f154f2006-12-08 02:39:47 -08001330}
1331
1332static ssize_t proc_fault_inject_write(struct file * file,
1333 const char __user * buf, size_t count, loff_t *ppos)
1334{
1335 struct task_struct *task;
Alexey Dobriyan774636e2015-09-09 15:36:59 -07001336 char buffer[PROC_NUMBUF];
Akinobu Mitaf4f154f2006-12-08 02:39:47 -08001337 int make_it_fail;
Alexey Dobriyan774636e2015-09-09 15:36:59 -07001338 int rv;
Akinobu Mitaf4f154f2006-12-08 02:39:47 -08001339
1340 if (!capable(CAP_SYS_RESOURCE))
1341 return -EPERM;
1342 memset(buffer, 0, sizeof(buffer));
1343 if (count > sizeof(buffer) - 1)
1344 count = sizeof(buffer) - 1;
1345 if (copy_from_user(buffer, buf, count))
1346 return -EFAULT;
Alexey Dobriyan774636e2015-09-09 15:36:59 -07001347 rv = kstrtoint(strstrip(buffer), 0, &make_it_fail);
1348 if (rv < 0)
1349 return rv;
Dave Jones16caed32014-04-07 15:39:15 -07001350 if (make_it_fail < 0 || make_it_fail > 1)
1351 return -EINVAL;
1352
Al Viro496ad9a2013-01-23 17:07:38 -05001353 task = get_proc_task(file_inode(file));
Akinobu Mitaf4f154f2006-12-08 02:39:47 -08001354 if (!task)
1355 return -ESRCH;
1356 task->make_it_fail = make_it_fail;
1357 put_task_struct(task);
Vincent Licba8aaf2009-09-22 16:45:38 -07001358
1359 return count;
Akinobu Mitaf4f154f2006-12-08 02:39:47 -08001360}
1361
Arjan van de Ven00977a52007-02-12 00:55:34 -08001362static const struct file_operations proc_fault_inject_operations = {
Akinobu Mitaf4f154f2006-12-08 02:39:47 -08001363 .read = proc_fault_inject_read,
1364 .write = proc_fault_inject_write,
Arnd Bergmann87df8422010-03-17 23:06:02 +01001365 .llseek = generic_file_llseek,
Akinobu Mitaf4f154f2006-12-08 02:39:47 -08001366};
Dmitry Vyukove41d58182017-07-12 14:34:35 -07001367
1368static ssize_t proc_fail_nth_write(struct file *file, const char __user *buf,
1369 size_t count, loff_t *ppos)
1370{
1371 struct task_struct *task;
Akinobu Mita9049f2f2017-07-14 14:49:52 -07001372 int err;
1373 unsigned int n;
Dmitry Vyukove41d58182017-07-12 14:34:35 -07001374
Akinobu Mita9049f2f2017-07-14 14:49:52 -07001375 err = kstrtouint_from_user(buf, count, 0, &n);
Dmitry Vyukove41d58182017-07-12 14:34:35 -07001376 if (err)
1377 return err;
Akinobu Mita1203c8e2017-07-14 14:49:57 -07001378
1379 task = get_proc_task(file_inode(file));
1380 if (!task)
1381 return -ESRCH;
Alexey Dobriyan9f7118b2018-02-06 15:36:55 -08001382 task->fail_nth = n;
Akinobu Mita1203c8e2017-07-14 14:49:57 -07001383 put_task_struct(task);
1384
Dmitry Vyukove41d58182017-07-12 14:34:35 -07001385 return count;
1386}
1387
1388static ssize_t proc_fail_nth_read(struct file *file, char __user *buf,
1389 size_t count, loff_t *ppos)
1390{
1391 struct task_struct *task;
Akinobu Mitabfc74092017-07-14 14:49:54 -07001392 char numbuf[PROC_NUMBUF];
1393 ssize_t len;
Dmitry Vyukove41d58182017-07-12 14:34:35 -07001394
1395 task = get_proc_task(file_inode(file));
1396 if (!task)
1397 return -ESRCH;
Alexey Dobriyan9f7118b2018-02-06 15:36:55 -08001398 len = snprintf(numbuf, sizeof(numbuf), "%u\n", task->fail_nth);
Akinobu Mita1203c8e2017-07-14 14:49:57 -07001399 put_task_struct(task);
Alexey Dobriyana44937f2018-08-21 21:54:27 -07001400 return simple_read_from_buffer(buf, count, ppos, numbuf, len);
Dmitry Vyukove41d58182017-07-12 14:34:35 -07001401}
1402
1403static const struct file_operations proc_fail_nth_operations = {
1404 .read = proc_fail_nth_read,
1405 .write = proc_fail_nth_write,
1406};
Akinobu Mitaf4f154f2006-12-08 02:39:47 -08001407#endif
1408
Arjan van de Ven97455122008-01-25 21:08:34 +01001409
Ingo Molnar43ae34c2007-07-09 18:52:00 +02001410#ifdef CONFIG_SCHED_DEBUG
1411/*
1412 * Print out various scheduling related per-task fields:
1413 */
1414static int sched_show(struct seq_file *m, void *v)
1415{
1416 struct inode *inode = m->private;
Christoph Hellwig76f668b2018-05-16 07:19:01 +02001417 struct pid_namespace *ns = proc_pid_ns(inode);
Ingo Molnar43ae34c2007-07-09 18:52:00 +02001418 struct task_struct *p;
1419
Ingo Molnar43ae34c2007-07-09 18:52:00 +02001420 p = get_proc_task(inode);
1421 if (!p)
1422 return -ESRCH;
Aleksa Sarai74dc3382017-08-06 14:41:41 +10001423 proc_sched_show_task(p, ns, m);
Ingo Molnar43ae34c2007-07-09 18:52:00 +02001424
1425 put_task_struct(p);
1426
1427 return 0;
1428}
1429
1430static ssize_t
1431sched_write(struct file *file, const char __user *buf,
1432 size_t count, loff_t *offset)
1433{
Al Viro496ad9a2013-01-23 17:07:38 -05001434 struct inode *inode = file_inode(file);
Ingo Molnar43ae34c2007-07-09 18:52:00 +02001435 struct task_struct *p;
1436
Ingo Molnar43ae34c2007-07-09 18:52:00 +02001437 p = get_proc_task(inode);
1438 if (!p)
1439 return -ESRCH;
1440 proc_sched_set_task(p);
1441
1442 put_task_struct(p);
1443
1444 return count;
1445}
1446
1447static int sched_open(struct inode *inode, struct file *filp)
1448{
Jovi Zhangc6a34052011-01-12 17:00:34 -08001449 return single_open(filp, sched_show, inode);
Ingo Molnar43ae34c2007-07-09 18:52:00 +02001450}
1451
1452static const struct file_operations proc_pid_sched_operations = {
1453 .open = sched_open,
1454 .read = seq_read,
1455 .write = sched_write,
1456 .llseek = seq_lseek,
Alexey Dobriyan5ea473a2007-07-31 00:38:50 -07001457 .release = single_release,
Ingo Molnar43ae34c2007-07-09 18:52:00 +02001458};
1459
1460#endif
1461
Mike Galbraith5091faa2010-11-30 14:18:03 +01001462#ifdef CONFIG_SCHED_AUTOGROUP
1463/*
1464 * Print out autogroup related information:
1465 */
1466static int sched_autogroup_show(struct seq_file *m, void *v)
1467{
1468 struct inode *inode = m->private;
1469 struct task_struct *p;
1470
1471 p = get_proc_task(inode);
1472 if (!p)
1473 return -ESRCH;
1474 proc_sched_autogroup_show_task(p, m);
1475
1476 put_task_struct(p);
1477
1478 return 0;
1479}
1480
1481static ssize_t
1482sched_autogroup_write(struct file *file, const char __user *buf,
1483 size_t count, loff_t *offset)
1484{
Al Viro496ad9a2013-01-23 17:07:38 -05001485 struct inode *inode = file_inode(file);
Mike Galbraith5091faa2010-11-30 14:18:03 +01001486 struct task_struct *p;
1487 char buffer[PROC_NUMBUF];
Alexey Dobriyan0a8cb8e2011-05-26 16:25:50 -07001488 int nice;
Mike Galbraith5091faa2010-11-30 14:18:03 +01001489 int err;
1490
1491 memset(buffer, 0, sizeof(buffer));
1492 if (count > sizeof(buffer) - 1)
1493 count = sizeof(buffer) - 1;
1494 if (copy_from_user(buffer, buf, count))
1495 return -EFAULT;
1496
Alexey Dobriyan0a8cb8e2011-05-26 16:25:50 -07001497 err = kstrtoint(strstrip(buffer), 0, &nice);
1498 if (err < 0)
1499 return err;
Mike Galbraith5091faa2010-11-30 14:18:03 +01001500
1501 p = get_proc_task(inode);
1502 if (!p)
1503 return -ESRCH;
1504
Hiroshi Shimamoto2e5b5b32012-02-23 17:41:27 +09001505 err = proc_sched_autogroup_set_nice(p, nice);
Mike Galbraith5091faa2010-11-30 14:18:03 +01001506 if (err)
1507 count = err;
1508
1509 put_task_struct(p);
1510
1511 return count;
1512}
1513
1514static int sched_autogroup_open(struct inode *inode, struct file *filp)
1515{
1516 int ret;
1517
1518 ret = single_open(filp, sched_autogroup_show, NULL);
1519 if (!ret) {
1520 struct seq_file *m = filp->private_data;
1521
1522 m->private = inode;
1523 }
1524 return ret;
1525}
1526
1527static const struct file_operations proc_pid_sched_autogroup_operations = {
1528 .open = sched_autogroup_open,
1529 .read = seq_read,
1530 .write = sched_autogroup_write,
1531 .llseek = seq_lseek,
1532 .release = single_release,
1533};
1534
1535#endif /* CONFIG_SCHED_AUTOGROUP */
1536
john stultz4614a696b2009-12-14 18:00:05 -08001537static ssize_t comm_write(struct file *file, const char __user *buf,
1538 size_t count, loff_t *offset)
1539{
Al Viro496ad9a2013-01-23 17:07:38 -05001540 struct inode *inode = file_inode(file);
john stultz4614a696b2009-12-14 18:00:05 -08001541 struct task_struct *p;
1542 char buffer[TASK_COMM_LEN];
David Rientjes830e0fc2013-04-30 15:28:18 -07001543 const size_t maxlen = sizeof(buffer) - 1;
john stultz4614a696b2009-12-14 18:00:05 -08001544
1545 memset(buffer, 0, sizeof(buffer));
David Rientjes830e0fc2013-04-30 15:28:18 -07001546 if (copy_from_user(buffer, buf, count > maxlen ? maxlen : count))
john stultz4614a696b2009-12-14 18:00:05 -08001547 return -EFAULT;
1548
1549 p = get_proc_task(inode);
1550 if (!p)
1551 return -ESRCH;
1552
1553 if (same_thread_group(current, p))
1554 set_task_comm(p, buffer);
1555 else
1556 count = -EINVAL;
1557
1558 put_task_struct(p);
1559
1560 return count;
1561}
1562
1563static int comm_show(struct seq_file *m, void *v)
1564{
1565 struct inode *inode = m->private;
1566 struct task_struct *p;
1567
1568 p = get_proc_task(inode);
1569 if (!p)
1570 return -ESRCH;
1571
Tejun Heo88b72b32018-05-18 08:47:13 -07001572 proc_task_name(m, p, false);
1573 seq_putc(m, '\n');
john stultz4614a696b2009-12-14 18:00:05 -08001574
1575 put_task_struct(p);
1576
1577 return 0;
1578}
1579
1580static int comm_open(struct inode *inode, struct file *filp)
1581{
Jovi Zhangc6a34052011-01-12 17:00:34 -08001582 return single_open(filp, comm_show, inode);
john stultz4614a696b2009-12-14 18:00:05 -08001583}
1584
1585static const struct file_operations proc_pid_set_comm_operations = {
1586 .open = comm_open,
1587 .read = seq_read,
1588 .write = comm_write,
1589 .llseek = seq_lseek,
1590 .release = single_release,
1591};
1592
Cyrill Gorcunov7773fbc2012-01-10 15:11:20 -08001593static int proc_exe_link(struct dentry *dentry, struct path *exe_path)
Matt Helsley925d1c42008-04-29 01:01:36 -07001594{
1595 struct task_struct *task;
Matt Helsley925d1c42008-04-29 01:01:36 -07001596 struct file *exe_file;
1597
David Howells2b0143b2015-03-17 22:25:59 +00001598 task = get_proc_task(d_inode(dentry));
Matt Helsley925d1c42008-04-29 01:01:36 -07001599 if (!task)
1600 return -ENOENT;
Mateusz Guzikcd81a9172016-08-23 16:20:38 +02001601 exe_file = get_task_exe_file(task);
Matt Helsley925d1c42008-04-29 01:01:36 -07001602 put_task_struct(task);
Matt Helsley925d1c42008-04-29 01:01:36 -07001603 if (exe_file) {
1604 *exe_path = exe_file->f_path;
1605 path_get(&exe_file->f_path);
1606 fput(exe_file);
1607 return 0;
1608 } else
1609 return -ENOENT;
1610}
1611
Al Viro6b255392015-11-17 10:20:54 -05001612static const char *proc_pid_get_link(struct dentry *dentry,
Al Virofceef392015-12-29 15:58:39 -05001613 struct inode *inode,
1614 struct delayed_call *done)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001615{
Christoph Hellwig408ef012012-06-18 10:47:03 -04001616 struct path path;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001617 int error = -EACCES;
1618
Al Viro6b255392015-11-17 10:20:54 -05001619 if (!dentry)
1620 return ERR_PTR(-ECHILD);
1621
Eric W. Biederman778c1142006-06-26 00:25:58 -07001622 /* Are we allowed to snoop on the tasks file descriptors? */
1623 if (!proc_fd_access_allowed(inode))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001624 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001625
Christoph Hellwig408ef012012-06-18 10:47:03 -04001626 error = PROC_I(inode)->op.proc_get_link(dentry, &path);
1627 if (error)
1628 goto out;
1629
Al Viro6e77137b2015-05-02 13:37:52 -04001630 nd_jump_link(&path);
Christoph Hellwig408ef012012-06-18 10:47:03 -04001631 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001632out:
Al Viro008b1502005-08-20 00:17:39 +01001633 return ERR_PTR(error);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001634}
1635
Jan Blunck3dcd25f2008-02-14 19:38:35 -08001636static int do_proc_readlink(struct path *path, char __user *buffer, int buflen)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001637{
Michal Hocko0ee931c2017-09-13 16:28:29 -07001638 char *tmp = (char *)__get_free_page(GFP_KERNEL);
Jan Blunck3dcd25f2008-02-14 19:38:35 -08001639 char *pathname;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001640 int len;
1641
1642 if (!tmp)
1643 return -ENOMEM;
Akinobu Mita0c28f282007-05-08 00:31:41 -07001644
Eric W. Biederman7b2a69b2010-12-05 15:51:21 -08001645 pathname = d_path(path, tmp, PAGE_SIZE);
Jan Blunck3dcd25f2008-02-14 19:38:35 -08001646 len = PTR_ERR(pathname);
1647 if (IS_ERR(pathname))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001648 goto out;
Jan Blunck3dcd25f2008-02-14 19:38:35 -08001649 len = tmp + PAGE_SIZE - 1 - pathname;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001650
1651 if (len > buflen)
1652 len = buflen;
Jan Blunck3dcd25f2008-02-14 19:38:35 -08001653 if (copy_to_user(buffer, pathname, len))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001654 len = -EFAULT;
1655 out:
1656 free_page((unsigned long)tmp);
1657 return len;
1658}
1659
1660static int proc_pid_readlink(struct dentry * dentry, char __user * buffer, int buflen)
1661{
1662 int error = -EACCES;
David Howells2b0143b2015-03-17 22:25:59 +00001663 struct inode *inode = d_inode(dentry);
Jan Blunck3dcd25f2008-02-14 19:38:35 -08001664 struct path path;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001665
Eric W. Biederman778c1142006-06-26 00:25:58 -07001666 /* Are we allowed to snoop on the tasks file descriptors? */
1667 if (!proc_fd_access_allowed(inode))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001668 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001669
Cyrill Gorcunov7773fbc2012-01-10 15:11:20 -08001670 error = PROC_I(inode)->op.proc_get_link(dentry, &path);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001671 if (error)
1672 goto out;
1673
Jan Blunck3dcd25f2008-02-14 19:38:35 -08001674 error = do_proc_readlink(&path, buffer, buflen);
1675 path_put(&path);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001676out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001677 return error;
1678}
1679
Cyrill Gorcunovfaf60af2012-08-23 14:43:24 +04001680const struct inode_operations proc_pid_link_inode_operations = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001681 .readlink = proc_pid_readlink,
Al Viro6b255392015-11-17 10:20:54 -05001682 .get_link = proc_pid_get_link,
Linus Torvalds6d76fa52006-07-15 12:26:45 -07001683 .setattr = proc_setattr,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001684};
1685
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001686
1687/* building an inode */
1688
Al Viroc6eb50d2017-09-30 14:45:42 -04001689void task_dump_owner(struct task_struct *task, umode_t mode,
Eric W. Biederman68eb94f2017-01-03 10:23:11 +13001690 kuid_t *ruid, kgid_t *rgid)
1691{
1692 /* Depending on the state of dumpable compute who should own a
1693 * proc file for a task.
1694 */
1695 const struct cred *cred;
1696 kuid_t uid;
1697 kgid_t gid;
1698
Alexey Dobriyan2e0ad552018-04-20 14:56:03 -07001699 if (unlikely(task->flags & PF_KTHREAD)) {
1700 *ruid = GLOBAL_ROOT_UID;
1701 *rgid = GLOBAL_ROOT_GID;
1702 return;
1703 }
1704
Eric W. Biederman68eb94f2017-01-03 10:23:11 +13001705 /* Default to the tasks effective ownership */
1706 rcu_read_lock();
1707 cred = __task_cred(task);
1708 uid = cred->euid;
1709 gid = cred->egid;
1710 rcu_read_unlock();
1711
1712 /*
1713 * Before the /proc/pid/status file was created the only way to read
1714 * the effective uid of a /process was to stat /proc/pid. Reading
1715 * /proc/pid/status is slow enough that procps and other packages
1716 * kept stating /proc/pid. To keep the rules in /proc simple I have
1717 * made this apply to all per process world readable and executable
1718 * directories.
1719 */
1720 if (mode != (S_IFDIR|S_IRUGO|S_IXUGO)) {
1721 struct mm_struct *mm;
1722 task_lock(task);
1723 mm = task->mm;
1724 /* Make non-dumpable tasks owned by some root */
1725 if (mm) {
1726 if (get_dumpable(mm) != SUID_DUMP_USER) {
1727 struct user_namespace *user_ns = mm->user_ns;
1728
1729 uid = make_kuid(user_ns, 0);
1730 if (!uid_valid(uid))
1731 uid = GLOBAL_ROOT_UID;
1732
1733 gid = make_kgid(user_ns, 0);
1734 if (!gid_valid(gid))
1735 gid = GLOBAL_ROOT_GID;
1736 }
1737 } else {
1738 uid = GLOBAL_ROOT_UID;
1739 gid = GLOBAL_ROOT_GID;
1740 }
1741 task_unlock(task);
1742 }
1743 *ruid = uid;
1744 *rgid = gid;
1745}
1746
Andreas Gruenbacherdb978da2016-11-10 22:18:28 +01001747struct inode *proc_pid_make_inode(struct super_block * sb,
1748 struct task_struct *task, umode_t mode)
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001749{
1750 struct inode * inode;
1751 struct proc_inode *ei;
1752
1753 /* We need a new inode */
1754
1755 inode = new_inode(sb);
1756 if (!inode)
1757 goto out;
1758
1759 /* Common stuff */
1760 ei = PROC_I(inode);
Andreas Gruenbacherdb978da2016-11-10 22:18:28 +01001761 inode->i_mode = mode;
Christoph Hellwig85fe4022010-10-23 11:19:54 -04001762 inode->i_ino = get_next_ino();
Deepa Dinamani078cd822016-09-14 07:48:04 -07001763 inode->i_mtime = inode->i_atime = inode->i_ctime = current_time(inode);
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001764 inode->i_op = &proc_def_inode_operations;
1765
1766 /*
1767 * grab the reference to task.
1768 */
Oleg Nesterov1a657f782006-10-02 02:18:59 -07001769 ei->pid = get_task_pid(task, PIDTYPE_PID);
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001770 if (!ei->pid)
1771 goto out_unlock;
1772
Eric W. Biederman68eb94f2017-01-03 10:23:11 +13001773 task_dump_owner(task, 0, &inode->i_uid, &inode->i_gid);
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001774 security_task_to_inode(task, inode);
1775
1776out:
1777 return inode;
1778
1779out_unlock:
1780 iput(inode);
1781 return NULL;
1782}
1783
David Howellsa528d352017-01-31 16:46:22 +00001784int pid_getattr(const struct path *path, struct kstat *stat,
1785 u32 request_mask, unsigned int query_flags)
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001786{
David Howellsa528d352017-01-31 16:46:22 +00001787 struct inode *inode = d_inode(path->dentry);
Christoph Hellwig76f668b2018-05-16 07:19:01 +02001788 struct pid_namespace *pid = proc_pid_ns(inode);
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001789 struct task_struct *task;
David Howellsc69e8d92008-11-14 10:39:19 +11001790
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001791 generic_fillattr(inode, stat);
1792
Eric W. Biedermandcb0f222012-02-09 08:48:21 -08001793 stat->uid = GLOBAL_ROOT_UID;
1794 stat->gid = GLOBAL_ROOT_GID;
Alexey Dobriyan94116922018-06-07 17:10:07 -07001795 rcu_read_lock();
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001796 task = pid_task(proc_pid(inode), PIDTYPE_PID);
1797 if (task) {
Lafcadio Wluiki796f5712017-02-24 15:00:23 -08001798 if (!has_pid_permissions(pid, task, HIDEPID_INVISIBLE)) {
Vasiliy Kulikov04996802012-01-10 15:11:31 -08001799 rcu_read_unlock();
1800 /*
1801 * This doesn't prevent learning whether PID exists,
1802 * it only makes getattr() consistent with readdir().
1803 */
1804 return -ENOENT;
1805 }
Eric W. Biederman68eb94f2017-01-03 10:23:11 +13001806 task_dump_owner(task, inode->i_mode, &stat->uid, &stat->gid);
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001807 }
1808 rcu_read_unlock();
1809 return 0;
1810}
1811
1812/* dentry stuff */
1813
1814/*
Al Viro1bbc5512018-05-02 21:26:16 -04001815 * Set <pid>/... inode ownership (can change due to setuid(), etc.)
1816 */
1817void pid_update_inode(struct task_struct *task, struct inode *inode)
1818{
1819 task_dump_owner(task, inode->i_mode, &inode->i_uid, &inode->i_gid);
1820
1821 inode->i_mode &= ~(S_ISUID | S_ISGID);
1822 security_task_to_inode(task, inode);
1823}
1824
1825/*
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001826 * Rewrite the inode's ownerships here because the owning task may have
1827 * performed a setuid(), etc.
1828 *
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001829 */
Al Viro1bbc5512018-05-02 21:26:16 -04001830static int pid_revalidate(struct dentry *dentry, unsigned int flags)
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001831{
Nick Piggin34286d62011-01-07 17:49:57 +11001832 struct inode *inode;
1833 struct task_struct *task;
David Howellsc69e8d92008-11-14 10:39:19 +11001834
Al Viro0b728e12012-06-10 16:03:43 -04001835 if (flags & LOOKUP_RCU)
Nick Piggin34286d62011-01-07 17:49:57 +11001836 return -ECHILD;
1837
David Howells2b0143b2015-03-17 22:25:59 +00001838 inode = d_inode(dentry);
Nick Piggin34286d62011-01-07 17:49:57 +11001839 task = get_proc_task(inode);
1840
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001841 if (task) {
Al Viro1bbc5512018-05-02 21:26:16 -04001842 pid_update_inode(task, inode);
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001843 put_task_struct(task);
1844 return 1;
1845 }
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001846 return 0;
1847}
1848
Oleg Nesterovd855a4b2014-01-23 15:55:39 -08001849static inline bool proc_inode_is_dead(struct inode *inode)
1850{
1851 return !proc_pid(inode)->tasks[PIDTYPE_PID].first;
1852}
1853
David Howells1dd704b2013-04-12 01:08:50 +01001854int pid_delete_dentry(const struct dentry *dentry)
1855{
1856 /* Is the task we represent dead?
1857 * If so, then don't put the dentry on the lru list,
1858 * kill it immediately.
1859 */
David Howells2b0143b2015-03-17 22:25:59 +00001860 return proc_inode_is_dead(d_inode(dentry));
David Howells1dd704b2013-04-12 01:08:50 +01001861}
1862
Eric W. Biederman6b4e3062010-03-07 16:41:34 -08001863const struct dentry_operations pid_dentry_operations =
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001864{
1865 .d_revalidate = pid_revalidate,
1866 .d_delete = pid_delete_dentry,
1867};
1868
1869/* Lookups */
1870
Eric W. Biederman1c0d04c2006-10-02 02:18:57 -07001871/*
1872 * Fill a directory entry.
1873 *
1874 * If possible create the dcache entry and derive our inode number and
1875 * file type from dcache entry.
1876 *
1877 * Since all of the proc inode numbers are dynamically generated, the inode
1878 * numbers do not exist until the inode is cache. This means creating the
1879 * the dcache entry in readdir is necessary to keep the inode numbers
1880 * reported by readdir in sync with the inode numbers reported
1881 * by stat.
1882 */
Al Virof0c3b502013-05-16 12:07:31 -04001883bool proc_fill_cache(struct file *file, struct dir_context *ctx,
Alexey Dobriyana4ef3892018-06-07 17:10:10 -07001884 const char *name, unsigned int len,
Eric Dumazetc5141e62007-05-08 00:26:15 -07001885 instantiate_t instantiate, struct task_struct *task, const void *ptr)
Eric W. Biederman61a28782006-10-02 02:18:49 -07001886{
Al Virof0c3b502013-05-16 12:07:31 -04001887 struct dentry *child, *dir = file->f_path.dentry;
Al Viro1df98b82013-06-15 11:33:10 +04001888 struct qstr qname = QSTR_INIT(name, len);
Eric W. Biederman61a28782006-10-02 02:18:49 -07001889 struct inode *inode;
Al Viro0168b9e2018-05-03 09:21:05 -04001890 unsigned type = DT_UNKNOWN;
1891 ino_t ino = 1;
Eric W. Biederman61a28782006-10-02 02:18:49 -07001892
Al Viro1df98b82013-06-15 11:33:10 +04001893 child = d_hash_and_lookup(dir, &qname);
Eric W. Biederman61a28782006-10-02 02:18:49 -07001894 if (!child) {
Al Viro37817642016-04-20 16:31:31 -04001895 DECLARE_WAIT_QUEUE_HEAD_ONSTACK(wq);
1896 child = d_alloc_parallel(dir, &qname, &wq);
1897 if (IS_ERR(child))
Al Viro1df98b82013-06-15 11:33:10 +04001898 goto end_instantiate;
Al Viro37817642016-04-20 16:31:31 -04001899 if (d_in_lookup(child)) {
Al Viro0168b9e2018-05-03 09:21:05 -04001900 struct dentry *res;
1901 res = instantiate(child, task, ptr);
Al Viro37817642016-04-20 16:31:31 -04001902 d_lookup_done(child);
Al Viro0168b9e2018-05-03 09:21:05 -04001903 if (unlikely(res)) {
1904 dput(child);
1905 child = res;
Al Virod85b3992018-06-08 01:17:11 -04001906 if (IS_ERR(child))
1907 goto end_instantiate;
Al Viro37817642016-04-20 16:31:31 -04001908 }
Eric W. Biederman61a28782006-10-02 02:18:49 -07001909 }
1910 }
David Howells2b0143b2015-03-17 22:25:59 +00001911 inode = d_inode(child);
Al Viro147ce692013-06-15 10:26:35 +04001912 ino = inode->i_ino;
1913 type = inode->i_mode >> 12;
Eric W. Biederman61a28782006-10-02 02:18:49 -07001914 dput(child);
Al Virod85b3992018-06-08 01:17:11 -04001915end_instantiate:
Al Virof0c3b502013-05-16 12:07:31 -04001916 return dir_emit(ctx, name, len, ino, type);
Eric W. Biederman61a28782006-10-02 02:18:49 -07001917}
1918
Pavel Emelyanov640708a2012-01-10 15:11:23 -08001919/*
1920 * dname_to_vma_addr - maps a dentry name into two unsigned longs
1921 * which represent vma start and end addresses.
1922 */
1923static int dname_to_vma_addr(struct dentry *dentry,
1924 unsigned long *start, unsigned long *end)
1925{
Alexey Dobriyanac7f1062018-02-06 15:36:59 -08001926 const char *str = dentry->d_name.name;
1927 unsigned long long sval, eval;
1928 unsigned int len;
1929
Alexey Dobriyan35318db2018-04-10 16:41:14 -07001930 if (str[0] == '0' && str[1] != '-')
1931 return -EINVAL;
Alexey Dobriyanac7f1062018-02-06 15:36:59 -08001932 len = _parse_integer(str, 16, &sval);
1933 if (len & KSTRTOX_OVERFLOW)
Pavel Emelyanov640708a2012-01-10 15:11:23 -08001934 return -EINVAL;
Alexey Dobriyanac7f1062018-02-06 15:36:59 -08001935 if (sval != (unsigned long)sval)
1936 return -EINVAL;
1937 str += len;
1938
1939 if (*str != '-')
1940 return -EINVAL;
1941 str++;
1942
Alexey Dobriyan35318db2018-04-10 16:41:14 -07001943 if (str[0] == '0' && str[1])
1944 return -EINVAL;
Alexey Dobriyanac7f1062018-02-06 15:36:59 -08001945 len = _parse_integer(str, 16, &eval);
1946 if (len & KSTRTOX_OVERFLOW)
1947 return -EINVAL;
1948 if (eval != (unsigned long)eval)
1949 return -EINVAL;
1950 str += len;
1951
1952 if (*str != '\0')
1953 return -EINVAL;
1954
1955 *start = sval;
1956 *end = eval;
Pavel Emelyanov640708a2012-01-10 15:11:23 -08001957
1958 return 0;
1959}
1960
Al Viro0b728e12012-06-10 16:03:43 -04001961static int map_files_d_revalidate(struct dentry *dentry, unsigned int flags)
Pavel Emelyanov640708a2012-01-10 15:11:23 -08001962{
1963 unsigned long vm_start, vm_end;
1964 bool exact_vma_exists = false;
1965 struct mm_struct *mm = NULL;
1966 struct task_struct *task;
Pavel Emelyanov640708a2012-01-10 15:11:23 -08001967 struct inode *inode;
1968 int status = 0;
1969
Al Viro0b728e12012-06-10 16:03:43 -04001970 if (flags & LOOKUP_RCU)
Pavel Emelyanov640708a2012-01-10 15:11:23 -08001971 return -ECHILD;
1972
David Howells2b0143b2015-03-17 22:25:59 +00001973 inode = d_inode(dentry);
Pavel Emelyanov640708a2012-01-10 15:11:23 -08001974 task = get_proc_task(inode);
1975 if (!task)
1976 goto out_notask;
1977
Jann Horncaaee622016-01-20 15:00:04 -08001978 mm = mm_access(task, PTRACE_MODE_READ_FSCREDS);
Cong Wang2344bec2012-05-31 16:26:18 -07001979 if (IS_ERR_OR_NULL(mm))
Pavel Emelyanov640708a2012-01-10 15:11:23 -08001980 goto out;
1981
1982 if (!dname_to_vma_addr(dentry, &vm_start, &vm_end)) {
1983 down_read(&mm->mmap_sem);
1984 exact_vma_exists = !!find_exact_vma(mm, vm_start, vm_end);
1985 up_read(&mm->mmap_sem);
1986 }
1987
1988 mmput(mm);
1989
1990 if (exact_vma_exists) {
Eric W. Biederman68eb94f2017-01-03 10:23:11 +13001991 task_dump_owner(task, 0, &inode->i_uid, &inode->i_gid);
1992
Pavel Emelyanov640708a2012-01-10 15:11:23 -08001993 security_task_to_inode(task, inode);
1994 status = 1;
1995 }
1996
1997out:
1998 put_task_struct(task);
1999
2000out_notask:
Pavel Emelyanov640708a2012-01-10 15:11:23 -08002001 return status;
2002}
2003
2004static const struct dentry_operations tid_map_files_dentry_operations = {
2005 .d_revalidate = map_files_d_revalidate,
2006 .d_delete = pid_delete_dentry,
2007};
2008
Al Viro6b255392015-11-17 10:20:54 -05002009static int map_files_get_link(struct dentry *dentry, struct path *path)
Pavel Emelyanov640708a2012-01-10 15:11:23 -08002010{
2011 unsigned long vm_start, vm_end;
2012 struct vm_area_struct *vma;
2013 struct task_struct *task;
2014 struct mm_struct *mm;
2015 int rc;
2016
2017 rc = -ENOENT;
David Howells2b0143b2015-03-17 22:25:59 +00002018 task = get_proc_task(d_inode(dentry));
Pavel Emelyanov640708a2012-01-10 15:11:23 -08002019 if (!task)
2020 goto out;
2021
2022 mm = get_task_mm(task);
2023 put_task_struct(task);
2024 if (!mm)
2025 goto out;
2026
2027 rc = dname_to_vma_addr(dentry, &vm_start, &vm_end);
2028 if (rc)
2029 goto out_mmput;
2030
Artem Fetishev70335ab2014-03-10 15:49:45 -07002031 rc = -ENOENT;
Pavel Emelyanov640708a2012-01-10 15:11:23 -08002032 down_read(&mm->mmap_sem);
2033 vma = find_exact_vma(mm, vm_start, vm_end);
2034 if (vma && vma->vm_file) {
2035 *path = vma->vm_file->f_path;
2036 path_get(path);
2037 rc = 0;
2038 }
2039 up_read(&mm->mmap_sem);
2040
2041out_mmput:
2042 mmput(mm);
2043out:
2044 return rc;
2045}
2046
2047struct map_files_info {
Alexey Dobriyan20d28cd2018-02-06 15:37:06 -08002048 unsigned long start;
2049 unsigned long end;
Al Viro7b540d02012-08-27 14:55:26 -04002050 fmode_t mode;
Pavel Emelyanov640708a2012-01-10 15:11:23 -08002051};
2052
Calvin Owensbdb4d102015-09-09 15:35:54 -07002053/*
2054 * Only allow CAP_SYS_ADMIN to follow the links, due to concerns about how the
2055 * symlinks may be used to bypass permissions on ancestor directories in the
2056 * path to the file in question.
2057 */
2058static const char *
Al Viro6b255392015-11-17 10:20:54 -05002059proc_map_files_get_link(struct dentry *dentry,
Al Virofceef392015-12-29 15:58:39 -05002060 struct inode *inode,
2061 struct delayed_call *done)
Calvin Owensbdb4d102015-09-09 15:35:54 -07002062{
2063 if (!capable(CAP_SYS_ADMIN))
2064 return ERR_PTR(-EPERM);
2065
Al Virofceef392015-12-29 15:58:39 -05002066 return proc_pid_get_link(dentry, inode, done);
Calvin Owensbdb4d102015-09-09 15:35:54 -07002067}
2068
2069/*
Al Viro6b255392015-11-17 10:20:54 -05002070 * Identical to proc_pid_link_inode_operations except for get_link()
Calvin Owensbdb4d102015-09-09 15:35:54 -07002071 */
2072static const struct inode_operations proc_map_files_link_inode_operations = {
2073 .readlink = proc_pid_readlink,
Al Viro6b255392015-11-17 10:20:54 -05002074 .get_link = proc_map_files_get_link,
Calvin Owensbdb4d102015-09-09 15:35:54 -07002075 .setattr = proc_setattr,
2076};
2077
Al Viro0168b9e2018-05-03 09:21:05 -04002078static struct dentry *
2079proc_map_files_instantiate(struct dentry *dentry,
Pavel Emelyanov640708a2012-01-10 15:11:23 -08002080 struct task_struct *task, const void *ptr)
2081{
Al Viro7b540d02012-08-27 14:55:26 -04002082 fmode_t mode = (fmode_t)(unsigned long)ptr;
Pavel Emelyanov640708a2012-01-10 15:11:23 -08002083 struct proc_inode *ei;
2084 struct inode *inode;
2085
Al Viro0168b9e2018-05-03 09:21:05 -04002086 inode = proc_pid_make_inode(dentry->d_sb, task, S_IFLNK |
Andreas Gruenbacherdb978da2016-11-10 22:18:28 +01002087 ((mode & FMODE_READ ) ? S_IRUSR : 0) |
2088 ((mode & FMODE_WRITE) ? S_IWUSR : 0));
Pavel Emelyanov640708a2012-01-10 15:11:23 -08002089 if (!inode)
Al Viro0168b9e2018-05-03 09:21:05 -04002090 return ERR_PTR(-ENOENT);
Pavel Emelyanov640708a2012-01-10 15:11:23 -08002091
2092 ei = PROC_I(inode);
Al Viro6b255392015-11-17 10:20:54 -05002093 ei->op.proc_get_link = map_files_get_link;
Pavel Emelyanov640708a2012-01-10 15:11:23 -08002094
Calvin Owensbdb4d102015-09-09 15:35:54 -07002095 inode->i_op = &proc_map_files_link_inode_operations;
Pavel Emelyanov640708a2012-01-10 15:11:23 -08002096 inode->i_size = 64;
Pavel Emelyanov640708a2012-01-10 15:11:23 -08002097
2098 d_set_d_op(dentry, &tid_map_files_dentry_operations);
Al Viro0168b9e2018-05-03 09:21:05 -04002099 return d_splice_alias(inode, dentry);
Pavel Emelyanov640708a2012-01-10 15:11:23 -08002100}
2101
2102static struct dentry *proc_map_files_lookup(struct inode *dir,
Al Viro00cd8dd2012-06-10 17:13:09 -04002103 struct dentry *dentry, unsigned int flags)
Pavel Emelyanov640708a2012-01-10 15:11:23 -08002104{
2105 unsigned long vm_start, vm_end;
2106 struct vm_area_struct *vma;
2107 struct task_struct *task;
Al Viro0168b9e2018-05-03 09:21:05 -04002108 struct dentry *result;
Pavel Emelyanov640708a2012-01-10 15:11:23 -08002109 struct mm_struct *mm;
2110
Al Viro0168b9e2018-05-03 09:21:05 -04002111 result = ERR_PTR(-ENOENT);
Pavel Emelyanov640708a2012-01-10 15:11:23 -08002112 task = get_proc_task(dir);
2113 if (!task)
2114 goto out;
2115
Al Viro0168b9e2018-05-03 09:21:05 -04002116 result = ERR_PTR(-EACCES);
Jann Horncaaee622016-01-20 15:00:04 -08002117 if (!ptrace_may_access(task, PTRACE_MODE_READ_FSCREDS))
Pavel Emelyanov640708a2012-01-10 15:11:23 -08002118 goto out_put_task;
2119
Al Viro0168b9e2018-05-03 09:21:05 -04002120 result = ERR_PTR(-ENOENT);
Pavel Emelyanov640708a2012-01-10 15:11:23 -08002121 if (dname_to_vma_addr(dentry, &vm_start, &vm_end))
Cyrill Gorcunoveb94cd92012-05-17 17:03:25 -07002122 goto out_put_task;
Pavel Emelyanov640708a2012-01-10 15:11:23 -08002123
2124 mm = get_task_mm(task);
2125 if (!mm)
Cyrill Gorcunoveb94cd92012-05-17 17:03:25 -07002126 goto out_put_task;
Pavel Emelyanov640708a2012-01-10 15:11:23 -08002127
2128 down_read(&mm->mmap_sem);
2129 vma = find_exact_vma(mm, vm_start, vm_end);
2130 if (!vma)
2131 goto out_no_vma;
2132
Stanislav Kinsbursky05f56482012-11-26 16:29:42 -08002133 if (vma->vm_file)
Al Viro0168b9e2018-05-03 09:21:05 -04002134 result = proc_map_files_instantiate(dentry, task,
Stanislav Kinsbursky05f56482012-11-26 16:29:42 -08002135 (void *)(unsigned long)vma->vm_file->f_mode);
Pavel Emelyanov640708a2012-01-10 15:11:23 -08002136
2137out_no_vma:
2138 up_read(&mm->mmap_sem);
2139 mmput(mm);
Pavel Emelyanov640708a2012-01-10 15:11:23 -08002140out_put_task:
2141 put_task_struct(task);
2142out:
Al Viro0168b9e2018-05-03 09:21:05 -04002143 return result;
Pavel Emelyanov640708a2012-01-10 15:11:23 -08002144}
2145
2146static const struct inode_operations proc_map_files_inode_operations = {
2147 .lookup = proc_map_files_lookup,
2148 .permission = proc_fd_permission,
2149 .setattr = proc_setattr,
2150};
2151
2152static int
Al Virof0c3b502013-05-16 12:07:31 -04002153proc_map_files_readdir(struct file *file, struct dir_context *ctx)
Pavel Emelyanov640708a2012-01-10 15:11:23 -08002154{
Pavel Emelyanov640708a2012-01-10 15:11:23 -08002155 struct vm_area_struct *vma;
2156 struct task_struct *task;
2157 struct mm_struct *mm;
Al Virof0c3b502013-05-16 12:07:31 -04002158 unsigned long nr_files, pos, i;
Kent Overstreet94f8f3b2019-03-11 23:31:18 -07002159 GENRADIX(struct map_files_info) fa;
Al Virof0c3b502013-05-16 12:07:31 -04002160 struct map_files_info *p;
Pavel Emelyanov640708a2012-01-10 15:11:23 -08002161 int ret;
2162
Kent Overstreet94f8f3b2019-03-11 23:31:18 -07002163 genradix_init(&fa);
2164
Pavel Emelyanov640708a2012-01-10 15:11:23 -08002165 ret = -ENOENT;
Al Virof0c3b502013-05-16 12:07:31 -04002166 task = get_proc_task(file_inode(file));
Pavel Emelyanov640708a2012-01-10 15:11:23 -08002167 if (!task)
2168 goto out;
2169
2170 ret = -EACCES;
Jann Horncaaee622016-01-20 15:00:04 -08002171 if (!ptrace_may_access(task, PTRACE_MODE_READ_FSCREDS))
Pavel Emelyanov640708a2012-01-10 15:11:23 -08002172 goto out_put_task;
2173
2174 ret = 0;
Al Virof0c3b502013-05-16 12:07:31 -04002175 if (!dir_emit_dots(file, ctx))
2176 goto out_put_task;
Pavel Emelyanov640708a2012-01-10 15:11:23 -08002177
Al Virof0c3b502013-05-16 12:07:31 -04002178 mm = get_task_mm(task);
2179 if (!mm)
2180 goto out_put_task;
2181 down_read(&mm->mmap_sem);
Pavel Emelyanov640708a2012-01-10 15:11:23 -08002182
Al Virof0c3b502013-05-16 12:07:31 -04002183 nr_files = 0;
Pavel Emelyanov640708a2012-01-10 15:11:23 -08002184
Al Virof0c3b502013-05-16 12:07:31 -04002185 /*
2186 * We need two passes here:
2187 *
2188 * 1) Collect vmas of mapped files with mmap_sem taken
2189 * 2) Release mmap_sem and instantiate entries
2190 *
2191 * otherwise we get lockdep complained, since filldir()
2192 * routine might require mmap_sem taken in might_fault().
2193 */
Pavel Emelyanov640708a2012-01-10 15:11:23 -08002194
Al Virof0c3b502013-05-16 12:07:31 -04002195 for (vma = mm->mmap, pos = 2; vma; vma = vma->vm_next) {
Kent Overstreet94f8f3b2019-03-11 23:31:18 -07002196 if (!vma->vm_file)
2197 continue;
2198 if (++pos <= ctx->pos)
2199 continue;
Al Virof0c3b502013-05-16 12:07:31 -04002200
Kent Overstreet94f8f3b2019-03-11 23:31:18 -07002201 p = genradix_ptr_alloc(&fa, nr_files++, GFP_KERNEL);
2202 if (!p) {
Al Virof0c3b502013-05-16 12:07:31 -04002203 ret = -ENOMEM;
Al Virof0c3b502013-05-16 12:07:31 -04002204 up_read(&mm->mmap_sem);
2205 mmput(mm);
2206 goto out_put_task;
2207 }
Al Virof0c3b502013-05-16 12:07:31 -04002208
Kent Overstreet94f8f3b2019-03-11 23:31:18 -07002209 p->start = vma->vm_start;
2210 p->end = vma->vm_end;
2211 p->mode = vma->vm_file->f_mode;
Pavel Emelyanov640708a2012-01-10 15:11:23 -08002212 }
Al Virof0c3b502013-05-16 12:07:31 -04002213 up_read(&mm->mmap_sem);
Alexey Dobriyanfe079a52018-04-10 16:32:05 -07002214 mmput(mm);
Al Virof0c3b502013-05-16 12:07:31 -04002215
2216 for (i = 0; i < nr_files; i++) {
Alexey Dobriyan20d28cd2018-02-06 15:37:06 -08002217 char buf[4 * sizeof(long) + 2]; /* max: %lx-%lx\0 */
2218 unsigned int len;
2219
Kent Overstreet94f8f3b2019-03-11 23:31:18 -07002220 p = genradix_ptr(&fa, i);
Alexey Dobriyan20d28cd2018-02-06 15:37:06 -08002221 len = snprintf(buf, sizeof(buf), "%lx-%lx", p->start, p->end);
Al Virof0c3b502013-05-16 12:07:31 -04002222 if (!proc_fill_cache(file, ctx,
Alexey Dobriyan20d28cd2018-02-06 15:37:06 -08002223 buf, len,
Al Virof0c3b502013-05-16 12:07:31 -04002224 proc_map_files_instantiate,
2225 task,
2226 (void *)(unsigned long)p->mode))
2227 break;
2228 ctx->pos++;
2229 }
Pavel Emelyanov640708a2012-01-10 15:11:23 -08002230
Pavel Emelyanov640708a2012-01-10 15:11:23 -08002231out_put_task:
2232 put_task_struct(task);
2233out:
Kent Overstreet94f8f3b2019-03-11 23:31:18 -07002234 genradix_free(&fa);
Pavel Emelyanov640708a2012-01-10 15:11:23 -08002235 return ret;
2236}
2237
2238static const struct file_operations proc_map_files_operations = {
2239 .read = generic_read_dir,
Al Virof50752e2016-04-20 17:13:54 -04002240 .iterate_shared = proc_map_files_readdir,
2241 .llseek = generic_file_llseek,
Pavel Emelyanov640708a2012-01-10 15:11:23 -08002242};
2243
Nicolas Pitreb18b6a92017-01-21 00:09:08 -05002244#if defined(CONFIG_CHECKPOINT_RESTORE) && defined(CONFIG_POSIX_TIMERS)
Pavel Emelyanov48f6a7a2013-03-11 13:12:45 +04002245struct timers_private {
2246 struct pid *pid;
2247 struct task_struct *task;
2248 struct sighand_struct *sighand;
Pavel Emelyanov57b80152013-03-11 13:13:08 +04002249 struct pid_namespace *ns;
Pavel Emelyanov48f6a7a2013-03-11 13:12:45 +04002250 unsigned long flags;
2251};
2252
2253static void *timers_start(struct seq_file *m, loff_t *pos)
2254{
2255 struct timers_private *tp = m->private;
2256
2257 tp->task = get_pid_task(tp->pid, PIDTYPE_PID);
2258 if (!tp->task)
2259 return ERR_PTR(-ESRCH);
2260
2261 tp->sighand = lock_task_sighand(tp->task, &tp->flags);
2262 if (!tp->sighand)
2263 return ERR_PTR(-ESRCH);
2264
2265 return seq_list_start(&tp->task->signal->posix_timers, *pos);
2266}
2267
2268static void *timers_next(struct seq_file *m, void *v, loff_t *pos)
2269{
2270 struct timers_private *tp = m->private;
2271 return seq_list_next(v, &tp->task->signal->posix_timers, pos);
2272}
2273
2274static void timers_stop(struct seq_file *m, void *v)
2275{
2276 struct timers_private *tp = m->private;
2277
2278 if (tp->sighand) {
2279 unlock_task_sighand(tp->task, &tp->flags);
2280 tp->sighand = NULL;
2281 }
2282
2283 if (tp->task) {
2284 put_task_struct(tp->task);
2285 tp->task = NULL;
2286 }
2287}
2288
2289static int show_timer(struct seq_file *m, void *v)
2290{
2291 struct k_itimer *timer;
Pavel Emelyanov57b80152013-03-11 13:13:08 +04002292 struct timers_private *tp = m->private;
2293 int notify;
Alexey Dobriyancedbcca2014-08-08 14:21:33 -07002294 static const char * const nstr[] = {
Pavel Emelyanov57b80152013-03-11 13:13:08 +04002295 [SIGEV_SIGNAL] = "signal",
2296 [SIGEV_NONE] = "none",
2297 [SIGEV_THREAD] = "thread",
2298 };
Pavel Emelyanov48f6a7a2013-03-11 13:12:45 +04002299
2300 timer = list_entry((struct list_head *)v, struct k_itimer, list);
Pavel Emelyanov57b80152013-03-11 13:13:08 +04002301 notify = timer->it_sigev_notify;
2302
Pavel Emelyanov48f6a7a2013-03-11 13:12:45 +04002303 seq_printf(m, "ID: %d\n", timer->it_id);
Linus Torvaldsba3edf1f2017-12-06 18:23:27 -08002304 seq_printf(m, "signal: %d/%px\n",
Joe Perches25ce3192015-04-15 16:18:17 -07002305 timer->sigq->info.si_signo,
2306 timer->sigq->info.si_value.sival_ptr);
Pavel Emelyanov57b80152013-03-11 13:13:08 +04002307 seq_printf(m, "notify: %s/%s.%d\n",
Joe Perches25ce3192015-04-15 16:18:17 -07002308 nstr[notify & ~SIGEV_THREAD_ID],
2309 (notify & SIGEV_THREAD_ID) ? "tid" : "pid",
2310 pid_nr_ns(timer->it_pid, tp->ns));
Pavel Tikhomirov15ef0292013-05-17 02:12:03 +04002311 seq_printf(m, "ClockID: %d\n", timer->it_clock);
Pavel Emelyanov48f6a7a2013-03-11 13:12:45 +04002312
2313 return 0;
2314}
2315
2316static const struct seq_operations proc_timers_seq_ops = {
2317 .start = timers_start,
2318 .next = timers_next,
2319 .stop = timers_stop,
2320 .show = show_timer,
2321};
2322
2323static int proc_timers_open(struct inode *inode, struct file *file)
2324{
2325 struct timers_private *tp;
2326
2327 tp = __seq_open_private(file, &proc_timers_seq_ops,
2328 sizeof(struct timers_private));
2329 if (!tp)
2330 return -ENOMEM;
2331
2332 tp->pid = proc_pid(inode);
Christoph Hellwig76f668b2018-05-16 07:19:01 +02002333 tp->ns = proc_pid_ns(inode);
Pavel Emelyanov48f6a7a2013-03-11 13:12:45 +04002334 return 0;
2335}
2336
2337static const struct file_operations proc_timers_operations = {
2338 .open = proc_timers_open,
2339 .read = seq_read,
2340 .llseek = seq_lseek,
2341 .release = seq_release_private,
2342};
Eric Engestromb5946be2016-03-17 14:20:57 -07002343#endif
Pavel Emelyanov640708a2012-01-10 15:11:23 -08002344
John Stultz5de23d42016-03-17 14:20:54 -07002345static ssize_t timerslack_ns_write(struct file *file, const char __user *buf,
2346 size_t count, loff_t *offset)
2347{
2348 struct inode *inode = file_inode(file);
2349 struct task_struct *p;
2350 u64 slack_ns;
2351 int err;
2352
2353 err = kstrtoull_from_user(buf, count, 10, &slack_ns);
2354 if (err < 0)
2355 return err;
2356
2357 p = get_proc_task(inode);
2358 if (!p)
2359 return -ESRCH;
2360
John Stultz4b2bd5f2016-10-07 17:02:33 -07002361 if (p != current) {
Benjamin Gordon8da0b4f2019-01-03 15:25:56 -08002362 rcu_read_lock();
2363 if (!ns_capable(__task_cred(p)->user_ns, CAP_SYS_NICE)) {
2364 rcu_read_unlock();
John Stultz4b2bd5f2016-10-07 17:02:33 -07002365 count = -EPERM;
2366 goto out;
2367 }
Benjamin Gordon8da0b4f2019-01-03 15:25:56 -08002368 rcu_read_unlock();
John Stultz5de23d42016-03-17 14:20:54 -07002369
John Stultz4b2bd5f2016-10-07 17:02:33 -07002370 err = security_task_setscheduler(p);
2371 if (err) {
2372 count = err;
2373 goto out;
2374 }
John Stultz904763e2016-10-07 17:02:29 -07002375 }
2376
John Stultz7abbaf92016-10-07 17:02:26 -07002377 task_lock(p);
2378 if (slack_ns == 0)
2379 p->timer_slack_ns = p->default_timer_slack_ns;
2380 else
2381 p->timer_slack_ns = slack_ns;
2382 task_unlock(p);
2383
2384out:
John Stultz5de23d42016-03-17 14:20:54 -07002385 put_task_struct(p);
2386
2387 return count;
2388}
2389
2390static int timerslack_ns_show(struct seq_file *m, void *v)
2391{
2392 struct inode *inode = m->private;
2393 struct task_struct *p;
John Stultz7abbaf92016-10-07 17:02:26 -07002394 int err = 0;
John Stultz5de23d42016-03-17 14:20:54 -07002395
2396 p = get_proc_task(inode);
2397 if (!p)
2398 return -ESRCH;
2399
John Stultz4b2bd5f2016-10-07 17:02:33 -07002400 if (p != current) {
Benjamin Gordon8da0b4f2019-01-03 15:25:56 -08002401 rcu_read_lock();
2402 if (!ns_capable(__task_cred(p)->user_ns, CAP_SYS_NICE)) {
2403 rcu_read_unlock();
John Stultz4b2bd5f2016-10-07 17:02:33 -07002404 err = -EPERM;
2405 goto out;
2406 }
Benjamin Gordon8da0b4f2019-01-03 15:25:56 -08002407 rcu_read_unlock();
2408
John Stultz4b2bd5f2016-10-07 17:02:33 -07002409 err = security_task_getscheduler(p);
2410 if (err)
2411 goto out;
2412 }
John Stultz904763e2016-10-07 17:02:29 -07002413
John Stultz7abbaf92016-10-07 17:02:26 -07002414 task_lock(p);
2415 seq_printf(m, "%llu\n", p->timer_slack_ns);
2416 task_unlock(p);
2417
2418out:
John Stultz5de23d42016-03-17 14:20:54 -07002419 put_task_struct(p);
2420
2421 return err;
2422}
2423
2424static int timerslack_ns_open(struct inode *inode, struct file *filp)
2425{
2426 return single_open(filp, timerslack_ns_show, inode);
2427}
2428
2429static const struct file_operations proc_pid_set_timerslack_ns_operations = {
2430 .open = timerslack_ns_open,
2431 .read = seq_read,
2432 .write = timerslack_ns_write,
2433 .llseek = seq_lseek,
2434 .release = single_release,
2435};
2436
Al Viro0168b9e2018-05-03 09:21:05 -04002437static struct dentry *proc_pident_instantiate(struct dentry *dentry,
2438 struct task_struct *task, const void *ptr)
Eric W. Biederman444ceed2006-10-02 02:18:49 -07002439{
Eric Dumazetc5141e62007-05-08 00:26:15 -07002440 const struct pid_entry *p = ptr;
Eric W. Biederman444ceed2006-10-02 02:18:49 -07002441 struct inode *inode;
2442 struct proc_inode *ei;
Eric W. Biederman444ceed2006-10-02 02:18:49 -07002443
Al Viro0168b9e2018-05-03 09:21:05 -04002444 inode = proc_pid_make_inode(dentry->d_sb, task, p->mode);
Eric W. Biederman444ceed2006-10-02 02:18:49 -07002445 if (!inode)
Al Viro0168b9e2018-05-03 09:21:05 -04002446 return ERR_PTR(-ENOENT);
Eric W. Biederman444ceed2006-10-02 02:18:49 -07002447
2448 ei = PROC_I(inode);
Eric W. Biederman444ceed2006-10-02 02:18:49 -07002449 if (S_ISDIR(inode->i_mode))
Miklos Szeredibfe86842011-10-28 14:13:29 +02002450 set_nlink(inode, 2); /* Use getattr to fix if necessary */
Eric W. Biederman444ceed2006-10-02 02:18:49 -07002451 if (p->iop)
2452 inode->i_op = p->iop;
2453 if (p->fop)
2454 inode->i_fop = p->fop;
2455 ei->op = p->op;
Al Viro1bbc5512018-05-02 21:26:16 -04002456 pid_update_inode(task, inode);
Nick Pigginfb045ad2011-01-07 17:49:55 +11002457 d_set_d_op(dentry, &pid_dentry_operations);
Al Viro0168b9e2018-05-03 09:21:05 -04002458 return d_splice_alias(inode, dentry);
Eric W. Biederman444ceed2006-10-02 02:18:49 -07002459}
2460
Linus Torvalds1da177e2005-04-16 15:20:36 -07002461static struct dentry *proc_pident_lookup(struct inode *dir,
2462 struct dentry *dentry,
Alexey Dobriyand5a572a2019-03-11 23:28:51 -07002463 const struct pid_entry *p,
2464 const struct pid_entry *end)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002465{
Eric W. Biederman99f89552006-06-26 00:25:55 -07002466 struct task_struct *task = get_proc_task(dir);
Al Viro0168b9e2018-05-03 09:21:05 -04002467 struct dentry *res = ERR_PTR(-ENOENT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002468
Eric W. Biederman99f89552006-06-26 00:25:55 -07002469 if (!task)
2470 goto out_no_task;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002471
Eric W. Biederman20cdc892006-10-02 02:17:07 -07002472 /*
2473 * Yes, it does not scale. And it should not. Don't add
2474 * new entries into /proc/<tgid>/ without very good reasons.
2475 */
Alexey Dobriyand5a572a2019-03-11 23:28:51 -07002476 for (; p < end; p++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002477 if (p->len != dentry->d_name.len)
2478 continue;
Alexey Dobriyan26b95132018-06-14 15:27:17 -07002479 if (!memcmp(dentry->d_name.name, p->name, p->len)) {
2480 res = proc_pident_instantiate(dentry, task, p);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002481 break;
Alexey Dobriyan26b95132018-06-14 15:27:17 -07002482 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002483 }
Eric W. Biederman99f89552006-06-26 00:25:55 -07002484 put_task_struct(task);
2485out_no_task:
Al Viro0168b9e2018-05-03 09:21:05 -04002486 return res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002487}
2488
Al Virof0c3b502013-05-16 12:07:31 -04002489static int proc_pident_readdir(struct file *file, struct dir_context *ctx,
Eric Dumazetc5141e62007-05-08 00:26:15 -07002490 const struct pid_entry *ents, unsigned int nents)
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002491{
Al Virof0c3b502013-05-16 12:07:31 -04002492 struct task_struct *task = get_proc_task(file_inode(file));
2493 const struct pid_entry *p;
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002494
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002495 if (!task)
Al Virof0c3b502013-05-16 12:07:31 -04002496 return -ENOENT;
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002497
Al Virof0c3b502013-05-16 12:07:31 -04002498 if (!dir_emit_dots(file, ctx))
2499 goto out;
2500
2501 if (ctx->pos >= nents + 2)
2502 goto out;
2503
Alexey Dobriyanbac5f5d2016-12-12 16:45:28 -08002504 for (p = ents + (ctx->pos - 2); p < ents + nents; p++) {
Al Virof0c3b502013-05-16 12:07:31 -04002505 if (!proc_fill_cache(file, ctx, p->name, p->len,
2506 proc_pident_instantiate, task, p))
2507 break;
2508 ctx->pos++;
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002509 }
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002510out:
Eric W. Biederman61a28782006-10-02 02:18:49 -07002511 put_task_struct(task);
Al Virof0c3b502013-05-16 12:07:31 -04002512 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002513}
2514
Linus Torvalds1da177e2005-04-16 15:20:36 -07002515#ifdef CONFIG_SECURITY
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002516static ssize_t proc_pid_attr_read(struct file * file, char __user * buf,
2517 size_t count, loff_t *ppos)
2518{
Al Viro496ad9a2013-01-23 17:07:38 -05002519 struct inode * inode = file_inode(file);
Al Viro04ff9702007-03-12 16:17:58 +00002520 char *p = NULL;
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002521 ssize_t length;
2522 struct task_struct *task = get_proc_task(inode);
2523
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002524 if (!task)
Al Viro04ff9702007-03-12 16:17:58 +00002525 return -ESRCH;
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002526
Casey Schaufler6d9c9392018-09-21 17:16:59 -07002527 length = security_getprocattr(task, PROC_I(inode)->op.lsm,
Josef "Jeff" Sipek2fddfee2006-12-08 02:36:36 -08002528 (char*)file->f_path.dentry->d_name.name,
Al Viro04ff9702007-03-12 16:17:58 +00002529 &p);
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002530 put_task_struct(task);
Al Viro04ff9702007-03-12 16:17:58 +00002531 if (length > 0)
2532 length = simple_read_from_buffer(buf, count, ppos, p, length);
2533 kfree(p);
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002534 return length;
2535}
2536
2537static ssize_t proc_pid_attr_write(struct file * file, const char __user * buf,
2538 size_t count, loff_t *ppos)
2539{
Al Viro496ad9a2013-01-23 17:07:38 -05002540 struct inode * inode = file_inode(file);
Alexey Dobriyan41089b62018-08-21 21:54:30 -07002541 struct task_struct *task;
Al Virobb646cd2015-12-24 00:16:30 -05002542 void *page;
Alexey Dobriyan41089b62018-08-21 21:54:30 -07002543 int rv;
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002544
Alexey Dobriyan41089b62018-08-21 21:54:30 -07002545 rcu_read_lock();
2546 task = pid_task(proc_pid(inode), PIDTYPE_PID);
2547 if (!task) {
2548 rcu_read_unlock();
2549 return -ESRCH;
2550 }
Stephen Smalleyb21507e2017-01-09 10:07:31 -05002551 /* A task may only write its own attributes. */
Alexey Dobriyan41089b62018-08-21 21:54:30 -07002552 if (current != task) {
2553 rcu_read_unlock();
2554 return -EACCES;
2555 }
Paul Moore35a196b2019-04-19 14:55:12 -04002556 /* Prevent changes to overridden credentials. */
2557 if (current_cred() != current_real_cred()) {
2558 rcu_read_unlock();
2559 return -EBUSY;
2560 }
Alexey Dobriyan41089b62018-08-21 21:54:30 -07002561 rcu_read_unlock();
Stephen Smalleyb21507e2017-01-09 10:07:31 -05002562
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002563 if (count > PAGE_SIZE)
2564 count = PAGE_SIZE;
2565
2566 /* No partial writes. */
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002567 if (*ppos != 0)
Alexey Dobriyan41089b62018-08-21 21:54:30 -07002568 return -EINVAL;
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002569
Al Virobb646cd2015-12-24 00:16:30 -05002570 page = memdup_user(buf, count);
2571 if (IS_ERR(page)) {
Alexey Dobriyan41089b62018-08-21 21:54:30 -07002572 rv = PTR_ERR(page);
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002573 goto out;
Al Virobb646cd2015-12-24 00:16:30 -05002574 }
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002575
David Howells107db7c2009-05-08 13:55:27 +01002576 /* Guard against adverse ptrace interaction */
Alexey Dobriyan41089b62018-08-21 21:54:30 -07002577 rv = mutex_lock_interruptible(&current->signal->cred_guard_mutex);
2578 if (rv < 0)
David Howells107db7c2009-05-08 13:55:27 +01002579 goto out_free;
2580
Casey Schaufler6d9c9392018-09-21 17:16:59 -07002581 rv = security_setprocattr(PROC_I(inode)->op.lsm,
2582 file->f_path.dentry->d_name.name, page,
2583 count);
Stephen Smalleyb21507e2017-01-09 10:07:31 -05002584 mutex_unlock(&current->signal->cred_guard_mutex);
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002585out_free:
Al Virobb646cd2015-12-24 00:16:30 -05002586 kfree(page);
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002587out:
Alexey Dobriyan41089b62018-08-21 21:54:30 -07002588 return rv;
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002589}
2590
Arjan van de Ven00977a52007-02-12 00:55:34 -08002591static const struct file_operations proc_pid_attr_operations = {
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002592 .read = proc_pid_attr_read,
2593 .write = proc_pid_attr_write,
Arnd Bergmann87df8422010-03-17 23:06:02 +01002594 .llseek = generic_file_llseek,
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002595};
2596
Casey Schaufler6d9c9392018-09-21 17:16:59 -07002597#define LSM_DIR_OPS(LSM) \
2598static int proc_##LSM##_attr_dir_iterate(struct file *filp, \
2599 struct dir_context *ctx) \
2600{ \
2601 return proc_pident_readdir(filp, ctx, \
2602 LSM##_attr_dir_stuff, \
2603 ARRAY_SIZE(LSM##_attr_dir_stuff)); \
2604} \
2605\
2606static const struct file_operations proc_##LSM##_attr_dir_ops = { \
2607 .read = generic_read_dir, \
2608 .iterate = proc_##LSM##_attr_dir_iterate, \
2609 .llseek = default_llseek, \
2610}; \
2611\
2612static struct dentry *proc_##LSM##_attr_dir_lookup(struct inode *dir, \
2613 struct dentry *dentry, unsigned int flags) \
2614{ \
2615 return proc_pident_lookup(dir, dentry, \
2616 LSM##_attr_dir_stuff, \
Alexey Dobriyand5a572a2019-03-11 23:28:51 -07002617 LSM##_attr_dir_stuff + ARRAY_SIZE(LSM##_attr_dir_stuff)); \
Casey Schaufler6d9c9392018-09-21 17:16:59 -07002618} \
2619\
2620static const struct inode_operations proc_##LSM##_attr_dir_inode_ops = { \
2621 .lookup = proc_##LSM##_attr_dir_lookup, \
2622 .getattr = pid_getattr, \
2623 .setattr = proc_setattr, \
2624}
2625
2626#ifdef CONFIG_SECURITY_SMACK
2627static const struct pid_entry smack_attr_dir_stuff[] = {
2628 ATTR("smack", "current", 0666),
2629};
2630LSM_DIR_OPS(smack);
2631#endif
2632
Eric Dumazetc5141e62007-05-08 00:26:15 -07002633static const struct pid_entry attr_dir_stuff[] = {
Casey Schaufler6d9c9392018-09-21 17:16:59 -07002634 ATTR(NULL, "current", 0666),
2635 ATTR(NULL, "prev", 0444),
2636 ATTR(NULL, "exec", 0666),
2637 ATTR(NULL, "fscreate", 0666),
2638 ATTR(NULL, "keycreate", 0666),
2639 ATTR(NULL, "sockcreate", 0666),
2640#ifdef CONFIG_SECURITY_SMACK
2641 DIR("smack", 0555,
2642 proc_smack_attr_dir_inode_ops, proc_smack_attr_dir_ops),
2643#endif
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002644};
2645
Al Virof0c3b502013-05-16 12:07:31 -04002646static int proc_attr_dir_readdir(struct file *file, struct dir_context *ctx)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002647{
Al Virof0c3b502013-05-16 12:07:31 -04002648 return proc_pident_readdir(file, ctx,
2649 attr_dir_stuff, ARRAY_SIZE(attr_dir_stuff));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002650}
2651
Arjan van de Ven00977a52007-02-12 00:55:34 -08002652static const struct file_operations proc_attr_dir_operations = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002653 .read = generic_read_dir,
Al Virof50752e2016-04-20 17:13:54 -04002654 .iterate_shared = proc_attr_dir_readdir,
2655 .llseek = generic_file_llseek,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002656};
2657
Eric W. Biederman72d9dcf2006-10-02 02:18:50 -07002658static struct dentry *proc_attr_dir_lookup(struct inode *dir,
Al Viro00cd8dd2012-06-10 17:13:09 -04002659 struct dentry *dentry, unsigned int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002660{
Eric W. Biederman7bcd6b02006-10-02 02:18:56 -07002661 return proc_pident_lookup(dir, dentry,
Alexey Dobriyand5a572a2019-03-11 23:28:51 -07002662 attr_dir_stuff,
2663 attr_dir_stuff + ARRAY_SIZE(attr_dir_stuff));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002664}
2665
Arjan van de Venc5ef1c42007-02-12 00:55:40 -08002666static const struct inode_operations proc_attr_dir_inode_operations = {
Eric W. Biederman72d9dcf2006-10-02 02:18:50 -07002667 .lookup = proc_attr_dir_lookup,
Eric W. Biederman99f89552006-06-26 00:25:55 -07002668 .getattr = pid_getattr,
Linus Torvalds6d76fa52006-07-15 12:26:45 -07002669 .setattr = proc_setattr,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002670};
2671
Linus Torvalds1da177e2005-04-16 15:20:36 -07002672#endif
2673
Christoph Hellwig698ba7b2009-12-15 16:47:37 -08002674#ifdef CONFIG_ELF_CORE
Kawai, Hidehiro3cb4a0b2007-07-19 01:48:28 -07002675static ssize_t proc_coredump_filter_read(struct file *file, char __user *buf,
2676 size_t count, loff_t *ppos)
2677{
Al Viro496ad9a2013-01-23 17:07:38 -05002678 struct task_struct *task = get_proc_task(file_inode(file));
Kawai, Hidehiro3cb4a0b2007-07-19 01:48:28 -07002679 struct mm_struct *mm;
2680 char buffer[PROC_NUMBUF];
2681 size_t len;
2682 int ret;
2683
2684 if (!task)
2685 return -ESRCH;
2686
2687 ret = 0;
2688 mm = get_task_mm(task);
2689 if (mm) {
2690 len = snprintf(buffer, sizeof(buffer), "%08lx\n",
2691 ((mm->flags & MMF_DUMP_FILTER_MASK) >>
2692 MMF_DUMP_FILTER_SHIFT));
2693 mmput(mm);
2694 ret = simple_read_from_buffer(buf, count, ppos, buffer, len);
2695 }
2696
2697 put_task_struct(task);
2698
2699 return ret;
2700}
2701
2702static ssize_t proc_coredump_filter_write(struct file *file,
2703 const char __user *buf,
2704 size_t count,
2705 loff_t *ppos)
2706{
2707 struct task_struct *task;
2708 struct mm_struct *mm;
Kawai, Hidehiro3cb4a0b2007-07-19 01:48:28 -07002709 unsigned int val;
2710 int ret;
2711 int i;
2712 unsigned long mask;
2713
Alexey Dobriyan774636e2015-09-09 15:36:59 -07002714 ret = kstrtouint_from_user(buf, count, 0, &val);
2715 if (ret < 0)
2716 return ret;
Kawai, Hidehiro3cb4a0b2007-07-19 01:48:28 -07002717
2718 ret = -ESRCH;
Al Viro496ad9a2013-01-23 17:07:38 -05002719 task = get_proc_task(file_inode(file));
Kawai, Hidehiro3cb4a0b2007-07-19 01:48:28 -07002720 if (!task)
2721 goto out_no_task;
2722
Kawai, Hidehiro3cb4a0b2007-07-19 01:48:28 -07002723 mm = get_task_mm(task);
2724 if (!mm)
2725 goto out_no_mm;
Colin Ian King41a0c2492015-12-18 14:22:01 -08002726 ret = 0;
Kawai, Hidehiro3cb4a0b2007-07-19 01:48:28 -07002727
2728 for (i = 0, mask = 1; i < MMF_DUMP_FILTER_BITS; i++, mask <<= 1) {
2729 if (val & mask)
2730 set_bit(i + MMF_DUMP_FILTER_SHIFT, &mm->flags);
2731 else
2732 clear_bit(i + MMF_DUMP_FILTER_SHIFT, &mm->flags);
2733 }
2734
2735 mmput(mm);
2736 out_no_mm:
2737 put_task_struct(task);
2738 out_no_task:
Alexey Dobriyan774636e2015-09-09 15:36:59 -07002739 if (ret < 0)
2740 return ret;
2741 return count;
Kawai, Hidehiro3cb4a0b2007-07-19 01:48:28 -07002742}
2743
2744static const struct file_operations proc_coredump_filter_operations = {
2745 .read = proc_coredump_filter_read,
2746 .write = proc_coredump_filter_write,
Arnd Bergmann87df8422010-03-17 23:06:02 +01002747 .llseek = generic_file_llseek,
Kawai, Hidehiro3cb4a0b2007-07-19 01:48:28 -07002748};
2749#endif
2750
Andrew Mortonaba76fd2006-12-10 02:19:48 -08002751#ifdef CONFIG_TASK_IO_ACCOUNTING
Alexey Dobriyan19aadc92014-08-08 14:21:50 -07002752static int do_io_accounting(struct task_struct *task, struct seq_file *m, int whole)
Andrew Mortonaba76fd2006-12-10 02:19:48 -08002753{
Andrea Righi940389b2008-07-28 00:48:12 +02002754 struct task_io_accounting acct = task->ioac;
Andrea Righi59954772008-07-27 17:29:15 +02002755 unsigned long flags;
Vasiliy Kulikov293eb1e2011-07-26 16:08:38 -07002756 int result;
Andrea Righi297c5d92008-07-25 01:48:49 -07002757
Vasiliy Kulikov293eb1e2011-07-26 16:08:38 -07002758 result = mutex_lock_killable(&task->signal->cred_guard_mutex);
2759 if (result)
2760 return result;
2761
Jann Horncaaee622016-01-20 15:00:04 -08002762 if (!ptrace_may_access(task, PTRACE_MODE_READ_FSCREDS)) {
Vasiliy Kulikov293eb1e2011-07-26 16:08:38 -07002763 result = -EACCES;
2764 goto out_unlock;
2765 }
Vasiliy Kulikov1d1221f2011-06-24 16:08:38 +04002766
Andrea Righi59954772008-07-27 17:29:15 +02002767 if (whole && lock_task_sighand(task, &flags)) {
2768 struct task_struct *t = task;
Andrea Righib2d002d2008-07-26 15:22:27 -07002769
Andrea Righi59954772008-07-27 17:29:15 +02002770 task_io_accounting_add(&acct, &task->signal->ioac);
2771 while_each_thread(task, t)
2772 task_io_accounting_add(&acct, &t->ioac);
Andrea Righi297c5d92008-07-25 01:48:49 -07002773
Andrea Righi59954772008-07-27 17:29:15 +02002774 unlock_task_sighand(task, &flags);
Andrea Righi297c5d92008-07-25 01:48:49 -07002775 }
Joe Perches25ce3192015-04-15 16:18:17 -07002776 seq_printf(m,
2777 "rchar: %llu\n"
2778 "wchar: %llu\n"
2779 "syscr: %llu\n"
2780 "syscw: %llu\n"
2781 "read_bytes: %llu\n"
2782 "write_bytes: %llu\n"
2783 "cancelled_write_bytes: %llu\n",
2784 (unsigned long long)acct.rchar,
2785 (unsigned long long)acct.wchar,
2786 (unsigned long long)acct.syscr,
2787 (unsigned long long)acct.syscw,
2788 (unsigned long long)acct.read_bytes,
2789 (unsigned long long)acct.write_bytes,
2790 (unsigned long long)acct.cancelled_write_bytes);
2791 result = 0;
2792
Vasiliy Kulikov293eb1e2011-07-26 16:08:38 -07002793out_unlock:
2794 mutex_unlock(&task->signal->cred_guard_mutex);
2795 return result;
Andrew Mortonaba76fd2006-12-10 02:19:48 -08002796}
Andrea Righi297c5d92008-07-25 01:48:49 -07002797
Alexey Dobriyan19aadc92014-08-08 14:21:50 -07002798static int proc_tid_io_accounting(struct seq_file *m, struct pid_namespace *ns,
2799 struct pid *pid, struct task_struct *task)
Andrea Righi297c5d92008-07-25 01:48:49 -07002800{
Alexey Dobriyan19aadc92014-08-08 14:21:50 -07002801 return do_io_accounting(task, m, 0);
Andrea Righi297c5d92008-07-25 01:48:49 -07002802}
2803
Alexey Dobriyan19aadc92014-08-08 14:21:50 -07002804static int proc_tgid_io_accounting(struct seq_file *m, struct pid_namespace *ns,
2805 struct pid *pid, struct task_struct *task)
Andrea Righi297c5d92008-07-25 01:48:49 -07002806{
Alexey Dobriyan19aadc92014-08-08 14:21:50 -07002807 return do_io_accounting(task, m, 1);
Andrea Righi297c5d92008-07-25 01:48:49 -07002808}
2809#endif /* CONFIG_TASK_IO_ACCOUNTING */
Andrew Mortonaba76fd2006-12-10 02:19:48 -08002810
Eric W. Biederman22d917d2011-11-17 00:11:58 -08002811#ifdef CONFIG_USER_NS
2812static int proc_id_map_open(struct inode *inode, struct file *file,
Fabian Frederickccf94f12014-08-08 14:21:22 -07002813 const struct seq_operations *seq_ops)
Eric W. Biederman22d917d2011-11-17 00:11:58 -08002814{
2815 struct user_namespace *ns = NULL;
2816 struct task_struct *task;
2817 struct seq_file *seq;
2818 int ret = -EINVAL;
2819
2820 task = get_proc_task(inode);
2821 if (task) {
2822 rcu_read_lock();
2823 ns = get_user_ns(task_cred_xxx(task, user_ns));
2824 rcu_read_unlock();
2825 put_task_struct(task);
2826 }
2827 if (!ns)
2828 goto err;
2829
2830 ret = seq_open(file, seq_ops);
2831 if (ret)
2832 goto err_put_ns;
2833
2834 seq = file->private_data;
2835 seq->private = ns;
2836
2837 return 0;
2838err_put_ns:
2839 put_user_ns(ns);
2840err:
2841 return ret;
2842}
2843
2844static int proc_id_map_release(struct inode *inode, struct file *file)
2845{
2846 struct seq_file *seq = file->private_data;
2847 struct user_namespace *ns = seq->private;
2848 put_user_ns(ns);
2849 return seq_release(inode, file);
2850}
2851
2852static int proc_uid_map_open(struct inode *inode, struct file *file)
2853{
2854 return proc_id_map_open(inode, file, &proc_uid_seq_operations);
2855}
2856
2857static int proc_gid_map_open(struct inode *inode, struct file *file)
2858{
2859 return proc_id_map_open(inode, file, &proc_gid_seq_operations);
2860}
2861
Eric W. Biedermanf76d2072012-08-30 01:24:05 -07002862static int proc_projid_map_open(struct inode *inode, struct file *file)
2863{
2864 return proc_id_map_open(inode, file, &proc_projid_seq_operations);
2865}
2866
Eric W. Biederman22d917d2011-11-17 00:11:58 -08002867static const struct file_operations proc_uid_map_operations = {
2868 .open = proc_uid_map_open,
2869 .write = proc_uid_map_write,
2870 .read = seq_read,
2871 .llseek = seq_lseek,
2872 .release = proc_id_map_release,
2873};
2874
2875static const struct file_operations proc_gid_map_operations = {
2876 .open = proc_gid_map_open,
2877 .write = proc_gid_map_write,
2878 .read = seq_read,
2879 .llseek = seq_lseek,
2880 .release = proc_id_map_release,
2881};
Eric W. Biedermanf76d2072012-08-30 01:24:05 -07002882
2883static const struct file_operations proc_projid_map_operations = {
2884 .open = proc_projid_map_open,
2885 .write = proc_projid_map_write,
2886 .read = seq_read,
2887 .llseek = seq_lseek,
2888 .release = proc_id_map_release,
2889};
Eric W. Biederman9cc46512014-12-02 12:27:26 -06002890
2891static int proc_setgroups_open(struct inode *inode, struct file *file)
2892{
2893 struct user_namespace *ns = NULL;
2894 struct task_struct *task;
2895 int ret;
2896
2897 ret = -ESRCH;
2898 task = get_proc_task(inode);
2899 if (task) {
2900 rcu_read_lock();
2901 ns = get_user_ns(task_cred_xxx(task, user_ns));
2902 rcu_read_unlock();
2903 put_task_struct(task);
2904 }
2905 if (!ns)
2906 goto err;
2907
2908 if (file->f_mode & FMODE_WRITE) {
2909 ret = -EACCES;
2910 if (!ns_capable(ns, CAP_SYS_ADMIN))
2911 goto err_put_ns;
2912 }
2913
2914 ret = single_open(file, &proc_setgroups_show, ns);
2915 if (ret)
2916 goto err_put_ns;
2917
2918 return 0;
2919err_put_ns:
2920 put_user_ns(ns);
2921err:
2922 return ret;
2923}
2924
2925static int proc_setgroups_release(struct inode *inode, struct file *file)
2926{
2927 struct seq_file *seq = file->private_data;
2928 struct user_namespace *ns = seq->private;
2929 int ret = single_release(inode, file);
2930 put_user_ns(ns);
2931 return ret;
2932}
2933
2934static const struct file_operations proc_setgroups_operations = {
2935 .open = proc_setgroups_open,
2936 .write = proc_setgroups_write,
2937 .read = seq_read,
2938 .llseek = seq_lseek,
2939 .release = proc_setgroups_release,
2940};
Eric W. Biederman22d917d2011-11-17 00:11:58 -08002941#endif /* CONFIG_USER_NS */
2942
Kees Cook47830722008-10-06 03:11:58 +04002943static int proc_pid_personality(struct seq_file *m, struct pid_namespace *ns,
2944 struct pid *pid, struct task_struct *task)
2945{
Al Viroa9712bc2011-03-23 15:52:50 -04002946 int err = lock_trace(task);
2947 if (!err) {
2948 seq_printf(m, "%08x\n", task->personality);
2949 unlock_trace(task);
2950 }
2951 return err;
Kees Cook47830722008-10-06 03:11:58 +04002952}
2953
Josh Poimboeuf7c23b332017-02-13 19:42:41 -06002954#ifdef CONFIG_LIVEPATCH
2955static int proc_pid_patch_state(struct seq_file *m, struct pid_namespace *ns,
2956 struct pid *pid, struct task_struct *task)
2957{
2958 seq_printf(m, "%d\n", task->patch_state);
2959 return 0;
2960}
2961#endif /* CONFIG_LIVEPATCH */
2962
Alexander Popovc8d12622018-08-17 01:17:01 +03002963#ifdef CONFIG_STACKLEAK_METRICS
2964static int proc_stack_depth(struct seq_file *m, struct pid_namespace *ns,
2965 struct pid *pid, struct task_struct *task)
2966{
2967 unsigned long prev_depth = THREAD_SIZE -
2968 (task->prev_lowest_stack & (THREAD_SIZE - 1));
2969 unsigned long depth = THREAD_SIZE -
2970 (task->lowest_stack & (THREAD_SIZE - 1));
2971
2972 seq_printf(m, "previous stack depth: %lu\nstack depth: %lu\n",
2973 prev_depth, depth);
2974 return 0;
2975}
2976#endif /* CONFIG_STACKLEAK_METRICS */
2977
Eric W. Biederman801199c2006-10-02 02:18:48 -07002978/*
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002979 * Thread groups
2980 */
Arjan van de Ven00977a52007-02-12 00:55:34 -08002981static const struct file_operations proc_task_operations;
Arjan van de Venc5ef1c42007-02-12 00:55:40 -08002982static const struct inode_operations proc_task_inode_operations;
Eric W. Biederman20cdc892006-10-02 02:17:07 -07002983
Eric Dumazetc5141e62007-05-08 00:26:15 -07002984static const struct pid_entry tgid_base_stuff[] = {
Alexey Dobriyan631f9c12008-11-10 01:32:52 +03002985 DIR("task", S_IRUGO|S_IXUGO, proc_task_inode_operations, proc_task_operations),
2986 DIR("fd", S_IRUSR|S_IXUSR, proc_fd_inode_operations, proc_fd_operations),
Pavel Emelyanov640708a2012-01-10 15:11:23 -08002987 DIR("map_files", S_IRUSR|S_IXUSR, proc_map_files_inode_operations, proc_map_files_operations),
Alexey Dobriyan631f9c12008-11-10 01:32:52 +03002988 DIR("fdinfo", S_IRUSR|S_IXUSR, proc_fdinfo_inode_operations, proc_fdinfo_operations),
Eric W. Biederman6b4e3062010-03-07 16:41:34 -08002989 DIR("ns", S_IRUSR|S_IXUGO, proc_ns_dir_inode_operations, proc_ns_dir_operations),
Andrew Mortonb2211a32008-03-11 18:03:35 -07002990#ifdef CONFIG_NET
Alexey Dobriyan631f9c12008-11-10 01:32:52 +03002991 DIR("net", S_IRUGO|S_IXUGO, proc_net_inode_operations, proc_net_operations),
Andrew Mortonb2211a32008-03-11 18:03:35 -07002992#endif
Alexey Dobriyan631f9c12008-11-10 01:32:52 +03002993 REG("environ", S_IRUSR, proc_environ_operations),
Al Viroc5317162016-10-05 18:43:43 -04002994 REG("auxv", S_IRUSR, proc_auxv_operations),
Alexey Dobriyan631f9c12008-11-10 01:32:52 +03002995 ONE("status", S_IRUGO, proc_pid_status),
Djalal Harouni35a35042014-04-07 15:38:36 -07002996 ONE("personality", S_IRUSR, proc_pid_personality),
Alexey Dobriyan1c963eb2014-08-08 14:21:37 -07002997 ONE("limits", S_IRUGO, proc_pid_limits),
Ingo Molnar43ae34c2007-07-09 18:52:00 +02002998#ifdef CONFIG_SCHED_DEBUG
Alexey Dobriyan631f9c12008-11-10 01:32:52 +03002999 REG("sched", S_IRUGO|S_IWUSR, proc_pid_sched_operations),
Ingo Molnar43ae34c2007-07-09 18:52:00 +02003000#endif
Mike Galbraith5091faa2010-11-30 14:18:03 +01003001#ifdef CONFIG_SCHED_AUTOGROUP
3002 REG("autogroup", S_IRUGO|S_IWUSR, proc_pid_sched_autogroup_operations),
3003#endif
john stultz4614a696b2009-12-14 18:00:05 -08003004 REG("comm", S_IRUGO|S_IWUSR, proc_pid_set_comm_operations),
Roland McGrathebcb6732008-07-25 19:46:00 -07003005#ifdef CONFIG_HAVE_ARCH_TRACEHOOK
Alexey Dobriyan09d93bd2014-08-08 14:21:39 -07003006 ONE("syscall", S_IRUSR, proc_pid_syscall),
Roland McGrathebcb6732008-07-25 19:46:00 -07003007#endif
Alexey Dobriyanc2c0bb42015-06-25 15:00:54 -07003008 REG("cmdline", S_IRUGO, proc_pid_cmdline_ops),
Alexey Dobriyan631f9c12008-11-10 01:32:52 +03003009 ONE("stat", S_IRUGO, proc_tgid_stat),
3010 ONE("statm", S_IRUGO, proc_pid_statm),
Siddhesh Poyarekarb7643752012-03-21 16:34:04 -07003011 REG("maps", S_IRUGO, proc_pid_maps_operations),
Eric W. Biederman28a6d672006-10-02 02:17:05 -07003012#ifdef CONFIG_NUMA
Siddhesh Poyarekarb7643752012-03-21 16:34:04 -07003013 REG("numa_maps", S_IRUGO, proc_pid_numa_maps_operations),
Eric W. Biederman28a6d672006-10-02 02:17:05 -07003014#endif
Alexey Dobriyan631f9c12008-11-10 01:32:52 +03003015 REG("mem", S_IRUSR|S_IWUSR, proc_mem_operations),
3016 LNK("cwd", proc_cwd_link),
3017 LNK("root", proc_root_link),
3018 LNK("exe", proc_exe_link),
3019 REG("mounts", S_IRUGO, proc_mounts_operations),
3020 REG("mountinfo", S_IRUGO, proc_mountinfo_operations),
3021 REG("mountstats", S_IRUSR, proc_mountstats_operations),
Matt Mackall1e883282008-02-04 22:29:07 -08003022#ifdef CONFIG_PROC_PAGE_MONITOR
Alexey Dobriyan631f9c12008-11-10 01:32:52 +03003023 REG("clear_refs", S_IWUSR, proc_clear_refs_operations),
Siddhesh Poyarekarb7643752012-03-21 16:34:04 -07003024 REG("smaps", S_IRUGO, proc_pid_smaps_operations),
Daniel Colascione493b0e92017-09-06 16:25:08 -07003025 REG("smaps_rollup", S_IRUGO, proc_pid_smaps_rollup_operations),
Djalal Harouni32ed74a2014-04-07 15:38:38 -07003026 REG("pagemap", S_IRUSR, proc_pagemap_operations),
Eric W. Biederman28a6d672006-10-02 02:17:05 -07003027#endif
3028#ifdef CONFIG_SECURITY
Alexey Dobriyan631f9c12008-11-10 01:32:52 +03003029 DIR("attr", S_IRUGO|S_IXUGO, proc_attr_dir_inode_operations, proc_attr_dir_operations),
Eric W. Biederman28a6d672006-10-02 02:17:05 -07003030#endif
3031#ifdef CONFIG_KALLSYMS
Alexey Dobriyanedfcd602014-08-08 14:21:44 -07003032 ONE("wchan", S_IRUGO, proc_pid_wchan),
Eric W. Biederman28a6d672006-10-02 02:17:05 -07003033#endif
Ken Chen2ec220e2008-11-10 11:26:08 +03003034#ifdef CONFIG_STACKTRACE
Djalal Harouni35a35042014-04-07 15:38:36 -07003035 ONE("stack", S_IRUSR, proc_pid_stack),
Eric W. Biederman28a6d672006-10-02 02:17:05 -07003036#endif
Naveen N. Rao5968cec2015-06-30 14:36:03 +05303037#ifdef CONFIG_SCHED_INFO
Alexey Dobriyanf6e826c2014-08-08 14:21:46 -07003038 ONE("schedstat", S_IRUGO, proc_pid_schedstat),
Eric W. Biederman28a6d672006-10-02 02:17:05 -07003039#endif
Arjan van de Ven97455122008-01-25 21:08:34 +01003040#ifdef CONFIG_LATENCYTOP
Alexey Dobriyan631f9c12008-11-10 01:32:52 +03003041 REG("latency", S_IRUGO, proc_lstats_operations),
Arjan van de Ven97455122008-01-25 21:08:34 +01003042#endif
Paul Menage8793d852007-10-18 23:39:39 -07003043#ifdef CONFIG_PROC_PID_CPUSET
Zefan Li52de4772014-09-18 16:03:36 +08003044 ONE("cpuset", S_IRUGO, proc_cpuset_show),
Eric W. Biederman28a6d672006-10-02 02:17:05 -07003045#endif
Paul Menagea4243162007-10-18 23:39:35 -07003046#ifdef CONFIG_CGROUPS
Zefan Li006f4ac2014-09-18 16:03:15 +08003047 ONE("cgroup", S_IRUGO, proc_cgroup_show),
Paul Menagea4243162007-10-18 23:39:35 -07003048#endif
Alexey Dobriyan6ba51e32014-08-08 14:21:48 -07003049 ONE("oom_score", S_IRUGO, proc_oom_score),
David Rientjesfa0cbbf2012-11-12 17:53:04 -08003050 REG("oom_adj", S_IRUGO|S_IWUSR, proc_oom_adj_operations),
David Rientjesa63d83f2010-08-09 17:19:46 -07003051 REG("oom_score_adj", S_IRUGO|S_IWUSR, proc_oom_score_adj_operations),
Richard Guy Briggs4b7d2482019-01-22 17:06:39 -05003052#ifdef CONFIG_AUDIT
Alexey Dobriyan631f9c12008-11-10 01:32:52 +03003053 REG("loginuid", S_IWUSR|S_IRUGO, proc_loginuid_operations),
3054 REG("sessionid", S_IRUGO, proc_sessionid_operations),
Eric W. Biederman28a6d672006-10-02 02:17:05 -07003055#endif
Akinobu Mitaf4f154f2006-12-08 02:39:47 -08003056#ifdef CONFIG_FAULT_INJECTION
Alexey Dobriyan631f9c12008-11-10 01:32:52 +03003057 REG("make-it-fail", S_IRUGO|S_IWUSR, proc_fault_inject_operations),
Akinobu Mita168c42b2017-07-14 14:50:00 -07003058 REG("fail-nth", 0644, proc_fail_nth_operations),
Akinobu Mitaf4f154f2006-12-08 02:39:47 -08003059#endif
Christoph Hellwig698ba7b2009-12-15 16:47:37 -08003060#ifdef CONFIG_ELF_CORE
Alexey Dobriyan631f9c12008-11-10 01:32:52 +03003061 REG("coredump_filter", S_IRUGO|S_IWUSR, proc_coredump_filter_operations),
Kawai, Hidehiro3cb4a0b2007-07-19 01:48:28 -07003062#endif
Andrew Mortonaba76fd2006-12-10 02:19:48 -08003063#ifdef CONFIG_TASK_IO_ACCOUNTING
Alexey Dobriyan19aadc92014-08-08 14:21:50 -07003064 ONE("io", S_IRUSR, proc_tgid_io_accounting),
Andrew Mortonaba76fd2006-12-10 02:19:48 -08003065#endif
Eric W. Biederman22d917d2011-11-17 00:11:58 -08003066#ifdef CONFIG_USER_NS
3067 REG("uid_map", S_IRUGO|S_IWUSR, proc_uid_map_operations),
3068 REG("gid_map", S_IRUGO|S_IWUSR, proc_gid_map_operations),
Eric W. Biedermanf76d2072012-08-30 01:24:05 -07003069 REG("projid_map", S_IRUGO|S_IWUSR, proc_projid_map_operations),
Eric W. Biederman9cc46512014-12-02 12:27:26 -06003070 REG("setgroups", S_IRUGO|S_IWUSR, proc_setgroups_operations),
Eric W. Biederman22d917d2011-11-17 00:11:58 -08003071#endif
Nicolas Pitreb18b6a92017-01-21 00:09:08 -05003072#if defined(CONFIG_CHECKPOINT_RESTORE) && defined(CONFIG_POSIX_TIMERS)
Pavel Emelyanov48f6a7a2013-03-11 13:12:45 +04003073 REG("timers", S_IRUGO, proc_timers_operations),
3074#endif
John Stultz5de23d42016-03-17 14:20:54 -07003075 REG("timerslack_ns", S_IRUGO|S_IWUGO, proc_pid_set_timerslack_ns_operations),
Josh Poimboeuf7c23b332017-02-13 19:42:41 -06003076#ifdef CONFIG_LIVEPATCH
3077 ONE("patch_state", S_IRUSR, proc_pid_patch_state),
3078#endif
Alexander Popovc8d12622018-08-17 01:17:01 +03003079#ifdef CONFIG_STACKLEAK_METRICS
3080 ONE("stack_depth", S_IRUGO, proc_stack_depth),
3081#endif
Eric W. Biederman28a6d672006-10-02 02:17:05 -07003082};
3083
Al Virof0c3b502013-05-16 12:07:31 -04003084static int proc_tgid_base_readdir(struct file *file, struct dir_context *ctx)
Eric W. Biederman28a6d672006-10-02 02:17:05 -07003085{
Al Virof0c3b502013-05-16 12:07:31 -04003086 return proc_pident_readdir(file, ctx,
3087 tgid_base_stuff, ARRAY_SIZE(tgid_base_stuff));
Eric W. Biederman28a6d672006-10-02 02:17:05 -07003088}
3089
Arjan van de Ven00977a52007-02-12 00:55:34 -08003090static const struct file_operations proc_tgid_base_operations = {
Eric W. Biederman28a6d672006-10-02 02:17:05 -07003091 .read = generic_read_dir,
Al Virof50752e2016-04-20 17:13:54 -04003092 .iterate_shared = proc_tgid_base_readdir,
3093 .llseek = generic_file_llseek,
Eric W. Biederman28a6d672006-10-02 02:17:05 -07003094};
3095
Christian Brauner3eb39f42018-11-19 00:51:56 +01003096struct pid *tgid_pidfd_to_pid(const struct file *file)
3097{
Christian Brauner30d158b2019-06-27 11:35:14 +02003098 if (file->f_op != &proc_tgid_base_operations)
Christian Brauner3eb39f42018-11-19 00:51:56 +01003099 return ERR_PTR(-EBADF);
3100
3101 return proc_pid(file_inode(file));
3102}
3103
Al Viro00cd8dd2012-06-10 17:13:09 -04003104static struct dentry *proc_tgid_base_lookup(struct inode *dir, struct dentry *dentry, unsigned int flags)
3105{
Eric W. Biederman7bcd6b02006-10-02 02:18:56 -07003106 return proc_pident_lookup(dir, dentry,
Alexey Dobriyand5a572a2019-03-11 23:28:51 -07003107 tgid_base_stuff,
3108 tgid_base_stuff + ARRAY_SIZE(tgid_base_stuff));
Eric W. Biederman28a6d672006-10-02 02:17:05 -07003109}
3110
Arjan van de Venc5ef1c42007-02-12 00:55:40 -08003111static const struct inode_operations proc_tgid_base_inode_operations = {
Eric W. Biederman28a6d672006-10-02 02:17:05 -07003112 .lookup = proc_tgid_base_lookup,
3113 .getattr = pid_getattr,
3114 .setattr = proc_setattr,
Vasiliy Kulikov04996802012-01-10 15:11:31 -08003115 .permission = proc_pid_permission,
Eric W. Biederman28a6d672006-10-02 02:17:05 -07003116};
3117
Pavel Emelyanov60347f62007-10-18 23:40:03 -07003118static void proc_flush_task_mnt(struct vfsmount *mnt, pid_t pid, pid_t tgid)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003119{
Eric W. Biederman48e64842006-06-26 00:25:48 -07003120 struct dentry *dentry, *leader, *dir;
Alexey Dobriyane3912ac2018-02-06 15:36:51 -08003121 char buf[10 + 1];
Eric W. Biederman48e64842006-06-26 00:25:48 -07003122 struct qstr name;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003123
Eric W. Biederman48e64842006-06-26 00:25:48 -07003124 name.name = buf;
Alexey Dobriyane3912ac2018-02-06 15:36:51 -08003125 name.len = snprintf(buf, sizeof(buf), "%u", pid);
Al Viro4f522a22013-02-11 23:20:37 -05003126 /* no ->d_hash() rejects on procfs */
Pavel Emelyanov60347f62007-10-18 23:40:03 -07003127 dentry = d_hash_and_lookup(mnt->mnt_root, &name);
Eric W. Biederman48e64842006-06-26 00:25:48 -07003128 if (dentry) {
Eric W. Biedermanbbd51922014-02-13 10:24:23 -08003129 d_invalidate(dentry);
Eric W. Biederman48e64842006-06-26 00:25:48 -07003130 dput(dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003131 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003132
Oleg Nesterovc35a7f12014-12-10 15:54:56 -08003133 if (pid == tgid)
3134 return;
3135
Eric W. Biederman48e64842006-06-26 00:25:48 -07003136 name.name = buf;
Alexey Dobriyane3912ac2018-02-06 15:36:51 -08003137 name.len = snprintf(buf, sizeof(buf), "%u", tgid);
Pavel Emelyanov60347f62007-10-18 23:40:03 -07003138 leader = d_hash_and_lookup(mnt->mnt_root, &name);
Eric W. Biederman48e64842006-06-26 00:25:48 -07003139 if (!leader)
3140 goto out;
3141
3142 name.name = "task";
3143 name.len = strlen(name.name);
3144 dir = d_hash_and_lookup(leader, &name);
3145 if (!dir)
3146 goto out_put_leader;
3147
3148 name.name = buf;
Alexey Dobriyane3912ac2018-02-06 15:36:51 -08003149 name.len = snprintf(buf, sizeof(buf), "%u", pid);
Eric W. Biederman48e64842006-06-26 00:25:48 -07003150 dentry = d_hash_and_lookup(dir, &name);
3151 if (dentry) {
Eric W. Biedermanbbd51922014-02-13 10:24:23 -08003152 d_invalidate(dentry);
Eric W. Biederman48e64842006-06-26 00:25:48 -07003153 dput(dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003154 }
Eric W. Biederman48e64842006-06-26 00:25:48 -07003155
3156 dput(dir);
3157out_put_leader:
3158 dput(leader);
3159out:
3160 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003161}
3162
Randy Dunlap0895e912007-10-21 21:00:10 -07003163/**
3164 * proc_flush_task - Remove dcache entries for @task from the /proc dcache.
3165 * @task: task that should be flushed.
3166 *
3167 * When flushing dentries from proc, one needs to flush them from global
Pavel Emelyanov60347f62007-10-18 23:40:03 -07003168 * proc (proc_mnt) and from all the namespaces' procs this task was seen
Randy Dunlap0895e912007-10-21 21:00:10 -07003169 * in. This call is supposed to do all of this job.
3170 *
3171 * Looks in the dcache for
3172 * /proc/@pid
3173 * /proc/@tgid/task/@pid
3174 * if either directory is present flushes it and all of it'ts children
3175 * from the dcache.
3176 *
3177 * It is safe and reasonable to cache /proc entries for a task until
3178 * that task exits. After that they just clog up the dcache with
3179 * useless entries, possibly causing useful dcache entries to be
3180 * flushed instead. This routine is proved to flush those useless
3181 * dcache entries at process exit time.
3182 *
3183 * NOTE: This routine is just an optimization so it does not guarantee
3184 * that no dcache entries will exist at process exit time it
3185 * just makes it very unlikely that any will persist.
Pavel Emelyanov60347f62007-10-18 23:40:03 -07003186 */
3187
3188void proc_flush_task(struct task_struct *task)
3189{
Eric W. Biederman9fcc2d12007-11-14 17:00:07 -08003190 int i;
Oleg Nesterov9b4d1cbe2009-09-22 16:45:34 -07003191 struct pid *pid, *tgid;
Pavel Emelyanov130f77e2007-10-18 23:40:11 -07003192 struct upid *upid;
3193
Pavel Emelyanov130f77e2007-10-18 23:40:11 -07003194 pid = task_pid(task);
Oleg Nesterov9b4d1cbe2009-09-22 16:45:34 -07003195 tgid = task_tgid(task);
Pavel Emelyanov130f77e2007-10-18 23:40:11 -07003196
Eric W. Biederman9fcc2d12007-11-14 17:00:07 -08003197 for (i = 0; i <= pid->level; i++) {
Pavel Emelyanov130f77e2007-10-18 23:40:11 -07003198 upid = &pid->numbers[i];
3199 proc_flush_task_mnt(upid->ns->proc_mnt, upid->nr,
Oleg Nesterov9b4d1cbe2009-09-22 16:45:34 -07003200 tgid->numbers[i].nr);
Pavel Emelyanov130f77e2007-10-18 23:40:11 -07003201 }
Pavel Emelyanov60347f62007-10-18 23:40:03 -07003202}
3203
Al Viro0168b9e2018-05-03 09:21:05 -04003204static struct dentry *proc_pid_instantiate(struct dentry * dentry,
Al Viroc52a47a2013-06-15 11:15:20 +04003205 struct task_struct *task, const void *ptr)
Eric W. Biederman444ceed2006-10-02 02:18:49 -07003206{
Eric W. Biederman444ceed2006-10-02 02:18:49 -07003207 struct inode *inode;
3208
Al Viro0168b9e2018-05-03 09:21:05 -04003209 inode = proc_pid_make_inode(dentry->d_sb, task, S_IFDIR | S_IRUGO | S_IXUGO);
Eric W. Biederman444ceed2006-10-02 02:18:49 -07003210 if (!inode)
Al Viro0168b9e2018-05-03 09:21:05 -04003211 return ERR_PTR(-ENOENT);
Eric W. Biederman444ceed2006-10-02 02:18:49 -07003212
Eric W. Biederman444ceed2006-10-02 02:18:49 -07003213 inode->i_op = &proc_tgid_base_inode_operations;
3214 inode->i_fop = &proc_tgid_base_operations;
3215 inode->i_flags|=S_IMMUTABLE;
Vegard Nossumaed54172008-06-05 22:46:53 -07003216
Alexey Dobriyan1270dd82016-12-12 16:45:32 -08003217 set_nlink(inode, nlink_tgid);
Al Viro1bbc5512018-05-02 21:26:16 -04003218 pid_update_inode(task, inode);
Eric W. Biederman444ceed2006-10-02 02:18:49 -07003219
Nick Pigginfb045ad2011-01-07 17:49:55 +11003220 d_set_d_op(dentry, &pid_dentry_operations);
Al Viro0168b9e2018-05-03 09:21:05 -04003221 return d_splice_alias(inode, dentry);
Eric W. Biederman444ceed2006-10-02 02:18:49 -07003222}
3223
Zhikang Zhang867aacc2019-03-05 15:50:29 -08003224struct dentry *proc_pid_lookup(struct dentry *dentry, unsigned int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003225{
3226 struct task_struct *task;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003227 unsigned tgid;
Pavel Emelyanovb4888932007-10-18 23:40:14 -07003228 struct pid_namespace *ns;
Al Viro0168b9e2018-05-03 09:21:05 -04003229 struct dentry *result = ERR_PTR(-ENOENT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003230
Alexey Dobriyandbcdb502014-08-08 14:21:25 -07003231 tgid = name_to_int(&dentry->d_name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003232 if (tgid == ~0U)
3233 goto out;
3234
Pavel Emelyanovb4888932007-10-18 23:40:14 -07003235 ns = dentry->d_sb->s_fs_info;
Eric W. Biedermande758732006-06-26 00:25:51 -07003236 rcu_read_lock();
Pavel Emelyanovb4888932007-10-18 23:40:14 -07003237 task = find_task_by_pid_ns(tgid, ns);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003238 if (task)
3239 get_task_struct(task);
Eric W. Biedermande758732006-06-26 00:25:51 -07003240 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07003241 if (!task)
3242 goto out;
3243
Al Viro0168b9e2018-05-03 09:21:05 -04003244 result = proc_pid_instantiate(dentry, task, NULL);
Eric W. Biederman48e64842006-06-26 00:25:48 -07003245 put_task_struct(task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003246out:
Al Viro0168b9e2018-05-03 09:21:05 -04003247 return result;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003248}
3249
Linus Torvalds1da177e2005-04-16 15:20:36 -07003250/*
Eric W. Biederman0804ef42006-10-02 02:17:04 -07003251 * Find the first task with tgid >= tgid
Eric W. Biederman0bc58a92006-06-26 00:25:50 -07003252 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07003253 */
Eric W. Biederman19fd4bb2007-11-28 16:21:26 -08003254struct tgid_iter {
3255 unsigned int tgid;
Eric W. Biederman0804ef42006-10-02 02:17:04 -07003256 struct task_struct *task;
Eric W. Biederman19fd4bb2007-11-28 16:21:26 -08003257};
3258static struct tgid_iter next_tgid(struct pid_namespace *ns, struct tgid_iter iter)
3259{
Eric W. Biederman0804ef42006-10-02 02:17:04 -07003260 struct pid *pid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003261
Eric W. Biederman19fd4bb2007-11-28 16:21:26 -08003262 if (iter.task)
3263 put_task_struct(iter.task);
Eric W. Biederman0804ef42006-10-02 02:17:04 -07003264 rcu_read_lock();
3265retry:
Eric W. Biederman19fd4bb2007-11-28 16:21:26 -08003266 iter.task = NULL;
3267 pid = find_ge_pid(iter.tgid, ns);
Eric W. Biederman0804ef42006-10-02 02:17:04 -07003268 if (pid) {
Eric W. Biederman19fd4bb2007-11-28 16:21:26 -08003269 iter.tgid = pid_nr_ns(pid, ns);
3270 iter.task = pid_task(pid, PIDTYPE_PID);
Eric W. Biederman0804ef42006-10-02 02:17:04 -07003271 /* What we to know is if the pid we have find is the
3272 * pid of a thread_group_leader. Testing for task
3273 * being a thread_group_leader is the obvious thing
3274 * todo but there is a window when it fails, due to
3275 * the pid transfer logic in de_thread.
3276 *
3277 * So we perform the straight forward test of seeing
3278 * if the pid we have found is the pid of a thread
3279 * group leader, and don't worry if the task we have
3280 * found doesn't happen to be a thread group leader.
3281 * As we don't care in the case of readdir.
3282 */
Eric W. Biederman19fd4bb2007-11-28 16:21:26 -08003283 if (!iter.task || !has_group_leader_pid(iter.task)) {
3284 iter.tgid += 1;
Eric W. Biederman0804ef42006-10-02 02:17:04 -07003285 goto retry;
Eric W. Biederman19fd4bb2007-11-28 16:21:26 -08003286 }
3287 get_task_struct(iter.task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003288 }
Eric W. Biederman454cc102006-06-26 00:25:51 -07003289 rcu_read_unlock();
Eric W. Biederman19fd4bb2007-11-28 16:21:26 -08003290 return iter;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003291}
3292
Eric W. Biederman00978752014-07-31 03:10:50 -07003293#define TGID_OFFSET (FIRST_PROCESS_ENTRY + 2)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003294
3295/* for the /proc/ directory itself, after non-process stuff has been done */
Al Virof0c3b502013-05-16 12:07:31 -04003296int proc_pid_readdir(struct file *file, struct dir_context *ctx)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003297{
Eric W. Biederman19fd4bb2007-11-28 16:21:26 -08003298 struct tgid_iter iter;
Christoph Hellwig76f668b2018-05-16 07:19:01 +02003299 struct pid_namespace *ns = proc_pid_ns(file_inode(file));
Al Virof0c3b502013-05-16 12:07:31 -04003300 loff_t pos = ctx->pos;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003301
Al Viro021ada72013-03-29 19:27:05 -04003302 if (pos >= PID_MAX_LIMIT + TGID_OFFSET)
Al Virof0c3b502013-05-16 12:07:31 -04003303 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003304
Eric W. Biederman00978752014-07-31 03:10:50 -07003305 if (pos == TGID_OFFSET - 2) {
David Howells2b0143b2015-03-17 22:25:59 +00003306 struct inode *inode = d_inode(ns->proc_self);
Al Virodb963162013-06-15 10:45:10 +04003307 if (!dir_emit(ctx, "self", 4, inode->i_ino, DT_LNK))
Al Virof0c3b502013-05-16 12:07:31 -04003308 return 0;
Eric W. Biederman00978752014-07-31 03:10:50 -07003309 ctx->pos = pos = pos + 1;
Al Viro021ada72013-03-29 19:27:05 -04003310 }
Eric W. Biederman00978752014-07-31 03:10:50 -07003311 if (pos == TGID_OFFSET - 1) {
David Howells2b0143b2015-03-17 22:25:59 +00003312 struct inode *inode = d_inode(ns->proc_thread_self);
Eric W. Biederman00978752014-07-31 03:10:50 -07003313 if (!dir_emit(ctx, "thread-self", 11, inode->i_ino, DT_LNK))
3314 return 0;
3315 ctx->pos = pos = pos + 1;
3316 }
3317 iter.tgid = pos - TGID_OFFSET;
Eric W. Biederman19fd4bb2007-11-28 16:21:26 -08003318 iter.task = NULL;
Eric W. Biederman19fd4bb2007-11-28 16:21:26 -08003319 for (iter = next_tgid(ns, iter);
3320 iter.task;
3321 iter.tgid += 1, iter = next_tgid(ns, iter)) {
Alexey Dobriyane3912ac2018-02-06 15:36:51 -08003322 char name[10 + 1];
Alexey Dobriyana4ef3892018-06-07 17:10:10 -07003323 unsigned int len;
Eric Dumazet3ba4bce2017-01-24 15:18:07 -08003324
3325 cond_resched();
Lafcadio Wluiki796f5712017-02-24 15:00:23 -08003326 if (!has_pid_permissions(ns, iter.task, HIDEPID_INVISIBLE))
Al Virof0c3b502013-05-16 12:07:31 -04003327 continue;
Vasiliy Kulikov04996802012-01-10 15:11:31 -08003328
Alexey Dobriyane3912ac2018-02-06 15:36:51 -08003329 len = snprintf(name, sizeof(name), "%u", iter.tgid);
Al Virof0c3b502013-05-16 12:07:31 -04003330 ctx->pos = iter.tgid + TGID_OFFSET;
3331 if (!proc_fill_cache(file, ctx, name, len,
3332 proc_pid_instantiate, iter.task, NULL)) {
Eric W. Biederman19fd4bb2007-11-28 16:21:26 -08003333 put_task_struct(iter.task);
Al Virof0c3b502013-05-16 12:07:31 -04003334 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003335 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003336 }
Al Virof0c3b502013-05-16 12:07:31 -04003337 ctx->pos = PID_MAX_LIMIT + TGID_OFFSET;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003338 return 0;
3339}
3340
Eric W. Biederman0bc58a92006-06-26 00:25:50 -07003341/*
Janis Danisevskis1b3044e2016-05-20 17:00:08 -07003342 * proc_tid_comm_permission is a special permission function exclusively
3343 * used for the node /proc/<pid>/task/<tid>/comm.
3344 * It bypasses generic permission checks in the case where a task of the same
3345 * task group attempts to access the node.
3346 * The rationale behind this is that glibc and bionic access this node for
3347 * cross thread naming (pthread_set/getname_np(!self)). However, if
3348 * PR_SET_DUMPABLE gets set to 0 this node among others becomes uid=0 gid=0,
3349 * which locks out the cross thread naming implementation.
3350 * This function makes sure that the node is always accessible for members of
3351 * same thread group.
3352 */
3353static int proc_tid_comm_permission(struct inode *inode, int mask)
3354{
3355 bool is_same_tgroup;
3356 struct task_struct *task;
3357
3358 task = get_proc_task(inode);
3359 if (!task)
3360 return -ESRCH;
3361 is_same_tgroup = same_thread_group(current, task);
3362 put_task_struct(task);
3363
3364 if (likely(is_same_tgroup && !(mask & MAY_EXEC))) {
3365 /* This file (/proc/<pid>/task/<tid>/comm) can always be
3366 * read or written by the members of the corresponding
3367 * thread group.
3368 */
3369 return 0;
3370 }
3371
3372 return generic_permission(inode, mask);
3373}
3374
3375static const struct inode_operations proc_tid_comm_inode_operations = {
3376 .permission = proc_tid_comm_permission,
3377};
3378
3379/*
Eric W. Biederman28a6d672006-10-02 02:17:05 -07003380 * Tasks
3381 */
Eric Dumazetc5141e62007-05-08 00:26:15 -07003382static const struct pid_entry tid_base_stuff[] = {
Alexey Dobriyan631f9c12008-11-10 01:32:52 +03003383 DIR("fd", S_IRUSR|S_IXUSR, proc_fd_inode_operations, proc_fd_operations),
Jerome Marchand38355412010-04-27 13:13:06 -07003384 DIR("fdinfo", S_IRUSR|S_IXUSR, proc_fdinfo_inode_operations, proc_fdinfo_operations),
Eric W. Biederman6b4e3062010-03-07 16:41:34 -08003385 DIR("ns", S_IRUSR|S_IXUGO, proc_ns_dir_inode_operations, proc_ns_dir_operations),
Eric W. Biederman6ba8ed72014-07-31 16:27:08 -07003386#ifdef CONFIG_NET
3387 DIR("net", S_IRUGO|S_IXUGO, proc_net_inode_operations, proc_net_operations),
3388#endif
Alexey Dobriyan631f9c12008-11-10 01:32:52 +03003389 REG("environ", S_IRUSR, proc_environ_operations),
Al Viroc5317162016-10-05 18:43:43 -04003390 REG("auxv", S_IRUSR, proc_auxv_operations),
Alexey Dobriyan631f9c12008-11-10 01:32:52 +03003391 ONE("status", S_IRUGO, proc_pid_status),
Djalal Harouni35a35042014-04-07 15:38:36 -07003392 ONE("personality", S_IRUSR, proc_pid_personality),
Alexey Dobriyan1c963eb2014-08-08 14:21:37 -07003393 ONE("limits", S_IRUGO, proc_pid_limits),
Ingo Molnar43ae34c2007-07-09 18:52:00 +02003394#ifdef CONFIG_SCHED_DEBUG
Alexey Dobriyan631f9c12008-11-10 01:32:52 +03003395 REG("sched", S_IRUGO|S_IWUSR, proc_pid_sched_operations),
Ingo Molnar43ae34c2007-07-09 18:52:00 +02003396#endif
Janis Danisevskis1b3044e2016-05-20 17:00:08 -07003397 NOD("comm", S_IFREG|S_IRUGO|S_IWUSR,
3398 &proc_tid_comm_inode_operations,
3399 &proc_pid_set_comm_operations, {}),
Roland McGrathebcb6732008-07-25 19:46:00 -07003400#ifdef CONFIG_HAVE_ARCH_TRACEHOOK
Alexey Dobriyan09d93bd2014-08-08 14:21:39 -07003401 ONE("syscall", S_IRUSR, proc_pid_syscall),
Roland McGrathebcb6732008-07-25 19:46:00 -07003402#endif
Alexey Dobriyanc2c0bb42015-06-25 15:00:54 -07003403 REG("cmdline", S_IRUGO, proc_pid_cmdline_ops),
Alexey Dobriyan631f9c12008-11-10 01:32:52 +03003404 ONE("stat", S_IRUGO, proc_tid_stat),
3405 ONE("statm", S_IRUGO, proc_pid_statm),
Vlastimil Babka871305b2018-08-21 21:52:48 -07003406 REG("maps", S_IRUGO, proc_pid_maps_operations),
Iago López Galeiras2e13ba52015-06-25 15:00:57 -07003407#ifdef CONFIG_PROC_CHILDREN
Cyrill Gorcunov818411612012-05-31 16:26:43 -07003408 REG("children", S_IRUGO, proc_tid_children_operations),
3409#endif
Eric W. Biederman28a6d672006-10-02 02:17:05 -07003410#ifdef CONFIG_NUMA
Vlastimil Babka871305b2018-08-21 21:52:48 -07003411 REG("numa_maps", S_IRUGO, proc_pid_numa_maps_operations),
Eric W. Biederman28a6d672006-10-02 02:17:05 -07003412#endif
Alexey Dobriyan631f9c12008-11-10 01:32:52 +03003413 REG("mem", S_IRUSR|S_IWUSR, proc_mem_operations),
3414 LNK("cwd", proc_cwd_link),
3415 LNK("root", proc_root_link),
3416 LNK("exe", proc_exe_link),
3417 REG("mounts", S_IRUGO, proc_mounts_operations),
3418 REG("mountinfo", S_IRUGO, proc_mountinfo_operations),
Matt Mackall1e883282008-02-04 22:29:07 -08003419#ifdef CONFIG_PROC_PAGE_MONITOR
Alexey Dobriyan631f9c12008-11-10 01:32:52 +03003420 REG("clear_refs", S_IWUSR, proc_clear_refs_operations),
Vlastimil Babka871305b2018-08-21 21:52:48 -07003421 REG("smaps", S_IRUGO, proc_pid_smaps_operations),
Daniel Colascione493b0e92017-09-06 16:25:08 -07003422 REG("smaps_rollup", S_IRUGO, proc_pid_smaps_rollup_operations),
Djalal Harouni32ed74a2014-04-07 15:38:38 -07003423 REG("pagemap", S_IRUSR, proc_pagemap_operations),
Eric W. Biederman28a6d672006-10-02 02:17:05 -07003424#endif
3425#ifdef CONFIG_SECURITY
Alexey Dobriyan631f9c12008-11-10 01:32:52 +03003426 DIR("attr", S_IRUGO|S_IXUGO, proc_attr_dir_inode_operations, proc_attr_dir_operations),
Eric W. Biederman28a6d672006-10-02 02:17:05 -07003427#endif
3428#ifdef CONFIG_KALLSYMS
Alexey Dobriyanedfcd602014-08-08 14:21:44 -07003429 ONE("wchan", S_IRUGO, proc_pid_wchan),
Eric W. Biederman28a6d672006-10-02 02:17:05 -07003430#endif
Ken Chen2ec220e2008-11-10 11:26:08 +03003431#ifdef CONFIG_STACKTRACE
Djalal Harouni35a35042014-04-07 15:38:36 -07003432 ONE("stack", S_IRUSR, proc_pid_stack),
Eric W. Biederman28a6d672006-10-02 02:17:05 -07003433#endif
Naveen N. Rao5968cec2015-06-30 14:36:03 +05303434#ifdef CONFIG_SCHED_INFO
Alexey Dobriyanf6e826c2014-08-08 14:21:46 -07003435 ONE("schedstat", S_IRUGO, proc_pid_schedstat),
Eric W. Biederman28a6d672006-10-02 02:17:05 -07003436#endif
Arjan van de Ven97455122008-01-25 21:08:34 +01003437#ifdef CONFIG_LATENCYTOP
Alexey Dobriyan631f9c12008-11-10 01:32:52 +03003438 REG("latency", S_IRUGO, proc_lstats_operations),
Arjan van de Ven97455122008-01-25 21:08:34 +01003439#endif
Paul Menage8793d852007-10-18 23:39:39 -07003440#ifdef CONFIG_PROC_PID_CPUSET
Zefan Li52de4772014-09-18 16:03:36 +08003441 ONE("cpuset", S_IRUGO, proc_cpuset_show),
Eric W. Biederman28a6d672006-10-02 02:17:05 -07003442#endif
Paul Menagea4243162007-10-18 23:39:35 -07003443#ifdef CONFIG_CGROUPS
Zefan Li006f4ac2014-09-18 16:03:15 +08003444 ONE("cgroup", S_IRUGO, proc_cgroup_show),
Paul Menagea4243162007-10-18 23:39:35 -07003445#endif
Alexey Dobriyan6ba51e32014-08-08 14:21:48 -07003446 ONE("oom_score", S_IRUGO, proc_oom_score),
David Rientjesfa0cbbf2012-11-12 17:53:04 -08003447 REG("oom_adj", S_IRUGO|S_IWUSR, proc_oom_adj_operations),
David Rientjesa63d83f2010-08-09 17:19:46 -07003448 REG("oom_score_adj", S_IRUGO|S_IWUSR, proc_oom_score_adj_operations),
Richard Guy Briggs4b7d2482019-01-22 17:06:39 -05003449#ifdef CONFIG_AUDIT
Alexey Dobriyan631f9c12008-11-10 01:32:52 +03003450 REG("loginuid", S_IWUSR|S_IRUGO, proc_loginuid_operations),
Al Viro26ec3c62011-02-15 21:24:05 -05003451 REG("sessionid", S_IRUGO, proc_sessionid_operations),
Eric W. Biederman28a6d672006-10-02 02:17:05 -07003452#endif
Akinobu Mitaf4f154f2006-12-08 02:39:47 -08003453#ifdef CONFIG_FAULT_INJECTION
Alexey Dobriyan631f9c12008-11-10 01:32:52 +03003454 REG("make-it-fail", S_IRUGO|S_IWUSR, proc_fault_inject_operations),
Akinobu Mita1203c8e2017-07-14 14:49:57 -07003455 REG("fail-nth", 0644, proc_fail_nth_operations),
Akinobu Mitaf4f154f2006-12-08 02:39:47 -08003456#endif
Andrea Righi297c5d92008-07-25 01:48:49 -07003457#ifdef CONFIG_TASK_IO_ACCOUNTING
Alexey Dobriyan19aadc92014-08-08 14:21:50 -07003458 ONE("io", S_IRUSR, proc_tid_io_accounting),
Andrea Righi297c5d92008-07-25 01:48:49 -07003459#endif
Eric W. Biederman22d917d2011-11-17 00:11:58 -08003460#ifdef CONFIG_USER_NS
3461 REG("uid_map", S_IRUGO|S_IWUSR, proc_uid_map_operations),
3462 REG("gid_map", S_IRUGO|S_IWUSR, proc_gid_map_operations),
Eric W. Biedermanf76d2072012-08-30 01:24:05 -07003463 REG("projid_map", S_IRUGO|S_IWUSR, proc_projid_map_operations),
Eric W. Biederman9cc46512014-12-02 12:27:26 -06003464 REG("setgroups", S_IRUGO|S_IWUSR, proc_setgroups_operations),
Eric W. Biederman22d917d2011-11-17 00:11:58 -08003465#endif
Josh Poimboeuf7c23b332017-02-13 19:42:41 -06003466#ifdef CONFIG_LIVEPATCH
3467 ONE("patch_state", S_IRUSR, proc_pid_patch_state),
3468#endif
Eric W. Biederman28a6d672006-10-02 02:17:05 -07003469};
3470
Al Virof0c3b502013-05-16 12:07:31 -04003471static int proc_tid_base_readdir(struct file *file, struct dir_context *ctx)
Eric W. Biederman28a6d672006-10-02 02:17:05 -07003472{
Al Virof0c3b502013-05-16 12:07:31 -04003473 return proc_pident_readdir(file, ctx,
3474 tid_base_stuff, ARRAY_SIZE(tid_base_stuff));
Eric W. Biederman28a6d672006-10-02 02:17:05 -07003475}
3476
Al Viro00cd8dd2012-06-10 17:13:09 -04003477static struct dentry *proc_tid_base_lookup(struct inode *dir, struct dentry *dentry, unsigned int flags)
3478{
Eric W. Biederman7bcd6b02006-10-02 02:18:56 -07003479 return proc_pident_lookup(dir, dentry,
Alexey Dobriyand5a572a2019-03-11 23:28:51 -07003480 tid_base_stuff,
3481 tid_base_stuff + ARRAY_SIZE(tid_base_stuff));
Eric W. Biederman28a6d672006-10-02 02:17:05 -07003482}
3483
Arjan van de Ven00977a52007-02-12 00:55:34 -08003484static const struct file_operations proc_tid_base_operations = {
Eric W. Biederman28a6d672006-10-02 02:17:05 -07003485 .read = generic_read_dir,
Al Virof50752e2016-04-20 17:13:54 -04003486 .iterate_shared = proc_tid_base_readdir,
3487 .llseek = generic_file_llseek,
Eric W. Biederman28a6d672006-10-02 02:17:05 -07003488};
3489
Arjan van de Venc5ef1c42007-02-12 00:55:40 -08003490static const struct inode_operations proc_tid_base_inode_operations = {
Eric W. Biederman28a6d672006-10-02 02:17:05 -07003491 .lookup = proc_tid_base_lookup,
3492 .getattr = pid_getattr,
3493 .setattr = proc_setattr,
3494};
3495
Al Viro0168b9e2018-05-03 09:21:05 -04003496static struct dentry *proc_task_instantiate(struct dentry *dentry,
3497 struct task_struct *task, const void *ptr)
Eric W. Biederman444ceed2006-10-02 02:18:49 -07003498{
Eric W. Biederman444ceed2006-10-02 02:18:49 -07003499 struct inode *inode;
Al Viro0168b9e2018-05-03 09:21:05 -04003500 inode = proc_pid_make_inode(dentry->d_sb, task, S_IFDIR | S_IRUGO | S_IXUGO);
Eric W. Biederman444ceed2006-10-02 02:18:49 -07003501 if (!inode)
Al Viro0168b9e2018-05-03 09:21:05 -04003502 return ERR_PTR(-ENOENT);
Al Viro1bbc5512018-05-02 21:26:16 -04003503
Eric W. Biederman444ceed2006-10-02 02:18:49 -07003504 inode->i_op = &proc_tid_base_inode_operations;
3505 inode->i_fop = &proc_tid_base_operations;
Al Viro1bbc5512018-05-02 21:26:16 -04003506 inode->i_flags |= S_IMMUTABLE;
Vegard Nossumaed54172008-06-05 22:46:53 -07003507
Alexey Dobriyan1270dd82016-12-12 16:45:32 -08003508 set_nlink(inode, nlink_tid);
Al Viro1bbc5512018-05-02 21:26:16 -04003509 pid_update_inode(task, inode);
Eric W. Biederman444ceed2006-10-02 02:18:49 -07003510
Nick Pigginfb045ad2011-01-07 17:49:55 +11003511 d_set_d_op(dentry, &pid_dentry_operations);
Al Viro0168b9e2018-05-03 09:21:05 -04003512 return d_splice_alias(inode, dentry);
Eric W. Biederman444ceed2006-10-02 02:18:49 -07003513}
3514
Al Viro00cd8dd2012-06-10 17:13:09 -04003515static struct dentry *proc_task_lookup(struct inode *dir, struct dentry * dentry, unsigned int flags)
Eric W. Biederman28a6d672006-10-02 02:17:05 -07003516{
Eric W. Biederman28a6d672006-10-02 02:17:05 -07003517 struct task_struct *task;
3518 struct task_struct *leader = get_proc_task(dir);
Eric W. Biederman28a6d672006-10-02 02:17:05 -07003519 unsigned tid;
Pavel Emelyanovb4888932007-10-18 23:40:14 -07003520 struct pid_namespace *ns;
Al Viro0168b9e2018-05-03 09:21:05 -04003521 struct dentry *result = ERR_PTR(-ENOENT);
Eric W. Biederman28a6d672006-10-02 02:17:05 -07003522
3523 if (!leader)
3524 goto out_no_task;
3525
Alexey Dobriyandbcdb502014-08-08 14:21:25 -07003526 tid = name_to_int(&dentry->d_name);
Eric W. Biederman28a6d672006-10-02 02:17:05 -07003527 if (tid == ~0U)
3528 goto out;
3529
Pavel Emelyanovb4888932007-10-18 23:40:14 -07003530 ns = dentry->d_sb->s_fs_info;
Eric W. Biederman28a6d672006-10-02 02:17:05 -07003531 rcu_read_lock();
Pavel Emelyanovb4888932007-10-18 23:40:14 -07003532 task = find_task_by_pid_ns(tid, ns);
Eric W. Biederman28a6d672006-10-02 02:17:05 -07003533 if (task)
3534 get_task_struct(task);
3535 rcu_read_unlock();
3536 if (!task)
3537 goto out;
Pavel Emelyanovbac0abd2007-10-18 23:40:18 -07003538 if (!same_thread_group(leader, task))
Eric W. Biederman28a6d672006-10-02 02:17:05 -07003539 goto out_drop_task;
3540
Al Viro0168b9e2018-05-03 09:21:05 -04003541 result = proc_task_instantiate(dentry, task, NULL);
Eric W. Biederman28a6d672006-10-02 02:17:05 -07003542out_drop_task:
3543 put_task_struct(task);
3544out:
3545 put_task_struct(leader);
3546out_no_task:
Al Viro0168b9e2018-05-03 09:21:05 -04003547 return result;
Eric W. Biederman28a6d672006-10-02 02:17:05 -07003548}
3549
3550/*
Eric W. Biederman0bc58a92006-06-26 00:25:50 -07003551 * Find the first tid of a thread group to return to user space.
3552 *
3553 * Usually this is just the thread group leader, but if the users
3554 * buffer was too small or there was a seek into the middle of the
3555 * directory we have more work todo.
3556 *
3557 * In the case of a short read we start with find_task_by_pid.
3558 *
3559 * In the case of a seek we start with the leader and walk nr
3560 * threads past it.
3561 */
Oleg Nesterov9f6e9632014-01-23 15:55:40 -08003562static struct task_struct *first_tid(struct pid *pid, int tid, loff_t f_pos,
3563 struct pid_namespace *ns)
Eric W. Biederman0bc58a92006-06-26 00:25:50 -07003564{
Oleg Nesterovd855a4b2014-01-23 15:55:39 -08003565 struct task_struct *pos, *task;
Oleg Nesterov9f6e9632014-01-23 15:55:40 -08003566 unsigned long nr = f_pos;
3567
3568 if (nr != f_pos) /* 32bit overflow? */
3569 return NULL;
Eric W. Biederman0bc58a92006-06-26 00:25:50 -07003570
Eric W. Biedermancc288732006-06-26 00:26:01 -07003571 rcu_read_lock();
Oleg Nesterovd855a4b2014-01-23 15:55:39 -08003572 task = pid_task(pid, PIDTYPE_PID);
3573 if (!task)
3574 goto fail;
3575
3576 /* Attempt to start with the tid of a thread */
Oleg Nesterov9f6e9632014-01-23 15:55:40 -08003577 if (tid && nr) {
Pavel Emelyanovb4888932007-10-18 23:40:14 -07003578 pos = find_task_by_pid_ns(tid, ns);
Oleg Nesterovd855a4b2014-01-23 15:55:39 -08003579 if (pos && same_thread_group(pos, task))
Oleg Nesterova872ff02006-06-26 00:26:01 -07003580 goto found;
Eric W. Biederman0bc58a92006-06-26 00:25:50 -07003581 }
3582
3583 /* If nr exceeds the number of threads there is nothing todo */
Oleg Nesterov9f6e9632014-01-23 15:55:40 -08003584 if (nr >= get_nr_threads(task))
Oleg Nesterovc986c142014-01-23 15:55:38 -08003585 goto fail;
Oleg Nesterova872ff02006-06-26 00:26:01 -07003586
3587 /* If we haven't found our starting place yet start
3588 * with the leader and walk nr threads forward.
3589 */
Oleg Nesterovd855a4b2014-01-23 15:55:39 -08003590 pos = task = task->group_leader;
Oleg Nesterovc986c142014-01-23 15:55:38 -08003591 do {
Oleg Nesterov9f6e9632014-01-23 15:55:40 -08003592 if (!nr--)
Oleg Nesterovc986c142014-01-23 15:55:38 -08003593 goto found;
Oleg Nesterovd855a4b2014-01-23 15:55:39 -08003594 } while_each_thread(task, pos);
Oleg Nesterovc986c142014-01-23 15:55:38 -08003595fail:
3596 pos = NULL;
3597 goto out;
Oleg Nesterova872ff02006-06-26 00:26:01 -07003598found:
3599 get_task_struct(pos);
3600out:
Eric W. Biedermancc288732006-06-26 00:26:01 -07003601 rcu_read_unlock();
Eric W. Biederman0bc58a92006-06-26 00:25:50 -07003602 return pos;
3603}
3604
3605/*
3606 * Find the next thread in the thread list.
3607 * Return NULL if there is an error or no next thread.
3608 *
3609 * The reference to the input task_struct is released.
3610 */
3611static struct task_struct *next_tid(struct task_struct *start)
3612{
Oleg Nesterovc1df7fb2006-06-26 00:26:02 -07003613 struct task_struct *pos = NULL;
Eric W. Biedermancc288732006-06-26 00:26:01 -07003614 rcu_read_lock();
Oleg Nesterovc1df7fb2006-06-26 00:26:02 -07003615 if (pid_alive(start)) {
Eric W. Biederman0bc58a92006-06-26 00:25:50 -07003616 pos = next_thread(start);
Oleg Nesterovc1df7fb2006-06-26 00:26:02 -07003617 if (thread_group_leader(pos))
3618 pos = NULL;
3619 else
3620 get_task_struct(pos);
3621 }
Eric W. Biedermancc288732006-06-26 00:26:01 -07003622 rcu_read_unlock();
Eric W. Biederman0bc58a92006-06-26 00:25:50 -07003623 put_task_struct(start);
3624 return pos;
3625}
3626
Linus Torvalds1da177e2005-04-16 15:20:36 -07003627/* for the /proc/TGID/task/ directories */
Al Virof0c3b502013-05-16 12:07:31 -04003628static int proc_task_readdir(struct file *file, struct dir_context *ctx)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003629{
Oleg Nesterovd855a4b2014-01-23 15:55:39 -08003630 struct inode *inode = file_inode(file);
3631 struct task_struct *task;
Pavel Emelyanovb4888932007-10-18 23:40:14 -07003632 struct pid_namespace *ns;
Al Virof0c3b502013-05-16 12:07:31 -04003633 int tid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003634
Oleg Nesterovd855a4b2014-01-23 15:55:39 -08003635 if (proc_inode_is_dead(inode))
Al Virof0c3b502013-05-16 12:07:31 -04003636 return -ENOENT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003637
Al Virof0c3b502013-05-16 12:07:31 -04003638 if (!dir_emit_dots(file, ctx))
Oleg Nesterovd855a4b2014-01-23 15:55:39 -08003639 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003640
Eric W. Biederman0bc58a92006-06-26 00:25:50 -07003641 /* f_version caches the tgid value that the last readdir call couldn't
3642 * return. lseek aka telldir automagically resets f_version to 0.
3643 */
Christoph Hellwig76f668b2018-05-16 07:19:01 +02003644 ns = proc_pid_ns(inode);
Al Virof0c3b502013-05-16 12:07:31 -04003645 tid = (int)file->f_version;
3646 file->f_version = 0;
Oleg Nesterovd855a4b2014-01-23 15:55:39 -08003647 for (task = first_tid(proc_pid(inode), tid, ctx->pos - 2, ns);
Eric W. Biederman0bc58a92006-06-26 00:25:50 -07003648 task;
Al Virof0c3b502013-05-16 12:07:31 -04003649 task = next_tid(task), ctx->pos++) {
Alexey Dobriyane3912ac2018-02-06 15:36:51 -08003650 char name[10 + 1];
Alexey Dobriyana4ef3892018-06-07 17:10:10 -07003651 unsigned int len;
Pavel Emelyanovb4888932007-10-18 23:40:14 -07003652 tid = task_pid_nr_ns(task, ns);
Alexey Dobriyane3912ac2018-02-06 15:36:51 -08003653 len = snprintf(name, sizeof(name), "%u", tid);
Al Virof0c3b502013-05-16 12:07:31 -04003654 if (!proc_fill_cache(file, ctx, name, len,
3655 proc_task_instantiate, task, NULL)) {
Eric W. Biederman0bc58a92006-06-26 00:25:50 -07003656 /* returning this tgid failed, save it as the first
3657 * pid for the next readir call */
Al Virof0c3b502013-05-16 12:07:31 -04003658 file->f_version = (u64)tid;
Eric W. Biederman0bc58a92006-06-26 00:25:50 -07003659 put_task_struct(task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003660 break;
Eric W. Biederman0bc58a92006-06-26 00:25:50 -07003661 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003662 }
Oleg Nesterovd855a4b2014-01-23 15:55:39 -08003663
Al Virof0c3b502013-05-16 12:07:31 -04003664 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003665}
Eric W. Biederman6e66b522006-06-26 00:25:47 -07003666
David Howellsa528d352017-01-31 16:46:22 +00003667static int proc_task_getattr(const struct path *path, struct kstat *stat,
3668 u32 request_mask, unsigned int query_flags)
Eric W. Biederman6e66b522006-06-26 00:25:47 -07003669{
David Howellsa528d352017-01-31 16:46:22 +00003670 struct inode *inode = d_inode(path->dentry);
Eric W. Biederman99f89552006-06-26 00:25:55 -07003671 struct task_struct *p = get_proc_task(inode);
Eric W. Biederman6e66b522006-06-26 00:25:47 -07003672 generic_fillattr(inode, stat);
3673
Eric W. Biederman99f89552006-06-26 00:25:55 -07003674 if (p) {
Eric W. Biederman99f89552006-06-26 00:25:55 -07003675 stat->nlink += get_nr_threads(p);
Eric W. Biederman99f89552006-06-26 00:25:55 -07003676 put_task_struct(p);
Eric W. Biederman6e66b522006-06-26 00:25:47 -07003677 }
3678
3679 return 0;
3680}
Eric W. Biederman28a6d672006-10-02 02:17:05 -07003681
Arjan van de Venc5ef1c42007-02-12 00:55:40 -08003682static const struct inode_operations proc_task_inode_operations = {
Eric W. Biederman28a6d672006-10-02 02:17:05 -07003683 .lookup = proc_task_lookup,
3684 .getattr = proc_task_getattr,
3685 .setattr = proc_setattr,
Vasiliy Kulikov04996802012-01-10 15:11:31 -08003686 .permission = proc_pid_permission,
Eric W. Biederman28a6d672006-10-02 02:17:05 -07003687};
3688
Arjan van de Ven00977a52007-02-12 00:55:34 -08003689static const struct file_operations proc_task_operations = {
Eric W. Biederman28a6d672006-10-02 02:17:05 -07003690 .read = generic_read_dir,
Al Virof50752e2016-04-20 17:13:54 -04003691 .iterate_shared = proc_task_readdir,
3692 .llseek = generic_file_llseek,
Eric W. Biederman28a6d672006-10-02 02:17:05 -07003693};
Alexey Dobriyan1270dd82016-12-12 16:45:32 -08003694
3695void __init set_proc_pid_nlink(void)
3696{
3697 nlink_tid = pid_entry_nlink(tid_base_stuff, ARRAY_SIZE(tid_base_stuff));
3698 nlink_tgid = pid_entry_nlink(tgid_base_stuff, ARRAY_SIZE(tgid_base_stuff));
3699}