blob: 6a803a0b75df45af049fd655a7e5a729f436708c [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 Torvaldse4b4e442018-05-17 13:04:17 -0700212static ssize_t get_mm_cmdline(struct mm_struct *mm, char __user *buf,
Linus Torvalds5ab82712018-05-17 15:17:33 -0700213 size_t count, loff_t *ppos)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700214{
Alexey Dobriyanc2c0bb42015-06-25 15:00:54 -0700215 unsigned long arg_start, arg_end, env_start, env_end;
Linus Torvalds5ab82712018-05-17 15:17:33 -0700216 unsigned long pos, len;
217 char *page;
Alexey Dobriyanc2c0bb42015-06-25 15:00:54 -0700218
Alexey Dobriyanc2c0bb42015-06-25 15:00:54 -0700219 /* Check if process spawned far enough to have cmdline. */
Linus Torvaldse4b4e442018-05-17 13:04:17 -0700220 if (!mm->env_end)
221 return 0;
Alexey Dobriyanc2c0bb42015-06-25 15:00:54 -0700222
Yang Shi88aa7cc2018-06-07 17:05:28 -0700223 spin_lock(&mm->arg_lock);
Alexey Dobriyanc2c0bb42015-06-25 15:00:54 -0700224 arg_start = mm->arg_start;
225 arg_end = mm->arg_end;
226 env_start = mm->env_start;
227 env_end = mm->env_end;
Yang Shi88aa7cc2018-06-07 17:05:28 -0700228 spin_unlock(&mm->arg_lock);
Alexey Dobriyanc2c0bb42015-06-25 15:00:54 -0700229
Linus Torvalds5ab82712018-05-17 15:17:33 -0700230 if (arg_start >= arg_end)
231 return 0;
Alexey Dobriyan6a6cbe72018-06-07 17:09:59 -0700232
Alexey Dobriyan2ca66ff2014-08-08 14:21:41 -0700233 /*
Linus Torvalds5ab82712018-05-17 15:17:33 -0700234 * We have traditionally allowed the user to re-write
235 * the argument strings and overflow the end result
236 * into the environment section. But only do that if
237 * the environment area is contiguous to the arguments.
Alexey Dobriyan2ca66ff2014-08-08 14:21:41 -0700238 */
Linus Torvalds5ab82712018-05-17 15:17:33 -0700239 if (env_start != arg_end || env_start >= env_end)
240 env_start = env_end = arg_end;
Alexey Dobriyanc2c0bb42015-06-25 15:00:54 -0700241
Linus Torvaldsf5b65342018-06-20 09:47:20 +0900242 /* .. and limit it to a maximum of one page of slop */
243 if (env_end >= arg_end + PAGE_SIZE)
244 env_end = arg_end + PAGE_SIZE - 1;
245
Linus Torvalds5ab82712018-05-17 15:17:33 -0700246 /* We're not going to care if "*ppos" has high bits set */
247 pos = arg_start + *ppos;
Alexey Dobriyanc2c0bb42015-06-25 15:00:54 -0700248
Linus Torvalds5ab82712018-05-17 15:17:33 -0700249 /* .. but we do check the result is in the proper range */
250 if (pos < arg_start || pos >= env_end)
251 return 0;
Alexey Dobriyanc2c0bb42015-06-25 15:00:54 -0700252
Linus Torvalds5ab82712018-05-17 15:17:33 -0700253 /* .. and we never go past env_end */
254 if (env_end - pos < count)
255 count = env_end - pos;
Alexey Dobriyanc2c0bb42015-06-25 15:00:54 -0700256
Linus Torvalds5ab82712018-05-17 15:17:33 -0700257 page = (char *)__get_free_page(GFP_KERNEL);
258 if (!page)
259 return -ENOMEM;
Alexey Dobriyanc2c0bb42015-06-25 15:00:54 -0700260
Linus Torvalds5ab82712018-05-17 15:17:33 -0700261 len = 0;
262 while (count) {
263 int got;
264 size_t size = min_t(size_t, PAGE_SIZE, count);
Linus Torvaldsf5b65342018-06-20 09:47:20 +0900265 long offset;
Alexey Dobriyanc2c0bb42015-06-25 15:00:54 -0700266
Linus Torvaldsf5b65342018-06-20 09:47:20 +0900267 /*
268 * Are we already starting past the official end?
269 * We always include the last byte that is *supposed*
270 * to be NUL
271 */
272 offset = (pos >= arg_end) ? pos - arg_end + 1 : 0;
273
274 got = access_remote_vm(mm, pos - offset, page, size + offset, FOLL_ANON);
275 if (got <= offset)
Linus Torvalds5ab82712018-05-17 15:17:33 -0700276 break;
Linus Torvaldsf5b65342018-06-20 09:47:20 +0900277 got -= offset;
Linus Torvalds5ab82712018-05-17 15:17:33 -0700278
279 /* Don't walk past a NUL character once you hit arg_end */
280 if (pos + got >= arg_end) {
281 int n = 0;
282
283 /*
284 * If we started before 'arg_end' but ended up
285 * at or after it, we start the NUL character
286 * check at arg_end-1 (where we expect the normal
287 * EOF to be).
288 *
289 * NOTE! This is smaller than 'got', because
290 * pos + got >= arg_end
291 */
292 if (pos < arg_end)
293 n = arg_end - pos - 1;
294
295 /* Cut off at first NUL after 'n' */
Linus Torvaldsf5b65342018-06-20 09:47:20 +0900296 got = n + strnlen(page+n, offset+got-n);
297 if (got < offset)
Linus Torvalds5ab82712018-05-17 15:17:33 -0700298 break;
Linus Torvaldsf5b65342018-06-20 09:47:20 +0900299 got -= offset;
300
301 /* Include the NUL if it existed */
302 if (got < size)
303 got++;
Alexey Dobriyanc2c0bb42015-06-25 15:00:54 -0700304 }
Alexey Dobriyana0a07b82017-02-24 15:00:20 -0800305
Linus Torvaldsf5b65342018-06-20 09:47:20 +0900306 got -= copy_to_user(buf, page+offset, got);
Linus Torvalds5ab82712018-05-17 15:17:33 -0700307 if (unlikely(!got)) {
308 if (!len)
309 len = -EFAULT;
310 break;
Alexey Dobriyanc2c0bb42015-06-25 15:00:54 -0700311 }
Linus Torvalds5ab82712018-05-17 15:17:33 -0700312 pos += got;
313 buf += got;
314 len += got;
315 count -= got;
Alexey Dobriyan3cb4e162018-06-07 17:10:02 -0700316 }
317
Alexey Dobriyanc2c0bb42015-06-25 15:00:54 -0700318 free_page((unsigned long)page);
Linus Torvalds5ab82712018-05-17 15:17:33 -0700319 return len;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700320}
321
Linus Torvaldse4b4e442018-05-17 13:04:17 -0700322static ssize_t get_task_cmdline(struct task_struct *tsk, char __user *buf,
323 size_t count, loff_t *pos)
324{
325 struct mm_struct *mm;
326 ssize_t ret;
327
328 mm = get_task_mm(tsk);
329 if (!mm)
330 return 0;
331
332 ret = get_mm_cmdline(mm, buf, count, pos);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700333 mmput(mm);
Linus Torvaldse4b4e442018-05-17 13:04:17 -0700334 return ret;
335}
336
337static ssize_t proc_pid_cmdline_read(struct file *file, char __user *buf,
338 size_t count, loff_t *pos)
339{
340 struct task_struct *tsk;
341 ssize_t ret;
342
343 BUG_ON(*pos < 0);
344
345 tsk = get_proc_task(file_inode(file));
346 if (!tsk)
347 return -ESRCH;
348 ret = get_task_cmdline(tsk, buf, count, pos);
349 put_task_struct(tsk);
350 if (ret > 0)
351 *pos += ret;
352 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700353}
354
Alexey Dobriyanc2c0bb42015-06-25 15:00:54 -0700355static const struct file_operations proc_pid_cmdline_ops = {
356 .read = proc_pid_cmdline_read,
357 .llseek = generic_file_llseek,
358};
359
Linus Torvalds1da177e2005-04-16 15:20:36 -0700360#ifdef CONFIG_KALLSYMS
361/*
362 * Provides a wchan file via kallsyms in a proper one-value-per-file format.
363 * Returns the resolved symbol. If that fails, simply return the address.
364 */
Alexey Dobriyanedfcd602014-08-08 14:21:44 -0700365static int proc_pid_wchan(struct seq_file *m, struct pid_namespace *ns,
366 struct pid *pid, struct task_struct *task)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700367{
Alexey Dobriyanffb45122007-05-08 00:28:41 -0700368 unsigned long wchan;
Tejun Heo9281ace2007-07-17 04:03:51 -0700369 char symname[KSYM_NAME_LEN];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700370
Alexey Dobriyan24b2ec22018-04-10 16:31:30 -0700371 if (!ptrace_may_access(task, PTRACE_MODE_READ_FSCREDS))
372 goto print0;
373
Linus Torvalds1da177e2005-04-16 15:20:36 -0700374 wchan = get_wchan(task);
Alexey Dobriyan24b2ec22018-04-10 16:31:30 -0700375 if (wchan && !lookup_symbol_name(wchan, symname)) {
Alexey Dobriyan21dae0a2018-04-10 16:31:34 -0700376 seq_puts(m, symname);
Alexey Dobriyan24b2ec22018-04-10 16:31:30 -0700377 return 0;
378 }
Joe Perches25ce3192015-04-15 16:18:17 -0700379
Alexey Dobriyan24b2ec22018-04-10 16:31:30 -0700380print0:
381 seq_putc(m, '0');
Joe Perches25ce3192015-04-15 16:18:17 -0700382 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700383}
384#endif /* CONFIG_KALLSYMS */
385
Al Viroa9712bc2011-03-23 15:52:50 -0400386static int lock_trace(struct task_struct *task)
387{
388 int err = mutex_lock_killable(&task->signal->cred_guard_mutex);
389 if (err)
390 return err;
Jann Horncaaee622016-01-20 15:00:04 -0800391 if (!ptrace_may_access(task, PTRACE_MODE_ATTACH_FSCREDS)) {
Al Viroa9712bc2011-03-23 15:52:50 -0400392 mutex_unlock(&task->signal->cred_guard_mutex);
393 return -EPERM;
394 }
395 return 0;
396}
397
398static void unlock_trace(struct task_struct *task)
399{
400 mutex_unlock(&task->signal->cred_guard_mutex);
401}
402
Ken Chen2ec220e2008-11-10 11:26:08 +0300403#ifdef CONFIG_STACKTRACE
404
405#define MAX_STACK_TRACE_DEPTH 64
406
407static int proc_pid_stack(struct seq_file *m, struct pid_namespace *ns,
408 struct pid *pid, struct task_struct *task)
409{
410 struct stack_trace trace;
411 unsigned long *entries;
Al Viroa9712bc2011-03-23 15:52:50 -0400412 int err;
Ken Chen2ec220e2008-11-10 11:26:08 +0300413
Jann Hornf8a00ce2018-10-05 15:51:58 -0700414 /*
415 * The ability to racily run the kernel stack unwinder on a running task
416 * and then observe the unwinder output is scary; while it is useful for
417 * debugging kernel issues, it can also allow an attacker to leak kernel
418 * stack contents.
419 * Doing this in a manner that is at least safe from races would require
420 * some work to ensure that the remote task can not be scheduled; and
421 * even then, this would still expose the unwinder as local attack
422 * surface.
423 * Therefore, this interface is restricted to root.
424 */
425 if (!file_ns_capable(m->file, &init_user_ns, CAP_SYS_ADMIN))
426 return -EACCES;
427
Kees Cook6da2ec52018-06-12 13:55:00 -0700428 entries = kmalloc_array(MAX_STACK_TRACE_DEPTH, sizeof(*entries),
429 GFP_KERNEL);
Ken Chen2ec220e2008-11-10 11:26:08 +0300430 if (!entries)
431 return -ENOMEM;
432
433 trace.nr_entries = 0;
434 trace.max_entries = MAX_STACK_TRACE_DEPTH;
435 trace.entries = entries;
436 trace.skip = 0;
Ken Chen2ec220e2008-11-10 11:26:08 +0300437
Al Viroa9712bc2011-03-23 15:52:50 -0400438 err = lock_trace(task);
439 if (!err) {
Alexey Dobriyan5d008fb2018-06-07 17:10:17 -0700440 unsigned int i;
441
Al Viroa9712bc2011-03-23 15:52:50 -0400442 save_stack_trace_tsk(task, &trace);
443
444 for (i = 0; i < trace.nr_entries; i++) {
Linus Torvalds8f5abe82017-11-27 16:45:56 -0800445 seq_printf(m, "[<0>] %pB\n", (void *)entries[i]);
Al Viroa9712bc2011-03-23 15:52:50 -0400446 }
447 unlock_trace(task);
Ken Chen2ec220e2008-11-10 11:26:08 +0300448 }
449 kfree(entries);
450
Al Viroa9712bc2011-03-23 15:52:50 -0400451 return err;
Ken Chen2ec220e2008-11-10 11:26:08 +0300452}
453#endif
454
Naveen N. Rao5968cec2015-06-30 14:36:03 +0530455#ifdef CONFIG_SCHED_INFO
Linus Torvalds1da177e2005-04-16 15:20:36 -0700456/*
457 * Provides /proc/PID/schedstat
458 */
Alexey Dobriyanf6e826c2014-08-08 14:21:46 -0700459static int proc_pid_schedstat(struct seq_file *m, struct pid_namespace *ns,
460 struct pid *pid, struct task_struct *task)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700461{
Naveen N. Rao5968cec2015-06-30 14:36:03 +0530462 if (unlikely(!sched_info_on()))
Alexey Dobriyan08b55772019-03-05 15:50:35 -0800463 seq_puts(m, "0 0 0\n");
Naveen N. Rao5968cec2015-06-30 14:36:03 +0530464 else
465 seq_printf(m, "%llu %llu %lu\n",
Joe Perches25ce3192015-04-15 16:18:17 -0700466 (unsigned long long)task->se.sum_exec_runtime,
467 (unsigned long long)task->sched_info.run_delay,
468 task->sched_info.pcount);
469
470 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700471}
472#endif
473
Arjan van de Ven97455122008-01-25 21:08:34 +0100474#ifdef CONFIG_LATENCYTOP
475static int lstats_show_proc(struct seq_file *m, void *v)
476{
477 int i;
Hiroshi Shimamoto13d77c32008-02-20 16:53:29 -0800478 struct inode *inode = m->private;
479 struct task_struct *task = get_proc_task(inode);
Arjan van de Ven97455122008-01-25 21:08:34 +0100480
Hiroshi Shimamoto13d77c32008-02-20 16:53:29 -0800481 if (!task)
482 return -ESRCH;
483 seq_puts(m, "Latency Top version : v0.1\n");
Alexey Dobriyanf6d2f582018-08-21 21:54:34 -0700484 for (i = 0; i < LT_SAVECOUNT; i++) {
Joe Perches34e49d42011-01-12 17:00:30 -0800485 struct latency_record *lr = &task->latency_record[i];
486 if (lr->backtrace[0]) {
Arjan van de Ven97455122008-01-25 21:08:34 +0100487 int q;
Joe Perches34e49d42011-01-12 17:00:30 -0800488 seq_printf(m, "%i %li %li",
489 lr->count, lr->time, lr->max);
Arjan van de Ven97455122008-01-25 21:08:34 +0100490 for (q = 0; q < LT_BACKTRACEDEPTH; q++) {
Joe Perches34e49d42011-01-12 17:00:30 -0800491 unsigned long bt = lr->backtrace[q];
492 if (!bt)
Arjan van de Ven97455122008-01-25 21:08:34 +0100493 break;
Joe Perches34e49d42011-01-12 17:00:30 -0800494 if (bt == ULONG_MAX)
Arjan van de Ven97455122008-01-25 21:08:34 +0100495 break;
Joe Perches34e49d42011-01-12 17:00:30 -0800496 seq_printf(m, " %ps", (void *)bt);
Arjan van de Ven97455122008-01-25 21:08:34 +0100497 }
Alexey Dobriyan9d6de122011-01-12 17:00:32 -0800498 seq_putc(m, '\n');
Arjan van de Ven97455122008-01-25 21:08:34 +0100499 }
500
501 }
Hiroshi Shimamoto13d77c32008-02-20 16:53:29 -0800502 put_task_struct(task);
Arjan van de Ven97455122008-01-25 21:08:34 +0100503 return 0;
504}
505
506static int lstats_open(struct inode *inode, struct file *file)
507{
Hiroshi Shimamoto13d77c32008-02-20 16:53:29 -0800508 return single_open(file, lstats_show_proc, inode);
Hiroshi Shimamotod6643d12008-02-14 10:27:00 -0800509}
510
Arjan van de Ven97455122008-01-25 21:08:34 +0100511static ssize_t lstats_write(struct file *file, const char __user *buf,
512 size_t count, loff_t *offs)
513{
Al Viro496ad9a2013-01-23 17:07:38 -0500514 struct task_struct *task = get_proc_task(file_inode(file));
Arjan van de Ven97455122008-01-25 21:08:34 +0100515
Hiroshi Shimamoto13d77c32008-02-20 16:53:29 -0800516 if (!task)
517 return -ESRCH;
Arjan van de Ven97455122008-01-25 21:08:34 +0100518 clear_all_latency_tracing(task);
Hiroshi Shimamoto13d77c32008-02-20 16:53:29 -0800519 put_task_struct(task);
Arjan van de Ven97455122008-01-25 21:08:34 +0100520
521 return count;
522}
523
524static const struct file_operations proc_lstats_operations = {
525 .open = lstats_open,
526 .read = seq_read,
527 .write = lstats_write,
528 .llseek = seq_lseek,
Hiroshi Shimamoto13d77c32008-02-20 16:53:29 -0800529 .release = single_release,
Arjan van de Ven97455122008-01-25 21:08:34 +0100530};
531
532#endif
533
Alexey Dobriyan6ba51e32014-08-08 14:21:48 -0700534static int proc_oom_score(struct seq_file *m, struct pid_namespace *ns,
535 struct pid *pid, struct task_struct *task)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700536{
Arun KSca79b0c2018-12-28 00:34:29 -0800537 unsigned long totalpages = totalram_pages() + total_swap_pages;
Oleg Nesterovb95c35e2010-04-01 15:13:57 +0200538 unsigned long points = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700539
Oleg Nesterovef419392016-08-02 14:03:19 -0700540 points = oom_badness(task, NULL, NULL, totalpages) *
541 1000 / totalpages;
Joe Perches25ce3192015-04-15 16:18:17 -0700542 seq_printf(m, "%lu\n", points);
543
544 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700545}
546
Neil Hormand85f50d2007-10-18 23:40:37 -0700547struct limit_names {
Alexey Dobriyancedbcca2014-08-08 14:21:33 -0700548 const char *name;
549 const char *unit;
Neil Hormand85f50d2007-10-18 23:40:37 -0700550};
551
552static const struct limit_names lnames[RLIM_NLIMITS] = {
Kees Cookcff4edb2009-09-22 16:45:32 -0700553 [RLIMIT_CPU] = {"Max cpu time", "seconds"},
Neil Hormand85f50d2007-10-18 23:40:37 -0700554 [RLIMIT_FSIZE] = {"Max file size", "bytes"},
555 [RLIMIT_DATA] = {"Max data size", "bytes"},
556 [RLIMIT_STACK] = {"Max stack size", "bytes"},
557 [RLIMIT_CORE] = {"Max core file size", "bytes"},
558 [RLIMIT_RSS] = {"Max resident set", "bytes"},
559 [RLIMIT_NPROC] = {"Max processes", "processes"},
560 [RLIMIT_NOFILE] = {"Max open files", "files"},
561 [RLIMIT_MEMLOCK] = {"Max locked memory", "bytes"},
562 [RLIMIT_AS] = {"Max address space", "bytes"},
563 [RLIMIT_LOCKS] = {"Max file locks", "locks"},
564 [RLIMIT_SIGPENDING] = {"Max pending signals", "signals"},
565 [RLIMIT_MSGQUEUE] = {"Max msgqueue size", "bytes"},
566 [RLIMIT_NICE] = {"Max nice priority", NULL},
567 [RLIMIT_RTPRIO] = {"Max realtime priority", NULL},
Eugene Teo88081172008-02-23 15:23:52 -0800568 [RLIMIT_RTTIME] = {"Max realtime timeout", "us"},
Neil Hormand85f50d2007-10-18 23:40:37 -0700569};
570
571/* Display limits for a process */
Alexey Dobriyan1c963eb2014-08-08 14:21:37 -0700572static int proc_pid_limits(struct seq_file *m, struct pid_namespace *ns,
573 struct pid *pid, struct task_struct *task)
Neil Hormand85f50d2007-10-18 23:40:37 -0700574{
575 unsigned int i;
Neil Hormand85f50d2007-10-18 23:40:37 -0700576 unsigned long flags;
Neil Hormand85f50d2007-10-18 23:40:37 -0700577
578 struct rlimit rlim[RLIM_NLIMITS];
579
Lai Jiangshana6bebbc2008-10-05 00:51:15 +0400580 if (!lock_task_sighand(task, &flags))
Neil Hormand85f50d2007-10-18 23:40:37 -0700581 return 0;
Neil Hormand85f50d2007-10-18 23:40:37 -0700582 memcpy(rlim, task->signal->rlim, sizeof(struct rlimit) * RLIM_NLIMITS);
583 unlock_task_sighand(task, &flags);
Neil Hormand85f50d2007-10-18 23:40:37 -0700584
585 /*
586 * print the file header
587 */
Alexey Dobriyanafe922c2019-01-03 15:26:09 -0800588 seq_puts(m, "Limit "
589 "Soft Limit "
590 "Hard Limit "
591 "Units \n");
Neil Hormand85f50d2007-10-18 23:40:37 -0700592
593 for (i = 0; i < RLIM_NLIMITS; i++) {
594 if (rlim[i].rlim_cur == RLIM_INFINITY)
Alexey Dobriyan1c963eb2014-08-08 14:21:37 -0700595 seq_printf(m, "%-25s %-20s ",
Joe Perches25ce3192015-04-15 16:18:17 -0700596 lnames[i].name, "unlimited");
Neil Hormand85f50d2007-10-18 23:40:37 -0700597 else
Alexey Dobriyan1c963eb2014-08-08 14:21:37 -0700598 seq_printf(m, "%-25s %-20lu ",
Joe Perches25ce3192015-04-15 16:18:17 -0700599 lnames[i].name, rlim[i].rlim_cur);
Neil Hormand85f50d2007-10-18 23:40:37 -0700600
601 if (rlim[i].rlim_max == RLIM_INFINITY)
Alexey Dobriyan1c963eb2014-08-08 14:21:37 -0700602 seq_printf(m, "%-20s ", "unlimited");
Neil Hormand85f50d2007-10-18 23:40:37 -0700603 else
Alexey Dobriyan1c963eb2014-08-08 14:21:37 -0700604 seq_printf(m, "%-20lu ", rlim[i].rlim_max);
Neil Hormand85f50d2007-10-18 23:40:37 -0700605
606 if (lnames[i].unit)
Alexey Dobriyan1c963eb2014-08-08 14:21:37 -0700607 seq_printf(m, "%-10s\n", lnames[i].unit);
Neil Hormand85f50d2007-10-18 23:40:37 -0700608 else
Alexey Dobriyan1c963eb2014-08-08 14:21:37 -0700609 seq_putc(m, '\n');
Neil Hormand85f50d2007-10-18 23:40:37 -0700610 }
611
Alexey Dobriyan1c963eb2014-08-08 14:21:37 -0700612 return 0;
Neil Hormand85f50d2007-10-18 23:40:37 -0700613}
614
Roland McGrathebcb6732008-07-25 19:46:00 -0700615#ifdef CONFIG_HAVE_ARCH_TRACEHOOK
Alexey Dobriyan09d93bd2014-08-08 14:21:39 -0700616static int proc_pid_syscall(struct seq_file *m, struct pid_namespace *ns,
617 struct pid *pid, struct task_struct *task)
Roland McGrathebcb6732008-07-25 19:46:00 -0700618{
Steven Rostedt (Red Hat)631b7ab2016-11-07 16:26:35 -0500619 struct syscall_info info;
620 u64 *args = &info.data.args[0];
Joe Perches25ce3192015-04-15 16:18:17 -0700621 int res;
622
623 res = lock_trace(task);
Al Viroa9712bc2011-03-23 15:52:50 -0400624 if (res)
625 return res;
Roland McGrathebcb6732008-07-25 19:46:00 -0700626
Steven Rostedt (Red Hat)631b7ab2016-11-07 16:26:35 -0500627 if (task_current_syscall(task, &info))
Alexey Dobriyan09d93bd2014-08-08 14:21:39 -0700628 seq_puts(m, "running\n");
Steven Rostedt (Red Hat)631b7ab2016-11-07 16:26:35 -0500629 else if (info.data.nr < 0)
630 seq_printf(m, "%d 0x%llx 0x%llx\n",
631 info.data.nr, info.sp, info.data.instruction_pointer);
Al Viroa9712bc2011-03-23 15:52:50 -0400632 else
Alexey Dobriyan09d93bd2014-08-08 14:21:39 -0700633 seq_printf(m,
Steven Rostedt (Red Hat)631b7ab2016-11-07 16:26:35 -0500634 "%d 0x%llx 0x%llx 0x%llx 0x%llx 0x%llx 0x%llx 0x%llx 0x%llx\n",
635 info.data.nr,
Roland McGrathebcb6732008-07-25 19:46:00 -0700636 args[0], args[1], args[2], args[3], args[4], args[5],
Steven Rostedt (Red Hat)631b7ab2016-11-07 16:26:35 -0500637 info.sp, info.data.instruction_pointer);
Al Viroa9712bc2011-03-23 15:52:50 -0400638 unlock_trace(task);
Joe Perches25ce3192015-04-15 16:18:17 -0700639
640 return 0;
Roland McGrathebcb6732008-07-25 19:46:00 -0700641}
642#endif /* CONFIG_HAVE_ARCH_TRACEHOOK */
643
Linus Torvalds1da177e2005-04-16 15:20:36 -0700644/************************************************************************/
645/* Here the fs part begins */
646/************************************************************************/
647
648/* permission checks */
Eric W. Biederman778c1142006-06-26 00:25:58 -0700649static int proc_fd_access_allowed(struct inode *inode)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700650{
Eric W. Biederman778c1142006-06-26 00:25:58 -0700651 struct task_struct *task;
652 int allowed = 0;
Eric W. Biedermandf26c402006-06-26 00:25:59 -0700653 /* Allow access to a task's file descriptors if it is us or we
654 * may use ptrace attach to the process and find out that
655 * information.
Eric W. Biederman778c1142006-06-26 00:25:58 -0700656 */
657 task = get_proc_task(inode);
Eric W. Biedermandf26c402006-06-26 00:25:59 -0700658 if (task) {
Jann Horncaaee622016-01-20 15:00:04 -0800659 allowed = ptrace_may_access(task, PTRACE_MODE_READ_FSCREDS);
Eric W. Biederman778c1142006-06-26 00:25:58 -0700660 put_task_struct(task);
Eric W. Biedermandf26c402006-06-26 00:25:59 -0700661 }
Eric W. Biederman778c1142006-06-26 00:25:58 -0700662 return allowed;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700663}
664
Eric W. Biederman6b4e3062010-03-07 16:41:34 -0800665int proc_setattr(struct dentry *dentry, struct iattr *attr)
Linus Torvalds6d76fa52006-07-15 12:26:45 -0700666{
667 int error;
David Howells2b0143b2015-03-17 22:25:59 +0000668 struct inode *inode = d_inode(dentry);
Linus Torvalds6d76fa52006-07-15 12:26:45 -0700669
670 if (attr->ia_valid & ATTR_MODE)
671 return -EPERM;
672
Jan Kara31051c82016-05-26 16:55:18 +0200673 error = setattr_prepare(dentry, attr);
Christoph Hellwig10257742010-06-04 11:30:02 +0200674 if (error)
675 return error;
676
Christoph Hellwig10257742010-06-04 11:30:02 +0200677 setattr_copy(inode, attr);
678 mark_inode_dirty(inode);
679 return 0;
Linus Torvalds6d76fa52006-07-15 12:26:45 -0700680}
681
Vasiliy Kulikov04996802012-01-10 15:11:31 -0800682/*
683 * May current process learn task's sched/cmdline info (for hide_pid_min=1)
684 * or euid/egid (for hide_pid_min=2)?
685 */
686static bool has_pid_permissions(struct pid_namespace *pid,
687 struct task_struct *task,
688 int hide_pid_min)
689{
690 if (pid->hide_pid < hide_pid_min)
691 return true;
692 if (in_group_p(pid->pid_gid))
693 return true;
Jann Horncaaee622016-01-20 15:00:04 -0800694 return ptrace_may_access(task, PTRACE_MODE_READ_FSCREDS);
Vasiliy Kulikov04996802012-01-10 15:11:31 -0800695}
696
697
698static int proc_pid_permission(struct inode *inode, int mask)
699{
Christoph Hellwig76f668b2018-05-16 07:19:01 +0200700 struct pid_namespace *pid = proc_pid_ns(inode);
Vasiliy Kulikov04996802012-01-10 15:11:31 -0800701 struct task_struct *task;
702 bool has_perms;
703
704 task = get_proc_task(inode);
Xiaotian Fenga2ef9902012-01-12 17:17:08 -0800705 if (!task)
706 return -ESRCH;
Lafcadio Wluiki796f5712017-02-24 15:00:23 -0800707 has_perms = has_pid_permissions(pid, task, HIDEPID_NO_ACCESS);
Vasiliy Kulikov04996802012-01-10 15:11:31 -0800708 put_task_struct(task);
709
710 if (!has_perms) {
Lafcadio Wluiki796f5712017-02-24 15:00:23 -0800711 if (pid->hide_pid == HIDEPID_INVISIBLE) {
Vasiliy Kulikov04996802012-01-10 15:11:31 -0800712 /*
713 * Let's make getdents(), stat(), and open()
714 * consistent with each other. If a process
715 * may not stat() a file, it shouldn't be seen
716 * in procfs at all.
717 */
718 return -ENOENT;
719 }
720
721 return -EPERM;
722 }
723 return generic_permission(inode, mask);
724}
725
726
727
Arjan van de Venc5ef1c42007-02-12 00:55:40 -0800728static const struct inode_operations proc_def_inode_operations = {
Linus Torvalds6d76fa52006-07-15 12:26:45 -0700729 .setattr = proc_setattr,
730};
731
Eric W. Biedermanbe614082008-02-08 04:18:30 -0800732static int proc_single_show(struct seq_file *m, void *v)
733{
734 struct inode *inode = m->private;
Christoph Hellwig76f668b2018-05-16 07:19:01 +0200735 struct pid_namespace *ns = proc_pid_ns(inode);
736 struct pid *pid = proc_pid(inode);
Eric W. Biedermanbe614082008-02-08 04:18:30 -0800737 struct task_struct *task;
738 int ret;
739
Eric W. Biedermanbe614082008-02-08 04:18:30 -0800740 task = get_pid_task(pid, PIDTYPE_PID);
741 if (!task)
742 return -ESRCH;
743
744 ret = PROC_I(inode)->op.proc_show(m, ns, pid, task);
745
746 put_task_struct(task);
747 return ret;
748}
749
750static int proc_single_open(struct inode *inode, struct file *filp)
751{
Jovi Zhangc6a34052011-01-12 17:00:34 -0800752 return single_open(filp, proc_single_show, inode);
Eric W. Biedermanbe614082008-02-08 04:18:30 -0800753}
754
755static const struct file_operations proc_single_file_operations = {
756 .open = proc_single_open,
757 .read = seq_read,
758 .llseek = seq_lseek,
759 .release = single_release,
760};
761
Oleg Nesterov5381e162014-10-09 15:25:24 -0700762
763struct mm_struct *proc_mem_open(struct inode *inode, unsigned int mode)
764{
765 struct task_struct *task = get_proc_task(inode);
766 struct mm_struct *mm = ERR_PTR(-ESRCH);
767
768 if (task) {
Jann Horncaaee622016-01-20 15:00:04 -0800769 mm = mm_access(task, mode | PTRACE_MODE_FSCREDS);
Oleg Nesterov5381e162014-10-09 15:25:24 -0700770 put_task_struct(task);
771
772 if (!IS_ERR_OR_NULL(mm)) {
773 /* ensure this mm_struct can't be freed */
Vegard Nossumf1f10072017-02-27 14:30:07 -0800774 mmgrab(mm);
Oleg Nesterov5381e162014-10-09 15:25:24 -0700775 /* but do not pin its memory */
776 mmput(mm);
777 }
778 }
779
780 return mm;
781}
782
Cong Wangb409e572012-05-31 16:26:17 -0700783static int __mem_open(struct inode *inode, struct file *file, unsigned int mode)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700784{
Oleg Nesterov5381e162014-10-09 15:25:24 -0700785 struct mm_struct *mm = proc_mem_open(inode, mode);
Linus Torvaldse2683372012-01-17 15:21:19 -0800786
787 if (IS_ERR(mm))
788 return PTR_ERR(mm);
789
Linus Torvaldse2683372012-01-17 15:21:19 -0800790 file->private_data = mm;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700791 return 0;
792}
793
Cong Wangb409e572012-05-31 16:26:17 -0700794static int mem_open(struct inode *inode, struct file *file)
795{
Djalal Harounibc452b42012-07-30 14:42:28 -0700796 int ret = __mem_open(inode, file, PTRACE_MODE_ATTACH);
797
798 /* OK to pass negative loff_t, we can catch out-of-range */
799 file->f_mode |= FMODE_UNSIGNED_OFFSET;
800
801 return ret;
Cong Wangb409e572012-05-31 16:26:17 -0700802}
803
Oleg Nesterov572d34b2012-01-31 17:14:54 +0100804static ssize_t mem_rw(struct file *file, char __user *buf,
805 size_t count, loff_t *ppos, int write)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700806{
Linus Torvaldse2683372012-01-17 15:21:19 -0800807 struct mm_struct *mm = file->private_data;
Oleg Nesterov572d34b2012-01-31 17:14:54 +0100808 unsigned long addr = *ppos;
809 ssize_t copied;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700810 char *page;
Linus Torvalds272ddc82016-10-24 19:00:44 -0700811 unsigned int flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700812
Linus Torvaldse2683372012-01-17 15:21:19 -0800813 if (!mm)
814 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700815
Michal Hocko0ee931c2017-09-13 16:28:29 -0700816 page = (char *)__get_free_page(GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700817 if (!page)
Linus Torvaldse2683372012-01-17 15:21:19 -0800818 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700819
Frederik Deweerdtf7ca54f2006-09-29 02:01:02 -0700820 copied = 0;
Vegard Nossum388f7932017-02-27 14:30:13 -0800821 if (!mmget_not_zero(mm))
Oleg Nesterov6d08f2c2012-01-31 17:15:11 +0100822 goto free;
823
Linus Torvaldsf511c0b2017-05-30 12:38:59 -0700824 flags = FOLL_FORCE | (write ? FOLL_WRITE : 0);
Lorenzo Stoakes6347e8d2016-10-13 01:20:19 +0100825
Linus Torvalds1da177e2005-04-16 15:20:36 -0700826 while (count > 0) {
Oleg Nesterov572d34b2012-01-31 17:14:54 +0100827 int this_len = min_t(int, count, PAGE_SIZE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700828
Oleg Nesterov572d34b2012-01-31 17:14:54 +0100829 if (write && copy_from_user(page, buf, this_len)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700830 copied = -EFAULT;
831 break;
832 }
Oleg Nesterov572d34b2012-01-31 17:14:54 +0100833
Lorenzo Stoakes6347e8d2016-10-13 01:20:19 +0100834 this_len = access_remote_vm(mm, addr, page, this_len, flags);
Oleg Nesterov572d34b2012-01-31 17:14:54 +0100835 if (!this_len) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700836 if (!copied)
837 copied = -EIO;
838 break;
839 }
Oleg Nesterov572d34b2012-01-31 17:14:54 +0100840
841 if (!write && copy_to_user(buf, page, this_len)) {
842 copied = -EFAULT;
843 break;
844 }
845
846 buf += this_len;
847 addr += this_len;
848 copied += this_len;
849 count -= this_len;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700850 }
Oleg Nesterov572d34b2012-01-31 17:14:54 +0100851 *ppos = addr;
KOSAKI Motohiro30cd8902011-05-26 16:25:52 -0700852
Oleg Nesterov6d08f2c2012-01-31 17:15:11 +0100853 mmput(mm);
854free:
KOSAKI Motohiro30cd8902011-05-26 16:25:52 -0700855 free_page((unsigned long) page);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700856 return copied;
857}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700858
Oleg Nesterov572d34b2012-01-31 17:14:54 +0100859static ssize_t mem_read(struct file *file, char __user *buf,
860 size_t count, loff_t *ppos)
861{
862 return mem_rw(file, buf, count, ppos, 0);
863}
864
865static ssize_t mem_write(struct file *file, const char __user *buf,
866 size_t count, loff_t *ppos)
867{
868 return mem_rw(file, (char __user*)buf, count, ppos, 1);
869}
870
Matt Mackall85863e42008-02-04 22:29:04 -0800871loff_t mem_lseek(struct file *file, loff_t offset, int orig)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700872{
873 switch (orig) {
874 case 0:
875 file->f_pos = offset;
876 break;
877 case 1:
878 file->f_pos += offset;
879 break;
880 default:
881 return -EINVAL;
882 }
883 force_successful_syscall_return();
884 return file->f_pos;
885}
886
Linus Torvaldse2683372012-01-17 15:21:19 -0800887static int mem_release(struct inode *inode, struct file *file)
888{
889 struct mm_struct *mm = file->private_data;
Oleg Nesterov71879d32012-01-31 17:14:38 +0100890 if (mm)
Oleg Nesterov6d08f2c2012-01-31 17:15:11 +0100891 mmdrop(mm);
Linus Torvaldse2683372012-01-17 15:21:19 -0800892 return 0;
893}
894
Arjan van de Ven00977a52007-02-12 00:55:34 -0800895static const struct file_operations proc_mem_operations = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700896 .llseek = mem_lseek,
897 .read = mem_read,
898 .write = mem_write,
899 .open = mem_open,
Linus Torvaldse2683372012-01-17 15:21:19 -0800900 .release = mem_release,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700901};
902
Cong Wangb409e572012-05-31 16:26:17 -0700903static int environ_open(struct inode *inode, struct file *file)
904{
905 return __mem_open(inode, file, PTRACE_MODE_READ);
906}
907
James Pearson315e28c2007-10-16 23:30:17 -0700908static ssize_t environ_read(struct file *file, char __user *buf,
909 size_t count, loff_t *ppos)
910{
James Pearson315e28c2007-10-16 23:30:17 -0700911 char *page;
912 unsigned long src = *ppos;
Cong Wangb409e572012-05-31 16:26:17 -0700913 int ret = 0;
914 struct mm_struct *mm = file->private_data;
Mateusz Guzika3b609e2016-01-20 15:01:05 -0800915 unsigned long env_start, env_end;
James Pearson315e28c2007-10-16 23:30:17 -0700916
Mathias Krause8148a732016-05-05 16:22:26 -0700917 /* Ensure the process spawned far enough to have an environment. */
918 if (!mm || !mm->env_end)
Cong Wangb409e572012-05-31 16:26:17 -0700919 return 0;
James Pearson315e28c2007-10-16 23:30:17 -0700920
Michal Hocko0ee931c2017-09-13 16:28:29 -0700921 page = (char *)__get_free_page(GFP_KERNEL);
James Pearson315e28c2007-10-16 23:30:17 -0700922 if (!page)
Cong Wangb409e572012-05-31 16:26:17 -0700923 return -ENOMEM;
James Pearson315e28c2007-10-16 23:30:17 -0700924
Al Virod6f64b82011-02-15 22:26:01 -0500925 ret = 0;
Vegard Nossum388f7932017-02-27 14:30:13 -0800926 if (!mmget_not_zero(mm))
Cong Wangb409e572012-05-31 16:26:17 -0700927 goto free;
Mateusz Guzika3b609e2016-01-20 15:01:05 -0800928
Yang Shi88aa7cc2018-06-07 17:05:28 -0700929 spin_lock(&mm->arg_lock);
Mateusz Guzika3b609e2016-01-20 15:01:05 -0800930 env_start = mm->env_start;
931 env_end = mm->env_end;
Yang Shi88aa7cc2018-06-07 17:05:28 -0700932 spin_unlock(&mm->arg_lock);
Mateusz Guzika3b609e2016-01-20 15:01:05 -0800933
James Pearson315e28c2007-10-16 23:30:17 -0700934 while (count > 0) {
Djalal Harounie8905ec2012-07-30 14:42:26 -0700935 size_t this_len, max_len;
936 int retval;
937
Mateusz Guzika3b609e2016-01-20 15:01:05 -0800938 if (src >= (env_end - env_start))
Djalal Harounie8905ec2012-07-30 14:42:26 -0700939 break;
James Pearson315e28c2007-10-16 23:30:17 -0700940
Mateusz Guzika3b609e2016-01-20 15:01:05 -0800941 this_len = env_end - (env_start + src);
James Pearson315e28c2007-10-16 23:30:17 -0700942
Djalal Harounie8905ec2012-07-30 14:42:26 -0700943 max_len = min_t(size_t, PAGE_SIZE, count);
944 this_len = min(max_len, this_len);
James Pearson315e28c2007-10-16 23:30:17 -0700945
Willy Tarreau7f7ccc22018-05-11 08:11:44 +0200946 retval = access_remote_vm(mm, (env_start + src), page, this_len, FOLL_ANON);
James Pearson315e28c2007-10-16 23:30:17 -0700947
948 if (retval <= 0) {
949 ret = retval;
950 break;
951 }
952
953 if (copy_to_user(buf, page, retval)) {
954 ret = -EFAULT;
955 break;
956 }
957
958 ret += retval;
959 src += retval;
960 buf += retval;
961 count -= retval;
962 }
963 *ppos = src;
James Pearson315e28c2007-10-16 23:30:17 -0700964 mmput(mm);
Cong Wangb409e572012-05-31 16:26:17 -0700965
966free:
James Pearson315e28c2007-10-16 23:30:17 -0700967 free_page((unsigned long) page);
James Pearson315e28c2007-10-16 23:30:17 -0700968 return ret;
969}
970
971static const struct file_operations proc_environ_operations = {
Cong Wangb409e572012-05-31 16:26:17 -0700972 .open = environ_open,
James Pearson315e28c2007-10-16 23:30:17 -0700973 .read = environ_read,
Arnd Bergmann87df8422010-03-17 23:06:02 +0100974 .llseek = generic_file_llseek,
Cong Wangb409e572012-05-31 16:26:17 -0700975 .release = mem_release,
James Pearson315e28c2007-10-16 23:30:17 -0700976};
977
Al Viroc5317162016-10-05 18:43:43 -0400978static int auxv_open(struct inode *inode, struct file *file)
979{
980 return __mem_open(inode, file, PTRACE_MODE_READ_FSCREDS);
981}
982
983static ssize_t auxv_read(struct file *file, char __user *buf,
984 size_t count, loff_t *ppos)
985{
986 struct mm_struct *mm = file->private_data;
987 unsigned int nwords = 0;
Leon Yu06b28492016-10-27 17:46:50 -0700988
989 if (!mm)
990 return 0;
Al Viroc5317162016-10-05 18:43:43 -0400991 do {
992 nwords += 2;
993 } while (mm->saved_auxv[nwords - 2] != 0); /* AT_NULL */
994 return simple_read_from_buffer(buf, count, ppos, mm->saved_auxv,
995 nwords * sizeof(mm->saved_auxv[0]));
996}
997
998static const struct file_operations proc_auxv_operations = {
999 .open = auxv_open,
1000 .read = auxv_read,
1001 .llseek = generic_file_llseek,
1002 .release = mem_release,
1003};
1004
David Rientjesfa0cbbf2012-11-12 17:53:04 -08001005static ssize_t oom_adj_read(struct file *file, char __user *buf, size_t count,
1006 loff_t *ppos)
1007{
Al Viro496ad9a2013-01-23 17:07:38 -05001008 struct task_struct *task = get_proc_task(file_inode(file));
David Rientjesfa0cbbf2012-11-12 17:53:04 -08001009 char buffer[PROC_NUMBUF];
1010 int oom_adj = OOM_ADJUST_MIN;
1011 size_t len;
David Rientjesfa0cbbf2012-11-12 17:53:04 -08001012
1013 if (!task)
1014 return -ESRCH;
Michal Hockof913da52016-07-28 15:44:37 -07001015 if (task->signal->oom_score_adj == OOM_SCORE_ADJ_MAX)
1016 oom_adj = OOM_ADJUST_MAX;
1017 else
1018 oom_adj = (task->signal->oom_score_adj * -OOM_DISABLE) /
1019 OOM_SCORE_ADJ_MAX;
David Rientjesfa0cbbf2012-11-12 17:53:04 -08001020 put_task_struct(task);
1021 len = snprintf(buffer, sizeof(buffer), "%d\n", oom_adj);
1022 return simple_read_from_buffer(buf, count, ppos, buffer, len);
1023}
1024
Michal Hocko1d5f0ac2016-07-28 15:44:40 -07001025static int __set_oom_adj(struct file *file, int oom_adj, bool legacy)
1026{
1027 static DEFINE_MUTEX(oom_adj_mutex);
Michal Hocko44a70ade2016-07-28 15:44:43 -07001028 struct mm_struct *mm = NULL;
Michal Hocko1d5f0ac2016-07-28 15:44:40 -07001029 struct task_struct *task;
1030 int err = 0;
1031
1032 task = get_proc_task(file_inode(file));
1033 if (!task)
1034 return -ESRCH;
1035
1036 mutex_lock(&oom_adj_mutex);
1037 if (legacy) {
1038 if (oom_adj < task->signal->oom_score_adj &&
1039 !capable(CAP_SYS_RESOURCE)) {
1040 err = -EACCES;
1041 goto err_unlock;
1042 }
1043 /*
1044 * /proc/pid/oom_adj is provided for legacy purposes, ask users to use
1045 * /proc/pid/oom_score_adj instead.
1046 */
1047 pr_warn_once("%s (%d): /proc/%d/oom_adj is deprecated, please use /proc/%d/oom_score_adj instead.\n",
1048 current->comm, task_pid_nr(current), task_pid_nr(task),
1049 task_pid_nr(task));
1050 } else {
1051 if ((short)oom_adj < task->signal->oom_score_adj_min &&
1052 !capable(CAP_SYS_RESOURCE)) {
1053 err = -EACCES;
1054 goto err_unlock;
1055 }
1056 }
1057
Michal Hocko44a70ade2016-07-28 15:44:43 -07001058 /*
1059 * Make sure we will check other processes sharing the mm if this is
1060 * not vfrok which wants its own oom_score_adj.
1061 * pin the mm so it doesn't go away and get reused after task_unlock
1062 */
1063 if (!task->vfork_done) {
1064 struct task_struct *p = find_lock_task_mm(task);
1065
1066 if (p) {
1067 if (atomic_read(&p->mm->mm_users) > 1) {
1068 mm = p->mm;
Vegard Nossumf1f10072017-02-27 14:30:07 -08001069 mmgrab(mm);
Michal Hocko44a70ade2016-07-28 15:44:43 -07001070 }
1071 task_unlock(p);
1072 }
1073 }
1074
Michal Hocko1d5f0ac2016-07-28 15:44:40 -07001075 task->signal->oom_score_adj = oom_adj;
1076 if (!legacy && has_capability_noaudit(current, CAP_SYS_RESOURCE))
1077 task->signal->oom_score_adj_min = (short)oom_adj;
1078 trace_oom_score_adj_update(task);
Michal Hocko44a70ade2016-07-28 15:44:43 -07001079
1080 if (mm) {
1081 struct task_struct *p;
1082
1083 rcu_read_lock();
1084 for_each_process(p) {
1085 if (same_thread_group(task, p))
1086 continue;
1087
1088 /* do not touch kernel threads or the global init */
1089 if (p->flags & PF_KTHREAD || is_global_init(p))
1090 continue;
1091
1092 task_lock(p);
1093 if (!p->vfork_done && process_shares_mm(p, mm)) {
Michal Hocko44a70ade2016-07-28 15:44:43 -07001094 p->signal->oom_score_adj = oom_adj;
1095 if (!legacy && has_capability_noaudit(current, CAP_SYS_RESOURCE))
1096 p->signal->oom_score_adj_min = (short)oom_adj;
1097 }
1098 task_unlock(p);
1099 }
1100 rcu_read_unlock();
1101 mmdrop(mm);
1102 }
Michal Hocko1d5f0ac2016-07-28 15:44:40 -07001103err_unlock:
1104 mutex_unlock(&oom_adj_mutex);
1105 put_task_struct(task);
1106 return err;
1107}
Michal Hockof913da52016-07-28 15:44:37 -07001108
David Rientjesb72bdfa2015-11-05 18:50:32 -08001109/*
1110 * /proc/pid/oom_adj exists solely for backwards compatibility with previous
1111 * kernels. The effective policy is defined by oom_score_adj, which has a
1112 * different scale: oom_adj grew exponentially and oom_score_adj grows linearly.
1113 * Values written to oom_adj are simply mapped linearly to oom_score_adj.
1114 * Processes that become oom disabled via oom_adj will still be oom disabled
1115 * with this implementation.
1116 *
1117 * oom_adj cannot be removed since existing userspace binaries use it.
1118 */
David Rientjesfa0cbbf2012-11-12 17:53:04 -08001119static ssize_t oom_adj_write(struct file *file, const char __user *buf,
1120 size_t count, loff_t *ppos)
1121{
David Rientjesfa0cbbf2012-11-12 17:53:04 -08001122 char buffer[PROC_NUMBUF];
1123 int oom_adj;
David Rientjesfa0cbbf2012-11-12 17:53:04 -08001124 int err;
1125
1126 memset(buffer, 0, sizeof(buffer));
1127 if (count > sizeof(buffer) - 1)
1128 count = sizeof(buffer) - 1;
1129 if (copy_from_user(buffer, buf, count)) {
1130 err = -EFAULT;
1131 goto out;
1132 }
1133
1134 err = kstrtoint(strstrip(buffer), 0, &oom_adj);
1135 if (err)
1136 goto out;
1137 if ((oom_adj < OOM_ADJUST_MIN || oom_adj > OOM_ADJUST_MAX) &&
1138 oom_adj != OOM_DISABLE) {
1139 err = -EINVAL;
1140 goto out;
1141 }
1142
David Rientjesfa0cbbf2012-11-12 17:53:04 -08001143 /*
1144 * Scale /proc/pid/oom_score_adj appropriately ensuring that a maximum
1145 * value is always attainable.
1146 */
1147 if (oom_adj == OOM_ADJUST_MAX)
1148 oom_adj = OOM_SCORE_ADJ_MAX;
1149 else
1150 oom_adj = (oom_adj * OOM_SCORE_ADJ_MAX) / -OOM_DISABLE;
1151
Michal Hocko1d5f0ac2016-07-28 15:44:40 -07001152 err = __set_oom_adj(file, oom_adj, true);
David Rientjesfa0cbbf2012-11-12 17:53:04 -08001153out:
1154 return err < 0 ? err : count;
1155}
1156
1157static const struct file_operations proc_oom_adj_operations = {
1158 .read = oom_adj_read,
1159 .write = oom_adj_write,
1160 .llseek = generic_file_llseek,
1161};
1162
David Rientjesa63d83f2010-08-09 17:19:46 -07001163static ssize_t oom_score_adj_read(struct file *file, char __user *buf,
1164 size_t count, loff_t *ppos)
1165{
Al Viro496ad9a2013-01-23 17:07:38 -05001166 struct task_struct *task = get_proc_task(file_inode(file));
David Rientjesa63d83f2010-08-09 17:19:46 -07001167 char buffer[PROC_NUMBUF];
David Rientjesa9c58b902012-12-11 16:02:54 -08001168 short oom_score_adj = OOM_SCORE_ADJ_MIN;
David Rientjesa63d83f2010-08-09 17:19:46 -07001169 size_t len;
1170
1171 if (!task)
1172 return -ESRCH;
Michal Hockof913da52016-07-28 15:44:37 -07001173 oom_score_adj = task->signal->oom_score_adj;
David Rientjesa63d83f2010-08-09 17:19:46 -07001174 put_task_struct(task);
David Rientjesa9c58b902012-12-11 16:02:54 -08001175 len = snprintf(buffer, sizeof(buffer), "%hd\n", oom_score_adj);
David Rientjesa63d83f2010-08-09 17:19:46 -07001176 return simple_read_from_buffer(buf, count, ppos, buffer, len);
1177}
1178
1179static ssize_t oom_score_adj_write(struct file *file, const char __user *buf,
1180 size_t count, loff_t *ppos)
1181{
David Rientjesa63d83f2010-08-09 17:19:46 -07001182 char buffer[PROC_NUMBUF];
Alexey Dobriyan0a8cb8e2011-05-26 16:25:50 -07001183 int oom_score_adj;
David Rientjesa63d83f2010-08-09 17:19:46 -07001184 int err;
1185
1186 memset(buffer, 0, sizeof(buffer));
1187 if (count > sizeof(buffer) - 1)
1188 count = sizeof(buffer) - 1;
David Rientjes723548b2010-10-26 14:21:25 -07001189 if (copy_from_user(buffer, buf, count)) {
1190 err = -EFAULT;
1191 goto out;
1192 }
David Rientjesa63d83f2010-08-09 17:19:46 -07001193
Alexey Dobriyan0a8cb8e2011-05-26 16:25:50 -07001194 err = kstrtoint(strstrip(buffer), 0, &oom_score_adj);
David Rientjesa63d83f2010-08-09 17:19:46 -07001195 if (err)
David Rientjes723548b2010-10-26 14:21:25 -07001196 goto out;
David Rientjesa63d83f2010-08-09 17:19:46 -07001197 if (oom_score_adj < OOM_SCORE_ADJ_MIN ||
David Rientjes723548b2010-10-26 14:21:25 -07001198 oom_score_adj > OOM_SCORE_ADJ_MAX) {
1199 err = -EINVAL;
1200 goto out;
1201 }
David Rientjesa63d83f2010-08-09 17:19:46 -07001202
Michal Hocko1d5f0ac2016-07-28 15:44:40 -07001203 err = __set_oom_adj(file, oom_score_adj, false);
David Rientjes723548b2010-10-26 14:21:25 -07001204out:
1205 return err < 0 ? err : count;
David Rientjesa63d83f2010-08-09 17:19:46 -07001206}
1207
1208static const struct file_operations proc_oom_score_adj_operations = {
1209 .read = oom_score_adj_read,
1210 .write = oom_score_adj_write,
Arnd Bergmann6038f372010-08-15 18:52:59 +02001211 .llseek = default_llseek,
David Rientjesa63d83f2010-08-09 17:19:46 -07001212};
1213
Richard Guy Briggs4b7d2482019-01-22 17:06:39 -05001214#ifdef CONFIG_AUDIT
Alexey Dobriyanb4eb4f72016-10-29 19:04:39 +03001215#define TMPBUFLEN 11
Linus Torvalds1da177e2005-04-16 15:20:36 -07001216static ssize_t proc_loginuid_read(struct file * file, char __user * buf,
1217 size_t count, loff_t *ppos)
1218{
Al Viro496ad9a2013-01-23 17:07:38 -05001219 struct inode * inode = file_inode(file);
Eric W. Biederman99f89552006-06-26 00:25:55 -07001220 struct task_struct *task = get_proc_task(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001221 ssize_t length;
1222 char tmpbuf[TMPBUFLEN];
1223
Eric W. Biederman99f89552006-06-26 00:25:55 -07001224 if (!task)
1225 return -ESRCH;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001226 length = scnprintf(tmpbuf, TMPBUFLEN, "%u",
Eric W. Biedermane1760bd2012-09-10 22:39:43 -07001227 from_kuid(file->f_cred->user_ns,
1228 audit_get_loginuid(task)));
Eric W. Biederman99f89552006-06-26 00:25:55 -07001229 put_task_struct(task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001230 return simple_read_from_buffer(buf, count, ppos, tmpbuf, length);
1231}
1232
1233static ssize_t proc_loginuid_write(struct file * file, const char __user * buf,
1234 size_t count, loff_t *ppos)
1235{
Al Viro496ad9a2013-01-23 17:07:38 -05001236 struct inode * inode = file_inode(file);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001237 uid_t loginuid;
Eric W. Biedermane1760bd2012-09-10 22:39:43 -07001238 kuid_t kloginuid;
Alexey Dobriyan774636e2015-09-09 15:36:59 -07001239 int rv;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001240
Paul E. McKenney7dc52152010-02-22 17:04:52 -08001241 rcu_read_lock();
1242 if (current != pid_task(proc_pid(inode), PIDTYPE_PID)) {
1243 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001244 return -EPERM;
Paul E. McKenney7dc52152010-02-22 17:04:52 -08001245 }
1246 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001247
Linus Torvalds1da177e2005-04-16 15:20:36 -07001248 if (*ppos != 0) {
1249 /* No partial writes. */
1250 return -EINVAL;
1251 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001252
Alexey Dobriyan774636e2015-09-09 15:36:59 -07001253 rv = kstrtou32_from_user(buf, count, 10, &loginuid);
1254 if (rv < 0)
1255 return rv;
Eric Paris81407c82013-05-24 09:49:14 -04001256
1257 /* is userspace tring to explicitly UNSET the loginuid? */
1258 if (loginuid == AUDIT_UID_UNSET) {
1259 kloginuid = INVALID_UID;
1260 } else {
1261 kloginuid = make_kuid(file->f_cred->user_ns, loginuid);
Alexey Dobriyan774636e2015-09-09 15:36:59 -07001262 if (!uid_valid(kloginuid))
1263 return -EINVAL;
Eric W. Biedermane1760bd2012-09-10 22:39:43 -07001264 }
1265
Alexey Dobriyan774636e2015-09-09 15:36:59 -07001266 rv = audit_set_loginuid(kloginuid);
1267 if (rv < 0)
1268 return rv;
1269 return count;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001270}
1271
Arjan van de Ven00977a52007-02-12 00:55:34 -08001272static const struct file_operations proc_loginuid_operations = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001273 .read = proc_loginuid_read,
1274 .write = proc_loginuid_write,
Arnd Bergmann87df8422010-03-17 23:06:02 +01001275 .llseek = generic_file_llseek,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001276};
Eric Paris1e0bd752008-03-13 08:15:31 -04001277
1278static ssize_t proc_sessionid_read(struct file * file, char __user * buf,
1279 size_t count, loff_t *ppos)
1280{
Al Viro496ad9a2013-01-23 17:07:38 -05001281 struct inode * inode = file_inode(file);
Eric Paris1e0bd752008-03-13 08:15:31 -04001282 struct task_struct *task = get_proc_task(inode);
1283 ssize_t length;
1284 char tmpbuf[TMPBUFLEN];
1285
1286 if (!task)
1287 return -ESRCH;
1288 length = scnprintf(tmpbuf, TMPBUFLEN, "%u",
1289 audit_get_sessionid(task));
1290 put_task_struct(task);
1291 return simple_read_from_buffer(buf, count, ppos, tmpbuf, length);
1292}
1293
1294static const struct file_operations proc_sessionid_operations = {
1295 .read = proc_sessionid_read,
Arnd Bergmann87df8422010-03-17 23:06:02 +01001296 .llseek = generic_file_llseek,
Eric Paris1e0bd752008-03-13 08:15:31 -04001297};
Linus Torvalds1da177e2005-04-16 15:20:36 -07001298#endif
1299
Akinobu Mitaf4f154f2006-12-08 02:39:47 -08001300#ifdef CONFIG_FAULT_INJECTION
1301static ssize_t proc_fault_inject_read(struct file * file, char __user * buf,
1302 size_t count, loff_t *ppos)
1303{
Al Viro496ad9a2013-01-23 17:07:38 -05001304 struct task_struct *task = get_proc_task(file_inode(file));
Akinobu Mitaf4f154f2006-12-08 02:39:47 -08001305 char buffer[PROC_NUMBUF];
1306 size_t len;
1307 int make_it_fail;
Akinobu Mitaf4f154f2006-12-08 02:39:47 -08001308
1309 if (!task)
1310 return -ESRCH;
1311 make_it_fail = task->make_it_fail;
1312 put_task_struct(task);
1313
1314 len = snprintf(buffer, sizeof(buffer), "%i\n", make_it_fail);
Akinobu Mita0c28f282007-05-08 00:31:41 -07001315
1316 return simple_read_from_buffer(buf, count, ppos, buffer, len);
Akinobu Mitaf4f154f2006-12-08 02:39:47 -08001317}
1318
1319static ssize_t proc_fault_inject_write(struct file * file,
1320 const char __user * buf, size_t count, loff_t *ppos)
1321{
1322 struct task_struct *task;
Alexey Dobriyan774636e2015-09-09 15:36:59 -07001323 char buffer[PROC_NUMBUF];
Akinobu Mitaf4f154f2006-12-08 02:39:47 -08001324 int make_it_fail;
Alexey Dobriyan774636e2015-09-09 15:36:59 -07001325 int rv;
Akinobu Mitaf4f154f2006-12-08 02:39:47 -08001326
1327 if (!capable(CAP_SYS_RESOURCE))
1328 return -EPERM;
1329 memset(buffer, 0, sizeof(buffer));
1330 if (count > sizeof(buffer) - 1)
1331 count = sizeof(buffer) - 1;
1332 if (copy_from_user(buffer, buf, count))
1333 return -EFAULT;
Alexey Dobriyan774636e2015-09-09 15:36:59 -07001334 rv = kstrtoint(strstrip(buffer), 0, &make_it_fail);
1335 if (rv < 0)
1336 return rv;
Dave Jones16caed32014-04-07 15:39:15 -07001337 if (make_it_fail < 0 || make_it_fail > 1)
1338 return -EINVAL;
1339
Al Viro496ad9a2013-01-23 17:07:38 -05001340 task = get_proc_task(file_inode(file));
Akinobu Mitaf4f154f2006-12-08 02:39:47 -08001341 if (!task)
1342 return -ESRCH;
1343 task->make_it_fail = make_it_fail;
1344 put_task_struct(task);
Vincent Licba8aaf2009-09-22 16:45:38 -07001345
1346 return count;
Akinobu Mitaf4f154f2006-12-08 02:39:47 -08001347}
1348
Arjan van de Ven00977a52007-02-12 00:55:34 -08001349static const struct file_operations proc_fault_inject_operations = {
Akinobu Mitaf4f154f2006-12-08 02:39:47 -08001350 .read = proc_fault_inject_read,
1351 .write = proc_fault_inject_write,
Arnd Bergmann87df8422010-03-17 23:06:02 +01001352 .llseek = generic_file_llseek,
Akinobu Mitaf4f154f2006-12-08 02:39:47 -08001353};
Dmitry Vyukove41d58182017-07-12 14:34:35 -07001354
1355static ssize_t proc_fail_nth_write(struct file *file, const char __user *buf,
1356 size_t count, loff_t *ppos)
1357{
1358 struct task_struct *task;
Akinobu Mita9049f2f2017-07-14 14:49:52 -07001359 int err;
1360 unsigned int n;
Dmitry Vyukove41d58182017-07-12 14:34:35 -07001361
Akinobu Mita9049f2f2017-07-14 14:49:52 -07001362 err = kstrtouint_from_user(buf, count, 0, &n);
Dmitry Vyukove41d58182017-07-12 14:34:35 -07001363 if (err)
1364 return err;
Akinobu Mita1203c8e2017-07-14 14:49:57 -07001365
1366 task = get_proc_task(file_inode(file));
1367 if (!task)
1368 return -ESRCH;
Alexey Dobriyan9f7118b2018-02-06 15:36:55 -08001369 task->fail_nth = n;
Akinobu Mita1203c8e2017-07-14 14:49:57 -07001370 put_task_struct(task);
1371
Dmitry Vyukove41d58182017-07-12 14:34:35 -07001372 return count;
1373}
1374
1375static ssize_t proc_fail_nth_read(struct file *file, char __user *buf,
1376 size_t count, loff_t *ppos)
1377{
1378 struct task_struct *task;
Akinobu Mitabfc74092017-07-14 14:49:54 -07001379 char numbuf[PROC_NUMBUF];
1380 ssize_t len;
Dmitry Vyukove41d58182017-07-12 14:34:35 -07001381
1382 task = get_proc_task(file_inode(file));
1383 if (!task)
1384 return -ESRCH;
Alexey Dobriyan9f7118b2018-02-06 15:36:55 -08001385 len = snprintf(numbuf, sizeof(numbuf), "%u\n", task->fail_nth);
Akinobu Mita1203c8e2017-07-14 14:49:57 -07001386 put_task_struct(task);
Alexey Dobriyana44937f2018-08-21 21:54:27 -07001387 return simple_read_from_buffer(buf, count, ppos, numbuf, len);
Dmitry Vyukove41d58182017-07-12 14:34:35 -07001388}
1389
1390static const struct file_operations proc_fail_nth_operations = {
1391 .read = proc_fail_nth_read,
1392 .write = proc_fail_nth_write,
1393};
Akinobu Mitaf4f154f2006-12-08 02:39:47 -08001394#endif
1395
Arjan van de Ven97455122008-01-25 21:08:34 +01001396
Ingo Molnar43ae34c2007-07-09 18:52:00 +02001397#ifdef CONFIG_SCHED_DEBUG
1398/*
1399 * Print out various scheduling related per-task fields:
1400 */
1401static int sched_show(struct seq_file *m, void *v)
1402{
1403 struct inode *inode = m->private;
Christoph Hellwig76f668b2018-05-16 07:19:01 +02001404 struct pid_namespace *ns = proc_pid_ns(inode);
Ingo Molnar43ae34c2007-07-09 18:52:00 +02001405 struct task_struct *p;
1406
Ingo Molnar43ae34c2007-07-09 18:52:00 +02001407 p = get_proc_task(inode);
1408 if (!p)
1409 return -ESRCH;
Aleksa Sarai74dc3382017-08-06 14:41:41 +10001410 proc_sched_show_task(p, ns, m);
Ingo Molnar43ae34c2007-07-09 18:52:00 +02001411
1412 put_task_struct(p);
1413
1414 return 0;
1415}
1416
1417static ssize_t
1418sched_write(struct file *file, const char __user *buf,
1419 size_t count, loff_t *offset)
1420{
Al Viro496ad9a2013-01-23 17:07:38 -05001421 struct inode *inode = file_inode(file);
Ingo Molnar43ae34c2007-07-09 18:52:00 +02001422 struct task_struct *p;
1423
Ingo Molnar43ae34c2007-07-09 18:52:00 +02001424 p = get_proc_task(inode);
1425 if (!p)
1426 return -ESRCH;
1427 proc_sched_set_task(p);
1428
1429 put_task_struct(p);
1430
1431 return count;
1432}
1433
1434static int sched_open(struct inode *inode, struct file *filp)
1435{
Jovi Zhangc6a34052011-01-12 17:00:34 -08001436 return single_open(filp, sched_show, inode);
Ingo Molnar43ae34c2007-07-09 18:52:00 +02001437}
1438
1439static const struct file_operations proc_pid_sched_operations = {
1440 .open = sched_open,
1441 .read = seq_read,
1442 .write = sched_write,
1443 .llseek = seq_lseek,
Alexey Dobriyan5ea473a2007-07-31 00:38:50 -07001444 .release = single_release,
Ingo Molnar43ae34c2007-07-09 18:52:00 +02001445};
1446
1447#endif
1448
Mike Galbraith5091faa2010-11-30 14:18:03 +01001449#ifdef CONFIG_SCHED_AUTOGROUP
1450/*
1451 * Print out autogroup related information:
1452 */
1453static int sched_autogroup_show(struct seq_file *m, void *v)
1454{
1455 struct inode *inode = m->private;
1456 struct task_struct *p;
1457
1458 p = get_proc_task(inode);
1459 if (!p)
1460 return -ESRCH;
1461 proc_sched_autogroup_show_task(p, m);
1462
1463 put_task_struct(p);
1464
1465 return 0;
1466}
1467
1468static ssize_t
1469sched_autogroup_write(struct file *file, const char __user *buf,
1470 size_t count, loff_t *offset)
1471{
Al Viro496ad9a2013-01-23 17:07:38 -05001472 struct inode *inode = file_inode(file);
Mike Galbraith5091faa2010-11-30 14:18:03 +01001473 struct task_struct *p;
1474 char buffer[PROC_NUMBUF];
Alexey Dobriyan0a8cb8e2011-05-26 16:25:50 -07001475 int nice;
Mike Galbraith5091faa2010-11-30 14:18:03 +01001476 int err;
1477
1478 memset(buffer, 0, sizeof(buffer));
1479 if (count > sizeof(buffer) - 1)
1480 count = sizeof(buffer) - 1;
1481 if (copy_from_user(buffer, buf, count))
1482 return -EFAULT;
1483
Alexey Dobriyan0a8cb8e2011-05-26 16:25:50 -07001484 err = kstrtoint(strstrip(buffer), 0, &nice);
1485 if (err < 0)
1486 return err;
Mike Galbraith5091faa2010-11-30 14:18:03 +01001487
1488 p = get_proc_task(inode);
1489 if (!p)
1490 return -ESRCH;
1491
Hiroshi Shimamoto2e5b5b32012-02-23 17:41:27 +09001492 err = proc_sched_autogroup_set_nice(p, nice);
Mike Galbraith5091faa2010-11-30 14:18:03 +01001493 if (err)
1494 count = err;
1495
1496 put_task_struct(p);
1497
1498 return count;
1499}
1500
1501static int sched_autogroup_open(struct inode *inode, struct file *filp)
1502{
1503 int ret;
1504
1505 ret = single_open(filp, sched_autogroup_show, NULL);
1506 if (!ret) {
1507 struct seq_file *m = filp->private_data;
1508
1509 m->private = inode;
1510 }
1511 return ret;
1512}
1513
1514static const struct file_operations proc_pid_sched_autogroup_operations = {
1515 .open = sched_autogroup_open,
1516 .read = seq_read,
1517 .write = sched_autogroup_write,
1518 .llseek = seq_lseek,
1519 .release = single_release,
1520};
1521
1522#endif /* CONFIG_SCHED_AUTOGROUP */
1523
john stultz4614a696b2009-12-14 18:00:05 -08001524static ssize_t comm_write(struct file *file, const char __user *buf,
1525 size_t count, loff_t *offset)
1526{
Al Viro496ad9a2013-01-23 17:07:38 -05001527 struct inode *inode = file_inode(file);
john stultz4614a696b2009-12-14 18:00:05 -08001528 struct task_struct *p;
1529 char buffer[TASK_COMM_LEN];
David Rientjes830e0fc2013-04-30 15:28:18 -07001530 const size_t maxlen = sizeof(buffer) - 1;
john stultz4614a696b2009-12-14 18:00:05 -08001531
1532 memset(buffer, 0, sizeof(buffer));
David Rientjes830e0fc2013-04-30 15:28:18 -07001533 if (copy_from_user(buffer, buf, count > maxlen ? maxlen : count))
john stultz4614a696b2009-12-14 18:00:05 -08001534 return -EFAULT;
1535
1536 p = get_proc_task(inode);
1537 if (!p)
1538 return -ESRCH;
1539
1540 if (same_thread_group(current, p))
1541 set_task_comm(p, buffer);
1542 else
1543 count = -EINVAL;
1544
1545 put_task_struct(p);
1546
1547 return count;
1548}
1549
1550static int comm_show(struct seq_file *m, void *v)
1551{
1552 struct inode *inode = m->private;
1553 struct task_struct *p;
1554
1555 p = get_proc_task(inode);
1556 if (!p)
1557 return -ESRCH;
1558
Tejun Heo88b72b32018-05-18 08:47:13 -07001559 proc_task_name(m, p, false);
1560 seq_putc(m, '\n');
john stultz4614a696b2009-12-14 18:00:05 -08001561
1562 put_task_struct(p);
1563
1564 return 0;
1565}
1566
1567static int comm_open(struct inode *inode, struct file *filp)
1568{
Jovi Zhangc6a34052011-01-12 17:00:34 -08001569 return single_open(filp, comm_show, inode);
john stultz4614a696b2009-12-14 18:00:05 -08001570}
1571
1572static const struct file_operations proc_pid_set_comm_operations = {
1573 .open = comm_open,
1574 .read = seq_read,
1575 .write = comm_write,
1576 .llseek = seq_lseek,
1577 .release = single_release,
1578};
1579
Cyrill Gorcunov7773fbc2012-01-10 15:11:20 -08001580static int proc_exe_link(struct dentry *dentry, struct path *exe_path)
Matt Helsley925d1c42008-04-29 01:01:36 -07001581{
1582 struct task_struct *task;
Matt Helsley925d1c42008-04-29 01:01:36 -07001583 struct file *exe_file;
1584
David Howells2b0143b2015-03-17 22:25:59 +00001585 task = get_proc_task(d_inode(dentry));
Matt Helsley925d1c42008-04-29 01:01:36 -07001586 if (!task)
1587 return -ENOENT;
Mateusz Guzikcd81a9172016-08-23 16:20:38 +02001588 exe_file = get_task_exe_file(task);
Matt Helsley925d1c42008-04-29 01:01:36 -07001589 put_task_struct(task);
Matt Helsley925d1c42008-04-29 01:01:36 -07001590 if (exe_file) {
1591 *exe_path = exe_file->f_path;
1592 path_get(&exe_file->f_path);
1593 fput(exe_file);
1594 return 0;
1595 } else
1596 return -ENOENT;
1597}
1598
Al Viro6b255392015-11-17 10:20:54 -05001599static const char *proc_pid_get_link(struct dentry *dentry,
Al Virofceef392015-12-29 15:58:39 -05001600 struct inode *inode,
1601 struct delayed_call *done)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001602{
Christoph Hellwig408ef012012-06-18 10:47:03 -04001603 struct path path;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001604 int error = -EACCES;
1605
Al Viro6b255392015-11-17 10:20:54 -05001606 if (!dentry)
1607 return ERR_PTR(-ECHILD);
1608
Eric W. Biederman778c1142006-06-26 00:25:58 -07001609 /* Are we allowed to snoop on the tasks file descriptors? */
1610 if (!proc_fd_access_allowed(inode))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001611 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001612
Christoph Hellwig408ef012012-06-18 10:47:03 -04001613 error = PROC_I(inode)->op.proc_get_link(dentry, &path);
1614 if (error)
1615 goto out;
1616
Al Viro6e77137b2015-05-02 13:37:52 -04001617 nd_jump_link(&path);
Christoph Hellwig408ef012012-06-18 10:47:03 -04001618 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001619out:
Al Viro008b1502005-08-20 00:17:39 +01001620 return ERR_PTR(error);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001621}
1622
Jan Blunck3dcd25f2008-02-14 19:38:35 -08001623static int do_proc_readlink(struct path *path, char __user *buffer, int buflen)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001624{
Michal Hocko0ee931c2017-09-13 16:28:29 -07001625 char *tmp = (char *)__get_free_page(GFP_KERNEL);
Jan Blunck3dcd25f2008-02-14 19:38:35 -08001626 char *pathname;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001627 int len;
1628
1629 if (!tmp)
1630 return -ENOMEM;
Akinobu Mita0c28f282007-05-08 00:31:41 -07001631
Eric W. Biederman7b2a69b2010-12-05 15:51:21 -08001632 pathname = d_path(path, tmp, PAGE_SIZE);
Jan Blunck3dcd25f2008-02-14 19:38:35 -08001633 len = PTR_ERR(pathname);
1634 if (IS_ERR(pathname))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001635 goto out;
Jan Blunck3dcd25f2008-02-14 19:38:35 -08001636 len = tmp + PAGE_SIZE - 1 - pathname;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001637
1638 if (len > buflen)
1639 len = buflen;
Jan Blunck3dcd25f2008-02-14 19:38:35 -08001640 if (copy_to_user(buffer, pathname, len))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001641 len = -EFAULT;
1642 out:
1643 free_page((unsigned long)tmp);
1644 return len;
1645}
1646
1647static int proc_pid_readlink(struct dentry * dentry, char __user * buffer, int buflen)
1648{
1649 int error = -EACCES;
David Howells2b0143b2015-03-17 22:25:59 +00001650 struct inode *inode = d_inode(dentry);
Jan Blunck3dcd25f2008-02-14 19:38:35 -08001651 struct path path;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001652
Eric W. Biederman778c1142006-06-26 00:25:58 -07001653 /* Are we allowed to snoop on the tasks file descriptors? */
1654 if (!proc_fd_access_allowed(inode))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001655 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001656
Cyrill Gorcunov7773fbc2012-01-10 15:11:20 -08001657 error = PROC_I(inode)->op.proc_get_link(dentry, &path);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001658 if (error)
1659 goto out;
1660
Jan Blunck3dcd25f2008-02-14 19:38:35 -08001661 error = do_proc_readlink(&path, buffer, buflen);
1662 path_put(&path);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001663out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001664 return error;
1665}
1666
Cyrill Gorcunovfaf60af2012-08-23 14:43:24 +04001667const struct inode_operations proc_pid_link_inode_operations = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001668 .readlink = proc_pid_readlink,
Al Viro6b255392015-11-17 10:20:54 -05001669 .get_link = proc_pid_get_link,
Linus Torvalds6d76fa52006-07-15 12:26:45 -07001670 .setattr = proc_setattr,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001671};
1672
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001673
1674/* building an inode */
1675
Al Viroc6eb50d2017-09-30 14:45:42 -04001676void task_dump_owner(struct task_struct *task, umode_t mode,
Eric W. Biederman68eb94f2017-01-03 10:23:11 +13001677 kuid_t *ruid, kgid_t *rgid)
1678{
1679 /* Depending on the state of dumpable compute who should own a
1680 * proc file for a task.
1681 */
1682 const struct cred *cred;
1683 kuid_t uid;
1684 kgid_t gid;
1685
Alexey Dobriyan2e0ad552018-04-20 14:56:03 -07001686 if (unlikely(task->flags & PF_KTHREAD)) {
1687 *ruid = GLOBAL_ROOT_UID;
1688 *rgid = GLOBAL_ROOT_GID;
1689 return;
1690 }
1691
Eric W. Biederman68eb94f2017-01-03 10:23:11 +13001692 /* Default to the tasks effective ownership */
1693 rcu_read_lock();
1694 cred = __task_cred(task);
1695 uid = cred->euid;
1696 gid = cred->egid;
1697 rcu_read_unlock();
1698
1699 /*
1700 * Before the /proc/pid/status file was created the only way to read
1701 * the effective uid of a /process was to stat /proc/pid. Reading
1702 * /proc/pid/status is slow enough that procps and other packages
1703 * kept stating /proc/pid. To keep the rules in /proc simple I have
1704 * made this apply to all per process world readable and executable
1705 * directories.
1706 */
1707 if (mode != (S_IFDIR|S_IRUGO|S_IXUGO)) {
1708 struct mm_struct *mm;
1709 task_lock(task);
1710 mm = task->mm;
1711 /* Make non-dumpable tasks owned by some root */
1712 if (mm) {
1713 if (get_dumpable(mm) != SUID_DUMP_USER) {
1714 struct user_namespace *user_ns = mm->user_ns;
1715
1716 uid = make_kuid(user_ns, 0);
1717 if (!uid_valid(uid))
1718 uid = GLOBAL_ROOT_UID;
1719
1720 gid = make_kgid(user_ns, 0);
1721 if (!gid_valid(gid))
1722 gid = GLOBAL_ROOT_GID;
1723 }
1724 } else {
1725 uid = GLOBAL_ROOT_UID;
1726 gid = GLOBAL_ROOT_GID;
1727 }
1728 task_unlock(task);
1729 }
1730 *ruid = uid;
1731 *rgid = gid;
1732}
1733
Andreas Gruenbacherdb978da2016-11-10 22:18:28 +01001734struct inode *proc_pid_make_inode(struct super_block * sb,
1735 struct task_struct *task, umode_t mode)
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001736{
1737 struct inode * inode;
1738 struct proc_inode *ei;
1739
1740 /* We need a new inode */
1741
1742 inode = new_inode(sb);
1743 if (!inode)
1744 goto out;
1745
1746 /* Common stuff */
1747 ei = PROC_I(inode);
Andreas Gruenbacherdb978da2016-11-10 22:18:28 +01001748 inode->i_mode = mode;
Christoph Hellwig85fe4022010-10-23 11:19:54 -04001749 inode->i_ino = get_next_ino();
Deepa Dinamani078cd822016-09-14 07:48:04 -07001750 inode->i_mtime = inode->i_atime = inode->i_ctime = current_time(inode);
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001751 inode->i_op = &proc_def_inode_operations;
1752
1753 /*
1754 * grab the reference to task.
1755 */
Oleg Nesterov1a657f782006-10-02 02:18:59 -07001756 ei->pid = get_task_pid(task, PIDTYPE_PID);
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001757 if (!ei->pid)
1758 goto out_unlock;
1759
Eric W. Biederman68eb94f2017-01-03 10:23:11 +13001760 task_dump_owner(task, 0, &inode->i_uid, &inode->i_gid);
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001761 security_task_to_inode(task, inode);
1762
1763out:
1764 return inode;
1765
1766out_unlock:
1767 iput(inode);
1768 return NULL;
1769}
1770
David Howellsa528d352017-01-31 16:46:22 +00001771int pid_getattr(const struct path *path, struct kstat *stat,
1772 u32 request_mask, unsigned int query_flags)
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001773{
David Howellsa528d352017-01-31 16:46:22 +00001774 struct inode *inode = d_inode(path->dentry);
Christoph Hellwig76f668b2018-05-16 07:19:01 +02001775 struct pid_namespace *pid = proc_pid_ns(inode);
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001776 struct task_struct *task;
David Howellsc69e8d92008-11-14 10:39:19 +11001777
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001778 generic_fillattr(inode, stat);
1779
Eric W. Biedermandcb0f222012-02-09 08:48:21 -08001780 stat->uid = GLOBAL_ROOT_UID;
1781 stat->gid = GLOBAL_ROOT_GID;
Alexey Dobriyan94116922018-06-07 17:10:07 -07001782 rcu_read_lock();
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001783 task = pid_task(proc_pid(inode), PIDTYPE_PID);
1784 if (task) {
Lafcadio Wluiki796f5712017-02-24 15:00:23 -08001785 if (!has_pid_permissions(pid, task, HIDEPID_INVISIBLE)) {
Vasiliy Kulikov04996802012-01-10 15:11:31 -08001786 rcu_read_unlock();
1787 /*
1788 * This doesn't prevent learning whether PID exists,
1789 * it only makes getattr() consistent with readdir().
1790 */
1791 return -ENOENT;
1792 }
Eric W. Biederman68eb94f2017-01-03 10:23:11 +13001793 task_dump_owner(task, inode->i_mode, &stat->uid, &stat->gid);
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001794 }
1795 rcu_read_unlock();
1796 return 0;
1797}
1798
1799/* dentry stuff */
1800
1801/*
Al Viro1bbc5512018-05-02 21:26:16 -04001802 * Set <pid>/... inode ownership (can change due to setuid(), etc.)
1803 */
1804void pid_update_inode(struct task_struct *task, struct inode *inode)
1805{
1806 task_dump_owner(task, inode->i_mode, &inode->i_uid, &inode->i_gid);
1807
1808 inode->i_mode &= ~(S_ISUID | S_ISGID);
1809 security_task_to_inode(task, inode);
1810}
1811
1812/*
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001813 * Rewrite the inode's ownerships here because the owning task may have
1814 * performed a setuid(), etc.
1815 *
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001816 */
Al Viro1bbc5512018-05-02 21:26:16 -04001817static int pid_revalidate(struct dentry *dentry, unsigned int flags)
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001818{
Nick Piggin34286d62011-01-07 17:49:57 +11001819 struct inode *inode;
1820 struct task_struct *task;
David Howellsc69e8d92008-11-14 10:39:19 +11001821
Al Viro0b728e12012-06-10 16:03:43 -04001822 if (flags & LOOKUP_RCU)
Nick Piggin34286d62011-01-07 17:49:57 +11001823 return -ECHILD;
1824
David Howells2b0143b2015-03-17 22:25:59 +00001825 inode = d_inode(dentry);
Nick Piggin34286d62011-01-07 17:49:57 +11001826 task = get_proc_task(inode);
1827
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001828 if (task) {
Al Viro1bbc5512018-05-02 21:26:16 -04001829 pid_update_inode(task, inode);
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001830 put_task_struct(task);
1831 return 1;
1832 }
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001833 return 0;
1834}
1835
Oleg Nesterovd855a4b2014-01-23 15:55:39 -08001836static inline bool proc_inode_is_dead(struct inode *inode)
1837{
1838 return !proc_pid(inode)->tasks[PIDTYPE_PID].first;
1839}
1840
David Howells1dd704b2013-04-12 01:08:50 +01001841int pid_delete_dentry(const struct dentry *dentry)
1842{
1843 /* Is the task we represent dead?
1844 * If so, then don't put the dentry on the lru list,
1845 * kill it immediately.
1846 */
David Howells2b0143b2015-03-17 22:25:59 +00001847 return proc_inode_is_dead(d_inode(dentry));
David Howells1dd704b2013-04-12 01:08:50 +01001848}
1849
Eric W. Biederman6b4e3062010-03-07 16:41:34 -08001850const struct dentry_operations pid_dentry_operations =
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001851{
1852 .d_revalidate = pid_revalidate,
1853 .d_delete = pid_delete_dentry,
1854};
1855
1856/* Lookups */
1857
Eric W. Biederman1c0d04c2006-10-02 02:18:57 -07001858/*
1859 * Fill a directory entry.
1860 *
1861 * If possible create the dcache entry and derive our inode number and
1862 * file type from dcache entry.
1863 *
1864 * Since all of the proc inode numbers are dynamically generated, the inode
1865 * numbers do not exist until the inode is cache. This means creating the
1866 * the dcache entry in readdir is necessary to keep the inode numbers
1867 * reported by readdir in sync with the inode numbers reported
1868 * by stat.
1869 */
Al Virof0c3b502013-05-16 12:07:31 -04001870bool proc_fill_cache(struct file *file, struct dir_context *ctx,
Alexey Dobriyana4ef3892018-06-07 17:10:10 -07001871 const char *name, unsigned int len,
Eric Dumazetc5141e62007-05-08 00:26:15 -07001872 instantiate_t instantiate, struct task_struct *task, const void *ptr)
Eric W. Biederman61a28782006-10-02 02:18:49 -07001873{
Al Virof0c3b502013-05-16 12:07:31 -04001874 struct dentry *child, *dir = file->f_path.dentry;
Al Viro1df98b82013-06-15 11:33:10 +04001875 struct qstr qname = QSTR_INIT(name, len);
Eric W. Biederman61a28782006-10-02 02:18:49 -07001876 struct inode *inode;
Al Viro0168b9e2018-05-03 09:21:05 -04001877 unsigned type = DT_UNKNOWN;
1878 ino_t ino = 1;
Eric W. Biederman61a28782006-10-02 02:18:49 -07001879
Al Viro1df98b82013-06-15 11:33:10 +04001880 child = d_hash_and_lookup(dir, &qname);
Eric W. Biederman61a28782006-10-02 02:18:49 -07001881 if (!child) {
Al Viro37817642016-04-20 16:31:31 -04001882 DECLARE_WAIT_QUEUE_HEAD_ONSTACK(wq);
1883 child = d_alloc_parallel(dir, &qname, &wq);
1884 if (IS_ERR(child))
Al Viro1df98b82013-06-15 11:33:10 +04001885 goto end_instantiate;
Al Viro37817642016-04-20 16:31:31 -04001886 if (d_in_lookup(child)) {
Al Viro0168b9e2018-05-03 09:21:05 -04001887 struct dentry *res;
1888 res = instantiate(child, task, ptr);
Al Viro37817642016-04-20 16:31:31 -04001889 d_lookup_done(child);
Al Viro0168b9e2018-05-03 09:21:05 -04001890 if (unlikely(res)) {
1891 dput(child);
1892 child = res;
Al Virod85b3992018-06-08 01:17:11 -04001893 if (IS_ERR(child))
1894 goto end_instantiate;
Al Viro37817642016-04-20 16:31:31 -04001895 }
Eric W. Biederman61a28782006-10-02 02:18:49 -07001896 }
1897 }
David Howells2b0143b2015-03-17 22:25:59 +00001898 inode = d_inode(child);
Al Viro147ce692013-06-15 10:26:35 +04001899 ino = inode->i_ino;
1900 type = inode->i_mode >> 12;
Eric W. Biederman61a28782006-10-02 02:18:49 -07001901 dput(child);
Al Virod85b3992018-06-08 01:17:11 -04001902end_instantiate:
Al Virof0c3b502013-05-16 12:07:31 -04001903 return dir_emit(ctx, name, len, ino, type);
Eric W. Biederman61a28782006-10-02 02:18:49 -07001904}
1905
Pavel Emelyanov640708a2012-01-10 15:11:23 -08001906/*
1907 * dname_to_vma_addr - maps a dentry name into two unsigned longs
1908 * which represent vma start and end addresses.
1909 */
1910static int dname_to_vma_addr(struct dentry *dentry,
1911 unsigned long *start, unsigned long *end)
1912{
Alexey Dobriyanac7f1062018-02-06 15:36:59 -08001913 const char *str = dentry->d_name.name;
1914 unsigned long long sval, eval;
1915 unsigned int len;
1916
Alexey Dobriyan35318db2018-04-10 16:41:14 -07001917 if (str[0] == '0' && str[1] != '-')
1918 return -EINVAL;
Alexey Dobriyanac7f1062018-02-06 15:36:59 -08001919 len = _parse_integer(str, 16, &sval);
1920 if (len & KSTRTOX_OVERFLOW)
Pavel Emelyanov640708a2012-01-10 15:11:23 -08001921 return -EINVAL;
Alexey Dobriyanac7f1062018-02-06 15:36:59 -08001922 if (sval != (unsigned long)sval)
1923 return -EINVAL;
1924 str += len;
1925
1926 if (*str != '-')
1927 return -EINVAL;
1928 str++;
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, &eval);
1933 if (len & KSTRTOX_OVERFLOW)
1934 return -EINVAL;
1935 if (eval != (unsigned long)eval)
1936 return -EINVAL;
1937 str += len;
1938
1939 if (*str != '\0')
1940 return -EINVAL;
1941
1942 *start = sval;
1943 *end = eval;
Pavel Emelyanov640708a2012-01-10 15:11:23 -08001944
1945 return 0;
1946}
1947
Al Viro0b728e12012-06-10 16:03:43 -04001948static int map_files_d_revalidate(struct dentry *dentry, unsigned int flags)
Pavel Emelyanov640708a2012-01-10 15:11:23 -08001949{
1950 unsigned long vm_start, vm_end;
1951 bool exact_vma_exists = false;
1952 struct mm_struct *mm = NULL;
1953 struct task_struct *task;
Pavel Emelyanov640708a2012-01-10 15:11:23 -08001954 struct inode *inode;
1955 int status = 0;
1956
Al Viro0b728e12012-06-10 16:03:43 -04001957 if (flags & LOOKUP_RCU)
Pavel Emelyanov640708a2012-01-10 15:11:23 -08001958 return -ECHILD;
1959
David Howells2b0143b2015-03-17 22:25:59 +00001960 inode = d_inode(dentry);
Pavel Emelyanov640708a2012-01-10 15:11:23 -08001961 task = get_proc_task(inode);
1962 if (!task)
1963 goto out_notask;
1964
Jann Horncaaee622016-01-20 15:00:04 -08001965 mm = mm_access(task, PTRACE_MODE_READ_FSCREDS);
Cong Wang2344bec2012-05-31 16:26:18 -07001966 if (IS_ERR_OR_NULL(mm))
Pavel Emelyanov640708a2012-01-10 15:11:23 -08001967 goto out;
1968
1969 if (!dname_to_vma_addr(dentry, &vm_start, &vm_end)) {
1970 down_read(&mm->mmap_sem);
1971 exact_vma_exists = !!find_exact_vma(mm, vm_start, vm_end);
1972 up_read(&mm->mmap_sem);
1973 }
1974
1975 mmput(mm);
1976
1977 if (exact_vma_exists) {
Eric W. Biederman68eb94f2017-01-03 10:23:11 +13001978 task_dump_owner(task, 0, &inode->i_uid, &inode->i_gid);
1979
Pavel Emelyanov640708a2012-01-10 15:11:23 -08001980 security_task_to_inode(task, inode);
1981 status = 1;
1982 }
1983
1984out:
1985 put_task_struct(task);
1986
1987out_notask:
Pavel Emelyanov640708a2012-01-10 15:11:23 -08001988 return status;
1989}
1990
1991static const struct dentry_operations tid_map_files_dentry_operations = {
1992 .d_revalidate = map_files_d_revalidate,
1993 .d_delete = pid_delete_dentry,
1994};
1995
Al Viro6b255392015-11-17 10:20:54 -05001996static int map_files_get_link(struct dentry *dentry, struct path *path)
Pavel Emelyanov640708a2012-01-10 15:11:23 -08001997{
1998 unsigned long vm_start, vm_end;
1999 struct vm_area_struct *vma;
2000 struct task_struct *task;
2001 struct mm_struct *mm;
2002 int rc;
2003
2004 rc = -ENOENT;
David Howells2b0143b2015-03-17 22:25:59 +00002005 task = get_proc_task(d_inode(dentry));
Pavel Emelyanov640708a2012-01-10 15:11:23 -08002006 if (!task)
2007 goto out;
2008
2009 mm = get_task_mm(task);
2010 put_task_struct(task);
2011 if (!mm)
2012 goto out;
2013
2014 rc = dname_to_vma_addr(dentry, &vm_start, &vm_end);
2015 if (rc)
2016 goto out_mmput;
2017
Artem Fetishev70335ab2014-03-10 15:49:45 -07002018 rc = -ENOENT;
Pavel Emelyanov640708a2012-01-10 15:11:23 -08002019 down_read(&mm->mmap_sem);
2020 vma = find_exact_vma(mm, vm_start, vm_end);
2021 if (vma && vma->vm_file) {
2022 *path = vma->vm_file->f_path;
2023 path_get(path);
2024 rc = 0;
2025 }
2026 up_read(&mm->mmap_sem);
2027
2028out_mmput:
2029 mmput(mm);
2030out:
2031 return rc;
2032}
2033
2034struct map_files_info {
Alexey Dobriyan20d28cd2018-02-06 15:37:06 -08002035 unsigned long start;
2036 unsigned long end;
Al Viro7b540d02012-08-27 14:55:26 -04002037 fmode_t mode;
Pavel Emelyanov640708a2012-01-10 15:11:23 -08002038};
2039
Calvin Owensbdb4d102015-09-09 15:35:54 -07002040/*
2041 * Only allow CAP_SYS_ADMIN to follow the links, due to concerns about how the
2042 * symlinks may be used to bypass permissions on ancestor directories in the
2043 * path to the file in question.
2044 */
2045static const char *
Al Viro6b255392015-11-17 10:20:54 -05002046proc_map_files_get_link(struct dentry *dentry,
Al Virofceef392015-12-29 15:58:39 -05002047 struct inode *inode,
2048 struct delayed_call *done)
Calvin Owensbdb4d102015-09-09 15:35:54 -07002049{
2050 if (!capable(CAP_SYS_ADMIN))
2051 return ERR_PTR(-EPERM);
2052
Al Virofceef392015-12-29 15:58:39 -05002053 return proc_pid_get_link(dentry, inode, done);
Calvin Owensbdb4d102015-09-09 15:35:54 -07002054}
2055
2056/*
Al Viro6b255392015-11-17 10:20:54 -05002057 * Identical to proc_pid_link_inode_operations except for get_link()
Calvin Owensbdb4d102015-09-09 15:35:54 -07002058 */
2059static const struct inode_operations proc_map_files_link_inode_operations = {
2060 .readlink = proc_pid_readlink,
Al Viro6b255392015-11-17 10:20:54 -05002061 .get_link = proc_map_files_get_link,
Calvin Owensbdb4d102015-09-09 15:35:54 -07002062 .setattr = proc_setattr,
2063};
2064
Al Viro0168b9e2018-05-03 09:21:05 -04002065static struct dentry *
2066proc_map_files_instantiate(struct dentry *dentry,
Pavel Emelyanov640708a2012-01-10 15:11:23 -08002067 struct task_struct *task, const void *ptr)
2068{
Al Viro7b540d02012-08-27 14:55:26 -04002069 fmode_t mode = (fmode_t)(unsigned long)ptr;
Pavel Emelyanov640708a2012-01-10 15:11:23 -08002070 struct proc_inode *ei;
2071 struct inode *inode;
2072
Al Viro0168b9e2018-05-03 09:21:05 -04002073 inode = proc_pid_make_inode(dentry->d_sb, task, S_IFLNK |
Andreas Gruenbacherdb978da2016-11-10 22:18:28 +01002074 ((mode & FMODE_READ ) ? S_IRUSR : 0) |
2075 ((mode & FMODE_WRITE) ? S_IWUSR : 0));
Pavel Emelyanov640708a2012-01-10 15:11:23 -08002076 if (!inode)
Al Viro0168b9e2018-05-03 09:21:05 -04002077 return ERR_PTR(-ENOENT);
Pavel Emelyanov640708a2012-01-10 15:11:23 -08002078
2079 ei = PROC_I(inode);
Al Viro6b255392015-11-17 10:20:54 -05002080 ei->op.proc_get_link = map_files_get_link;
Pavel Emelyanov640708a2012-01-10 15:11:23 -08002081
Calvin Owensbdb4d102015-09-09 15:35:54 -07002082 inode->i_op = &proc_map_files_link_inode_operations;
Pavel Emelyanov640708a2012-01-10 15:11:23 -08002083 inode->i_size = 64;
Pavel Emelyanov640708a2012-01-10 15:11:23 -08002084
2085 d_set_d_op(dentry, &tid_map_files_dentry_operations);
Al Viro0168b9e2018-05-03 09:21:05 -04002086 return d_splice_alias(inode, dentry);
Pavel Emelyanov640708a2012-01-10 15:11:23 -08002087}
2088
2089static struct dentry *proc_map_files_lookup(struct inode *dir,
Al Viro00cd8dd2012-06-10 17:13:09 -04002090 struct dentry *dentry, unsigned int flags)
Pavel Emelyanov640708a2012-01-10 15:11:23 -08002091{
2092 unsigned long vm_start, vm_end;
2093 struct vm_area_struct *vma;
2094 struct task_struct *task;
Al Viro0168b9e2018-05-03 09:21:05 -04002095 struct dentry *result;
Pavel Emelyanov640708a2012-01-10 15:11:23 -08002096 struct mm_struct *mm;
2097
Al Viro0168b9e2018-05-03 09:21:05 -04002098 result = ERR_PTR(-ENOENT);
Pavel Emelyanov640708a2012-01-10 15:11:23 -08002099 task = get_proc_task(dir);
2100 if (!task)
2101 goto out;
2102
Al Viro0168b9e2018-05-03 09:21:05 -04002103 result = ERR_PTR(-EACCES);
Jann Horncaaee622016-01-20 15:00:04 -08002104 if (!ptrace_may_access(task, PTRACE_MODE_READ_FSCREDS))
Pavel Emelyanov640708a2012-01-10 15:11:23 -08002105 goto out_put_task;
2106
Al Viro0168b9e2018-05-03 09:21:05 -04002107 result = ERR_PTR(-ENOENT);
Pavel Emelyanov640708a2012-01-10 15:11:23 -08002108 if (dname_to_vma_addr(dentry, &vm_start, &vm_end))
Cyrill Gorcunoveb94cd92012-05-17 17:03:25 -07002109 goto out_put_task;
Pavel Emelyanov640708a2012-01-10 15:11:23 -08002110
2111 mm = get_task_mm(task);
2112 if (!mm)
Cyrill Gorcunoveb94cd92012-05-17 17:03:25 -07002113 goto out_put_task;
Pavel Emelyanov640708a2012-01-10 15:11:23 -08002114
2115 down_read(&mm->mmap_sem);
2116 vma = find_exact_vma(mm, vm_start, vm_end);
2117 if (!vma)
2118 goto out_no_vma;
2119
Stanislav Kinsbursky05f56482012-11-26 16:29:42 -08002120 if (vma->vm_file)
Al Viro0168b9e2018-05-03 09:21:05 -04002121 result = proc_map_files_instantiate(dentry, task,
Stanislav Kinsbursky05f56482012-11-26 16:29:42 -08002122 (void *)(unsigned long)vma->vm_file->f_mode);
Pavel Emelyanov640708a2012-01-10 15:11:23 -08002123
2124out_no_vma:
2125 up_read(&mm->mmap_sem);
2126 mmput(mm);
Pavel Emelyanov640708a2012-01-10 15:11:23 -08002127out_put_task:
2128 put_task_struct(task);
2129out:
Al Viro0168b9e2018-05-03 09:21:05 -04002130 return result;
Pavel Emelyanov640708a2012-01-10 15:11:23 -08002131}
2132
2133static const struct inode_operations proc_map_files_inode_operations = {
2134 .lookup = proc_map_files_lookup,
2135 .permission = proc_fd_permission,
2136 .setattr = proc_setattr,
2137};
2138
2139static int
Al Virof0c3b502013-05-16 12:07:31 -04002140proc_map_files_readdir(struct file *file, struct dir_context *ctx)
Pavel Emelyanov640708a2012-01-10 15:11:23 -08002141{
Pavel Emelyanov640708a2012-01-10 15:11:23 -08002142 struct vm_area_struct *vma;
2143 struct task_struct *task;
2144 struct mm_struct *mm;
Al Virof0c3b502013-05-16 12:07:31 -04002145 unsigned long nr_files, pos, i;
Kent Overstreet94f8f3b2019-03-11 23:31:18 -07002146 GENRADIX(struct map_files_info) fa;
Al Virof0c3b502013-05-16 12:07:31 -04002147 struct map_files_info *p;
Pavel Emelyanov640708a2012-01-10 15:11:23 -08002148 int ret;
2149
Kent Overstreet94f8f3b2019-03-11 23:31:18 -07002150 genradix_init(&fa);
2151
Pavel Emelyanov640708a2012-01-10 15:11:23 -08002152 ret = -ENOENT;
Al Virof0c3b502013-05-16 12:07:31 -04002153 task = get_proc_task(file_inode(file));
Pavel Emelyanov640708a2012-01-10 15:11:23 -08002154 if (!task)
2155 goto out;
2156
2157 ret = -EACCES;
Jann Horncaaee622016-01-20 15:00:04 -08002158 if (!ptrace_may_access(task, PTRACE_MODE_READ_FSCREDS))
Pavel Emelyanov640708a2012-01-10 15:11:23 -08002159 goto out_put_task;
2160
2161 ret = 0;
Al Virof0c3b502013-05-16 12:07:31 -04002162 if (!dir_emit_dots(file, ctx))
2163 goto out_put_task;
Pavel Emelyanov640708a2012-01-10 15:11:23 -08002164
Al Virof0c3b502013-05-16 12:07:31 -04002165 mm = get_task_mm(task);
2166 if (!mm)
2167 goto out_put_task;
2168 down_read(&mm->mmap_sem);
Pavel Emelyanov640708a2012-01-10 15:11:23 -08002169
Al Virof0c3b502013-05-16 12:07:31 -04002170 nr_files = 0;
Pavel Emelyanov640708a2012-01-10 15:11:23 -08002171
Al Virof0c3b502013-05-16 12:07:31 -04002172 /*
2173 * We need two passes here:
2174 *
2175 * 1) Collect vmas of mapped files with mmap_sem taken
2176 * 2) Release mmap_sem and instantiate entries
2177 *
2178 * otherwise we get lockdep complained, since filldir()
2179 * routine might require mmap_sem taken in might_fault().
2180 */
Pavel Emelyanov640708a2012-01-10 15:11:23 -08002181
Al Virof0c3b502013-05-16 12:07:31 -04002182 for (vma = mm->mmap, pos = 2; vma; vma = vma->vm_next) {
Kent Overstreet94f8f3b2019-03-11 23:31:18 -07002183 if (!vma->vm_file)
2184 continue;
2185 if (++pos <= ctx->pos)
2186 continue;
Al Virof0c3b502013-05-16 12:07:31 -04002187
Kent Overstreet94f8f3b2019-03-11 23:31:18 -07002188 p = genradix_ptr_alloc(&fa, nr_files++, GFP_KERNEL);
2189 if (!p) {
Al Virof0c3b502013-05-16 12:07:31 -04002190 ret = -ENOMEM;
Al Virof0c3b502013-05-16 12:07:31 -04002191 up_read(&mm->mmap_sem);
2192 mmput(mm);
2193 goto out_put_task;
2194 }
Al Virof0c3b502013-05-16 12:07:31 -04002195
Kent Overstreet94f8f3b2019-03-11 23:31:18 -07002196 p->start = vma->vm_start;
2197 p->end = vma->vm_end;
2198 p->mode = vma->vm_file->f_mode;
Pavel Emelyanov640708a2012-01-10 15:11:23 -08002199 }
Al Virof0c3b502013-05-16 12:07:31 -04002200 up_read(&mm->mmap_sem);
Alexey Dobriyanfe079a52018-04-10 16:32:05 -07002201 mmput(mm);
Al Virof0c3b502013-05-16 12:07:31 -04002202
2203 for (i = 0; i < nr_files; i++) {
Alexey Dobriyan20d28cd2018-02-06 15:37:06 -08002204 char buf[4 * sizeof(long) + 2]; /* max: %lx-%lx\0 */
2205 unsigned int len;
2206
Kent Overstreet94f8f3b2019-03-11 23:31:18 -07002207 p = genradix_ptr(&fa, i);
Alexey Dobriyan20d28cd2018-02-06 15:37:06 -08002208 len = snprintf(buf, sizeof(buf), "%lx-%lx", p->start, p->end);
Al Virof0c3b502013-05-16 12:07:31 -04002209 if (!proc_fill_cache(file, ctx,
Alexey Dobriyan20d28cd2018-02-06 15:37:06 -08002210 buf, len,
Al Virof0c3b502013-05-16 12:07:31 -04002211 proc_map_files_instantiate,
2212 task,
2213 (void *)(unsigned long)p->mode))
2214 break;
2215 ctx->pos++;
2216 }
Pavel Emelyanov640708a2012-01-10 15:11:23 -08002217
Pavel Emelyanov640708a2012-01-10 15:11:23 -08002218out_put_task:
2219 put_task_struct(task);
2220out:
Kent Overstreet94f8f3b2019-03-11 23:31:18 -07002221 genradix_free(&fa);
Pavel Emelyanov640708a2012-01-10 15:11:23 -08002222 return ret;
2223}
2224
2225static const struct file_operations proc_map_files_operations = {
2226 .read = generic_read_dir,
Al Virof50752e2016-04-20 17:13:54 -04002227 .iterate_shared = proc_map_files_readdir,
2228 .llseek = generic_file_llseek,
Pavel Emelyanov640708a2012-01-10 15:11:23 -08002229};
2230
Nicolas Pitreb18b6a92017-01-21 00:09:08 -05002231#if defined(CONFIG_CHECKPOINT_RESTORE) && defined(CONFIG_POSIX_TIMERS)
Pavel Emelyanov48f6a7a2013-03-11 13:12:45 +04002232struct timers_private {
2233 struct pid *pid;
2234 struct task_struct *task;
2235 struct sighand_struct *sighand;
Pavel Emelyanov57b80152013-03-11 13:13:08 +04002236 struct pid_namespace *ns;
Pavel Emelyanov48f6a7a2013-03-11 13:12:45 +04002237 unsigned long flags;
2238};
2239
2240static void *timers_start(struct seq_file *m, loff_t *pos)
2241{
2242 struct timers_private *tp = m->private;
2243
2244 tp->task = get_pid_task(tp->pid, PIDTYPE_PID);
2245 if (!tp->task)
2246 return ERR_PTR(-ESRCH);
2247
2248 tp->sighand = lock_task_sighand(tp->task, &tp->flags);
2249 if (!tp->sighand)
2250 return ERR_PTR(-ESRCH);
2251
2252 return seq_list_start(&tp->task->signal->posix_timers, *pos);
2253}
2254
2255static void *timers_next(struct seq_file *m, void *v, loff_t *pos)
2256{
2257 struct timers_private *tp = m->private;
2258 return seq_list_next(v, &tp->task->signal->posix_timers, pos);
2259}
2260
2261static void timers_stop(struct seq_file *m, void *v)
2262{
2263 struct timers_private *tp = m->private;
2264
2265 if (tp->sighand) {
2266 unlock_task_sighand(tp->task, &tp->flags);
2267 tp->sighand = NULL;
2268 }
2269
2270 if (tp->task) {
2271 put_task_struct(tp->task);
2272 tp->task = NULL;
2273 }
2274}
2275
2276static int show_timer(struct seq_file *m, void *v)
2277{
2278 struct k_itimer *timer;
Pavel Emelyanov57b80152013-03-11 13:13:08 +04002279 struct timers_private *tp = m->private;
2280 int notify;
Alexey Dobriyancedbcca2014-08-08 14:21:33 -07002281 static const char * const nstr[] = {
Pavel Emelyanov57b80152013-03-11 13:13:08 +04002282 [SIGEV_SIGNAL] = "signal",
2283 [SIGEV_NONE] = "none",
2284 [SIGEV_THREAD] = "thread",
2285 };
Pavel Emelyanov48f6a7a2013-03-11 13:12:45 +04002286
2287 timer = list_entry((struct list_head *)v, struct k_itimer, list);
Pavel Emelyanov57b80152013-03-11 13:13:08 +04002288 notify = timer->it_sigev_notify;
2289
Pavel Emelyanov48f6a7a2013-03-11 13:12:45 +04002290 seq_printf(m, "ID: %d\n", timer->it_id);
Linus Torvaldsba3edf1f2017-12-06 18:23:27 -08002291 seq_printf(m, "signal: %d/%px\n",
Joe Perches25ce3192015-04-15 16:18:17 -07002292 timer->sigq->info.si_signo,
2293 timer->sigq->info.si_value.sival_ptr);
Pavel Emelyanov57b80152013-03-11 13:13:08 +04002294 seq_printf(m, "notify: %s/%s.%d\n",
Joe Perches25ce3192015-04-15 16:18:17 -07002295 nstr[notify & ~SIGEV_THREAD_ID],
2296 (notify & SIGEV_THREAD_ID) ? "tid" : "pid",
2297 pid_nr_ns(timer->it_pid, tp->ns));
Pavel Tikhomirov15ef0292013-05-17 02:12:03 +04002298 seq_printf(m, "ClockID: %d\n", timer->it_clock);
Pavel Emelyanov48f6a7a2013-03-11 13:12:45 +04002299
2300 return 0;
2301}
2302
2303static const struct seq_operations proc_timers_seq_ops = {
2304 .start = timers_start,
2305 .next = timers_next,
2306 .stop = timers_stop,
2307 .show = show_timer,
2308};
2309
2310static int proc_timers_open(struct inode *inode, struct file *file)
2311{
2312 struct timers_private *tp;
2313
2314 tp = __seq_open_private(file, &proc_timers_seq_ops,
2315 sizeof(struct timers_private));
2316 if (!tp)
2317 return -ENOMEM;
2318
2319 tp->pid = proc_pid(inode);
Christoph Hellwig76f668b2018-05-16 07:19:01 +02002320 tp->ns = proc_pid_ns(inode);
Pavel Emelyanov48f6a7a2013-03-11 13:12:45 +04002321 return 0;
2322}
2323
2324static const struct file_operations proc_timers_operations = {
2325 .open = proc_timers_open,
2326 .read = seq_read,
2327 .llseek = seq_lseek,
2328 .release = seq_release_private,
2329};
Eric Engestromb5946be2016-03-17 14:20:57 -07002330#endif
Pavel Emelyanov640708a2012-01-10 15:11:23 -08002331
John Stultz5de23d42016-03-17 14:20:54 -07002332static ssize_t timerslack_ns_write(struct file *file, const char __user *buf,
2333 size_t count, loff_t *offset)
2334{
2335 struct inode *inode = file_inode(file);
2336 struct task_struct *p;
2337 u64 slack_ns;
2338 int err;
2339
2340 err = kstrtoull_from_user(buf, count, 10, &slack_ns);
2341 if (err < 0)
2342 return err;
2343
2344 p = get_proc_task(inode);
2345 if (!p)
2346 return -ESRCH;
2347
John Stultz4b2bd5f2016-10-07 17:02:33 -07002348 if (p != current) {
Benjamin Gordon8da0b4f2019-01-03 15:25:56 -08002349 rcu_read_lock();
2350 if (!ns_capable(__task_cred(p)->user_ns, CAP_SYS_NICE)) {
2351 rcu_read_unlock();
John Stultz4b2bd5f2016-10-07 17:02:33 -07002352 count = -EPERM;
2353 goto out;
2354 }
Benjamin Gordon8da0b4f2019-01-03 15:25:56 -08002355 rcu_read_unlock();
John Stultz5de23d42016-03-17 14:20:54 -07002356
John Stultz4b2bd5f2016-10-07 17:02:33 -07002357 err = security_task_setscheduler(p);
2358 if (err) {
2359 count = err;
2360 goto out;
2361 }
John Stultz904763e2016-10-07 17:02:29 -07002362 }
2363
John Stultz7abbaf92016-10-07 17:02:26 -07002364 task_lock(p);
2365 if (slack_ns == 0)
2366 p->timer_slack_ns = p->default_timer_slack_ns;
2367 else
2368 p->timer_slack_ns = slack_ns;
2369 task_unlock(p);
2370
2371out:
John Stultz5de23d42016-03-17 14:20:54 -07002372 put_task_struct(p);
2373
2374 return count;
2375}
2376
2377static int timerslack_ns_show(struct seq_file *m, void *v)
2378{
2379 struct inode *inode = m->private;
2380 struct task_struct *p;
John Stultz7abbaf92016-10-07 17:02:26 -07002381 int err = 0;
John Stultz5de23d42016-03-17 14:20:54 -07002382
2383 p = get_proc_task(inode);
2384 if (!p)
2385 return -ESRCH;
2386
John Stultz4b2bd5f2016-10-07 17:02:33 -07002387 if (p != current) {
Benjamin Gordon8da0b4f2019-01-03 15:25:56 -08002388 rcu_read_lock();
2389 if (!ns_capable(__task_cred(p)->user_ns, CAP_SYS_NICE)) {
2390 rcu_read_unlock();
John Stultz4b2bd5f2016-10-07 17:02:33 -07002391 err = -EPERM;
2392 goto out;
2393 }
Benjamin Gordon8da0b4f2019-01-03 15:25:56 -08002394 rcu_read_unlock();
2395
John Stultz4b2bd5f2016-10-07 17:02:33 -07002396 err = security_task_getscheduler(p);
2397 if (err)
2398 goto out;
2399 }
John Stultz904763e2016-10-07 17:02:29 -07002400
John Stultz7abbaf92016-10-07 17:02:26 -07002401 task_lock(p);
2402 seq_printf(m, "%llu\n", p->timer_slack_ns);
2403 task_unlock(p);
2404
2405out:
John Stultz5de23d42016-03-17 14:20:54 -07002406 put_task_struct(p);
2407
2408 return err;
2409}
2410
2411static int timerslack_ns_open(struct inode *inode, struct file *filp)
2412{
2413 return single_open(filp, timerslack_ns_show, inode);
2414}
2415
2416static const struct file_operations proc_pid_set_timerslack_ns_operations = {
2417 .open = timerslack_ns_open,
2418 .read = seq_read,
2419 .write = timerslack_ns_write,
2420 .llseek = seq_lseek,
2421 .release = single_release,
2422};
2423
Al Viro0168b9e2018-05-03 09:21:05 -04002424static struct dentry *proc_pident_instantiate(struct dentry *dentry,
2425 struct task_struct *task, const void *ptr)
Eric W. Biederman444ceed2006-10-02 02:18:49 -07002426{
Eric Dumazetc5141e62007-05-08 00:26:15 -07002427 const struct pid_entry *p = ptr;
Eric W. Biederman444ceed2006-10-02 02:18:49 -07002428 struct inode *inode;
2429 struct proc_inode *ei;
Eric W. Biederman444ceed2006-10-02 02:18:49 -07002430
Al Viro0168b9e2018-05-03 09:21:05 -04002431 inode = proc_pid_make_inode(dentry->d_sb, task, p->mode);
Eric W. Biederman444ceed2006-10-02 02:18:49 -07002432 if (!inode)
Al Viro0168b9e2018-05-03 09:21:05 -04002433 return ERR_PTR(-ENOENT);
Eric W. Biederman444ceed2006-10-02 02:18:49 -07002434
2435 ei = PROC_I(inode);
Eric W. Biederman444ceed2006-10-02 02:18:49 -07002436 if (S_ISDIR(inode->i_mode))
Miklos Szeredibfe86842011-10-28 14:13:29 +02002437 set_nlink(inode, 2); /* Use getattr to fix if necessary */
Eric W. Biederman444ceed2006-10-02 02:18:49 -07002438 if (p->iop)
2439 inode->i_op = p->iop;
2440 if (p->fop)
2441 inode->i_fop = p->fop;
2442 ei->op = p->op;
Al Viro1bbc5512018-05-02 21:26:16 -04002443 pid_update_inode(task, inode);
Nick Pigginfb045ad2011-01-07 17:49:55 +11002444 d_set_d_op(dentry, &pid_dentry_operations);
Al Viro0168b9e2018-05-03 09:21:05 -04002445 return d_splice_alias(inode, dentry);
Eric W. Biederman444ceed2006-10-02 02:18:49 -07002446}
2447
Linus Torvalds1da177e2005-04-16 15:20:36 -07002448static struct dentry *proc_pident_lookup(struct inode *dir,
2449 struct dentry *dentry,
Alexey Dobriyand5a572a2019-03-11 23:28:51 -07002450 const struct pid_entry *p,
2451 const struct pid_entry *end)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002452{
Eric W. Biederman99f89552006-06-26 00:25:55 -07002453 struct task_struct *task = get_proc_task(dir);
Al Viro0168b9e2018-05-03 09:21:05 -04002454 struct dentry *res = ERR_PTR(-ENOENT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002455
Eric W. Biederman99f89552006-06-26 00:25:55 -07002456 if (!task)
2457 goto out_no_task;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002458
Eric W. Biederman20cdc892006-10-02 02:17:07 -07002459 /*
2460 * Yes, it does not scale. And it should not. Don't add
2461 * new entries into /proc/<tgid>/ without very good reasons.
2462 */
Alexey Dobriyand5a572a2019-03-11 23:28:51 -07002463 for (; p < end; p++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002464 if (p->len != dentry->d_name.len)
2465 continue;
Alexey Dobriyan26b95132018-06-14 15:27:17 -07002466 if (!memcmp(dentry->d_name.name, p->name, p->len)) {
2467 res = proc_pident_instantiate(dentry, task, p);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002468 break;
Alexey Dobriyan26b95132018-06-14 15:27:17 -07002469 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002470 }
Eric W. Biederman99f89552006-06-26 00:25:55 -07002471 put_task_struct(task);
2472out_no_task:
Al Viro0168b9e2018-05-03 09:21:05 -04002473 return res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002474}
2475
Al Virof0c3b502013-05-16 12:07:31 -04002476static int proc_pident_readdir(struct file *file, struct dir_context *ctx,
Eric Dumazetc5141e62007-05-08 00:26:15 -07002477 const struct pid_entry *ents, unsigned int nents)
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002478{
Al Virof0c3b502013-05-16 12:07:31 -04002479 struct task_struct *task = get_proc_task(file_inode(file));
2480 const struct pid_entry *p;
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002481
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002482 if (!task)
Al Virof0c3b502013-05-16 12:07:31 -04002483 return -ENOENT;
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002484
Al Virof0c3b502013-05-16 12:07:31 -04002485 if (!dir_emit_dots(file, ctx))
2486 goto out;
2487
2488 if (ctx->pos >= nents + 2)
2489 goto out;
2490
Alexey Dobriyanbac5f5d2016-12-12 16:45:28 -08002491 for (p = ents + (ctx->pos - 2); p < ents + nents; p++) {
Al Virof0c3b502013-05-16 12:07:31 -04002492 if (!proc_fill_cache(file, ctx, p->name, p->len,
2493 proc_pident_instantiate, task, p))
2494 break;
2495 ctx->pos++;
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002496 }
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002497out:
Eric W. Biederman61a28782006-10-02 02:18:49 -07002498 put_task_struct(task);
Al Virof0c3b502013-05-16 12:07:31 -04002499 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002500}
2501
Linus Torvalds1da177e2005-04-16 15:20:36 -07002502#ifdef CONFIG_SECURITY
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002503static ssize_t proc_pid_attr_read(struct file * file, char __user * buf,
2504 size_t count, loff_t *ppos)
2505{
Al Viro496ad9a2013-01-23 17:07:38 -05002506 struct inode * inode = file_inode(file);
Al Viro04ff9702007-03-12 16:17:58 +00002507 char *p = NULL;
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002508 ssize_t length;
2509 struct task_struct *task = get_proc_task(inode);
2510
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002511 if (!task)
Al Viro04ff9702007-03-12 16:17:58 +00002512 return -ESRCH;
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002513
Casey Schaufler6d9c9392018-09-21 17:16:59 -07002514 length = security_getprocattr(task, PROC_I(inode)->op.lsm,
Josef "Jeff" Sipek2fddfee2006-12-08 02:36:36 -08002515 (char*)file->f_path.dentry->d_name.name,
Al Viro04ff9702007-03-12 16:17:58 +00002516 &p);
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002517 put_task_struct(task);
Al Viro04ff9702007-03-12 16:17:58 +00002518 if (length > 0)
2519 length = simple_read_from_buffer(buf, count, ppos, p, length);
2520 kfree(p);
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002521 return length;
2522}
2523
2524static ssize_t proc_pid_attr_write(struct file * file, const char __user * buf,
2525 size_t count, loff_t *ppos)
2526{
Al Viro496ad9a2013-01-23 17:07:38 -05002527 struct inode * inode = file_inode(file);
Alexey Dobriyan41089b62018-08-21 21:54:30 -07002528 struct task_struct *task;
Al Virobb646cd2015-12-24 00:16:30 -05002529 void *page;
Alexey Dobriyan41089b62018-08-21 21:54:30 -07002530 int rv;
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002531
Alexey Dobriyan41089b62018-08-21 21:54:30 -07002532 rcu_read_lock();
2533 task = pid_task(proc_pid(inode), PIDTYPE_PID);
2534 if (!task) {
2535 rcu_read_unlock();
2536 return -ESRCH;
2537 }
Stephen Smalleyb21507e2017-01-09 10:07:31 -05002538 /* A task may only write its own attributes. */
Alexey Dobriyan41089b62018-08-21 21:54:30 -07002539 if (current != task) {
2540 rcu_read_unlock();
2541 return -EACCES;
2542 }
2543 rcu_read_unlock();
Stephen Smalleyb21507e2017-01-09 10:07:31 -05002544
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002545 if (count > PAGE_SIZE)
2546 count = PAGE_SIZE;
2547
2548 /* No partial writes. */
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002549 if (*ppos != 0)
Alexey Dobriyan41089b62018-08-21 21:54:30 -07002550 return -EINVAL;
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002551
Al Virobb646cd2015-12-24 00:16:30 -05002552 page = memdup_user(buf, count);
2553 if (IS_ERR(page)) {
Alexey Dobriyan41089b62018-08-21 21:54:30 -07002554 rv = PTR_ERR(page);
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002555 goto out;
Al Virobb646cd2015-12-24 00:16:30 -05002556 }
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002557
David Howells107db7c2009-05-08 13:55:27 +01002558 /* Guard against adverse ptrace interaction */
Alexey Dobriyan41089b62018-08-21 21:54:30 -07002559 rv = mutex_lock_interruptible(&current->signal->cred_guard_mutex);
2560 if (rv < 0)
David Howells107db7c2009-05-08 13:55:27 +01002561 goto out_free;
2562
Casey Schaufler6d9c9392018-09-21 17:16:59 -07002563 rv = security_setprocattr(PROC_I(inode)->op.lsm,
2564 file->f_path.dentry->d_name.name, page,
2565 count);
Stephen Smalleyb21507e2017-01-09 10:07:31 -05002566 mutex_unlock(&current->signal->cred_guard_mutex);
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002567out_free:
Al Virobb646cd2015-12-24 00:16:30 -05002568 kfree(page);
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002569out:
Alexey Dobriyan41089b62018-08-21 21:54:30 -07002570 return rv;
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002571}
2572
Arjan van de Ven00977a52007-02-12 00:55:34 -08002573static const struct file_operations proc_pid_attr_operations = {
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002574 .read = proc_pid_attr_read,
2575 .write = proc_pid_attr_write,
Arnd Bergmann87df8422010-03-17 23:06:02 +01002576 .llseek = generic_file_llseek,
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002577};
2578
Casey Schaufler6d9c9392018-09-21 17:16:59 -07002579#define LSM_DIR_OPS(LSM) \
2580static int proc_##LSM##_attr_dir_iterate(struct file *filp, \
2581 struct dir_context *ctx) \
2582{ \
2583 return proc_pident_readdir(filp, ctx, \
2584 LSM##_attr_dir_stuff, \
2585 ARRAY_SIZE(LSM##_attr_dir_stuff)); \
2586} \
2587\
2588static const struct file_operations proc_##LSM##_attr_dir_ops = { \
2589 .read = generic_read_dir, \
2590 .iterate = proc_##LSM##_attr_dir_iterate, \
2591 .llseek = default_llseek, \
2592}; \
2593\
2594static struct dentry *proc_##LSM##_attr_dir_lookup(struct inode *dir, \
2595 struct dentry *dentry, unsigned int flags) \
2596{ \
2597 return proc_pident_lookup(dir, dentry, \
2598 LSM##_attr_dir_stuff, \
Alexey Dobriyand5a572a2019-03-11 23:28:51 -07002599 LSM##_attr_dir_stuff + ARRAY_SIZE(LSM##_attr_dir_stuff)); \
Casey Schaufler6d9c9392018-09-21 17:16:59 -07002600} \
2601\
2602static const struct inode_operations proc_##LSM##_attr_dir_inode_ops = { \
2603 .lookup = proc_##LSM##_attr_dir_lookup, \
2604 .getattr = pid_getattr, \
2605 .setattr = proc_setattr, \
2606}
2607
2608#ifdef CONFIG_SECURITY_SMACK
2609static const struct pid_entry smack_attr_dir_stuff[] = {
2610 ATTR("smack", "current", 0666),
2611};
2612LSM_DIR_OPS(smack);
2613#endif
2614
Eric Dumazetc5141e62007-05-08 00:26:15 -07002615static const struct pid_entry attr_dir_stuff[] = {
Casey Schaufler6d9c9392018-09-21 17:16:59 -07002616 ATTR(NULL, "current", 0666),
2617 ATTR(NULL, "prev", 0444),
2618 ATTR(NULL, "exec", 0666),
2619 ATTR(NULL, "fscreate", 0666),
2620 ATTR(NULL, "keycreate", 0666),
2621 ATTR(NULL, "sockcreate", 0666),
2622#ifdef CONFIG_SECURITY_SMACK
2623 DIR("smack", 0555,
2624 proc_smack_attr_dir_inode_ops, proc_smack_attr_dir_ops),
2625#endif
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002626};
2627
Al Virof0c3b502013-05-16 12:07:31 -04002628static int proc_attr_dir_readdir(struct file *file, struct dir_context *ctx)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002629{
Al Virof0c3b502013-05-16 12:07:31 -04002630 return proc_pident_readdir(file, ctx,
2631 attr_dir_stuff, ARRAY_SIZE(attr_dir_stuff));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002632}
2633
Arjan van de Ven00977a52007-02-12 00:55:34 -08002634static const struct file_operations proc_attr_dir_operations = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002635 .read = generic_read_dir,
Al Virof50752e2016-04-20 17:13:54 -04002636 .iterate_shared = proc_attr_dir_readdir,
2637 .llseek = generic_file_llseek,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002638};
2639
Eric W. Biederman72d9dcf2006-10-02 02:18:50 -07002640static struct dentry *proc_attr_dir_lookup(struct inode *dir,
Al Viro00cd8dd2012-06-10 17:13:09 -04002641 struct dentry *dentry, unsigned int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002642{
Eric W. Biederman7bcd6b02006-10-02 02:18:56 -07002643 return proc_pident_lookup(dir, dentry,
Alexey Dobriyand5a572a2019-03-11 23:28:51 -07002644 attr_dir_stuff,
2645 attr_dir_stuff + ARRAY_SIZE(attr_dir_stuff));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002646}
2647
Arjan van de Venc5ef1c42007-02-12 00:55:40 -08002648static const struct inode_operations proc_attr_dir_inode_operations = {
Eric W. Biederman72d9dcf2006-10-02 02:18:50 -07002649 .lookup = proc_attr_dir_lookup,
Eric W. Biederman99f89552006-06-26 00:25:55 -07002650 .getattr = pid_getattr,
Linus Torvalds6d76fa52006-07-15 12:26:45 -07002651 .setattr = proc_setattr,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002652};
2653
Linus Torvalds1da177e2005-04-16 15:20:36 -07002654#endif
2655
Christoph Hellwig698ba7b2009-12-15 16:47:37 -08002656#ifdef CONFIG_ELF_CORE
Kawai, Hidehiro3cb4a0b2007-07-19 01:48:28 -07002657static ssize_t proc_coredump_filter_read(struct file *file, char __user *buf,
2658 size_t count, loff_t *ppos)
2659{
Al Viro496ad9a2013-01-23 17:07:38 -05002660 struct task_struct *task = get_proc_task(file_inode(file));
Kawai, Hidehiro3cb4a0b2007-07-19 01:48:28 -07002661 struct mm_struct *mm;
2662 char buffer[PROC_NUMBUF];
2663 size_t len;
2664 int ret;
2665
2666 if (!task)
2667 return -ESRCH;
2668
2669 ret = 0;
2670 mm = get_task_mm(task);
2671 if (mm) {
2672 len = snprintf(buffer, sizeof(buffer), "%08lx\n",
2673 ((mm->flags & MMF_DUMP_FILTER_MASK) >>
2674 MMF_DUMP_FILTER_SHIFT));
2675 mmput(mm);
2676 ret = simple_read_from_buffer(buf, count, ppos, buffer, len);
2677 }
2678
2679 put_task_struct(task);
2680
2681 return ret;
2682}
2683
2684static ssize_t proc_coredump_filter_write(struct file *file,
2685 const char __user *buf,
2686 size_t count,
2687 loff_t *ppos)
2688{
2689 struct task_struct *task;
2690 struct mm_struct *mm;
Kawai, Hidehiro3cb4a0b2007-07-19 01:48:28 -07002691 unsigned int val;
2692 int ret;
2693 int i;
2694 unsigned long mask;
2695
Alexey Dobriyan774636e2015-09-09 15:36:59 -07002696 ret = kstrtouint_from_user(buf, count, 0, &val);
2697 if (ret < 0)
2698 return ret;
Kawai, Hidehiro3cb4a0b2007-07-19 01:48:28 -07002699
2700 ret = -ESRCH;
Al Viro496ad9a2013-01-23 17:07:38 -05002701 task = get_proc_task(file_inode(file));
Kawai, Hidehiro3cb4a0b2007-07-19 01:48:28 -07002702 if (!task)
2703 goto out_no_task;
2704
Kawai, Hidehiro3cb4a0b2007-07-19 01:48:28 -07002705 mm = get_task_mm(task);
2706 if (!mm)
2707 goto out_no_mm;
Colin Ian King41a0c2492015-12-18 14:22:01 -08002708 ret = 0;
Kawai, Hidehiro3cb4a0b2007-07-19 01:48:28 -07002709
2710 for (i = 0, mask = 1; i < MMF_DUMP_FILTER_BITS; i++, mask <<= 1) {
2711 if (val & mask)
2712 set_bit(i + MMF_DUMP_FILTER_SHIFT, &mm->flags);
2713 else
2714 clear_bit(i + MMF_DUMP_FILTER_SHIFT, &mm->flags);
2715 }
2716
2717 mmput(mm);
2718 out_no_mm:
2719 put_task_struct(task);
2720 out_no_task:
Alexey Dobriyan774636e2015-09-09 15:36:59 -07002721 if (ret < 0)
2722 return ret;
2723 return count;
Kawai, Hidehiro3cb4a0b2007-07-19 01:48:28 -07002724}
2725
2726static const struct file_operations proc_coredump_filter_operations = {
2727 .read = proc_coredump_filter_read,
2728 .write = proc_coredump_filter_write,
Arnd Bergmann87df8422010-03-17 23:06:02 +01002729 .llseek = generic_file_llseek,
Kawai, Hidehiro3cb4a0b2007-07-19 01:48:28 -07002730};
2731#endif
2732
Andrew Mortonaba76fd2006-12-10 02:19:48 -08002733#ifdef CONFIG_TASK_IO_ACCOUNTING
Alexey Dobriyan19aadc92014-08-08 14:21:50 -07002734static int do_io_accounting(struct task_struct *task, struct seq_file *m, int whole)
Andrew Mortonaba76fd2006-12-10 02:19:48 -08002735{
Andrea Righi940389b2008-07-28 00:48:12 +02002736 struct task_io_accounting acct = task->ioac;
Andrea Righi59954772008-07-27 17:29:15 +02002737 unsigned long flags;
Vasiliy Kulikov293eb1e2011-07-26 16:08:38 -07002738 int result;
Andrea Righi297c5d92008-07-25 01:48:49 -07002739
Vasiliy Kulikov293eb1e2011-07-26 16:08:38 -07002740 result = mutex_lock_killable(&task->signal->cred_guard_mutex);
2741 if (result)
2742 return result;
2743
Jann Horncaaee622016-01-20 15:00:04 -08002744 if (!ptrace_may_access(task, PTRACE_MODE_READ_FSCREDS)) {
Vasiliy Kulikov293eb1e2011-07-26 16:08:38 -07002745 result = -EACCES;
2746 goto out_unlock;
2747 }
Vasiliy Kulikov1d1221f2011-06-24 16:08:38 +04002748
Andrea Righi59954772008-07-27 17:29:15 +02002749 if (whole && lock_task_sighand(task, &flags)) {
2750 struct task_struct *t = task;
Andrea Righib2d002d2008-07-26 15:22:27 -07002751
Andrea Righi59954772008-07-27 17:29:15 +02002752 task_io_accounting_add(&acct, &task->signal->ioac);
2753 while_each_thread(task, t)
2754 task_io_accounting_add(&acct, &t->ioac);
Andrea Righi297c5d92008-07-25 01:48:49 -07002755
Andrea Righi59954772008-07-27 17:29:15 +02002756 unlock_task_sighand(task, &flags);
Andrea Righi297c5d92008-07-25 01:48:49 -07002757 }
Joe Perches25ce3192015-04-15 16:18:17 -07002758 seq_printf(m,
2759 "rchar: %llu\n"
2760 "wchar: %llu\n"
2761 "syscr: %llu\n"
2762 "syscw: %llu\n"
2763 "read_bytes: %llu\n"
2764 "write_bytes: %llu\n"
2765 "cancelled_write_bytes: %llu\n",
2766 (unsigned long long)acct.rchar,
2767 (unsigned long long)acct.wchar,
2768 (unsigned long long)acct.syscr,
2769 (unsigned long long)acct.syscw,
2770 (unsigned long long)acct.read_bytes,
2771 (unsigned long long)acct.write_bytes,
2772 (unsigned long long)acct.cancelled_write_bytes);
2773 result = 0;
2774
Vasiliy Kulikov293eb1e2011-07-26 16:08:38 -07002775out_unlock:
2776 mutex_unlock(&task->signal->cred_guard_mutex);
2777 return result;
Andrew Mortonaba76fd2006-12-10 02:19:48 -08002778}
Andrea Righi297c5d92008-07-25 01:48:49 -07002779
Alexey Dobriyan19aadc92014-08-08 14:21:50 -07002780static int proc_tid_io_accounting(struct seq_file *m, struct pid_namespace *ns,
2781 struct pid *pid, struct task_struct *task)
Andrea Righi297c5d92008-07-25 01:48:49 -07002782{
Alexey Dobriyan19aadc92014-08-08 14:21:50 -07002783 return do_io_accounting(task, m, 0);
Andrea Righi297c5d92008-07-25 01:48:49 -07002784}
2785
Alexey Dobriyan19aadc92014-08-08 14:21:50 -07002786static int proc_tgid_io_accounting(struct seq_file *m, struct pid_namespace *ns,
2787 struct pid *pid, struct task_struct *task)
Andrea Righi297c5d92008-07-25 01:48:49 -07002788{
Alexey Dobriyan19aadc92014-08-08 14:21:50 -07002789 return do_io_accounting(task, m, 1);
Andrea Righi297c5d92008-07-25 01:48:49 -07002790}
2791#endif /* CONFIG_TASK_IO_ACCOUNTING */
Andrew Mortonaba76fd2006-12-10 02:19:48 -08002792
Eric W. Biederman22d917d2011-11-17 00:11:58 -08002793#ifdef CONFIG_USER_NS
2794static int proc_id_map_open(struct inode *inode, struct file *file,
Fabian Frederickccf94f12014-08-08 14:21:22 -07002795 const struct seq_operations *seq_ops)
Eric W. Biederman22d917d2011-11-17 00:11:58 -08002796{
2797 struct user_namespace *ns = NULL;
2798 struct task_struct *task;
2799 struct seq_file *seq;
2800 int ret = -EINVAL;
2801
2802 task = get_proc_task(inode);
2803 if (task) {
2804 rcu_read_lock();
2805 ns = get_user_ns(task_cred_xxx(task, user_ns));
2806 rcu_read_unlock();
2807 put_task_struct(task);
2808 }
2809 if (!ns)
2810 goto err;
2811
2812 ret = seq_open(file, seq_ops);
2813 if (ret)
2814 goto err_put_ns;
2815
2816 seq = file->private_data;
2817 seq->private = ns;
2818
2819 return 0;
2820err_put_ns:
2821 put_user_ns(ns);
2822err:
2823 return ret;
2824}
2825
2826static int proc_id_map_release(struct inode *inode, struct file *file)
2827{
2828 struct seq_file *seq = file->private_data;
2829 struct user_namespace *ns = seq->private;
2830 put_user_ns(ns);
2831 return seq_release(inode, file);
2832}
2833
2834static int proc_uid_map_open(struct inode *inode, struct file *file)
2835{
2836 return proc_id_map_open(inode, file, &proc_uid_seq_operations);
2837}
2838
2839static int proc_gid_map_open(struct inode *inode, struct file *file)
2840{
2841 return proc_id_map_open(inode, file, &proc_gid_seq_operations);
2842}
2843
Eric W. Biedermanf76d2072012-08-30 01:24:05 -07002844static int proc_projid_map_open(struct inode *inode, struct file *file)
2845{
2846 return proc_id_map_open(inode, file, &proc_projid_seq_operations);
2847}
2848
Eric W. Biederman22d917d2011-11-17 00:11:58 -08002849static const struct file_operations proc_uid_map_operations = {
2850 .open = proc_uid_map_open,
2851 .write = proc_uid_map_write,
2852 .read = seq_read,
2853 .llseek = seq_lseek,
2854 .release = proc_id_map_release,
2855};
2856
2857static const struct file_operations proc_gid_map_operations = {
2858 .open = proc_gid_map_open,
2859 .write = proc_gid_map_write,
2860 .read = seq_read,
2861 .llseek = seq_lseek,
2862 .release = proc_id_map_release,
2863};
Eric W. Biedermanf76d2072012-08-30 01:24:05 -07002864
2865static const struct file_operations proc_projid_map_operations = {
2866 .open = proc_projid_map_open,
2867 .write = proc_projid_map_write,
2868 .read = seq_read,
2869 .llseek = seq_lseek,
2870 .release = proc_id_map_release,
2871};
Eric W. Biederman9cc46512014-12-02 12:27:26 -06002872
2873static int proc_setgroups_open(struct inode *inode, struct file *file)
2874{
2875 struct user_namespace *ns = NULL;
2876 struct task_struct *task;
2877 int ret;
2878
2879 ret = -ESRCH;
2880 task = get_proc_task(inode);
2881 if (task) {
2882 rcu_read_lock();
2883 ns = get_user_ns(task_cred_xxx(task, user_ns));
2884 rcu_read_unlock();
2885 put_task_struct(task);
2886 }
2887 if (!ns)
2888 goto err;
2889
2890 if (file->f_mode & FMODE_WRITE) {
2891 ret = -EACCES;
2892 if (!ns_capable(ns, CAP_SYS_ADMIN))
2893 goto err_put_ns;
2894 }
2895
2896 ret = single_open(file, &proc_setgroups_show, ns);
2897 if (ret)
2898 goto err_put_ns;
2899
2900 return 0;
2901err_put_ns:
2902 put_user_ns(ns);
2903err:
2904 return ret;
2905}
2906
2907static int proc_setgroups_release(struct inode *inode, struct file *file)
2908{
2909 struct seq_file *seq = file->private_data;
2910 struct user_namespace *ns = seq->private;
2911 int ret = single_release(inode, file);
2912 put_user_ns(ns);
2913 return ret;
2914}
2915
2916static const struct file_operations proc_setgroups_operations = {
2917 .open = proc_setgroups_open,
2918 .write = proc_setgroups_write,
2919 .read = seq_read,
2920 .llseek = seq_lseek,
2921 .release = proc_setgroups_release,
2922};
Eric W. Biederman22d917d2011-11-17 00:11:58 -08002923#endif /* CONFIG_USER_NS */
2924
Kees Cook47830722008-10-06 03:11:58 +04002925static int proc_pid_personality(struct seq_file *m, struct pid_namespace *ns,
2926 struct pid *pid, struct task_struct *task)
2927{
Al Viroa9712bc2011-03-23 15:52:50 -04002928 int err = lock_trace(task);
2929 if (!err) {
2930 seq_printf(m, "%08x\n", task->personality);
2931 unlock_trace(task);
2932 }
2933 return err;
Kees Cook47830722008-10-06 03:11:58 +04002934}
2935
Josh Poimboeuf7c23b332017-02-13 19:42:41 -06002936#ifdef CONFIG_LIVEPATCH
2937static int proc_pid_patch_state(struct seq_file *m, struct pid_namespace *ns,
2938 struct pid *pid, struct task_struct *task)
2939{
2940 seq_printf(m, "%d\n", task->patch_state);
2941 return 0;
2942}
2943#endif /* CONFIG_LIVEPATCH */
2944
Alexander Popovc8d12622018-08-17 01:17:01 +03002945#ifdef CONFIG_STACKLEAK_METRICS
2946static int proc_stack_depth(struct seq_file *m, struct pid_namespace *ns,
2947 struct pid *pid, struct task_struct *task)
2948{
2949 unsigned long prev_depth = THREAD_SIZE -
2950 (task->prev_lowest_stack & (THREAD_SIZE - 1));
2951 unsigned long depth = THREAD_SIZE -
2952 (task->lowest_stack & (THREAD_SIZE - 1));
2953
2954 seq_printf(m, "previous stack depth: %lu\nstack depth: %lu\n",
2955 prev_depth, depth);
2956 return 0;
2957}
2958#endif /* CONFIG_STACKLEAK_METRICS */
2959
Eric W. Biederman801199c2006-10-02 02:18:48 -07002960/*
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002961 * Thread groups
2962 */
Arjan van de Ven00977a52007-02-12 00:55:34 -08002963static const struct file_operations proc_task_operations;
Arjan van de Venc5ef1c42007-02-12 00:55:40 -08002964static const struct inode_operations proc_task_inode_operations;
Eric W. Biederman20cdc892006-10-02 02:17:07 -07002965
Eric Dumazetc5141e62007-05-08 00:26:15 -07002966static const struct pid_entry tgid_base_stuff[] = {
Alexey Dobriyan631f9c12008-11-10 01:32:52 +03002967 DIR("task", S_IRUGO|S_IXUGO, proc_task_inode_operations, proc_task_operations),
2968 DIR("fd", S_IRUSR|S_IXUSR, proc_fd_inode_operations, proc_fd_operations),
Pavel Emelyanov640708a2012-01-10 15:11:23 -08002969 DIR("map_files", S_IRUSR|S_IXUSR, proc_map_files_inode_operations, proc_map_files_operations),
Alexey Dobriyan631f9c12008-11-10 01:32:52 +03002970 DIR("fdinfo", S_IRUSR|S_IXUSR, proc_fdinfo_inode_operations, proc_fdinfo_operations),
Eric W. Biederman6b4e3062010-03-07 16:41:34 -08002971 DIR("ns", S_IRUSR|S_IXUGO, proc_ns_dir_inode_operations, proc_ns_dir_operations),
Andrew Mortonb2211a32008-03-11 18:03:35 -07002972#ifdef CONFIG_NET
Alexey Dobriyan631f9c12008-11-10 01:32:52 +03002973 DIR("net", S_IRUGO|S_IXUGO, proc_net_inode_operations, proc_net_operations),
Andrew Mortonb2211a32008-03-11 18:03:35 -07002974#endif
Alexey Dobriyan631f9c12008-11-10 01:32:52 +03002975 REG("environ", S_IRUSR, proc_environ_operations),
Al Viroc5317162016-10-05 18:43:43 -04002976 REG("auxv", S_IRUSR, proc_auxv_operations),
Alexey Dobriyan631f9c12008-11-10 01:32:52 +03002977 ONE("status", S_IRUGO, proc_pid_status),
Djalal Harouni35a35042014-04-07 15:38:36 -07002978 ONE("personality", S_IRUSR, proc_pid_personality),
Alexey Dobriyan1c963eb2014-08-08 14:21:37 -07002979 ONE("limits", S_IRUGO, proc_pid_limits),
Ingo Molnar43ae34c2007-07-09 18:52:00 +02002980#ifdef CONFIG_SCHED_DEBUG
Alexey Dobriyan631f9c12008-11-10 01:32:52 +03002981 REG("sched", S_IRUGO|S_IWUSR, proc_pid_sched_operations),
Ingo Molnar43ae34c2007-07-09 18:52:00 +02002982#endif
Mike Galbraith5091faa2010-11-30 14:18:03 +01002983#ifdef CONFIG_SCHED_AUTOGROUP
2984 REG("autogroup", S_IRUGO|S_IWUSR, proc_pid_sched_autogroup_operations),
2985#endif
john stultz4614a696b2009-12-14 18:00:05 -08002986 REG("comm", S_IRUGO|S_IWUSR, proc_pid_set_comm_operations),
Roland McGrathebcb6732008-07-25 19:46:00 -07002987#ifdef CONFIG_HAVE_ARCH_TRACEHOOK
Alexey Dobriyan09d93bd2014-08-08 14:21:39 -07002988 ONE("syscall", S_IRUSR, proc_pid_syscall),
Roland McGrathebcb6732008-07-25 19:46:00 -07002989#endif
Alexey Dobriyanc2c0bb42015-06-25 15:00:54 -07002990 REG("cmdline", S_IRUGO, proc_pid_cmdline_ops),
Alexey Dobriyan631f9c12008-11-10 01:32:52 +03002991 ONE("stat", S_IRUGO, proc_tgid_stat),
2992 ONE("statm", S_IRUGO, proc_pid_statm),
Siddhesh Poyarekarb7643752012-03-21 16:34:04 -07002993 REG("maps", S_IRUGO, proc_pid_maps_operations),
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002994#ifdef CONFIG_NUMA
Siddhesh Poyarekarb7643752012-03-21 16:34:04 -07002995 REG("numa_maps", S_IRUGO, proc_pid_numa_maps_operations),
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002996#endif
Alexey Dobriyan631f9c12008-11-10 01:32:52 +03002997 REG("mem", S_IRUSR|S_IWUSR, proc_mem_operations),
2998 LNK("cwd", proc_cwd_link),
2999 LNK("root", proc_root_link),
3000 LNK("exe", proc_exe_link),
3001 REG("mounts", S_IRUGO, proc_mounts_operations),
3002 REG("mountinfo", S_IRUGO, proc_mountinfo_operations),
3003 REG("mountstats", S_IRUSR, proc_mountstats_operations),
Matt Mackall1e883282008-02-04 22:29:07 -08003004#ifdef CONFIG_PROC_PAGE_MONITOR
Alexey Dobriyan631f9c12008-11-10 01:32:52 +03003005 REG("clear_refs", S_IWUSR, proc_clear_refs_operations),
Siddhesh Poyarekarb7643752012-03-21 16:34:04 -07003006 REG("smaps", S_IRUGO, proc_pid_smaps_operations),
Daniel Colascione493b0e92017-09-06 16:25:08 -07003007 REG("smaps_rollup", S_IRUGO, proc_pid_smaps_rollup_operations),
Djalal Harouni32ed74a2014-04-07 15:38:38 -07003008 REG("pagemap", S_IRUSR, proc_pagemap_operations),
Eric W. Biederman28a6d672006-10-02 02:17:05 -07003009#endif
3010#ifdef CONFIG_SECURITY
Alexey Dobriyan631f9c12008-11-10 01:32:52 +03003011 DIR("attr", S_IRUGO|S_IXUGO, proc_attr_dir_inode_operations, proc_attr_dir_operations),
Eric W. Biederman28a6d672006-10-02 02:17:05 -07003012#endif
3013#ifdef CONFIG_KALLSYMS
Alexey Dobriyanedfcd602014-08-08 14:21:44 -07003014 ONE("wchan", S_IRUGO, proc_pid_wchan),
Eric W. Biederman28a6d672006-10-02 02:17:05 -07003015#endif
Ken Chen2ec220e2008-11-10 11:26:08 +03003016#ifdef CONFIG_STACKTRACE
Djalal Harouni35a35042014-04-07 15:38:36 -07003017 ONE("stack", S_IRUSR, proc_pid_stack),
Eric W. Biederman28a6d672006-10-02 02:17:05 -07003018#endif
Naveen N. Rao5968cec2015-06-30 14:36:03 +05303019#ifdef CONFIG_SCHED_INFO
Alexey Dobriyanf6e826c2014-08-08 14:21:46 -07003020 ONE("schedstat", S_IRUGO, proc_pid_schedstat),
Eric W. Biederman28a6d672006-10-02 02:17:05 -07003021#endif
Arjan van de Ven97455122008-01-25 21:08:34 +01003022#ifdef CONFIG_LATENCYTOP
Alexey Dobriyan631f9c12008-11-10 01:32:52 +03003023 REG("latency", S_IRUGO, proc_lstats_operations),
Arjan van de Ven97455122008-01-25 21:08:34 +01003024#endif
Paul Menage8793d852007-10-18 23:39:39 -07003025#ifdef CONFIG_PROC_PID_CPUSET
Zefan Li52de4772014-09-18 16:03:36 +08003026 ONE("cpuset", S_IRUGO, proc_cpuset_show),
Eric W. Biederman28a6d672006-10-02 02:17:05 -07003027#endif
Paul Menagea4243162007-10-18 23:39:35 -07003028#ifdef CONFIG_CGROUPS
Zefan Li006f4ac2014-09-18 16:03:15 +08003029 ONE("cgroup", S_IRUGO, proc_cgroup_show),
Paul Menagea4243162007-10-18 23:39:35 -07003030#endif
Alexey Dobriyan6ba51e32014-08-08 14:21:48 -07003031 ONE("oom_score", S_IRUGO, proc_oom_score),
David Rientjesfa0cbbf2012-11-12 17:53:04 -08003032 REG("oom_adj", S_IRUGO|S_IWUSR, proc_oom_adj_operations),
David Rientjesa63d83f2010-08-09 17:19:46 -07003033 REG("oom_score_adj", S_IRUGO|S_IWUSR, proc_oom_score_adj_operations),
Richard Guy Briggs4b7d2482019-01-22 17:06:39 -05003034#ifdef CONFIG_AUDIT
Alexey Dobriyan631f9c12008-11-10 01:32:52 +03003035 REG("loginuid", S_IWUSR|S_IRUGO, proc_loginuid_operations),
3036 REG("sessionid", S_IRUGO, proc_sessionid_operations),
Eric W. Biederman28a6d672006-10-02 02:17:05 -07003037#endif
Akinobu Mitaf4f154f2006-12-08 02:39:47 -08003038#ifdef CONFIG_FAULT_INJECTION
Alexey Dobriyan631f9c12008-11-10 01:32:52 +03003039 REG("make-it-fail", S_IRUGO|S_IWUSR, proc_fault_inject_operations),
Akinobu Mita168c42b2017-07-14 14:50:00 -07003040 REG("fail-nth", 0644, proc_fail_nth_operations),
Akinobu Mitaf4f154f2006-12-08 02:39:47 -08003041#endif
Christoph Hellwig698ba7b2009-12-15 16:47:37 -08003042#ifdef CONFIG_ELF_CORE
Alexey Dobriyan631f9c12008-11-10 01:32:52 +03003043 REG("coredump_filter", S_IRUGO|S_IWUSR, proc_coredump_filter_operations),
Kawai, Hidehiro3cb4a0b2007-07-19 01:48:28 -07003044#endif
Andrew Mortonaba76fd2006-12-10 02:19:48 -08003045#ifdef CONFIG_TASK_IO_ACCOUNTING
Alexey Dobriyan19aadc92014-08-08 14:21:50 -07003046 ONE("io", S_IRUSR, proc_tgid_io_accounting),
Andrew Mortonaba76fd2006-12-10 02:19:48 -08003047#endif
Eric W. Biederman22d917d2011-11-17 00:11:58 -08003048#ifdef CONFIG_USER_NS
3049 REG("uid_map", S_IRUGO|S_IWUSR, proc_uid_map_operations),
3050 REG("gid_map", S_IRUGO|S_IWUSR, proc_gid_map_operations),
Eric W. Biedermanf76d2072012-08-30 01:24:05 -07003051 REG("projid_map", S_IRUGO|S_IWUSR, proc_projid_map_operations),
Eric W. Biederman9cc46512014-12-02 12:27:26 -06003052 REG("setgroups", S_IRUGO|S_IWUSR, proc_setgroups_operations),
Eric W. Biederman22d917d2011-11-17 00:11:58 -08003053#endif
Nicolas Pitreb18b6a92017-01-21 00:09:08 -05003054#if defined(CONFIG_CHECKPOINT_RESTORE) && defined(CONFIG_POSIX_TIMERS)
Pavel Emelyanov48f6a7a2013-03-11 13:12:45 +04003055 REG("timers", S_IRUGO, proc_timers_operations),
3056#endif
John Stultz5de23d42016-03-17 14:20:54 -07003057 REG("timerslack_ns", S_IRUGO|S_IWUGO, proc_pid_set_timerslack_ns_operations),
Josh Poimboeuf7c23b332017-02-13 19:42:41 -06003058#ifdef CONFIG_LIVEPATCH
3059 ONE("patch_state", S_IRUSR, proc_pid_patch_state),
3060#endif
Alexander Popovc8d12622018-08-17 01:17:01 +03003061#ifdef CONFIG_STACKLEAK_METRICS
3062 ONE("stack_depth", S_IRUGO, proc_stack_depth),
3063#endif
Eric W. Biederman28a6d672006-10-02 02:17:05 -07003064};
3065
Al Virof0c3b502013-05-16 12:07:31 -04003066static int proc_tgid_base_readdir(struct file *file, struct dir_context *ctx)
Eric W. Biederman28a6d672006-10-02 02:17:05 -07003067{
Al Virof0c3b502013-05-16 12:07:31 -04003068 return proc_pident_readdir(file, ctx,
3069 tgid_base_stuff, ARRAY_SIZE(tgid_base_stuff));
Eric W. Biederman28a6d672006-10-02 02:17:05 -07003070}
3071
Arjan van de Ven00977a52007-02-12 00:55:34 -08003072static const struct file_operations proc_tgid_base_operations = {
Eric W. Biederman28a6d672006-10-02 02:17:05 -07003073 .read = generic_read_dir,
Al Virof50752e2016-04-20 17:13:54 -04003074 .iterate_shared = proc_tgid_base_readdir,
3075 .llseek = generic_file_llseek,
Eric W. Biederman28a6d672006-10-02 02:17:05 -07003076};
3077
Christian Brauner3eb39f42018-11-19 00:51:56 +01003078struct pid *tgid_pidfd_to_pid(const struct file *file)
3079{
3080 if (!d_is_dir(file->f_path.dentry) ||
3081 (file->f_op != &proc_tgid_base_operations))
3082 return ERR_PTR(-EBADF);
3083
3084 return proc_pid(file_inode(file));
3085}
3086
Al Viro00cd8dd2012-06-10 17:13:09 -04003087static struct dentry *proc_tgid_base_lookup(struct inode *dir, struct dentry *dentry, unsigned int flags)
3088{
Eric W. Biederman7bcd6b02006-10-02 02:18:56 -07003089 return proc_pident_lookup(dir, dentry,
Alexey Dobriyand5a572a2019-03-11 23:28:51 -07003090 tgid_base_stuff,
3091 tgid_base_stuff + ARRAY_SIZE(tgid_base_stuff));
Eric W. Biederman28a6d672006-10-02 02:17:05 -07003092}
3093
Arjan van de Venc5ef1c42007-02-12 00:55:40 -08003094static const struct inode_operations proc_tgid_base_inode_operations = {
Eric W. Biederman28a6d672006-10-02 02:17:05 -07003095 .lookup = proc_tgid_base_lookup,
3096 .getattr = pid_getattr,
3097 .setattr = proc_setattr,
Vasiliy Kulikov04996802012-01-10 15:11:31 -08003098 .permission = proc_pid_permission,
Eric W. Biederman28a6d672006-10-02 02:17:05 -07003099};
3100
Pavel Emelyanov60347f62007-10-18 23:40:03 -07003101static void proc_flush_task_mnt(struct vfsmount *mnt, pid_t pid, pid_t tgid)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003102{
Eric W. Biederman48e64842006-06-26 00:25:48 -07003103 struct dentry *dentry, *leader, *dir;
Alexey Dobriyane3912ac2018-02-06 15:36:51 -08003104 char buf[10 + 1];
Eric W. Biederman48e64842006-06-26 00:25:48 -07003105 struct qstr name;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003106
Eric W. Biederman48e64842006-06-26 00:25:48 -07003107 name.name = buf;
Alexey Dobriyane3912ac2018-02-06 15:36:51 -08003108 name.len = snprintf(buf, sizeof(buf), "%u", pid);
Al Viro4f522a22013-02-11 23:20:37 -05003109 /* no ->d_hash() rejects on procfs */
Pavel Emelyanov60347f62007-10-18 23:40:03 -07003110 dentry = d_hash_and_lookup(mnt->mnt_root, &name);
Eric W. Biederman48e64842006-06-26 00:25:48 -07003111 if (dentry) {
Eric W. Biedermanbbd51922014-02-13 10:24:23 -08003112 d_invalidate(dentry);
Eric W. Biederman48e64842006-06-26 00:25:48 -07003113 dput(dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003114 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003115
Oleg Nesterovc35a7f12014-12-10 15:54:56 -08003116 if (pid == tgid)
3117 return;
3118
Eric W. Biederman48e64842006-06-26 00:25:48 -07003119 name.name = buf;
Alexey Dobriyane3912ac2018-02-06 15:36:51 -08003120 name.len = snprintf(buf, sizeof(buf), "%u", tgid);
Pavel Emelyanov60347f62007-10-18 23:40:03 -07003121 leader = d_hash_and_lookup(mnt->mnt_root, &name);
Eric W. Biederman48e64842006-06-26 00:25:48 -07003122 if (!leader)
3123 goto out;
3124
3125 name.name = "task";
3126 name.len = strlen(name.name);
3127 dir = d_hash_and_lookup(leader, &name);
3128 if (!dir)
3129 goto out_put_leader;
3130
3131 name.name = buf;
Alexey Dobriyane3912ac2018-02-06 15:36:51 -08003132 name.len = snprintf(buf, sizeof(buf), "%u", pid);
Eric W. Biederman48e64842006-06-26 00:25:48 -07003133 dentry = d_hash_and_lookup(dir, &name);
3134 if (dentry) {
Eric W. Biedermanbbd51922014-02-13 10:24:23 -08003135 d_invalidate(dentry);
Eric W. Biederman48e64842006-06-26 00:25:48 -07003136 dput(dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003137 }
Eric W. Biederman48e64842006-06-26 00:25:48 -07003138
3139 dput(dir);
3140out_put_leader:
3141 dput(leader);
3142out:
3143 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003144}
3145
Randy Dunlap0895e912007-10-21 21:00:10 -07003146/**
3147 * proc_flush_task - Remove dcache entries for @task from the /proc dcache.
3148 * @task: task that should be flushed.
3149 *
3150 * When flushing dentries from proc, one needs to flush them from global
Pavel Emelyanov60347f62007-10-18 23:40:03 -07003151 * proc (proc_mnt) and from all the namespaces' procs this task was seen
Randy Dunlap0895e912007-10-21 21:00:10 -07003152 * in. This call is supposed to do all of this job.
3153 *
3154 * Looks in the dcache for
3155 * /proc/@pid
3156 * /proc/@tgid/task/@pid
3157 * if either directory is present flushes it and all of it'ts children
3158 * from the dcache.
3159 *
3160 * It is safe and reasonable to cache /proc entries for a task until
3161 * that task exits. After that they just clog up the dcache with
3162 * useless entries, possibly causing useful dcache entries to be
3163 * flushed instead. This routine is proved to flush those useless
3164 * dcache entries at process exit time.
3165 *
3166 * NOTE: This routine is just an optimization so it does not guarantee
3167 * that no dcache entries will exist at process exit time it
3168 * just makes it very unlikely that any will persist.
Pavel Emelyanov60347f62007-10-18 23:40:03 -07003169 */
3170
3171void proc_flush_task(struct task_struct *task)
3172{
Eric W. Biederman9fcc2d12007-11-14 17:00:07 -08003173 int i;
Oleg Nesterov9b4d1cbe2009-09-22 16:45:34 -07003174 struct pid *pid, *tgid;
Pavel Emelyanov130f77e2007-10-18 23:40:11 -07003175 struct upid *upid;
3176
Pavel Emelyanov130f77e2007-10-18 23:40:11 -07003177 pid = task_pid(task);
Oleg Nesterov9b4d1cbe2009-09-22 16:45:34 -07003178 tgid = task_tgid(task);
Pavel Emelyanov130f77e2007-10-18 23:40:11 -07003179
Eric W. Biederman9fcc2d12007-11-14 17:00:07 -08003180 for (i = 0; i <= pid->level; i++) {
Pavel Emelyanov130f77e2007-10-18 23:40:11 -07003181 upid = &pid->numbers[i];
3182 proc_flush_task_mnt(upid->ns->proc_mnt, upid->nr,
Oleg Nesterov9b4d1cbe2009-09-22 16:45:34 -07003183 tgid->numbers[i].nr);
Pavel Emelyanov130f77e2007-10-18 23:40:11 -07003184 }
Pavel Emelyanov60347f62007-10-18 23:40:03 -07003185}
3186
Al Viro0168b9e2018-05-03 09:21:05 -04003187static struct dentry *proc_pid_instantiate(struct dentry * dentry,
Al Viroc52a47a2013-06-15 11:15:20 +04003188 struct task_struct *task, const void *ptr)
Eric W. Biederman444ceed2006-10-02 02:18:49 -07003189{
Eric W. Biederman444ceed2006-10-02 02:18:49 -07003190 struct inode *inode;
3191
Al Viro0168b9e2018-05-03 09:21:05 -04003192 inode = proc_pid_make_inode(dentry->d_sb, task, S_IFDIR | S_IRUGO | S_IXUGO);
Eric W. Biederman444ceed2006-10-02 02:18:49 -07003193 if (!inode)
Al Viro0168b9e2018-05-03 09:21:05 -04003194 return ERR_PTR(-ENOENT);
Eric W. Biederman444ceed2006-10-02 02:18:49 -07003195
Eric W. Biederman444ceed2006-10-02 02:18:49 -07003196 inode->i_op = &proc_tgid_base_inode_operations;
3197 inode->i_fop = &proc_tgid_base_operations;
3198 inode->i_flags|=S_IMMUTABLE;
Vegard Nossumaed54172008-06-05 22:46:53 -07003199
Alexey Dobriyan1270dd82016-12-12 16:45:32 -08003200 set_nlink(inode, nlink_tgid);
Al Viro1bbc5512018-05-02 21:26:16 -04003201 pid_update_inode(task, inode);
Eric W. Biederman444ceed2006-10-02 02:18:49 -07003202
Nick Pigginfb045ad2011-01-07 17:49:55 +11003203 d_set_d_op(dentry, &pid_dentry_operations);
Al Viro0168b9e2018-05-03 09:21:05 -04003204 return d_splice_alias(inode, dentry);
Eric W. Biederman444ceed2006-10-02 02:18:49 -07003205}
3206
Zhikang Zhang867aacc2019-03-05 15:50:29 -08003207struct dentry *proc_pid_lookup(struct dentry *dentry, unsigned int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003208{
3209 struct task_struct *task;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003210 unsigned tgid;
Pavel Emelyanovb4888932007-10-18 23:40:14 -07003211 struct pid_namespace *ns;
Al Viro0168b9e2018-05-03 09:21:05 -04003212 struct dentry *result = ERR_PTR(-ENOENT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003213
Alexey Dobriyandbcdb502014-08-08 14:21:25 -07003214 tgid = name_to_int(&dentry->d_name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003215 if (tgid == ~0U)
3216 goto out;
3217
Pavel Emelyanovb4888932007-10-18 23:40:14 -07003218 ns = dentry->d_sb->s_fs_info;
Eric W. Biedermande758732006-06-26 00:25:51 -07003219 rcu_read_lock();
Pavel Emelyanovb4888932007-10-18 23:40:14 -07003220 task = find_task_by_pid_ns(tgid, ns);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003221 if (task)
3222 get_task_struct(task);
Eric W. Biedermande758732006-06-26 00:25:51 -07003223 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07003224 if (!task)
3225 goto out;
3226
Al Viro0168b9e2018-05-03 09:21:05 -04003227 result = proc_pid_instantiate(dentry, task, NULL);
Eric W. Biederman48e64842006-06-26 00:25:48 -07003228 put_task_struct(task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003229out:
Al Viro0168b9e2018-05-03 09:21:05 -04003230 return result;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003231}
3232
Linus Torvalds1da177e2005-04-16 15:20:36 -07003233/*
Eric W. Biederman0804ef42006-10-02 02:17:04 -07003234 * Find the first task with tgid >= tgid
Eric W. Biederman0bc58a92006-06-26 00:25:50 -07003235 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07003236 */
Eric W. Biederman19fd4bb2007-11-28 16:21:26 -08003237struct tgid_iter {
3238 unsigned int tgid;
Eric W. Biederman0804ef42006-10-02 02:17:04 -07003239 struct task_struct *task;
Eric W. Biederman19fd4bb2007-11-28 16:21:26 -08003240};
3241static struct tgid_iter next_tgid(struct pid_namespace *ns, struct tgid_iter iter)
3242{
Eric W. Biederman0804ef42006-10-02 02:17:04 -07003243 struct pid *pid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003244
Eric W. Biederman19fd4bb2007-11-28 16:21:26 -08003245 if (iter.task)
3246 put_task_struct(iter.task);
Eric W. Biederman0804ef42006-10-02 02:17:04 -07003247 rcu_read_lock();
3248retry:
Eric W. Biederman19fd4bb2007-11-28 16:21:26 -08003249 iter.task = NULL;
3250 pid = find_ge_pid(iter.tgid, ns);
Eric W. Biederman0804ef42006-10-02 02:17:04 -07003251 if (pid) {
Eric W. Biederman19fd4bb2007-11-28 16:21:26 -08003252 iter.tgid = pid_nr_ns(pid, ns);
3253 iter.task = pid_task(pid, PIDTYPE_PID);
Eric W. Biederman0804ef42006-10-02 02:17:04 -07003254 /* What we to know is if the pid we have find is the
3255 * pid of a thread_group_leader. Testing for task
3256 * being a thread_group_leader is the obvious thing
3257 * todo but there is a window when it fails, due to
3258 * the pid transfer logic in de_thread.
3259 *
3260 * So we perform the straight forward test of seeing
3261 * if the pid we have found is the pid of a thread
3262 * group leader, and don't worry if the task we have
3263 * found doesn't happen to be a thread group leader.
3264 * As we don't care in the case of readdir.
3265 */
Eric W. Biederman19fd4bb2007-11-28 16:21:26 -08003266 if (!iter.task || !has_group_leader_pid(iter.task)) {
3267 iter.tgid += 1;
Eric W. Biederman0804ef42006-10-02 02:17:04 -07003268 goto retry;
Eric W. Biederman19fd4bb2007-11-28 16:21:26 -08003269 }
3270 get_task_struct(iter.task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003271 }
Eric W. Biederman454cc102006-06-26 00:25:51 -07003272 rcu_read_unlock();
Eric W. Biederman19fd4bb2007-11-28 16:21:26 -08003273 return iter;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003274}
3275
Eric W. Biederman00978752014-07-31 03:10:50 -07003276#define TGID_OFFSET (FIRST_PROCESS_ENTRY + 2)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003277
3278/* for the /proc/ directory itself, after non-process stuff has been done */
Al Virof0c3b502013-05-16 12:07:31 -04003279int proc_pid_readdir(struct file *file, struct dir_context *ctx)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003280{
Eric W. Biederman19fd4bb2007-11-28 16:21:26 -08003281 struct tgid_iter iter;
Christoph Hellwig76f668b2018-05-16 07:19:01 +02003282 struct pid_namespace *ns = proc_pid_ns(file_inode(file));
Al Virof0c3b502013-05-16 12:07:31 -04003283 loff_t pos = ctx->pos;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003284
Al Viro021ada72013-03-29 19:27:05 -04003285 if (pos >= PID_MAX_LIMIT + TGID_OFFSET)
Al Virof0c3b502013-05-16 12:07:31 -04003286 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003287
Eric W. Biederman00978752014-07-31 03:10:50 -07003288 if (pos == TGID_OFFSET - 2) {
David Howells2b0143b2015-03-17 22:25:59 +00003289 struct inode *inode = d_inode(ns->proc_self);
Al Virodb963162013-06-15 10:45:10 +04003290 if (!dir_emit(ctx, "self", 4, inode->i_ino, DT_LNK))
Al Virof0c3b502013-05-16 12:07:31 -04003291 return 0;
Eric W. Biederman00978752014-07-31 03:10:50 -07003292 ctx->pos = pos = pos + 1;
Al Viro021ada72013-03-29 19:27:05 -04003293 }
Eric W. Biederman00978752014-07-31 03:10:50 -07003294 if (pos == TGID_OFFSET - 1) {
David Howells2b0143b2015-03-17 22:25:59 +00003295 struct inode *inode = d_inode(ns->proc_thread_self);
Eric W. Biederman00978752014-07-31 03:10:50 -07003296 if (!dir_emit(ctx, "thread-self", 11, inode->i_ino, DT_LNK))
3297 return 0;
3298 ctx->pos = pos = pos + 1;
3299 }
3300 iter.tgid = pos - TGID_OFFSET;
Eric W. Biederman19fd4bb2007-11-28 16:21:26 -08003301 iter.task = NULL;
Eric W. Biederman19fd4bb2007-11-28 16:21:26 -08003302 for (iter = next_tgid(ns, iter);
3303 iter.task;
3304 iter.tgid += 1, iter = next_tgid(ns, iter)) {
Alexey Dobriyane3912ac2018-02-06 15:36:51 -08003305 char name[10 + 1];
Alexey Dobriyana4ef3892018-06-07 17:10:10 -07003306 unsigned int len;
Eric Dumazet3ba4bce2017-01-24 15:18:07 -08003307
3308 cond_resched();
Lafcadio Wluiki796f5712017-02-24 15:00:23 -08003309 if (!has_pid_permissions(ns, iter.task, HIDEPID_INVISIBLE))
Al Virof0c3b502013-05-16 12:07:31 -04003310 continue;
Vasiliy Kulikov04996802012-01-10 15:11:31 -08003311
Alexey Dobriyane3912ac2018-02-06 15:36:51 -08003312 len = snprintf(name, sizeof(name), "%u", iter.tgid);
Al Virof0c3b502013-05-16 12:07:31 -04003313 ctx->pos = iter.tgid + TGID_OFFSET;
3314 if (!proc_fill_cache(file, ctx, name, len,
3315 proc_pid_instantiate, iter.task, NULL)) {
Eric W. Biederman19fd4bb2007-11-28 16:21:26 -08003316 put_task_struct(iter.task);
Al Virof0c3b502013-05-16 12:07:31 -04003317 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003318 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003319 }
Al Virof0c3b502013-05-16 12:07:31 -04003320 ctx->pos = PID_MAX_LIMIT + TGID_OFFSET;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003321 return 0;
3322}
3323
Eric W. Biederman0bc58a92006-06-26 00:25:50 -07003324/*
Janis Danisevskis1b3044e2016-05-20 17:00:08 -07003325 * proc_tid_comm_permission is a special permission function exclusively
3326 * used for the node /proc/<pid>/task/<tid>/comm.
3327 * It bypasses generic permission checks in the case where a task of the same
3328 * task group attempts to access the node.
3329 * The rationale behind this is that glibc and bionic access this node for
3330 * cross thread naming (pthread_set/getname_np(!self)). However, if
3331 * PR_SET_DUMPABLE gets set to 0 this node among others becomes uid=0 gid=0,
3332 * which locks out the cross thread naming implementation.
3333 * This function makes sure that the node is always accessible for members of
3334 * same thread group.
3335 */
3336static int proc_tid_comm_permission(struct inode *inode, int mask)
3337{
3338 bool is_same_tgroup;
3339 struct task_struct *task;
3340
3341 task = get_proc_task(inode);
3342 if (!task)
3343 return -ESRCH;
3344 is_same_tgroup = same_thread_group(current, task);
3345 put_task_struct(task);
3346
3347 if (likely(is_same_tgroup && !(mask & MAY_EXEC))) {
3348 /* This file (/proc/<pid>/task/<tid>/comm) can always be
3349 * read or written by the members of the corresponding
3350 * thread group.
3351 */
3352 return 0;
3353 }
3354
3355 return generic_permission(inode, mask);
3356}
3357
3358static const struct inode_operations proc_tid_comm_inode_operations = {
3359 .permission = proc_tid_comm_permission,
3360};
3361
3362/*
Eric W. Biederman28a6d672006-10-02 02:17:05 -07003363 * Tasks
3364 */
Eric Dumazetc5141e62007-05-08 00:26:15 -07003365static const struct pid_entry tid_base_stuff[] = {
Alexey Dobriyan631f9c12008-11-10 01:32:52 +03003366 DIR("fd", S_IRUSR|S_IXUSR, proc_fd_inode_operations, proc_fd_operations),
Jerome Marchand38355412010-04-27 13:13:06 -07003367 DIR("fdinfo", S_IRUSR|S_IXUSR, proc_fdinfo_inode_operations, proc_fdinfo_operations),
Eric W. Biederman6b4e3062010-03-07 16:41:34 -08003368 DIR("ns", S_IRUSR|S_IXUGO, proc_ns_dir_inode_operations, proc_ns_dir_operations),
Eric W. Biederman6ba8ed72014-07-31 16:27:08 -07003369#ifdef CONFIG_NET
3370 DIR("net", S_IRUGO|S_IXUGO, proc_net_inode_operations, proc_net_operations),
3371#endif
Alexey Dobriyan631f9c12008-11-10 01:32:52 +03003372 REG("environ", S_IRUSR, proc_environ_operations),
Al Viroc5317162016-10-05 18:43:43 -04003373 REG("auxv", S_IRUSR, proc_auxv_operations),
Alexey Dobriyan631f9c12008-11-10 01:32:52 +03003374 ONE("status", S_IRUGO, proc_pid_status),
Djalal Harouni35a35042014-04-07 15:38:36 -07003375 ONE("personality", S_IRUSR, proc_pid_personality),
Alexey Dobriyan1c963eb2014-08-08 14:21:37 -07003376 ONE("limits", S_IRUGO, proc_pid_limits),
Ingo Molnar43ae34c2007-07-09 18:52:00 +02003377#ifdef CONFIG_SCHED_DEBUG
Alexey Dobriyan631f9c12008-11-10 01:32:52 +03003378 REG("sched", S_IRUGO|S_IWUSR, proc_pid_sched_operations),
Ingo Molnar43ae34c2007-07-09 18:52:00 +02003379#endif
Janis Danisevskis1b3044e2016-05-20 17:00:08 -07003380 NOD("comm", S_IFREG|S_IRUGO|S_IWUSR,
3381 &proc_tid_comm_inode_operations,
3382 &proc_pid_set_comm_operations, {}),
Roland McGrathebcb6732008-07-25 19:46:00 -07003383#ifdef CONFIG_HAVE_ARCH_TRACEHOOK
Alexey Dobriyan09d93bd2014-08-08 14:21:39 -07003384 ONE("syscall", S_IRUSR, proc_pid_syscall),
Roland McGrathebcb6732008-07-25 19:46:00 -07003385#endif
Alexey Dobriyanc2c0bb42015-06-25 15:00:54 -07003386 REG("cmdline", S_IRUGO, proc_pid_cmdline_ops),
Alexey Dobriyan631f9c12008-11-10 01:32:52 +03003387 ONE("stat", S_IRUGO, proc_tid_stat),
3388 ONE("statm", S_IRUGO, proc_pid_statm),
Vlastimil Babka871305b2018-08-21 21:52:48 -07003389 REG("maps", S_IRUGO, proc_pid_maps_operations),
Iago López Galeiras2e13ba52015-06-25 15:00:57 -07003390#ifdef CONFIG_PROC_CHILDREN
Cyrill Gorcunov818411612012-05-31 16:26:43 -07003391 REG("children", S_IRUGO, proc_tid_children_operations),
3392#endif
Eric W. Biederman28a6d672006-10-02 02:17:05 -07003393#ifdef CONFIG_NUMA
Vlastimil Babka871305b2018-08-21 21:52:48 -07003394 REG("numa_maps", S_IRUGO, proc_pid_numa_maps_operations),
Eric W. Biederman28a6d672006-10-02 02:17:05 -07003395#endif
Alexey Dobriyan631f9c12008-11-10 01:32:52 +03003396 REG("mem", S_IRUSR|S_IWUSR, proc_mem_operations),
3397 LNK("cwd", proc_cwd_link),
3398 LNK("root", proc_root_link),
3399 LNK("exe", proc_exe_link),
3400 REG("mounts", S_IRUGO, proc_mounts_operations),
3401 REG("mountinfo", S_IRUGO, proc_mountinfo_operations),
Matt Mackall1e883282008-02-04 22:29:07 -08003402#ifdef CONFIG_PROC_PAGE_MONITOR
Alexey Dobriyan631f9c12008-11-10 01:32:52 +03003403 REG("clear_refs", S_IWUSR, proc_clear_refs_operations),
Vlastimil Babka871305b2018-08-21 21:52:48 -07003404 REG("smaps", S_IRUGO, proc_pid_smaps_operations),
Daniel Colascione493b0e92017-09-06 16:25:08 -07003405 REG("smaps_rollup", S_IRUGO, proc_pid_smaps_rollup_operations),
Djalal Harouni32ed74a2014-04-07 15:38:38 -07003406 REG("pagemap", S_IRUSR, proc_pagemap_operations),
Eric W. Biederman28a6d672006-10-02 02:17:05 -07003407#endif
3408#ifdef CONFIG_SECURITY
Alexey Dobriyan631f9c12008-11-10 01:32:52 +03003409 DIR("attr", S_IRUGO|S_IXUGO, proc_attr_dir_inode_operations, proc_attr_dir_operations),
Eric W. Biederman28a6d672006-10-02 02:17:05 -07003410#endif
3411#ifdef CONFIG_KALLSYMS
Alexey Dobriyanedfcd602014-08-08 14:21:44 -07003412 ONE("wchan", S_IRUGO, proc_pid_wchan),
Eric W. Biederman28a6d672006-10-02 02:17:05 -07003413#endif
Ken Chen2ec220e2008-11-10 11:26:08 +03003414#ifdef CONFIG_STACKTRACE
Djalal Harouni35a35042014-04-07 15:38:36 -07003415 ONE("stack", S_IRUSR, proc_pid_stack),
Eric W. Biederman28a6d672006-10-02 02:17:05 -07003416#endif
Naveen N. Rao5968cec2015-06-30 14:36:03 +05303417#ifdef CONFIG_SCHED_INFO
Alexey Dobriyanf6e826c2014-08-08 14:21:46 -07003418 ONE("schedstat", S_IRUGO, proc_pid_schedstat),
Eric W. Biederman28a6d672006-10-02 02:17:05 -07003419#endif
Arjan van de Ven97455122008-01-25 21:08:34 +01003420#ifdef CONFIG_LATENCYTOP
Alexey Dobriyan631f9c12008-11-10 01:32:52 +03003421 REG("latency", S_IRUGO, proc_lstats_operations),
Arjan van de Ven97455122008-01-25 21:08:34 +01003422#endif
Paul Menage8793d852007-10-18 23:39:39 -07003423#ifdef CONFIG_PROC_PID_CPUSET
Zefan Li52de4772014-09-18 16:03:36 +08003424 ONE("cpuset", S_IRUGO, proc_cpuset_show),
Eric W. Biederman28a6d672006-10-02 02:17:05 -07003425#endif
Paul Menagea4243162007-10-18 23:39:35 -07003426#ifdef CONFIG_CGROUPS
Zefan Li006f4ac2014-09-18 16:03:15 +08003427 ONE("cgroup", S_IRUGO, proc_cgroup_show),
Paul Menagea4243162007-10-18 23:39:35 -07003428#endif
Alexey Dobriyan6ba51e32014-08-08 14:21:48 -07003429 ONE("oom_score", S_IRUGO, proc_oom_score),
David Rientjesfa0cbbf2012-11-12 17:53:04 -08003430 REG("oom_adj", S_IRUGO|S_IWUSR, proc_oom_adj_operations),
David Rientjesa63d83f2010-08-09 17:19:46 -07003431 REG("oom_score_adj", S_IRUGO|S_IWUSR, proc_oom_score_adj_operations),
Richard Guy Briggs4b7d2482019-01-22 17:06:39 -05003432#ifdef CONFIG_AUDIT
Alexey Dobriyan631f9c12008-11-10 01:32:52 +03003433 REG("loginuid", S_IWUSR|S_IRUGO, proc_loginuid_operations),
Al Viro26ec3c62011-02-15 21:24:05 -05003434 REG("sessionid", S_IRUGO, proc_sessionid_operations),
Eric W. Biederman28a6d672006-10-02 02:17:05 -07003435#endif
Akinobu Mitaf4f154f2006-12-08 02:39:47 -08003436#ifdef CONFIG_FAULT_INJECTION
Alexey Dobriyan631f9c12008-11-10 01:32:52 +03003437 REG("make-it-fail", S_IRUGO|S_IWUSR, proc_fault_inject_operations),
Akinobu Mita1203c8e2017-07-14 14:49:57 -07003438 REG("fail-nth", 0644, proc_fail_nth_operations),
Akinobu Mitaf4f154f2006-12-08 02:39:47 -08003439#endif
Andrea Righi297c5d92008-07-25 01:48:49 -07003440#ifdef CONFIG_TASK_IO_ACCOUNTING
Alexey Dobriyan19aadc92014-08-08 14:21:50 -07003441 ONE("io", S_IRUSR, proc_tid_io_accounting),
Andrea Righi297c5d92008-07-25 01:48:49 -07003442#endif
Eric W. Biederman22d917d2011-11-17 00:11:58 -08003443#ifdef CONFIG_USER_NS
3444 REG("uid_map", S_IRUGO|S_IWUSR, proc_uid_map_operations),
3445 REG("gid_map", S_IRUGO|S_IWUSR, proc_gid_map_operations),
Eric W. Biedermanf76d2072012-08-30 01:24:05 -07003446 REG("projid_map", S_IRUGO|S_IWUSR, proc_projid_map_operations),
Eric W. Biederman9cc46512014-12-02 12:27:26 -06003447 REG("setgroups", S_IRUGO|S_IWUSR, proc_setgroups_operations),
Eric W. Biederman22d917d2011-11-17 00:11:58 -08003448#endif
Josh Poimboeuf7c23b332017-02-13 19:42:41 -06003449#ifdef CONFIG_LIVEPATCH
3450 ONE("patch_state", S_IRUSR, proc_pid_patch_state),
3451#endif
Eric W. Biederman28a6d672006-10-02 02:17:05 -07003452};
3453
Al Virof0c3b502013-05-16 12:07:31 -04003454static int proc_tid_base_readdir(struct file *file, struct dir_context *ctx)
Eric W. Biederman28a6d672006-10-02 02:17:05 -07003455{
Al Virof0c3b502013-05-16 12:07:31 -04003456 return proc_pident_readdir(file, ctx,
3457 tid_base_stuff, ARRAY_SIZE(tid_base_stuff));
Eric W. Biederman28a6d672006-10-02 02:17:05 -07003458}
3459
Al Viro00cd8dd2012-06-10 17:13:09 -04003460static struct dentry *proc_tid_base_lookup(struct inode *dir, struct dentry *dentry, unsigned int flags)
3461{
Eric W. Biederman7bcd6b02006-10-02 02:18:56 -07003462 return proc_pident_lookup(dir, dentry,
Alexey Dobriyand5a572a2019-03-11 23:28:51 -07003463 tid_base_stuff,
3464 tid_base_stuff + ARRAY_SIZE(tid_base_stuff));
Eric W. Biederman28a6d672006-10-02 02:17:05 -07003465}
3466
Arjan van de Ven00977a52007-02-12 00:55:34 -08003467static const struct file_operations proc_tid_base_operations = {
Eric W. Biederman28a6d672006-10-02 02:17:05 -07003468 .read = generic_read_dir,
Al Virof50752e2016-04-20 17:13:54 -04003469 .iterate_shared = proc_tid_base_readdir,
3470 .llseek = generic_file_llseek,
Eric W. Biederman28a6d672006-10-02 02:17:05 -07003471};
3472
Arjan van de Venc5ef1c42007-02-12 00:55:40 -08003473static const struct inode_operations proc_tid_base_inode_operations = {
Eric W. Biederman28a6d672006-10-02 02:17:05 -07003474 .lookup = proc_tid_base_lookup,
3475 .getattr = pid_getattr,
3476 .setattr = proc_setattr,
3477};
3478
Al Viro0168b9e2018-05-03 09:21:05 -04003479static struct dentry *proc_task_instantiate(struct dentry *dentry,
3480 struct task_struct *task, const void *ptr)
Eric W. Biederman444ceed2006-10-02 02:18:49 -07003481{
Eric W. Biederman444ceed2006-10-02 02:18:49 -07003482 struct inode *inode;
Al Viro0168b9e2018-05-03 09:21:05 -04003483 inode = proc_pid_make_inode(dentry->d_sb, task, S_IFDIR | S_IRUGO | S_IXUGO);
Eric W. Biederman444ceed2006-10-02 02:18:49 -07003484 if (!inode)
Al Viro0168b9e2018-05-03 09:21:05 -04003485 return ERR_PTR(-ENOENT);
Al Viro1bbc5512018-05-02 21:26:16 -04003486
Eric W. Biederman444ceed2006-10-02 02:18:49 -07003487 inode->i_op = &proc_tid_base_inode_operations;
3488 inode->i_fop = &proc_tid_base_operations;
Al Viro1bbc5512018-05-02 21:26:16 -04003489 inode->i_flags |= S_IMMUTABLE;
Vegard Nossumaed54172008-06-05 22:46:53 -07003490
Alexey Dobriyan1270dd82016-12-12 16:45:32 -08003491 set_nlink(inode, nlink_tid);
Al Viro1bbc5512018-05-02 21:26:16 -04003492 pid_update_inode(task, inode);
Eric W. Biederman444ceed2006-10-02 02:18:49 -07003493
Nick Pigginfb045ad2011-01-07 17:49:55 +11003494 d_set_d_op(dentry, &pid_dentry_operations);
Al Viro0168b9e2018-05-03 09:21:05 -04003495 return d_splice_alias(inode, dentry);
Eric W. Biederman444ceed2006-10-02 02:18:49 -07003496}
3497
Al Viro00cd8dd2012-06-10 17:13:09 -04003498static struct dentry *proc_task_lookup(struct inode *dir, struct dentry * dentry, unsigned int flags)
Eric W. Biederman28a6d672006-10-02 02:17:05 -07003499{
Eric W. Biederman28a6d672006-10-02 02:17:05 -07003500 struct task_struct *task;
3501 struct task_struct *leader = get_proc_task(dir);
Eric W. Biederman28a6d672006-10-02 02:17:05 -07003502 unsigned tid;
Pavel Emelyanovb4888932007-10-18 23:40:14 -07003503 struct pid_namespace *ns;
Al Viro0168b9e2018-05-03 09:21:05 -04003504 struct dentry *result = ERR_PTR(-ENOENT);
Eric W. Biederman28a6d672006-10-02 02:17:05 -07003505
3506 if (!leader)
3507 goto out_no_task;
3508
Alexey Dobriyandbcdb502014-08-08 14:21:25 -07003509 tid = name_to_int(&dentry->d_name);
Eric W. Biederman28a6d672006-10-02 02:17:05 -07003510 if (tid == ~0U)
3511 goto out;
3512
Pavel Emelyanovb4888932007-10-18 23:40:14 -07003513 ns = dentry->d_sb->s_fs_info;
Eric W. Biederman28a6d672006-10-02 02:17:05 -07003514 rcu_read_lock();
Pavel Emelyanovb4888932007-10-18 23:40:14 -07003515 task = find_task_by_pid_ns(tid, ns);
Eric W. Biederman28a6d672006-10-02 02:17:05 -07003516 if (task)
3517 get_task_struct(task);
3518 rcu_read_unlock();
3519 if (!task)
3520 goto out;
Pavel Emelyanovbac0abd2007-10-18 23:40:18 -07003521 if (!same_thread_group(leader, task))
Eric W. Biederman28a6d672006-10-02 02:17:05 -07003522 goto out_drop_task;
3523
Al Viro0168b9e2018-05-03 09:21:05 -04003524 result = proc_task_instantiate(dentry, task, NULL);
Eric W. Biederman28a6d672006-10-02 02:17:05 -07003525out_drop_task:
3526 put_task_struct(task);
3527out:
3528 put_task_struct(leader);
3529out_no_task:
Al Viro0168b9e2018-05-03 09:21:05 -04003530 return result;
Eric W. Biederman28a6d672006-10-02 02:17:05 -07003531}
3532
3533/*
Eric W. Biederman0bc58a92006-06-26 00:25:50 -07003534 * Find the first tid of a thread group to return to user space.
3535 *
3536 * Usually this is just the thread group leader, but if the users
3537 * buffer was too small or there was a seek into the middle of the
3538 * directory we have more work todo.
3539 *
3540 * In the case of a short read we start with find_task_by_pid.
3541 *
3542 * In the case of a seek we start with the leader and walk nr
3543 * threads past it.
3544 */
Oleg Nesterov9f6e9632014-01-23 15:55:40 -08003545static struct task_struct *first_tid(struct pid *pid, int tid, loff_t f_pos,
3546 struct pid_namespace *ns)
Eric W. Biederman0bc58a92006-06-26 00:25:50 -07003547{
Oleg Nesterovd855a4b2014-01-23 15:55:39 -08003548 struct task_struct *pos, *task;
Oleg Nesterov9f6e9632014-01-23 15:55:40 -08003549 unsigned long nr = f_pos;
3550
3551 if (nr != f_pos) /* 32bit overflow? */
3552 return NULL;
Eric W. Biederman0bc58a92006-06-26 00:25:50 -07003553
Eric W. Biedermancc288732006-06-26 00:26:01 -07003554 rcu_read_lock();
Oleg Nesterovd855a4b2014-01-23 15:55:39 -08003555 task = pid_task(pid, PIDTYPE_PID);
3556 if (!task)
3557 goto fail;
3558
3559 /* Attempt to start with the tid of a thread */
Oleg Nesterov9f6e9632014-01-23 15:55:40 -08003560 if (tid && nr) {
Pavel Emelyanovb4888932007-10-18 23:40:14 -07003561 pos = find_task_by_pid_ns(tid, ns);
Oleg Nesterovd855a4b2014-01-23 15:55:39 -08003562 if (pos && same_thread_group(pos, task))
Oleg Nesterova872ff02006-06-26 00:26:01 -07003563 goto found;
Eric W. Biederman0bc58a92006-06-26 00:25:50 -07003564 }
3565
3566 /* If nr exceeds the number of threads there is nothing todo */
Oleg Nesterov9f6e9632014-01-23 15:55:40 -08003567 if (nr >= get_nr_threads(task))
Oleg Nesterovc986c142014-01-23 15:55:38 -08003568 goto fail;
Oleg Nesterova872ff02006-06-26 00:26:01 -07003569
3570 /* If we haven't found our starting place yet start
3571 * with the leader and walk nr threads forward.
3572 */
Oleg Nesterovd855a4b2014-01-23 15:55:39 -08003573 pos = task = task->group_leader;
Oleg Nesterovc986c142014-01-23 15:55:38 -08003574 do {
Oleg Nesterov9f6e9632014-01-23 15:55:40 -08003575 if (!nr--)
Oleg Nesterovc986c142014-01-23 15:55:38 -08003576 goto found;
Oleg Nesterovd855a4b2014-01-23 15:55:39 -08003577 } while_each_thread(task, pos);
Oleg Nesterovc986c142014-01-23 15:55:38 -08003578fail:
3579 pos = NULL;
3580 goto out;
Oleg Nesterova872ff02006-06-26 00:26:01 -07003581found:
3582 get_task_struct(pos);
3583out:
Eric W. Biedermancc288732006-06-26 00:26:01 -07003584 rcu_read_unlock();
Eric W. Biederman0bc58a92006-06-26 00:25:50 -07003585 return pos;
3586}
3587
3588/*
3589 * Find the next thread in the thread list.
3590 * Return NULL if there is an error or no next thread.
3591 *
3592 * The reference to the input task_struct is released.
3593 */
3594static struct task_struct *next_tid(struct task_struct *start)
3595{
Oleg Nesterovc1df7fb2006-06-26 00:26:02 -07003596 struct task_struct *pos = NULL;
Eric W. Biedermancc288732006-06-26 00:26:01 -07003597 rcu_read_lock();
Oleg Nesterovc1df7fb2006-06-26 00:26:02 -07003598 if (pid_alive(start)) {
Eric W. Biederman0bc58a92006-06-26 00:25:50 -07003599 pos = next_thread(start);
Oleg Nesterovc1df7fb2006-06-26 00:26:02 -07003600 if (thread_group_leader(pos))
3601 pos = NULL;
3602 else
3603 get_task_struct(pos);
3604 }
Eric W. Biedermancc288732006-06-26 00:26:01 -07003605 rcu_read_unlock();
Eric W. Biederman0bc58a92006-06-26 00:25:50 -07003606 put_task_struct(start);
3607 return pos;
3608}
3609
Linus Torvalds1da177e2005-04-16 15:20:36 -07003610/* for the /proc/TGID/task/ directories */
Al Virof0c3b502013-05-16 12:07:31 -04003611static int proc_task_readdir(struct file *file, struct dir_context *ctx)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003612{
Oleg Nesterovd855a4b2014-01-23 15:55:39 -08003613 struct inode *inode = file_inode(file);
3614 struct task_struct *task;
Pavel Emelyanovb4888932007-10-18 23:40:14 -07003615 struct pid_namespace *ns;
Al Virof0c3b502013-05-16 12:07:31 -04003616 int tid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003617
Oleg Nesterovd855a4b2014-01-23 15:55:39 -08003618 if (proc_inode_is_dead(inode))
Al Virof0c3b502013-05-16 12:07:31 -04003619 return -ENOENT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003620
Al Virof0c3b502013-05-16 12:07:31 -04003621 if (!dir_emit_dots(file, ctx))
Oleg Nesterovd855a4b2014-01-23 15:55:39 -08003622 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003623
Eric W. Biederman0bc58a92006-06-26 00:25:50 -07003624 /* f_version caches the tgid value that the last readdir call couldn't
3625 * return. lseek aka telldir automagically resets f_version to 0.
3626 */
Christoph Hellwig76f668b2018-05-16 07:19:01 +02003627 ns = proc_pid_ns(inode);
Al Virof0c3b502013-05-16 12:07:31 -04003628 tid = (int)file->f_version;
3629 file->f_version = 0;
Oleg Nesterovd855a4b2014-01-23 15:55:39 -08003630 for (task = first_tid(proc_pid(inode), tid, ctx->pos - 2, ns);
Eric W. Biederman0bc58a92006-06-26 00:25:50 -07003631 task;
Al Virof0c3b502013-05-16 12:07:31 -04003632 task = next_tid(task), ctx->pos++) {
Alexey Dobriyane3912ac2018-02-06 15:36:51 -08003633 char name[10 + 1];
Alexey Dobriyana4ef3892018-06-07 17:10:10 -07003634 unsigned int len;
Pavel Emelyanovb4888932007-10-18 23:40:14 -07003635 tid = task_pid_nr_ns(task, ns);
Alexey Dobriyane3912ac2018-02-06 15:36:51 -08003636 len = snprintf(name, sizeof(name), "%u", tid);
Al Virof0c3b502013-05-16 12:07:31 -04003637 if (!proc_fill_cache(file, ctx, name, len,
3638 proc_task_instantiate, task, NULL)) {
Eric W. Biederman0bc58a92006-06-26 00:25:50 -07003639 /* returning this tgid failed, save it as the first
3640 * pid for the next readir call */
Al Virof0c3b502013-05-16 12:07:31 -04003641 file->f_version = (u64)tid;
Eric W. Biederman0bc58a92006-06-26 00:25:50 -07003642 put_task_struct(task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003643 break;
Eric W. Biederman0bc58a92006-06-26 00:25:50 -07003644 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003645 }
Oleg Nesterovd855a4b2014-01-23 15:55:39 -08003646
Al Virof0c3b502013-05-16 12:07:31 -04003647 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003648}
Eric W. Biederman6e66b522006-06-26 00:25:47 -07003649
David Howellsa528d352017-01-31 16:46:22 +00003650static int proc_task_getattr(const struct path *path, struct kstat *stat,
3651 u32 request_mask, unsigned int query_flags)
Eric W. Biederman6e66b522006-06-26 00:25:47 -07003652{
David Howellsa528d352017-01-31 16:46:22 +00003653 struct inode *inode = d_inode(path->dentry);
Eric W. Biederman99f89552006-06-26 00:25:55 -07003654 struct task_struct *p = get_proc_task(inode);
Eric W. Biederman6e66b522006-06-26 00:25:47 -07003655 generic_fillattr(inode, stat);
3656
Eric W. Biederman99f89552006-06-26 00:25:55 -07003657 if (p) {
Eric W. Biederman99f89552006-06-26 00:25:55 -07003658 stat->nlink += get_nr_threads(p);
Eric W. Biederman99f89552006-06-26 00:25:55 -07003659 put_task_struct(p);
Eric W. Biederman6e66b522006-06-26 00:25:47 -07003660 }
3661
3662 return 0;
3663}
Eric W. Biederman28a6d672006-10-02 02:17:05 -07003664
Arjan van de Venc5ef1c42007-02-12 00:55:40 -08003665static const struct inode_operations proc_task_inode_operations = {
Eric W. Biederman28a6d672006-10-02 02:17:05 -07003666 .lookup = proc_task_lookup,
3667 .getattr = proc_task_getattr,
3668 .setattr = proc_setattr,
Vasiliy Kulikov04996802012-01-10 15:11:31 -08003669 .permission = proc_pid_permission,
Eric W. Biederman28a6d672006-10-02 02:17:05 -07003670};
3671
Arjan van de Ven00977a52007-02-12 00:55:34 -08003672static const struct file_operations proc_task_operations = {
Eric W. Biederman28a6d672006-10-02 02:17:05 -07003673 .read = generic_read_dir,
Al Virof50752e2016-04-20 17:13:54 -04003674 .iterate_shared = proc_task_readdir,
3675 .llseek = generic_file_llseek,
Eric W. Biederman28a6d672006-10-02 02:17:05 -07003676};
Alexey Dobriyan1270dd82016-12-12 16:45:32 -08003677
3678void __init set_proc_pid_nlink(void)
3679{
3680 nlink_tid = pid_entry_nlink(tid_base_stuff, ARRAY_SIZE(tid_base_stuff));
3681 nlink_tgid = pid_entry_nlink(tgid_base_stuff, ARRAY_SIZE(tgid_base_stuff));
3682}