blob: 534fb1ae498aabade3c22228ed6b960d18ed05cc [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{
Ken Chen2ec220e2008-11-10 11:26:08 +0300410 unsigned long *entries;
Al Viroa9712bc2011-03-23 15:52:50 -0400411 int err;
Ken Chen2ec220e2008-11-10 11:26:08 +0300412
Jann Hornf8a00ce2018-10-05 15:51:58 -0700413 /*
414 * The ability to racily run the kernel stack unwinder on a running task
415 * and then observe the unwinder output is scary; while it is useful for
416 * debugging kernel issues, it can also allow an attacker to leak kernel
417 * stack contents.
418 * Doing this in a manner that is at least safe from races would require
419 * some work to ensure that the remote task can not be scheduled; and
420 * even then, this would still expose the unwinder as local attack
421 * surface.
422 * Therefore, this interface is restricted to root.
423 */
424 if (!file_ns_capable(m->file, &init_user_ns, CAP_SYS_ADMIN))
425 return -EACCES;
426
Kees Cook6da2ec52018-06-12 13:55:00 -0700427 entries = kmalloc_array(MAX_STACK_TRACE_DEPTH, sizeof(*entries),
428 GFP_KERNEL);
Ken Chen2ec220e2008-11-10 11:26:08 +0300429 if (!entries)
430 return -ENOMEM;
431
Al Viroa9712bc2011-03-23 15:52:50 -0400432 err = lock_trace(task);
433 if (!err) {
Thomas Gleixnere988e5e2019-04-25 11:44:58 +0200434 unsigned int i, nr_entries;
Alexey Dobriyan5d008fb2018-06-07 17:10:17 -0700435
Thomas Gleixnere988e5e2019-04-25 11:44:58 +0200436 nr_entries = stack_trace_save_tsk(task, entries,
437 MAX_STACK_TRACE_DEPTH, 0);
Al Viroa9712bc2011-03-23 15:52:50 -0400438
Thomas Gleixnere988e5e2019-04-25 11:44:58 +0200439 for (i = 0; i < nr_entries; i++) {
Linus Torvalds8f5abe82017-11-27 16:45:56 -0800440 seq_printf(m, "[<0>] %pB\n", (void *)entries[i]);
Al Viroa9712bc2011-03-23 15:52:50 -0400441 }
Thomas Gleixnere988e5e2019-04-25 11:44:58 +0200442
Al Viroa9712bc2011-03-23 15:52:50 -0400443 unlock_trace(task);
Ken Chen2ec220e2008-11-10 11:26:08 +0300444 }
445 kfree(entries);
446
Al Viroa9712bc2011-03-23 15:52:50 -0400447 return err;
Ken Chen2ec220e2008-11-10 11:26:08 +0300448}
449#endif
450
Naveen N. Rao5968cec2015-06-30 14:36:03 +0530451#ifdef CONFIG_SCHED_INFO
Linus Torvalds1da177e2005-04-16 15:20:36 -0700452/*
453 * Provides /proc/PID/schedstat
454 */
Alexey Dobriyanf6e826c2014-08-08 14:21:46 -0700455static int proc_pid_schedstat(struct seq_file *m, struct pid_namespace *ns,
456 struct pid *pid, struct task_struct *task)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700457{
Naveen N. Rao5968cec2015-06-30 14:36:03 +0530458 if (unlikely(!sched_info_on()))
Alexey Dobriyan08b55772019-03-05 15:50:35 -0800459 seq_puts(m, "0 0 0\n");
Naveen N. Rao5968cec2015-06-30 14:36:03 +0530460 else
461 seq_printf(m, "%llu %llu %lu\n",
Joe Perches25ce3192015-04-15 16:18:17 -0700462 (unsigned long long)task->se.sum_exec_runtime,
463 (unsigned long long)task->sched_info.run_delay,
464 task->sched_info.pcount);
465
466 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700467}
468#endif
469
Arjan van de Ven97455122008-01-25 21:08:34 +0100470#ifdef CONFIG_LATENCYTOP
471static int lstats_show_proc(struct seq_file *m, void *v)
472{
473 int i;
Hiroshi Shimamoto13d77c32008-02-20 16:53:29 -0800474 struct inode *inode = m->private;
475 struct task_struct *task = get_proc_task(inode);
Arjan van de Ven97455122008-01-25 21:08:34 +0100476
Hiroshi Shimamoto13d77c32008-02-20 16:53:29 -0800477 if (!task)
478 return -ESRCH;
479 seq_puts(m, "Latency Top version : v0.1\n");
Alexey Dobriyanf6d2f582018-08-21 21:54:34 -0700480 for (i = 0; i < LT_SAVECOUNT; i++) {
Joe Perches34e49d42011-01-12 17:00:30 -0800481 struct latency_record *lr = &task->latency_record[i];
482 if (lr->backtrace[0]) {
Arjan van de Ven97455122008-01-25 21:08:34 +0100483 int q;
Joe Perches34e49d42011-01-12 17:00:30 -0800484 seq_printf(m, "%i %li %li",
485 lr->count, lr->time, lr->max);
Arjan van de Ven97455122008-01-25 21:08:34 +0100486 for (q = 0; q < LT_BACKTRACEDEPTH; q++) {
Joe Perches34e49d42011-01-12 17:00:30 -0800487 unsigned long bt = lr->backtrace[q];
Thomas Gleixneraccddc42019-04-10 12:28:08 +0200488
Joe Perches34e49d42011-01-12 17:00:30 -0800489 if (!bt)
Arjan van de Ven97455122008-01-25 21:08:34 +0100490 break;
Joe Perches34e49d42011-01-12 17:00:30 -0800491 seq_printf(m, " %ps", (void *)bt);
Arjan van de Ven97455122008-01-25 21:08:34 +0100492 }
Alexey Dobriyan9d6de122011-01-12 17:00:32 -0800493 seq_putc(m, '\n');
Arjan van de Ven97455122008-01-25 21:08:34 +0100494 }
495
496 }
Hiroshi Shimamoto13d77c32008-02-20 16:53:29 -0800497 put_task_struct(task);
Arjan van de Ven97455122008-01-25 21:08:34 +0100498 return 0;
499}
500
501static int lstats_open(struct inode *inode, struct file *file)
502{
Hiroshi Shimamoto13d77c32008-02-20 16:53:29 -0800503 return single_open(file, lstats_show_proc, inode);
Hiroshi Shimamotod6643d12008-02-14 10:27:00 -0800504}
505
Arjan van de Ven97455122008-01-25 21:08:34 +0100506static ssize_t lstats_write(struct file *file, const char __user *buf,
507 size_t count, loff_t *offs)
508{
Al Viro496ad9a2013-01-23 17:07:38 -0500509 struct task_struct *task = get_proc_task(file_inode(file));
Arjan van de Ven97455122008-01-25 21:08:34 +0100510
Hiroshi Shimamoto13d77c32008-02-20 16:53:29 -0800511 if (!task)
512 return -ESRCH;
Lin Fenge02c9b02019-05-14 15:42:34 -0700513 clear_tsk_latency_tracing(task);
Hiroshi Shimamoto13d77c32008-02-20 16:53:29 -0800514 put_task_struct(task);
Arjan van de Ven97455122008-01-25 21:08:34 +0100515
516 return count;
517}
518
519static const struct file_operations proc_lstats_operations = {
520 .open = lstats_open,
521 .read = seq_read,
522 .write = lstats_write,
523 .llseek = seq_lseek,
Hiroshi Shimamoto13d77c32008-02-20 16:53:29 -0800524 .release = single_release,
Arjan van de Ven97455122008-01-25 21:08:34 +0100525};
526
527#endif
528
Alexey Dobriyan6ba51e32014-08-08 14:21:48 -0700529static int proc_oom_score(struct seq_file *m, struct pid_namespace *ns,
530 struct pid *pid, struct task_struct *task)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700531{
Arun KSca79b0c2018-12-28 00:34:29 -0800532 unsigned long totalpages = totalram_pages() + total_swap_pages;
Oleg Nesterovb95c35e2010-04-01 15:13:57 +0200533 unsigned long points = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700534
Oleg Nesterovef419392016-08-02 14:03:19 -0700535 points = oom_badness(task, NULL, NULL, totalpages) *
536 1000 / totalpages;
Joe Perches25ce3192015-04-15 16:18:17 -0700537 seq_printf(m, "%lu\n", points);
538
539 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700540}
541
Neil Hormand85f50d2007-10-18 23:40:37 -0700542struct limit_names {
Alexey Dobriyancedbcca2014-08-08 14:21:33 -0700543 const char *name;
544 const char *unit;
Neil Hormand85f50d2007-10-18 23:40:37 -0700545};
546
547static const struct limit_names lnames[RLIM_NLIMITS] = {
Kees Cookcff4edb2009-09-22 16:45:32 -0700548 [RLIMIT_CPU] = {"Max cpu time", "seconds"},
Neil Hormand85f50d2007-10-18 23:40:37 -0700549 [RLIMIT_FSIZE] = {"Max file size", "bytes"},
550 [RLIMIT_DATA] = {"Max data size", "bytes"},
551 [RLIMIT_STACK] = {"Max stack size", "bytes"},
552 [RLIMIT_CORE] = {"Max core file size", "bytes"},
553 [RLIMIT_RSS] = {"Max resident set", "bytes"},
554 [RLIMIT_NPROC] = {"Max processes", "processes"},
555 [RLIMIT_NOFILE] = {"Max open files", "files"},
556 [RLIMIT_MEMLOCK] = {"Max locked memory", "bytes"},
557 [RLIMIT_AS] = {"Max address space", "bytes"},
558 [RLIMIT_LOCKS] = {"Max file locks", "locks"},
559 [RLIMIT_SIGPENDING] = {"Max pending signals", "signals"},
560 [RLIMIT_MSGQUEUE] = {"Max msgqueue size", "bytes"},
561 [RLIMIT_NICE] = {"Max nice priority", NULL},
562 [RLIMIT_RTPRIO] = {"Max realtime priority", NULL},
Eugene Teo88081172008-02-23 15:23:52 -0800563 [RLIMIT_RTTIME] = {"Max realtime timeout", "us"},
Neil Hormand85f50d2007-10-18 23:40:37 -0700564};
565
566/* Display limits for a process */
Alexey Dobriyan1c963eb2014-08-08 14:21:37 -0700567static int proc_pid_limits(struct seq_file *m, struct pid_namespace *ns,
568 struct pid *pid, struct task_struct *task)
Neil Hormand85f50d2007-10-18 23:40:37 -0700569{
570 unsigned int i;
Neil Hormand85f50d2007-10-18 23:40:37 -0700571 unsigned long flags;
Neil Hormand85f50d2007-10-18 23:40:37 -0700572
573 struct rlimit rlim[RLIM_NLIMITS];
574
Lai Jiangshana6bebbc2008-10-05 00:51:15 +0400575 if (!lock_task_sighand(task, &flags))
Neil Hormand85f50d2007-10-18 23:40:37 -0700576 return 0;
Neil Hormand85f50d2007-10-18 23:40:37 -0700577 memcpy(rlim, task->signal->rlim, sizeof(struct rlimit) * RLIM_NLIMITS);
578 unlock_task_sighand(task, &flags);
Neil Hormand85f50d2007-10-18 23:40:37 -0700579
580 /*
581 * print the file header
582 */
Alexey Dobriyanafe922c2019-01-03 15:26:09 -0800583 seq_puts(m, "Limit "
584 "Soft Limit "
585 "Hard Limit "
586 "Units \n");
Neil Hormand85f50d2007-10-18 23:40:37 -0700587
588 for (i = 0; i < RLIM_NLIMITS; i++) {
589 if (rlim[i].rlim_cur == RLIM_INFINITY)
Alexey Dobriyan1c963eb2014-08-08 14:21:37 -0700590 seq_printf(m, "%-25s %-20s ",
Joe Perches25ce3192015-04-15 16:18:17 -0700591 lnames[i].name, "unlimited");
Neil Hormand85f50d2007-10-18 23:40:37 -0700592 else
Alexey Dobriyan1c963eb2014-08-08 14:21:37 -0700593 seq_printf(m, "%-25s %-20lu ",
Joe Perches25ce3192015-04-15 16:18:17 -0700594 lnames[i].name, rlim[i].rlim_cur);
Neil Hormand85f50d2007-10-18 23:40:37 -0700595
596 if (rlim[i].rlim_max == RLIM_INFINITY)
Alexey Dobriyan1c963eb2014-08-08 14:21:37 -0700597 seq_printf(m, "%-20s ", "unlimited");
Neil Hormand85f50d2007-10-18 23:40:37 -0700598 else
Alexey Dobriyan1c963eb2014-08-08 14:21:37 -0700599 seq_printf(m, "%-20lu ", rlim[i].rlim_max);
Neil Hormand85f50d2007-10-18 23:40:37 -0700600
601 if (lnames[i].unit)
Alexey Dobriyan1c963eb2014-08-08 14:21:37 -0700602 seq_printf(m, "%-10s\n", lnames[i].unit);
Neil Hormand85f50d2007-10-18 23:40:37 -0700603 else
Alexey Dobriyan1c963eb2014-08-08 14:21:37 -0700604 seq_putc(m, '\n');
Neil Hormand85f50d2007-10-18 23:40:37 -0700605 }
606
Alexey Dobriyan1c963eb2014-08-08 14:21:37 -0700607 return 0;
Neil Hormand85f50d2007-10-18 23:40:37 -0700608}
609
Roland McGrathebcb6732008-07-25 19:46:00 -0700610#ifdef CONFIG_HAVE_ARCH_TRACEHOOK
Alexey Dobriyan09d93bd2014-08-08 14:21:39 -0700611static int proc_pid_syscall(struct seq_file *m, struct pid_namespace *ns,
612 struct pid *pid, struct task_struct *task)
Roland McGrathebcb6732008-07-25 19:46:00 -0700613{
Steven Rostedt (Red Hat)631b7ab2016-11-07 16:26:35 -0500614 struct syscall_info info;
615 u64 *args = &info.data.args[0];
Joe Perches25ce3192015-04-15 16:18:17 -0700616 int res;
617
618 res = lock_trace(task);
Al Viroa9712bc2011-03-23 15:52:50 -0400619 if (res)
620 return res;
Roland McGrathebcb6732008-07-25 19:46:00 -0700621
Steven Rostedt (Red Hat)631b7ab2016-11-07 16:26:35 -0500622 if (task_current_syscall(task, &info))
Alexey Dobriyan09d93bd2014-08-08 14:21:39 -0700623 seq_puts(m, "running\n");
Steven Rostedt (Red Hat)631b7ab2016-11-07 16:26:35 -0500624 else if (info.data.nr < 0)
625 seq_printf(m, "%d 0x%llx 0x%llx\n",
626 info.data.nr, info.sp, info.data.instruction_pointer);
Al Viroa9712bc2011-03-23 15:52:50 -0400627 else
Alexey Dobriyan09d93bd2014-08-08 14:21:39 -0700628 seq_printf(m,
Steven Rostedt (Red Hat)631b7ab2016-11-07 16:26:35 -0500629 "%d 0x%llx 0x%llx 0x%llx 0x%llx 0x%llx 0x%llx 0x%llx 0x%llx\n",
630 info.data.nr,
Roland McGrathebcb6732008-07-25 19:46:00 -0700631 args[0], args[1], args[2], args[3], args[4], args[5],
Steven Rostedt (Red Hat)631b7ab2016-11-07 16:26:35 -0500632 info.sp, info.data.instruction_pointer);
Al Viroa9712bc2011-03-23 15:52:50 -0400633 unlock_trace(task);
Joe Perches25ce3192015-04-15 16:18:17 -0700634
635 return 0;
Roland McGrathebcb6732008-07-25 19:46:00 -0700636}
637#endif /* CONFIG_HAVE_ARCH_TRACEHOOK */
638
Linus Torvalds1da177e2005-04-16 15:20:36 -0700639/************************************************************************/
640/* Here the fs part begins */
641/************************************************************************/
642
643/* permission checks */
Eric W. Biederman778c1142006-06-26 00:25:58 -0700644static int proc_fd_access_allowed(struct inode *inode)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700645{
Eric W. Biederman778c1142006-06-26 00:25:58 -0700646 struct task_struct *task;
647 int allowed = 0;
Eric W. Biedermandf26c402006-06-26 00:25:59 -0700648 /* Allow access to a task's file descriptors if it is us or we
649 * may use ptrace attach to the process and find out that
650 * information.
Eric W. Biederman778c1142006-06-26 00:25:58 -0700651 */
652 task = get_proc_task(inode);
Eric W. Biedermandf26c402006-06-26 00:25:59 -0700653 if (task) {
Jann Horncaaee622016-01-20 15:00:04 -0800654 allowed = ptrace_may_access(task, PTRACE_MODE_READ_FSCREDS);
Eric W. Biederman778c1142006-06-26 00:25:58 -0700655 put_task_struct(task);
Eric W. Biedermandf26c402006-06-26 00:25:59 -0700656 }
Eric W. Biederman778c1142006-06-26 00:25:58 -0700657 return allowed;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700658}
659
Eric W. Biederman6b4e3062010-03-07 16:41:34 -0800660int proc_setattr(struct dentry *dentry, struct iattr *attr)
Linus Torvalds6d76fa52006-07-15 12:26:45 -0700661{
662 int error;
David Howells2b0143b2015-03-17 22:25:59 +0000663 struct inode *inode = d_inode(dentry);
Linus Torvalds6d76fa52006-07-15 12:26:45 -0700664
665 if (attr->ia_valid & ATTR_MODE)
666 return -EPERM;
667
Jan Kara31051c82016-05-26 16:55:18 +0200668 error = setattr_prepare(dentry, attr);
Christoph Hellwig10257742010-06-04 11:30:02 +0200669 if (error)
670 return error;
671
Christoph Hellwig10257742010-06-04 11:30:02 +0200672 setattr_copy(inode, attr);
673 mark_inode_dirty(inode);
674 return 0;
Linus Torvalds6d76fa52006-07-15 12:26:45 -0700675}
676
Vasiliy Kulikov04996802012-01-10 15:11:31 -0800677/*
678 * May current process learn task's sched/cmdline info (for hide_pid_min=1)
679 * or euid/egid (for hide_pid_min=2)?
680 */
681static bool has_pid_permissions(struct pid_namespace *pid,
682 struct task_struct *task,
683 int hide_pid_min)
684{
685 if (pid->hide_pid < hide_pid_min)
686 return true;
687 if (in_group_p(pid->pid_gid))
688 return true;
Jann Horncaaee622016-01-20 15:00:04 -0800689 return ptrace_may_access(task, PTRACE_MODE_READ_FSCREDS);
Vasiliy Kulikov04996802012-01-10 15:11:31 -0800690}
691
692
693static int proc_pid_permission(struct inode *inode, int mask)
694{
Christoph Hellwig76f668b2018-05-16 07:19:01 +0200695 struct pid_namespace *pid = proc_pid_ns(inode);
Vasiliy Kulikov04996802012-01-10 15:11:31 -0800696 struct task_struct *task;
697 bool has_perms;
698
699 task = get_proc_task(inode);
Xiaotian Fenga2ef9902012-01-12 17:17:08 -0800700 if (!task)
701 return -ESRCH;
Lafcadio Wluiki796f5712017-02-24 15:00:23 -0800702 has_perms = has_pid_permissions(pid, task, HIDEPID_NO_ACCESS);
Vasiliy Kulikov04996802012-01-10 15:11:31 -0800703 put_task_struct(task);
704
705 if (!has_perms) {
Lafcadio Wluiki796f5712017-02-24 15:00:23 -0800706 if (pid->hide_pid == HIDEPID_INVISIBLE) {
Vasiliy Kulikov04996802012-01-10 15:11:31 -0800707 /*
708 * Let's make getdents(), stat(), and open()
709 * consistent with each other. If a process
710 * may not stat() a file, it shouldn't be seen
711 * in procfs at all.
712 */
713 return -ENOENT;
714 }
715
716 return -EPERM;
717 }
718 return generic_permission(inode, mask);
719}
720
721
722
Arjan van de Venc5ef1c42007-02-12 00:55:40 -0800723static const struct inode_operations proc_def_inode_operations = {
Linus Torvalds6d76fa52006-07-15 12:26:45 -0700724 .setattr = proc_setattr,
725};
726
Eric W. Biedermanbe614082008-02-08 04:18:30 -0800727static int proc_single_show(struct seq_file *m, void *v)
728{
729 struct inode *inode = m->private;
Christoph Hellwig76f668b2018-05-16 07:19:01 +0200730 struct pid_namespace *ns = proc_pid_ns(inode);
731 struct pid *pid = proc_pid(inode);
Eric W. Biedermanbe614082008-02-08 04:18:30 -0800732 struct task_struct *task;
733 int ret;
734
Eric W. Biedermanbe614082008-02-08 04:18:30 -0800735 task = get_pid_task(pid, PIDTYPE_PID);
736 if (!task)
737 return -ESRCH;
738
739 ret = PROC_I(inode)->op.proc_show(m, ns, pid, task);
740
741 put_task_struct(task);
742 return ret;
743}
744
745static int proc_single_open(struct inode *inode, struct file *filp)
746{
Jovi Zhangc6a34052011-01-12 17:00:34 -0800747 return single_open(filp, proc_single_show, inode);
Eric W. Biedermanbe614082008-02-08 04:18:30 -0800748}
749
750static const struct file_operations proc_single_file_operations = {
751 .open = proc_single_open,
752 .read = seq_read,
753 .llseek = seq_lseek,
754 .release = single_release,
755};
756
Oleg Nesterov5381e162014-10-09 15:25:24 -0700757
758struct mm_struct *proc_mem_open(struct inode *inode, unsigned int mode)
759{
760 struct task_struct *task = get_proc_task(inode);
761 struct mm_struct *mm = ERR_PTR(-ESRCH);
762
763 if (task) {
Jann Horncaaee622016-01-20 15:00:04 -0800764 mm = mm_access(task, mode | PTRACE_MODE_FSCREDS);
Oleg Nesterov5381e162014-10-09 15:25:24 -0700765 put_task_struct(task);
766
767 if (!IS_ERR_OR_NULL(mm)) {
768 /* ensure this mm_struct can't be freed */
Vegard Nossumf1f10072017-02-27 14:30:07 -0800769 mmgrab(mm);
Oleg Nesterov5381e162014-10-09 15:25:24 -0700770 /* but do not pin its memory */
771 mmput(mm);
772 }
773 }
774
775 return mm;
776}
777
Cong Wangb409e572012-05-31 16:26:17 -0700778static int __mem_open(struct inode *inode, struct file *file, unsigned int mode)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700779{
Oleg Nesterov5381e162014-10-09 15:25:24 -0700780 struct mm_struct *mm = proc_mem_open(inode, mode);
Linus Torvaldse2683372012-01-17 15:21:19 -0800781
782 if (IS_ERR(mm))
783 return PTR_ERR(mm);
784
Linus Torvaldse2683372012-01-17 15:21:19 -0800785 file->private_data = mm;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700786 return 0;
787}
788
Cong Wangb409e572012-05-31 16:26:17 -0700789static int mem_open(struct inode *inode, struct file *file)
790{
Djalal Harounibc452b42012-07-30 14:42:28 -0700791 int ret = __mem_open(inode, file, PTRACE_MODE_ATTACH);
792
793 /* OK to pass negative loff_t, we can catch out-of-range */
794 file->f_mode |= FMODE_UNSIGNED_OFFSET;
795
796 return ret;
Cong Wangb409e572012-05-31 16:26:17 -0700797}
798
Oleg Nesterov572d34b2012-01-31 17:14:54 +0100799static ssize_t mem_rw(struct file *file, char __user *buf,
800 size_t count, loff_t *ppos, int write)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700801{
Linus Torvaldse2683372012-01-17 15:21:19 -0800802 struct mm_struct *mm = file->private_data;
Oleg Nesterov572d34b2012-01-31 17:14:54 +0100803 unsigned long addr = *ppos;
804 ssize_t copied;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700805 char *page;
Linus Torvalds272ddc82016-10-24 19:00:44 -0700806 unsigned int flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700807
Linus Torvaldse2683372012-01-17 15:21:19 -0800808 if (!mm)
809 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700810
Michal Hocko0ee931c2017-09-13 16:28:29 -0700811 page = (char *)__get_free_page(GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700812 if (!page)
Linus Torvaldse2683372012-01-17 15:21:19 -0800813 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700814
Frederik Deweerdtf7ca54f2006-09-29 02:01:02 -0700815 copied = 0;
Vegard Nossum388f7932017-02-27 14:30:13 -0800816 if (!mmget_not_zero(mm))
Oleg Nesterov6d08f2c2012-01-31 17:15:11 +0100817 goto free;
818
Linus Torvaldsf511c0b2017-05-30 12:38:59 -0700819 flags = FOLL_FORCE | (write ? FOLL_WRITE : 0);
Lorenzo Stoakes6347e8d2016-10-13 01:20:19 +0100820
Linus Torvalds1da177e2005-04-16 15:20:36 -0700821 while (count > 0) {
Oleg Nesterov572d34b2012-01-31 17:14:54 +0100822 int this_len = min_t(int, count, PAGE_SIZE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700823
Oleg Nesterov572d34b2012-01-31 17:14:54 +0100824 if (write && copy_from_user(page, buf, this_len)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700825 copied = -EFAULT;
826 break;
827 }
Oleg Nesterov572d34b2012-01-31 17:14:54 +0100828
Lorenzo Stoakes6347e8d2016-10-13 01:20:19 +0100829 this_len = access_remote_vm(mm, addr, page, this_len, flags);
Oleg Nesterov572d34b2012-01-31 17:14:54 +0100830 if (!this_len) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700831 if (!copied)
832 copied = -EIO;
833 break;
834 }
Oleg Nesterov572d34b2012-01-31 17:14:54 +0100835
836 if (!write && copy_to_user(buf, page, this_len)) {
837 copied = -EFAULT;
838 break;
839 }
840
841 buf += this_len;
842 addr += this_len;
843 copied += this_len;
844 count -= this_len;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700845 }
Oleg Nesterov572d34b2012-01-31 17:14:54 +0100846 *ppos = addr;
KOSAKI Motohiro30cd8902011-05-26 16:25:52 -0700847
Oleg Nesterov6d08f2c2012-01-31 17:15:11 +0100848 mmput(mm);
849free:
KOSAKI Motohiro30cd8902011-05-26 16:25:52 -0700850 free_page((unsigned long) page);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700851 return copied;
852}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700853
Oleg Nesterov572d34b2012-01-31 17:14:54 +0100854static ssize_t mem_read(struct file *file, char __user *buf,
855 size_t count, loff_t *ppos)
856{
857 return mem_rw(file, buf, count, ppos, 0);
858}
859
860static ssize_t mem_write(struct file *file, const char __user *buf,
861 size_t count, loff_t *ppos)
862{
863 return mem_rw(file, (char __user*)buf, count, ppos, 1);
864}
865
Matt Mackall85863e42008-02-04 22:29:04 -0800866loff_t mem_lseek(struct file *file, loff_t offset, int orig)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700867{
868 switch (orig) {
869 case 0:
870 file->f_pos = offset;
871 break;
872 case 1:
873 file->f_pos += offset;
874 break;
875 default:
876 return -EINVAL;
877 }
878 force_successful_syscall_return();
879 return file->f_pos;
880}
881
Linus Torvaldse2683372012-01-17 15:21:19 -0800882static int mem_release(struct inode *inode, struct file *file)
883{
884 struct mm_struct *mm = file->private_data;
Oleg Nesterov71879d32012-01-31 17:14:38 +0100885 if (mm)
Oleg Nesterov6d08f2c2012-01-31 17:15:11 +0100886 mmdrop(mm);
Linus Torvaldse2683372012-01-17 15:21:19 -0800887 return 0;
888}
889
Arjan van de Ven00977a52007-02-12 00:55:34 -0800890static const struct file_operations proc_mem_operations = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700891 .llseek = mem_lseek,
892 .read = mem_read,
893 .write = mem_write,
894 .open = mem_open,
Linus Torvaldse2683372012-01-17 15:21:19 -0800895 .release = mem_release,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700896};
897
Cong Wangb409e572012-05-31 16:26:17 -0700898static int environ_open(struct inode *inode, struct file *file)
899{
900 return __mem_open(inode, file, PTRACE_MODE_READ);
901}
902
James Pearson315e28c2007-10-16 23:30:17 -0700903static ssize_t environ_read(struct file *file, char __user *buf,
904 size_t count, loff_t *ppos)
905{
James Pearson315e28c2007-10-16 23:30:17 -0700906 char *page;
907 unsigned long src = *ppos;
Cong Wangb409e572012-05-31 16:26:17 -0700908 int ret = 0;
909 struct mm_struct *mm = file->private_data;
Mateusz Guzika3b609e2016-01-20 15:01:05 -0800910 unsigned long env_start, env_end;
James Pearson315e28c2007-10-16 23:30:17 -0700911
Mathias Krause8148a732016-05-05 16:22:26 -0700912 /* Ensure the process spawned far enough to have an environment. */
913 if (!mm || !mm->env_end)
Cong Wangb409e572012-05-31 16:26:17 -0700914 return 0;
James Pearson315e28c2007-10-16 23:30:17 -0700915
Michal Hocko0ee931c2017-09-13 16:28:29 -0700916 page = (char *)__get_free_page(GFP_KERNEL);
James Pearson315e28c2007-10-16 23:30:17 -0700917 if (!page)
Cong Wangb409e572012-05-31 16:26:17 -0700918 return -ENOMEM;
James Pearson315e28c2007-10-16 23:30:17 -0700919
Al Virod6f64b82011-02-15 22:26:01 -0500920 ret = 0;
Vegard Nossum388f7932017-02-27 14:30:13 -0800921 if (!mmget_not_zero(mm))
Cong Wangb409e572012-05-31 16:26:17 -0700922 goto free;
Mateusz Guzika3b609e2016-01-20 15:01:05 -0800923
Yang Shi88aa7cc2018-06-07 17:05:28 -0700924 spin_lock(&mm->arg_lock);
Mateusz Guzika3b609e2016-01-20 15:01:05 -0800925 env_start = mm->env_start;
926 env_end = mm->env_end;
Yang Shi88aa7cc2018-06-07 17:05:28 -0700927 spin_unlock(&mm->arg_lock);
Mateusz Guzika3b609e2016-01-20 15:01:05 -0800928
James Pearson315e28c2007-10-16 23:30:17 -0700929 while (count > 0) {
Djalal Harounie8905ec2012-07-30 14:42:26 -0700930 size_t this_len, max_len;
931 int retval;
932
Mateusz Guzika3b609e2016-01-20 15:01:05 -0800933 if (src >= (env_end - env_start))
Djalal Harounie8905ec2012-07-30 14:42:26 -0700934 break;
James Pearson315e28c2007-10-16 23:30:17 -0700935
Mateusz Guzika3b609e2016-01-20 15:01:05 -0800936 this_len = env_end - (env_start + src);
James Pearson315e28c2007-10-16 23:30:17 -0700937
Djalal Harounie8905ec2012-07-30 14:42:26 -0700938 max_len = min_t(size_t, PAGE_SIZE, count);
939 this_len = min(max_len, this_len);
James Pearson315e28c2007-10-16 23:30:17 -0700940
Willy Tarreau7f7ccc22018-05-11 08:11:44 +0200941 retval = access_remote_vm(mm, (env_start + src), page, this_len, FOLL_ANON);
James Pearson315e28c2007-10-16 23:30:17 -0700942
943 if (retval <= 0) {
944 ret = retval;
945 break;
946 }
947
948 if (copy_to_user(buf, page, retval)) {
949 ret = -EFAULT;
950 break;
951 }
952
953 ret += retval;
954 src += retval;
955 buf += retval;
956 count -= retval;
957 }
958 *ppos = src;
James Pearson315e28c2007-10-16 23:30:17 -0700959 mmput(mm);
Cong Wangb409e572012-05-31 16:26:17 -0700960
961free:
James Pearson315e28c2007-10-16 23:30:17 -0700962 free_page((unsigned long) page);
James Pearson315e28c2007-10-16 23:30:17 -0700963 return ret;
964}
965
966static const struct file_operations proc_environ_operations = {
Cong Wangb409e572012-05-31 16:26:17 -0700967 .open = environ_open,
James Pearson315e28c2007-10-16 23:30:17 -0700968 .read = environ_read,
Arnd Bergmann87df8422010-03-17 23:06:02 +0100969 .llseek = generic_file_llseek,
Cong Wangb409e572012-05-31 16:26:17 -0700970 .release = mem_release,
James Pearson315e28c2007-10-16 23:30:17 -0700971};
972
Al Viroc5317162016-10-05 18:43:43 -0400973static int auxv_open(struct inode *inode, struct file *file)
974{
975 return __mem_open(inode, file, PTRACE_MODE_READ_FSCREDS);
976}
977
978static ssize_t auxv_read(struct file *file, char __user *buf,
979 size_t count, loff_t *ppos)
980{
981 struct mm_struct *mm = file->private_data;
982 unsigned int nwords = 0;
Leon Yu06b28492016-10-27 17:46:50 -0700983
984 if (!mm)
985 return 0;
Al Viroc5317162016-10-05 18:43:43 -0400986 do {
987 nwords += 2;
988 } while (mm->saved_auxv[nwords - 2] != 0); /* AT_NULL */
989 return simple_read_from_buffer(buf, count, ppos, mm->saved_auxv,
990 nwords * sizeof(mm->saved_auxv[0]));
991}
992
993static const struct file_operations proc_auxv_operations = {
994 .open = auxv_open,
995 .read = auxv_read,
996 .llseek = generic_file_llseek,
997 .release = mem_release,
998};
999
David Rientjesfa0cbbf2012-11-12 17:53:04 -08001000static ssize_t oom_adj_read(struct file *file, char __user *buf, size_t count,
1001 loff_t *ppos)
1002{
Al Viro496ad9a2013-01-23 17:07:38 -05001003 struct task_struct *task = get_proc_task(file_inode(file));
David Rientjesfa0cbbf2012-11-12 17:53:04 -08001004 char buffer[PROC_NUMBUF];
1005 int oom_adj = OOM_ADJUST_MIN;
1006 size_t len;
David Rientjesfa0cbbf2012-11-12 17:53:04 -08001007
1008 if (!task)
1009 return -ESRCH;
Michal Hockof913da52016-07-28 15:44:37 -07001010 if (task->signal->oom_score_adj == OOM_SCORE_ADJ_MAX)
1011 oom_adj = OOM_ADJUST_MAX;
1012 else
1013 oom_adj = (task->signal->oom_score_adj * -OOM_DISABLE) /
1014 OOM_SCORE_ADJ_MAX;
David Rientjesfa0cbbf2012-11-12 17:53:04 -08001015 put_task_struct(task);
1016 len = snprintf(buffer, sizeof(buffer), "%d\n", oom_adj);
1017 return simple_read_from_buffer(buf, count, ppos, buffer, len);
1018}
1019
Michal Hocko1d5f0ac2016-07-28 15:44:40 -07001020static int __set_oom_adj(struct file *file, int oom_adj, bool legacy)
1021{
1022 static DEFINE_MUTEX(oom_adj_mutex);
Michal Hocko44a70ade2016-07-28 15:44:43 -07001023 struct mm_struct *mm = NULL;
Michal Hocko1d5f0ac2016-07-28 15:44:40 -07001024 struct task_struct *task;
1025 int err = 0;
1026
1027 task = get_proc_task(file_inode(file));
1028 if (!task)
1029 return -ESRCH;
1030
1031 mutex_lock(&oom_adj_mutex);
1032 if (legacy) {
1033 if (oom_adj < task->signal->oom_score_adj &&
1034 !capable(CAP_SYS_RESOURCE)) {
1035 err = -EACCES;
1036 goto err_unlock;
1037 }
1038 /*
1039 * /proc/pid/oom_adj is provided for legacy purposes, ask users to use
1040 * /proc/pid/oom_score_adj instead.
1041 */
1042 pr_warn_once("%s (%d): /proc/%d/oom_adj is deprecated, please use /proc/%d/oom_score_adj instead.\n",
1043 current->comm, task_pid_nr(current), task_pid_nr(task),
1044 task_pid_nr(task));
1045 } else {
1046 if ((short)oom_adj < task->signal->oom_score_adj_min &&
1047 !capable(CAP_SYS_RESOURCE)) {
1048 err = -EACCES;
1049 goto err_unlock;
1050 }
1051 }
1052
Michal Hocko44a70ade2016-07-28 15:44:43 -07001053 /*
1054 * Make sure we will check other processes sharing the mm if this is
1055 * not vfrok which wants its own oom_score_adj.
1056 * pin the mm so it doesn't go away and get reused after task_unlock
1057 */
1058 if (!task->vfork_done) {
1059 struct task_struct *p = find_lock_task_mm(task);
1060
1061 if (p) {
1062 if (atomic_read(&p->mm->mm_users) > 1) {
1063 mm = p->mm;
Vegard Nossumf1f10072017-02-27 14:30:07 -08001064 mmgrab(mm);
Michal Hocko44a70ade2016-07-28 15:44:43 -07001065 }
1066 task_unlock(p);
1067 }
1068 }
1069
Michal Hocko1d5f0ac2016-07-28 15:44:40 -07001070 task->signal->oom_score_adj = oom_adj;
1071 if (!legacy && has_capability_noaudit(current, CAP_SYS_RESOURCE))
1072 task->signal->oom_score_adj_min = (short)oom_adj;
1073 trace_oom_score_adj_update(task);
Michal Hocko44a70ade2016-07-28 15:44:43 -07001074
1075 if (mm) {
1076 struct task_struct *p;
1077
1078 rcu_read_lock();
1079 for_each_process(p) {
1080 if (same_thread_group(task, p))
1081 continue;
1082
1083 /* do not touch kernel threads or the global init */
1084 if (p->flags & PF_KTHREAD || is_global_init(p))
1085 continue;
1086
1087 task_lock(p);
1088 if (!p->vfork_done && process_shares_mm(p, mm)) {
Michal Hocko44a70ade2016-07-28 15:44:43 -07001089 p->signal->oom_score_adj = oom_adj;
1090 if (!legacy && has_capability_noaudit(current, CAP_SYS_RESOURCE))
1091 p->signal->oom_score_adj_min = (short)oom_adj;
1092 }
1093 task_unlock(p);
1094 }
1095 rcu_read_unlock();
1096 mmdrop(mm);
1097 }
Michal Hocko1d5f0ac2016-07-28 15:44:40 -07001098err_unlock:
1099 mutex_unlock(&oom_adj_mutex);
1100 put_task_struct(task);
1101 return err;
1102}
Michal Hockof913da52016-07-28 15:44:37 -07001103
David Rientjesb72bdfa2015-11-05 18:50:32 -08001104/*
1105 * /proc/pid/oom_adj exists solely for backwards compatibility with previous
1106 * kernels. The effective policy is defined by oom_score_adj, which has a
1107 * different scale: oom_adj grew exponentially and oom_score_adj grows linearly.
1108 * Values written to oom_adj are simply mapped linearly to oom_score_adj.
1109 * Processes that become oom disabled via oom_adj will still be oom disabled
1110 * with this implementation.
1111 *
1112 * oom_adj cannot be removed since existing userspace binaries use it.
1113 */
David Rientjesfa0cbbf2012-11-12 17:53:04 -08001114static ssize_t oom_adj_write(struct file *file, const char __user *buf,
1115 size_t count, loff_t *ppos)
1116{
David Rientjesfa0cbbf2012-11-12 17:53:04 -08001117 char buffer[PROC_NUMBUF];
1118 int oom_adj;
David Rientjesfa0cbbf2012-11-12 17:53:04 -08001119 int err;
1120
1121 memset(buffer, 0, sizeof(buffer));
1122 if (count > sizeof(buffer) - 1)
1123 count = sizeof(buffer) - 1;
1124 if (copy_from_user(buffer, buf, count)) {
1125 err = -EFAULT;
1126 goto out;
1127 }
1128
1129 err = kstrtoint(strstrip(buffer), 0, &oom_adj);
1130 if (err)
1131 goto out;
1132 if ((oom_adj < OOM_ADJUST_MIN || oom_adj > OOM_ADJUST_MAX) &&
1133 oom_adj != OOM_DISABLE) {
1134 err = -EINVAL;
1135 goto out;
1136 }
1137
David Rientjesfa0cbbf2012-11-12 17:53:04 -08001138 /*
1139 * Scale /proc/pid/oom_score_adj appropriately ensuring that a maximum
1140 * value is always attainable.
1141 */
1142 if (oom_adj == OOM_ADJUST_MAX)
1143 oom_adj = OOM_SCORE_ADJ_MAX;
1144 else
1145 oom_adj = (oom_adj * OOM_SCORE_ADJ_MAX) / -OOM_DISABLE;
1146
Michal Hocko1d5f0ac2016-07-28 15:44:40 -07001147 err = __set_oom_adj(file, oom_adj, true);
David Rientjesfa0cbbf2012-11-12 17:53:04 -08001148out:
1149 return err < 0 ? err : count;
1150}
1151
1152static const struct file_operations proc_oom_adj_operations = {
1153 .read = oom_adj_read,
1154 .write = oom_adj_write,
1155 .llseek = generic_file_llseek,
1156};
1157
David Rientjesa63d83f2010-08-09 17:19:46 -07001158static ssize_t oom_score_adj_read(struct file *file, char __user *buf,
1159 size_t count, loff_t *ppos)
1160{
Al Viro496ad9a2013-01-23 17:07:38 -05001161 struct task_struct *task = get_proc_task(file_inode(file));
David Rientjesa63d83f2010-08-09 17:19:46 -07001162 char buffer[PROC_NUMBUF];
David Rientjesa9c58b902012-12-11 16:02:54 -08001163 short oom_score_adj = OOM_SCORE_ADJ_MIN;
David Rientjesa63d83f2010-08-09 17:19:46 -07001164 size_t len;
1165
1166 if (!task)
1167 return -ESRCH;
Michal Hockof913da52016-07-28 15:44:37 -07001168 oom_score_adj = task->signal->oom_score_adj;
David Rientjesa63d83f2010-08-09 17:19:46 -07001169 put_task_struct(task);
David Rientjesa9c58b902012-12-11 16:02:54 -08001170 len = snprintf(buffer, sizeof(buffer), "%hd\n", oom_score_adj);
David Rientjesa63d83f2010-08-09 17:19:46 -07001171 return simple_read_from_buffer(buf, count, ppos, buffer, len);
1172}
1173
1174static ssize_t oom_score_adj_write(struct file *file, const char __user *buf,
1175 size_t count, loff_t *ppos)
1176{
David Rientjesa63d83f2010-08-09 17:19:46 -07001177 char buffer[PROC_NUMBUF];
Alexey Dobriyan0a8cb8e2011-05-26 16:25:50 -07001178 int oom_score_adj;
David Rientjesa63d83f2010-08-09 17:19:46 -07001179 int err;
1180
1181 memset(buffer, 0, sizeof(buffer));
1182 if (count > sizeof(buffer) - 1)
1183 count = sizeof(buffer) - 1;
David Rientjes723548b2010-10-26 14:21:25 -07001184 if (copy_from_user(buffer, buf, count)) {
1185 err = -EFAULT;
1186 goto out;
1187 }
David Rientjesa63d83f2010-08-09 17:19:46 -07001188
Alexey Dobriyan0a8cb8e2011-05-26 16:25:50 -07001189 err = kstrtoint(strstrip(buffer), 0, &oom_score_adj);
David Rientjesa63d83f2010-08-09 17:19:46 -07001190 if (err)
David Rientjes723548b2010-10-26 14:21:25 -07001191 goto out;
David Rientjesa63d83f2010-08-09 17:19:46 -07001192 if (oom_score_adj < OOM_SCORE_ADJ_MIN ||
David Rientjes723548b2010-10-26 14:21:25 -07001193 oom_score_adj > OOM_SCORE_ADJ_MAX) {
1194 err = -EINVAL;
1195 goto out;
1196 }
David Rientjesa63d83f2010-08-09 17:19:46 -07001197
Michal Hocko1d5f0ac2016-07-28 15:44:40 -07001198 err = __set_oom_adj(file, oom_score_adj, false);
David Rientjes723548b2010-10-26 14:21:25 -07001199out:
1200 return err < 0 ? err : count;
David Rientjesa63d83f2010-08-09 17:19:46 -07001201}
1202
1203static const struct file_operations proc_oom_score_adj_operations = {
1204 .read = oom_score_adj_read,
1205 .write = oom_score_adj_write,
Arnd Bergmann6038f372010-08-15 18:52:59 +02001206 .llseek = default_llseek,
David Rientjesa63d83f2010-08-09 17:19:46 -07001207};
1208
Richard Guy Briggs4b7d2482019-01-22 17:06:39 -05001209#ifdef CONFIG_AUDIT
Alexey Dobriyanb4eb4f72016-10-29 19:04:39 +03001210#define TMPBUFLEN 11
Linus Torvalds1da177e2005-04-16 15:20:36 -07001211static ssize_t proc_loginuid_read(struct file * file, char __user * buf,
1212 size_t count, loff_t *ppos)
1213{
Al Viro496ad9a2013-01-23 17:07:38 -05001214 struct inode * inode = file_inode(file);
Eric W. Biederman99f89552006-06-26 00:25:55 -07001215 struct task_struct *task = get_proc_task(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001216 ssize_t length;
1217 char tmpbuf[TMPBUFLEN];
1218
Eric W. Biederman99f89552006-06-26 00:25:55 -07001219 if (!task)
1220 return -ESRCH;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001221 length = scnprintf(tmpbuf, TMPBUFLEN, "%u",
Eric W. Biedermane1760bd2012-09-10 22:39:43 -07001222 from_kuid(file->f_cred->user_ns,
1223 audit_get_loginuid(task)));
Eric W. Biederman99f89552006-06-26 00:25:55 -07001224 put_task_struct(task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001225 return simple_read_from_buffer(buf, count, ppos, tmpbuf, length);
1226}
1227
1228static ssize_t proc_loginuid_write(struct file * file, const char __user * buf,
1229 size_t count, loff_t *ppos)
1230{
Al Viro496ad9a2013-01-23 17:07:38 -05001231 struct inode * inode = file_inode(file);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001232 uid_t loginuid;
Eric W. Biedermane1760bd2012-09-10 22:39:43 -07001233 kuid_t kloginuid;
Alexey Dobriyan774636e2015-09-09 15:36:59 -07001234 int rv;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001235
Paul E. McKenney7dc52152010-02-22 17:04:52 -08001236 rcu_read_lock();
1237 if (current != pid_task(proc_pid(inode), PIDTYPE_PID)) {
1238 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001239 return -EPERM;
Paul E. McKenney7dc52152010-02-22 17:04:52 -08001240 }
1241 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001242
Linus Torvalds1da177e2005-04-16 15:20:36 -07001243 if (*ppos != 0) {
1244 /* No partial writes. */
1245 return -EINVAL;
1246 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001247
Alexey Dobriyan774636e2015-09-09 15:36:59 -07001248 rv = kstrtou32_from_user(buf, count, 10, &loginuid);
1249 if (rv < 0)
1250 return rv;
Eric Paris81407c82013-05-24 09:49:14 -04001251
1252 /* is userspace tring to explicitly UNSET the loginuid? */
1253 if (loginuid == AUDIT_UID_UNSET) {
1254 kloginuid = INVALID_UID;
1255 } else {
1256 kloginuid = make_kuid(file->f_cred->user_ns, loginuid);
Alexey Dobriyan774636e2015-09-09 15:36:59 -07001257 if (!uid_valid(kloginuid))
1258 return -EINVAL;
Eric W. Biedermane1760bd2012-09-10 22:39:43 -07001259 }
1260
Alexey Dobriyan774636e2015-09-09 15:36:59 -07001261 rv = audit_set_loginuid(kloginuid);
1262 if (rv < 0)
1263 return rv;
1264 return count;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001265}
1266
Arjan van de Ven00977a52007-02-12 00:55:34 -08001267static const struct file_operations proc_loginuid_operations = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001268 .read = proc_loginuid_read,
1269 .write = proc_loginuid_write,
Arnd Bergmann87df8422010-03-17 23:06:02 +01001270 .llseek = generic_file_llseek,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001271};
Eric Paris1e0bd752008-03-13 08:15:31 -04001272
1273static ssize_t proc_sessionid_read(struct file * file, char __user * buf,
1274 size_t count, loff_t *ppos)
1275{
Al Viro496ad9a2013-01-23 17:07:38 -05001276 struct inode * inode = file_inode(file);
Eric Paris1e0bd752008-03-13 08:15:31 -04001277 struct task_struct *task = get_proc_task(inode);
1278 ssize_t length;
1279 char tmpbuf[TMPBUFLEN];
1280
1281 if (!task)
1282 return -ESRCH;
1283 length = scnprintf(tmpbuf, TMPBUFLEN, "%u",
1284 audit_get_sessionid(task));
1285 put_task_struct(task);
1286 return simple_read_from_buffer(buf, count, ppos, tmpbuf, length);
1287}
1288
1289static const struct file_operations proc_sessionid_operations = {
1290 .read = proc_sessionid_read,
Arnd Bergmann87df8422010-03-17 23:06:02 +01001291 .llseek = generic_file_llseek,
Eric Paris1e0bd752008-03-13 08:15:31 -04001292};
Linus Torvalds1da177e2005-04-16 15:20:36 -07001293#endif
1294
Akinobu Mitaf4f154f2006-12-08 02:39:47 -08001295#ifdef CONFIG_FAULT_INJECTION
1296static ssize_t proc_fault_inject_read(struct file * file, char __user * buf,
1297 size_t count, loff_t *ppos)
1298{
Al Viro496ad9a2013-01-23 17:07:38 -05001299 struct task_struct *task = get_proc_task(file_inode(file));
Akinobu Mitaf4f154f2006-12-08 02:39:47 -08001300 char buffer[PROC_NUMBUF];
1301 size_t len;
1302 int make_it_fail;
Akinobu Mitaf4f154f2006-12-08 02:39:47 -08001303
1304 if (!task)
1305 return -ESRCH;
1306 make_it_fail = task->make_it_fail;
1307 put_task_struct(task);
1308
1309 len = snprintf(buffer, sizeof(buffer), "%i\n", make_it_fail);
Akinobu Mita0c28f282007-05-08 00:31:41 -07001310
1311 return simple_read_from_buffer(buf, count, ppos, buffer, len);
Akinobu Mitaf4f154f2006-12-08 02:39:47 -08001312}
1313
1314static ssize_t proc_fault_inject_write(struct file * file,
1315 const char __user * buf, size_t count, loff_t *ppos)
1316{
1317 struct task_struct *task;
Alexey Dobriyan774636e2015-09-09 15:36:59 -07001318 char buffer[PROC_NUMBUF];
Akinobu Mitaf4f154f2006-12-08 02:39:47 -08001319 int make_it_fail;
Alexey Dobriyan774636e2015-09-09 15:36:59 -07001320 int rv;
Akinobu Mitaf4f154f2006-12-08 02:39:47 -08001321
1322 if (!capable(CAP_SYS_RESOURCE))
1323 return -EPERM;
1324 memset(buffer, 0, sizeof(buffer));
1325 if (count > sizeof(buffer) - 1)
1326 count = sizeof(buffer) - 1;
1327 if (copy_from_user(buffer, buf, count))
1328 return -EFAULT;
Alexey Dobriyan774636e2015-09-09 15:36:59 -07001329 rv = kstrtoint(strstrip(buffer), 0, &make_it_fail);
1330 if (rv < 0)
1331 return rv;
Dave Jones16caed32014-04-07 15:39:15 -07001332 if (make_it_fail < 0 || make_it_fail > 1)
1333 return -EINVAL;
1334
Al Viro496ad9a2013-01-23 17:07:38 -05001335 task = get_proc_task(file_inode(file));
Akinobu Mitaf4f154f2006-12-08 02:39:47 -08001336 if (!task)
1337 return -ESRCH;
1338 task->make_it_fail = make_it_fail;
1339 put_task_struct(task);
Vincent Licba8aaf2009-09-22 16:45:38 -07001340
1341 return count;
Akinobu Mitaf4f154f2006-12-08 02:39:47 -08001342}
1343
Arjan van de Ven00977a52007-02-12 00:55:34 -08001344static const struct file_operations proc_fault_inject_operations = {
Akinobu Mitaf4f154f2006-12-08 02:39:47 -08001345 .read = proc_fault_inject_read,
1346 .write = proc_fault_inject_write,
Arnd Bergmann87df8422010-03-17 23:06:02 +01001347 .llseek = generic_file_llseek,
Akinobu Mitaf4f154f2006-12-08 02:39:47 -08001348};
Dmitry Vyukove41d58182017-07-12 14:34:35 -07001349
1350static ssize_t proc_fail_nth_write(struct file *file, const char __user *buf,
1351 size_t count, loff_t *ppos)
1352{
1353 struct task_struct *task;
Akinobu Mita9049f2f2017-07-14 14:49:52 -07001354 int err;
1355 unsigned int n;
Dmitry Vyukove41d58182017-07-12 14:34:35 -07001356
Akinobu Mita9049f2f2017-07-14 14:49:52 -07001357 err = kstrtouint_from_user(buf, count, 0, &n);
Dmitry Vyukove41d58182017-07-12 14:34:35 -07001358 if (err)
1359 return err;
Akinobu Mita1203c8e2017-07-14 14:49:57 -07001360
1361 task = get_proc_task(file_inode(file));
1362 if (!task)
1363 return -ESRCH;
Alexey Dobriyan9f7118b2018-02-06 15:36:55 -08001364 task->fail_nth = n;
Akinobu Mita1203c8e2017-07-14 14:49:57 -07001365 put_task_struct(task);
1366
Dmitry Vyukove41d58182017-07-12 14:34:35 -07001367 return count;
1368}
1369
1370static ssize_t proc_fail_nth_read(struct file *file, char __user *buf,
1371 size_t count, loff_t *ppos)
1372{
1373 struct task_struct *task;
Akinobu Mitabfc74092017-07-14 14:49:54 -07001374 char numbuf[PROC_NUMBUF];
1375 ssize_t len;
Dmitry Vyukove41d58182017-07-12 14:34:35 -07001376
1377 task = get_proc_task(file_inode(file));
1378 if (!task)
1379 return -ESRCH;
Alexey Dobriyan9f7118b2018-02-06 15:36:55 -08001380 len = snprintf(numbuf, sizeof(numbuf), "%u\n", task->fail_nth);
Akinobu Mita1203c8e2017-07-14 14:49:57 -07001381 put_task_struct(task);
Alexey Dobriyana44937f2018-08-21 21:54:27 -07001382 return simple_read_from_buffer(buf, count, ppos, numbuf, len);
Dmitry Vyukove41d58182017-07-12 14:34:35 -07001383}
1384
1385static const struct file_operations proc_fail_nth_operations = {
1386 .read = proc_fail_nth_read,
1387 .write = proc_fail_nth_write,
1388};
Akinobu Mitaf4f154f2006-12-08 02:39:47 -08001389#endif
1390
Arjan van de Ven97455122008-01-25 21:08:34 +01001391
Ingo Molnar43ae34c2007-07-09 18:52:00 +02001392#ifdef CONFIG_SCHED_DEBUG
1393/*
1394 * Print out various scheduling related per-task fields:
1395 */
1396static int sched_show(struct seq_file *m, void *v)
1397{
1398 struct inode *inode = m->private;
Christoph Hellwig76f668b2018-05-16 07:19:01 +02001399 struct pid_namespace *ns = proc_pid_ns(inode);
Ingo Molnar43ae34c2007-07-09 18:52:00 +02001400 struct task_struct *p;
1401
Ingo Molnar43ae34c2007-07-09 18:52:00 +02001402 p = get_proc_task(inode);
1403 if (!p)
1404 return -ESRCH;
Aleksa Sarai74dc3382017-08-06 14:41:41 +10001405 proc_sched_show_task(p, ns, m);
Ingo Molnar43ae34c2007-07-09 18:52:00 +02001406
1407 put_task_struct(p);
1408
1409 return 0;
1410}
1411
1412static ssize_t
1413sched_write(struct file *file, const char __user *buf,
1414 size_t count, loff_t *offset)
1415{
Al Viro496ad9a2013-01-23 17:07:38 -05001416 struct inode *inode = file_inode(file);
Ingo Molnar43ae34c2007-07-09 18:52:00 +02001417 struct task_struct *p;
1418
Ingo Molnar43ae34c2007-07-09 18:52:00 +02001419 p = get_proc_task(inode);
1420 if (!p)
1421 return -ESRCH;
1422 proc_sched_set_task(p);
1423
1424 put_task_struct(p);
1425
1426 return count;
1427}
1428
1429static int sched_open(struct inode *inode, struct file *filp)
1430{
Jovi Zhangc6a34052011-01-12 17:00:34 -08001431 return single_open(filp, sched_show, inode);
Ingo Molnar43ae34c2007-07-09 18:52:00 +02001432}
1433
1434static const struct file_operations proc_pid_sched_operations = {
1435 .open = sched_open,
1436 .read = seq_read,
1437 .write = sched_write,
1438 .llseek = seq_lseek,
Alexey Dobriyan5ea473a2007-07-31 00:38:50 -07001439 .release = single_release,
Ingo Molnar43ae34c2007-07-09 18:52:00 +02001440};
1441
1442#endif
1443
Mike Galbraith5091faa2010-11-30 14:18:03 +01001444#ifdef CONFIG_SCHED_AUTOGROUP
1445/*
1446 * Print out autogroup related information:
1447 */
1448static int sched_autogroup_show(struct seq_file *m, void *v)
1449{
1450 struct inode *inode = m->private;
1451 struct task_struct *p;
1452
1453 p = get_proc_task(inode);
1454 if (!p)
1455 return -ESRCH;
1456 proc_sched_autogroup_show_task(p, m);
1457
1458 put_task_struct(p);
1459
1460 return 0;
1461}
1462
1463static ssize_t
1464sched_autogroup_write(struct file *file, const char __user *buf,
1465 size_t count, loff_t *offset)
1466{
Al Viro496ad9a2013-01-23 17:07:38 -05001467 struct inode *inode = file_inode(file);
Mike Galbraith5091faa2010-11-30 14:18:03 +01001468 struct task_struct *p;
1469 char buffer[PROC_NUMBUF];
Alexey Dobriyan0a8cb8e2011-05-26 16:25:50 -07001470 int nice;
Mike Galbraith5091faa2010-11-30 14:18:03 +01001471 int err;
1472
1473 memset(buffer, 0, sizeof(buffer));
1474 if (count > sizeof(buffer) - 1)
1475 count = sizeof(buffer) - 1;
1476 if (copy_from_user(buffer, buf, count))
1477 return -EFAULT;
1478
Alexey Dobriyan0a8cb8e2011-05-26 16:25:50 -07001479 err = kstrtoint(strstrip(buffer), 0, &nice);
1480 if (err < 0)
1481 return err;
Mike Galbraith5091faa2010-11-30 14:18:03 +01001482
1483 p = get_proc_task(inode);
1484 if (!p)
1485 return -ESRCH;
1486
Hiroshi Shimamoto2e5b5b32012-02-23 17:41:27 +09001487 err = proc_sched_autogroup_set_nice(p, nice);
Mike Galbraith5091faa2010-11-30 14:18:03 +01001488 if (err)
1489 count = err;
1490
1491 put_task_struct(p);
1492
1493 return count;
1494}
1495
1496static int sched_autogroup_open(struct inode *inode, struct file *filp)
1497{
1498 int ret;
1499
1500 ret = single_open(filp, sched_autogroup_show, NULL);
1501 if (!ret) {
1502 struct seq_file *m = filp->private_data;
1503
1504 m->private = inode;
1505 }
1506 return ret;
1507}
1508
1509static const struct file_operations proc_pid_sched_autogroup_operations = {
1510 .open = sched_autogroup_open,
1511 .read = seq_read,
1512 .write = sched_autogroup_write,
1513 .llseek = seq_lseek,
1514 .release = single_release,
1515};
1516
1517#endif /* CONFIG_SCHED_AUTOGROUP */
1518
john stultz4614a696b2009-12-14 18:00:05 -08001519static ssize_t comm_write(struct file *file, const char __user *buf,
1520 size_t count, loff_t *offset)
1521{
Al Viro496ad9a2013-01-23 17:07:38 -05001522 struct inode *inode = file_inode(file);
john stultz4614a696b2009-12-14 18:00:05 -08001523 struct task_struct *p;
1524 char buffer[TASK_COMM_LEN];
David Rientjes830e0fc2013-04-30 15:28:18 -07001525 const size_t maxlen = sizeof(buffer) - 1;
john stultz4614a696b2009-12-14 18:00:05 -08001526
1527 memset(buffer, 0, sizeof(buffer));
David Rientjes830e0fc2013-04-30 15:28:18 -07001528 if (copy_from_user(buffer, buf, count > maxlen ? maxlen : count))
john stultz4614a696b2009-12-14 18:00:05 -08001529 return -EFAULT;
1530
1531 p = get_proc_task(inode);
1532 if (!p)
1533 return -ESRCH;
1534
1535 if (same_thread_group(current, p))
1536 set_task_comm(p, buffer);
1537 else
1538 count = -EINVAL;
1539
1540 put_task_struct(p);
1541
1542 return count;
1543}
1544
1545static int comm_show(struct seq_file *m, void *v)
1546{
1547 struct inode *inode = m->private;
1548 struct task_struct *p;
1549
1550 p = get_proc_task(inode);
1551 if (!p)
1552 return -ESRCH;
1553
Tejun Heo88b72b32018-05-18 08:47:13 -07001554 proc_task_name(m, p, false);
1555 seq_putc(m, '\n');
john stultz4614a696b2009-12-14 18:00:05 -08001556
1557 put_task_struct(p);
1558
1559 return 0;
1560}
1561
1562static int comm_open(struct inode *inode, struct file *filp)
1563{
Jovi Zhangc6a34052011-01-12 17:00:34 -08001564 return single_open(filp, comm_show, inode);
john stultz4614a696b2009-12-14 18:00:05 -08001565}
1566
1567static const struct file_operations proc_pid_set_comm_operations = {
1568 .open = comm_open,
1569 .read = seq_read,
1570 .write = comm_write,
1571 .llseek = seq_lseek,
1572 .release = single_release,
1573};
1574
Cyrill Gorcunov7773fbc2012-01-10 15:11:20 -08001575static int proc_exe_link(struct dentry *dentry, struct path *exe_path)
Matt Helsley925d1c42008-04-29 01:01:36 -07001576{
1577 struct task_struct *task;
Matt Helsley925d1c42008-04-29 01:01:36 -07001578 struct file *exe_file;
1579
David Howells2b0143b2015-03-17 22:25:59 +00001580 task = get_proc_task(d_inode(dentry));
Matt Helsley925d1c42008-04-29 01:01:36 -07001581 if (!task)
1582 return -ENOENT;
Mateusz Guzikcd81a9172016-08-23 16:20:38 +02001583 exe_file = get_task_exe_file(task);
Matt Helsley925d1c42008-04-29 01:01:36 -07001584 put_task_struct(task);
Matt Helsley925d1c42008-04-29 01:01:36 -07001585 if (exe_file) {
1586 *exe_path = exe_file->f_path;
1587 path_get(&exe_file->f_path);
1588 fput(exe_file);
1589 return 0;
1590 } else
1591 return -ENOENT;
1592}
1593
Al Viro6b255392015-11-17 10:20:54 -05001594static const char *proc_pid_get_link(struct dentry *dentry,
Al Virofceef392015-12-29 15:58:39 -05001595 struct inode *inode,
1596 struct delayed_call *done)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001597{
Christoph Hellwig408ef012012-06-18 10:47:03 -04001598 struct path path;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001599 int error = -EACCES;
1600
Al Viro6b255392015-11-17 10:20:54 -05001601 if (!dentry)
1602 return ERR_PTR(-ECHILD);
1603
Eric W. Biederman778c1142006-06-26 00:25:58 -07001604 /* Are we allowed to snoop on the tasks file descriptors? */
1605 if (!proc_fd_access_allowed(inode))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001606 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001607
Christoph Hellwig408ef012012-06-18 10:47:03 -04001608 error = PROC_I(inode)->op.proc_get_link(dentry, &path);
1609 if (error)
1610 goto out;
1611
Al Viro6e77137b2015-05-02 13:37:52 -04001612 nd_jump_link(&path);
Christoph Hellwig408ef012012-06-18 10:47:03 -04001613 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001614out:
Al Viro008b1502005-08-20 00:17:39 +01001615 return ERR_PTR(error);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001616}
1617
Jan Blunck3dcd25f2008-02-14 19:38:35 -08001618static int do_proc_readlink(struct path *path, char __user *buffer, int buflen)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001619{
Michal Hocko0ee931c2017-09-13 16:28:29 -07001620 char *tmp = (char *)__get_free_page(GFP_KERNEL);
Jan Blunck3dcd25f2008-02-14 19:38:35 -08001621 char *pathname;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001622 int len;
1623
1624 if (!tmp)
1625 return -ENOMEM;
Akinobu Mita0c28f282007-05-08 00:31:41 -07001626
Eric W. Biederman7b2a69b2010-12-05 15:51:21 -08001627 pathname = d_path(path, tmp, PAGE_SIZE);
Jan Blunck3dcd25f2008-02-14 19:38:35 -08001628 len = PTR_ERR(pathname);
1629 if (IS_ERR(pathname))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001630 goto out;
Jan Blunck3dcd25f2008-02-14 19:38:35 -08001631 len = tmp + PAGE_SIZE - 1 - pathname;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001632
1633 if (len > buflen)
1634 len = buflen;
Jan Blunck3dcd25f2008-02-14 19:38:35 -08001635 if (copy_to_user(buffer, pathname, len))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001636 len = -EFAULT;
1637 out:
1638 free_page((unsigned long)tmp);
1639 return len;
1640}
1641
1642static int proc_pid_readlink(struct dentry * dentry, char __user * buffer, int buflen)
1643{
1644 int error = -EACCES;
David Howells2b0143b2015-03-17 22:25:59 +00001645 struct inode *inode = d_inode(dentry);
Jan Blunck3dcd25f2008-02-14 19:38:35 -08001646 struct path path;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001647
Eric W. Biederman778c1142006-06-26 00:25:58 -07001648 /* Are we allowed to snoop on the tasks file descriptors? */
1649 if (!proc_fd_access_allowed(inode))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001650 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001651
Cyrill Gorcunov7773fbc2012-01-10 15:11:20 -08001652 error = PROC_I(inode)->op.proc_get_link(dentry, &path);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001653 if (error)
1654 goto out;
1655
Jan Blunck3dcd25f2008-02-14 19:38:35 -08001656 error = do_proc_readlink(&path, buffer, buflen);
1657 path_put(&path);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001658out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001659 return error;
1660}
1661
Cyrill Gorcunovfaf60af2012-08-23 14:43:24 +04001662const struct inode_operations proc_pid_link_inode_operations = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001663 .readlink = proc_pid_readlink,
Al Viro6b255392015-11-17 10:20:54 -05001664 .get_link = proc_pid_get_link,
Linus Torvalds6d76fa52006-07-15 12:26:45 -07001665 .setattr = proc_setattr,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001666};
1667
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001668
1669/* building an inode */
1670
Al Viroc6eb50d2017-09-30 14:45:42 -04001671void task_dump_owner(struct task_struct *task, umode_t mode,
Eric W. Biederman68eb94f2017-01-03 10:23:11 +13001672 kuid_t *ruid, kgid_t *rgid)
1673{
1674 /* Depending on the state of dumpable compute who should own a
1675 * proc file for a task.
1676 */
1677 const struct cred *cred;
1678 kuid_t uid;
1679 kgid_t gid;
1680
Alexey Dobriyan2e0ad552018-04-20 14:56:03 -07001681 if (unlikely(task->flags & PF_KTHREAD)) {
1682 *ruid = GLOBAL_ROOT_UID;
1683 *rgid = GLOBAL_ROOT_GID;
1684 return;
1685 }
1686
Eric W. Biederman68eb94f2017-01-03 10:23:11 +13001687 /* Default to the tasks effective ownership */
1688 rcu_read_lock();
1689 cred = __task_cred(task);
1690 uid = cred->euid;
1691 gid = cred->egid;
1692 rcu_read_unlock();
1693
1694 /*
1695 * Before the /proc/pid/status file was created the only way to read
1696 * the effective uid of a /process was to stat /proc/pid. Reading
1697 * /proc/pid/status is slow enough that procps and other packages
1698 * kept stating /proc/pid. To keep the rules in /proc simple I have
1699 * made this apply to all per process world readable and executable
1700 * directories.
1701 */
1702 if (mode != (S_IFDIR|S_IRUGO|S_IXUGO)) {
1703 struct mm_struct *mm;
1704 task_lock(task);
1705 mm = task->mm;
1706 /* Make non-dumpable tasks owned by some root */
1707 if (mm) {
1708 if (get_dumpable(mm) != SUID_DUMP_USER) {
1709 struct user_namespace *user_ns = mm->user_ns;
1710
1711 uid = make_kuid(user_ns, 0);
1712 if (!uid_valid(uid))
1713 uid = GLOBAL_ROOT_UID;
1714
1715 gid = make_kgid(user_ns, 0);
1716 if (!gid_valid(gid))
1717 gid = GLOBAL_ROOT_GID;
1718 }
1719 } else {
1720 uid = GLOBAL_ROOT_UID;
1721 gid = GLOBAL_ROOT_GID;
1722 }
1723 task_unlock(task);
1724 }
1725 *ruid = uid;
1726 *rgid = gid;
1727}
1728
Andreas Gruenbacherdb978da2016-11-10 22:18:28 +01001729struct inode *proc_pid_make_inode(struct super_block * sb,
1730 struct task_struct *task, umode_t mode)
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001731{
1732 struct inode * inode;
1733 struct proc_inode *ei;
1734
1735 /* We need a new inode */
1736
1737 inode = new_inode(sb);
1738 if (!inode)
1739 goto out;
1740
1741 /* Common stuff */
1742 ei = PROC_I(inode);
Andreas Gruenbacherdb978da2016-11-10 22:18:28 +01001743 inode->i_mode = mode;
Christoph Hellwig85fe4022010-10-23 11:19:54 -04001744 inode->i_ino = get_next_ino();
Deepa Dinamani078cd822016-09-14 07:48:04 -07001745 inode->i_mtime = inode->i_atime = inode->i_ctime = current_time(inode);
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001746 inode->i_op = &proc_def_inode_operations;
1747
1748 /*
1749 * grab the reference to task.
1750 */
Oleg Nesterov1a657f782006-10-02 02:18:59 -07001751 ei->pid = get_task_pid(task, PIDTYPE_PID);
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001752 if (!ei->pid)
1753 goto out_unlock;
1754
Eric W. Biederman68eb94f2017-01-03 10:23:11 +13001755 task_dump_owner(task, 0, &inode->i_uid, &inode->i_gid);
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001756 security_task_to_inode(task, inode);
1757
1758out:
1759 return inode;
1760
1761out_unlock:
1762 iput(inode);
1763 return NULL;
1764}
1765
David Howellsa528d352017-01-31 16:46:22 +00001766int pid_getattr(const struct path *path, struct kstat *stat,
1767 u32 request_mask, unsigned int query_flags)
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001768{
David Howellsa528d352017-01-31 16:46:22 +00001769 struct inode *inode = d_inode(path->dentry);
Christoph Hellwig76f668b2018-05-16 07:19:01 +02001770 struct pid_namespace *pid = proc_pid_ns(inode);
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001771 struct task_struct *task;
David Howellsc69e8d92008-11-14 10:39:19 +11001772
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001773 generic_fillattr(inode, stat);
1774
Eric W. Biedermandcb0f222012-02-09 08:48:21 -08001775 stat->uid = GLOBAL_ROOT_UID;
1776 stat->gid = GLOBAL_ROOT_GID;
Alexey Dobriyan94116922018-06-07 17:10:07 -07001777 rcu_read_lock();
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001778 task = pid_task(proc_pid(inode), PIDTYPE_PID);
1779 if (task) {
Lafcadio Wluiki796f5712017-02-24 15:00:23 -08001780 if (!has_pid_permissions(pid, task, HIDEPID_INVISIBLE)) {
Vasiliy Kulikov04996802012-01-10 15:11:31 -08001781 rcu_read_unlock();
1782 /*
1783 * This doesn't prevent learning whether PID exists,
1784 * it only makes getattr() consistent with readdir().
1785 */
1786 return -ENOENT;
1787 }
Eric W. Biederman68eb94f2017-01-03 10:23:11 +13001788 task_dump_owner(task, inode->i_mode, &stat->uid, &stat->gid);
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001789 }
1790 rcu_read_unlock();
1791 return 0;
1792}
1793
1794/* dentry stuff */
1795
1796/*
Al Viro1bbc5512018-05-02 21:26:16 -04001797 * Set <pid>/... inode ownership (can change due to setuid(), etc.)
1798 */
1799void pid_update_inode(struct task_struct *task, struct inode *inode)
1800{
1801 task_dump_owner(task, inode->i_mode, &inode->i_uid, &inode->i_gid);
1802
1803 inode->i_mode &= ~(S_ISUID | S_ISGID);
1804 security_task_to_inode(task, inode);
1805}
1806
1807/*
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001808 * Rewrite the inode's ownerships here because the owning task may have
1809 * performed a setuid(), etc.
1810 *
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001811 */
Al Viro1bbc5512018-05-02 21:26:16 -04001812static int pid_revalidate(struct dentry *dentry, unsigned int flags)
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001813{
Nick Piggin34286d62011-01-07 17:49:57 +11001814 struct inode *inode;
1815 struct task_struct *task;
David Howellsc69e8d92008-11-14 10:39:19 +11001816
Al Viro0b728e12012-06-10 16:03:43 -04001817 if (flags & LOOKUP_RCU)
Nick Piggin34286d62011-01-07 17:49:57 +11001818 return -ECHILD;
1819
David Howells2b0143b2015-03-17 22:25:59 +00001820 inode = d_inode(dentry);
Nick Piggin34286d62011-01-07 17:49:57 +11001821 task = get_proc_task(inode);
1822
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001823 if (task) {
Al Viro1bbc5512018-05-02 21:26:16 -04001824 pid_update_inode(task, inode);
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001825 put_task_struct(task);
1826 return 1;
1827 }
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001828 return 0;
1829}
1830
Oleg Nesterovd855a4b2014-01-23 15:55:39 -08001831static inline bool proc_inode_is_dead(struct inode *inode)
1832{
1833 return !proc_pid(inode)->tasks[PIDTYPE_PID].first;
1834}
1835
David Howells1dd704b2013-04-12 01:08:50 +01001836int pid_delete_dentry(const struct dentry *dentry)
1837{
1838 /* Is the task we represent dead?
1839 * If so, then don't put the dentry on the lru list,
1840 * kill it immediately.
1841 */
David Howells2b0143b2015-03-17 22:25:59 +00001842 return proc_inode_is_dead(d_inode(dentry));
David Howells1dd704b2013-04-12 01:08:50 +01001843}
1844
Eric W. Biederman6b4e3062010-03-07 16:41:34 -08001845const struct dentry_operations pid_dentry_operations =
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001846{
1847 .d_revalidate = pid_revalidate,
1848 .d_delete = pid_delete_dentry,
1849};
1850
1851/* Lookups */
1852
Eric W. Biederman1c0d04c2006-10-02 02:18:57 -07001853/*
1854 * Fill a directory entry.
1855 *
1856 * If possible create the dcache entry and derive our inode number and
1857 * file type from dcache entry.
1858 *
1859 * Since all of the proc inode numbers are dynamically generated, the inode
1860 * numbers do not exist until the inode is cache. This means creating the
1861 * the dcache entry in readdir is necessary to keep the inode numbers
1862 * reported by readdir in sync with the inode numbers reported
1863 * by stat.
1864 */
Al Virof0c3b502013-05-16 12:07:31 -04001865bool proc_fill_cache(struct file *file, struct dir_context *ctx,
Alexey Dobriyana4ef3892018-06-07 17:10:10 -07001866 const char *name, unsigned int len,
Eric Dumazetc5141e62007-05-08 00:26:15 -07001867 instantiate_t instantiate, struct task_struct *task, const void *ptr)
Eric W. Biederman61a28782006-10-02 02:18:49 -07001868{
Al Virof0c3b502013-05-16 12:07:31 -04001869 struct dentry *child, *dir = file->f_path.dentry;
Al Viro1df98b82013-06-15 11:33:10 +04001870 struct qstr qname = QSTR_INIT(name, len);
Eric W. Biederman61a28782006-10-02 02:18:49 -07001871 struct inode *inode;
Al Viro0168b9e2018-05-03 09:21:05 -04001872 unsigned type = DT_UNKNOWN;
1873 ino_t ino = 1;
Eric W. Biederman61a28782006-10-02 02:18:49 -07001874
Al Viro1df98b82013-06-15 11:33:10 +04001875 child = d_hash_and_lookup(dir, &qname);
Eric W. Biederman61a28782006-10-02 02:18:49 -07001876 if (!child) {
Al Viro37817642016-04-20 16:31:31 -04001877 DECLARE_WAIT_QUEUE_HEAD_ONSTACK(wq);
1878 child = d_alloc_parallel(dir, &qname, &wq);
1879 if (IS_ERR(child))
Al Viro1df98b82013-06-15 11:33:10 +04001880 goto end_instantiate;
Al Viro37817642016-04-20 16:31:31 -04001881 if (d_in_lookup(child)) {
Al Viro0168b9e2018-05-03 09:21:05 -04001882 struct dentry *res;
1883 res = instantiate(child, task, ptr);
Al Viro37817642016-04-20 16:31:31 -04001884 d_lookup_done(child);
Al Viro0168b9e2018-05-03 09:21:05 -04001885 if (unlikely(res)) {
1886 dput(child);
1887 child = res;
Al Virod85b3992018-06-08 01:17:11 -04001888 if (IS_ERR(child))
1889 goto end_instantiate;
Al Viro37817642016-04-20 16:31:31 -04001890 }
Eric W. Biederman61a28782006-10-02 02:18:49 -07001891 }
1892 }
David Howells2b0143b2015-03-17 22:25:59 +00001893 inode = d_inode(child);
Al Viro147ce692013-06-15 10:26:35 +04001894 ino = inode->i_ino;
1895 type = inode->i_mode >> 12;
Eric W. Biederman61a28782006-10-02 02:18:49 -07001896 dput(child);
Al Virod85b3992018-06-08 01:17:11 -04001897end_instantiate:
Al Virof0c3b502013-05-16 12:07:31 -04001898 return dir_emit(ctx, name, len, ino, type);
Eric W. Biederman61a28782006-10-02 02:18:49 -07001899}
1900
Pavel Emelyanov640708a2012-01-10 15:11:23 -08001901/*
1902 * dname_to_vma_addr - maps a dentry name into two unsigned longs
1903 * which represent vma start and end addresses.
1904 */
1905static int dname_to_vma_addr(struct dentry *dentry,
1906 unsigned long *start, unsigned long *end)
1907{
Alexey Dobriyanac7f1062018-02-06 15:36:59 -08001908 const char *str = dentry->d_name.name;
1909 unsigned long long sval, eval;
1910 unsigned int len;
1911
Alexey Dobriyan35318db2018-04-10 16:41:14 -07001912 if (str[0] == '0' && str[1] != '-')
1913 return -EINVAL;
Alexey Dobriyanac7f1062018-02-06 15:36:59 -08001914 len = _parse_integer(str, 16, &sval);
1915 if (len & KSTRTOX_OVERFLOW)
Pavel Emelyanov640708a2012-01-10 15:11:23 -08001916 return -EINVAL;
Alexey Dobriyanac7f1062018-02-06 15:36:59 -08001917 if (sval != (unsigned long)sval)
1918 return -EINVAL;
1919 str += len;
1920
1921 if (*str != '-')
1922 return -EINVAL;
1923 str++;
1924
Alexey Dobriyan35318db2018-04-10 16:41:14 -07001925 if (str[0] == '0' && str[1])
1926 return -EINVAL;
Alexey Dobriyanac7f1062018-02-06 15:36:59 -08001927 len = _parse_integer(str, 16, &eval);
1928 if (len & KSTRTOX_OVERFLOW)
1929 return -EINVAL;
1930 if (eval != (unsigned long)eval)
1931 return -EINVAL;
1932 str += len;
1933
1934 if (*str != '\0')
1935 return -EINVAL;
1936
1937 *start = sval;
1938 *end = eval;
Pavel Emelyanov640708a2012-01-10 15:11:23 -08001939
1940 return 0;
1941}
1942
Al Viro0b728e12012-06-10 16:03:43 -04001943static int map_files_d_revalidate(struct dentry *dentry, unsigned int flags)
Pavel Emelyanov640708a2012-01-10 15:11:23 -08001944{
1945 unsigned long vm_start, vm_end;
1946 bool exact_vma_exists = false;
1947 struct mm_struct *mm = NULL;
1948 struct task_struct *task;
Pavel Emelyanov640708a2012-01-10 15:11:23 -08001949 struct inode *inode;
1950 int status = 0;
1951
Al Viro0b728e12012-06-10 16:03:43 -04001952 if (flags & LOOKUP_RCU)
Pavel Emelyanov640708a2012-01-10 15:11:23 -08001953 return -ECHILD;
1954
David Howells2b0143b2015-03-17 22:25:59 +00001955 inode = d_inode(dentry);
Pavel Emelyanov640708a2012-01-10 15:11:23 -08001956 task = get_proc_task(inode);
1957 if (!task)
1958 goto out_notask;
1959
Jann Horncaaee622016-01-20 15:00:04 -08001960 mm = mm_access(task, PTRACE_MODE_READ_FSCREDS);
Cong Wang2344bec2012-05-31 16:26:18 -07001961 if (IS_ERR_OR_NULL(mm))
Pavel Emelyanov640708a2012-01-10 15:11:23 -08001962 goto out;
1963
1964 if (!dname_to_vma_addr(dentry, &vm_start, &vm_end)) {
Konstantin Khlebnikovcd9e2bb2019-07-11 21:00:03 -07001965 status = down_read_killable(&mm->mmap_sem);
1966 if (!status) {
1967 exact_vma_exists = !!find_exact_vma(mm, vm_start,
1968 vm_end);
1969 up_read(&mm->mmap_sem);
1970 }
Pavel Emelyanov640708a2012-01-10 15:11:23 -08001971 }
1972
1973 mmput(mm);
1974
1975 if (exact_vma_exists) {
Eric W. Biederman68eb94f2017-01-03 10:23:11 +13001976 task_dump_owner(task, 0, &inode->i_uid, &inode->i_gid);
1977
Pavel Emelyanov640708a2012-01-10 15:11:23 -08001978 security_task_to_inode(task, inode);
1979 status = 1;
1980 }
1981
1982out:
1983 put_task_struct(task);
1984
1985out_notask:
Pavel Emelyanov640708a2012-01-10 15:11:23 -08001986 return status;
1987}
1988
1989static const struct dentry_operations tid_map_files_dentry_operations = {
1990 .d_revalidate = map_files_d_revalidate,
1991 .d_delete = pid_delete_dentry,
1992};
1993
Al Viro6b255392015-11-17 10:20:54 -05001994static int map_files_get_link(struct dentry *dentry, struct path *path)
Pavel Emelyanov640708a2012-01-10 15:11:23 -08001995{
1996 unsigned long vm_start, vm_end;
1997 struct vm_area_struct *vma;
1998 struct task_struct *task;
1999 struct mm_struct *mm;
2000 int rc;
2001
2002 rc = -ENOENT;
David Howells2b0143b2015-03-17 22:25:59 +00002003 task = get_proc_task(d_inode(dentry));
Pavel Emelyanov640708a2012-01-10 15:11:23 -08002004 if (!task)
2005 goto out;
2006
2007 mm = get_task_mm(task);
2008 put_task_struct(task);
2009 if (!mm)
2010 goto out;
2011
2012 rc = dname_to_vma_addr(dentry, &vm_start, &vm_end);
2013 if (rc)
2014 goto out_mmput;
2015
Konstantin Khlebnikovcd9e2bb2019-07-11 21:00:03 -07002016 rc = down_read_killable(&mm->mmap_sem);
2017 if (rc)
2018 goto out_mmput;
2019
Artem Fetishev70335ab2014-03-10 15:49:45 -07002020 rc = -ENOENT;
Pavel Emelyanov640708a2012-01-10 15:11:23 -08002021 vma = find_exact_vma(mm, vm_start, vm_end);
2022 if (vma && vma->vm_file) {
2023 *path = vma->vm_file->f_path;
2024 path_get(path);
2025 rc = 0;
2026 }
2027 up_read(&mm->mmap_sem);
2028
2029out_mmput:
2030 mmput(mm);
2031out:
2032 return rc;
2033}
2034
2035struct map_files_info {
Alexey Dobriyan20d28cd2018-02-06 15:37:06 -08002036 unsigned long start;
2037 unsigned long end;
Al Viro7b540d02012-08-27 14:55:26 -04002038 fmode_t mode;
Pavel Emelyanov640708a2012-01-10 15:11:23 -08002039};
2040
Calvin Owensbdb4d102015-09-09 15:35:54 -07002041/*
2042 * Only allow CAP_SYS_ADMIN to follow the links, due to concerns about how the
2043 * symlinks may be used to bypass permissions on ancestor directories in the
2044 * path to the file in question.
2045 */
2046static const char *
Al Viro6b255392015-11-17 10:20:54 -05002047proc_map_files_get_link(struct dentry *dentry,
Al Virofceef392015-12-29 15:58:39 -05002048 struct inode *inode,
2049 struct delayed_call *done)
Calvin Owensbdb4d102015-09-09 15:35:54 -07002050{
2051 if (!capable(CAP_SYS_ADMIN))
2052 return ERR_PTR(-EPERM);
2053
Al Virofceef392015-12-29 15:58:39 -05002054 return proc_pid_get_link(dentry, inode, done);
Calvin Owensbdb4d102015-09-09 15:35:54 -07002055}
2056
2057/*
Al Viro6b255392015-11-17 10:20:54 -05002058 * Identical to proc_pid_link_inode_operations except for get_link()
Calvin Owensbdb4d102015-09-09 15:35:54 -07002059 */
2060static const struct inode_operations proc_map_files_link_inode_operations = {
2061 .readlink = proc_pid_readlink,
Al Viro6b255392015-11-17 10:20:54 -05002062 .get_link = proc_map_files_get_link,
Calvin Owensbdb4d102015-09-09 15:35:54 -07002063 .setattr = proc_setattr,
2064};
2065
Al Viro0168b9e2018-05-03 09:21:05 -04002066static struct dentry *
2067proc_map_files_instantiate(struct dentry *dentry,
Pavel Emelyanov640708a2012-01-10 15:11:23 -08002068 struct task_struct *task, const void *ptr)
2069{
Al Viro7b540d02012-08-27 14:55:26 -04002070 fmode_t mode = (fmode_t)(unsigned long)ptr;
Pavel Emelyanov640708a2012-01-10 15:11:23 -08002071 struct proc_inode *ei;
2072 struct inode *inode;
2073
Al Viro0168b9e2018-05-03 09:21:05 -04002074 inode = proc_pid_make_inode(dentry->d_sb, task, S_IFLNK |
Andreas Gruenbacherdb978da2016-11-10 22:18:28 +01002075 ((mode & FMODE_READ ) ? S_IRUSR : 0) |
2076 ((mode & FMODE_WRITE) ? S_IWUSR : 0));
Pavel Emelyanov640708a2012-01-10 15:11:23 -08002077 if (!inode)
Al Viro0168b9e2018-05-03 09:21:05 -04002078 return ERR_PTR(-ENOENT);
Pavel Emelyanov640708a2012-01-10 15:11:23 -08002079
2080 ei = PROC_I(inode);
Al Viro6b255392015-11-17 10:20:54 -05002081 ei->op.proc_get_link = map_files_get_link;
Pavel Emelyanov640708a2012-01-10 15:11:23 -08002082
Calvin Owensbdb4d102015-09-09 15:35:54 -07002083 inode->i_op = &proc_map_files_link_inode_operations;
Pavel Emelyanov640708a2012-01-10 15:11:23 -08002084 inode->i_size = 64;
Pavel Emelyanov640708a2012-01-10 15:11:23 -08002085
2086 d_set_d_op(dentry, &tid_map_files_dentry_operations);
Al Viro0168b9e2018-05-03 09:21:05 -04002087 return d_splice_alias(inode, dentry);
Pavel Emelyanov640708a2012-01-10 15:11:23 -08002088}
2089
2090static struct dentry *proc_map_files_lookup(struct inode *dir,
Al Viro00cd8dd2012-06-10 17:13:09 -04002091 struct dentry *dentry, unsigned int flags)
Pavel Emelyanov640708a2012-01-10 15:11:23 -08002092{
2093 unsigned long vm_start, vm_end;
2094 struct vm_area_struct *vma;
2095 struct task_struct *task;
Al Viro0168b9e2018-05-03 09:21:05 -04002096 struct dentry *result;
Pavel Emelyanov640708a2012-01-10 15:11:23 -08002097 struct mm_struct *mm;
2098
Al Viro0168b9e2018-05-03 09:21:05 -04002099 result = ERR_PTR(-ENOENT);
Pavel Emelyanov640708a2012-01-10 15:11:23 -08002100 task = get_proc_task(dir);
2101 if (!task)
2102 goto out;
2103
Al Viro0168b9e2018-05-03 09:21:05 -04002104 result = ERR_PTR(-EACCES);
Jann Horncaaee622016-01-20 15:00:04 -08002105 if (!ptrace_may_access(task, PTRACE_MODE_READ_FSCREDS))
Pavel Emelyanov640708a2012-01-10 15:11:23 -08002106 goto out_put_task;
2107
Al Viro0168b9e2018-05-03 09:21:05 -04002108 result = ERR_PTR(-ENOENT);
Pavel Emelyanov640708a2012-01-10 15:11:23 -08002109 if (dname_to_vma_addr(dentry, &vm_start, &vm_end))
Cyrill Gorcunoveb94cd92012-05-17 17:03:25 -07002110 goto out_put_task;
Pavel Emelyanov640708a2012-01-10 15:11:23 -08002111
2112 mm = get_task_mm(task);
2113 if (!mm)
Cyrill Gorcunoveb94cd92012-05-17 17:03:25 -07002114 goto out_put_task;
Pavel Emelyanov640708a2012-01-10 15:11:23 -08002115
Konstantin Khlebnikovcd9e2bb2019-07-11 21:00:03 -07002116 result = ERR_PTR(-EINTR);
2117 if (down_read_killable(&mm->mmap_sem))
2118 goto out_put_mm;
2119
2120 result = ERR_PTR(-ENOENT);
Pavel Emelyanov640708a2012-01-10 15:11:23 -08002121 vma = find_exact_vma(mm, vm_start, vm_end);
2122 if (!vma)
2123 goto out_no_vma;
2124
Stanislav Kinsbursky05f56482012-11-26 16:29:42 -08002125 if (vma->vm_file)
Al Viro0168b9e2018-05-03 09:21:05 -04002126 result = proc_map_files_instantiate(dentry, task,
Stanislav Kinsbursky05f56482012-11-26 16:29:42 -08002127 (void *)(unsigned long)vma->vm_file->f_mode);
Pavel Emelyanov640708a2012-01-10 15:11:23 -08002128
2129out_no_vma:
2130 up_read(&mm->mmap_sem);
Konstantin Khlebnikovcd9e2bb2019-07-11 21:00:03 -07002131out_put_mm:
Pavel Emelyanov640708a2012-01-10 15:11:23 -08002132 mmput(mm);
Pavel Emelyanov640708a2012-01-10 15:11:23 -08002133out_put_task:
2134 put_task_struct(task);
2135out:
Al Viro0168b9e2018-05-03 09:21:05 -04002136 return result;
Pavel Emelyanov640708a2012-01-10 15:11:23 -08002137}
2138
2139static const struct inode_operations proc_map_files_inode_operations = {
2140 .lookup = proc_map_files_lookup,
2141 .permission = proc_fd_permission,
2142 .setattr = proc_setattr,
2143};
2144
2145static int
Al Virof0c3b502013-05-16 12:07:31 -04002146proc_map_files_readdir(struct file *file, struct dir_context *ctx)
Pavel Emelyanov640708a2012-01-10 15:11:23 -08002147{
Pavel Emelyanov640708a2012-01-10 15:11:23 -08002148 struct vm_area_struct *vma;
2149 struct task_struct *task;
2150 struct mm_struct *mm;
Al Virof0c3b502013-05-16 12:07:31 -04002151 unsigned long nr_files, pos, i;
Kent Overstreet94f8f3b2019-03-11 23:31:18 -07002152 GENRADIX(struct map_files_info) fa;
Al Virof0c3b502013-05-16 12:07:31 -04002153 struct map_files_info *p;
Pavel Emelyanov640708a2012-01-10 15:11:23 -08002154 int ret;
2155
Kent Overstreet94f8f3b2019-03-11 23:31:18 -07002156 genradix_init(&fa);
2157
Pavel Emelyanov640708a2012-01-10 15:11:23 -08002158 ret = -ENOENT;
Al Virof0c3b502013-05-16 12:07:31 -04002159 task = get_proc_task(file_inode(file));
Pavel Emelyanov640708a2012-01-10 15:11:23 -08002160 if (!task)
2161 goto out;
2162
2163 ret = -EACCES;
Jann Horncaaee622016-01-20 15:00:04 -08002164 if (!ptrace_may_access(task, PTRACE_MODE_READ_FSCREDS))
Pavel Emelyanov640708a2012-01-10 15:11:23 -08002165 goto out_put_task;
2166
2167 ret = 0;
Al Virof0c3b502013-05-16 12:07:31 -04002168 if (!dir_emit_dots(file, ctx))
2169 goto out_put_task;
Pavel Emelyanov640708a2012-01-10 15:11:23 -08002170
Al Virof0c3b502013-05-16 12:07:31 -04002171 mm = get_task_mm(task);
2172 if (!mm)
2173 goto out_put_task;
Konstantin Khlebnikovcd9e2bb2019-07-11 21:00:03 -07002174
2175 ret = down_read_killable(&mm->mmap_sem);
2176 if (ret) {
2177 mmput(mm);
2178 goto out_put_task;
2179 }
Pavel Emelyanov640708a2012-01-10 15:11:23 -08002180
Al Virof0c3b502013-05-16 12:07:31 -04002181 nr_files = 0;
Pavel Emelyanov640708a2012-01-10 15:11:23 -08002182
Al Virof0c3b502013-05-16 12:07:31 -04002183 /*
2184 * We need two passes here:
2185 *
2186 * 1) Collect vmas of mapped files with mmap_sem taken
2187 * 2) Release mmap_sem and instantiate entries
2188 *
2189 * otherwise we get lockdep complained, since filldir()
2190 * routine might require mmap_sem taken in might_fault().
2191 */
Pavel Emelyanov640708a2012-01-10 15:11:23 -08002192
Al Virof0c3b502013-05-16 12:07:31 -04002193 for (vma = mm->mmap, pos = 2; vma; vma = vma->vm_next) {
Kent Overstreet94f8f3b2019-03-11 23:31:18 -07002194 if (!vma->vm_file)
2195 continue;
2196 if (++pos <= ctx->pos)
2197 continue;
Al Virof0c3b502013-05-16 12:07:31 -04002198
Kent Overstreet94f8f3b2019-03-11 23:31:18 -07002199 p = genradix_ptr_alloc(&fa, nr_files++, GFP_KERNEL);
2200 if (!p) {
Al Virof0c3b502013-05-16 12:07:31 -04002201 ret = -ENOMEM;
Al Virof0c3b502013-05-16 12:07:31 -04002202 up_read(&mm->mmap_sem);
2203 mmput(mm);
2204 goto out_put_task;
2205 }
Al Virof0c3b502013-05-16 12:07:31 -04002206
Kent Overstreet94f8f3b2019-03-11 23:31:18 -07002207 p->start = vma->vm_start;
2208 p->end = vma->vm_end;
2209 p->mode = vma->vm_file->f_mode;
Pavel Emelyanov640708a2012-01-10 15:11:23 -08002210 }
Al Virof0c3b502013-05-16 12:07:31 -04002211 up_read(&mm->mmap_sem);
Alexey Dobriyanfe079a52018-04-10 16:32:05 -07002212 mmput(mm);
Al Virof0c3b502013-05-16 12:07:31 -04002213
2214 for (i = 0; i < nr_files; i++) {
Alexey Dobriyan20d28cd2018-02-06 15:37:06 -08002215 char buf[4 * sizeof(long) + 2]; /* max: %lx-%lx\0 */
2216 unsigned int len;
2217
Kent Overstreet94f8f3b2019-03-11 23:31:18 -07002218 p = genradix_ptr(&fa, i);
Alexey Dobriyan20d28cd2018-02-06 15:37:06 -08002219 len = snprintf(buf, sizeof(buf), "%lx-%lx", p->start, p->end);
Al Virof0c3b502013-05-16 12:07:31 -04002220 if (!proc_fill_cache(file, ctx,
Alexey Dobriyan20d28cd2018-02-06 15:37:06 -08002221 buf, len,
Al Virof0c3b502013-05-16 12:07:31 -04002222 proc_map_files_instantiate,
2223 task,
2224 (void *)(unsigned long)p->mode))
2225 break;
2226 ctx->pos++;
2227 }
Pavel Emelyanov640708a2012-01-10 15:11:23 -08002228
Pavel Emelyanov640708a2012-01-10 15:11:23 -08002229out_put_task:
2230 put_task_struct(task);
2231out:
Kent Overstreet94f8f3b2019-03-11 23:31:18 -07002232 genradix_free(&fa);
Pavel Emelyanov640708a2012-01-10 15:11:23 -08002233 return ret;
2234}
2235
2236static const struct file_operations proc_map_files_operations = {
2237 .read = generic_read_dir,
Al Virof50752e2016-04-20 17:13:54 -04002238 .iterate_shared = proc_map_files_readdir,
2239 .llseek = generic_file_llseek,
Pavel Emelyanov640708a2012-01-10 15:11:23 -08002240};
2241
Nicolas Pitreb18b6a92017-01-21 00:09:08 -05002242#if defined(CONFIG_CHECKPOINT_RESTORE) && defined(CONFIG_POSIX_TIMERS)
Pavel Emelyanov48f6a7a2013-03-11 13:12:45 +04002243struct timers_private {
2244 struct pid *pid;
2245 struct task_struct *task;
2246 struct sighand_struct *sighand;
Pavel Emelyanov57b80152013-03-11 13:13:08 +04002247 struct pid_namespace *ns;
Pavel Emelyanov48f6a7a2013-03-11 13:12:45 +04002248 unsigned long flags;
2249};
2250
2251static void *timers_start(struct seq_file *m, loff_t *pos)
2252{
2253 struct timers_private *tp = m->private;
2254
2255 tp->task = get_pid_task(tp->pid, PIDTYPE_PID);
2256 if (!tp->task)
2257 return ERR_PTR(-ESRCH);
2258
2259 tp->sighand = lock_task_sighand(tp->task, &tp->flags);
2260 if (!tp->sighand)
2261 return ERR_PTR(-ESRCH);
2262
2263 return seq_list_start(&tp->task->signal->posix_timers, *pos);
2264}
2265
2266static void *timers_next(struct seq_file *m, void *v, loff_t *pos)
2267{
2268 struct timers_private *tp = m->private;
2269 return seq_list_next(v, &tp->task->signal->posix_timers, pos);
2270}
2271
2272static void timers_stop(struct seq_file *m, void *v)
2273{
2274 struct timers_private *tp = m->private;
2275
2276 if (tp->sighand) {
2277 unlock_task_sighand(tp->task, &tp->flags);
2278 tp->sighand = NULL;
2279 }
2280
2281 if (tp->task) {
2282 put_task_struct(tp->task);
2283 tp->task = NULL;
2284 }
2285}
2286
2287static int show_timer(struct seq_file *m, void *v)
2288{
2289 struct k_itimer *timer;
Pavel Emelyanov57b80152013-03-11 13:13:08 +04002290 struct timers_private *tp = m->private;
2291 int notify;
Alexey Dobriyancedbcca2014-08-08 14:21:33 -07002292 static const char * const nstr[] = {
Pavel Emelyanov57b80152013-03-11 13:13:08 +04002293 [SIGEV_SIGNAL] = "signal",
2294 [SIGEV_NONE] = "none",
2295 [SIGEV_THREAD] = "thread",
2296 };
Pavel Emelyanov48f6a7a2013-03-11 13:12:45 +04002297
2298 timer = list_entry((struct list_head *)v, struct k_itimer, list);
Pavel Emelyanov57b80152013-03-11 13:13:08 +04002299 notify = timer->it_sigev_notify;
2300
Pavel Emelyanov48f6a7a2013-03-11 13:12:45 +04002301 seq_printf(m, "ID: %d\n", timer->it_id);
Linus Torvaldsba3edf1f2017-12-06 18:23:27 -08002302 seq_printf(m, "signal: %d/%px\n",
Joe Perches25ce3192015-04-15 16:18:17 -07002303 timer->sigq->info.si_signo,
2304 timer->sigq->info.si_value.sival_ptr);
Pavel Emelyanov57b80152013-03-11 13:13:08 +04002305 seq_printf(m, "notify: %s/%s.%d\n",
Joe Perches25ce3192015-04-15 16:18:17 -07002306 nstr[notify & ~SIGEV_THREAD_ID],
2307 (notify & SIGEV_THREAD_ID) ? "tid" : "pid",
2308 pid_nr_ns(timer->it_pid, tp->ns));
Pavel Tikhomirov15ef0292013-05-17 02:12:03 +04002309 seq_printf(m, "ClockID: %d\n", timer->it_clock);
Pavel Emelyanov48f6a7a2013-03-11 13:12:45 +04002310
2311 return 0;
2312}
2313
2314static const struct seq_operations proc_timers_seq_ops = {
2315 .start = timers_start,
2316 .next = timers_next,
2317 .stop = timers_stop,
2318 .show = show_timer,
2319};
2320
2321static int proc_timers_open(struct inode *inode, struct file *file)
2322{
2323 struct timers_private *tp;
2324
2325 tp = __seq_open_private(file, &proc_timers_seq_ops,
2326 sizeof(struct timers_private));
2327 if (!tp)
2328 return -ENOMEM;
2329
2330 tp->pid = proc_pid(inode);
Christoph Hellwig76f668b2018-05-16 07:19:01 +02002331 tp->ns = proc_pid_ns(inode);
Pavel Emelyanov48f6a7a2013-03-11 13:12:45 +04002332 return 0;
2333}
2334
2335static const struct file_operations proc_timers_operations = {
2336 .open = proc_timers_open,
2337 .read = seq_read,
2338 .llseek = seq_lseek,
2339 .release = seq_release_private,
2340};
Eric Engestromb5946be2016-03-17 14:20:57 -07002341#endif
Pavel Emelyanov640708a2012-01-10 15:11:23 -08002342
John Stultz5de23d42016-03-17 14:20:54 -07002343static ssize_t timerslack_ns_write(struct file *file, const char __user *buf,
2344 size_t count, loff_t *offset)
2345{
2346 struct inode *inode = file_inode(file);
2347 struct task_struct *p;
2348 u64 slack_ns;
2349 int err;
2350
2351 err = kstrtoull_from_user(buf, count, 10, &slack_ns);
2352 if (err < 0)
2353 return err;
2354
2355 p = get_proc_task(inode);
2356 if (!p)
2357 return -ESRCH;
2358
John Stultz4b2bd5f2016-10-07 17:02:33 -07002359 if (p != current) {
Benjamin Gordon8da0b4f2019-01-03 15:25:56 -08002360 rcu_read_lock();
2361 if (!ns_capable(__task_cred(p)->user_ns, CAP_SYS_NICE)) {
2362 rcu_read_unlock();
John Stultz4b2bd5f2016-10-07 17:02:33 -07002363 count = -EPERM;
2364 goto out;
2365 }
Benjamin Gordon8da0b4f2019-01-03 15:25:56 -08002366 rcu_read_unlock();
John Stultz5de23d42016-03-17 14:20:54 -07002367
John Stultz4b2bd5f2016-10-07 17:02:33 -07002368 err = security_task_setscheduler(p);
2369 if (err) {
2370 count = err;
2371 goto out;
2372 }
John Stultz904763e2016-10-07 17:02:29 -07002373 }
2374
John Stultz7abbaf92016-10-07 17:02:26 -07002375 task_lock(p);
2376 if (slack_ns == 0)
2377 p->timer_slack_ns = p->default_timer_slack_ns;
2378 else
2379 p->timer_slack_ns = slack_ns;
2380 task_unlock(p);
2381
2382out:
John Stultz5de23d42016-03-17 14:20:54 -07002383 put_task_struct(p);
2384
2385 return count;
2386}
2387
2388static int timerslack_ns_show(struct seq_file *m, void *v)
2389{
2390 struct inode *inode = m->private;
2391 struct task_struct *p;
John Stultz7abbaf92016-10-07 17:02:26 -07002392 int err = 0;
John Stultz5de23d42016-03-17 14:20:54 -07002393
2394 p = get_proc_task(inode);
2395 if (!p)
2396 return -ESRCH;
2397
John Stultz4b2bd5f2016-10-07 17:02:33 -07002398 if (p != current) {
Benjamin Gordon8da0b4f2019-01-03 15:25:56 -08002399 rcu_read_lock();
2400 if (!ns_capable(__task_cred(p)->user_ns, CAP_SYS_NICE)) {
2401 rcu_read_unlock();
John Stultz4b2bd5f2016-10-07 17:02:33 -07002402 err = -EPERM;
2403 goto out;
2404 }
Benjamin Gordon8da0b4f2019-01-03 15:25:56 -08002405 rcu_read_unlock();
2406
John Stultz4b2bd5f2016-10-07 17:02:33 -07002407 err = security_task_getscheduler(p);
2408 if (err)
2409 goto out;
2410 }
John Stultz904763e2016-10-07 17:02:29 -07002411
John Stultz7abbaf92016-10-07 17:02:26 -07002412 task_lock(p);
2413 seq_printf(m, "%llu\n", p->timer_slack_ns);
2414 task_unlock(p);
2415
2416out:
John Stultz5de23d42016-03-17 14:20:54 -07002417 put_task_struct(p);
2418
2419 return err;
2420}
2421
2422static int timerslack_ns_open(struct inode *inode, struct file *filp)
2423{
2424 return single_open(filp, timerslack_ns_show, inode);
2425}
2426
2427static const struct file_operations proc_pid_set_timerslack_ns_operations = {
2428 .open = timerslack_ns_open,
2429 .read = seq_read,
2430 .write = timerslack_ns_write,
2431 .llseek = seq_lseek,
2432 .release = single_release,
2433};
2434
Al Viro0168b9e2018-05-03 09:21:05 -04002435static struct dentry *proc_pident_instantiate(struct dentry *dentry,
2436 struct task_struct *task, const void *ptr)
Eric W. Biederman444ceed2006-10-02 02:18:49 -07002437{
Eric Dumazetc5141e62007-05-08 00:26:15 -07002438 const struct pid_entry *p = ptr;
Eric W. Biederman444ceed2006-10-02 02:18:49 -07002439 struct inode *inode;
2440 struct proc_inode *ei;
Eric W. Biederman444ceed2006-10-02 02:18:49 -07002441
Al Viro0168b9e2018-05-03 09:21:05 -04002442 inode = proc_pid_make_inode(dentry->d_sb, task, p->mode);
Eric W. Biederman444ceed2006-10-02 02:18:49 -07002443 if (!inode)
Al Viro0168b9e2018-05-03 09:21:05 -04002444 return ERR_PTR(-ENOENT);
Eric W. Biederman444ceed2006-10-02 02:18:49 -07002445
2446 ei = PROC_I(inode);
Eric W. Biederman444ceed2006-10-02 02:18:49 -07002447 if (S_ISDIR(inode->i_mode))
Miklos Szeredibfe86842011-10-28 14:13:29 +02002448 set_nlink(inode, 2); /* Use getattr to fix if necessary */
Eric W. Biederman444ceed2006-10-02 02:18:49 -07002449 if (p->iop)
2450 inode->i_op = p->iop;
2451 if (p->fop)
2452 inode->i_fop = p->fop;
2453 ei->op = p->op;
Al Viro1bbc5512018-05-02 21:26:16 -04002454 pid_update_inode(task, inode);
Nick Pigginfb045ad2011-01-07 17:49:55 +11002455 d_set_d_op(dentry, &pid_dentry_operations);
Al Viro0168b9e2018-05-03 09:21:05 -04002456 return d_splice_alias(inode, dentry);
Eric W. Biederman444ceed2006-10-02 02:18:49 -07002457}
2458
Linus Torvalds1da177e2005-04-16 15:20:36 -07002459static struct dentry *proc_pident_lookup(struct inode *dir,
2460 struct dentry *dentry,
Alexey Dobriyand5a572a2019-03-11 23:28:51 -07002461 const struct pid_entry *p,
2462 const struct pid_entry *end)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002463{
Eric W. Biederman99f89552006-06-26 00:25:55 -07002464 struct task_struct *task = get_proc_task(dir);
Al Viro0168b9e2018-05-03 09:21:05 -04002465 struct dentry *res = ERR_PTR(-ENOENT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002466
Eric W. Biederman99f89552006-06-26 00:25:55 -07002467 if (!task)
2468 goto out_no_task;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002469
Eric W. Biederman20cdc892006-10-02 02:17:07 -07002470 /*
2471 * Yes, it does not scale. And it should not. Don't add
2472 * new entries into /proc/<tgid>/ without very good reasons.
2473 */
Alexey Dobriyand5a572a2019-03-11 23:28:51 -07002474 for (; p < end; p++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002475 if (p->len != dentry->d_name.len)
2476 continue;
Alexey Dobriyan26b95132018-06-14 15:27:17 -07002477 if (!memcmp(dentry->d_name.name, p->name, p->len)) {
2478 res = proc_pident_instantiate(dentry, task, p);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002479 break;
Alexey Dobriyan26b95132018-06-14 15:27:17 -07002480 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002481 }
Eric W. Biederman99f89552006-06-26 00:25:55 -07002482 put_task_struct(task);
2483out_no_task:
Al Viro0168b9e2018-05-03 09:21:05 -04002484 return res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002485}
2486
Al Virof0c3b502013-05-16 12:07:31 -04002487static int proc_pident_readdir(struct file *file, struct dir_context *ctx,
Eric Dumazetc5141e62007-05-08 00:26:15 -07002488 const struct pid_entry *ents, unsigned int nents)
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002489{
Al Virof0c3b502013-05-16 12:07:31 -04002490 struct task_struct *task = get_proc_task(file_inode(file));
2491 const struct pid_entry *p;
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002492
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002493 if (!task)
Al Virof0c3b502013-05-16 12:07:31 -04002494 return -ENOENT;
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002495
Al Virof0c3b502013-05-16 12:07:31 -04002496 if (!dir_emit_dots(file, ctx))
2497 goto out;
2498
2499 if (ctx->pos >= nents + 2)
2500 goto out;
2501
Alexey Dobriyanbac5f5d2016-12-12 16:45:28 -08002502 for (p = ents + (ctx->pos - 2); p < ents + nents; p++) {
Al Virof0c3b502013-05-16 12:07:31 -04002503 if (!proc_fill_cache(file, ctx, p->name, p->len,
2504 proc_pident_instantiate, task, p))
2505 break;
2506 ctx->pos++;
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002507 }
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002508out:
Eric W. Biederman61a28782006-10-02 02:18:49 -07002509 put_task_struct(task);
Al Virof0c3b502013-05-16 12:07:31 -04002510 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002511}
2512
Linus Torvalds1da177e2005-04-16 15:20:36 -07002513#ifdef CONFIG_SECURITY
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002514static ssize_t proc_pid_attr_read(struct file * file, char __user * buf,
2515 size_t count, loff_t *ppos)
2516{
Al Viro496ad9a2013-01-23 17:07:38 -05002517 struct inode * inode = file_inode(file);
Al Viro04ff9702007-03-12 16:17:58 +00002518 char *p = NULL;
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002519 ssize_t length;
2520 struct task_struct *task = get_proc_task(inode);
2521
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002522 if (!task)
Al Viro04ff9702007-03-12 16:17:58 +00002523 return -ESRCH;
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002524
Casey Schaufler6d9c9392018-09-21 17:16:59 -07002525 length = security_getprocattr(task, PROC_I(inode)->op.lsm,
Josef "Jeff" Sipek2fddfee2006-12-08 02:36:36 -08002526 (char*)file->f_path.dentry->d_name.name,
Al Viro04ff9702007-03-12 16:17:58 +00002527 &p);
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002528 put_task_struct(task);
Al Viro04ff9702007-03-12 16:17:58 +00002529 if (length > 0)
2530 length = simple_read_from_buffer(buf, count, ppos, p, length);
2531 kfree(p);
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002532 return length;
2533}
2534
2535static ssize_t proc_pid_attr_write(struct file * file, const char __user * buf,
2536 size_t count, loff_t *ppos)
2537{
Al Viro496ad9a2013-01-23 17:07:38 -05002538 struct inode * inode = file_inode(file);
Alexey Dobriyan41089b62018-08-21 21:54:30 -07002539 struct task_struct *task;
Al Virobb646cd2015-12-24 00:16:30 -05002540 void *page;
Alexey Dobriyan41089b62018-08-21 21:54:30 -07002541 int rv;
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002542
Alexey Dobriyan41089b62018-08-21 21:54:30 -07002543 rcu_read_lock();
2544 task = pid_task(proc_pid(inode), PIDTYPE_PID);
2545 if (!task) {
2546 rcu_read_unlock();
2547 return -ESRCH;
2548 }
Stephen Smalleyb21507e2017-01-09 10:07:31 -05002549 /* A task may only write its own attributes. */
Alexey Dobriyan41089b62018-08-21 21:54:30 -07002550 if (current != task) {
2551 rcu_read_unlock();
2552 return -EACCES;
2553 }
Paul Moore35a196b2019-04-19 14:55:12 -04002554 /* Prevent changes to overridden credentials. */
2555 if (current_cred() != current_real_cred()) {
2556 rcu_read_unlock();
2557 return -EBUSY;
2558 }
Alexey Dobriyan41089b62018-08-21 21:54:30 -07002559 rcu_read_unlock();
Stephen Smalleyb21507e2017-01-09 10:07:31 -05002560
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002561 if (count > PAGE_SIZE)
2562 count = PAGE_SIZE;
2563
2564 /* No partial writes. */
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002565 if (*ppos != 0)
Alexey Dobriyan41089b62018-08-21 21:54:30 -07002566 return -EINVAL;
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002567
Al Virobb646cd2015-12-24 00:16:30 -05002568 page = memdup_user(buf, count);
2569 if (IS_ERR(page)) {
Alexey Dobriyan41089b62018-08-21 21:54:30 -07002570 rv = PTR_ERR(page);
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002571 goto out;
Al Virobb646cd2015-12-24 00:16:30 -05002572 }
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002573
David Howells107db7c2009-05-08 13:55:27 +01002574 /* Guard against adverse ptrace interaction */
Alexey Dobriyan41089b62018-08-21 21:54:30 -07002575 rv = mutex_lock_interruptible(&current->signal->cred_guard_mutex);
2576 if (rv < 0)
David Howells107db7c2009-05-08 13:55:27 +01002577 goto out_free;
2578
Casey Schaufler6d9c9392018-09-21 17:16:59 -07002579 rv = security_setprocattr(PROC_I(inode)->op.lsm,
2580 file->f_path.dentry->d_name.name, page,
2581 count);
Stephen Smalleyb21507e2017-01-09 10:07:31 -05002582 mutex_unlock(&current->signal->cred_guard_mutex);
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002583out_free:
Al Virobb646cd2015-12-24 00:16:30 -05002584 kfree(page);
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002585out:
Alexey Dobriyan41089b62018-08-21 21:54:30 -07002586 return rv;
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002587}
2588
Arjan van de Ven00977a52007-02-12 00:55:34 -08002589static const struct file_operations proc_pid_attr_operations = {
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002590 .read = proc_pid_attr_read,
2591 .write = proc_pid_attr_write,
Arnd Bergmann87df8422010-03-17 23:06:02 +01002592 .llseek = generic_file_llseek,
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002593};
2594
Casey Schaufler6d9c9392018-09-21 17:16:59 -07002595#define LSM_DIR_OPS(LSM) \
2596static int proc_##LSM##_attr_dir_iterate(struct file *filp, \
2597 struct dir_context *ctx) \
2598{ \
2599 return proc_pident_readdir(filp, ctx, \
2600 LSM##_attr_dir_stuff, \
2601 ARRAY_SIZE(LSM##_attr_dir_stuff)); \
2602} \
2603\
2604static const struct file_operations proc_##LSM##_attr_dir_ops = { \
2605 .read = generic_read_dir, \
2606 .iterate = proc_##LSM##_attr_dir_iterate, \
2607 .llseek = default_llseek, \
2608}; \
2609\
2610static struct dentry *proc_##LSM##_attr_dir_lookup(struct inode *dir, \
2611 struct dentry *dentry, unsigned int flags) \
2612{ \
2613 return proc_pident_lookup(dir, dentry, \
2614 LSM##_attr_dir_stuff, \
Alexey Dobriyand5a572a2019-03-11 23:28:51 -07002615 LSM##_attr_dir_stuff + ARRAY_SIZE(LSM##_attr_dir_stuff)); \
Casey Schaufler6d9c9392018-09-21 17:16:59 -07002616} \
2617\
2618static const struct inode_operations proc_##LSM##_attr_dir_inode_ops = { \
2619 .lookup = proc_##LSM##_attr_dir_lookup, \
2620 .getattr = pid_getattr, \
2621 .setattr = proc_setattr, \
2622}
2623
2624#ifdef CONFIG_SECURITY_SMACK
2625static const struct pid_entry smack_attr_dir_stuff[] = {
2626 ATTR("smack", "current", 0666),
2627};
2628LSM_DIR_OPS(smack);
2629#endif
2630
Eric Dumazetc5141e62007-05-08 00:26:15 -07002631static const struct pid_entry attr_dir_stuff[] = {
Casey Schaufler6d9c9392018-09-21 17:16:59 -07002632 ATTR(NULL, "current", 0666),
2633 ATTR(NULL, "prev", 0444),
2634 ATTR(NULL, "exec", 0666),
2635 ATTR(NULL, "fscreate", 0666),
2636 ATTR(NULL, "keycreate", 0666),
2637 ATTR(NULL, "sockcreate", 0666),
2638#ifdef CONFIG_SECURITY_SMACK
2639 DIR("smack", 0555,
2640 proc_smack_attr_dir_inode_ops, proc_smack_attr_dir_ops),
2641#endif
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002642};
2643
Al Virof0c3b502013-05-16 12:07:31 -04002644static int proc_attr_dir_readdir(struct file *file, struct dir_context *ctx)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002645{
Al Virof0c3b502013-05-16 12:07:31 -04002646 return proc_pident_readdir(file, ctx,
2647 attr_dir_stuff, ARRAY_SIZE(attr_dir_stuff));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002648}
2649
Arjan van de Ven00977a52007-02-12 00:55:34 -08002650static const struct file_operations proc_attr_dir_operations = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002651 .read = generic_read_dir,
Al Virof50752e2016-04-20 17:13:54 -04002652 .iterate_shared = proc_attr_dir_readdir,
2653 .llseek = generic_file_llseek,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002654};
2655
Eric W. Biederman72d9dcf2006-10-02 02:18:50 -07002656static struct dentry *proc_attr_dir_lookup(struct inode *dir,
Al Viro00cd8dd2012-06-10 17:13:09 -04002657 struct dentry *dentry, unsigned int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002658{
Eric W. Biederman7bcd6b02006-10-02 02:18:56 -07002659 return proc_pident_lookup(dir, dentry,
Alexey Dobriyand5a572a2019-03-11 23:28:51 -07002660 attr_dir_stuff,
2661 attr_dir_stuff + ARRAY_SIZE(attr_dir_stuff));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002662}
2663
Arjan van de Venc5ef1c42007-02-12 00:55:40 -08002664static const struct inode_operations proc_attr_dir_inode_operations = {
Eric W. Biederman72d9dcf2006-10-02 02:18:50 -07002665 .lookup = proc_attr_dir_lookup,
Eric W. Biederman99f89552006-06-26 00:25:55 -07002666 .getattr = pid_getattr,
Linus Torvalds6d76fa52006-07-15 12:26:45 -07002667 .setattr = proc_setattr,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002668};
2669
Linus Torvalds1da177e2005-04-16 15:20:36 -07002670#endif
2671
Christoph Hellwig698ba7b2009-12-15 16:47:37 -08002672#ifdef CONFIG_ELF_CORE
Kawai, Hidehiro3cb4a0b2007-07-19 01:48:28 -07002673static ssize_t proc_coredump_filter_read(struct file *file, char __user *buf,
2674 size_t count, loff_t *ppos)
2675{
Al Viro496ad9a2013-01-23 17:07:38 -05002676 struct task_struct *task = get_proc_task(file_inode(file));
Kawai, Hidehiro3cb4a0b2007-07-19 01:48:28 -07002677 struct mm_struct *mm;
2678 char buffer[PROC_NUMBUF];
2679 size_t len;
2680 int ret;
2681
2682 if (!task)
2683 return -ESRCH;
2684
2685 ret = 0;
2686 mm = get_task_mm(task);
2687 if (mm) {
2688 len = snprintf(buffer, sizeof(buffer), "%08lx\n",
2689 ((mm->flags & MMF_DUMP_FILTER_MASK) >>
2690 MMF_DUMP_FILTER_SHIFT));
2691 mmput(mm);
2692 ret = simple_read_from_buffer(buf, count, ppos, buffer, len);
2693 }
2694
2695 put_task_struct(task);
2696
2697 return ret;
2698}
2699
2700static ssize_t proc_coredump_filter_write(struct file *file,
2701 const char __user *buf,
2702 size_t count,
2703 loff_t *ppos)
2704{
2705 struct task_struct *task;
2706 struct mm_struct *mm;
Kawai, Hidehiro3cb4a0b2007-07-19 01:48:28 -07002707 unsigned int val;
2708 int ret;
2709 int i;
2710 unsigned long mask;
2711
Alexey Dobriyan774636e2015-09-09 15:36:59 -07002712 ret = kstrtouint_from_user(buf, count, 0, &val);
2713 if (ret < 0)
2714 return ret;
Kawai, Hidehiro3cb4a0b2007-07-19 01:48:28 -07002715
2716 ret = -ESRCH;
Al Viro496ad9a2013-01-23 17:07:38 -05002717 task = get_proc_task(file_inode(file));
Kawai, Hidehiro3cb4a0b2007-07-19 01:48:28 -07002718 if (!task)
2719 goto out_no_task;
2720
Kawai, Hidehiro3cb4a0b2007-07-19 01:48:28 -07002721 mm = get_task_mm(task);
2722 if (!mm)
2723 goto out_no_mm;
Colin Ian King41a0c2492015-12-18 14:22:01 -08002724 ret = 0;
Kawai, Hidehiro3cb4a0b2007-07-19 01:48:28 -07002725
2726 for (i = 0, mask = 1; i < MMF_DUMP_FILTER_BITS; i++, mask <<= 1) {
2727 if (val & mask)
2728 set_bit(i + MMF_DUMP_FILTER_SHIFT, &mm->flags);
2729 else
2730 clear_bit(i + MMF_DUMP_FILTER_SHIFT, &mm->flags);
2731 }
2732
2733 mmput(mm);
2734 out_no_mm:
2735 put_task_struct(task);
2736 out_no_task:
Alexey Dobriyan774636e2015-09-09 15:36:59 -07002737 if (ret < 0)
2738 return ret;
2739 return count;
Kawai, Hidehiro3cb4a0b2007-07-19 01:48:28 -07002740}
2741
2742static const struct file_operations proc_coredump_filter_operations = {
2743 .read = proc_coredump_filter_read,
2744 .write = proc_coredump_filter_write,
Arnd Bergmann87df8422010-03-17 23:06:02 +01002745 .llseek = generic_file_llseek,
Kawai, Hidehiro3cb4a0b2007-07-19 01:48:28 -07002746};
2747#endif
2748
Andrew Mortonaba76fd2006-12-10 02:19:48 -08002749#ifdef CONFIG_TASK_IO_ACCOUNTING
Alexey Dobriyan19aadc92014-08-08 14:21:50 -07002750static int do_io_accounting(struct task_struct *task, struct seq_file *m, int whole)
Andrew Mortonaba76fd2006-12-10 02:19:48 -08002751{
Andrea Righi940389b2008-07-28 00:48:12 +02002752 struct task_io_accounting acct = task->ioac;
Andrea Righi59954772008-07-27 17:29:15 +02002753 unsigned long flags;
Vasiliy Kulikov293eb1e2011-07-26 16:08:38 -07002754 int result;
Andrea Righi297c5d92008-07-25 01:48:49 -07002755
Vasiliy Kulikov293eb1e2011-07-26 16:08:38 -07002756 result = mutex_lock_killable(&task->signal->cred_guard_mutex);
2757 if (result)
2758 return result;
2759
Jann Horncaaee622016-01-20 15:00:04 -08002760 if (!ptrace_may_access(task, PTRACE_MODE_READ_FSCREDS)) {
Vasiliy Kulikov293eb1e2011-07-26 16:08:38 -07002761 result = -EACCES;
2762 goto out_unlock;
2763 }
Vasiliy Kulikov1d1221f2011-06-24 16:08:38 +04002764
Andrea Righi59954772008-07-27 17:29:15 +02002765 if (whole && lock_task_sighand(task, &flags)) {
2766 struct task_struct *t = task;
Andrea Righib2d002d2008-07-26 15:22:27 -07002767
Andrea Righi59954772008-07-27 17:29:15 +02002768 task_io_accounting_add(&acct, &task->signal->ioac);
2769 while_each_thread(task, t)
2770 task_io_accounting_add(&acct, &t->ioac);
Andrea Righi297c5d92008-07-25 01:48:49 -07002771
Andrea Righi59954772008-07-27 17:29:15 +02002772 unlock_task_sighand(task, &flags);
Andrea Righi297c5d92008-07-25 01:48:49 -07002773 }
Joe Perches25ce3192015-04-15 16:18:17 -07002774 seq_printf(m,
2775 "rchar: %llu\n"
2776 "wchar: %llu\n"
2777 "syscr: %llu\n"
2778 "syscw: %llu\n"
2779 "read_bytes: %llu\n"
2780 "write_bytes: %llu\n"
2781 "cancelled_write_bytes: %llu\n",
2782 (unsigned long long)acct.rchar,
2783 (unsigned long long)acct.wchar,
2784 (unsigned long long)acct.syscr,
2785 (unsigned long long)acct.syscw,
2786 (unsigned long long)acct.read_bytes,
2787 (unsigned long long)acct.write_bytes,
2788 (unsigned long long)acct.cancelled_write_bytes);
2789 result = 0;
2790
Vasiliy Kulikov293eb1e2011-07-26 16:08:38 -07002791out_unlock:
2792 mutex_unlock(&task->signal->cred_guard_mutex);
2793 return result;
Andrew Mortonaba76fd2006-12-10 02:19:48 -08002794}
Andrea Righi297c5d92008-07-25 01:48:49 -07002795
Alexey Dobriyan19aadc92014-08-08 14:21:50 -07002796static int proc_tid_io_accounting(struct seq_file *m, struct pid_namespace *ns,
2797 struct pid *pid, struct task_struct *task)
Andrea Righi297c5d92008-07-25 01:48:49 -07002798{
Alexey Dobriyan19aadc92014-08-08 14:21:50 -07002799 return do_io_accounting(task, m, 0);
Andrea Righi297c5d92008-07-25 01:48:49 -07002800}
2801
Alexey Dobriyan19aadc92014-08-08 14:21:50 -07002802static int proc_tgid_io_accounting(struct seq_file *m, struct pid_namespace *ns,
2803 struct pid *pid, struct task_struct *task)
Andrea Righi297c5d92008-07-25 01:48:49 -07002804{
Alexey Dobriyan19aadc92014-08-08 14:21:50 -07002805 return do_io_accounting(task, m, 1);
Andrea Righi297c5d92008-07-25 01:48:49 -07002806}
2807#endif /* CONFIG_TASK_IO_ACCOUNTING */
Andrew Mortonaba76fd2006-12-10 02:19:48 -08002808
Eric W. Biederman22d917d2011-11-17 00:11:58 -08002809#ifdef CONFIG_USER_NS
2810static int proc_id_map_open(struct inode *inode, struct file *file,
Fabian Frederickccf94f12014-08-08 14:21:22 -07002811 const struct seq_operations *seq_ops)
Eric W. Biederman22d917d2011-11-17 00:11:58 -08002812{
2813 struct user_namespace *ns = NULL;
2814 struct task_struct *task;
2815 struct seq_file *seq;
2816 int ret = -EINVAL;
2817
2818 task = get_proc_task(inode);
2819 if (task) {
2820 rcu_read_lock();
2821 ns = get_user_ns(task_cred_xxx(task, user_ns));
2822 rcu_read_unlock();
2823 put_task_struct(task);
2824 }
2825 if (!ns)
2826 goto err;
2827
2828 ret = seq_open(file, seq_ops);
2829 if (ret)
2830 goto err_put_ns;
2831
2832 seq = file->private_data;
2833 seq->private = ns;
2834
2835 return 0;
2836err_put_ns:
2837 put_user_ns(ns);
2838err:
2839 return ret;
2840}
2841
2842static int proc_id_map_release(struct inode *inode, struct file *file)
2843{
2844 struct seq_file *seq = file->private_data;
2845 struct user_namespace *ns = seq->private;
2846 put_user_ns(ns);
2847 return seq_release(inode, file);
2848}
2849
2850static int proc_uid_map_open(struct inode *inode, struct file *file)
2851{
2852 return proc_id_map_open(inode, file, &proc_uid_seq_operations);
2853}
2854
2855static int proc_gid_map_open(struct inode *inode, struct file *file)
2856{
2857 return proc_id_map_open(inode, file, &proc_gid_seq_operations);
2858}
2859
Eric W. Biedermanf76d2072012-08-30 01:24:05 -07002860static int proc_projid_map_open(struct inode *inode, struct file *file)
2861{
2862 return proc_id_map_open(inode, file, &proc_projid_seq_operations);
2863}
2864
Eric W. Biederman22d917d2011-11-17 00:11:58 -08002865static const struct file_operations proc_uid_map_operations = {
2866 .open = proc_uid_map_open,
2867 .write = proc_uid_map_write,
2868 .read = seq_read,
2869 .llseek = seq_lseek,
2870 .release = proc_id_map_release,
2871};
2872
2873static const struct file_operations proc_gid_map_operations = {
2874 .open = proc_gid_map_open,
2875 .write = proc_gid_map_write,
2876 .read = seq_read,
2877 .llseek = seq_lseek,
2878 .release = proc_id_map_release,
2879};
Eric W. Biedermanf76d2072012-08-30 01:24:05 -07002880
2881static const struct file_operations proc_projid_map_operations = {
2882 .open = proc_projid_map_open,
2883 .write = proc_projid_map_write,
2884 .read = seq_read,
2885 .llseek = seq_lseek,
2886 .release = proc_id_map_release,
2887};
Eric W. Biederman9cc46512014-12-02 12:27:26 -06002888
2889static int proc_setgroups_open(struct inode *inode, struct file *file)
2890{
2891 struct user_namespace *ns = NULL;
2892 struct task_struct *task;
2893 int ret;
2894
2895 ret = -ESRCH;
2896 task = get_proc_task(inode);
2897 if (task) {
2898 rcu_read_lock();
2899 ns = get_user_ns(task_cred_xxx(task, user_ns));
2900 rcu_read_unlock();
2901 put_task_struct(task);
2902 }
2903 if (!ns)
2904 goto err;
2905
2906 if (file->f_mode & FMODE_WRITE) {
2907 ret = -EACCES;
2908 if (!ns_capable(ns, CAP_SYS_ADMIN))
2909 goto err_put_ns;
2910 }
2911
2912 ret = single_open(file, &proc_setgroups_show, ns);
2913 if (ret)
2914 goto err_put_ns;
2915
2916 return 0;
2917err_put_ns:
2918 put_user_ns(ns);
2919err:
2920 return ret;
2921}
2922
2923static int proc_setgroups_release(struct inode *inode, struct file *file)
2924{
2925 struct seq_file *seq = file->private_data;
2926 struct user_namespace *ns = seq->private;
2927 int ret = single_release(inode, file);
2928 put_user_ns(ns);
2929 return ret;
2930}
2931
2932static const struct file_operations proc_setgroups_operations = {
2933 .open = proc_setgroups_open,
2934 .write = proc_setgroups_write,
2935 .read = seq_read,
2936 .llseek = seq_lseek,
2937 .release = proc_setgroups_release,
2938};
Eric W. Biederman22d917d2011-11-17 00:11:58 -08002939#endif /* CONFIG_USER_NS */
2940
Kees Cook47830722008-10-06 03:11:58 +04002941static int proc_pid_personality(struct seq_file *m, struct pid_namespace *ns,
2942 struct pid *pid, struct task_struct *task)
2943{
Al Viroa9712bc2011-03-23 15:52:50 -04002944 int err = lock_trace(task);
2945 if (!err) {
2946 seq_printf(m, "%08x\n", task->personality);
2947 unlock_trace(task);
2948 }
2949 return err;
Kees Cook47830722008-10-06 03:11:58 +04002950}
2951
Josh Poimboeuf7c23b332017-02-13 19:42:41 -06002952#ifdef CONFIG_LIVEPATCH
2953static int proc_pid_patch_state(struct seq_file *m, struct pid_namespace *ns,
2954 struct pid *pid, struct task_struct *task)
2955{
2956 seq_printf(m, "%d\n", task->patch_state);
2957 return 0;
2958}
2959#endif /* CONFIG_LIVEPATCH */
2960
Alexander Popovc8d12622018-08-17 01:17:01 +03002961#ifdef CONFIG_STACKLEAK_METRICS
2962static int proc_stack_depth(struct seq_file *m, struct pid_namespace *ns,
2963 struct pid *pid, struct task_struct *task)
2964{
2965 unsigned long prev_depth = THREAD_SIZE -
2966 (task->prev_lowest_stack & (THREAD_SIZE - 1));
2967 unsigned long depth = THREAD_SIZE -
2968 (task->lowest_stack & (THREAD_SIZE - 1));
2969
2970 seq_printf(m, "previous stack depth: %lu\nstack depth: %lu\n",
2971 prev_depth, depth);
2972 return 0;
2973}
2974#endif /* CONFIG_STACKLEAK_METRICS */
2975
Eric W. Biederman801199c2006-10-02 02:18:48 -07002976/*
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002977 * Thread groups
2978 */
Arjan van de Ven00977a52007-02-12 00:55:34 -08002979static const struct file_operations proc_task_operations;
Arjan van de Venc5ef1c42007-02-12 00:55:40 -08002980static const struct inode_operations proc_task_inode_operations;
Eric W. Biederman20cdc892006-10-02 02:17:07 -07002981
Eric Dumazetc5141e62007-05-08 00:26:15 -07002982static const struct pid_entry tgid_base_stuff[] = {
Alexey Dobriyan631f9c12008-11-10 01:32:52 +03002983 DIR("task", S_IRUGO|S_IXUGO, proc_task_inode_operations, proc_task_operations),
2984 DIR("fd", S_IRUSR|S_IXUSR, proc_fd_inode_operations, proc_fd_operations),
Pavel Emelyanov640708a2012-01-10 15:11:23 -08002985 DIR("map_files", S_IRUSR|S_IXUSR, proc_map_files_inode_operations, proc_map_files_operations),
Alexey Dobriyan631f9c12008-11-10 01:32:52 +03002986 DIR("fdinfo", S_IRUSR|S_IXUSR, proc_fdinfo_inode_operations, proc_fdinfo_operations),
Eric W. Biederman6b4e3062010-03-07 16:41:34 -08002987 DIR("ns", S_IRUSR|S_IXUGO, proc_ns_dir_inode_operations, proc_ns_dir_operations),
Andrew Mortonb2211a32008-03-11 18:03:35 -07002988#ifdef CONFIG_NET
Alexey Dobriyan631f9c12008-11-10 01:32:52 +03002989 DIR("net", S_IRUGO|S_IXUGO, proc_net_inode_operations, proc_net_operations),
Andrew Mortonb2211a32008-03-11 18:03:35 -07002990#endif
Alexey Dobriyan631f9c12008-11-10 01:32:52 +03002991 REG("environ", S_IRUSR, proc_environ_operations),
Al Viroc5317162016-10-05 18:43:43 -04002992 REG("auxv", S_IRUSR, proc_auxv_operations),
Alexey Dobriyan631f9c12008-11-10 01:32:52 +03002993 ONE("status", S_IRUGO, proc_pid_status),
Djalal Harouni35a35042014-04-07 15:38:36 -07002994 ONE("personality", S_IRUSR, proc_pid_personality),
Alexey Dobriyan1c963eb2014-08-08 14:21:37 -07002995 ONE("limits", S_IRUGO, proc_pid_limits),
Ingo Molnar43ae34c2007-07-09 18:52:00 +02002996#ifdef CONFIG_SCHED_DEBUG
Alexey Dobriyan631f9c12008-11-10 01:32:52 +03002997 REG("sched", S_IRUGO|S_IWUSR, proc_pid_sched_operations),
Ingo Molnar43ae34c2007-07-09 18:52:00 +02002998#endif
Mike Galbraith5091faa2010-11-30 14:18:03 +01002999#ifdef CONFIG_SCHED_AUTOGROUP
3000 REG("autogroup", S_IRUGO|S_IWUSR, proc_pid_sched_autogroup_operations),
3001#endif
john stultz4614a696b2009-12-14 18:00:05 -08003002 REG("comm", S_IRUGO|S_IWUSR, proc_pid_set_comm_operations),
Roland McGrathebcb6732008-07-25 19:46:00 -07003003#ifdef CONFIG_HAVE_ARCH_TRACEHOOK
Alexey Dobriyan09d93bd2014-08-08 14:21:39 -07003004 ONE("syscall", S_IRUSR, proc_pid_syscall),
Roland McGrathebcb6732008-07-25 19:46:00 -07003005#endif
Alexey Dobriyanc2c0bb42015-06-25 15:00:54 -07003006 REG("cmdline", S_IRUGO, proc_pid_cmdline_ops),
Alexey Dobriyan631f9c12008-11-10 01:32:52 +03003007 ONE("stat", S_IRUGO, proc_tgid_stat),
3008 ONE("statm", S_IRUGO, proc_pid_statm),
Siddhesh Poyarekarb7643752012-03-21 16:34:04 -07003009 REG("maps", S_IRUGO, proc_pid_maps_operations),
Eric W. Biederman28a6d672006-10-02 02:17:05 -07003010#ifdef CONFIG_NUMA
Siddhesh Poyarekarb7643752012-03-21 16:34:04 -07003011 REG("numa_maps", S_IRUGO, proc_pid_numa_maps_operations),
Eric W. Biederman28a6d672006-10-02 02:17:05 -07003012#endif
Alexey Dobriyan631f9c12008-11-10 01:32:52 +03003013 REG("mem", S_IRUSR|S_IWUSR, proc_mem_operations),
3014 LNK("cwd", proc_cwd_link),
3015 LNK("root", proc_root_link),
3016 LNK("exe", proc_exe_link),
3017 REG("mounts", S_IRUGO, proc_mounts_operations),
3018 REG("mountinfo", S_IRUGO, proc_mountinfo_operations),
3019 REG("mountstats", S_IRUSR, proc_mountstats_operations),
Matt Mackall1e883282008-02-04 22:29:07 -08003020#ifdef CONFIG_PROC_PAGE_MONITOR
Alexey Dobriyan631f9c12008-11-10 01:32:52 +03003021 REG("clear_refs", S_IWUSR, proc_clear_refs_operations),
Siddhesh Poyarekarb7643752012-03-21 16:34:04 -07003022 REG("smaps", S_IRUGO, proc_pid_smaps_operations),
Daniel Colascione493b0e92017-09-06 16:25:08 -07003023 REG("smaps_rollup", S_IRUGO, proc_pid_smaps_rollup_operations),
Djalal Harouni32ed74a2014-04-07 15:38:38 -07003024 REG("pagemap", S_IRUSR, proc_pagemap_operations),
Eric W. Biederman28a6d672006-10-02 02:17:05 -07003025#endif
3026#ifdef CONFIG_SECURITY
Alexey Dobriyan631f9c12008-11-10 01:32:52 +03003027 DIR("attr", S_IRUGO|S_IXUGO, proc_attr_dir_inode_operations, proc_attr_dir_operations),
Eric W. Biederman28a6d672006-10-02 02:17:05 -07003028#endif
3029#ifdef CONFIG_KALLSYMS
Alexey Dobriyanedfcd602014-08-08 14:21:44 -07003030 ONE("wchan", S_IRUGO, proc_pid_wchan),
Eric W. Biederman28a6d672006-10-02 02:17:05 -07003031#endif
Ken Chen2ec220e2008-11-10 11:26:08 +03003032#ifdef CONFIG_STACKTRACE
Djalal Harouni35a35042014-04-07 15:38:36 -07003033 ONE("stack", S_IRUSR, proc_pid_stack),
Eric W. Biederman28a6d672006-10-02 02:17:05 -07003034#endif
Naveen N. Rao5968cec2015-06-30 14:36:03 +05303035#ifdef CONFIG_SCHED_INFO
Alexey Dobriyanf6e826c2014-08-08 14:21:46 -07003036 ONE("schedstat", S_IRUGO, proc_pid_schedstat),
Eric W. Biederman28a6d672006-10-02 02:17:05 -07003037#endif
Arjan van de Ven97455122008-01-25 21:08:34 +01003038#ifdef CONFIG_LATENCYTOP
Alexey Dobriyan631f9c12008-11-10 01:32:52 +03003039 REG("latency", S_IRUGO, proc_lstats_operations),
Arjan van de Ven97455122008-01-25 21:08:34 +01003040#endif
Paul Menage8793d852007-10-18 23:39:39 -07003041#ifdef CONFIG_PROC_PID_CPUSET
Zefan Li52de4772014-09-18 16:03:36 +08003042 ONE("cpuset", S_IRUGO, proc_cpuset_show),
Eric W. Biederman28a6d672006-10-02 02:17:05 -07003043#endif
Paul Menagea4243162007-10-18 23:39:35 -07003044#ifdef CONFIG_CGROUPS
Zefan Li006f4ac2014-09-18 16:03:15 +08003045 ONE("cgroup", S_IRUGO, proc_cgroup_show),
Paul Menagea4243162007-10-18 23:39:35 -07003046#endif
Alexey Dobriyan6ba51e32014-08-08 14:21:48 -07003047 ONE("oom_score", S_IRUGO, proc_oom_score),
David Rientjesfa0cbbf2012-11-12 17:53:04 -08003048 REG("oom_adj", S_IRUGO|S_IWUSR, proc_oom_adj_operations),
David Rientjesa63d83f2010-08-09 17:19:46 -07003049 REG("oom_score_adj", S_IRUGO|S_IWUSR, proc_oom_score_adj_operations),
Richard Guy Briggs4b7d2482019-01-22 17:06:39 -05003050#ifdef CONFIG_AUDIT
Alexey Dobriyan631f9c12008-11-10 01:32:52 +03003051 REG("loginuid", S_IWUSR|S_IRUGO, proc_loginuid_operations),
3052 REG("sessionid", S_IRUGO, proc_sessionid_operations),
Eric W. Biederman28a6d672006-10-02 02:17:05 -07003053#endif
Akinobu Mitaf4f154f2006-12-08 02:39:47 -08003054#ifdef CONFIG_FAULT_INJECTION
Alexey Dobriyan631f9c12008-11-10 01:32:52 +03003055 REG("make-it-fail", S_IRUGO|S_IWUSR, proc_fault_inject_operations),
Akinobu Mita168c42b2017-07-14 14:50:00 -07003056 REG("fail-nth", 0644, proc_fail_nth_operations),
Akinobu Mitaf4f154f2006-12-08 02:39:47 -08003057#endif
Christoph Hellwig698ba7b2009-12-15 16:47:37 -08003058#ifdef CONFIG_ELF_CORE
Alexey Dobriyan631f9c12008-11-10 01:32:52 +03003059 REG("coredump_filter", S_IRUGO|S_IWUSR, proc_coredump_filter_operations),
Kawai, Hidehiro3cb4a0b2007-07-19 01:48:28 -07003060#endif
Andrew Mortonaba76fd2006-12-10 02:19:48 -08003061#ifdef CONFIG_TASK_IO_ACCOUNTING
Alexey Dobriyan19aadc92014-08-08 14:21:50 -07003062 ONE("io", S_IRUSR, proc_tgid_io_accounting),
Andrew Mortonaba76fd2006-12-10 02:19:48 -08003063#endif
Eric W. Biederman22d917d2011-11-17 00:11:58 -08003064#ifdef CONFIG_USER_NS
3065 REG("uid_map", S_IRUGO|S_IWUSR, proc_uid_map_operations),
3066 REG("gid_map", S_IRUGO|S_IWUSR, proc_gid_map_operations),
Eric W. Biedermanf76d2072012-08-30 01:24:05 -07003067 REG("projid_map", S_IRUGO|S_IWUSR, proc_projid_map_operations),
Eric W. Biederman9cc46512014-12-02 12:27:26 -06003068 REG("setgroups", S_IRUGO|S_IWUSR, proc_setgroups_operations),
Eric W. Biederman22d917d2011-11-17 00:11:58 -08003069#endif
Nicolas Pitreb18b6a92017-01-21 00:09:08 -05003070#if defined(CONFIG_CHECKPOINT_RESTORE) && defined(CONFIG_POSIX_TIMERS)
Pavel Emelyanov48f6a7a2013-03-11 13:12:45 +04003071 REG("timers", S_IRUGO, proc_timers_operations),
3072#endif
John Stultz5de23d42016-03-17 14:20:54 -07003073 REG("timerslack_ns", S_IRUGO|S_IWUGO, proc_pid_set_timerslack_ns_operations),
Josh Poimboeuf7c23b332017-02-13 19:42:41 -06003074#ifdef CONFIG_LIVEPATCH
3075 ONE("patch_state", S_IRUSR, proc_pid_patch_state),
3076#endif
Alexander Popovc8d12622018-08-17 01:17:01 +03003077#ifdef CONFIG_STACKLEAK_METRICS
3078 ONE("stack_depth", S_IRUGO, proc_stack_depth),
3079#endif
Aubrey Li68bc30b2019-06-06 09:22:34 +08003080#ifdef CONFIG_PROC_PID_ARCH_STATUS
3081 ONE("arch_status", S_IRUGO, proc_pid_arch_status),
3082#endif
Eric W. Biederman28a6d672006-10-02 02:17:05 -07003083};
3084
Al Virof0c3b502013-05-16 12:07:31 -04003085static int proc_tgid_base_readdir(struct file *file, struct dir_context *ctx)
Eric W. Biederman28a6d672006-10-02 02:17:05 -07003086{
Al Virof0c3b502013-05-16 12:07:31 -04003087 return proc_pident_readdir(file, ctx,
3088 tgid_base_stuff, ARRAY_SIZE(tgid_base_stuff));
Eric W. Biederman28a6d672006-10-02 02:17:05 -07003089}
3090
Arjan van de Ven00977a52007-02-12 00:55:34 -08003091static const struct file_operations proc_tgid_base_operations = {
Eric W. Biederman28a6d672006-10-02 02:17:05 -07003092 .read = generic_read_dir,
Al Virof50752e2016-04-20 17:13:54 -04003093 .iterate_shared = proc_tgid_base_readdir,
3094 .llseek = generic_file_llseek,
Eric W. Biederman28a6d672006-10-02 02:17:05 -07003095};
3096
Christian Brauner3eb39f42018-11-19 00:51:56 +01003097struct pid *tgid_pidfd_to_pid(const struct file *file)
3098{
Christian Brauner30d158b2019-06-27 11:35:14 +02003099 if (file->f_op != &proc_tgid_base_operations)
Christian Brauner3eb39f42018-11-19 00:51:56 +01003100 return ERR_PTR(-EBADF);
3101
3102 return proc_pid(file_inode(file));
3103}
3104
Al Viro00cd8dd2012-06-10 17:13:09 -04003105static struct dentry *proc_tgid_base_lookup(struct inode *dir, struct dentry *dentry, unsigned int flags)
3106{
Eric W. Biederman7bcd6b02006-10-02 02:18:56 -07003107 return proc_pident_lookup(dir, dentry,
Alexey Dobriyand5a572a2019-03-11 23:28:51 -07003108 tgid_base_stuff,
3109 tgid_base_stuff + ARRAY_SIZE(tgid_base_stuff));
Eric W. Biederman28a6d672006-10-02 02:17:05 -07003110}
3111
Arjan van de Venc5ef1c42007-02-12 00:55:40 -08003112static const struct inode_operations proc_tgid_base_inode_operations = {
Eric W. Biederman28a6d672006-10-02 02:17:05 -07003113 .lookup = proc_tgid_base_lookup,
3114 .getattr = pid_getattr,
3115 .setattr = proc_setattr,
Vasiliy Kulikov04996802012-01-10 15:11:31 -08003116 .permission = proc_pid_permission,
Eric W. Biederman28a6d672006-10-02 02:17:05 -07003117};
3118
Pavel Emelyanov60347f62007-10-18 23:40:03 -07003119static void proc_flush_task_mnt(struct vfsmount *mnt, pid_t pid, pid_t tgid)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003120{
Eric W. Biederman48e64842006-06-26 00:25:48 -07003121 struct dentry *dentry, *leader, *dir;
Alexey Dobriyane3912ac2018-02-06 15:36:51 -08003122 char buf[10 + 1];
Eric W. Biederman48e64842006-06-26 00:25:48 -07003123 struct qstr name;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003124
Eric W. Biederman48e64842006-06-26 00:25:48 -07003125 name.name = buf;
Alexey Dobriyane3912ac2018-02-06 15:36:51 -08003126 name.len = snprintf(buf, sizeof(buf), "%u", pid);
Al Viro4f522a22013-02-11 23:20:37 -05003127 /* no ->d_hash() rejects on procfs */
Pavel Emelyanov60347f62007-10-18 23:40:03 -07003128 dentry = d_hash_and_lookup(mnt->mnt_root, &name);
Eric W. Biederman48e64842006-06-26 00:25:48 -07003129 if (dentry) {
Eric W. Biedermanbbd51922014-02-13 10:24:23 -08003130 d_invalidate(dentry);
Eric W. Biederman48e64842006-06-26 00:25:48 -07003131 dput(dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003132 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003133
Oleg Nesterovc35a7f12014-12-10 15:54:56 -08003134 if (pid == tgid)
3135 return;
3136
Eric W. Biederman48e64842006-06-26 00:25:48 -07003137 name.name = buf;
Alexey Dobriyane3912ac2018-02-06 15:36:51 -08003138 name.len = snprintf(buf, sizeof(buf), "%u", tgid);
Pavel Emelyanov60347f62007-10-18 23:40:03 -07003139 leader = d_hash_and_lookup(mnt->mnt_root, &name);
Eric W. Biederman48e64842006-06-26 00:25:48 -07003140 if (!leader)
3141 goto out;
3142
3143 name.name = "task";
3144 name.len = strlen(name.name);
3145 dir = d_hash_and_lookup(leader, &name);
3146 if (!dir)
3147 goto out_put_leader;
3148
3149 name.name = buf;
Alexey Dobriyane3912ac2018-02-06 15:36:51 -08003150 name.len = snprintf(buf, sizeof(buf), "%u", pid);
Eric W. Biederman48e64842006-06-26 00:25:48 -07003151 dentry = d_hash_and_lookup(dir, &name);
3152 if (dentry) {
Eric W. Biedermanbbd51922014-02-13 10:24:23 -08003153 d_invalidate(dentry);
Eric W. Biederman48e64842006-06-26 00:25:48 -07003154 dput(dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003155 }
Eric W. Biederman48e64842006-06-26 00:25:48 -07003156
3157 dput(dir);
3158out_put_leader:
3159 dput(leader);
3160out:
3161 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003162}
3163
Randy Dunlap0895e912007-10-21 21:00:10 -07003164/**
3165 * proc_flush_task - Remove dcache entries for @task from the /proc dcache.
3166 * @task: task that should be flushed.
3167 *
3168 * When flushing dentries from proc, one needs to flush them from global
Pavel Emelyanov60347f62007-10-18 23:40:03 -07003169 * proc (proc_mnt) and from all the namespaces' procs this task was seen
Randy Dunlap0895e912007-10-21 21:00:10 -07003170 * in. This call is supposed to do all of this job.
3171 *
3172 * Looks in the dcache for
3173 * /proc/@pid
3174 * /proc/@tgid/task/@pid
3175 * if either directory is present flushes it and all of it'ts children
3176 * from the dcache.
3177 *
3178 * It is safe and reasonable to cache /proc entries for a task until
3179 * that task exits. After that they just clog up the dcache with
3180 * useless entries, possibly causing useful dcache entries to be
3181 * flushed instead. This routine is proved to flush those useless
3182 * dcache entries at process exit time.
3183 *
3184 * NOTE: This routine is just an optimization so it does not guarantee
3185 * that no dcache entries will exist at process exit time it
3186 * just makes it very unlikely that any will persist.
Pavel Emelyanov60347f62007-10-18 23:40:03 -07003187 */
3188
3189void proc_flush_task(struct task_struct *task)
3190{
Eric W. Biederman9fcc2d12007-11-14 17:00:07 -08003191 int i;
Oleg Nesterov9b4d1cbe2009-09-22 16:45:34 -07003192 struct pid *pid, *tgid;
Pavel Emelyanov130f77e2007-10-18 23:40:11 -07003193 struct upid *upid;
3194
Pavel Emelyanov130f77e2007-10-18 23:40:11 -07003195 pid = task_pid(task);
Oleg Nesterov9b4d1cbe2009-09-22 16:45:34 -07003196 tgid = task_tgid(task);
Pavel Emelyanov130f77e2007-10-18 23:40:11 -07003197
Eric W. Biederman9fcc2d12007-11-14 17:00:07 -08003198 for (i = 0; i <= pid->level; i++) {
Pavel Emelyanov130f77e2007-10-18 23:40:11 -07003199 upid = &pid->numbers[i];
3200 proc_flush_task_mnt(upid->ns->proc_mnt, upid->nr,
Oleg Nesterov9b4d1cbe2009-09-22 16:45:34 -07003201 tgid->numbers[i].nr);
Pavel Emelyanov130f77e2007-10-18 23:40:11 -07003202 }
Pavel Emelyanov60347f62007-10-18 23:40:03 -07003203}
3204
Al Viro0168b9e2018-05-03 09:21:05 -04003205static struct dentry *proc_pid_instantiate(struct dentry * dentry,
Al Viroc52a47a2013-06-15 11:15:20 +04003206 struct task_struct *task, const void *ptr)
Eric W. Biederman444ceed2006-10-02 02:18:49 -07003207{
Eric W. Biederman444ceed2006-10-02 02:18:49 -07003208 struct inode *inode;
3209
Al Viro0168b9e2018-05-03 09:21:05 -04003210 inode = proc_pid_make_inode(dentry->d_sb, task, S_IFDIR | S_IRUGO | S_IXUGO);
Eric W. Biederman444ceed2006-10-02 02:18:49 -07003211 if (!inode)
Al Viro0168b9e2018-05-03 09:21:05 -04003212 return ERR_PTR(-ENOENT);
Eric W. Biederman444ceed2006-10-02 02:18:49 -07003213
Eric W. Biederman444ceed2006-10-02 02:18:49 -07003214 inode->i_op = &proc_tgid_base_inode_operations;
3215 inode->i_fop = &proc_tgid_base_operations;
3216 inode->i_flags|=S_IMMUTABLE;
Vegard Nossumaed54172008-06-05 22:46:53 -07003217
Alexey Dobriyan1270dd82016-12-12 16:45:32 -08003218 set_nlink(inode, nlink_tgid);
Al Viro1bbc5512018-05-02 21:26:16 -04003219 pid_update_inode(task, inode);
Eric W. Biederman444ceed2006-10-02 02:18:49 -07003220
Nick Pigginfb045ad2011-01-07 17:49:55 +11003221 d_set_d_op(dentry, &pid_dentry_operations);
Al Viro0168b9e2018-05-03 09:21:05 -04003222 return d_splice_alias(inode, dentry);
Eric W. Biederman444ceed2006-10-02 02:18:49 -07003223}
3224
Zhikang Zhang867aacc2019-03-05 15:50:29 -08003225struct dentry *proc_pid_lookup(struct dentry *dentry, unsigned int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003226{
3227 struct task_struct *task;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003228 unsigned tgid;
Pavel Emelyanovb4888932007-10-18 23:40:14 -07003229 struct pid_namespace *ns;
Al Viro0168b9e2018-05-03 09:21:05 -04003230 struct dentry *result = ERR_PTR(-ENOENT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003231
Alexey Dobriyandbcdb502014-08-08 14:21:25 -07003232 tgid = name_to_int(&dentry->d_name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003233 if (tgid == ~0U)
3234 goto out;
3235
Pavel Emelyanovb4888932007-10-18 23:40:14 -07003236 ns = dentry->d_sb->s_fs_info;
Eric W. Biedermande758732006-06-26 00:25:51 -07003237 rcu_read_lock();
Pavel Emelyanovb4888932007-10-18 23:40:14 -07003238 task = find_task_by_pid_ns(tgid, ns);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003239 if (task)
3240 get_task_struct(task);
Eric W. Biedermande758732006-06-26 00:25:51 -07003241 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07003242 if (!task)
3243 goto out;
3244
Al Viro0168b9e2018-05-03 09:21:05 -04003245 result = proc_pid_instantiate(dentry, task, NULL);
Eric W. Biederman48e64842006-06-26 00:25:48 -07003246 put_task_struct(task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003247out:
Al Viro0168b9e2018-05-03 09:21:05 -04003248 return result;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003249}
3250
Linus Torvalds1da177e2005-04-16 15:20:36 -07003251/*
Eric W. Biederman0804ef42006-10-02 02:17:04 -07003252 * Find the first task with tgid >= tgid
Eric W. Biederman0bc58a92006-06-26 00:25:50 -07003253 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07003254 */
Eric W. Biederman19fd4bb2007-11-28 16:21:26 -08003255struct tgid_iter {
3256 unsigned int tgid;
Eric W. Biederman0804ef42006-10-02 02:17:04 -07003257 struct task_struct *task;
Eric W. Biederman19fd4bb2007-11-28 16:21:26 -08003258};
3259static struct tgid_iter next_tgid(struct pid_namespace *ns, struct tgid_iter iter)
3260{
Eric W. Biederman0804ef42006-10-02 02:17:04 -07003261 struct pid *pid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003262
Eric W. Biederman19fd4bb2007-11-28 16:21:26 -08003263 if (iter.task)
3264 put_task_struct(iter.task);
Eric W. Biederman0804ef42006-10-02 02:17:04 -07003265 rcu_read_lock();
3266retry:
Eric W. Biederman19fd4bb2007-11-28 16:21:26 -08003267 iter.task = NULL;
3268 pid = find_ge_pid(iter.tgid, ns);
Eric W. Biederman0804ef42006-10-02 02:17:04 -07003269 if (pid) {
Eric W. Biederman19fd4bb2007-11-28 16:21:26 -08003270 iter.tgid = pid_nr_ns(pid, ns);
3271 iter.task = pid_task(pid, PIDTYPE_PID);
Eric W. Biederman0804ef42006-10-02 02:17:04 -07003272 /* What we to know is if the pid we have find is the
3273 * pid of a thread_group_leader. Testing for task
3274 * being a thread_group_leader is the obvious thing
3275 * todo but there is a window when it fails, due to
3276 * the pid transfer logic in de_thread.
3277 *
3278 * So we perform the straight forward test of seeing
3279 * if the pid we have found is the pid of a thread
3280 * group leader, and don't worry if the task we have
3281 * found doesn't happen to be a thread group leader.
3282 * As we don't care in the case of readdir.
3283 */
Eric W. Biederman19fd4bb2007-11-28 16:21:26 -08003284 if (!iter.task || !has_group_leader_pid(iter.task)) {
3285 iter.tgid += 1;
Eric W. Biederman0804ef42006-10-02 02:17:04 -07003286 goto retry;
Eric W. Biederman19fd4bb2007-11-28 16:21:26 -08003287 }
3288 get_task_struct(iter.task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003289 }
Eric W. Biederman454cc102006-06-26 00:25:51 -07003290 rcu_read_unlock();
Eric W. Biederman19fd4bb2007-11-28 16:21:26 -08003291 return iter;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003292}
3293
Eric W. Biederman00978752014-07-31 03:10:50 -07003294#define TGID_OFFSET (FIRST_PROCESS_ENTRY + 2)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003295
3296/* for the /proc/ directory itself, after non-process stuff has been done */
Al Virof0c3b502013-05-16 12:07:31 -04003297int proc_pid_readdir(struct file *file, struct dir_context *ctx)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003298{
Eric W. Biederman19fd4bb2007-11-28 16:21:26 -08003299 struct tgid_iter iter;
Christoph Hellwig76f668b2018-05-16 07:19:01 +02003300 struct pid_namespace *ns = proc_pid_ns(file_inode(file));
Al Virof0c3b502013-05-16 12:07:31 -04003301 loff_t pos = ctx->pos;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003302
Al Viro021ada72013-03-29 19:27:05 -04003303 if (pos >= PID_MAX_LIMIT + TGID_OFFSET)
Al Virof0c3b502013-05-16 12:07:31 -04003304 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003305
Eric W. Biederman00978752014-07-31 03:10:50 -07003306 if (pos == TGID_OFFSET - 2) {
David Howells2b0143b2015-03-17 22:25:59 +00003307 struct inode *inode = d_inode(ns->proc_self);
Al Virodb963162013-06-15 10:45:10 +04003308 if (!dir_emit(ctx, "self", 4, inode->i_ino, DT_LNK))
Al Virof0c3b502013-05-16 12:07:31 -04003309 return 0;
Eric W. Biederman00978752014-07-31 03:10:50 -07003310 ctx->pos = pos = pos + 1;
Al Viro021ada72013-03-29 19:27:05 -04003311 }
Eric W. Biederman00978752014-07-31 03:10:50 -07003312 if (pos == TGID_OFFSET - 1) {
David Howells2b0143b2015-03-17 22:25:59 +00003313 struct inode *inode = d_inode(ns->proc_thread_self);
Eric W. Biederman00978752014-07-31 03:10:50 -07003314 if (!dir_emit(ctx, "thread-self", 11, inode->i_ino, DT_LNK))
3315 return 0;
3316 ctx->pos = pos = pos + 1;
3317 }
3318 iter.tgid = pos - TGID_OFFSET;
Eric W. Biederman19fd4bb2007-11-28 16:21:26 -08003319 iter.task = NULL;
Eric W. Biederman19fd4bb2007-11-28 16:21:26 -08003320 for (iter = next_tgid(ns, iter);
3321 iter.task;
3322 iter.tgid += 1, iter = next_tgid(ns, iter)) {
Alexey Dobriyane3912ac2018-02-06 15:36:51 -08003323 char name[10 + 1];
Alexey Dobriyana4ef3892018-06-07 17:10:10 -07003324 unsigned int len;
Eric Dumazet3ba4bce2017-01-24 15:18:07 -08003325
3326 cond_resched();
Lafcadio Wluiki796f5712017-02-24 15:00:23 -08003327 if (!has_pid_permissions(ns, iter.task, HIDEPID_INVISIBLE))
Al Virof0c3b502013-05-16 12:07:31 -04003328 continue;
Vasiliy Kulikov04996802012-01-10 15:11:31 -08003329
Alexey Dobriyane3912ac2018-02-06 15:36:51 -08003330 len = snprintf(name, sizeof(name), "%u", iter.tgid);
Al Virof0c3b502013-05-16 12:07:31 -04003331 ctx->pos = iter.tgid + TGID_OFFSET;
3332 if (!proc_fill_cache(file, ctx, name, len,
3333 proc_pid_instantiate, iter.task, NULL)) {
Eric W. Biederman19fd4bb2007-11-28 16:21:26 -08003334 put_task_struct(iter.task);
Al Virof0c3b502013-05-16 12:07:31 -04003335 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003336 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003337 }
Al Virof0c3b502013-05-16 12:07:31 -04003338 ctx->pos = PID_MAX_LIMIT + TGID_OFFSET;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003339 return 0;
3340}
3341
Eric W. Biederman0bc58a92006-06-26 00:25:50 -07003342/*
Janis Danisevskis1b3044e2016-05-20 17:00:08 -07003343 * proc_tid_comm_permission is a special permission function exclusively
3344 * used for the node /proc/<pid>/task/<tid>/comm.
3345 * It bypasses generic permission checks in the case where a task of the same
3346 * task group attempts to access the node.
3347 * The rationale behind this is that glibc and bionic access this node for
3348 * cross thread naming (pthread_set/getname_np(!self)). However, if
3349 * PR_SET_DUMPABLE gets set to 0 this node among others becomes uid=0 gid=0,
3350 * which locks out the cross thread naming implementation.
3351 * This function makes sure that the node is always accessible for members of
3352 * same thread group.
3353 */
3354static int proc_tid_comm_permission(struct inode *inode, int mask)
3355{
3356 bool is_same_tgroup;
3357 struct task_struct *task;
3358
3359 task = get_proc_task(inode);
3360 if (!task)
3361 return -ESRCH;
3362 is_same_tgroup = same_thread_group(current, task);
3363 put_task_struct(task);
3364
3365 if (likely(is_same_tgroup && !(mask & MAY_EXEC))) {
3366 /* This file (/proc/<pid>/task/<tid>/comm) can always be
3367 * read or written by the members of the corresponding
3368 * thread group.
3369 */
3370 return 0;
3371 }
3372
3373 return generic_permission(inode, mask);
3374}
3375
3376static const struct inode_operations proc_tid_comm_inode_operations = {
3377 .permission = proc_tid_comm_permission,
3378};
3379
3380/*
Eric W. Biederman28a6d672006-10-02 02:17:05 -07003381 * Tasks
3382 */
Eric Dumazetc5141e62007-05-08 00:26:15 -07003383static const struct pid_entry tid_base_stuff[] = {
Alexey Dobriyan631f9c12008-11-10 01:32:52 +03003384 DIR("fd", S_IRUSR|S_IXUSR, proc_fd_inode_operations, proc_fd_operations),
Jerome Marchand38355412010-04-27 13:13:06 -07003385 DIR("fdinfo", S_IRUSR|S_IXUSR, proc_fdinfo_inode_operations, proc_fdinfo_operations),
Eric W. Biederman6b4e3062010-03-07 16:41:34 -08003386 DIR("ns", S_IRUSR|S_IXUGO, proc_ns_dir_inode_operations, proc_ns_dir_operations),
Eric W. Biederman6ba8ed72014-07-31 16:27:08 -07003387#ifdef CONFIG_NET
3388 DIR("net", S_IRUGO|S_IXUGO, proc_net_inode_operations, proc_net_operations),
3389#endif
Alexey Dobriyan631f9c12008-11-10 01:32:52 +03003390 REG("environ", S_IRUSR, proc_environ_operations),
Al Viroc5317162016-10-05 18:43:43 -04003391 REG("auxv", S_IRUSR, proc_auxv_operations),
Alexey Dobriyan631f9c12008-11-10 01:32:52 +03003392 ONE("status", S_IRUGO, proc_pid_status),
Djalal Harouni35a35042014-04-07 15:38:36 -07003393 ONE("personality", S_IRUSR, proc_pid_personality),
Alexey Dobriyan1c963eb2014-08-08 14:21:37 -07003394 ONE("limits", S_IRUGO, proc_pid_limits),
Ingo Molnar43ae34c2007-07-09 18:52:00 +02003395#ifdef CONFIG_SCHED_DEBUG
Alexey Dobriyan631f9c12008-11-10 01:32:52 +03003396 REG("sched", S_IRUGO|S_IWUSR, proc_pid_sched_operations),
Ingo Molnar43ae34c2007-07-09 18:52:00 +02003397#endif
Janis Danisevskis1b3044e2016-05-20 17:00:08 -07003398 NOD("comm", S_IFREG|S_IRUGO|S_IWUSR,
3399 &proc_tid_comm_inode_operations,
3400 &proc_pid_set_comm_operations, {}),
Roland McGrathebcb6732008-07-25 19:46:00 -07003401#ifdef CONFIG_HAVE_ARCH_TRACEHOOK
Alexey Dobriyan09d93bd2014-08-08 14:21:39 -07003402 ONE("syscall", S_IRUSR, proc_pid_syscall),
Roland McGrathebcb6732008-07-25 19:46:00 -07003403#endif
Alexey Dobriyanc2c0bb42015-06-25 15:00:54 -07003404 REG("cmdline", S_IRUGO, proc_pid_cmdline_ops),
Alexey Dobriyan631f9c12008-11-10 01:32:52 +03003405 ONE("stat", S_IRUGO, proc_tid_stat),
3406 ONE("statm", S_IRUGO, proc_pid_statm),
Vlastimil Babka871305b2018-08-21 21:52:48 -07003407 REG("maps", S_IRUGO, proc_pid_maps_operations),
Iago López Galeiras2e13ba52015-06-25 15:00:57 -07003408#ifdef CONFIG_PROC_CHILDREN
Cyrill Gorcunov818411612012-05-31 16:26:43 -07003409 REG("children", S_IRUGO, proc_tid_children_operations),
3410#endif
Eric W. Biederman28a6d672006-10-02 02:17:05 -07003411#ifdef CONFIG_NUMA
Vlastimil Babka871305b2018-08-21 21:52:48 -07003412 REG("numa_maps", S_IRUGO, proc_pid_numa_maps_operations),
Eric W. Biederman28a6d672006-10-02 02:17:05 -07003413#endif
Alexey Dobriyan631f9c12008-11-10 01:32:52 +03003414 REG("mem", S_IRUSR|S_IWUSR, proc_mem_operations),
3415 LNK("cwd", proc_cwd_link),
3416 LNK("root", proc_root_link),
3417 LNK("exe", proc_exe_link),
3418 REG("mounts", S_IRUGO, proc_mounts_operations),
3419 REG("mountinfo", S_IRUGO, proc_mountinfo_operations),
Matt Mackall1e883282008-02-04 22:29:07 -08003420#ifdef CONFIG_PROC_PAGE_MONITOR
Alexey Dobriyan631f9c12008-11-10 01:32:52 +03003421 REG("clear_refs", S_IWUSR, proc_clear_refs_operations),
Vlastimil Babka871305b2018-08-21 21:52:48 -07003422 REG("smaps", S_IRUGO, proc_pid_smaps_operations),
Daniel Colascione493b0e92017-09-06 16:25:08 -07003423 REG("smaps_rollup", S_IRUGO, proc_pid_smaps_rollup_operations),
Djalal Harouni32ed74a2014-04-07 15:38:38 -07003424 REG("pagemap", S_IRUSR, proc_pagemap_operations),
Eric W. Biederman28a6d672006-10-02 02:17:05 -07003425#endif
3426#ifdef CONFIG_SECURITY
Alexey Dobriyan631f9c12008-11-10 01:32:52 +03003427 DIR("attr", S_IRUGO|S_IXUGO, proc_attr_dir_inode_operations, proc_attr_dir_operations),
Eric W. Biederman28a6d672006-10-02 02:17:05 -07003428#endif
3429#ifdef CONFIG_KALLSYMS
Alexey Dobriyanedfcd602014-08-08 14:21:44 -07003430 ONE("wchan", S_IRUGO, proc_pid_wchan),
Eric W. Biederman28a6d672006-10-02 02:17:05 -07003431#endif
Ken Chen2ec220e2008-11-10 11:26:08 +03003432#ifdef CONFIG_STACKTRACE
Djalal Harouni35a35042014-04-07 15:38:36 -07003433 ONE("stack", S_IRUSR, proc_pid_stack),
Eric W. Biederman28a6d672006-10-02 02:17:05 -07003434#endif
Naveen N. Rao5968cec2015-06-30 14:36:03 +05303435#ifdef CONFIG_SCHED_INFO
Alexey Dobriyanf6e826c2014-08-08 14:21:46 -07003436 ONE("schedstat", S_IRUGO, proc_pid_schedstat),
Eric W. Biederman28a6d672006-10-02 02:17:05 -07003437#endif
Arjan van de Ven97455122008-01-25 21:08:34 +01003438#ifdef CONFIG_LATENCYTOP
Alexey Dobriyan631f9c12008-11-10 01:32:52 +03003439 REG("latency", S_IRUGO, proc_lstats_operations),
Arjan van de Ven97455122008-01-25 21:08:34 +01003440#endif
Paul Menage8793d852007-10-18 23:39:39 -07003441#ifdef CONFIG_PROC_PID_CPUSET
Zefan Li52de4772014-09-18 16:03:36 +08003442 ONE("cpuset", S_IRUGO, proc_cpuset_show),
Eric W. Biederman28a6d672006-10-02 02:17:05 -07003443#endif
Paul Menagea4243162007-10-18 23:39:35 -07003444#ifdef CONFIG_CGROUPS
Zefan Li006f4ac2014-09-18 16:03:15 +08003445 ONE("cgroup", S_IRUGO, proc_cgroup_show),
Paul Menagea4243162007-10-18 23:39:35 -07003446#endif
Alexey Dobriyan6ba51e32014-08-08 14:21:48 -07003447 ONE("oom_score", S_IRUGO, proc_oom_score),
David Rientjesfa0cbbf2012-11-12 17:53:04 -08003448 REG("oom_adj", S_IRUGO|S_IWUSR, proc_oom_adj_operations),
David Rientjesa63d83f2010-08-09 17:19:46 -07003449 REG("oom_score_adj", S_IRUGO|S_IWUSR, proc_oom_score_adj_operations),
Richard Guy Briggs4b7d2482019-01-22 17:06:39 -05003450#ifdef CONFIG_AUDIT
Alexey Dobriyan631f9c12008-11-10 01:32:52 +03003451 REG("loginuid", S_IWUSR|S_IRUGO, proc_loginuid_operations),
Al Viro26ec3c62011-02-15 21:24:05 -05003452 REG("sessionid", S_IRUGO, proc_sessionid_operations),
Eric W. Biederman28a6d672006-10-02 02:17:05 -07003453#endif
Akinobu Mitaf4f154f2006-12-08 02:39:47 -08003454#ifdef CONFIG_FAULT_INJECTION
Alexey Dobriyan631f9c12008-11-10 01:32:52 +03003455 REG("make-it-fail", S_IRUGO|S_IWUSR, proc_fault_inject_operations),
Akinobu Mita1203c8e2017-07-14 14:49:57 -07003456 REG("fail-nth", 0644, proc_fail_nth_operations),
Akinobu Mitaf4f154f2006-12-08 02:39:47 -08003457#endif
Andrea Righi297c5d92008-07-25 01:48:49 -07003458#ifdef CONFIG_TASK_IO_ACCOUNTING
Alexey Dobriyan19aadc92014-08-08 14:21:50 -07003459 ONE("io", S_IRUSR, proc_tid_io_accounting),
Andrea Righi297c5d92008-07-25 01:48:49 -07003460#endif
Eric W. Biederman22d917d2011-11-17 00:11:58 -08003461#ifdef CONFIG_USER_NS
3462 REG("uid_map", S_IRUGO|S_IWUSR, proc_uid_map_operations),
3463 REG("gid_map", S_IRUGO|S_IWUSR, proc_gid_map_operations),
Eric W. Biedermanf76d2072012-08-30 01:24:05 -07003464 REG("projid_map", S_IRUGO|S_IWUSR, proc_projid_map_operations),
Eric W. Biederman9cc46512014-12-02 12:27:26 -06003465 REG("setgroups", S_IRUGO|S_IWUSR, proc_setgroups_operations),
Eric W. Biederman22d917d2011-11-17 00:11:58 -08003466#endif
Josh Poimboeuf7c23b332017-02-13 19:42:41 -06003467#ifdef CONFIG_LIVEPATCH
3468 ONE("patch_state", S_IRUSR, proc_pid_patch_state),
3469#endif
Aubrey Li68bc30b2019-06-06 09:22:34 +08003470#ifdef CONFIG_PROC_PID_ARCH_STATUS
3471 ONE("arch_status", S_IRUGO, proc_pid_arch_status),
3472#endif
Eric W. Biederman28a6d672006-10-02 02:17:05 -07003473};
3474
Al Virof0c3b502013-05-16 12:07:31 -04003475static int proc_tid_base_readdir(struct file *file, struct dir_context *ctx)
Eric W. Biederman28a6d672006-10-02 02:17:05 -07003476{
Al Virof0c3b502013-05-16 12:07:31 -04003477 return proc_pident_readdir(file, ctx,
3478 tid_base_stuff, ARRAY_SIZE(tid_base_stuff));
Eric W. Biederman28a6d672006-10-02 02:17:05 -07003479}
3480
Al Viro00cd8dd2012-06-10 17:13:09 -04003481static struct dentry *proc_tid_base_lookup(struct inode *dir, struct dentry *dentry, unsigned int flags)
3482{
Eric W. Biederman7bcd6b02006-10-02 02:18:56 -07003483 return proc_pident_lookup(dir, dentry,
Alexey Dobriyand5a572a2019-03-11 23:28:51 -07003484 tid_base_stuff,
3485 tid_base_stuff + ARRAY_SIZE(tid_base_stuff));
Eric W. Biederman28a6d672006-10-02 02:17:05 -07003486}
3487
Arjan van de Ven00977a52007-02-12 00:55:34 -08003488static const struct file_operations proc_tid_base_operations = {
Eric W. Biederman28a6d672006-10-02 02:17:05 -07003489 .read = generic_read_dir,
Al Virof50752e2016-04-20 17:13:54 -04003490 .iterate_shared = proc_tid_base_readdir,
3491 .llseek = generic_file_llseek,
Eric W. Biederman28a6d672006-10-02 02:17:05 -07003492};
3493
Arjan van de Venc5ef1c42007-02-12 00:55:40 -08003494static const struct inode_operations proc_tid_base_inode_operations = {
Eric W. Biederman28a6d672006-10-02 02:17:05 -07003495 .lookup = proc_tid_base_lookup,
3496 .getattr = pid_getattr,
3497 .setattr = proc_setattr,
3498};
3499
Al Viro0168b9e2018-05-03 09:21:05 -04003500static struct dentry *proc_task_instantiate(struct dentry *dentry,
3501 struct task_struct *task, const void *ptr)
Eric W. Biederman444ceed2006-10-02 02:18:49 -07003502{
Eric W. Biederman444ceed2006-10-02 02:18:49 -07003503 struct inode *inode;
Al Viro0168b9e2018-05-03 09:21:05 -04003504 inode = proc_pid_make_inode(dentry->d_sb, task, S_IFDIR | S_IRUGO | S_IXUGO);
Eric W. Biederman444ceed2006-10-02 02:18:49 -07003505 if (!inode)
Al Viro0168b9e2018-05-03 09:21:05 -04003506 return ERR_PTR(-ENOENT);
Al Viro1bbc5512018-05-02 21:26:16 -04003507
Eric W. Biederman444ceed2006-10-02 02:18:49 -07003508 inode->i_op = &proc_tid_base_inode_operations;
3509 inode->i_fop = &proc_tid_base_operations;
Al Viro1bbc5512018-05-02 21:26:16 -04003510 inode->i_flags |= S_IMMUTABLE;
Vegard Nossumaed54172008-06-05 22:46:53 -07003511
Alexey Dobriyan1270dd82016-12-12 16:45:32 -08003512 set_nlink(inode, nlink_tid);
Al Viro1bbc5512018-05-02 21:26:16 -04003513 pid_update_inode(task, inode);
Eric W. Biederman444ceed2006-10-02 02:18:49 -07003514
Nick Pigginfb045ad2011-01-07 17:49:55 +11003515 d_set_d_op(dentry, &pid_dentry_operations);
Al Viro0168b9e2018-05-03 09:21:05 -04003516 return d_splice_alias(inode, dentry);
Eric W. Biederman444ceed2006-10-02 02:18:49 -07003517}
3518
Al Viro00cd8dd2012-06-10 17:13:09 -04003519static struct dentry *proc_task_lookup(struct inode *dir, struct dentry * dentry, unsigned int flags)
Eric W. Biederman28a6d672006-10-02 02:17:05 -07003520{
Eric W. Biederman28a6d672006-10-02 02:17:05 -07003521 struct task_struct *task;
3522 struct task_struct *leader = get_proc_task(dir);
Eric W. Biederman28a6d672006-10-02 02:17:05 -07003523 unsigned tid;
Pavel Emelyanovb4888932007-10-18 23:40:14 -07003524 struct pid_namespace *ns;
Al Viro0168b9e2018-05-03 09:21:05 -04003525 struct dentry *result = ERR_PTR(-ENOENT);
Eric W. Biederman28a6d672006-10-02 02:17:05 -07003526
3527 if (!leader)
3528 goto out_no_task;
3529
Alexey Dobriyandbcdb502014-08-08 14:21:25 -07003530 tid = name_to_int(&dentry->d_name);
Eric W. Biederman28a6d672006-10-02 02:17:05 -07003531 if (tid == ~0U)
3532 goto out;
3533
Pavel Emelyanovb4888932007-10-18 23:40:14 -07003534 ns = dentry->d_sb->s_fs_info;
Eric W. Biederman28a6d672006-10-02 02:17:05 -07003535 rcu_read_lock();
Pavel Emelyanovb4888932007-10-18 23:40:14 -07003536 task = find_task_by_pid_ns(tid, ns);
Eric W. Biederman28a6d672006-10-02 02:17:05 -07003537 if (task)
3538 get_task_struct(task);
3539 rcu_read_unlock();
3540 if (!task)
3541 goto out;
Pavel Emelyanovbac0abd2007-10-18 23:40:18 -07003542 if (!same_thread_group(leader, task))
Eric W. Biederman28a6d672006-10-02 02:17:05 -07003543 goto out_drop_task;
3544
Al Viro0168b9e2018-05-03 09:21:05 -04003545 result = proc_task_instantiate(dentry, task, NULL);
Eric W. Biederman28a6d672006-10-02 02:17:05 -07003546out_drop_task:
3547 put_task_struct(task);
3548out:
3549 put_task_struct(leader);
3550out_no_task:
Al Viro0168b9e2018-05-03 09:21:05 -04003551 return result;
Eric W. Biederman28a6d672006-10-02 02:17:05 -07003552}
3553
3554/*
Eric W. Biederman0bc58a92006-06-26 00:25:50 -07003555 * Find the first tid of a thread group to return to user space.
3556 *
3557 * Usually this is just the thread group leader, but if the users
3558 * buffer was too small or there was a seek into the middle of the
3559 * directory we have more work todo.
3560 *
3561 * In the case of a short read we start with find_task_by_pid.
3562 *
3563 * In the case of a seek we start with the leader and walk nr
3564 * threads past it.
3565 */
Oleg Nesterov9f6e9632014-01-23 15:55:40 -08003566static struct task_struct *first_tid(struct pid *pid, int tid, loff_t f_pos,
3567 struct pid_namespace *ns)
Eric W. Biederman0bc58a92006-06-26 00:25:50 -07003568{
Oleg Nesterovd855a4b2014-01-23 15:55:39 -08003569 struct task_struct *pos, *task;
Oleg Nesterov9f6e9632014-01-23 15:55:40 -08003570 unsigned long nr = f_pos;
3571
3572 if (nr != f_pos) /* 32bit overflow? */
3573 return NULL;
Eric W. Biederman0bc58a92006-06-26 00:25:50 -07003574
Eric W. Biedermancc288732006-06-26 00:26:01 -07003575 rcu_read_lock();
Oleg Nesterovd855a4b2014-01-23 15:55:39 -08003576 task = pid_task(pid, PIDTYPE_PID);
3577 if (!task)
3578 goto fail;
3579
3580 /* Attempt to start with the tid of a thread */
Oleg Nesterov9f6e9632014-01-23 15:55:40 -08003581 if (tid && nr) {
Pavel Emelyanovb4888932007-10-18 23:40:14 -07003582 pos = find_task_by_pid_ns(tid, ns);
Oleg Nesterovd855a4b2014-01-23 15:55:39 -08003583 if (pos && same_thread_group(pos, task))
Oleg Nesterova872ff02006-06-26 00:26:01 -07003584 goto found;
Eric W. Biederman0bc58a92006-06-26 00:25:50 -07003585 }
3586
3587 /* If nr exceeds the number of threads there is nothing todo */
Oleg Nesterov9f6e9632014-01-23 15:55:40 -08003588 if (nr >= get_nr_threads(task))
Oleg Nesterovc986c142014-01-23 15:55:38 -08003589 goto fail;
Oleg Nesterova872ff02006-06-26 00:26:01 -07003590
3591 /* If we haven't found our starting place yet start
3592 * with the leader and walk nr threads forward.
3593 */
Oleg Nesterovd855a4b2014-01-23 15:55:39 -08003594 pos = task = task->group_leader;
Oleg Nesterovc986c142014-01-23 15:55:38 -08003595 do {
Oleg Nesterov9f6e9632014-01-23 15:55:40 -08003596 if (!nr--)
Oleg Nesterovc986c142014-01-23 15:55:38 -08003597 goto found;
Oleg Nesterovd855a4b2014-01-23 15:55:39 -08003598 } while_each_thread(task, pos);
Oleg Nesterovc986c142014-01-23 15:55:38 -08003599fail:
3600 pos = NULL;
3601 goto out;
Oleg Nesterova872ff02006-06-26 00:26:01 -07003602found:
3603 get_task_struct(pos);
3604out:
Eric W. Biedermancc288732006-06-26 00:26:01 -07003605 rcu_read_unlock();
Eric W. Biederman0bc58a92006-06-26 00:25:50 -07003606 return pos;
3607}
3608
3609/*
3610 * Find the next thread in the thread list.
3611 * Return NULL if there is an error or no next thread.
3612 *
3613 * The reference to the input task_struct is released.
3614 */
3615static struct task_struct *next_tid(struct task_struct *start)
3616{
Oleg Nesterovc1df7fb2006-06-26 00:26:02 -07003617 struct task_struct *pos = NULL;
Eric W. Biedermancc288732006-06-26 00:26:01 -07003618 rcu_read_lock();
Oleg Nesterovc1df7fb2006-06-26 00:26:02 -07003619 if (pid_alive(start)) {
Eric W. Biederman0bc58a92006-06-26 00:25:50 -07003620 pos = next_thread(start);
Oleg Nesterovc1df7fb2006-06-26 00:26:02 -07003621 if (thread_group_leader(pos))
3622 pos = NULL;
3623 else
3624 get_task_struct(pos);
3625 }
Eric W. Biedermancc288732006-06-26 00:26:01 -07003626 rcu_read_unlock();
Eric W. Biederman0bc58a92006-06-26 00:25:50 -07003627 put_task_struct(start);
3628 return pos;
3629}
3630
Linus Torvalds1da177e2005-04-16 15:20:36 -07003631/* for the /proc/TGID/task/ directories */
Al Virof0c3b502013-05-16 12:07:31 -04003632static int proc_task_readdir(struct file *file, struct dir_context *ctx)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003633{
Oleg Nesterovd855a4b2014-01-23 15:55:39 -08003634 struct inode *inode = file_inode(file);
3635 struct task_struct *task;
Pavel Emelyanovb4888932007-10-18 23:40:14 -07003636 struct pid_namespace *ns;
Al Virof0c3b502013-05-16 12:07:31 -04003637 int tid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003638
Oleg Nesterovd855a4b2014-01-23 15:55:39 -08003639 if (proc_inode_is_dead(inode))
Al Virof0c3b502013-05-16 12:07:31 -04003640 return -ENOENT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003641
Al Virof0c3b502013-05-16 12:07:31 -04003642 if (!dir_emit_dots(file, ctx))
Oleg Nesterovd855a4b2014-01-23 15:55:39 -08003643 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003644
Eric W. Biederman0bc58a92006-06-26 00:25:50 -07003645 /* f_version caches the tgid value that the last readdir call couldn't
3646 * return. lseek aka telldir automagically resets f_version to 0.
3647 */
Christoph Hellwig76f668b2018-05-16 07:19:01 +02003648 ns = proc_pid_ns(inode);
Al Virof0c3b502013-05-16 12:07:31 -04003649 tid = (int)file->f_version;
3650 file->f_version = 0;
Oleg Nesterovd855a4b2014-01-23 15:55:39 -08003651 for (task = first_tid(proc_pid(inode), tid, ctx->pos - 2, ns);
Eric W. Biederman0bc58a92006-06-26 00:25:50 -07003652 task;
Al Virof0c3b502013-05-16 12:07:31 -04003653 task = next_tid(task), ctx->pos++) {
Alexey Dobriyane3912ac2018-02-06 15:36:51 -08003654 char name[10 + 1];
Alexey Dobriyana4ef3892018-06-07 17:10:10 -07003655 unsigned int len;
Pavel Emelyanovb4888932007-10-18 23:40:14 -07003656 tid = task_pid_nr_ns(task, ns);
Alexey Dobriyane3912ac2018-02-06 15:36:51 -08003657 len = snprintf(name, sizeof(name), "%u", tid);
Al Virof0c3b502013-05-16 12:07:31 -04003658 if (!proc_fill_cache(file, ctx, name, len,
3659 proc_task_instantiate, task, NULL)) {
Eric W. Biederman0bc58a92006-06-26 00:25:50 -07003660 /* returning this tgid failed, save it as the first
3661 * pid for the next readir call */
Al Virof0c3b502013-05-16 12:07:31 -04003662 file->f_version = (u64)tid;
Eric W. Biederman0bc58a92006-06-26 00:25:50 -07003663 put_task_struct(task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003664 break;
Eric W. Biederman0bc58a92006-06-26 00:25:50 -07003665 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003666 }
Oleg Nesterovd855a4b2014-01-23 15:55:39 -08003667
Al Virof0c3b502013-05-16 12:07:31 -04003668 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003669}
Eric W. Biederman6e66b522006-06-26 00:25:47 -07003670
David Howellsa528d352017-01-31 16:46:22 +00003671static int proc_task_getattr(const struct path *path, struct kstat *stat,
3672 u32 request_mask, unsigned int query_flags)
Eric W. Biederman6e66b522006-06-26 00:25:47 -07003673{
David Howellsa528d352017-01-31 16:46:22 +00003674 struct inode *inode = d_inode(path->dentry);
Eric W. Biederman99f89552006-06-26 00:25:55 -07003675 struct task_struct *p = get_proc_task(inode);
Eric W. Biederman6e66b522006-06-26 00:25:47 -07003676 generic_fillattr(inode, stat);
3677
Eric W. Biederman99f89552006-06-26 00:25:55 -07003678 if (p) {
Eric W. Biederman99f89552006-06-26 00:25:55 -07003679 stat->nlink += get_nr_threads(p);
Eric W. Biederman99f89552006-06-26 00:25:55 -07003680 put_task_struct(p);
Eric W. Biederman6e66b522006-06-26 00:25:47 -07003681 }
3682
3683 return 0;
3684}
Eric W. Biederman28a6d672006-10-02 02:17:05 -07003685
Arjan van de Venc5ef1c42007-02-12 00:55:40 -08003686static const struct inode_operations proc_task_inode_operations = {
Eric W. Biederman28a6d672006-10-02 02:17:05 -07003687 .lookup = proc_task_lookup,
3688 .getattr = proc_task_getattr,
3689 .setattr = proc_setattr,
Vasiliy Kulikov04996802012-01-10 15:11:31 -08003690 .permission = proc_pid_permission,
Eric W. Biederman28a6d672006-10-02 02:17:05 -07003691};
3692
Arjan van de Ven00977a52007-02-12 00:55:34 -08003693static const struct file_operations proc_task_operations = {
Eric W. Biederman28a6d672006-10-02 02:17:05 -07003694 .read = generic_read_dir,
Al Virof50752e2016-04-20 17:13:54 -04003695 .iterate_shared = proc_task_readdir,
3696 .llseek = generic_file_llseek,
Eric W. Biederman28a6d672006-10-02 02:17:05 -07003697};
Alexey Dobriyan1270dd82016-12-12 16:45:32 -08003698
3699void __init set_proc_pid_nlink(void)
3700{
3701 nlink_tid = pid_entry_nlink(tid_base_stuff, ARRAY_SIZE(tid_base_stuff));
3702 nlink_tgid = pid_entry_nlink(tgid_base_stuff, ARRAY_SIZE(tgid_base_stuff));
3703}