blob: 77eb628ecc7f2b1adf1ffefc7aa7d7331f3ef9a4 [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
Shakeel Buttac311a12019-07-11 21:00:31 -0700535 points = oom_badness(task, totalpages) * 1000 / totalpages;
Joe Perches25ce3192015-04-15 16:18:17 -0700536 seq_printf(m, "%lu\n", points);
537
538 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700539}
540
Neil Hormand85f50d2007-10-18 23:40:37 -0700541struct limit_names {
Alexey Dobriyancedbcca2014-08-08 14:21:33 -0700542 const char *name;
543 const char *unit;
Neil Hormand85f50d2007-10-18 23:40:37 -0700544};
545
546static const struct limit_names lnames[RLIM_NLIMITS] = {
Kees Cookcff4edb2009-09-22 16:45:32 -0700547 [RLIMIT_CPU] = {"Max cpu time", "seconds"},
Neil Hormand85f50d2007-10-18 23:40:37 -0700548 [RLIMIT_FSIZE] = {"Max file size", "bytes"},
549 [RLIMIT_DATA] = {"Max data size", "bytes"},
550 [RLIMIT_STACK] = {"Max stack size", "bytes"},
551 [RLIMIT_CORE] = {"Max core file size", "bytes"},
552 [RLIMIT_RSS] = {"Max resident set", "bytes"},
553 [RLIMIT_NPROC] = {"Max processes", "processes"},
554 [RLIMIT_NOFILE] = {"Max open files", "files"},
555 [RLIMIT_MEMLOCK] = {"Max locked memory", "bytes"},
556 [RLIMIT_AS] = {"Max address space", "bytes"},
557 [RLIMIT_LOCKS] = {"Max file locks", "locks"},
558 [RLIMIT_SIGPENDING] = {"Max pending signals", "signals"},
559 [RLIMIT_MSGQUEUE] = {"Max msgqueue size", "bytes"},
560 [RLIMIT_NICE] = {"Max nice priority", NULL},
561 [RLIMIT_RTPRIO] = {"Max realtime priority", NULL},
Eugene Teo88081172008-02-23 15:23:52 -0800562 [RLIMIT_RTTIME] = {"Max realtime timeout", "us"},
Neil Hormand85f50d2007-10-18 23:40:37 -0700563};
564
565/* Display limits for a process */
Alexey Dobriyan1c963eb2014-08-08 14:21:37 -0700566static int proc_pid_limits(struct seq_file *m, struct pid_namespace *ns,
567 struct pid *pid, struct task_struct *task)
Neil Hormand85f50d2007-10-18 23:40:37 -0700568{
569 unsigned int i;
Neil Hormand85f50d2007-10-18 23:40:37 -0700570 unsigned long flags;
Neil Hormand85f50d2007-10-18 23:40:37 -0700571
572 struct rlimit rlim[RLIM_NLIMITS];
573
Lai Jiangshana6bebbc2008-10-05 00:51:15 +0400574 if (!lock_task_sighand(task, &flags))
Neil Hormand85f50d2007-10-18 23:40:37 -0700575 return 0;
Neil Hormand85f50d2007-10-18 23:40:37 -0700576 memcpy(rlim, task->signal->rlim, sizeof(struct rlimit) * RLIM_NLIMITS);
577 unlock_task_sighand(task, &flags);
Neil Hormand85f50d2007-10-18 23:40:37 -0700578
579 /*
580 * print the file header
581 */
Alexey Dobriyanafe922c2019-01-03 15:26:09 -0800582 seq_puts(m, "Limit "
583 "Soft Limit "
584 "Hard Limit "
585 "Units \n");
Neil Hormand85f50d2007-10-18 23:40:37 -0700586
587 for (i = 0; i < RLIM_NLIMITS; i++) {
588 if (rlim[i].rlim_cur == RLIM_INFINITY)
Alexey Dobriyan1c963eb2014-08-08 14:21:37 -0700589 seq_printf(m, "%-25s %-20s ",
Joe Perches25ce3192015-04-15 16:18:17 -0700590 lnames[i].name, "unlimited");
Neil Hormand85f50d2007-10-18 23:40:37 -0700591 else
Alexey Dobriyan1c963eb2014-08-08 14:21:37 -0700592 seq_printf(m, "%-25s %-20lu ",
Joe Perches25ce3192015-04-15 16:18:17 -0700593 lnames[i].name, rlim[i].rlim_cur);
Neil Hormand85f50d2007-10-18 23:40:37 -0700594
595 if (rlim[i].rlim_max == RLIM_INFINITY)
Alexey Dobriyan1c963eb2014-08-08 14:21:37 -0700596 seq_printf(m, "%-20s ", "unlimited");
Neil Hormand85f50d2007-10-18 23:40:37 -0700597 else
Alexey Dobriyan1c963eb2014-08-08 14:21:37 -0700598 seq_printf(m, "%-20lu ", rlim[i].rlim_max);
Neil Hormand85f50d2007-10-18 23:40:37 -0700599
600 if (lnames[i].unit)
Alexey Dobriyan1c963eb2014-08-08 14:21:37 -0700601 seq_printf(m, "%-10s\n", lnames[i].unit);
Neil Hormand85f50d2007-10-18 23:40:37 -0700602 else
Alexey Dobriyan1c963eb2014-08-08 14:21:37 -0700603 seq_putc(m, '\n');
Neil Hormand85f50d2007-10-18 23:40:37 -0700604 }
605
Alexey Dobriyan1c963eb2014-08-08 14:21:37 -0700606 return 0;
Neil Hormand85f50d2007-10-18 23:40:37 -0700607}
608
Roland McGrathebcb6732008-07-25 19:46:00 -0700609#ifdef CONFIG_HAVE_ARCH_TRACEHOOK
Alexey Dobriyan09d93bd2014-08-08 14:21:39 -0700610static int proc_pid_syscall(struct seq_file *m, struct pid_namespace *ns,
611 struct pid *pid, struct task_struct *task)
Roland McGrathebcb6732008-07-25 19:46:00 -0700612{
Steven Rostedt (Red Hat)631b7ab2016-11-07 16:26:35 -0500613 struct syscall_info info;
614 u64 *args = &info.data.args[0];
Joe Perches25ce3192015-04-15 16:18:17 -0700615 int res;
616
617 res = lock_trace(task);
Al Viroa9712bc2011-03-23 15:52:50 -0400618 if (res)
619 return res;
Roland McGrathebcb6732008-07-25 19:46:00 -0700620
Steven Rostedt (Red Hat)631b7ab2016-11-07 16:26:35 -0500621 if (task_current_syscall(task, &info))
Alexey Dobriyan09d93bd2014-08-08 14:21:39 -0700622 seq_puts(m, "running\n");
Steven Rostedt (Red Hat)631b7ab2016-11-07 16:26:35 -0500623 else if (info.data.nr < 0)
624 seq_printf(m, "%d 0x%llx 0x%llx\n",
625 info.data.nr, info.sp, info.data.instruction_pointer);
Al Viroa9712bc2011-03-23 15:52:50 -0400626 else
Alexey Dobriyan09d93bd2014-08-08 14:21:39 -0700627 seq_printf(m,
Steven Rostedt (Red Hat)631b7ab2016-11-07 16:26:35 -0500628 "%d 0x%llx 0x%llx 0x%llx 0x%llx 0x%llx 0x%llx 0x%llx 0x%llx\n",
629 info.data.nr,
Roland McGrathebcb6732008-07-25 19:46:00 -0700630 args[0], args[1], args[2], args[3], args[4], args[5],
Steven Rostedt (Red Hat)631b7ab2016-11-07 16:26:35 -0500631 info.sp, info.data.instruction_pointer);
Al Viroa9712bc2011-03-23 15:52:50 -0400632 unlock_trace(task);
Joe Perches25ce3192015-04-15 16:18:17 -0700633
634 return 0;
Roland McGrathebcb6732008-07-25 19:46:00 -0700635}
636#endif /* CONFIG_HAVE_ARCH_TRACEHOOK */
637
Linus Torvalds1da177e2005-04-16 15:20:36 -0700638/************************************************************************/
639/* Here the fs part begins */
640/************************************************************************/
641
642/* permission checks */
Eric W. Biederman778c1142006-06-26 00:25:58 -0700643static int proc_fd_access_allowed(struct inode *inode)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700644{
Eric W. Biederman778c1142006-06-26 00:25:58 -0700645 struct task_struct *task;
646 int allowed = 0;
Eric W. Biedermandf26c402006-06-26 00:25:59 -0700647 /* Allow access to a task's file descriptors if it is us or we
648 * may use ptrace attach to the process and find out that
649 * information.
Eric W. Biederman778c1142006-06-26 00:25:58 -0700650 */
651 task = get_proc_task(inode);
Eric W. Biedermandf26c402006-06-26 00:25:59 -0700652 if (task) {
Jann Horncaaee622016-01-20 15:00:04 -0800653 allowed = ptrace_may_access(task, PTRACE_MODE_READ_FSCREDS);
Eric W. Biederman778c1142006-06-26 00:25:58 -0700654 put_task_struct(task);
Eric W. Biedermandf26c402006-06-26 00:25:59 -0700655 }
Eric W. Biederman778c1142006-06-26 00:25:58 -0700656 return allowed;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700657}
658
Eric W. Biederman6b4e3062010-03-07 16:41:34 -0800659int proc_setattr(struct dentry *dentry, struct iattr *attr)
Linus Torvalds6d76fa52006-07-15 12:26:45 -0700660{
661 int error;
David Howells2b0143b2015-03-17 22:25:59 +0000662 struct inode *inode = d_inode(dentry);
Linus Torvalds6d76fa52006-07-15 12:26:45 -0700663
664 if (attr->ia_valid & ATTR_MODE)
665 return -EPERM;
666
Jan Kara31051c82016-05-26 16:55:18 +0200667 error = setattr_prepare(dentry, attr);
Christoph Hellwig10257742010-06-04 11:30:02 +0200668 if (error)
669 return error;
670
Christoph Hellwig10257742010-06-04 11:30:02 +0200671 setattr_copy(inode, attr);
672 mark_inode_dirty(inode);
673 return 0;
Linus Torvalds6d76fa52006-07-15 12:26:45 -0700674}
675
Vasiliy Kulikov04996802012-01-10 15:11:31 -0800676/*
677 * May current process learn task's sched/cmdline info (for hide_pid_min=1)
678 * or euid/egid (for hide_pid_min=2)?
679 */
680static bool has_pid_permissions(struct pid_namespace *pid,
681 struct task_struct *task,
682 int hide_pid_min)
683{
684 if (pid->hide_pid < hide_pid_min)
685 return true;
686 if (in_group_p(pid->pid_gid))
687 return true;
Jann Horncaaee622016-01-20 15:00:04 -0800688 return ptrace_may_access(task, PTRACE_MODE_READ_FSCREDS);
Vasiliy Kulikov04996802012-01-10 15:11:31 -0800689}
690
691
692static int proc_pid_permission(struct inode *inode, int mask)
693{
Christoph Hellwig76f668b2018-05-16 07:19:01 +0200694 struct pid_namespace *pid = proc_pid_ns(inode);
Vasiliy Kulikov04996802012-01-10 15:11:31 -0800695 struct task_struct *task;
696 bool has_perms;
697
698 task = get_proc_task(inode);
Xiaotian Fenga2ef9902012-01-12 17:17:08 -0800699 if (!task)
700 return -ESRCH;
Lafcadio Wluiki796f5712017-02-24 15:00:23 -0800701 has_perms = has_pid_permissions(pid, task, HIDEPID_NO_ACCESS);
Vasiliy Kulikov04996802012-01-10 15:11:31 -0800702 put_task_struct(task);
703
704 if (!has_perms) {
Lafcadio Wluiki796f5712017-02-24 15:00:23 -0800705 if (pid->hide_pid == HIDEPID_INVISIBLE) {
Vasiliy Kulikov04996802012-01-10 15:11:31 -0800706 /*
707 * Let's make getdents(), stat(), and open()
708 * consistent with each other. If a process
709 * may not stat() a file, it shouldn't be seen
710 * in procfs at all.
711 */
712 return -ENOENT;
713 }
714
715 return -EPERM;
716 }
717 return generic_permission(inode, mask);
718}
719
720
721
Arjan van de Venc5ef1c42007-02-12 00:55:40 -0800722static const struct inode_operations proc_def_inode_operations = {
Linus Torvalds6d76fa52006-07-15 12:26:45 -0700723 .setattr = proc_setattr,
724};
725
Eric W. Biedermanbe614082008-02-08 04:18:30 -0800726static int proc_single_show(struct seq_file *m, void *v)
727{
728 struct inode *inode = m->private;
Christoph Hellwig76f668b2018-05-16 07:19:01 +0200729 struct pid_namespace *ns = proc_pid_ns(inode);
730 struct pid *pid = proc_pid(inode);
Eric W. Biedermanbe614082008-02-08 04:18:30 -0800731 struct task_struct *task;
732 int ret;
733
Eric W. Biedermanbe614082008-02-08 04:18:30 -0800734 task = get_pid_task(pid, PIDTYPE_PID);
735 if (!task)
736 return -ESRCH;
737
738 ret = PROC_I(inode)->op.proc_show(m, ns, pid, task);
739
740 put_task_struct(task);
741 return ret;
742}
743
744static int proc_single_open(struct inode *inode, struct file *filp)
745{
Jovi Zhangc6a34052011-01-12 17:00:34 -0800746 return single_open(filp, proc_single_show, inode);
Eric W. Biedermanbe614082008-02-08 04:18:30 -0800747}
748
749static const struct file_operations proc_single_file_operations = {
750 .open = proc_single_open,
751 .read = seq_read,
752 .llseek = seq_lseek,
753 .release = single_release,
754};
755
Oleg Nesterov5381e162014-10-09 15:25:24 -0700756
757struct mm_struct *proc_mem_open(struct inode *inode, unsigned int mode)
758{
759 struct task_struct *task = get_proc_task(inode);
760 struct mm_struct *mm = ERR_PTR(-ESRCH);
761
762 if (task) {
Jann Horncaaee622016-01-20 15:00:04 -0800763 mm = mm_access(task, mode | PTRACE_MODE_FSCREDS);
Oleg Nesterov5381e162014-10-09 15:25:24 -0700764 put_task_struct(task);
765
766 if (!IS_ERR_OR_NULL(mm)) {
767 /* ensure this mm_struct can't be freed */
Vegard Nossumf1f10072017-02-27 14:30:07 -0800768 mmgrab(mm);
Oleg Nesterov5381e162014-10-09 15:25:24 -0700769 /* but do not pin its memory */
770 mmput(mm);
771 }
772 }
773
774 return mm;
775}
776
Cong Wangb409e572012-05-31 16:26:17 -0700777static int __mem_open(struct inode *inode, struct file *file, unsigned int mode)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700778{
Oleg Nesterov5381e162014-10-09 15:25:24 -0700779 struct mm_struct *mm = proc_mem_open(inode, mode);
Linus Torvaldse2683372012-01-17 15:21:19 -0800780
781 if (IS_ERR(mm))
782 return PTR_ERR(mm);
783
Linus Torvaldse2683372012-01-17 15:21:19 -0800784 file->private_data = mm;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700785 return 0;
786}
787
Cong Wangb409e572012-05-31 16:26:17 -0700788static int mem_open(struct inode *inode, struct file *file)
789{
Djalal Harounibc452b42012-07-30 14:42:28 -0700790 int ret = __mem_open(inode, file, PTRACE_MODE_ATTACH);
791
792 /* OK to pass negative loff_t, we can catch out-of-range */
793 file->f_mode |= FMODE_UNSIGNED_OFFSET;
794
795 return ret;
Cong Wangb409e572012-05-31 16:26:17 -0700796}
797
Oleg Nesterov572d34b2012-01-31 17:14:54 +0100798static ssize_t mem_rw(struct file *file, char __user *buf,
799 size_t count, loff_t *ppos, int write)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700800{
Linus Torvaldse2683372012-01-17 15:21:19 -0800801 struct mm_struct *mm = file->private_data;
Oleg Nesterov572d34b2012-01-31 17:14:54 +0100802 unsigned long addr = *ppos;
803 ssize_t copied;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700804 char *page;
Linus Torvalds272ddc82016-10-24 19:00:44 -0700805 unsigned int flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700806
Linus Torvaldse2683372012-01-17 15:21:19 -0800807 if (!mm)
808 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700809
Michal Hocko0ee931c2017-09-13 16:28:29 -0700810 page = (char *)__get_free_page(GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700811 if (!page)
Linus Torvaldse2683372012-01-17 15:21:19 -0800812 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700813
Frederik Deweerdtf7ca54f2006-09-29 02:01:02 -0700814 copied = 0;
Vegard Nossum388f7932017-02-27 14:30:13 -0800815 if (!mmget_not_zero(mm))
Oleg Nesterov6d08f2c2012-01-31 17:15:11 +0100816 goto free;
817
Linus Torvaldsf511c0b2017-05-30 12:38:59 -0700818 flags = FOLL_FORCE | (write ? FOLL_WRITE : 0);
Lorenzo Stoakes6347e8d2016-10-13 01:20:19 +0100819
Linus Torvalds1da177e2005-04-16 15:20:36 -0700820 while (count > 0) {
Oleg Nesterov572d34b2012-01-31 17:14:54 +0100821 int this_len = min_t(int, count, PAGE_SIZE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700822
Oleg Nesterov572d34b2012-01-31 17:14:54 +0100823 if (write && copy_from_user(page, buf, this_len)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700824 copied = -EFAULT;
825 break;
826 }
Oleg Nesterov572d34b2012-01-31 17:14:54 +0100827
Lorenzo Stoakes6347e8d2016-10-13 01:20:19 +0100828 this_len = access_remote_vm(mm, addr, page, this_len, flags);
Oleg Nesterov572d34b2012-01-31 17:14:54 +0100829 if (!this_len) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700830 if (!copied)
831 copied = -EIO;
832 break;
833 }
Oleg Nesterov572d34b2012-01-31 17:14:54 +0100834
835 if (!write && copy_to_user(buf, page, this_len)) {
836 copied = -EFAULT;
837 break;
838 }
839
840 buf += this_len;
841 addr += this_len;
842 copied += this_len;
843 count -= this_len;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700844 }
Oleg Nesterov572d34b2012-01-31 17:14:54 +0100845 *ppos = addr;
KOSAKI Motohiro30cd8902011-05-26 16:25:52 -0700846
Oleg Nesterov6d08f2c2012-01-31 17:15:11 +0100847 mmput(mm);
848free:
KOSAKI Motohiro30cd8902011-05-26 16:25:52 -0700849 free_page((unsigned long) page);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700850 return copied;
851}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700852
Oleg Nesterov572d34b2012-01-31 17:14:54 +0100853static ssize_t mem_read(struct file *file, char __user *buf,
854 size_t count, loff_t *ppos)
855{
856 return mem_rw(file, buf, count, ppos, 0);
857}
858
859static ssize_t mem_write(struct file *file, const char __user *buf,
860 size_t count, loff_t *ppos)
861{
862 return mem_rw(file, (char __user*)buf, count, ppos, 1);
863}
864
Matt Mackall85863e42008-02-04 22:29:04 -0800865loff_t mem_lseek(struct file *file, loff_t offset, int orig)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700866{
867 switch (orig) {
868 case 0:
869 file->f_pos = offset;
870 break;
871 case 1:
872 file->f_pos += offset;
873 break;
874 default:
875 return -EINVAL;
876 }
877 force_successful_syscall_return();
878 return file->f_pos;
879}
880
Linus Torvaldse2683372012-01-17 15:21:19 -0800881static int mem_release(struct inode *inode, struct file *file)
882{
883 struct mm_struct *mm = file->private_data;
Oleg Nesterov71879d32012-01-31 17:14:38 +0100884 if (mm)
Oleg Nesterov6d08f2c2012-01-31 17:15:11 +0100885 mmdrop(mm);
Linus Torvaldse2683372012-01-17 15:21:19 -0800886 return 0;
887}
888
Arjan van de Ven00977a52007-02-12 00:55:34 -0800889static const struct file_operations proc_mem_operations = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700890 .llseek = mem_lseek,
891 .read = mem_read,
892 .write = mem_write,
893 .open = mem_open,
Linus Torvaldse2683372012-01-17 15:21:19 -0800894 .release = mem_release,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700895};
896
Cong Wangb409e572012-05-31 16:26:17 -0700897static int environ_open(struct inode *inode, struct file *file)
898{
899 return __mem_open(inode, file, PTRACE_MODE_READ);
900}
901
James Pearson315e28c2007-10-16 23:30:17 -0700902static ssize_t environ_read(struct file *file, char __user *buf,
903 size_t count, loff_t *ppos)
904{
James Pearson315e28c2007-10-16 23:30:17 -0700905 char *page;
906 unsigned long src = *ppos;
Cong Wangb409e572012-05-31 16:26:17 -0700907 int ret = 0;
908 struct mm_struct *mm = file->private_data;
Mateusz Guzika3b609e2016-01-20 15:01:05 -0800909 unsigned long env_start, env_end;
James Pearson315e28c2007-10-16 23:30:17 -0700910
Mathias Krause8148a732016-05-05 16:22:26 -0700911 /* Ensure the process spawned far enough to have an environment. */
912 if (!mm || !mm->env_end)
Cong Wangb409e572012-05-31 16:26:17 -0700913 return 0;
James Pearson315e28c2007-10-16 23:30:17 -0700914
Michal Hocko0ee931c2017-09-13 16:28:29 -0700915 page = (char *)__get_free_page(GFP_KERNEL);
James Pearson315e28c2007-10-16 23:30:17 -0700916 if (!page)
Cong Wangb409e572012-05-31 16:26:17 -0700917 return -ENOMEM;
James Pearson315e28c2007-10-16 23:30:17 -0700918
Al Virod6f64b82011-02-15 22:26:01 -0500919 ret = 0;
Vegard Nossum388f7932017-02-27 14:30:13 -0800920 if (!mmget_not_zero(mm))
Cong Wangb409e572012-05-31 16:26:17 -0700921 goto free;
Mateusz Guzika3b609e2016-01-20 15:01:05 -0800922
Yang Shi88aa7cc2018-06-07 17:05:28 -0700923 spin_lock(&mm->arg_lock);
Mateusz Guzika3b609e2016-01-20 15:01:05 -0800924 env_start = mm->env_start;
925 env_end = mm->env_end;
Yang Shi88aa7cc2018-06-07 17:05:28 -0700926 spin_unlock(&mm->arg_lock);
Mateusz Guzika3b609e2016-01-20 15:01:05 -0800927
James Pearson315e28c2007-10-16 23:30:17 -0700928 while (count > 0) {
Djalal Harounie8905ec2012-07-30 14:42:26 -0700929 size_t this_len, max_len;
930 int retval;
931
Mateusz Guzika3b609e2016-01-20 15:01:05 -0800932 if (src >= (env_end - env_start))
Djalal Harounie8905ec2012-07-30 14:42:26 -0700933 break;
James Pearson315e28c2007-10-16 23:30:17 -0700934
Mateusz Guzika3b609e2016-01-20 15:01:05 -0800935 this_len = env_end - (env_start + src);
James Pearson315e28c2007-10-16 23:30:17 -0700936
Djalal Harounie8905ec2012-07-30 14:42:26 -0700937 max_len = min_t(size_t, PAGE_SIZE, count);
938 this_len = min(max_len, this_len);
James Pearson315e28c2007-10-16 23:30:17 -0700939
Willy Tarreau7f7ccc22018-05-11 08:11:44 +0200940 retval = access_remote_vm(mm, (env_start + src), page, this_len, FOLL_ANON);
James Pearson315e28c2007-10-16 23:30:17 -0700941
942 if (retval <= 0) {
943 ret = retval;
944 break;
945 }
946
947 if (copy_to_user(buf, page, retval)) {
948 ret = -EFAULT;
949 break;
950 }
951
952 ret += retval;
953 src += retval;
954 buf += retval;
955 count -= retval;
956 }
957 *ppos = src;
James Pearson315e28c2007-10-16 23:30:17 -0700958 mmput(mm);
Cong Wangb409e572012-05-31 16:26:17 -0700959
960free:
James Pearson315e28c2007-10-16 23:30:17 -0700961 free_page((unsigned long) page);
James Pearson315e28c2007-10-16 23:30:17 -0700962 return ret;
963}
964
965static const struct file_operations proc_environ_operations = {
Cong Wangb409e572012-05-31 16:26:17 -0700966 .open = environ_open,
James Pearson315e28c2007-10-16 23:30:17 -0700967 .read = environ_read,
Arnd Bergmann87df8422010-03-17 23:06:02 +0100968 .llseek = generic_file_llseek,
Cong Wangb409e572012-05-31 16:26:17 -0700969 .release = mem_release,
James Pearson315e28c2007-10-16 23:30:17 -0700970};
971
Al Viroc5317162016-10-05 18:43:43 -0400972static int auxv_open(struct inode *inode, struct file *file)
973{
974 return __mem_open(inode, file, PTRACE_MODE_READ_FSCREDS);
975}
976
977static ssize_t auxv_read(struct file *file, char __user *buf,
978 size_t count, loff_t *ppos)
979{
980 struct mm_struct *mm = file->private_data;
981 unsigned int nwords = 0;
Leon Yu06b28492016-10-27 17:46:50 -0700982
983 if (!mm)
984 return 0;
Al Viroc5317162016-10-05 18:43:43 -0400985 do {
986 nwords += 2;
987 } while (mm->saved_auxv[nwords - 2] != 0); /* AT_NULL */
988 return simple_read_from_buffer(buf, count, ppos, mm->saved_auxv,
989 nwords * sizeof(mm->saved_auxv[0]));
990}
991
992static const struct file_operations proc_auxv_operations = {
993 .open = auxv_open,
994 .read = auxv_read,
995 .llseek = generic_file_llseek,
996 .release = mem_release,
997};
998
David Rientjesfa0cbbf2012-11-12 17:53:04 -0800999static ssize_t oom_adj_read(struct file *file, char __user *buf, size_t count,
1000 loff_t *ppos)
1001{
Al Viro496ad9a2013-01-23 17:07:38 -05001002 struct task_struct *task = get_proc_task(file_inode(file));
David Rientjesfa0cbbf2012-11-12 17:53:04 -08001003 char buffer[PROC_NUMBUF];
1004 int oom_adj = OOM_ADJUST_MIN;
1005 size_t len;
David Rientjesfa0cbbf2012-11-12 17:53:04 -08001006
1007 if (!task)
1008 return -ESRCH;
Michal Hockof913da52016-07-28 15:44:37 -07001009 if (task->signal->oom_score_adj == OOM_SCORE_ADJ_MAX)
1010 oom_adj = OOM_ADJUST_MAX;
1011 else
1012 oom_adj = (task->signal->oom_score_adj * -OOM_DISABLE) /
1013 OOM_SCORE_ADJ_MAX;
David Rientjesfa0cbbf2012-11-12 17:53:04 -08001014 put_task_struct(task);
1015 len = snprintf(buffer, sizeof(buffer), "%d\n", oom_adj);
1016 return simple_read_from_buffer(buf, count, ppos, buffer, len);
1017}
1018
Michal Hocko1d5f0ac2016-07-28 15:44:40 -07001019static int __set_oom_adj(struct file *file, int oom_adj, bool legacy)
1020{
1021 static DEFINE_MUTEX(oom_adj_mutex);
Michal Hocko44a70ade2016-07-28 15:44:43 -07001022 struct mm_struct *mm = NULL;
Michal Hocko1d5f0ac2016-07-28 15:44:40 -07001023 struct task_struct *task;
1024 int err = 0;
1025
1026 task = get_proc_task(file_inode(file));
1027 if (!task)
1028 return -ESRCH;
1029
1030 mutex_lock(&oom_adj_mutex);
1031 if (legacy) {
1032 if (oom_adj < task->signal->oom_score_adj &&
1033 !capable(CAP_SYS_RESOURCE)) {
1034 err = -EACCES;
1035 goto err_unlock;
1036 }
1037 /*
1038 * /proc/pid/oom_adj is provided for legacy purposes, ask users to use
1039 * /proc/pid/oom_score_adj instead.
1040 */
1041 pr_warn_once("%s (%d): /proc/%d/oom_adj is deprecated, please use /proc/%d/oom_score_adj instead.\n",
1042 current->comm, task_pid_nr(current), task_pid_nr(task),
1043 task_pid_nr(task));
1044 } else {
1045 if ((short)oom_adj < task->signal->oom_score_adj_min &&
1046 !capable(CAP_SYS_RESOURCE)) {
1047 err = -EACCES;
1048 goto err_unlock;
1049 }
1050 }
1051
Michal Hocko44a70ade2016-07-28 15:44:43 -07001052 /*
1053 * Make sure we will check other processes sharing the mm if this is
1054 * not vfrok which wants its own oom_score_adj.
1055 * pin the mm so it doesn't go away and get reused after task_unlock
1056 */
1057 if (!task->vfork_done) {
1058 struct task_struct *p = find_lock_task_mm(task);
1059
1060 if (p) {
1061 if (atomic_read(&p->mm->mm_users) > 1) {
1062 mm = p->mm;
Vegard Nossumf1f10072017-02-27 14:30:07 -08001063 mmgrab(mm);
Michal Hocko44a70ade2016-07-28 15:44:43 -07001064 }
1065 task_unlock(p);
1066 }
1067 }
1068
Michal Hocko1d5f0ac2016-07-28 15:44:40 -07001069 task->signal->oom_score_adj = oom_adj;
1070 if (!legacy && has_capability_noaudit(current, CAP_SYS_RESOURCE))
1071 task->signal->oom_score_adj_min = (short)oom_adj;
1072 trace_oom_score_adj_update(task);
Michal Hocko44a70ade2016-07-28 15:44:43 -07001073
1074 if (mm) {
1075 struct task_struct *p;
1076
1077 rcu_read_lock();
1078 for_each_process(p) {
1079 if (same_thread_group(task, p))
1080 continue;
1081
1082 /* do not touch kernel threads or the global init */
1083 if (p->flags & PF_KTHREAD || is_global_init(p))
1084 continue;
1085
1086 task_lock(p);
1087 if (!p->vfork_done && process_shares_mm(p, mm)) {
Michal Hocko44a70ade2016-07-28 15:44:43 -07001088 p->signal->oom_score_adj = oom_adj;
1089 if (!legacy && has_capability_noaudit(current, CAP_SYS_RESOURCE))
1090 p->signal->oom_score_adj_min = (short)oom_adj;
1091 }
1092 task_unlock(p);
1093 }
1094 rcu_read_unlock();
1095 mmdrop(mm);
1096 }
Michal Hocko1d5f0ac2016-07-28 15:44:40 -07001097err_unlock:
1098 mutex_unlock(&oom_adj_mutex);
1099 put_task_struct(task);
1100 return err;
1101}
Michal Hockof913da52016-07-28 15:44:37 -07001102
David Rientjesb72bdfa2015-11-05 18:50:32 -08001103/*
1104 * /proc/pid/oom_adj exists solely for backwards compatibility with previous
1105 * kernels. The effective policy is defined by oom_score_adj, which has a
1106 * different scale: oom_adj grew exponentially and oom_score_adj grows linearly.
1107 * Values written to oom_adj are simply mapped linearly to oom_score_adj.
1108 * Processes that become oom disabled via oom_adj will still be oom disabled
1109 * with this implementation.
1110 *
1111 * oom_adj cannot be removed since existing userspace binaries use it.
1112 */
David Rientjesfa0cbbf2012-11-12 17:53:04 -08001113static ssize_t oom_adj_write(struct file *file, const char __user *buf,
1114 size_t count, loff_t *ppos)
1115{
David Rientjesfa0cbbf2012-11-12 17:53:04 -08001116 char buffer[PROC_NUMBUF];
1117 int oom_adj;
David Rientjesfa0cbbf2012-11-12 17:53:04 -08001118 int err;
1119
1120 memset(buffer, 0, sizeof(buffer));
1121 if (count > sizeof(buffer) - 1)
1122 count = sizeof(buffer) - 1;
1123 if (copy_from_user(buffer, buf, count)) {
1124 err = -EFAULT;
1125 goto out;
1126 }
1127
1128 err = kstrtoint(strstrip(buffer), 0, &oom_adj);
1129 if (err)
1130 goto out;
1131 if ((oom_adj < OOM_ADJUST_MIN || oom_adj > OOM_ADJUST_MAX) &&
1132 oom_adj != OOM_DISABLE) {
1133 err = -EINVAL;
1134 goto out;
1135 }
1136
David Rientjesfa0cbbf2012-11-12 17:53:04 -08001137 /*
1138 * Scale /proc/pid/oom_score_adj appropriately ensuring that a maximum
1139 * value is always attainable.
1140 */
1141 if (oom_adj == OOM_ADJUST_MAX)
1142 oom_adj = OOM_SCORE_ADJ_MAX;
1143 else
1144 oom_adj = (oom_adj * OOM_SCORE_ADJ_MAX) / -OOM_DISABLE;
1145
Michal Hocko1d5f0ac2016-07-28 15:44:40 -07001146 err = __set_oom_adj(file, oom_adj, true);
David Rientjesfa0cbbf2012-11-12 17:53:04 -08001147out:
1148 return err < 0 ? err : count;
1149}
1150
1151static const struct file_operations proc_oom_adj_operations = {
1152 .read = oom_adj_read,
1153 .write = oom_adj_write,
1154 .llseek = generic_file_llseek,
1155};
1156
David Rientjesa63d83f2010-08-09 17:19:46 -07001157static ssize_t oom_score_adj_read(struct file *file, char __user *buf,
1158 size_t count, loff_t *ppos)
1159{
Al Viro496ad9a2013-01-23 17:07:38 -05001160 struct task_struct *task = get_proc_task(file_inode(file));
David Rientjesa63d83f2010-08-09 17:19:46 -07001161 char buffer[PROC_NUMBUF];
David Rientjesa9c58b902012-12-11 16:02:54 -08001162 short oom_score_adj = OOM_SCORE_ADJ_MIN;
David Rientjesa63d83f2010-08-09 17:19:46 -07001163 size_t len;
1164
1165 if (!task)
1166 return -ESRCH;
Michal Hockof913da52016-07-28 15:44:37 -07001167 oom_score_adj = task->signal->oom_score_adj;
David Rientjesa63d83f2010-08-09 17:19:46 -07001168 put_task_struct(task);
David Rientjesa9c58b902012-12-11 16:02:54 -08001169 len = snprintf(buffer, sizeof(buffer), "%hd\n", oom_score_adj);
David Rientjesa63d83f2010-08-09 17:19:46 -07001170 return simple_read_from_buffer(buf, count, ppos, buffer, len);
1171}
1172
1173static ssize_t oom_score_adj_write(struct file *file, const char __user *buf,
1174 size_t count, loff_t *ppos)
1175{
David Rientjesa63d83f2010-08-09 17:19:46 -07001176 char buffer[PROC_NUMBUF];
Alexey Dobriyan0a8cb8e2011-05-26 16:25:50 -07001177 int oom_score_adj;
David Rientjesa63d83f2010-08-09 17:19:46 -07001178 int err;
1179
1180 memset(buffer, 0, sizeof(buffer));
1181 if (count > sizeof(buffer) - 1)
1182 count = sizeof(buffer) - 1;
David Rientjes723548b2010-10-26 14:21:25 -07001183 if (copy_from_user(buffer, buf, count)) {
1184 err = -EFAULT;
1185 goto out;
1186 }
David Rientjesa63d83f2010-08-09 17:19:46 -07001187
Alexey Dobriyan0a8cb8e2011-05-26 16:25:50 -07001188 err = kstrtoint(strstrip(buffer), 0, &oom_score_adj);
David Rientjesa63d83f2010-08-09 17:19:46 -07001189 if (err)
David Rientjes723548b2010-10-26 14:21:25 -07001190 goto out;
David Rientjesa63d83f2010-08-09 17:19:46 -07001191 if (oom_score_adj < OOM_SCORE_ADJ_MIN ||
David Rientjes723548b2010-10-26 14:21:25 -07001192 oom_score_adj > OOM_SCORE_ADJ_MAX) {
1193 err = -EINVAL;
1194 goto out;
1195 }
David Rientjesa63d83f2010-08-09 17:19:46 -07001196
Michal Hocko1d5f0ac2016-07-28 15:44:40 -07001197 err = __set_oom_adj(file, oom_score_adj, false);
David Rientjes723548b2010-10-26 14:21:25 -07001198out:
1199 return err < 0 ? err : count;
David Rientjesa63d83f2010-08-09 17:19:46 -07001200}
1201
1202static const struct file_operations proc_oom_score_adj_operations = {
1203 .read = oom_score_adj_read,
1204 .write = oom_score_adj_write,
Arnd Bergmann6038f372010-08-15 18:52:59 +02001205 .llseek = default_llseek,
David Rientjesa63d83f2010-08-09 17:19:46 -07001206};
1207
Richard Guy Briggs4b7d2482019-01-22 17:06:39 -05001208#ifdef CONFIG_AUDIT
Alexey Dobriyanb4eb4f72016-10-29 19:04:39 +03001209#define TMPBUFLEN 11
Linus Torvalds1da177e2005-04-16 15:20:36 -07001210static ssize_t proc_loginuid_read(struct file * file, char __user * buf,
1211 size_t count, loff_t *ppos)
1212{
Al Viro496ad9a2013-01-23 17:07:38 -05001213 struct inode * inode = file_inode(file);
Eric W. Biederman99f89552006-06-26 00:25:55 -07001214 struct task_struct *task = get_proc_task(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001215 ssize_t length;
1216 char tmpbuf[TMPBUFLEN];
1217
Eric W. Biederman99f89552006-06-26 00:25:55 -07001218 if (!task)
1219 return -ESRCH;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001220 length = scnprintf(tmpbuf, TMPBUFLEN, "%u",
Eric W. Biedermane1760bd2012-09-10 22:39:43 -07001221 from_kuid(file->f_cred->user_ns,
1222 audit_get_loginuid(task)));
Eric W. Biederman99f89552006-06-26 00:25:55 -07001223 put_task_struct(task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001224 return simple_read_from_buffer(buf, count, ppos, tmpbuf, length);
1225}
1226
1227static ssize_t proc_loginuid_write(struct file * file, const char __user * buf,
1228 size_t count, loff_t *ppos)
1229{
Al Viro496ad9a2013-01-23 17:07:38 -05001230 struct inode * inode = file_inode(file);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001231 uid_t loginuid;
Eric W. Biedermane1760bd2012-09-10 22:39:43 -07001232 kuid_t kloginuid;
Alexey Dobriyan774636e2015-09-09 15:36:59 -07001233 int rv;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001234
Paul E. McKenney7dc52152010-02-22 17:04:52 -08001235 rcu_read_lock();
1236 if (current != pid_task(proc_pid(inode), PIDTYPE_PID)) {
1237 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001238 return -EPERM;
Paul E. McKenney7dc52152010-02-22 17:04:52 -08001239 }
1240 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001241
Linus Torvalds1da177e2005-04-16 15:20:36 -07001242 if (*ppos != 0) {
1243 /* No partial writes. */
1244 return -EINVAL;
1245 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001246
Alexey Dobriyan774636e2015-09-09 15:36:59 -07001247 rv = kstrtou32_from_user(buf, count, 10, &loginuid);
1248 if (rv < 0)
1249 return rv;
Eric Paris81407c82013-05-24 09:49:14 -04001250
1251 /* is userspace tring to explicitly UNSET the loginuid? */
1252 if (loginuid == AUDIT_UID_UNSET) {
1253 kloginuid = INVALID_UID;
1254 } else {
1255 kloginuid = make_kuid(file->f_cred->user_ns, loginuid);
Alexey Dobriyan774636e2015-09-09 15:36:59 -07001256 if (!uid_valid(kloginuid))
1257 return -EINVAL;
Eric W. Biedermane1760bd2012-09-10 22:39:43 -07001258 }
1259
Alexey Dobriyan774636e2015-09-09 15:36:59 -07001260 rv = audit_set_loginuid(kloginuid);
1261 if (rv < 0)
1262 return rv;
1263 return count;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001264}
1265
Arjan van de Ven00977a52007-02-12 00:55:34 -08001266static const struct file_operations proc_loginuid_operations = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001267 .read = proc_loginuid_read,
1268 .write = proc_loginuid_write,
Arnd Bergmann87df8422010-03-17 23:06:02 +01001269 .llseek = generic_file_llseek,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001270};
Eric Paris1e0bd752008-03-13 08:15:31 -04001271
1272static ssize_t proc_sessionid_read(struct file * file, char __user * buf,
1273 size_t count, loff_t *ppos)
1274{
Al Viro496ad9a2013-01-23 17:07:38 -05001275 struct inode * inode = file_inode(file);
Eric Paris1e0bd752008-03-13 08:15:31 -04001276 struct task_struct *task = get_proc_task(inode);
1277 ssize_t length;
1278 char tmpbuf[TMPBUFLEN];
1279
1280 if (!task)
1281 return -ESRCH;
1282 length = scnprintf(tmpbuf, TMPBUFLEN, "%u",
1283 audit_get_sessionid(task));
1284 put_task_struct(task);
1285 return simple_read_from_buffer(buf, count, ppos, tmpbuf, length);
1286}
1287
1288static const struct file_operations proc_sessionid_operations = {
1289 .read = proc_sessionid_read,
Arnd Bergmann87df8422010-03-17 23:06:02 +01001290 .llseek = generic_file_llseek,
Eric Paris1e0bd752008-03-13 08:15:31 -04001291};
Linus Torvalds1da177e2005-04-16 15:20:36 -07001292#endif
1293
Akinobu Mitaf4f154f2006-12-08 02:39:47 -08001294#ifdef CONFIG_FAULT_INJECTION
1295static ssize_t proc_fault_inject_read(struct file * file, char __user * buf,
1296 size_t count, loff_t *ppos)
1297{
Al Viro496ad9a2013-01-23 17:07:38 -05001298 struct task_struct *task = get_proc_task(file_inode(file));
Akinobu Mitaf4f154f2006-12-08 02:39:47 -08001299 char buffer[PROC_NUMBUF];
1300 size_t len;
1301 int make_it_fail;
Akinobu Mitaf4f154f2006-12-08 02:39:47 -08001302
1303 if (!task)
1304 return -ESRCH;
1305 make_it_fail = task->make_it_fail;
1306 put_task_struct(task);
1307
1308 len = snprintf(buffer, sizeof(buffer), "%i\n", make_it_fail);
Akinobu Mita0c28f282007-05-08 00:31:41 -07001309
1310 return simple_read_from_buffer(buf, count, ppos, buffer, len);
Akinobu Mitaf4f154f2006-12-08 02:39:47 -08001311}
1312
1313static ssize_t proc_fault_inject_write(struct file * file,
1314 const char __user * buf, size_t count, loff_t *ppos)
1315{
1316 struct task_struct *task;
Alexey Dobriyan774636e2015-09-09 15:36:59 -07001317 char buffer[PROC_NUMBUF];
Akinobu Mitaf4f154f2006-12-08 02:39:47 -08001318 int make_it_fail;
Alexey Dobriyan774636e2015-09-09 15:36:59 -07001319 int rv;
Akinobu Mitaf4f154f2006-12-08 02:39:47 -08001320
1321 if (!capable(CAP_SYS_RESOURCE))
1322 return -EPERM;
1323 memset(buffer, 0, sizeof(buffer));
1324 if (count > sizeof(buffer) - 1)
1325 count = sizeof(buffer) - 1;
1326 if (copy_from_user(buffer, buf, count))
1327 return -EFAULT;
Alexey Dobriyan774636e2015-09-09 15:36:59 -07001328 rv = kstrtoint(strstrip(buffer), 0, &make_it_fail);
1329 if (rv < 0)
1330 return rv;
Dave Jones16caed32014-04-07 15:39:15 -07001331 if (make_it_fail < 0 || make_it_fail > 1)
1332 return -EINVAL;
1333
Al Viro496ad9a2013-01-23 17:07:38 -05001334 task = get_proc_task(file_inode(file));
Akinobu Mitaf4f154f2006-12-08 02:39:47 -08001335 if (!task)
1336 return -ESRCH;
1337 task->make_it_fail = make_it_fail;
1338 put_task_struct(task);
Vincent Licba8aaf2009-09-22 16:45:38 -07001339
1340 return count;
Akinobu Mitaf4f154f2006-12-08 02:39:47 -08001341}
1342
Arjan van de Ven00977a52007-02-12 00:55:34 -08001343static const struct file_operations proc_fault_inject_operations = {
Akinobu Mitaf4f154f2006-12-08 02:39:47 -08001344 .read = proc_fault_inject_read,
1345 .write = proc_fault_inject_write,
Arnd Bergmann87df8422010-03-17 23:06:02 +01001346 .llseek = generic_file_llseek,
Akinobu Mitaf4f154f2006-12-08 02:39:47 -08001347};
Dmitry Vyukove41d58182017-07-12 14:34:35 -07001348
1349static ssize_t proc_fail_nth_write(struct file *file, const char __user *buf,
1350 size_t count, loff_t *ppos)
1351{
1352 struct task_struct *task;
Akinobu Mita9049f2f2017-07-14 14:49:52 -07001353 int err;
1354 unsigned int n;
Dmitry Vyukove41d58182017-07-12 14:34:35 -07001355
Akinobu Mita9049f2f2017-07-14 14:49:52 -07001356 err = kstrtouint_from_user(buf, count, 0, &n);
Dmitry Vyukove41d58182017-07-12 14:34:35 -07001357 if (err)
1358 return err;
Akinobu Mita1203c8e2017-07-14 14:49:57 -07001359
1360 task = get_proc_task(file_inode(file));
1361 if (!task)
1362 return -ESRCH;
Alexey Dobriyan9f7118b2018-02-06 15:36:55 -08001363 task->fail_nth = n;
Akinobu Mita1203c8e2017-07-14 14:49:57 -07001364 put_task_struct(task);
1365
Dmitry Vyukove41d58182017-07-12 14:34:35 -07001366 return count;
1367}
1368
1369static ssize_t proc_fail_nth_read(struct file *file, char __user *buf,
1370 size_t count, loff_t *ppos)
1371{
1372 struct task_struct *task;
Akinobu Mitabfc74092017-07-14 14:49:54 -07001373 char numbuf[PROC_NUMBUF];
1374 ssize_t len;
Dmitry Vyukove41d58182017-07-12 14:34:35 -07001375
1376 task = get_proc_task(file_inode(file));
1377 if (!task)
1378 return -ESRCH;
Alexey Dobriyan9f7118b2018-02-06 15:36:55 -08001379 len = snprintf(numbuf, sizeof(numbuf), "%u\n", task->fail_nth);
Akinobu Mita1203c8e2017-07-14 14:49:57 -07001380 put_task_struct(task);
Alexey Dobriyana44937f2018-08-21 21:54:27 -07001381 return simple_read_from_buffer(buf, count, ppos, numbuf, len);
Dmitry Vyukove41d58182017-07-12 14:34:35 -07001382}
1383
1384static const struct file_operations proc_fail_nth_operations = {
1385 .read = proc_fail_nth_read,
1386 .write = proc_fail_nth_write,
1387};
Akinobu Mitaf4f154f2006-12-08 02:39:47 -08001388#endif
1389
Arjan van de Ven97455122008-01-25 21:08:34 +01001390
Ingo Molnar43ae34c2007-07-09 18:52:00 +02001391#ifdef CONFIG_SCHED_DEBUG
1392/*
1393 * Print out various scheduling related per-task fields:
1394 */
1395static int sched_show(struct seq_file *m, void *v)
1396{
1397 struct inode *inode = m->private;
Christoph Hellwig76f668b2018-05-16 07:19:01 +02001398 struct pid_namespace *ns = proc_pid_ns(inode);
Ingo Molnar43ae34c2007-07-09 18:52:00 +02001399 struct task_struct *p;
1400
Ingo Molnar43ae34c2007-07-09 18:52:00 +02001401 p = get_proc_task(inode);
1402 if (!p)
1403 return -ESRCH;
Aleksa Sarai74dc3382017-08-06 14:41:41 +10001404 proc_sched_show_task(p, ns, m);
Ingo Molnar43ae34c2007-07-09 18:52:00 +02001405
1406 put_task_struct(p);
1407
1408 return 0;
1409}
1410
1411static ssize_t
1412sched_write(struct file *file, const char __user *buf,
1413 size_t count, loff_t *offset)
1414{
Al Viro496ad9a2013-01-23 17:07:38 -05001415 struct inode *inode = file_inode(file);
Ingo Molnar43ae34c2007-07-09 18:52:00 +02001416 struct task_struct *p;
1417
Ingo Molnar43ae34c2007-07-09 18:52:00 +02001418 p = get_proc_task(inode);
1419 if (!p)
1420 return -ESRCH;
1421 proc_sched_set_task(p);
1422
1423 put_task_struct(p);
1424
1425 return count;
1426}
1427
1428static int sched_open(struct inode *inode, struct file *filp)
1429{
Jovi Zhangc6a34052011-01-12 17:00:34 -08001430 return single_open(filp, sched_show, inode);
Ingo Molnar43ae34c2007-07-09 18:52:00 +02001431}
1432
1433static const struct file_operations proc_pid_sched_operations = {
1434 .open = sched_open,
1435 .read = seq_read,
1436 .write = sched_write,
1437 .llseek = seq_lseek,
Alexey Dobriyan5ea473a2007-07-31 00:38:50 -07001438 .release = single_release,
Ingo Molnar43ae34c2007-07-09 18:52:00 +02001439};
1440
1441#endif
1442
Mike Galbraith5091faa2010-11-30 14:18:03 +01001443#ifdef CONFIG_SCHED_AUTOGROUP
1444/*
1445 * Print out autogroup related information:
1446 */
1447static int sched_autogroup_show(struct seq_file *m, void *v)
1448{
1449 struct inode *inode = m->private;
1450 struct task_struct *p;
1451
1452 p = get_proc_task(inode);
1453 if (!p)
1454 return -ESRCH;
1455 proc_sched_autogroup_show_task(p, m);
1456
1457 put_task_struct(p);
1458
1459 return 0;
1460}
1461
1462static ssize_t
1463sched_autogroup_write(struct file *file, const char __user *buf,
1464 size_t count, loff_t *offset)
1465{
Al Viro496ad9a2013-01-23 17:07:38 -05001466 struct inode *inode = file_inode(file);
Mike Galbraith5091faa2010-11-30 14:18:03 +01001467 struct task_struct *p;
1468 char buffer[PROC_NUMBUF];
Alexey Dobriyan0a8cb8e2011-05-26 16:25:50 -07001469 int nice;
Mike Galbraith5091faa2010-11-30 14:18:03 +01001470 int err;
1471
1472 memset(buffer, 0, sizeof(buffer));
1473 if (count > sizeof(buffer) - 1)
1474 count = sizeof(buffer) - 1;
1475 if (copy_from_user(buffer, buf, count))
1476 return -EFAULT;
1477
Alexey Dobriyan0a8cb8e2011-05-26 16:25:50 -07001478 err = kstrtoint(strstrip(buffer), 0, &nice);
1479 if (err < 0)
1480 return err;
Mike Galbraith5091faa2010-11-30 14:18:03 +01001481
1482 p = get_proc_task(inode);
1483 if (!p)
1484 return -ESRCH;
1485
Hiroshi Shimamoto2e5b5b32012-02-23 17:41:27 +09001486 err = proc_sched_autogroup_set_nice(p, nice);
Mike Galbraith5091faa2010-11-30 14:18:03 +01001487 if (err)
1488 count = err;
1489
1490 put_task_struct(p);
1491
1492 return count;
1493}
1494
1495static int sched_autogroup_open(struct inode *inode, struct file *filp)
1496{
1497 int ret;
1498
1499 ret = single_open(filp, sched_autogroup_show, NULL);
1500 if (!ret) {
1501 struct seq_file *m = filp->private_data;
1502
1503 m->private = inode;
1504 }
1505 return ret;
1506}
1507
1508static const struct file_operations proc_pid_sched_autogroup_operations = {
1509 .open = sched_autogroup_open,
1510 .read = seq_read,
1511 .write = sched_autogroup_write,
1512 .llseek = seq_lseek,
1513 .release = single_release,
1514};
1515
1516#endif /* CONFIG_SCHED_AUTOGROUP */
1517
john stultz4614a696b2009-12-14 18:00:05 -08001518static ssize_t comm_write(struct file *file, const char __user *buf,
1519 size_t count, loff_t *offset)
1520{
Al Viro496ad9a2013-01-23 17:07:38 -05001521 struct inode *inode = file_inode(file);
john stultz4614a696b2009-12-14 18:00:05 -08001522 struct task_struct *p;
1523 char buffer[TASK_COMM_LEN];
David Rientjes830e0fc2013-04-30 15:28:18 -07001524 const size_t maxlen = sizeof(buffer) - 1;
john stultz4614a696b2009-12-14 18:00:05 -08001525
1526 memset(buffer, 0, sizeof(buffer));
David Rientjes830e0fc2013-04-30 15:28:18 -07001527 if (copy_from_user(buffer, buf, count > maxlen ? maxlen : count))
john stultz4614a696b2009-12-14 18:00:05 -08001528 return -EFAULT;
1529
1530 p = get_proc_task(inode);
1531 if (!p)
1532 return -ESRCH;
1533
1534 if (same_thread_group(current, p))
1535 set_task_comm(p, buffer);
1536 else
1537 count = -EINVAL;
1538
1539 put_task_struct(p);
1540
1541 return count;
1542}
1543
1544static int comm_show(struct seq_file *m, void *v)
1545{
1546 struct inode *inode = m->private;
1547 struct task_struct *p;
1548
1549 p = get_proc_task(inode);
1550 if (!p)
1551 return -ESRCH;
1552
Tejun Heo88b72b32018-05-18 08:47:13 -07001553 proc_task_name(m, p, false);
1554 seq_putc(m, '\n');
john stultz4614a696b2009-12-14 18:00:05 -08001555
1556 put_task_struct(p);
1557
1558 return 0;
1559}
1560
1561static int comm_open(struct inode *inode, struct file *filp)
1562{
Jovi Zhangc6a34052011-01-12 17:00:34 -08001563 return single_open(filp, comm_show, inode);
john stultz4614a696b2009-12-14 18:00:05 -08001564}
1565
1566static const struct file_operations proc_pid_set_comm_operations = {
1567 .open = comm_open,
1568 .read = seq_read,
1569 .write = comm_write,
1570 .llseek = seq_lseek,
1571 .release = single_release,
1572};
1573
Cyrill Gorcunov7773fbc2012-01-10 15:11:20 -08001574static int proc_exe_link(struct dentry *dentry, struct path *exe_path)
Matt Helsley925d1c42008-04-29 01:01:36 -07001575{
1576 struct task_struct *task;
Matt Helsley925d1c42008-04-29 01:01:36 -07001577 struct file *exe_file;
1578
David Howells2b0143b2015-03-17 22:25:59 +00001579 task = get_proc_task(d_inode(dentry));
Matt Helsley925d1c42008-04-29 01:01:36 -07001580 if (!task)
1581 return -ENOENT;
Mateusz Guzikcd81a9172016-08-23 16:20:38 +02001582 exe_file = get_task_exe_file(task);
Matt Helsley925d1c42008-04-29 01:01:36 -07001583 put_task_struct(task);
Matt Helsley925d1c42008-04-29 01:01:36 -07001584 if (exe_file) {
1585 *exe_path = exe_file->f_path;
1586 path_get(&exe_file->f_path);
1587 fput(exe_file);
1588 return 0;
1589 } else
1590 return -ENOENT;
1591}
1592
Al Viro6b255392015-11-17 10:20:54 -05001593static const char *proc_pid_get_link(struct dentry *dentry,
Al Virofceef392015-12-29 15:58:39 -05001594 struct inode *inode,
1595 struct delayed_call *done)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001596{
Christoph Hellwig408ef012012-06-18 10:47:03 -04001597 struct path path;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001598 int error = -EACCES;
1599
Al Viro6b255392015-11-17 10:20:54 -05001600 if (!dentry)
1601 return ERR_PTR(-ECHILD);
1602
Eric W. Biederman778c1142006-06-26 00:25:58 -07001603 /* Are we allowed to snoop on the tasks file descriptors? */
1604 if (!proc_fd_access_allowed(inode))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001605 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001606
Christoph Hellwig408ef012012-06-18 10:47:03 -04001607 error = PROC_I(inode)->op.proc_get_link(dentry, &path);
1608 if (error)
1609 goto out;
1610
Al Viro6e77137b2015-05-02 13:37:52 -04001611 nd_jump_link(&path);
Christoph Hellwig408ef012012-06-18 10:47:03 -04001612 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001613out:
Al Viro008b1502005-08-20 00:17:39 +01001614 return ERR_PTR(error);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001615}
1616
Jan Blunck3dcd25f2008-02-14 19:38:35 -08001617static int do_proc_readlink(struct path *path, char __user *buffer, int buflen)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001618{
Michal Hocko0ee931c2017-09-13 16:28:29 -07001619 char *tmp = (char *)__get_free_page(GFP_KERNEL);
Jan Blunck3dcd25f2008-02-14 19:38:35 -08001620 char *pathname;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001621 int len;
1622
1623 if (!tmp)
1624 return -ENOMEM;
Akinobu Mita0c28f282007-05-08 00:31:41 -07001625
Eric W. Biederman7b2a69b2010-12-05 15:51:21 -08001626 pathname = d_path(path, tmp, PAGE_SIZE);
Jan Blunck3dcd25f2008-02-14 19:38:35 -08001627 len = PTR_ERR(pathname);
1628 if (IS_ERR(pathname))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001629 goto out;
Jan Blunck3dcd25f2008-02-14 19:38:35 -08001630 len = tmp + PAGE_SIZE - 1 - pathname;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001631
1632 if (len > buflen)
1633 len = buflen;
Jan Blunck3dcd25f2008-02-14 19:38:35 -08001634 if (copy_to_user(buffer, pathname, len))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001635 len = -EFAULT;
1636 out:
1637 free_page((unsigned long)tmp);
1638 return len;
1639}
1640
1641static int proc_pid_readlink(struct dentry * dentry, char __user * buffer, int buflen)
1642{
1643 int error = -EACCES;
David Howells2b0143b2015-03-17 22:25:59 +00001644 struct inode *inode = d_inode(dentry);
Jan Blunck3dcd25f2008-02-14 19:38:35 -08001645 struct path path;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001646
Eric W. Biederman778c1142006-06-26 00:25:58 -07001647 /* Are we allowed to snoop on the tasks file descriptors? */
1648 if (!proc_fd_access_allowed(inode))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001649 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001650
Cyrill Gorcunov7773fbc2012-01-10 15:11:20 -08001651 error = PROC_I(inode)->op.proc_get_link(dentry, &path);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001652 if (error)
1653 goto out;
1654
Jan Blunck3dcd25f2008-02-14 19:38:35 -08001655 error = do_proc_readlink(&path, buffer, buflen);
1656 path_put(&path);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001657out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001658 return error;
1659}
1660
Cyrill Gorcunovfaf60af2012-08-23 14:43:24 +04001661const struct inode_operations proc_pid_link_inode_operations = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001662 .readlink = proc_pid_readlink,
Al Viro6b255392015-11-17 10:20:54 -05001663 .get_link = proc_pid_get_link,
Linus Torvalds6d76fa52006-07-15 12:26:45 -07001664 .setattr = proc_setattr,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001665};
1666
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001667
1668/* building an inode */
1669
Al Viroc6eb50d2017-09-30 14:45:42 -04001670void task_dump_owner(struct task_struct *task, umode_t mode,
Eric W. Biederman68eb94f2017-01-03 10:23:11 +13001671 kuid_t *ruid, kgid_t *rgid)
1672{
1673 /* Depending on the state of dumpable compute who should own a
1674 * proc file for a task.
1675 */
1676 const struct cred *cred;
1677 kuid_t uid;
1678 kgid_t gid;
1679
Alexey Dobriyan2e0ad552018-04-20 14:56:03 -07001680 if (unlikely(task->flags & PF_KTHREAD)) {
1681 *ruid = GLOBAL_ROOT_UID;
1682 *rgid = GLOBAL_ROOT_GID;
1683 return;
1684 }
1685
Eric W. Biederman68eb94f2017-01-03 10:23:11 +13001686 /* Default to the tasks effective ownership */
1687 rcu_read_lock();
1688 cred = __task_cred(task);
1689 uid = cred->euid;
1690 gid = cred->egid;
1691 rcu_read_unlock();
1692
1693 /*
1694 * Before the /proc/pid/status file was created the only way to read
1695 * the effective uid of a /process was to stat /proc/pid. Reading
1696 * /proc/pid/status is slow enough that procps and other packages
1697 * kept stating /proc/pid. To keep the rules in /proc simple I have
1698 * made this apply to all per process world readable and executable
1699 * directories.
1700 */
1701 if (mode != (S_IFDIR|S_IRUGO|S_IXUGO)) {
1702 struct mm_struct *mm;
1703 task_lock(task);
1704 mm = task->mm;
1705 /* Make non-dumpable tasks owned by some root */
1706 if (mm) {
1707 if (get_dumpable(mm) != SUID_DUMP_USER) {
1708 struct user_namespace *user_ns = mm->user_ns;
1709
1710 uid = make_kuid(user_ns, 0);
1711 if (!uid_valid(uid))
1712 uid = GLOBAL_ROOT_UID;
1713
1714 gid = make_kgid(user_ns, 0);
1715 if (!gid_valid(gid))
1716 gid = GLOBAL_ROOT_GID;
1717 }
1718 } else {
1719 uid = GLOBAL_ROOT_UID;
1720 gid = GLOBAL_ROOT_GID;
1721 }
1722 task_unlock(task);
1723 }
1724 *ruid = uid;
1725 *rgid = gid;
1726}
1727
Andreas Gruenbacherdb978da2016-11-10 22:18:28 +01001728struct inode *proc_pid_make_inode(struct super_block * sb,
1729 struct task_struct *task, umode_t mode)
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001730{
1731 struct inode * inode;
1732 struct proc_inode *ei;
1733
1734 /* We need a new inode */
1735
1736 inode = new_inode(sb);
1737 if (!inode)
1738 goto out;
1739
1740 /* Common stuff */
1741 ei = PROC_I(inode);
Andreas Gruenbacherdb978da2016-11-10 22:18:28 +01001742 inode->i_mode = mode;
Christoph Hellwig85fe4022010-10-23 11:19:54 -04001743 inode->i_ino = get_next_ino();
Deepa Dinamani078cd822016-09-14 07:48:04 -07001744 inode->i_mtime = inode->i_atime = inode->i_ctime = current_time(inode);
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001745 inode->i_op = &proc_def_inode_operations;
1746
1747 /*
1748 * grab the reference to task.
1749 */
Oleg Nesterov1a657f782006-10-02 02:18:59 -07001750 ei->pid = get_task_pid(task, PIDTYPE_PID);
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001751 if (!ei->pid)
1752 goto out_unlock;
1753
Eric W. Biederman68eb94f2017-01-03 10:23:11 +13001754 task_dump_owner(task, 0, &inode->i_uid, &inode->i_gid);
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001755 security_task_to_inode(task, inode);
1756
1757out:
1758 return inode;
1759
1760out_unlock:
1761 iput(inode);
1762 return NULL;
1763}
1764
David Howellsa528d352017-01-31 16:46:22 +00001765int pid_getattr(const struct path *path, struct kstat *stat,
1766 u32 request_mask, unsigned int query_flags)
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001767{
David Howellsa528d352017-01-31 16:46:22 +00001768 struct inode *inode = d_inode(path->dentry);
Christoph Hellwig76f668b2018-05-16 07:19:01 +02001769 struct pid_namespace *pid = proc_pid_ns(inode);
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001770 struct task_struct *task;
David Howellsc69e8d92008-11-14 10:39:19 +11001771
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001772 generic_fillattr(inode, stat);
1773
Eric W. Biedermandcb0f222012-02-09 08:48:21 -08001774 stat->uid = GLOBAL_ROOT_UID;
1775 stat->gid = GLOBAL_ROOT_GID;
Alexey Dobriyan94116922018-06-07 17:10:07 -07001776 rcu_read_lock();
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001777 task = pid_task(proc_pid(inode), PIDTYPE_PID);
1778 if (task) {
Lafcadio Wluiki796f5712017-02-24 15:00:23 -08001779 if (!has_pid_permissions(pid, task, HIDEPID_INVISIBLE)) {
Vasiliy Kulikov04996802012-01-10 15:11:31 -08001780 rcu_read_unlock();
1781 /*
1782 * This doesn't prevent learning whether PID exists,
1783 * it only makes getattr() consistent with readdir().
1784 */
1785 return -ENOENT;
1786 }
Eric W. Biederman68eb94f2017-01-03 10:23:11 +13001787 task_dump_owner(task, inode->i_mode, &stat->uid, &stat->gid);
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001788 }
1789 rcu_read_unlock();
1790 return 0;
1791}
1792
1793/* dentry stuff */
1794
1795/*
Al Viro1bbc5512018-05-02 21:26:16 -04001796 * Set <pid>/... inode ownership (can change due to setuid(), etc.)
1797 */
1798void pid_update_inode(struct task_struct *task, struct inode *inode)
1799{
1800 task_dump_owner(task, inode->i_mode, &inode->i_uid, &inode->i_gid);
1801
1802 inode->i_mode &= ~(S_ISUID | S_ISGID);
1803 security_task_to_inode(task, inode);
1804}
1805
1806/*
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001807 * Rewrite the inode's ownerships here because the owning task may have
1808 * performed a setuid(), etc.
1809 *
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001810 */
Al Viro1bbc5512018-05-02 21:26:16 -04001811static int pid_revalidate(struct dentry *dentry, unsigned int flags)
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001812{
Nick Piggin34286d62011-01-07 17:49:57 +11001813 struct inode *inode;
1814 struct task_struct *task;
David Howellsc69e8d92008-11-14 10:39:19 +11001815
Al Viro0b728e12012-06-10 16:03:43 -04001816 if (flags & LOOKUP_RCU)
Nick Piggin34286d62011-01-07 17:49:57 +11001817 return -ECHILD;
1818
David Howells2b0143b2015-03-17 22:25:59 +00001819 inode = d_inode(dentry);
Nick Piggin34286d62011-01-07 17:49:57 +11001820 task = get_proc_task(inode);
1821
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001822 if (task) {
Al Viro1bbc5512018-05-02 21:26:16 -04001823 pid_update_inode(task, inode);
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001824 put_task_struct(task);
1825 return 1;
1826 }
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001827 return 0;
1828}
1829
Oleg Nesterovd855a4b2014-01-23 15:55:39 -08001830static inline bool proc_inode_is_dead(struct inode *inode)
1831{
1832 return !proc_pid(inode)->tasks[PIDTYPE_PID].first;
1833}
1834
David Howells1dd704b2013-04-12 01:08:50 +01001835int pid_delete_dentry(const struct dentry *dentry)
1836{
1837 /* Is the task we represent dead?
1838 * If so, then don't put the dentry on the lru list,
1839 * kill it immediately.
1840 */
David Howells2b0143b2015-03-17 22:25:59 +00001841 return proc_inode_is_dead(d_inode(dentry));
David Howells1dd704b2013-04-12 01:08:50 +01001842}
1843
Eric W. Biederman6b4e3062010-03-07 16:41:34 -08001844const struct dentry_operations pid_dentry_operations =
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001845{
1846 .d_revalidate = pid_revalidate,
1847 .d_delete = pid_delete_dentry,
1848};
1849
1850/* Lookups */
1851
Eric W. Biederman1c0d04c2006-10-02 02:18:57 -07001852/*
1853 * Fill a directory entry.
1854 *
1855 * If possible create the dcache entry and derive our inode number and
1856 * file type from dcache entry.
1857 *
1858 * Since all of the proc inode numbers are dynamically generated, the inode
1859 * numbers do not exist until the inode is cache. This means creating the
1860 * the dcache entry in readdir is necessary to keep the inode numbers
1861 * reported by readdir in sync with the inode numbers reported
1862 * by stat.
1863 */
Al Virof0c3b502013-05-16 12:07:31 -04001864bool proc_fill_cache(struct file *file, struct dir_context *ctx,
Alexey Dobriyana4ef3892018-06-07 17:10:10 -07001865 const char *name, unsigned int len,
Eric Dumazetc5141e62007-05-08 00:26:15 -07001866 instantiate_t instantiate, struct task_struct *task, const void *ptr)
Eric W. Biederman61a28782006-10-02 02:18:49 -07001867{
Al Virof0c3b502013-05-16 12:07:31 -04001868 struct dentry *child, *dir = file->f_path.dentry;
Al Viro1df98b82013-06-15 11:33:10 +04001869 struct qstr qname = QSTR_INIT(name, len);
Eric W. Biederman61a28782006-10-02 02:18:49 -07001870 struct inode *inode;
Al Viro0168b9e2018-05-03 09:21:05 -04001871 unsigned type = DT_UNKNOWN;
1872 ino_t ino = 1;
Eric W. Biederman61a28782006-10-02 02:18:49 -07001873
Al Viro1df98b82013-06-15 11:33:10 +04001874 child = d_hash_and_lookup(dir, &qname);
Eric W. Biederman61a28782006-10-02 02:18:49 -07001875 if (!child) {
Al Viro37817642016-04-20 16:31:31 -04001876 DECLARE_WAIT_QUEUE_HEAD_ONSTACK(wq);
1877 child = d_alloc_parallel(dir, &qname, &wq);
1878 if (IS_ERR(child))
Al Viro1df98b82013-06-15 11:33:10 +04001879 goto end_instantiate;
Al Viro37817642016-04-20 16:31:31 -04001880 if (d_in_lookup(child)) {
Al Viro0168b9e2018-05-03 09:21:05 -04001881 struct dentry *res;
1882 res = instantiate(child, task, ptr);
Al Viro37817642016-04-20 16:31:31 -04001883 d_lookup_done(child);
Al Viro0168b9e2018-05-03 09:21:05 -04001884 if (unlikely(res)) {
1885 dput(child);
1886 child = res;
Al Virod85b3992018-06-08 01:17:11 -04001887 if (IS_ERR(child))
1888 goto end_instantiate;
Al Viro37817642016-04-20 16:31:31 -04001889 }
Eric W. Biederman61a28782006-10-02 02:18:49 -07001890 }
1891 }
David Howells2b0143b2015-03-17 22:25:59 +00001892 inode = d_inode(child);
Al Viro147ce692013-06-15 10:26:35 +04001893 ino = inode->i_ino;
1894 type = inode->i_mode >> 12;
Eric W. Biederman61a28782006-10-02 02:18:49 -07001895 dput(child);
Al Virod85b3992018-06-08 01:17:11 -04001896end_instantiate:
Al Virof0c3b502013-05-16 12:07:31 -04001897 return dir_emit(ctx, name, len, ino, type);
Eric W. Biederman61a28782006-10-02 02:18:49 -07001898}
1899
Pavel Emelyanov640708a2012-01-10 15:11:23 -08001900/*
1901 * dname_to_vma_addr - maps a dentry name into two unsigned longs
1902 * which represent vma start and end addresses.
1903 */
1904static int dname_to_vma_addr(struct dentry *dentry,
1905 unsigned long *start, unsigned long *end)
1906{
Alexey Dobriyanac7f1062018-02-06 15:36:59 -08001907 const char *str = dentry->d_name.name;
1908 unsigned long long sval, eval;
1909 unsigned int len;
1910
Alexey Dobriyan35318db2018-04-10 16:41:14 -07001911 if (str[0] == '0' && str[1] != '-')
1912 return -EINVAL;
Alexey Dobriyanac7f1062018-02-06 15:36:59 -08001913 len = _parse_integer(str, 16, &sval);
1914 if (len & KSTRTOX_OVERFLOW)
Pavel Emelyanov640708a2012-01-10 15:11:23 -08001915 return -EINVAL;
Alexey Dobriyanac7f1062018-02-06 15:36:59 -08001916 if (sval != (unsigned long)sval)
1917 return -EINVAL;
1918 str += len;
1919
1920 if (*str != '-')
1921 return -EINVAL;
1922 str++;
1923
Alexey Dobriyan35318db2018-04-10 16:41:14 -07001924 if (str[0] == '0' && str[1])
1925 return -EINVAL;
Alexey Dobriyanac7f1062018-02-06 15:36:59 -08001926 len = _parse_integer(str, 16, &eval);
1927 if (len & KSTRTOX_OVERFLOW)
1928 return -EINVAL;
1929 if (eval != (unsigned long)eval)
1930 return -EINVAL;
1931 str += len;
1932
1933 if (*str != '\0')
1934 return -EINVAL;
1935
1936 *start = sval;
1937 *end = eval;
Pavel Emelyanov640708a2012-01-10 15:11:23 -08001938
1939 return 0;
1940}
1941
Al Viro0b728e12012-06-10 16:03:43 -04001942static int map_files_d_revalidate(struct dentry *dentry, unsigned int flags)
Pavel Emelyanov640708a2012-01-10 15:11:23 -08001943{
1944 unsigned long vm_start, vm_end;
1945 bool exact_vma_exists = false;
1946 struct mm_struct *mm = NULL;
1947 struct task_struct *task;
Pavel Emelyanov640708a2012-01-10 15:11:23 -08001948 struct inode *inode;
1949 int status = 0;
1950
Al Viro0b728e12012-06-10 16:03:43 -04001951 if (flags & LOOKUP_RCU)
Pavel Emelyanov640708a2012-01-10 15:11:23 -08001952 return -ECHILD;
1953
David Howells2b0143b2015-03-17 22:25:59 +00001954 inode = d_inode(dentry);
Pavel Emelyanov640708a2012-01-10 15:11:23 -08001955 task = get_proc_task(inode);
1956 if (!task)
1957 goto out_notask;
1958
Jann Horncaaee622016-01-20 15:00:04 -08001959 mm = mm_access(task, PTRACE_MODE_READ_FSCREDS);
Cong Wang2344bec2012-05-31 16:26:18 -07001960 if (IS_ERR_OR_NULL(mm))
Pavel Emelyanov640708a2012-01-10 15:11:23 -08001961 goto out;
1962
1963 if (!dname_to_vma_addr(dentry, &vm_start, &vm_end)) {
Konstantin Khlebnikovcd9e2bb2019-07-11 21:00:03 -07001964 status = down_read_killable(&mm->mmap_sem);
1965 if (!status) {
1966 exact_vma_exists = !!find_exact_vma(mm, vm_start,
1967 vm_end);
1968 up_read(&mm->mmap_sem);
1969 }
Pavel Emelyanov640708a2012-01-10 15:11:23 -08001970 }
1971
1972 mmput(mm);
1973
1974 if (exact_vma_exists) {
Eric W. Biederman68eb94f2017-01-03 10:23:11 +13001975 task_dump_owner(task, 0, &inode->i_uid, &inode->i_gid);
1976
Pavel Emelyanov640708a2012-01-10 15:11:23 -08001977 security_task_to_inode(task, inode);
1978 status = 1;
1979 }
1980
1981out:
1982 put_task_struct(task);
1983
1984out_notask:
Pavel Emelyanov640708a2012-01-10 15:11:23 -08001985 return status;
1986}
1987
1988static const struct dentry_operations tid_map_files_dentry_operations = {
1989 .d_revalidate = map_files_d_revalidate,
1990 .d_delete = pid_delete_dentry,
1991};
1992
Al Viro6b255392015-11-17 10:20:54 -05001993static int map_files_get_link(struct dentry *dentry, struct path *path)
Pavel Emelyanov640708a2012-01-10 15:11:23 -08001994{
1995 unsigned long vm_start, vm_end;
1996 struct vm_area_struct *vma;
1997 struct task_struct *task;
1998 struct mm_struct *mm;
1999 int rc;
2000
2001 rc = -ENOENT;
David Howells2b0143b2015-03-17 22:25:59 +00002002 task = get_proc_task(d_inode(dentry));
Pavel Emelyanov640708a2012-01-10 15:11:23 -08002003 if (!task)
2004 goto out;
2005
2006 mm = get_task_mm(task);
2007 put_task_struct(task);
2008 if (!mm)
2009 goto out;
2010
2011 rc = dname_to_vma_addr(dentry, &vm_start, &vm_end);
2012 if (rc)
2013 goto out_mmput;
2014
Konstantin Khlebnikovcd9e2bb2019-07-11 21:00:03 -07002015 rc = down_read_killable(&mm->mmap_sem);
2016 if (rc)
2017 goto out_mmput;
2018
Artem Fetishev70335ab2014-03-10 15:49:45 -07002019 rc = -ENOENT;
Pavel Emelyanov640708a2012-01-10 15:11:23 -08002020 vma = find_exact_vma(mm, vm_start, vm_end);
2021 if (vma && vma->vm_file) {
2022 *path = vma->vm_file->f_path;
2023 path_get(path);
2024 rc = 0;
2025 }
2026 up_read(&mm->mmap_sem);
2027
2028out_mmput:
2029 mmput(mm);
2030out:
2031 return rc;
2032}
2033
2034struct map_files_info {
Alexey Dobriyan20d28cd2018-02-06 15:37:06 -08002035 unsigned long start;
2036 unsigned long end;
Al Viro7b540d02012-08-27 14:55:26 -04002037 fmode_t mode;
Pavel Emelyanov640708a2012-01-10 15:11:23 -08002038};
2039
Calvin Owensbdb4d102015-09-09 15:35:54 -07002040/*
2041 * Only allow CAP_SYS_ADMIN to follow the links, due to concerns about how the
2042 * symlinks may be used to bypass permissions on ancestor directories in the
2043 * path to the file in question.
2044 */
2045static const char *
Al Viro6b255392015-11-17 10:20:54 -05002046proc_map_files_get_link(struct dentry *dentry,
Al Virofceef392015-12-29 15:58:39 -05002047 struct inode *inode,
2048 struct delayed_call *done)
Calvin Owensbdb4d102015-09-09 15:35:54 -07002049{
2050 if (!capable(CAP_SYS_ADMIN))
2051 return ERR_PTR(-EPERM);
2052
Al Virofceef392015-12-29 15:58:39 -05002053 return proc_pid_get_link(dentry, inode, done);
Calvin Owensbdb4d102015-09-09 15:35:54 -07002054}
2055
2056/*
Al Viro6b255392015-11-17 10:20:54 -05002057 * Identical to proc_pid_link_inode_operations except for get_link()
Calvin Owensbdb4d102015-09-09 15:35:54 -07002058 */
2059static const struct inode_operations proc_map_files_link_inode_operations = {
2060 .readlink = proc_pid_readlink,
Al Viro6b255392015-11-17 10:20:54 -05002061 .get_link = proc_map_files_get_link,
Calvin Owensbdb4d102015-09-09 15:35:54 -07002062 .setattr = proc_setattr,
2063};
2064
Al Viro0168b9e2018-05-03 09:21:05 -04002065static struct dentry *
2066proc_map_files_instantiate(struct dentry *dentry,
Pavel Emelyanov640708a2012-01-10 15:11:23 -08002067 struct task_struct *task, const void *ptr)
2068{
Al Viro7b540d02012-08-27 14:55:26 -04002069 fmode_t mode = (fmode_t)(unsigned long)ptr;
Pavel Emelyanov640708a2012-01-10 15:11:23 -08002070 struct proc_inode *ei;
2071 struct inode *inode;
2072
Al Viro0168b9e2018-05-03 09:21:05 -04002073 inode = proc_pid_make_inode(dentry->d_sb, task, S_IFLNK |
Andreas Gruenbacherdb978da2016-11-10 22:18:28 +01002074 ((mode & FMODE_READ ) ? S_IRUSR : 0) |
2075 ((mode & FMODE_WRITE) ? S_IWUSR : 0));
Pavel Emelyanov640708a2012-01-10 15:11:23 -08002076 if (!inode)
Al Viro0168b9e2018-05-03 09:21:05 -04002077 return ERR_PTR(-ENOENT);
Pavel Emelyanov640708a2012-01-10 15:11:23 -08002078
2079 ei = PROC_I(inode);
Al Viro6b255392015-11-17 10:20:54 -05002080 ei->op.proc_get_link = map_files_get_link;
Pavel Emelyanov640708a2012-01-10 15:11:23 -08002081
Calvin Owensbdb4d102015-09-09 15:35:54 -07002082 inode->i_op = &proc_map_files_link_inode_operations;
Pavel Emelyanov640708a2012-01-10 15:11:23 -08002083 inode->i_size = 64;
Pavel Emelyanov640708a2012-01-10 15:11:23 -08002084
2085 d_set_d_op(dentry, &tid_map_files_dentry_operations);
Al Viro0168b9e2018-05-03 09:21:05 -04002086 return d_splice_alias(inode, dentry);
Pavel Emelyanov640708a2012-01-10 15:11:23 -08002087}
2088
2089static struct dentry *proc_map_files_lookup(struct inode *dir,
Al Viro00cd8dd2012-06-10 17:13:09 -04002090 struct dentry *dentry, unsigned int flags)
Pavel Emelyanov640708a2012-01-10 15:11:23 -08002091{
2092 unsigned long vm_start, vm_end;
2093 struct vm_area_struct *vma;
2094 struct task_struct *task;
Al Viro0168b9e2018-05-03 09:21:05 -04002095 struct dentry *result;
Pavel Emelyanov640708a2012-01-10 15:11:23 -08002096 struct mm_struct *mm;
2097
Al Viro0168b9e2018-05-03 09:21:05 -04002098 result = ERR_PTR(-ENOENT);
Pavel Emelyanov640708a2012-01-10 15:11:23 -08002099 task = get_proc_task(dir);
2100 if (!task)
2101 goto out;
2102
Al Viro0168b9e2018-05-03 09:21:05 -04002103 result = ERR_PTR(-EACCES);
Jann Horncaaee622016-01-20 15:00:04 -08002104 if (!ptrace_may_access(task, PTRACE_MODE_READ_FSCREDS))
Pavel Emelyanov640708a2012-01-10 15:11:23 -08002105 goto out_put_task;
2106
Al Viro0168b9e2018-05-03 09:21:05 -04002107 result = ERR_PTR(-ENOENT);
Pavel Emelyanov640708a2012-01-10 15:11:23 -08002108 if (dname_to_vma_addr(dentry, &vm_start, &vm_end))
Cyrill Gorcunoveb94cd92012-05-17 17:03:25 -07002109 goto out_put_task;
Pavel Emelyanov640708a2012-01-10 15:11:23 -08002110
2111 mm = get_task_mm(task);
2112 if (!mm)
Cyrill Gorcunoveb94cd92012-05-17 17:03:25 -07002113 goto out_put_task;
Pavel Emelyanov640708a2012-01-10 15:11:23 -08002114
Konstantin Khlebnikovcd9e2bb2019-07-11 21:00:03 -07002115 result = ERR_PTR(-EINTR);
2116 if (down_read_killable(&mm->mmap_sem))
2117 goto out_put_mm;
2118
2119 result = ERR_PTR(-ENOENT);
Pavel Emelyanov640708a2012-01-10 15:11:23 -08002120 vma = find_exact_vma(mm, vm_start, vm_end);
2121 if (!vma)
2122 goto out_no_vma;
2123
Stanislav Kinsbursky05f56482012-11-26 16:29:42 -08002124 if (vma->vm_file)
Al Viro0168b9e2018-05-03 09:21:05 -04002125 result = proc_map_files_instantiate(dentry, task,
Stanislav Kinsbursky05f56482012-11-26 16:29:42 -08002126 (void *)(unsigned long)vma->vm_file->f_mode);
Pavel Emelyanov640708a2012-01-10 15:11:23 -08002127
2128out_no_vma:
2129 up_read(&mm->mmap_sem);
Konstantin Khlebnikovcd9e2bb2019-07-11 21:00:03 -07002130out_put_mm:
Pavel Emelyanov640708a2012-01-10 15:11:23 -08002131 mmput(mm);
Pavel Emelyanov640708a2012-01-10 15:11:23 -08002132out_put_task:
2133 put_task_struct(task);
2134out:
Al Viro0168b9e2018-05-03 09:21:05 -04002135 return result;
Pavel Emelyanov640708a2012-01-10 15:11:23 -08002136}
2137
2138static const struct inode_operations proc_map_files_inode_operations = {
2139 .lookup = proc_map_files_lookup,
2140 .permission = proc_fd_permission,
2141 .setattr = proc_setattr,
2142};
2143
2144static int
Al Virof0c3b502013-05-16 12:07:31 -04002145proc_map_files_readdir(struct file *file, struct dir_context *ctx)
Pavel Emelyanov640708a2012-01-10 15:11:23 -08002146{
Pavel Emelyanov640708a2012-01-10 15:11:23 -08002147 struct vm_area_struct *vma;
2148 struct task_struct *task;
2149 struct mm_struct *mm;
Al Virof0c3b502013-05-16 12:07:31 -04002150 unsigned long nr_files, pos, i;
Kent Overstreet94f8f3b2019-03-11 23:31:18 -07002151 GENRADIX(struct map_files_info) fa;
Al Virof0c3b502013-05-16 12:07:31 -04002152 struct map_files_info *p;
Pavel Emelyanov640708a2012-01-10 15:11:23 -08002153 int ret;
2154
Kent Overstreet94f8f3b2019-03-11 23:31:18 -07002155 genradix_init(&fa);
2156
Pavel Emelyanov640708a2012-01-10 15:11:23 -08002157 ret = -ENOENT;
Al Virof0c3b502013-05-16 12:07:31 -04002158 task = get_proc_task(file_inode(file));
Pavel Emelyanov640708a2012-01-10 15:11:23 -08002159 if (!task)
2160 goto out;
2161
2162 ret = -EACCES;
Jann Horncaaee622016-01-20 15:00:04 -08002163 if (!ptrace_may_access(task, PTRACE_MODE_READ_FSCREDS))
Pavel Emelyanov640708a2012-01-10 15:11:23 -08002164 goto out_put_task;
2165
2166 ret = 0;
Al Virof0c3b502013-05-16 12:07:31 -04002167 if (!dir_emit_dots(file, ctx))
2168 goto out_put_task;
Pavel Emelyanov640708a2012-01-10 15:11:23 -08002169
Al Virof0c3b502013-05-16 12:07:31 -04002170 mm = get_task_mm(task);
2171 if (!mm)
2172 goto out_put_task;
Konstantin Khlebnikovcd9e2bb2019-07-11 21:00:03 -07002173
2174 ret = down_read_killable(&mm->mmap_sem);
2175 if (ret) {
2176 mmput(mm);
2177 goto out_put_task;
2178 }
Pavel Emelyanov640708a2012-01-10 15:11:23 -08002179
Al Virof0c3b502013-05-16 12:07:31 -04002180 nr_files = 0;
Pavel Emelyanov640708a2012-01-10 15:11:23 -08002181
Al Virof0c3b502013-05-16 12:07:31 -04002182 /*
2183 * We need two passes here:
2184 *
2185 * 1) Collect vmas of mapped files with mmap_sem taken
2186 * 2) Release mmap_sem and instantiate entries
2187 *
2188 * otherwise we get lockdep complained, since filldir()
2189 * routine might require mmap_sem taken in might_fault().
2190 */
Pavel Emelyanov640708a2012-01-10 15:11:23 -08002191
Al Virof0c3b502013-05-16 12:07:31 -04002192 for (vma = mm->mmap, pos = 2; vma; vma = vma->vm_next) {
Kent Overstreet94f8f3b2019-03-11 23:31:18 -07002193 if (!vma->vm_file)
2194 continue;
2195 if (++pos <= ctx->pos)
2196 continue;
Al Virof0c3b502013-05-16 12:07:31 -04002197
Kent Overstreet94f8f3b2019-03-11 23:31:18 -07002198 p = genradix_ptr_alloc(&fa, nr_files++, GFP_KERNEL);
2199 if (!p) {
Al Virof0c3b502013-05-16 12:07:31 -04002200 ret = -ENOMEM;
Al Virof0c3b502013-05-16 12:07:31 -04002201 up_read(&mm->mmap_sem);
2202 mmput(mm);
2203 goto out_put_task;
2204 }
Al Virof0c3b502013-05-16 12:07:31 -04002205
Kent Overstreet94f8f3b2019-03-11 23:31:18 -07002206 p->start = vma->vm_start;
2207 p->end = vma->vm_end;
2208 p->mode = vma->vm_file->f_mode;
Pavel Emelyanov640708a2012-01-10 15:11:23 -08002209 }
Al Virof0c3b502013-05-16 12:07:31 -04002210 up_read(&mm->mmap_sem);
Alexey Dobriyanfe079a52018-04-10 16:32:05 -07002211 mmput(mm);
Al Virof0c3b502013-05-16 12:07:31 -04002212
2213 for (i = 0; i < nr_files; i++) {
Alexey Dobriyan20d28cd2018-02-06 15:37:06 -08002214 char buf[4 * sizeof(long) + 2]; /* max: %lx-%lx\0 */
2215 unsigned int len;
2216
Kent Overstreet94f8f3b2019-03-11 23:31:18 -07002217 p = genradix_ptr(&fa, i);
Alexey Dobriyan20d28cd2018-02-06 15:37:06 -08002218 len = snprintf(buf, sizeof(buf), "%lx-%lx", p->start, p->end);
Al Virof0c3b502013-05-16 12:07:31 -04002219 if (!proc_fill_cache(file, ctx,
Alexey Dobriyan20d28cd2018-02-06 15:37:06 -08002220 buf, len,
Al Virof0c3b502013-05-16 12:07:31 -04002221 proc_map_files_instantiate,
2222 task,
2223 (void *)(unsigned long)p->mode))
2224 break;
2225 ctx->pos++;
2226 }
Pavel Emelyanov640708a2012-01-10 15:11:23 -08002227
Pavel Emelyanov640708a2012-01-10 15:11:23 -08002228out_put_task:
2229 put_task_struct(task);
2230out:
Kent Overstreet94f8f3b2019-03-11 23:31:18 -07002231 genradix_free(&fa);
Pavel Emelyanov640708a2012-01-10 15:11:23 -08002232 return ret;
2233}
2234
2235static const struct file_operations proc_map_files_operations = {
2236 .read = generic_read_dir,
Al Virof50752e2016-04-20 17:13:54 -04002237 .iterate_shared = proc_map_files_readdir,
2238 .llseek = generic_file_llseek,
Pavel Emelyanov640708a2012-01-10 15:11:23 -08002239};
2240
Nicolas Pitreb18b6a92017-01-21 00:09:08 -05002241#if defined(CONFIG_CHECKPOINT_RESTORE) && defined(CONFIG_POSIX_TIMERS)
Pavel Emelyanov48f6a7a2013-03-11 13:12:45 +04002242struct timers_private {
2243 struct pid *pid;
2244 struct task_struct *task;
2245 struct sighand_struct *sighand;
Pavel Emelyanov57b80152013-03-11 13:13:08 +04002246 struct pid_namespace *ns;
Pavel Emelyanov48f6a7a2013-03-11 13:12:45 +04002247 unsigned long flags;
2248};
2249
2250static void *timers_start(struct seq_file *m, loff_t *pos)
2251{
2252 struct timers_private *tp = m->private;
2253
2254 tp->task = get_pid_task(tp->pid, PIDTYPE_PID);
2255 if (!tp->task)
2256 return ERR_PTR(-ESRCH);
2257
2258 tp->sighand = lock_task_sighand(tp->task, &tp->flags);
2259 if (!tp->sighand)
2260 return ERR_PTR(-ESRCH);
2261
2262 return seq_list_start(&tp->task->signal->posix_timers, *pos);
2263}
2264
2265static void *timers_next(struct seq_file *m, void *v, loff_t *pos)
2266{
2267 struct timers_private *tp = m->private;
2268 return seq_list_next(v, &tp->task->signal->posix_timers, pos);
2269}
2270
2271static void timers_stop(struct seq_file *m, void *v)
2272{
2273 struct timers_private *tp = m->private;
2274
2275 if (tp->sighand) {
2276 unlock_task_sighand(tp->task, &tp->flags);
2277 tp->sighand = NULL;
2278 }
2279
2280 if (tp->task) {
2281 put_task_struct(tp->task);
2282 tp->task = NULL;
2283 }
2284}
2285
2286static int show_timer(struct seq_file *m, void *v)
2287{
2288 struct k_itimer *timer;
Pavel Emelyanov57b80152013-03-11 13:13:08 +04002289 struct timers_private *tp = m->private;
2290 int notify;
Alexey Dobriyancedbcca2014-08-08 14:21:33 -07002291 static const char * const nstr[] = {
Pavel Emelyanov57b80152013-03-11 13:13:08 +04002292 [SIGEV_SIGNAL] = "signal",
2293 [SIGEV_NONE] = "none",
2294 [SIGEV_THREAD] = "thread",
2295 };
Pavel Emelyanov48f6a7a2013-03-11 13:12:45 +04002296
2297 timer = list_entry((struct list_head *)v, struct k_itimer, list);
Pavel Emelyanov57b80152013-03-11 13:13:08 +04002298 notify = timer->it_sigev_notify;
2299
Pavel Emelyanov48f6a7a2013-03-11 13:12:45 +04002300 seq_printf(m, "ID: %d\n", timer->it_id);
Linus Torvaldsba3edf1f2017-12-06 18:23:27 -08002301 seq_printf(m, "signal: %d/%px\n",
Joe Perches25ce3192015-04-15 16:18:17 -07002302 timer->sigq->info.si_signo,
2303 timer->sigq->info.si_value.sival_ptr);
Pavel Emelyanov57b80152013-03-11 13:13:08 +04002304 seq_printf(m, "notify: %s/%s.%d\n",
Joe Perches25ce3192015-04-15 16:18:17 -07002305 nstr[notify & ~SIGEV_THREAD_ID],
2306 (notify & SIGEV_THREAD_ID) ? "tid" : "pid",
2307 pid_nr_ns(timer->it_pid, tp->ns));
Pavel Tikhomirov15ef0292013-05-17 02:12:03 +04002308 seq_printf(m, "ClockID: %d\n", timer->it_clock);
Pavel Emelyanov48f6a7a2013-03-11 13:12:45 +04002309
2310 return 0;
2311}
2312
2313static const struct seq_operations proc_timers_seq_ops = {
2314 .start = timers_start,
2315 .next = timers_next,
2316 .stop = timers_stop,
2317 .show = show_timer,
2318};
2319
2320static int proc_timers_open(struct inode *inode, struct file *file)
2321{
2322 struct timers_private *tp;
2323
2324 tp = __seq_open_private(file, &proc_timers_seq_ops,
2325 sizeof(struct timers_private));
2326 if (!tp)
2327 return -ENOMEM;
2328
2329 tp->pid = proc_pid(inode);
Christoph Hellwig76f668b2018-05-16 07:19:01 +02002330 tp->ns = proc_pid_ns(inode);
Pavel Emelyanov48f6a7a2013-03-11 13:12:45 +04002331 return 0;
2332}
2333
2334static const struct file_operations proc_timers_operations = {
2335 .open = proc_timers_open,
2336 .read = seq_read,
2337 .llseek = seq_lseek,
2338 .release = seq_release_private,
2339};
Eric Engestromb5946be2016-03-17 14:20:57 -07002340#endif
Pavel Emelyanov640708a2012-01-10 15:11:23 -08002341
John Stultz5de23d42016-03-17 14:20:54 -07002342static ssize_t timerslack_ns_write(struct file *file, const char __user *buf,
2343 size_t count, loff_t *offset)
2344{
2345 struct inode *inode = file_inode(file);
2346 struct task_struct *p;
2347 u64 slack_ns;
2348 int err;
2349
2350 err = kstrtoull_from_user(buf, count, 10, &slack_ns);
2351 if (err < 0)
2352 return err;
2353
2354 p = get_proc_task(inode);
2355 if (!p)
2356 return -ESRCH;
2357
John Stultz4b2bd5f2016-10-07 17:02:33 -07002358 if (p != current) {
Benjamin Gordon8da0b4f2019-01-03 15:25:56 -08002359 rcu_read_lock();
2360 if (!ns_capable(__task_cred(p)->user_ns, CAP_SYS_NICE)) {
2361 rcu_read_unlock();
John Stultz4b2bd5f2016-10-07 17:02:33 -07002362 count = -EPERM;
2363 goto out;
2364 }
Benjamin Gordon8da0b4f2019-01-03 15:25:56 -08002365 rcu_read_unlock();
John Stultz5de23d42016-03-17 14:20:54 -07002366
John Stultz4b2bd5f2016-10-07 17:02:33 -07002367 err = security_task_setscheduler(p);
2368 if (err) {
2369 count = err;
2370 goto out;
2371 }
John Stultz904763e2016-10-07 17:02:29 -07002372 }
2373
John Stultz7abbaf92016-10-07 17:02:26 -07002374 task_lock(p);
2375 if (slack_ns == 0)
2376 p->timer_slack_ns = p->default_timer_slack_ns;
2377 else
2378 p->timer_slack_ns = slack_ns;
2379 task_unlock(p);
2380
2381out:
John Stultz5de23d42016-03-17 14:20:54 -07002382 put_task_struct(p);
2383
2384 return count;
2385}
2386
2387static int timerslack_ns_show(struct seq_file *m, void *v)
2388{
2389 struct inode *inode = m->private;
2390 struct task_struct *p;
John Stultz7abbaf92016-10-07 17:02:26 -07002391 int err = 0;
John Stultz5de23d42016-03-17 14:20:54 -07002392
2393 p = get_proc_task(inode);
2394 if (!p)
2395 return -ESRCH;
2396
John Stultz4b2bd5f2016-10-07 17:02:33 -07002397 if (p != current) {
Benjamin Gordon8da0b4f2019-01-03 15:25:56 -08002398 rcu_read_lock();
2399 if (!ns_capable(__task_cred(p)->user_ns, CAP_SYS_NICE)) {
2400 rcu_read_unlock();
John Stultz4b2bd5f2016-10-07 17:02:33 -07002401 err = -EPERM;
2402 goto out;
2403 }
Benjamin Gordon8da0b4f2019-01-03 15:25:56 -08002404 rcu_read_unlock();
2405
John Stultz4b2bd5f2016-10-07 17:02:33 -07002406 err = security_task_getscheduler(p);
2407 if (err)
2408 goto out;
2409 }
John Stultz904763e2016-10-07 17:02:29 -07002410
John Stultz7abbaf92016-10-07 17:02:26 -07002411 task_lock(p);
2412 seq_printf(m, "%llu\n", p->timer_slack_ns);
2413 task_unlock(p);
2414
2415out:
John Stultz5de23d42016-03-17 14:20:54 -07002416 put_task_struct(p);
2417
2418 return err;
2419}
2420
2421static int timerslack_ns_open(struct inode *inode, struct file *filp)
2422{
2423 return single_open(filp, timerslack_ns_show, inode);
2424}
2425
2426static const struct file_operations proc_pid_set_timerslack_ns_operations = {
2427 .open = timerslack_ns_open,
2428 .read = seq_read,
2429 .write = timerslack_ns_write,
2430 .llseek = seq_lseek,
2431 .release = single_release,
2432};
2433
Al Viro0168b9e2018-05-03 09:21:05 -04002434static struct dentry *proc_pident_instantiate(struct dentry *dentry,
2435 struct task_struct *task, const void *ptr)
Eric W. Biederman444ceed2006-10-02 02:18:49 -07002436{
Eric Dumazetc5141e62007-05-08 00:26:15 -07002437 const struct pid_entry *p = ptr;
Eric W. Biederman444ceed2006-10-02 02:18:49 -07002438 struct inode *inode;
2439 struct proc_inode *ei;
Eric W. Biederman444ceed2006-10-02 02:18:49 -07002440
Al Viro0168b9e2018-05-03 09:21:05 -04002441 inode = proc_pid_make_inode(dentry->d_sb, task, p->mode);
Eric W. Biederman444ceed2006-10-02 02:18:49 -07002442 if (!inode)
Al Viro0168b9e2018-05-03 09:21:05 -04002443 return ERR_PTR(-ENOENT);
Eric W. Biederman444ceed2006-10-02 02:18:49 -07002444
2445 ei = PROC_I(inode);
Eric W. Biederman444ceed2006-10-02 02:18:49 -07002446 if (S_ISDIR(inode->i_mode))
Miklos Szeredibfe86842011-10-28 14:13:29 +02002447 set_nlink(inode, 2); /* Use getattr to fix if necessary */
Eric W. Biederman444ceed2006-10-02 02:18:49 -07002448 if (p->iop)
2449 inode->i_op = p->iop;
2450 if (p->fop)
2451 inode->i_fop = p->fop;
2452 ei->op = p->op;
Al Viro1bbc5512018-05-02 21:26:16 -04002453 pid_update_inode(task, inode);
Nick Pigginfb045ad2011-01-07 17:49:55 +11002454 d_set_d_op(dentry, &pid_dentry_operations);
Al Viro0168b9e2018-05-03 09:21:05 -04002455 return d_splice_alias(inode, dentry);
Eric W. Biederman444ceed2006-10-02 02:18:49 -07002456}
2457
Linus Torvalds1da177e2005-04-16 15:20:36 -07002458static struct dentry *proc_pident_lookup(struct inode *dir,
2459 struct dentry *dentry,
Alexey Dobriyand5a572a2019-03-11 23:28:51 -07002460 const struct pid_entry *p,
2461 const struct pid_entry *end)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002462{
Eric W. Biederman99f89552006-06-26 00:25:55 -07002463 struct task_struct *task = get_proc_task(dir);
Al Viro0168b9e2018-05-03 09:21:05 -04002464 struct dentry *res = ERR_PTR(-ENOENT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002465
Eric W. Biederman99f89552006-06-26 00:25:55 -07002466 if (!task)
2467 goto out_no_task;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002468
Eric W. Biederman20cdc892006-10-02 02:17:07 -07002469 /*
2470 * Yes, it does not scale. And it should not. Don't add
2471 * new entries into /proc/<tgid>/ without very good reasons.
2472 */
Alexey Dobriyand5a572a2019-03-11 23:28:51 -07002473 for (; p < end; p++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002474 if (p->len != dentry->d_name.len)
2475 continue;
Alexey Dobriyan26b95132018-06-14 15:27:17 -07002476 if (!memcmp(dentry->d_name.name, p->name, p->len)) {
2477 res = proc_pident_instantiate(dentry, task, p);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002478 break;
Alexey Dobriyan26b95132018-06-14 15:27:17 -07002479 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002480 }
Eric W. Biederman99f89552006-06-26 00:25:55 -07002481 put_task_struct(task);
2482out_no_task:
Al Viro0168b9e2018-05-03 09:21:05 -04002483 return res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002484}
2485
Al Virof0c3b502013-05-16 12:07:31 -04002486static int proc_pident_readdir(struct file *file, struct dir_context *ctx,
Eric Dumazetc5141e62007-05-08 00:26:15 -07002487 const struct pid_entry *ents, unsigned int nents)
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002488{
Al Virof0c3b502013-05-16 12:07:31 -04002489 struct task_struct *task = get_proc_task(file_inode(file));
2490 const struct pid_entry *p;
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002491
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002492 if (!task)
Al Virof0c3b502013-05-16 12:07:31 -04002493 return -ENOENT;
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002494
Al Virof0c3b502013-05-16 12:07:31 -04002495 if (!dir_emit_dots(file, ctx))
2496 goto out;
2497
2498 if (ctx->pos >= nents + 2)
2499 goto out;
2500
Alexey Dobriyanbac5f5d2016-12-12 16:45:28 -08002501 for (p = ents + (ctx->pos - 2); p < ents + nents; p++) {
Al Virof0c3b502013-05-16 12:07:31 -04002502 if (!proc_fill_cache(file, ctx, p->name, p->len,
2503 proc_pident_instantiate, task, p))
2504 break;
2505 ctx->pos++;
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002506 }
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002507out:
Eric W. Biederman61a28782006-10-02 02:18:49 -07002508 put_task_struct(task);
Al Virof0c3b502013-05-16 12:07:31 -04002509 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002510}
2511
Linus Torvalds1da177e2005-04-16 15:20:36 -07002512#ifdef CONFIG_SECURITY
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002513static ssize_t proc_pid_attr_read(struct file * file, char __user * buf,
2514 size_t count, loff_t *ppos)
2515{
Al Viro496ad9a2013-01-23 17:07:38 -05002516 struct inode * inode = file_inode(file);
Al Viro04ff9702007-03-12 16:17:58 +00002517 char *p = NULL;
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002518 ssize_t length;
2519 struct task_struct *task = get_proc_task(inode);
2520
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002521 if (!task)
Al Viro04ff9702007-03-12 16:17:58 +00002522 return -ESRCH;
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002523
Casey Schaufler6d9c9392018-09-21 17:16:59 -07002524 length = security_getprocattr(task, PROC_I(inode)->op.lsm,
Josef "Jeff" Sipek2fddfee2006-12-08 02:36:36 -08002525 (char*)file->f_path.dentry->d_name.name,
Al Viro04ff9702007-03-12 16:17:58 +00002526 &p);
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002527 put_task_struct(task);
Al Viro04ff9702007-03-12 16:17:58 +00002528 if (length > 0)
2529 length = simple_read_from_buffer(buf, count, ppos, p, length);
2530 kfree(p);
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002531 return length;
2532}
2533
2534static ssize_t proc_pid_attr_write(struct file * file, const char __user * buf,
2535 size_t count, loff_t *ppos)
2536{
Al Viro496ad9a2013-01-23 17:07:38 -05002537 struct inode * inode = file_inode(file);
Alexey Dobriyan41089b62018-08-21 21:54:30 -07002538 struct task_struct *task;
Al Virobb646cd2015-12-24 00:16:30 -05002539 void *page;
Alexey Dobriyan41089b62018-08-21 21:54:30 -07002540 int rv;
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002541
Alexey Dobriyan41089b62018-08-21 21:54:30 -07002542 rcu_read_lock();
2543 task = pid_task(proc_pid(inode), PIDTYPE_PID);
2544 if (!task) {
2545 rcu_read_unlock();
2546 return -ESRCH;
2547 }
Stephen Smalleyb21507e2017-01-09 10:07:31 -05002548 /* A task may only write its own attributes. */
Alexey Dobriyan41089b62018-08-21 21:54:30 -07002549 if (current != task) {
2550 rcu_read_unlock();
2551 return -EACCES;
2552 }
Paul Moore35a196b2019-04-19 14:55:12 -04002553 /* Prevent changes to overridden credentials. */
2554 if (current_cred() != current_real_cred()) {
2555 rcu_read_unlock();
2556 return -EBUSY;
2557 }
Alexey Dobriyan41089b62018-08-21 21:54:30 -07002558 rcu_read_unlock();
Stephen Smalleyb21507e2017-01-09 10:07:31 -05002559
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002560 if (count > PAGE_SIZE)
2561 count = PAGE_SIZE;
2562
2563 /* No partial writes. */
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002564 if (*ppos != 0)
Alexey Dobriyan41089b62018-08-21 21:54:30 -07002565 return -EINVAL;
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002566
Al Virobb646cd2015-12-24 00:16:30 -05002567 page = memdup_user(buf, count);
2568 if (IS_ERR(page)) {
Alexey Dobriyan41089b62018-08-21 21:54:30 -07002569 rv = PTR_ERR(page);
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002570 goto out;
Al Virobb646cd2015-12-24 00:16:30 -05002571 }
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002572
David Howells107db7c2009-05-08 13:55:27 +01002573 /* Guard against adverse ptrace interaction */
Alexey Dobriyan41089b62018-08-21 21:54:30 -07002574 rv = mutex_lock_interruptible(&current->signal->cred_guard_mutex);
2575 if (rv < 0)
David Howells107db7c2009-05-08 13:55:27 +01002576 goto out_free;
2577
Casey Schaufler6d9c9392018-09-21 17:16:59 -07002578 rv = security_setprocattr(PROC_I(inode)->op.lsm,
2579 file->f_path.dentry->d_name.name, page,
2580 count);
Stephen Smalleyb21507e2017-01-09 10:07:31 -05002581 mutex_unlock(&current->signal->cred_guard_mutex);
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002582out_free:
Al Virobb646cd2015-12-24 00:16:30 -05002583 kfree(page);
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002584out:
Alexey Dobriyan41089b62018-08-21 21:54:30 -07002585 return rv;
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002586}
2587
Arjan van de Ven00977a52007-02-12 00:55:34 -08002588static const struct file_operations proc_pid_attr_operations = {
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002589 .read = proc_pid_attr_read,
2590 .write = proc_pid_attr_write,
Arnd Bergmann87df8422010-03-17 23:06:02 +01002591 .llseek = generic_file_llseek,
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002592};
2593
Casey Schaufler6d9c9392018-09-21 17:16:59 -07002594#define LSM_DIR_OPS(LSM) \
2595static int proc_##LSM##_attr_dir_iterate(struct file *filp, \
2596 struct dir_context *ctx) \
2597{ \
2598 return proc_pident_readdir(filp, ctx, \
2599 LSM##_attr_dir_stuff, \
2600 ARRAY_SIZE(LSM##_attr_dir_stuff)); \
2601} \
2602\
2603static const struct file_operations proc_##LSM##_attr_dir_ops = { \
2604 .read = generic_read_dir, \
2605 .iterate = proc_##LSM##_attr_dir_iterate, \
2606 .llseek = default_llseek, \
2607}; \
2608\
2609static struct dentry *proc_##LSM##_attr_dir_lookup(struct inode *dir, \
2610 struct dentry *dentry, unsigned int flags) \
2611{ \
2612 return proc_pident_lookup(dir, dentry, \
2613 LSM##_attr_dir_stuff, \
Alexey Dobriyand5a572a2019-03-11 23:28:51 -07002614 LSM##_attr_dir_stuff + ARRAY_SIZE(LSM##_attr_dir_stuff)); \
Casey Schaufler6d9c9392018-09-21 17:16:59 -07002615} \
2616\
2617static const struct inode_operations proc_##LSM##_attr_dir_inode_ops = { \
2618 .lookup = proc_##LSM##_attr_dir_lookup, \
2619 .getattr = pid_getattr, \
2620 .setattr = proc_setattr, \
2621}
2622
2623#ifdef CONFIG_SECURITY_SMACK
2624static const struct pid_entry smack_attr_dir_stuff[] = {
2625 ATTR("smack", "current", 0666),
2626};
2627LSM_DIR_OPS(smack);
2628#endif
2629
Eric Dumazetc5141e62007-05-08 00:26:15 -07002630static const struct pid_entry attr_dir_stuff[] = {
Casey Schaufler6d9c9392018-09-21 17:16:59 -07002631 ATTR(NULL, "current", 0666),
2632 ATTR(NULL, "prev", 0444),
2633 ATTR(NULL, "exec", 0666),
2634 ATTR(NULL, "fscreate", 0666),
2635 ATTR(NULL, "keycreate", 0666),
2636 ATTR(NULL, "sockcreate", 0666),
2637#ifdef CONFIG_SECURITY_SMACK
2638 DIR("smack", 0555,
2639 proc_smack_attr_dir_inode_ops, proc_smack_attr_dir_ops),
2640#endif
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002641};
2642
Al Virof0c3b502013-05-16 12:07:31 -04002643static int proc_attr_dir_readdir(struct file *file, struct dir_context *ctx)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002644{
Al Virof0c3b502013-05-16 12:07:31 -04002645 return proc_pident_readdir(file, ctx,
2646 attr_dir_stuff, ARRAY_SIZE(attr_dir_stuff));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002647}
2648
Arjan van de Ven00977a52007-02-12 00:55:34 -08002649static const struct file_operations proc_attr_dir_operations = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002650 .read = generic_read_dir,
Al Virof50752e2016-04-20 17:13:54 -04002651 .iterate_shared = proc_attr_dir_readdir,
2652 .llseek = generic_file_llseek,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002653};
2654
Eric W. Biederman72d9dcf2006-10-02 02:18:50 -07002655static struct dentry *proc_attr_dir_lookup(struct inode *dir,
Al Viro00cd8dd2012-06-10 17:13:09 -04002656 struct dentry *dentry, unsigned int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002657{
Eric W. Biederman7bcd6b02006-10-02 02:18:56 -07002658 return proc_pident_lookup(dir, dentry,
Alexey Dobriyand5a572a2019-03-11 23:28:51 -07002659 attr_dir_stuff,
2660 attr_dir_stuff + ARRAY_SIZE(attr_dir_stuff));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002661}
2662
Arjan van de Venc5ef1c42007-02-12 00:55:40 -08002663static const struct inode_operations proc_attr_dir_inode_operations = {
Eric W. Biederman72d9dcf2006-10-02 02:18:50 -07002664 .lookup = proc_attr_dir_lookup,
Eric W. Biederman99f89552006-06-26 00:25:55 -07002665 .getattr = pid_getattr,
Linus Torvalds6d76fa52006-07-15 12:26:45 -07002666 .setattr = proc_setattr,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002667};
2668
Linus Torvalds1da177e2005-04-16 15:20:36 -07002669#endif
2670
Christoph Hellwig698ba7b2009-12-15 16:47:37 -08002671#ifdef CONFIG_ELF_CORE
Kawai, Hidehiro3cb4a0b2007-07-19 01:48:28 -07002672static ssize_t proc_coredump_filter_read(struct file *file, char __user *buf,
2673 size_t count, loff_t *ppos)
2674{
Al Viro496ad9a2013-01-23 17:07:38 -05002675 struct task_struct *task = get_proc_task(file_inode(file));
Kawai, Hidehiro3cb4a0b2007-07-19 01:48:28 -07002676 struct mm_struct *mm;
2677 char buffer[PROC_NUMBUF];
2678 size_t len;
2679 int ret;
2680
2681 if (!task)
2682 return -ESRCH;
2683
2684 ret = 0;
2685 mm = get_task_mm(task);
2686 if (mm) {
2687 len = snprintf(buffer, sizeof(buffer), "%08lx\n",
2688 ((mm->flags & MMF_DUMP_FILTER_MASK) >>
2689 MMF_DUMP_FILTER_SHIFT));
2690 mmput(mm);
2691 ret = simple_read_from_buffer(buf, count, ppos, buffer, len);
2692 }
2693
2694 put_task_struct(task);
2695
2696 return ret;
2697}
2698
2699static ssize_t proc_coredump_filter_write(struct file *file,
2700 const char __user *buf,
2701 size_t count,
2702 loff_t *ppos)
2703{
2704 struct task_struct *task;
2705 struct mm_struct *mm;
Kawai, Hidehiro3cb4a0b2007-07-19 01:48:28 -07002706 unsigned int val;
2707 int ret;
2708 int i;
2709 unsigned long mask;
2710
Alexey Dobriyan774636e2015-09-09 15:36:59 -07002711 ret = kstrtouint_from_user(buf, count, 0, &val);
2712 if (ret < 0)
2713 return ret;
Kawai, Hidehiro3cb4a0b2007-07-19 01:48:28 -07002714
2715 ret = -ESRCH;
Al Viro496ad9a2013-01-23 17:07:38 -05002716 task = get_proc_task(file_inode(file));
Kawai, Hidehiro3cb4a0b2007-07-19 01:48:28 -07002717 if (!task)
2718 goto out_no_task;
2719
Kawai, Hidehiro3cb4a0b2007-07-19 01:48:28 -07002720 mm = get_task_mm(task);
2721 if (!mm)
2722 goto out_no_mm;
Colin Ian King41a0c2492015-12-18 14:22:01 -08002723 ret = 0;
Kawai, Hidehiro3cb4a0b2007-07-19 01:48:28 -07002724
2725 for (i = 0, mask = 1; i < MMF_DUMP_FILTER_BITS; i++, mask <<= 1) {
2726 if (val & mask)
2727 set_bit(i + MMF_DUMP_FILTER_SHIFT, &mm->flags);
2728 else
2729 clear_bit(i + MMF_DUMP_FILTER_SHIFT, &mm->flags);
2730 }
2731
2732 mmput(mm);
2733 out_no_mm:
2734 put_task_struct(task);
2735 out_no_task:
Alexey Dobriyan774636e2015-09-09 15:36:59 -07002736 if (ret < 0)
2737 return ret;
2738 return count;
Kawai, Hidehiro3cb4a0b2007-07-19 01:48:28 -07002739}
2740
2741static const struct file_operations proc_coredump_filter_operations = {
2742 .read = proc_coredump_filter_read,
2743 .write = proc_coredump_filter_write,
Arnd Bergmann87df8422010-03-17 23:06:02 +01002744 .llseek = generic_file_llseek,
Kawai, Hidehiro3cb4a0b2007-07-19 01:48:28 -07002745};
2746#endif
2747
Andrew Mortonaba76fd2006-12-10 02:19:48 -08002748#ifdef CONFIG_TASK_IO_ACCOUNTING
Alexey Dobriyan19aadc92014-08-08 14:21:50 -07002749static int do_io_accounting(struct task_struct *task, struct seq_file *m, int whole)
Andrew Mortonaba76fd2006-12-10 02:19:48 -08002750{
Andrea Righi940389b2008-07-28 00:48:12 +02002751 struct task_io_accounting acct = task->ioac;
Andrea Righi59954772008-07-27 17:29:15 +02002752 unsigned long flags;
Vasiliy Kulikov293eb1e2011-07-26 16:08:38 -07002753 int result;
Andrea Righi297c5d92008-07-25 01:48:49 -07002754
Vasiliy Kulikov293eb1e2011-07-26 16:08:38 -07002755 result = mutex_lock_killable(&task->signal->cred_guard_mutex);
2756 if (result)
2757 return result;
2758
Jann Horncaaee622016-01-20 15:00:04 -08002759 if (!ptrace_may_access(task, PTRACE_MODE_READ_FSCREDS)) {
Vasiliy Kulikov293eb1e2011-07-26 16:08:38 -07002760 result = -EACCES;
2761 goto out_unlock;
2762 }
Vasiliy Kulikov1d1221f2011-06-24 16:08:38 +04002763
Andrea Righi59954772008-07-27 17:29:15 +02002764 if (whole && lock_task_sighand(task, &flags)) {
2765 struct task_struct *t = task;
Andrea Righib2d002d2008-07-26 15:22:27 -07002766
Andrea Righi59954772008-07-27 17:29:15 +02002767 task_io_accounting_add(&acct, &task->signal->ioac);
2768 while_each_thread(task, t)
2769 task_io_accounting_add(&acct, &t->ioac);
Andrea Righi297c5d92008-07-25 01:48:49 -07002770
Andrea Righi59954772008-07-27 17:29:15 +02002771 unlock_task_sighand(task, &flags);
Andrea Righi297c5d92008-07-25 01:48:49 -07002772 }
Joe Perches25ce3192015-04-15 16:18:17 -07002773 seq_printf(m,
2774 "rchar: %llu\n"
2775 "wchar: %llu\n"
2776 "syscr: %llu\n"
2777 "syscw: %llu\n"
2778 "read_bytes: %llu\n"
2779 "write_bytes: %llu\n"
2780 "cancelled_write_bytes: %llu\n",
2781 (unsigned long long)acct.rchar,
2782 (unsigned long long)acct.wchar,
2783 (unsigned long long)acct.syscr,
2784 (unsigned long long)acct.syscw,
2785 (unsigned long long)acct.read_bytes,
2786 (unsigned long long)acct.write_bytes,
2787 (unsigned long long)acct.cancelled_write_bytes);
2788 result = 0;
2789
Vasiliy Kulikov293eb1e2011-07-26 16:08:38 -07002790out_unlock:
2791 mutex_unlock(&task->signal->cred_guard_mutex);
2792 return result;
Andrew Mortonaba76fd2006-12-10 02:19:48 -08002793}
Andrea Righi297c5d92008-07-25 01:48:49 -07002794
Alexey Dobriyan19aadc92014-08-08 14:21:50 -07002795static int proc_tid_io_accounting(struct seq_file *m, struct pid_namespace *ns,
2796 struct pid *pid, struct task_struct *task)
Andrea Righi297c5d92008-07-25 01:48:49 -07002797{
Alexey Dobriyan19aadc92014-08-08 14:21:50 -07002798 return do_io_accounting(task, m, 0);
Andrea Righi297c5d92008-07-25 01:48:49 -07002799}
2800
Alexey Dobriyan19aadc92014-08-08 14:21:50 -07002801static int proc_tgid_io_accounting(struct seq_file *m, struct pid_namespace *ns,
2802 struct pid *pid, struct task_struct *task)
Andrea Righi297c5d92008-07-25 01:48:49 -07002803{
Alexey Dobriyan19aadc92014-08-08 14:21:50 -07002804 return do_io_accounting(task, m, 1);
Andrea Righi297c5d92008-07-25 01:48:49 -07002805}
2806#endif /* CONFIG_TASK_IO_ACCOUNTING */
Andrew Mortonaba76fd2006-12-10 02:19:48 -08002807
Eric W. Biederman22d917d2011-11-17 00:11:58 -08002808#ifdef CONFIG_USER_NS
2809static int proc_id_map_open(struct inode *inode, struct file *file,
Fabian Frederickccf94f12014-08-08 14:21:22 -07002810 const struct seq_operations *seq_ops)
Eric W. Biederman22d917d2011-11-17 00:11:58 -08002811{
2812 struct user_namespace *ns = NULL;
2813 struct task_struct *task;
2814 struct seq_file *seq;
2815 int ret = -EINVAL;
2816
2817 task = get_proc_task(inode);
2818 if (task) {
2819 rcu_read_lock();
2820 ns = get_user_ns(task_cred_xxx(task, user_ns));
2821 rcu_read_unlock();
2822 put_task_struct(task);
2823 }
2824 if (!ns)
2825 goto err;
2826
2827 ret = seq_open(file, seq_ops);
2828 if (ret)
2829 goto err_put_ns;
2830
2831 seq = file->private_data;
2832 seq->private = ns;
2833
2834 return 0;
2835err_put_ns:
2836 put_user_ns(ns);
2837err:
2838 return ret;
2839}
2840
2841static int proc_id_map_release(struct inode *inode, struct file *file)
2842{
2843 struct seq_file *seq = file->private_data;
2844 struct user_namespace *ns = seq->private;
2845 put_user_ns(ns);
2846 return seq_release(inode, file);
2847}
2848
2849static int proc_uid_map_open(struct inode *inode, struct file *file)
2850{
2851 return proc_id_map_open(inode, file, &proc_uid_seq_operations);
2852}
2853
2854static int proc_gid_map_open(struct inode *inode, struct file *file)
2855{
2856 return proc_id_map_open(inode, file, &proc_gid_seq_operations);
2857}
2858
Eric W. Biedermanf76d2072012-08-30 01:24:05 -07002859static int proc_projid_map_open(struct inode *inode, struct file *file)
2860{
2861 return proc_id_map_open(inode, file, &proc_projid_seq_operations);
2862}
2863
Eric W. Biederman22d917d2011-11-17 00:11:58 -08002864static const struct file_operations proc_uid_map_operations = {
2865 .open = proc_uid_map_open,
2866 .write = proc_uid_map_write,
2867 .read = seq_read,
2868 .llseek = seq_lseek,
2869 .release = proc_id_map_release,
2870};
2871
2872static const struct file_operations proc_gid_map_operations = {
2873 .open = proc_gid_map_open,
2874 .write = proc_gid_map_write,
2875 .read = seq_read,
2876 .llseek = seq_lseek,
2877 .release = proc_id_map_release,
2878};
Eric W. Biedermanf76d2072012-08-30 01:24:05 -07002879
2880static const struct file_operations proc_projid_map_operations = {
2881 .open = proc_projid_map_open,
2882 .write = proc_projid_map_write,
2883 .read = seq_read,
2884 .llseek = seq_lseek,
2885 .release = proc_id_map_release,
2886};
Eric W. Biederman9cc46512014-12-02 12:27:26 -06002887
2888static int proc_setgroups_open(struct inode *inode, struct file *file)
2889{
2890 struct user_namespace *ns = NULL;
2891 struct task_struct *task;
2892 int ret;
2893
2894 ret = -ESRCH;
2895 task = get_proc_task(inode);
2896 if (task) {
2897 rcu_read_lock();
2898 ns = get_user_ns(task_cred_xxx(task, user_ns));
2899 rcu_read_unlock();
2900 put_task_struct(task);
2901 }
2902 if (!ns)
2903 goto err;
2904
2905 if (file->f_mode & FMODE_WRITE) {
2906 ret = -EACCES;
2907 if (!ns_capable(ns, CAP_SYS_ADMIN))
2908 goto err_put_ns;
2909 }
2910
2911 ret = single_open(file, &proc_setgroups_show, ns);
2912 if (ret)
2913 goto err_put_ns;
2914
2915 return 0;
2916err_put_ns:
2917 put_user_ns(ns);
2918err:
2919 return ret;
2920}
2921
2922static int proc_setgroups_release(struct inode *inode, struct file *file)
2923{
2924 struct seq_file *seq = file->private_data;
2925 struct user_namespace *ns = seq->private;
2926 int ret = single_release(inode, file);
2927 put_user_ns(ns);
2928 return ret;
2929}
2930
2931static const struct file_operations proc_setgroups_operations = {
2932 .open = proc_setgroups_open,
2933 .write = proc_setgroups_write,
2934 .read = seq_read,
2935 .llseek = seq_lseek,
2936 .release = proc_setgroups_release,
2937};
Eric W. Biederman22d917d2011-11-17 00:11:58 -08002938#endif /* CONFIG_USER_NS */
2939
Kees Cook47830722008-10-06 03:11:58 +04002940static int proc_pid_personality(struct seq_file *m, struct pid_namespace *ns,
2941 struct pid *pid, struct task_struct *task)
2942{
Al Viroa9712bc2011-03-23 15:52:50 -04002943 int err = lock_trace(task);
2944 if (!err) {
2945 seq_printf(m, "%08x\n", task->personality);
2946 unlock_trace(task);
2947 }
2948 return err;
Kees Cook47830722008-10-06 03:11:58 +04002949}
2950
Josh Poimboeuf7c23b332017-02-13 19:42:41 -06002951#ifdef CONFIG_LIVEPATCH
2952static int proc_pid_patch_state(struct seq_file *m, struct pid_namespace *ns,
2953 struct pid *pid, struct task_struct *task)
2954{
2955 seq_printf(m, "%d\n", task->patch_state);
2956 return 0;
2957}
2958#endif /* CONFIG_LIVEPATCH */
2959
Alexander Popovc8d12622018-08-17 01:17:01 +03002960#ifdef CONFIG_STACKLEAK_METRICS
2961static int proc_stack_depth(struct seq_file *m, struct pid_namespace *ns,
2962 struct pid *pid, struct task_struct *task)
2963{
2964 unsigned long prev_depth = THREAD_SIZE -
2965 (task->prev_lowest_stack & (THREAD_SIZE - 1));
2966 unsigned long depth = THREAD_SIZE -
2967 (task->lowest_stack & (THREAD_SIZE - 1));
2968
2969 seq_printf(m, "previous stack depth: %lu\nstack depth: %lu\n",
2970 prev_depth, depth);
2971 return 0;
2972}
2973#endif /* CONFIG_STACKLEAK_METRICS */
2974
Eric W. Biederman801199c2006-10-02 02:18:48 -07002975/*
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002976 * Thread groups
2977 */
Arjan van de Ven00977a52007-02-12 00:55:34 -08002978static const struct file_operations proc_task_operations;
Arjan van de Venc5ef1c42007-02-12 00:55:40 -08002979static const struct inode_operations proc_task_inode_operations;
Eric W. Biederman20cdc892006-10-02 02:17:07 -07002980
Eric Dumazetc5141e62007-05-08 00:26:15 -07002981static const struct pid_entry tgid_base_stuff[] = {
Alexey Dobriyan631f9c12008-11-10 01:32:52 +03002982 DIR("task", S_IRUGO|S_IXUGO, proc_task_inode_operations, proc_task_operations),
2983 DIR("fd", S_IRUSR|S_IXUSR, proc_fd_inode_operations, proc_fd_operations),
Pavel Emelyanov640708a2012-01-10 15:11:23 -08002984 DIR("map_files", S_IRUSR|S_IXUSR, proc_map_files_inode_operations, proc_map_files_operations),
Alexey Dobriyan631f9c12008-11-10 01:32:52 +03002985 DIR("fdinfo", S_IRUSR|S_IXUSR, proc_fdinfo_inode_operations, proc_fdinfo_operations),
Eric W. Biederman6b4e3062010-03-07 16:41:34 -08002986 DIR("ns", S_IRUSR|S_IXUGO, proc_ns_dir_inode_operations, proc_ns_dir_operations),
Andrew Mortonb2211a32008-03-11 18:03:35 -07002987#ifdef CONFIG_NET
Alexey Dobriyan631f9c12008-11-10 01:32:52 +03002988 DIR("net", S_IRUGO|S_IXUGO, proc_net_inode_operations, proc_net_operations),
Andrew Mortonb2211a32008-03-11 18:03:35 -07002989#endif
Alexey Dobriyan631f9c12008-11-10 01:32:52 +03002990 REG("environ", S_IRUSR, proc_environ_operations),
Al Viroc5317162016-10-05 18:43:43 -04002991 REG("auxv", S_IRUSR, proc_auxv_operations),
Alexey Dobriyan631f9c12008-11-10 01:32:52 +03002992 ONE("status", S_IRUGO, proc_pid_status),
Djalal Harouni35a35042014-04-07 15:38:36 -07002993 ONE("personality", S_IRUSR, proc_pid_personality),
Alexey Dobriyan1c963eb2014-08-08 14:21:37 -07002994 ONE("limits", S_IRUGO, proc_pid_limits),
Ingo Molnar43ae34c2007-07-09 18:52:00 +02002995#ifdef CONFIG_SCHED_DEBUG
Alexey Dobriyan631f9c12008-11-10 01:32:52 +03002996 REG("sched", S_IRUGO|S_IWUSR, proc_pid_sched_operations),
Ingo Molnar43ae34c2007-07-09 18:52:00 +02002997#endif
Mike Galbraith5091faa2010-11-30 14:18:03 +01002998#ifdef CONFIG_SCHED_AUTOGROUP
2999 REG("autogroup", S_IRUGO|S_IWUSR, proc_pid_sched_autogroup_operations),
3000#endif
john stultz4614a696b2009-12-14 18:00:05 -08003001 REG("comm", S_IRUGO|S_IWUSR, proc_pid_set_comm_operations),
Roland McGrathebcb6732008-07-25 19:46:00 -07003002#ifdef CONFIG_HAVE_ARCH_TRACEHOOK
Alexey Dobriyan09d93bd2014-08-08 14:21:39 -07003003 ONE("syscall", S_IRUSR, proc_pid_syscall),
Roland McGrathebcb6732008-07-25 19:46:00 -07003004#endif
Alexey Dobriyanc2c0bb42015-06-25 15:00:54 -07003005 REG("cmdline", S_IRUGO, proc_pid_cmdline_ops),
Alexey Dobriyan631f9c12008-11-10 01:32:52 +03003006 ONE("stat", S_IRUGO, proc_tgid_stat),
3007 ONE("statm", S_IRUGO, proc_pid_statm),
Siddhesh Poyarekarb7643752012-03-21 16:34:04 -07003008 REG("maps", S_IRUGO, proc_pid_maps_operations),
Eric W. Biederman28a6d672006-10-02 02:17:05 -07003009#ifdef CONFIG_NUMA
Siddhesh Poyarekarb7643752012-03-21 16:34:04 -07003010 REG("numa_maps", S_IRUGO, proc_pid_numa_maps_operations),
Eric W. Biederman28a6d672006-10-02 02:17:05 -07003011#endif
Alexey Dobriyan631f9c12008-11-10 01:32:52 +03003012 REG("mem", S_IRUSR|S_IWUSR, proc_mem_operations),
3013 LNK("cwd", proc_cwd_link),
3014 LNK("root", proc_root_link),
3015 LNK("exe", proc_exe_link),
3016 REG("mounts", S_IRUGO, proc_mounts_operations),
3017 REG("mountinfo", S_IRUGO, proc_mountinfo_operations),
3018 REG("mountstats", S_IRUSR, proc_mountstats_operations),
Matt Mackall1e883282008-02-04 22:29:07 -08003019#ifdef CONFIG_PROC_PAGE_MONITOR
Alexey Dobriyan631f9c12008-11-10 01:32:52 +03003020 REG("clear_refs", S_IWUSR, proc_clear_refs_operations),
Siddhesh Poyarekarb7643752012-03-21 16:34:04 -07003021 REG("smaps", S_IRUGO, proc_pid_smaps_operations),
Daniel Colascione493b0e92017-09-06 16:25:08 -07003022 REG("smaps_rollup", S_IRUGO, proc_pid_smaps_rollup_operations),
Djalal Harouni32ed74a2014-04-07 15:38:38 -07003023 REG("pagemap", S_IRUSR, proc_pagemap_operations),
Eric W. Biederman28a6d672006-10-02 02:17:05 -07003024#endif
3025#ifdef CONFIG_SECURITY
Alexey Dobriyan631f9c12008-11-10 01:32:52 +03003026 DIR("attr", S_IRUGO|S_IXUGO, proc_attr_dir_inode_operations, proc_attr_dir_operations),
Eric W. Biederman28a6d672006-10-02 02:17:05 -07003027#endif
3028#ifdef CONFIG_KALLSYMS
Alexey Dobriyanedfcd602014-08-08 14:21:44 -07003029 ONE("wchan", S_IRUGO, proc_pid_wchan),
Eric W. Biederman28a6d672006-10-02 02:17:05 -07003030#endif
Ken Chen2ec220e2008-11-10 11:26:08 +03003031#ifdef CONFIG_STACKTRACE
Djalal Harouni35a35042014-04-07 15:38:36 -07003032 ONE("stack", S_IRUSR, proc_pid_stack),
Eric W. Biederman28a6d672006-10-02 02:17:05 -07003033#endif
Naveen N. Rao5968cec2015-06-30 14:36:03 +05303034#ifdef CONFIG_SCHED_INFO
Alexey Dobriyanf6e826c2014-08-08 14:21:46 -07003035 ONE("schedstat", S_IRUGO, proc_pid_schedstat),
Eric W. Biederman28a6d672006-10-02 02:17:05 -07003036#endif
Arjan van de Ven97455122008-01-25 21:08:34 +01003037#ifdef CONFIG_LATENCYTOP
Alexey Dobriyan631f9c12008-11-10 01:32:52 +03003038 REG("latency", S_IRUGO, proc_lstats_operations),
Arjan van de Ven97455122008-01-25 21:08:34 +01003039#endif
Paul Menage8793d852007-10-18 23:39:39 -07003040#ifdef CONFIG_PROC_PID_CPUSET
Zefan Li52de4772014-09-18 16:03:36 +08003041 ONE("cpuset", S_IRUGO, proc_cpuset_show),
Eric W. Biederman28a6d672006-10-02 02:17:05 -07003042#endif
Paul Menagea4243162007-10-18 23:39:35 -07003043#ifdef CONFIG_CGROUPS
Zefan Li006f4ac2014-09-18 16:03:15 +08003044 ONE("cgroup", S_IRUGO, proc_cgroup_show),
Paul Menagea4243162007-10-18 23:39:35 -07003045#endif
Alexey Dobriyan6ba51e32014-08-08 14:21:48 -07003046 ONE("oom_score", S_IRUGO, proc_oom_score),
David Rientjesfa0cbbf2012-11-12 17:53:04 -08003047 REG("oom_adj", S_IRUGO|S_IWUSR, proc_oom_adj_operations),
David Rientjesa63d83f2010-08-09 17:19:46 -07003048 REG("oom_score_adj", S_IRUGO|S_IWUSR, proc_oom_score_adj_operations),
Richard Guy Briggs4b7d2482019-01-22 17:06:39 -05003049#ifdef CONFIG_AUDIT
Alexey Dobriyan631f9c12008-11-10 01:32:52 +03003050 REG("loginuid", S_IWUSR|S_IRUGO, proc_loginuid_operations),
3051 REG("sessionid", S_IRUGO, proc_sessionid_operations),
Eric W. Biederman28a6d672006-10-02 02:17:05 -07003052#endif
Akinobu Mitaf4f154f2006-12-08 02:39:47 -08003053#ifdef CONFIG_FAULT_INJECTION
Alexey Dobriyan631f9c12008-11-10 01:32:52 +03003054 REG("make-it-fail", S_IRUGO|S_IWUSR, proc_fault_inject_operations),
Akinobu Mita168c42b2017-07-14 14:50:00 -07003055 REG("fail-nth", 0644, proc_fail_nth_operations),
Akinobu Mitaf4f154f2006-12-08 02:39:47 -08003056#endif
Christoph Hellwig698ba7b2009-12-15 16:47:37 -08003057#ifdef CONFIG_ELF_CORE
Alexey Dobriyan631f9c12008-11-10 01:32:52 +03003058 REG("coredump_filter", S_IRUGO|S_IWUSR, proc_coredump_filter_operations),
Kawai, Hidehiro3cb4a0b2007-07-19 01:48:28 -07003059#endif
Andrew Mortonaba76fd2006-12-10 02:19:48 -08003060#ifdef CONFIG_TASK_IO_ACCOUNTING
Alexey Dobriyan19aadc92014-08-08 14:21:50 -07003061 ONE("io", S_IRUSR, proc_tgid_io_accounting),
Andrew Mortonaba76fd2006-12-10 02:19:48 -08003062#endif
Eric W. Biederman22d917d2011-11-17 00:11:58 -08003063#ifdef CONFIG_USER_NS
3064 REG("uid_map", S_IRUGO|S_IWUSR, proc_uid_map_operations),
3065 REG("gid_map", S_IRUGO|S_IWUSR, proc_gid_map_operations),
Eric W. Biedermanf76d2072012-08-30 01:24:05 -07003066 REG("projid_map", S_IRUGO|S_IWUSR, proc_projid_map_operations),
Eric W. Biederman9cc46512014-12-02 12:27:26 -06003067 REG("setgroups", S_IRUGO|S_IWUSR, proc_setgroups_operations),
Eric W. Biederman22d917d2011-11-17 00:11:58 -08003068#endif
Nicolas Pitreb18b6a92017-01-21 00:09:08 -05003069#if defined(CONFIG_CHECKPOINT_RESTORE) && defined(CONFIG_POSIX_TIMERS)
Pavel Emelyanov48f6a7a2013-03-11 13:12:45 +04003070 REG("timers", S_IRUGO, proc_timers_operations),
3071#endif
John Stultz5de23d42016-03-17 14:20:54 -07003072 REG("timerslack_ns", S_IRUGO|S_IWUGO, proc_pid_set_timerslack_ns_operations),
Josh Poimboeuf7c23b332017-02-13 19:42:41 -06003073#ifdef CONFIG_LIVEPATCH
3074 ONE("patch_state", S_IRUSR, proc_pid_patch_state),
3075#endif
Alexander Popovc8d12622018-08-17 01:17:01 +03003076#ifdef CONFIG_STACKLEAK_METRICS
3077 ONE("stack_depth", S_IRUGO, proc_stack_depth),
3078#endif
Aubrey Li68bc30b2019-06-06 09:22:34 +08003079#ifdef CONFIG_PROC_PID_ARCH_STATUS
3080 ONE("arch_status", S_IRUGO, proc_pid_arch_status),
3081#endif
Eric W. Biederman28a6d672006-10-02 02:17:05 -07003082};
3083
Al Virof0c3b502013-05-16 12:07:31 -04003084static int proc_tgid_base_readdir(struct file *file, struct dir_context *ctx)
Eric W. Biederman28a6d672006-10-02 02:17:05 -07003085{
Al Virof0c3b502013-05-16 12:07:31 -04003086 return proc_pident_readdir(file, ctx,
3087 tgid_base_stuff, ARRAY_SIZE(tgid_base_stuff));
Eric W. Biederman28a6d672006-10-02 02:17:05 -07003088}
3089
Arjan van de Ven00977a52007-02-12 00:55:34 -08003090static const struct file_operations proc_tgid_base_operations = {
Eric W. Biederman28a6d672006-10-02 02:17:05 -07003091 .read = generic_read_dir,
Al Virof50752e2016-04-20 17:13:54 -04003092 .iterate_shared = proc_tgid_base_readdir,
3093 .llseek = generic_file_llseek,
Eric W. Biederman28a6d672006-10-02 02:17:05 -07003094};
3095
Christian Brauner3eb39f42018-11-19 00:51:56 +01003096struct pid *tgid_pidfd_to_pid(const struct file *file)
3097{
Christian Brauner30d158b2019-06-27 11:35:14 +02003098 if (file->f_op != &proc_tgid_base_operations)
Christian Brauner3eb39f42018-11-19 00:51:56 +01003099 return ERR_PTR(-EBADF);
3100
3101 return proc_pid(file_inode(file));
3102}
3103
Al Viro00cd8dd2012-06-10 17:13:09 -04003104static struct dentry *proc_tgid_base_lookup(struct inode *dir, struct dentry *dentry, unsigned int flags)
3105{
Eric W. Biederman7bcd6b02006-10-02 02:18:56 -07003106 return proc_pident_lookup(dir, dentry,
Alexey Dobriyand5a572a2019-03-11 23:28:51 -07003107 tgid_base_stuff,
3108 tgid_base_stuff + ARRAY_SIZE(tgid_base_stuff));
Eric W. Biederman28a6d672006-10-02 02:17:05 -07003109}
3110
Arjan van de Venc5ef1c42007-02-12 00:55:40 -08003111static const struct inode_operations proc_tgid_base_inode_operations = {
Eric W. Biederman28a6d672006-10-02 02:17:05 -07003112 .lookup = proc_tgid_base_lookup,
3113 .getattr = pid_getattr,
3114 .setattr = proc_setattr,
Vasiliy Kulikov04996802012-01-10 15:11:31 -08003115 .permission = proc_pid_permission,
Eric W. Biederman28a6d672006-10-02 02:17:05 -07003116};
3117
Pavel Emelyanov60347f62007-10-18 23:40:03 -07003118static void proc_flush_task_mnt(struct vfsmount *mnt, pid_t pid, pid_t tgid)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003119{
Eric W. Biederman48e64842006-06-26 00:25:48 -07003120 struct dentry *dentry, *leader, *dir;
Alexey Dobriyane3912ac2018-02-06 15:36:51 -08003121 char buf[10 + 1];
Eric W. Biederman48e64842006-06-26 00:25:48 -07003122 struct qstr name;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003123
Eric W. Biederman48e64842006-06-26 00:25:48 -07003124 name.name = buf;
Alexey Dobriyane3912ac2018-02-06 15:36:51 -08003125 name.len = snprintf(buf, sizeof(buf), "%u", pid);
Al Viro4f522a22013-02-11 23:20:37 -05003126 /* no ->d_hash() rejects on procfs */
Pavel Emelyanov60347f62007-10-18 23:40:03 -07003127 dentry = d_hash_and_lookup(mnt->mnt_root, &name);
Eric W. Biederman48e64842006-06-26 00:25:48 -07003128 if (dentry) {
Eric W. Biedermanbbd51922014-02-13 10:24:23 -08003129 d_invalidate(dentry);
Eric W. Biederman48e64842006-06-26 00:25:48 -07003130 dput(dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003131 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003132
Oleg Nesterovc35a7f12014-12-10 15:54:56 -08003133 if (pid == tgid)
3134 return;
3135
Eric W. Biederman48e64842006-06-26 00:25:48 -07003136 name.name = buf;
Alexey Dobriyane3912ac2018-02-06 15:36:51 -08003137 name.len = snprintf(buf, sizeof(buf), "%u", tgid);
Pavel Emelyanov60347f62007-10-18 23:40:03 -07003138 leader = d_hash_and_lookup(mnt->mnt_root, &name);
Eric W. Biederman48e64842006-06-26 00:25:48 -07003139 if (!leader)
3140 goto out;
3141
3142 name.name = "task";
3143 name.len = strlen(name.name);
3144 dir = d_hash_and_lookup(leader, &name);
3145 if (!dir)
3146 goto out_put_leader;
3147
3148 name.name = buf;
Alexey Dobriyane3912ac2018-02-06 15:36:51 -08003149 name.len = snprintf(buf, sizeof(buf), "%u", pid);
Eric W. Biederman48e64842006-06-26 00:25:48 -07003150 dentry = d_hash_and_lookup(dir, &name);
3151 if (dentry) {
Eric W. Biedermanbbd51922014-02-13 10:24:23 -08003152 d_invalidate(dentry);
Eric W. Biederman48e64842006-06-26 00:25:48 -07003153 dput(dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003154 }
Eric W. Biederman48e64842006-06-26 00:25:48 -07003155
3156 dput(dir);
3157out_put_leader:
3158 dput(leader);
3159out:
3160 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003161}
3162
Randy Dunlap0895e912007-10-21 21:00:10 -07003163/**
3164 * proc_flush_task - Remove dcache entries for @task from the /proc dcache.
3165 * @task: task that should be flushed.
3166 *
3167 * When flushing dentries from proc, one needs to flush them from global
Pavel Emelyanov60347f62007-10-18 23:40:03 -07003168 * proc (proc_mnt) and from all the namespaces' procs this task was seen
Randy Dunlap0895e912007-10-21 21:00:10 -07003169 * in. This call is supposed to do all of this job.
3170 *
3171 * Looks in the dcache for
3172 * /proc/@pid
3173 * /proc/@tgid/task/@pid
3174 * if either directory is present flushes it and all of it'ts children
3175 * from the dcache.
3176 *
3177 * It is safe and reasonable to cache /proc entries for a task until
3178 * that task exits. After that they just clog up the dcache with
3179 * useless entries, possibly causing useful dcache entries to be
3180 * flushed instead. This routine is proved to flush those useless
3181 * dcache entries at process exit time.
3182 *
3183 * NOTE: This routine is just an optimization so it does not guarantee
3184 * that no dcache entries will exist at process exit time it
3185 * just makes it very unlikely that any will persist.
Pavel Emelyanov60347f62007-10-18 23:40:03 -07003186 */
3187
3188void proc_flush_task(struct task_struct *task)
3189{
Eric W. Biederman9fcc2d12007-11-14 17:00:07 -08003190 int i;
Oleg Nesterov9b4d1cbe2009-09-22 16:45:34 -07003191 struct pid *pid, *tgid;
Pavel Emelyanov130f77e2007-10-18 23:40:11 -07003192 struct upid *upid;
3193
Pavel Emelyanov130f77e2007-10-18 23:40:11 -07003194 pid = task_pid(task);
Oleg Nesterov9b4d1cbe2009-09-22 16:45:34 -07003195 tgid = task_tgid(task);
Pavel Emelyanov130f77e2007-10-18 23:40:11 -07003196
Eric W. Biederman9fcc2d12007-11-14 17:00:07 -08003197 for (i = 0; i <= pid->level; i++) {
Pavel Emelyanov130f77e2007-10-18 23:40:11 -07003198 upid = &pid->numbers[i];
3199 proc_flush_task_mnt(upid->ns->proc_mnt, upid->nr,
Oleg Nesterov9b4d1cbe2009-09-22 16:45:34 -07003200 tgid->numbers[i].nr);
Pavel Emelyanov130f77e2007-10-18 23:40:11 -07003201 }
Pavel Emelyanov60347f62007-10-18 23:40:03 -07003202}
3203
Al Viro0168b9e2018-05-03 09:21:05 -04003204static struct dentry *proc_pid_instantiate(struct dentry * dentry,
Al Viroc52a47a2013-06-15 11:15:20 +04003205 struct task_struct *task, const void *ptr)
Eric W. Biederman444ceed2006-10-02 02:18:49 -07003206{
Eric W. Biederman444ceed2006-10-02 02:18:49 -07003207 struct inode *inode;
3208
Al Viro0168b9e2018-05-03 09:21:05 -04003209 inode = proc_pid_make_inode(dentry->d_sb, task, S_IFDIR | S_IRUGO | S_IXUGO);
Eric W. Biederman444ceed2006-10-02 02:18:49 -07003210 if (!inode)
Al Viro0168b9e2018-05-03 09:21:05 -04003211 return ERR_PTR(-ENOENT);
Eric W. Biederman444ceed2006-10-02 02:18:49 -07003212
Eric W. Biederman444ceed2006-10-02 02:18:49 -07003213 inode->i_op = &proc_tgid_base_inode_operations;
3214 inode->i_fop = &proc_tgid_base_operations;
3215 inode->i_flags|=S_IMMUTABLE;
Vegard Nossumaed54172008-06-05 22:46:53 -07003216
Alexey Dobriyan1270dd82016-12-12 16:45:32 -08003217 set_nlink(inode, nlink_tgid);
Al Viro1bbc5512018-05-02 21:26:16 -04003218 pid_update_inode(task, inode);
Eric W. Biederman444ceed2006-10-02 02:18:49 -07003219
Nick Pigginfb045ad2011-01-07 17:49:55 +11003220 d_set_d_op(dentry, &pid_dentry_operations);
Al Viro0168b9e2018-05-03 09:21:05 -04003221 return d_splice_alias(inode, dentry);
Eric W. Biederman444ceed2006-10-02 02:18:49 -07003222}
3223
Zhikang Zhang867aacc2019-03-05 15:50:29 -08003224struct dentry *proc_pid_lookup(struct dentry *dentry, unsigned int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003225{
3226 struct task_struct *task;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003227 unsigned tgid;
Pavel Emelyanovb4888932007-10-18 23:40:14 -07003228 struct pid_namespace *ns;
Al Viro0168b9e2018-05-03 09:21:05 -04003229 struct dentry *result = ERR_PTR(-ENOENT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003230
Alexey Dobriyandbcdb502014-08-08 14:21:25 -07003231 tgid = name_to_int(&dentry->d_name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003232 if (tgid == ~0U)
3233 goto out;
3234
Pavel Emelyanovb4888932007-10-18 23:40:14 -07003235 ns = dentry->d_sb->s_fs_info;
Eric W. Biedermande758732006-06-26 00:25:51 -07003236 rcu_read_lock();
Pavel Emelyanovb4888932007-10-18 23:40:14 -07003237 task = find_task_by_pid_ns(tgid, ns);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003238 if (task)
3239 get_task_struct(task);
Eric W. Biedermande758732006-06-26 00:25:51 -07003240 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07003241 if (!task)
3242 goto out;
3243
Al Viro0168b9e2018-05-03 09:21:05 -04003244 result = proc_pid_instantiate(dentry, task, NULL);
Eric W. Biederman48e64842006-06-26 00:25:48 -07003245 put_task_struct(task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003246out:
Al Viro0168b9e2018-05-03 09:21:05 -04003247 return result;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003248}
3249
Linus Torvalds1da177e2005-04-16 15:20:36 -07003250/*
Eric W. Biederman0804ef42006-10-02 02:17:04 -07003251 * Find the first task with tgid >= tgid
Eric W. Biederman0bc58a92006-06-26 00:25:50 -07003252 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07003253 */
Eric W. Biederman19fd4bb2007-11-28 16:21:26 -08003254struct tgid_iter {
3255 unsigned int tgid;
Eric W. Biederman0804ef42006-10-02 02:17:04 -07003256 struct task_struct *task;
Eric W. Biederman19fd4bb2007-11-28 16:21:26 -08003257};
3258static struct tgid_iter next_tgid(struct pid_namespace *ns, struct tgid_iter iter)
3259{
Eric W. Biederman0804ef42006-10-02 02:17:04 -07003260 struct pid *pid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003261
Eric W. Biederman19fd4bb2007-11-28 16:21:26 -08003262 if (iter.task)
3263 put_task_struct(iter.task);
Eric W. Biederman0804ef42006-10-02 02:17:04 -07003264 rcu_read_lock();
3265retry:
Eric W. Biederman19fd4bb2007-11-28 16:21:26 -08003266 iter.task = NULL;
3267 pid = find_ge_pid(iter.tgid, ns);
Eric W. Biederman0804ef42006-10-02 02:17:04 -07003268 if (pid) {
Eric W. Biederman19fd4bb2007-11-28 16:21:26 -08003269 iter.tgid = pid_nr_ns(pid, ns);
3270 iter.task = pid_task(pid, PIDTYPE_PID);
Eric W. Biederman0804ef42006-10-02 02:17:04 -07003271 /* What we to know is if the pid we have find is the
3272 * pid of a thread_group_leader. Testing for task
3273 * being a thread_group_leader is the obvious thing
3274 * todo but there is a window when it fails, due to
3275 * the pid transfer logic in de_thread.
3276 *
3277 * So we perform the straight forward test of seeing
3278 * if the pid we have found is the pid of a thread
3279 * group leader, and don't worry if the task we have
3280 * found doesn't happen to be a thread group leader.
3281 * As we don't care in the case of readdir.
3282 */
Eric W. Biederman19fd4bb2007-11-28 16:21:26 -08003283 if (!iter.task || !has_group_leader_pid(iter.task)) {
3284 iter.tgid += 1;
Eric W. Biederman0804ef42006-10-02 02:17:04 -07003285 goto retry;
Eric W. Biederman19fd4bb2007-11-28 16:21:26 -08003286 }
3287 get_task_struct(iter.task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003288 }
Eric W. Biederman454cc102006-06-26 00:25:51 -07003289 rcu_read_unlock();
Eric W. Biederman19fd4bb2007-11-28 16:21:26 -08003290 return iter;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003291}
3292
Eric W. Biederman00978752014-07-31 03:10:50 -07003293#define TGID_OFFSET (FIRST_PROCESS_ENTRY + 2)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003294
3295/* for the /proc/ directory itself, after non-process stuff has been done */
Al Virof0c3b502013-05-16 12:07:31 -04003296int proc_pid_readdir(struct file *file, struct dir_context *ctx)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003297{
Eric W. Biederman19fd4bb2007-11-28 16:21:26 -08003298 struct tgid_iter iter;
Christoph Hellwig76f668b2018-05-16 07:19:01 +02003299 struct pid_namespace *ns = proc_pid_ns(file_inode(file));
Al Virof0c3b502013-05-16 12:07:31 -04003300 loff_t pos = ctx->pos;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003301
Al Viro021ada72013-03-29 19:27:05 -04003302 if (pos >= PID_MAX_LIMIT + TGID_OFFSET)
Al Virof0c3b502013-05-16 12:07:31 -04003303 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003304
Eric W. Biederman00978752014-07-31 03:10:50 -07003305 if (pos == TGID_OFFSET - 2) {
David Howells2b0143b2015-03-17 22:25:59 +00003306 struct inode *inode = d_inode(ns->proc_self);
Al Virodb963162013-06-15 10:45:10 +04003307 if (!dir_emit(ctx, "self", 4, inode->i_ino, DT_LNK))
Al Virof0c3b502013-05-16 12:07:31 -04003308 return 0;
Eric W. Biederman00978752014-07-31 03:10:50 -07003309 ctx->pos = pos = pos + 1;
Al Viro021ada72013-03-29 19:27:05 -04003310 }
Eric W. Biederman00978752014-07-31 03:10:50 -07003311 if (pos == TGID_OFFSET - 1) {
David Howells2b0143b2015-03-17 22:25:59 +00003312 struct inode *inode = d_inode(ns->proc_thread_self);
Eric W. Biederman00978752014-07-31 03:10:50 -07003313 if (!dir_emit(ctx, "thread-self", 11, inode->i_ino, DT_LNK))
3314 return 0;
3315 ctx->pos = pos = pos + 1;
3316 }
3317 iter.tgid = pos - TGID_OFFSET;
Eric W. Biederman19fd4bb2007-11-28 16:21:26 -08003318 iter.task = NULL;
Eric W. Biederman19fd4bb2007-11-28 16:21:26 -08003319 for (iter = next_tgid(ns, iter);
3320 iter.task;
3321 iter.tgid += 1, iter = next_tgid(ns, iter)) {
Alexey Dobriyane3912ac2018-02-06 15:36:51 -08003322 char name[10 + 1];
Alexey Dobriyana4ef3892018-06-07 17:10:10 -07003323 unsigned int len;
Eric Dumazet3ba4bce2017-01-24 15:18:07 -08003324
3325 cond_resched();
Lafcadio Wluiki796f5712017-02-24 15:00:23 -08003326 if (!has_pid_permissions(ns, iter.task, HIDEPID_INVISIBLE))
Al Virof0c3b502013-05-16 12:07:31 -04003327 continue;
Vasiliy Kulikov04996802012-01-10 15:11:31 -08003328
Alexey Dobriyane3912ac2018-02-06 15:36:51 -08003329 len = snprintf(name, sizeof(name), "%u", iter.tgid);
Al Virof0c3b502013-05-16 12:07:31 -04003330 ctx->pos = iter.tgid + TGID_OFFSET;
3331 if (!proc_fill_cache(file, ctx, name, len,
3332 proc_pid_instantiate, iter.task, NULL)) {
Eric W. Biederman19fd4bb2007-11-28 16:21:26 -08003333 put_task_struct(iter.task);
Al Virof0c3b502013-05-16 12:07:31 -04003334 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003335 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003336 }
Al Virof0c3b502013-05-16 12:07:31 -04003337 ctx->pos = PID_MAX_LIMIT + TGID_OFFSET;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003338 return 0;
3339}
3340
Eric W. Biederman0bc58a92006-06-26 00:25:50 -07003341/*
Janis Danisevskis1b3044e2016-05-20 17:00:08 -07003342 * proc_tid_comm_permission is a special permission function exclusively
3343 * used for the node /proc/<pid>/task/<tid>/comm.
3344 * It bypasses generic permission checks in the case where a task of the same
3345 * task group attempts to access the node.
3346 * The rationale behind this is that glibc and bionic access this node for
3347 * cross thread naming (pthread_set/getname_np(!self)). However, if
3348 * PR_SET_DUMPABLE gets set to 0 this node among others becomes uid=0 gid=0,
3349 * which locks out the cross thread naming implementation.
3350 * This function makes sure that the node is always accessible for members of
3351 * same thread group.
3352 */
3353static int proc_tid_comm_permission(struct inode *inode, int mask)
3354{
3355 bool is_same_tgroup;
3356 struct task_struct *task;
3357
3358 task = get_proc_task(inode);
3359 if (!task)
3360 return -ESRCH;
3361 is_same_tgroup = same_thread_group(current, task);
3362 put_task_struct(task);
3363
3364 if (likely(is_same_tgroup && !(mask & MAY_EXEC))) {
3365 /* This file (/proc/<pid>/task/<tid>/comm) can always be
3366 * read or written by the members of the corresponding
3367 * thread group.
3368 */
3369 return 0;
3370 }
3371
3372 return generic_permission(inode, mask);
3373}
3374
3375static const struct inode_operations proc_tid_comm_inode_operations = {
3376 .permission = proc_tid_comm_permission,
3377};
3378
3379/*
Eric W. Biederman28a6d672006-10-02 02:17:05 -07003380 * Tasks
3381 */
Eric Dumazetc5141e62007-05-08 00:26:15 -07003382static const struct pid_entry tid_base_stuff[] = {
Alexey Dobriyan631f9c12008-11-10 01:32:52 +03003383 DIR("fd", S_IRUSR|S_IXUSR, proc_fd_inode_operations, proc_fd_operations),
Jerome Marchand38355412010-04-27 13:13:06 -07003384 DIR("fdinfo", S_IRUSR|S_IXUSR, proc_fdinfo_inode_operations, proc_fdinfo_operations),
Eric W. Biederman6b4e3062010-03-07 16:41:34 -08003385 DIR("ns", S_IRUSR|S_IXUGO, proc_ns_dir_inode_operations, proc_ns_dir_operations),
Eric W. Biederman6ba8ed72014-07-31 16:27:08 -07003386#ifdef CONFIG_NET
3387 DIR("net", S_IRUGO|S_IXUGO, proc_net_inode_operations, proc_net_operations),
3388#endif
Alexey Dobriyan631f9c12008-11-10 01:32:52 +03003389 REG("environ", S_IRUSR, proc_environ_operations),
Al Viroc5317162016-10-05 18:43:43 -04003390 REG("auxv", S_IRUSR, proc_auxv_operations),
Alexey Dobriyan631f9c12008-11-10 01:32:52 +03003391 ONE("status", S_IRUGO, proc_pid_status),
Djalal Harouni35a35042014-04-07 15:38:36 -07003392 ONE("personality", S_IRUSR, proc_pid_personality),
Alexey Dobriyan1c963eb2014-08-08 14:21:37 -07003393 ONE("limits", S_IRUGO, proc_pid_limits),
Ingo Molnar43ae34c2007-07-09 18:52:00 +02003394#ifdef CONFIG_SCHED_DEBUG
Alexey Dobriyan631f9c12008-11-10 01:32:52 +03003395 REG("sched", S_IRUGO|S_IWUSR, proc_pid_sched_operations),
Ingo Molnar43ae34c2007-07-09 18:52:00 +02003396#endif
Janis Danisevskis1b3044e2016-05-20 17:00:08 -07003397 NOD("comm", S_IFREG|S_IRUGO|S_IWUSR,
3398 &proc_tid_comm_inode_operations,
3399 &proc_pid_set_comm_operations, {}),
Roland McGrathebcb6732008-07-25 19:46:00 -07003400#ifdef CONFIG_HAVE_ARCH_TRACEHOOK
Alexey Dobriyan09d93bd2014-08-08 14:21:39 -07003401 ONE("syscall", S_IRUSR, proc_pid_syscall),
Roland McGrathebcb6732008-07-25 19:46:00 -07003402#endif
Alexey Dobriyanc2c0bb42015-06-25 15:00:54 -07003403 REG("cmdline", S_IRUGO, proc_pid_cmdline_ops),
Alexey Dobriyan631f9c12008-11-10 01:32:52 +03003404 ONE("stat", S_IRUGO, proc_tid_stat),
3405 ONE("statm", S_IRUGO, proc_pid_statm),
Vlastimil Babka871305b2018-08-21 21:52:48 -07003406 REG("maps", S_IRUGO, proc_pid_maps_operations),
Iago López Galeiras2e13ba52015-06-25 15:00:57 -07003407#ifdef CONFIG_PROC_CHILDREN
Cyrill Gorcunov818411612012-05-31 16:26:43 -07003408 REG("children", S_IRUGO, proc_tid_children_operations),
3409#endif
Eric W. Biederman28a6d672006-10-02 02:17:05 -07003410#ifdef CONFIG_NUMA
Vlastimil Babka871305b2018-08-21 21:52:48 -07003411 REG("numa_maps", S_IRUGO, proc_pid_numa_maps_operations),
Eric W. Biederman28a6d672006-10-02 02:17:05 -07003412#endif
Alexey Dobriyan631f9c12008-11-10 01:32:52 +03003413 REG("mem", S_IRUSR|S_IWUSR, proc_mem_operations),
3414 LNK("cwd", proc_cwd_link),
3415 LNK("root", proc_root_link),
3416 LNK("exe", proc_exe_link),
3417 REG("mounts", S_IRUGO, proc_mounts_operations),
3418 REG("mountinfo", S_IRUGO, proc_mountinfo_operations),
Matt Mackall1e883282008-02-04 22:29:07 -08003419#ifdef CONFIG_PROC_PAGE_MONITOR
Alexey Dobriyan631f9c12008-11-10 01:32:52 +03003420 REG("clear_refs", S_IWUSR, proc_clear_refs_operations),
Vlastimil Babka871305b2018-08-21 21:52:48 -07003421 REG("smaps", S_IRUGO, proc_pid_smaps_operations),
Daniel Colascione493b0e92017-09-06 16:25:08 -07003422 REG("smaps_rollup", S_IRUGO, proc_pid_smaps_rollup_operations),
Djalal Harouni32ed74a2014-04-07 15:38:38 -07003423 REG("pagemap", S_IRUSR, proc_pagemap_operations),
Eric W. Biederman28a6d672006-10-02 02:17:05 -07003424#endif
3425#ifdef CONFIG_SECURITY
Alexey Dobriyan631f9c12008-11-10 01:32:52 +03003426 DIR("attr", S_IRUGO|S_IXUGO, proc_attr_dir_inode_operations, proc_attr_dir_operations),
Eric W. Biederman28a6d672006-10-02 02:17:05 -07003427#endif
3428#ifdef CONFIG_KALLSYMS
Alexey Dobriyanedfcd602014-08-08 14:21:44 -07003429 ONE("wchan", S_IRUGO, proc_pid_wchan),
Eric W. Biederman28a6d672006-10-02 02:17:05 -07003430#endif
Ken Chen2ec220e2008-11-10 11:26:08 +03003431#ifdef CONFIG_STACKTRACE
Djalal Harouni35a35042014-04-07 15:38:36 -07003432 ONE("stack", S_IRUSR, proc_pid_stack),
Eric W. Biederman28a6d672006-10-02 02:17:05 -07003433#endif
Naveen N. Rao5968cec2015-06-30 14:36:03 +05303434#ifdef CONFIG_SCHED_INFO
Alexey Dobriyanf6e826c2014-08-08 14:21:46 -07003435 ONE("schedstat", S_IRUGO, proc_pid_schedstat),
Eric W. Biederman28a6d672006-10-02 02:17:05 -07003436#endif
Arjan van de Ven97455122008-01-25 21:08:34 +01003437#ifdef CONFIG_LATENCYTOP
Alexey Dobriyan631f9c12008-11-10 01:32:52 +03003438 REG("latency", S_IRUGO, proc_lstats_operations),
Arjan van de Ven97455122008-01-25 21:08:34 +01003439#endif
Paul Menage8793d852007-10-18 23:39:39 -07003440#ifdef CONFIG_PROC_PID_CPUSET
Zefan Li52de4772014-09-18 16:03:36 +08003441 ONE("cpuset", S_IRUGO, proc_cpuset_show),
Eric W. Biederman28a6d672006-10-02 02:17:05 -07003442#endif
Paul Menagea4243162007-10-18 23:39:35 -07003443#ifdef CONFIG_CGROUPS
Zefan Li006f4ac2014-09-18 16:03:15 +08003444 ONE("cgroup", S_IRUGO, proc_cgroup_show),
Paul Menagea4243162007-10-18 23:39:35 -07003445#endif
Alexey Dobriyan6ba51e32014-08-08 14:21:48 -07003446 ONE("oom_score", S_IRUGO, proc_oom_score),
David Rientjesfa0cbbf2012-11-12 17:53:04 -08003447 REG("oom_adj", S_IRUGO|S_IWUSR, proc_oom_adj_operations),
David Rientjesa63d83f2010-08-09 17:19:46 -07003448 REG("oom_score_adj", S_IRUGO|S_IWUSR, proc_oom_score_adj_operations),
Richard Guy Briggs4b7d2482019-01-22 17:06:39 -05003449#ifdef CONFIG_AUDIT
Alexey Dobriyan631f9c12008-11-10 01:32:52 +03003450 REG("loginuid", S_IWUSR|S_IRUGO, proc_loginuid_operations),
Al Viro26ec3c62011-02-15 21:24:05 -05003451 REG("sessionid", S_IRUGO, proc_sessionid_operations),
Eric W. Biederman28a6d672006-10-02 02:17:05 -07003452#endif
Akinobu Mitaf4f154f2006-12-08 02:39:47 -08003453#ifdef CONFIG_FAULT_INJECTION
Alexey Dobriyan631f9c12008-11-10 01:32:52 +03003454 REG("make-it-fail", S_IRUGO|S_IWUSR, proc_fault_inject_operations),
Akinobu Mita1203c8e2017-07-14 14:49:57 -07003455 REG("fail-nth", 0644, proc_fail_nth_operations),
Akinobu Mitaf4f154f2006-12-08 02:39:47 -08003456#endif
Andrea Righi297c5d92008-07-25 01:48:49 -07003457#ifdef CONFIG_TASK_IO_ACCOUNTING
Alexey Dobriyan19aadc92014-08-08 14:21:50 -07003458 ONE("io", S_IRUSR, proc_tid_io_accounting),
Andrea Righi297c5d92008-07-25 01:48:49 -07003459#endif
Eric W. Biederman22d917d2011-11-17 00:11:58 -08003460#ifdef CONFIG_USER_NS
3461 REG("uid_map", S_IRUGO|S_IWUSR, proc_uid_map_operations),
3462 REG("gid_map", S_IRUGO|S_IWUSR, proc_gid_map_operations),
Eric W. Biedermanf76d2072012-08-30 01:24:05 -07003463 REG("projid_map", S_IRUGO|S_IWUSR, proc_projid_map_operations),
Eric W. Biederman9cc46512014-12-02 12:27:26 -06003464 REG("setgroups", S_IRUGO|S_IWUSR, proc_setgroups_operations),
Eric W. Biederman22d917d2011-11-17 00:11:58 -08003465#endif
Josh Poimboeuf7c23b332017-02-13 19:42:41 -06003466#ifdef CONFIG_LIVEPATCH
3467 ONE("patch_state", S_IRUSR, proc_pid_patch_state),
3468#endif
Aubrey Li68bc30b2019-06-06 09:22:34 +08003469#ifdef CONFIG_PROC_PID_ARCH_STATUS
3470 ONE("arch_status", S_IRUGO, proc_pid_arch_status),
3471#endif
Eric W. Biederman28a6d672006-10-02 02:17:05 -07003472};
3473
Al Virof0c3b502013-05-16 12:07:31 -04003474static int proc_tid_base_readdir(struct file *file, struct dir_context *ctx)
Eric W. Biederman28a6d672006-10-02 02:17:05 -07003475{
Al Virof0c3b502013-05-16 12:07:31 -04003476 return proc_pident_readdir(file, ctx,
3477 tid_base_stuff, ARRAY_SIZE(tid_base_stuff));
Eric W. Biederman28a6d672006-10-02 02:17:05 -07003478}
3479
Al Viro00cd8dd2012-06-10 17:13:09 -04003480static struct dentry *proc_tid_base_lookup(struct inode *dir, struct dentry *dentry, unsigned int flags)
3481{
Eric W. Biederman7bcd6b02006-10-02 02:18:56 -07003482 return proc_pident_lookup(dir, dentry,
Alexey Dobriyand5a572a2019-03-11 23:28:51 -07003483 tid_base_stuff,
3484 tid_base_stuff + ARRAY_SIZE(tid_base_stuff));
Eric W. Biederman28a6d672006-10-02 02:17:05 -07003485}
3486
Arjan van de Ven00977a52007-02-12 00:55:34 -08003487static const struct file_operations proc_tid_base_operations = {
Eric W. Biederman28a6d672006-10-02 02:17:05 -07003488 .read = generic_read_dir,
Al Virof50752e2016-04-20 17:13:54 -04003489 .iterate_shared = proc_tid_base_readdir,
3490 .llseek = generic_file_llseek,
Eric W. Biederman28a6d672006-10-02 02:17:05 -07003491};
3492
Arjan van de Venc5ef1c42007-02-12 00:55:40 -08003493static const struct inode_operations proc_tid_base_inode_operations = {
Eric W. Biederman28a6d672006-10-02 02:17:05 -07003494 .lookup = proc_tid_base_lookup,
3495 .getattr = pid_getattr,
3496 .setattr = proc_setattr,
3497};
3498
Al Viro0168b9e2018-05-03 09:21:05 -04003499static struct dentry *proc_task_instantiate(struct dentry *dentry,
3500 struct task_struct *task, const void *ptr)
Eric W. Biederman444ceed2006-10-02 02:18:49 -07003501{
Eric W. Biederman444ceed2006-10-02 02:18:49 -07003502 struct inode *inode;
Al Viro0168b9e2018-05-03 09:21:05 -04003503 inode = proc_pid_make_inode(dentry->d_sb, task, S_IFDIR | S_IRUGO | S_IXUGO);
Eric W. Biederman444ceed2006-10-02 02:18:49 -07003504 if (!inode)
Al Viro0168b9e2018-05-03 09:21:05 -04003505 return ERR_PTR(-ENOENT);
Al Viro1bbc5512018-05-02 21:26:16 -04003506
Eric W. Biederman444ceed2006-10-02 02:18:49 -07003507 inode->i_op = &proc_tid_base_inode_operations;
3508 inode->i_fop = &proc_tid_base_operations;
Al Viro1bbc5512018-05-02 21:26:16 -04003509 inode->i_flags |= S_IMMUTABLE;
Vegard Nossumaed54172008-06-05 22:46:53 -07003510
Alexey Dobriyan1270dd82016-12-12 16:45:32 -08003511 set_nlink(inode, nlink_tid);
Al Viro1bbc5512018-05-02 21:26:16 -04003512 pid_update_inode(task, inode);
Eric W. Biederman444ceed2006-10-02 02:18:49 -07003513
Nick Pigginfb045ad2011-01-07 17:49:55 +11003514 d_set_d_op(dentry, &pid_dentry_operations);
Al Viro0168b9e2018-05-03 09:21:05 -04003515 return d_splice_alias(inode, dentry);
Eric W. Biederman444ceed2006-10-02 02:18:49 -07003516}
3517
Al Viro00cd8dd2012-06-10 17:13:09 -04003518static struct dentry *proc_task_lookup(struct inode *dir, struct dentry * dentry, unsigned int flags)
Eric W. Biederman28a6d672006-10-02 02:17:05 -07003519{
Eric W. Biederman28a6d672006-10-02 02:17:05 -07003520 struct task_struct *task;
3521 struct task_struct *leader = get_proc_task(dir);
Eric W. Biederman28a6d672006-10-02 02:17:05 -07003522 unsigned tid;
Pavel Emelyanovb4888932007-10-18 23:40:14 -07003523 struct pid_namespace *ns;
Al Viro0168b9e2018-05-03 09:21:05 -04003524 struct dentry *result = ERR_PTR(-ENOENT);
Eric W. Biederman28a6d672006-10-02 02:17:05 -07003525
3526 if (!leader)
3527 goto out_no_task;
3528
Alexey Dobriyandbcdb502014-08-08 14:21:25 -07003529 tid = name_to_int(&dentry->d_name);
Eric W. Biederman28a6d672006-10-02 02:17:05 -07003530 if (tid == ~0U)
3531 goto out;
3532
Pavel Emelyanovb4888932007-10-18 23:40:14 -07003533 ns = dentry->d_sb->s_fs_info;
Eric W. Biederman28a6d672006-10-02 02:17:05 -07003534 rcu_read_lock();
Pavel Emelyanovb4888932007-10-18 23:40:14 -07003535 task = find_task_by_pid_ns(tid, ns);
Eric W. Biederman28a6d672006-10-02 02:17:05 -07003536 if (task)
3537 get_task_struct(task);
3538 rcu_read_unlock();
3539 if (!task)
3540 goto out;
Pavel Emelyanovbac0abd2007-10-18 23:40:18 -07003541 if (!same_thread_group(leader, task))
Eric W. Biederman28a6d672006-10-02 02:17:05 -07003542 goto out_drop_task;
3543
Al Viro0168b9e2018-05-03 09:21:05 -04003544 result = proc_task_instantiate(dentry, task, NULL);
Eric W. Biederman28a6d672006-10-02 02:17:05 -07003545out_drop_task:
3546 put_task_struct(task);
3547out:
3548 put_task_struct(leader);
3549out_no_task:
Al Viro0168b9e2018-05-03 09:21:05 -04003550 return result;
Eric W. Biederman28a6d672006-10-02 02:17:05 -07003551}
3552
3553/*
Eric W. Biederman0bc58a92006-06-26 00:25:50 -07003554 * Find the first tid of a thread group to return to user space.
3555 *
3556 * Usually this is just the thread group leader, but if the users
3557 * buffer was too small or there was a seek into the middle of the
3558 * directory we have more work todo.
3559 *
3560 * In the case of a short read we start with find_task_by_pid.
3561 *
3562 * In the case of a seek we start with the leader and walk nr
3563 * threads past it.
3564 */
Oleg Nesterov9f6e9632014-01-23 15:55:40 -08003565static struct task_struct *first_tid(struct pid *pid, int tid, loff_t f_pos,
3566 struct pid_namespace *ns)
Eric W. Biederman0bc58a92006-06-26 00:25:50 -07003567{
Oleg Nesterovd855a4b2014-01-23 15:55:39 -08003568 struct task_struct *pos, *task;
Oleg Nesterov9f6e9632014-01-23 15:55:40 -08003569 unsigned long nr = f_pos;
3570
3571 if (nr != f_pos) /* 32bit overflow? */
3572 return NULL;
Eric W. Biederman0bc58a92006-06-26 00:25:50 -07003573
Eric W. Biedermancc288732006-06-26 00:26:01 -07003574 rcu_read_lock();
Oleg Nesterovd855a4b2014-01-23 15:55:39 -08003575 task = pid_task(pid, PIDTYPE_PID);
3576 if (!task)
3577 goto fail;
3578
3579 /* Attempt to start with the tid of a thread */
Oleg Nesterov9f6e9632014-01-23 15:55:40 -08003580 if (tid && nr) {
Pavel Emelyanovb4888932007-10-18 23:40:14 -07003581 pos = find_task_by_pid_ns(tid, ns);
Oleg Nesterovd855a4b2014-01-23 15:55:39 -08003582 if (pos && same_thread_group(pos, task))
Oleg Nesterova872ff02006-06-26 00:26:01 -07003583 goto found;
Eric W. Biederman0bc58a92006-06-26 00:25:50 -07003584 }
3585
3586 /* If nr exceeds the number of threads there is nothing todo */
Oleg Nesterov9f6e9632014-01-23 15:55:40 -08003587 if (nr >= get_nr_threads(task))
Oleg Nesterovc986c142014-01-23 15:55:38 -08003588 goto fail;
Oleg Nesterova872ff02006-06-26 00:26:01 -07003589
3590 /* If we haven't found our starting place yet start
3591 * with the leader and walk nr threads forward.
3592 */
Oleg Nesterovd855a4b2014-01-23 15:55:39 -08003593 pos = task = task->group_leader;
Oleg Nesterovc986c142014-01-23 15:55:38 -08003594 do {
Oleg Nesterov9f6e9632014-01-23 15:55:40 -08003595 if (!nr--)
Oleg Nesterovc986c142014-01-23 15:55:38 -08003596 goto found;
Oleg Nesterovd855a4b2014-01-23 15:55:39 -08003597 } while_each_thread(task, pos);
Oleg Nesterovc986c142014-01-23 15:55:38 -08003598fail:
3599 pos = NULL;
3600 goto out;
Oleg Nesterova872ff02006-06-26 00:26:01 -07003601found:
3602 get_task_struct(pos);
3603out:
Eric W. Biedermancc288732006-06-26 00:26:01 -07003604 rcu_read_unlock();
Eric W. Biederman0bc58a92006-06-26 00:25:50 -07003605 return pos;
3606}
3607
3608/*
3609 * Find the next thread in the thread list.
3610 * Return NULL if there is an error or no next thread.
3611 *
3612 * The reference to the input task_struct is released.
3613 */
3614static struct task_struct *next_tid(struct task_struct *start)
3615{
Oleg Nesterovc1df7fb2006-06-26 00:26:02 -07003616 struct task_struct *pos = NULL;
Eric W. Biedermancc288732006-06-26 00:26:01 -07003617 rcu_read_lock();
Oleg Nesterovc1df7fb2006-06-26 00:26:02 -07003618 if (pid_alive(start)) {
Eric W. Biederman0bc58a92006-06-26 00:25:50 -07003619 pos = next_thread(start);
Oleg Nesterovc1df7fb2006-06-26 00:26:02 -07003620 if (thread_group_leader(pos))
3621 pos = NULL;
3622 else
3623 get_task_struct(pos);
3624 }
Eric W. Biedermancc288732006-06-26 00:26:01 -07003625 rcu_read_unlock();
Eric W. Biederman0bc58a92006-06-26 00:25:50 -07003626 put_task_struct(start);
3627 return pos;
3628}
3629
Linus Torvalds1da177e2005-04-16 15:20:36 -07003630/* for the /proc/TGID/task/ directories */
Al Virof0c3b502013-05-16 12:07:31 -04003631static int proc_task_readdir(struct file *file, struct dir_context *ctx)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003632{
Oleg Nesterovd855a4b2014-01-23 15:55:39 -08003633 struct inode *inode = file_inode(file);
3634 struct task_struct *task;
Pavel Emelyanovb4888932007-10-18 23:40:14 -07003635 struct pid_namespace *ns;
Al Virof0c3b502013-05-16 12:07:31 -04003636 int tid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003637
Oleg Nesterovd855a4b2014-01-23 15:55:39 -08003638 if (proc_inode_is_dead(inode))
Al Virof0c3b502013-05-16 12:07:31 -04003639 return -ENOENT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003640
Al Virof0c3b502013-05-16 12:07:31 -04003641 if (!dir_emit_dots(file, ctx))
Oleg Nesterovd855a4b2014-01-23 15:55:39 -08003642 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003643
Eric W. Biederman0bc58a92006-06-26 00:25:50 -07003644 /* f_version caches the tgid value that the last readdir call couldn't
3645 * return. lseek aka telldir automagically resets f_version to 0.
3646 */
Christoph Hellwig76f668b2018-05-16 07:19:01 +02003647 ns = proc_pid_ns(inode);
Al Virof0c3b502013-05-16 12:07:31 -04003648 tid = (int)file->f_version;
3649 file->f_version = 0;
Oleg Nesterovd855a4b2014-01-23 15:55:39 -08003650 for (task = first_tid(proc_pid(inode), tid, ctx->pos - 2, ns);
Eric W. Biederman0bc58a92006-06-26 00:25:50 -07003651 task;
Al Virof0c3b502013-05-16 12:07:31 -04003652 task = next_tid(task), ctx->pos++) {
Alexey Dobriyane3912ac2018-02-06 15:36:51 -08003653 char name[10 + 1];
Alexey Dobriyana4ef3892018-06-07 17:10:10 -07003654 unsigned int len;
Pavel Emelyanovb4888932007-10-18 23:40:14 -07003655 tid = task_pid_nr_ns(task, ns);
Alexey Dobriyane3912ac2018-02-06 15:36:51 -08003656 len = snprintf(name, sizeof(name), "%u", tid);
Al Virof0c3b502013-05-16 12:07:31 -04003657 if (!proc_fill_cache(file, ctx, name, len,
3658 proc_task_instantiate, task, NULL)) {
Eric W. Biederman0bc58a92006-06-26 00:25:50 -07003659 /* returning this tgid failed, save it as the first
3660 * pid for the next readir call */
Al Virof0c3b502013-05-16 12:07:31 -04003661 file->f_version = (u64)tid;
Eric W. Biederman0bc58a92006-06-26 00:25:50 -07003662 put_task_struct(task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003663 break;
Eric W. Biederman0bc58a92006-06-26 00:25:50 -07003664 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003665 }
Oleg Nesterovd855a4b2014-01-23 15:55:39 -08003666
Al Virof0c3b502013-05-16 12:07:31 -04003667 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003668}
Eric W. Biederman6e66b522006-06-26 00:25:47 -07003669
David Howellsa528d352017-01-31 16:46:22 +00003670static int proc_task_getattr(const struct path *path, struct kstat *stat,
3671 u32 request_mask, unsigned int query_flags)
Eric W. Biederman6e66b522006-06-26 00:25:47 -07003672{
David Howellsa528d352017-01-31 16:46:22 +00003673 struct inode *inode = d_inode(path->dentry);
Eric W. Biederman99f89552006-06-26 00:25:55 -07003674 struct task_struct *p = get_proc_task(inode);
Eric W. Biederman6e66b522006-06-26 00:25:47 -07003675 generic_fillattr(inode, stat);
3676
Eric W. Biederman99f89552006-06-26 00:25:55 -07003677 if (p) {
Eric W. Biederman99f89552006-06-26 00:25:55 -07003678 stat->nlink += get_nr_threads(p);
Eric W. Biederman99f89552006-06-26 00:25:55 -07003679 put_task_struct(p);
Eric W. Biederman6e66b522006-06-26 00:25:47 -07003680 }
3681
3682 return 0;
3683}
Eric W. Biederman28a6d672006-10-02 02:17:05 -07003684
Arjan van de Venc5ef1c42007-02-12 00:55:40 -08003685static const struct inode_operations proc_task_inode_operations = {
Eric W. Biederman28a6d672006-10-02 02:17:05 -07003686 .lookup = proc_task_lookup,
3687 .getattr = proc_task_getattr,
3688 .setattr = proc_setattr,
Vasiliy Kulikov04996802012-01-10 15:11:31 -08003689 .permission = proc_pid_permission,
Eric W. Biederman28a6d672006-10-02 02:17:05 -07003690};
3691
Arjan van de Ven00977a52007-02-12 00:55:34 -08003692static const struct file_operations proc_task_operations = {
Eric W. Biederman28a6d672006-10-02 02:17:05 -07003693 .read = generic_read_dir,
Al Virof50752e2016-04-20 17:13:54 -04003694 .iterate_shared = proc_task_readdir,
3695 .llseek = generic_file_llseek,
Eric W. Biederman28a6d672006-10-02 02:17:05 -07003696};
Alexey Dobriyan1270dd82016-12-12 16:45:32 -08003697
3698void __init set_proc_pid_nlink(void)
3699{
3700 nlink_tid = pid_entry_nlink(tid_base_stuff, ARRAY_SIZE(tid_base_stuff));
3701 nlink_tgid = pid_entry_nlink(tgid_base_stuff, ARRAY_SIZE(tgid_base_stuff));
3702}