blob: 17a60e17a103d05e0c0750d4294312092bf844cd [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>
Kees Cookec7c20d2021-09-29 15:02:13 -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>
Andrei Vagin04a86822019-11-12 01:27:16 +000097#include <linux/time_namespace.h>
Chen Yue79f15a2020-01-15 17:28:51 +080098#include <linux/resctrl.h>
Ohhoon Kwonc2f273e2021-09-07 19:57:35 -070099#include <linux/cn_proc.h>
Connor O'Brien8bea10e2018-01-31 18:11:57 -0800100#include <linux/cpufreq_times.h>
KAMEZAWA Hiroyuki43d2b112012-01-10 15:08:09 -0800101#include <trace/events/oom.h>
Qais Yousef99aa5732023-07-11 22:37:40 +0000102#include <trace/hooks/sched.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -0700103#include "internal.h"
Cyrill Gorcunovfaf60af2012-08-23 14:43:24 +0400104#include "fd.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -0700105
Alexey Dobriyanac7f1062018-02-06 15:36:59 -0800106#include "../../lib/kstrtox.h"
107
Eric W. Biederman0f2fe202006-06-26 00:25:46 -0700108/* NOTE:
109 * Implementing inode permission operations in /proc is almost
110 * certainly an error. Permission checks need to happen during
111 * each system call not at open time. The reason is that most of
112 * what we wish to check for permissions in /proc varies at runtime.
113 *
114 * The classic example of a problem is opening file descriptors
115 * in /proc for a task before it execs a suid executable.
116 */
117
Alexey Dobriyanefb1a572018-02-06 15:37:24 -0800118static u8 nlink_tid __ro_after_init;
119static u8 nlink_tgid __ro_after_init;
Alexey Dobriyan1270dd82016-12-12 16:45:32 -0800120
Linus Torvalds1da177e2005-04-16 15:20:36 -0700121struct pid_entry {
Alexey Dobriyancedbcca2014-08-08 14:21:33 -0700122 const char *name;
Alexey Dobriyan623f5942016-12-12 16:45:08 -0800123 unsigned int len;
Al Virod161a132011-07-24 03:36:29 -0400124 umode_t mode;
Arjan van de Venc5ef1c42007-02-12 00:55:40 -0800125 const struct inode_operations *iop;
Arjan van de Ven00977a52007-02-12 00:55:34 -0800126 const struct file_operations *fop;
Eric W. Biederman20cdc892006-10-02 02:17:07 -0700127 union proc_op op;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700128};
129
Eric W. Biederman61a28782006-10-02 02:18:49 -0700130#define NOD(NAME, MODE, IOP, FOP, OP) { \
Eric W. Biederman20cdc892006-10-02 02:17:07 -0700131 .name = (NAME), \
Eric Dumazetc5141e62007-05-08 00:26:15 -0700132 .len = sizeof(NAME) - 1, \
Eric W. Biederman20cdc892006-10-02 02:17:07 -0700133 .mode = MODE, \
134 .iop = IOP, \
135 .fop = FOP, \
136 .op = OP, \
137}
138
Alexey Dobriyan631f9c12008-11-10 01:32:52 +0300139#define DIR(NAME, MODE, iops, fops) \
140 NOD(NAME, (S_IFDIR|(MODE)), &iops, &fops, {} )
141#define LNK(NAME, get_link) \
Eric W. Biederman61a28782006-10-02 02:18:49 -0700142 NOD(NAME, (S_IFLNK|S_IRWXUGO), \
Eric W. Biederman20cdc892006-10-02 02:17:07 -0700143 &proc_pid_link_inode_operations, NULL, \
Alexey Dobriyan631f9c12008-11-10 01:32:52 +0300144 { .proc_get_link = get_link } )
145#define REG(NAME, MODE, fops) \
146 NOD(NAME, (S_IFREG|(MODE)), NULL, &fops, {})
Alexey Dobriyan631f9c12008-11-10 01:32:52 +0300147#define ONE(NAME, MODE, show) \
Casey Schaufler6d9c9392018-09-21 17:16:59 -0700148 NOD(NAME, (S_IFREG|(MODE)), \
Eric W. Biedermanbe614082008-02-08 04:18:30 -0800149 NULL, &proc_single_file_operations, \
Alexey Dobriyan631f9c12008-11-10 01:32:52 +0300150 { .proc_show = show } )
Casey Schaufler6d9c9392018-09-21 17:16:59 -0700151#define ATTR(LSM, NAME, MODE) \
152 NOD(NAME, (S_IFREG|(MODE)), \
153 NULL, &proc_pid_attr_operations, \
154 { .lsm = LSM })
Linus Torvalds1da177e2005-04-16 15:20:36 -0700155
Vegard Nossumaed54172008-06-05 22:46:53 -0700156/*
157 * Count the number of hardlinks for the pid_entry table, excluding the .
158 * and .. links.
159 */
Alexey Dobriyan1270dd82016-12-12 16:45:32 -0800160static unsigned int __init pid_entry_nlink(const struct pid_entry *entries,
Vegard Nossumaed54172008-06-05 22:46:53 -0700161 unsigned int n)
162{
163 unsigned int i;
164 unsigned int count;
165
Alexey Dobriyan1270dd82016-12-12 16:45:32 -0800166 count = 2;
Vegard Nossumaed54172008-06-05 22:46:53 -0700167 for (i = 0; i < n; ++i) {
168 if (S_ISDIR(entries[i].mode))
169 ++count;
170 }
171
172 return count;
173}
174
Miklos Szeredif7ad3c62010-08-10 11:41:36 +0200175static int get_task_root(struct task_struct *task, struct path *root)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700176{
Hugh Dickins7c2c7d92009-03-28 23:21:27 +0000177 int result = -ENOENT;
178
Miklos Szeredi0494f6e2005-09-06 15:18:22 -0700179 task_lock(task);
Miklos Szeredif7ad3c62010-08-10 11:41:36 +0200180 if (task->fs) {
181 get_fs_root(task->fs, root);
Hugh Dickins7c2c7d92009-03-28 23:21:27 +0000182 result = 0;
183 }
Miklos Szeredi0494f6e2005-09-06 15:18:22 -0700184 task_unlock(task);
Hugh Dickins7c2c7d92009-03-28 23:21:27 +0000185 return result;
Miklos Szeredi0494f6e2005-09-06 15:18:22 -0700186}
187
Cyrill Gorcunov7773fbc2012-01-10 15:11:20 -0800188static int proc_cwd_link(struct dentry *dentry, struct path *path)
Miklos Szeredi0494f6e2005-09-06 15:18:22 -0700189{
David Howells2b0143b2015-03-17 22:25:59 +0000190 struct task_struct *task = get_proc_task(d_inode(dentry));
Miklos Szeredi0494f6e2005-09-06 15:18:22 -0700191 int result = -ENOENT;
Eric W. Biederman99f89552006-06-26 00:25:55 -0700192
193 if (task) {
Miklos Szeredif7ad3c62010-08-10 11:41:36 +0200194 task_lock(task);
195 if (task->fs) {
196 get_fs_pwd(task->fs, path);
197 result = 0;
198 }
199 task_unlock(task);
Eric W. Biederman99f89552006-06-26 00:25:55 -0700200 put_task_struct(task);
201 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700202 return result;
203}
204
Cyrill Gorcunov7773fbc2012-01-10 15:11:20 -0800205static int proc_root_link(struct dentry *dentry, struct path *path)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700206{
David Howells2b0143b2015-03-17 22:25:59 +0000207 struct task_struct *task = get_proc_task(d_inode(dentry));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700208 int result = -ENOENT;
Eric W. Biederman99f89552006-06-26 00:25:55 -0700209
210 if (task) {
Miklos Szeredif7ad3c62010-08-10 11:41:36 +0200211 result = get_task_root(task, path);
Eric W. Biederman99f89552006-06-26 00:25:55 -0700212 put_task_struct(task);
213 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700214 return result;
215}
216
Linus Torvaldsd26d0cd92019-07-13 14:27:14 -0700217/*
218 * If the user used setproctitle(), we just get the string from
219 * user space at arg_start, and limit it to a maximum of one page.
220 */
221static ssize_t get_mm_proctitle(struct mm_struct *mm, char __user *buf,
222 size_t count, unsigned long pos,
223 unsigned long arg_start)
224{
225 char *page;
226 int ret, got;
227
228 if (pos >= PAGE_SIZE)
229 return 0;
230
231 page = (char *)__get_free_page(GFP_KERNEL);
232 if (!page)
233 return -ENOMEM;
234
235 ret = 0;
236 got = access_remote_vm(mm, arg_start, page, PAGE_SIZE, FOLL_ANON);
237 if (got > 0) {
238 int len = strnlen(page, got);
239
240 /* Include the NUL character if it was found */
241 if (len < got)
242 len++;
243
244 if (len > pos) {
245 len -= pos;
246 if (len > count)
247 len = count;
248 len -= copy_to_user(buf, page+pos, len);
249 if (!len)
250 len = -EFAULT;
251 ret = len;
252 }
253 }
254 free_page((unsigned long)page);
255 return ret;
256}
257
Linus Torvaldse4b4e442018-05-17 13:04:17 -0700258static ssize_t get_mm_cmdline(struct mm_struct *mm, char __user *buf,
Linus Torvalds5ab82712018-05-17 15:17:33 -0700259 size_t count, loff_t *ppos)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700260{
Alexey Dobriyanc2c0bb42015-06-25 15:00:54 -0700261 unsigned long arg_start, arg_end, env_start, env_end;
Linus Torvalds5ab82712018-05-17 15:17:33 -0700262 unsigned long pos, len;
Linus Torvaldsd26d0cd92019-07-13 14:27:14 -0700263 char *page, c;
Alexey Dobriyanc2c0bb42015-06-25 15:00:54 -0700264
Alexey Dobriyanc2c0bb42015-06-25 15:00:54 -0700265 /* Check if process spawned far enough to have cmdline. */
Linus Torvaldse4b4e442018-05-17 13:04:17 -0700266 if (!mm->env_end)
267 return 0;
Alexey Dobriyanc2c0bb42015-06-25 15:00:54 -0700268
Yang Shi88aa7cc2018-06-07 17:05:28 -0700269 spin_lock(&mm->arg_lock);
Alexey Dobriyanc2c0bb42015-06-25 15:00:54 -0700270 arg_start = mm->arg_start;
271 arg_end = mm->arg_end;
272 env_start = mm->env_start;
273 env_end = mm->env_end;
Yang Shi88aa7cc2018-06-07 17:05:28 -0700274 spin_unlock(&mm->arg_lock);
Alexey Dobriyanc2c0bb42015-06-25 15:00:54 -0700275
Linus Torvalds5ab82712018-05-17 15:17:33 -0700276 if (arg_start >= arg_end)
277 return 0;
Alexey Dobriyan6a6cbe72018-06-07 17:09:59 -0700278
Alexey Dobriyan2ca66ff2014-08-08 14:21:41 -0700279 /*
Linus Torvaldsd26d0cd92019-07-13 14:27:14 -0700280 * We allow setproctitle() to overwrite the argument
281 * strings, and overflow past the original end. But
282 * only when it overflows into the environment area.
Alexey Dobriyan2ca66ff2014-08-08 14:21:41 -0700283 */
Linus Torvaldsd26d0cd92019-07-13 14:27:14 -0700284 if (env_start != arg_end || env_end < env_start)
Linus Torvalds5ab82712018-05-17 15:17:33 -0700285 env_start = env_end = arg_end;
Linus Torvaldsd26d0cd92019-07-13 14:27:14 -0700286 len = env_end - arg_start;
Linus Torvaldsf5b65342018-06-20 09:47:20 +0900287
Linus Torvalds5ab82712018-05-17 15:17:33 -0700288 /* We're not going to care if "*ppos" has high bits set */
Linus Torvaldsd26d0cd92019-07-13 14:27:14 -0700289 pos = *ppos;
290 if (pos >= len)
291 return 0;
292 if (count > len - pos)
293 count = len - pos;
294 if (!count)
Linus Torvalds5ab82712018-05-17 15:17:33 -0700295 return 0;
Alexey Dobriyanc2c0bb42015-06-25 15:00:54 -0700296
Linus Torvaldsd26d0cd92019-07-13 14:27:14 -0700297 /*
298 * Magical special case: if the argv[] end byte is not
299 * zero, the user has overwritten it with setproctitle(3).
300 *
301 * Possible future enhancement: do this only once when
302 * pos is 0, and set a flag in the 'struct file'.
303 */
304 if (access_remote_vm(mm, arg_end-1, &c, 1, FOLL_ANON) == 1 && c)
305 return get_mm_proctitle(mm, buf, count, pos, arg_start);
306
307 /*
308 * For the non-setproctitle() case we limit things strictly
309 * to the [arg_start, arg_end[ range.
310 */
311 pos += arg_start;
Linus Torvalds3d712542019-07-13 13:40:13 -0700312 if (pos < arg_start || pos >= arg_end)
Alexey Dobriyanc2c0bb42015-06-25 15:00:54 -0700313 return 0;
Linus Torvalds3d712542019-07-13 13:40:13 -0700314 if (count > arg_end - pos)
315 count = arg_end - pos;
Alexey Dobriyanc2c0bb42015-06-25 15:00:54 -0700316
Linus Torvalds5ab82712018-05-17 15:17:33 -0700317 page = (char *)__get_free_page(GFP_KERNEL);
318 if (!page)
319 return -ENOMEM;
Alexey Dobriyanc2c0bb42015-06-25 15:00:54 -0700320
Linus Torvalds5ab82712018-05-17 15:17:33 -0700321 len = 0;
322 while (count) {
323 int got;
324 size_t size = min_t(size_t, PAGE_SIZE, count);
Alexey Dobriyanc2c0bb42015-06-25 15:00:54 -0700325
Linus Torvalds3d712542019-07-13 13:40:13 -0700326 got = access_remote_vm(mm, pos, page, size, FOLL_ANON);
327 if (got <= 0)
Linus Torvalds5ab82712018-05-17 15:17:33 -0700328 break;
Linus Torvalds3d712542019-07-13 13:40:13 -0700329 got -= copy_to_user(buf, page, got);
Linus Torvalds5ab82712018-05-17 15:17:33 -0700330 if (unlikely(!got)) {
331 if (!len)
332 len = -EFAULT;
333 break;
Alexey Dobriyanc2c0bb42015-06-25 15:00:54 -0700334 }
Linus Torvalds5ab82712018-05-17 15:17:33 -0700335 pos += got;
336 buf += got;
337 len += got;
338 count -= got;
Alexey Dobriyan3cb4e162018-06-07 17:10:02 -0700339 }
340
Alexey Dobriyanc2c0bb42015-06-25 15:00:54 -0700341 free_page((unsigned long)page);
Linus Torvalds5ab82712018-05-17 15:17:33 -0700342 return len;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700343}
344
Linus Torvaldse4b4e442018-05-17 13:04:17 -0700345static ssize_t get_task_cmdline(struct task_struct *tsk, char __user *buf,
346 size_t count, loff_t *pos)
347{
348 struct mm_struct *mm;
Qais Yousef99aa5732023-07-11 22:37:40 +0000349 bool prio_inherited = false;
350 int saved_prio;
Linus Torvaldse4b4e442018-05-17 13:04:17 -0700351 ssize_t ret;
352
353 mm = get_task_mm(tsk);
354 if (!mm)
355 return 0;
356
Qais Yousef99aa5732023-07-11 22:37:40 +0000357 /*
358 * access_remote_vm() holds the hot mmap_sem lock which can cause the
359 * task for which we read cmdline etc for by some debug deamon to slow
360 * down and suffer a performance hit. Especially if the reader task has
361 * a low nice value.
362 */
363 trace_android_vh_prio_inheritance(tsk, &saved_prio, &prio_inherited);
Linus Torvaldse4b4e442018-05-17 13:04:17 -0700364 ret = get_mm_cmdline(mm, buf, count, pos);
Qais Yousef99aa5732023-07-11 22:37:40 +0000365 if (prio_inherited)
366 trace_android_vh_prio_restore(saved_prio);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700367 mmput(mm);
Linus Torvaldse4b4e442018-05-17 13:04:17 -0700368 return ret;
369}
370
371static ssize_t proc_pid_cmdline_read(struct file *file, char __user *buf,
372 size_t count, loff_t *pos)
373{
374 struct task_struct *tsk;
375 ssize_t ret;
376
377 BUG_ON(*pos < 0);
378
379 tsk = get_proc_task(file_inode(file));
380 if (!tsk)
381 return -ESRCH;
382 ret = get_task_cmdline(tsk, buf, count, pos);
383 put_task_struct(tsk);
384 if (ret > 0)
385 *pos += ret;
386 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700387}
388
Alexey Dobriyanc2c0bb42015-06-25 15:00:54 -0700389static const struct file_operations proc_pid_cmdline_ops = {
390 .read = proc_pid_cmdline_read,
391 .llseek = generic_file_llseek,
392};
393
Linus Torvalds1da177e2005-04-16 15:20:36 -0700394#ifdef CONFIG_KALLSYMS
395/*
396 * Provides a wchan file via kallsyms in a proper one-value-per-file format.
397 * Returns the resolved symbol. If that fails, simply return the address.
398 */
Alexey Dobriyanedfcd602014-08-08 14:21:44 -0700399static int proc_pid_wchan(struct seq_file *m, struct pid_namespace *ns,
400 struct pid *pid, struct task_struct *task)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700401{
Alexey Dobriyanffb45122007-05-08 00:28:41 -0700402 unsigned long wchan;
Kees Cookec7c20d2021-09-29 15:02:13 -0700403 char symname[KSYM_NAME_LEN];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700404
Kees Cookec7c20d2021-09-29 15:02:13 -0700405 if (!ptrace_may_access(task, PTRACE_MODE_READ_FSCREDS))
406 goto print0;
Alexey Dobriyan24b2ec22018-04-10 16:31:30 -0700407
Kees Cookec7c20d2021-09-29 15:02:13 -0700408 wchan = get_wchan(task);
409 if (wchan && !lookup_symbol_name(wchan, symname)) {
410 seq_puts(m, symname);
411 return 0;
412 }
Joe Perches25ce3192015-04-15 16:18:17 -0700413
Kees Cookec7c20d2021-09-29 15:02:13 -0700414print0:
415 seq_putc(m, '0');
Joe Perches25ce3192015-04-15 16:18:17 -0700416 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700417}
418#endif /* CONFIG_KALLSYMS */
419
Al Viroa9712bc2011-03-23 15:52:50 -0400420static int lock_trace(struct task_struct *task)
421{
Eric W. Biedermanf7cfd872020-12-03 14:12:00 -0600422 int err = down_read_killable(&task->signal->exec_update_lock);
Al Viroa9712bc2011-03-23 15:52:50 -0400423 if (err)
424 return err;
Jann Horncaaee622016-01-20 15:00:04 -0800425 if (!ptrace_may_access(task, PTRACE_MODE_ATTACH_FSCREDS)) {
Eric W. Biedermanf7cfd872020-12-03 14:12:00 -0600426 up_read(&task->signal->exec_update_lock);
Al Viroa9712bc2011-03-23 15:52:50 -0400427 return -EPERM;
428 }
429 return 0;
430}
431
432static void unlock_trace(struct task_struct *task)
433{
Eric W. Biedermanf7cfd872020-12-03 14:12:00 -0600434 up_read(&task->signal->exec_update_lock);
Al Viroa9712bc2011-03-23 15:52:50 -0400435}
436
Ken Chen2ec220e2008-11-10 11:26:08 +0300437#ifdef CONFIG_STACKTRACE
438
439#define MAX_STACK_TRACE_DEPTH 64
440
441static int proc_pid_stack(struct seq_file *m, struct pid_namespace *ns,
442 struct pid *pid, struct task_struct *task)
443{
Ken Chen2ec220e2008-11-10 11:26:08 +0300444 unsigned long *entries;
Al Viroa9712bc2011-03-23 15:52:50 -0400445 int err;
Ken Chen2ec220e2008-11-10 11:26:08 +0300446
Jann Hornf8a00ce2018-10-05 15:51:58 -0700447 /*
448 * The ability to racily run the kernel stack unwinder on a running task
449 * and then observe the unwinder output is scary; while it is useful for
450 * debugging kernel issues, it can also allow an attacker to leak kernel
451 * stack contents.
452 * Doing this in a manner that is at least safe from races would require
453 * some work to ensure that the remote task can not be scheduled; and
454 * even then, this would still expose the unwinder as local attack
455 * surface.
456 * Therefore, this interface is restricted to root.
457 */
458 if (!file_ns_capable(m->file, &init_user_ns, CAP_SYS_ADMIN))
459 return -EACCES;
460
Kees Cook6da2ec52018-06-12 13:55:00 -0700461 entries = kmalloc_array(MAX_STACK_TRACE_DEPTH, sizeof(*entries),
462 GFP_KERNEL);
Ken Chen2ec220e2008-11-10 11:26:08 +0300463 if (!entries)
464 return -ENOMEM;
465
Al Viroa9712bc2011-03-23 15:52:50 -0400466 err = lock_trace(task);
467 if (!err) {
Thomas Gleixnere988e5e2019-04-25 11:44:58 +0200468 unsigned int i, nr_entries;
Alexey Dobriyan5d008fb2018-06-07 17:10:17 -0700469
Thomas Gleixnere988e5e2019-04-25 11:44:58 +0200470 nr_entries = stack_trace_save_tsk(task, entries,
471 MAX_STACK_TRACE_DEPTH, 0);
Al Viroa9712bc2011-03-23 15:52:50 -0400472
Thomas Gleixnere988e5e2019-04-25 11:44:58 +0200473 for (i = 0; i < nr_entries; i++) {
Linus Torvalds8f5abe82017-11-27 16:45:56 -0800474 seq_printf(m, "[<0>] %pB\n", (void *)entries[i]);
Al Viroa9712bc2011-03-23 15:52:50 -0400475 }
Thomas Gleixnere988e5e2019-04-25 11:44:58 +0200476
Al Viroa9712bc2011-03-23 15:52:50 -0400477 unlock_trace(task);
Ken Chen2ec220e2008-11-10 11:26:08 +0300478 }
479 kfree(entries);
480
Al Viroa9712bc2011-03-23 15:52:50 -0400481 return err;
Ken Chen2ec220e2008-11-10 11:26:08 +0300482}
483#endif
484
Naveen N. Rao5968cec2015-06-30 14:36:03 +0530485#ifdef CONFIG_SCHED_INFO
Linus Torvalds1da177e2005-04-16 15:20:36 -0700486/*
487 * Provides /proc/PID/schedstat
488 */
Alexey Dobriyanf6e826c2014-08-08 14:21:46 -0700489static int proc_pid_schedstat(struct seq_file *m, struct pid_namespace *ns,
490 struct pid *pid, struct task_struct *task)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700491{
Naveen N. Rao5968cec2015-06-30 14:36:03 +0530492 if (unlikely(!sched_info_on()))
Alexey Dobriyan08b55772019-03-05 15:50:35 -0800493 seq_puts(m, "0 0 0\n");
Naveen N. Rao5968cec2015-06-30 14:36:03 +0530494 else
495 seq_printf(m, "%llu %llu %lu\n",
Joe Perches25ce3192015-04-15 16:18:17 -0700496 (unsigned long long)task->se.sum_exec_runtime,
497 (unsigned long long)task->sched_info.run_delay,
498 task->sched_info.pcount);
499
500 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700501}
502#endif
503
Arjan van de Ven97455122008-01-25 21:08:34 +0100504#ifdef CONFIG_LATENCYTOP
505static int lstats_show_proc(struct seq_file *m, void *v)
506{
507 int i;
Hiroshi Shimamoto13d77c32008-02-20 16:53:29 -0800508 struct inode *inode = m->private;
509 struct task_struct *task = get_proc_task(inode);
Arjan van de Ven97455122008-01-25 21:08:34 +0100510
Hiroshi Shimamoto13d77c32008-02-20 16:53:29 -0800511 if (!task)
512 return -ESRCH;
513 seq_puts(m, "Latency Top version : v0.1\n");
Alexey Dobriyanf6d2f582018-08-21 21:54:34 -0700514 for (i = 0; i < LT_SAVECOUNT; i++) {
Joe Perches34e49d42011-01-12 17:00:30 -0800515 struct latency_record *lr = &task->latency_record[i];
516 if (lr->backtrace[0]) {
Arjan van de Ven97455122008-01-25 21:08:34 +0100517 int q;
Joe Perches34e49d42011-01-12 17:00:30 -0800518 seq_printf(m, "%i %li %li",
519 lr->count, lr->time, lr->max);
Arjan van de Ven97455122008-01-25 21:08:34 +0100520 for (q = 0; q < LT_BACKTRACEDEPTH; q++) {
Joe Perches34e49d42011-01-12 17:00:30 -0800521 unsigned long bt = lr->backtrace[q];
Thomas Gleixneraccddc42019-04-10 12:28:08 +0200522
Joe Perches34e49d42011-01-12 17:00:30 -0800523 if (!bt)
Arjan van de Ven97455122008-01-25 21:08:34 +0100524 break;
Joe Perches34e49d42011-01-12 17:00:30 -0800525 seq_printf(m, " %ps", (void *)bt);
Arjan van de Ven97455122008-01-25 21:08:34 +0100526 }
Alexey Dobriyan9d6de122011-01-12 17:00:32 -0800527 seq_putc(m, '\n');
Arjan van de Ven97455122008-01-25 21:08:34 +0100528 }
529
530 }
Hiroshi Shimamoto13d77c32008-02-20 16:53:29 -0800531 put_task_struct(task);
Arjan van de Ven97455122008-01-25 21:08:34 +0100532 return 0;
533}
534
535static int lstats_open(struct inode *inode, struct file *file)
536{
Hiroshi Shimamoto13d77c32008-02-20 16:53:29 -0800537 return single_open(file, lstats_show_proc, inode);
Hiroshi Shimamotod6643d12008-02-14 10:27:00 -0800538}
539
Arjan van de Ven97455122008-01-25 21:08:34 +0100540static ssize_t lstats_write(struct file *file, const char __user *buf,
541 size_t count, loff_t *offs)
542{
Al Viro496ad9a2013-01-23 17:07:38 -0500543 struct task_struct *task = get_proc_task(file_inode(file));
Arjan van de Ven97455122008-01-25 21:08:34 +0100544
Hiroshi Shimamoto13d77c32008-02-20 16:53:29 -0800545 if (!task)
546 return -ESRCH;
Lin Fenge02c9b02019-05-14 15:42:34 -0700547 clear_tsk_latency_tracing(task);
Hiroshi Shimamoto13d77c32008-02-20 16:53:29 -0800548 put_task_struct(task);
Arjan van de Ven97455122008-01-25 21:08:34 +0100549
550 return count;
551}
552
553static const struct file_operations proc_lstats_operations = {
554 .open = lstats_open,
555 .read = seq_read,
556 .write = lstats_write,
557 .llseek = seq_lseek,
Hiroshi Shimamoto13d77c32008-02-20 16:53:29 -0800558 .release = single_release,
Arjan van de Ven97455122008-01-25 21:08:34 +0100559};
560
561#endif
562
Alexey Dobriyan6ba51e32014-08-08 14:21:48 -0700563static int proc_oom_score(struct seq_file *m, struct pid_namespace *ns,
564 struct pid *pid, struct task_struct *task)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700565{
Arun KSca79b0c2018-12-28 00:34:29 -0800566 unsigned long totalpages = totalram_pages() + total_swap_pages;
Oleg Nesterovb95c35e2010-04-01 15:13:57 +0200567 unsigned long points = 0;
Yafang Shao9066e5c2020-08-11 18:31:22 -0700568 long badness;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700569
Yafang Shao9066e5c2020-08-11 18:31:22 -0700570 badness = oom_badness(task, totalpages);
571 /*
572 * Special case OOM_SCORE_ADJ_MIN for all others scale the
573 * badness value into [0, 2000] range which we have been
574 * exporting for a long time so userspace might depend on it.
575 */
576 if (badness != LONG_MIN)
577 points = (1000 + badness * 1000 / (long)totalpages) * 2 / 3;
578
Joe Perches25ce3192015-04-15 16:18:17 -0700579 seq_printf(m, "%lu\n", points);
580
581 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700582}
583
Neil Hormand85f50d2007-10-18 23:40:37 -0700584struct limit_names {
Alexey Dobriyancedbcca2014-08-08 14:21:33 -0700585 const char *name;
586 const char *unit;
Neil Hormand85f50d2007-10-18 23:40:37 -0700587};
588
589static const struct limit_names lnames[RLIM_NLIMITS] = {
Kees Cookcff4edb2009-09-22 16:45:32 -0700590 [RLIMIT_CPU] = {"Max cpu time", "seconds"},
Neil Hormand85f50d2007-10-18 23:40:37 -0700591 [RLIMIT_FSIZE] = {"Max file size", "bytes"},
592 [RLIMIT_DATA] = {"Max data size", "bytes"},
593 [RLIMIT_STACK] = {"Max stack size", "bytes"},
594 [RLIMIT_CORE] = {"Max core file size", "bytes"},
595 [RLIMIT_RSS] = {"Max resident set", "bytes"},
596 [RLIMIT_NPROC] = {"Max processes", "processes"},
597 [RLIMIT_NOFILE] = {"Max open files", "files"},
598 [RLIMIT_MEMLOCK] = {"Max locked memory", "bytes"},
599 [RLIMIT_AS] = {"Max address space", "bytes"},
600 [RLIMIT_LOCKS] = {"Max file locks", "locks"},
601 [RLIMIT_SIGPENDING] = {"Max pending signals", "signals"},
602 [RLIMIT_MSGQUEUE] = {"Max msgqueue size", "bytes"},
603 [RLIMIT_NICE] = {"Max nice priority", NULL},
604 [RLIMIT_RTPRIO] = {"Max realtime priority", NULL},
Eugene Teo88081172008-02-23 15:23:52 -0800605 [RLIMIT_RTTIME] = {"Max realtime timeout", "us"},
Neil Hormand85f50d2007-10-18 23:40:37 -0700606};
607
608/* Display limits for a process */
Alexey Dobriyan1c963eb2014-08-08 14:21:37 -0700609static int proc_pid_limits(struct seq_file *m, struct pid_namespace *ns,
610 struct pid *pid, struct task_struct *task)
Neil Hormand85f50d2007-10-18 23:40:37 -0700611{
612 unsigned int i;
Neil Hormand85f50d2007-10-18 23:40:37 -0700613 unsigned long flags;
Neil Hormand85f50d2007-10-18 23:40:37 -0700614
615 struct rlimit rlim[RLIM_NLIMITS];
616
Lai Jiangshana6bebbc2008-10-05 00:51:15 +0400617 if (!lock_task_sighand(task, &flags))
Neil Hormand85f50d2007-10-18 23:40:37 -0700618 return 0;
Neil Hormand85f50d2007-10-18 23:40:37 -0700619 memcpy(rlim, task->signal->rlim, sizeof(struct rlimit) * RLIM_NLIMITS);
620 unlock_task_sighand(task, &flags);
Neil Hormand85f50d2007-10-18 23:40:37 -0700621
622 /*
623 * print the file header
624 */
Alexey Dobriyanafe922c2019-01-03 15:26:09 -0800625 seq_puts(m, "Limit "
626 "Soft Limit "
627 "Hard Limit "
628 "Units \n");
Neil Hormand85f50d2007-10-18 23:40:37 -0700629
630 for (i = 0; i < RLIM_NLIMITS; i++) {
631 if (rlim[i].rlim_cur == RLIM_INFINITY)
Alexey Dobriyan1c963eb2014-08-08 14:21:37 -0700632 seq_printf(m, "%-25s %-20s ",
Joe Perches25ce3192015-04-15 16:18:17 -0700633 lnames[i].name, "unlimited");
Neil Hormand85f50d2007-10-18 23:40:37 -0700634 else
Alexey Dobriyan1c963eb2014-08-08 14:21:37 -0700635 seq_printf(m, "%-25s %-20lu ",
Joe Perches25ce3192015-04-15 16:18:17 -0700636 lnames[i].name, rlim[i].rlim_cur);
Neil Hormand85f50d2007-10-18 23:40:37 -0700637
638 if (rlim[i].rlim_max == RLIM_INFINITY)
Alexey Dobriyan1c963eb2014-08-08 14:21:37 -0700639 seq_printf(m, "%-20s ", "unlimited");
Neil Hormand85f50d2007-10-18 23:40:37 -0700640 else
Alexey Dobriyan1c963eb2014-08-08 14:21:37 -0700641 seq_printf(m, "%-20lu ", rlim[i].rlim_max);
Neil Hormand85f50d2007-10-18 23:40:37 -0700642
643 if (lnames[i].unit)
Alexey Dobriyan1c963eb2014-08-08 14:21:37 -0700644 seq_printf(m, "%-10s\n", lnames[i].unit);
Neil Hormand85f50d2007-10-18 23:40:37 -0700645 else
Alexey Dobriyan1c963eb2014-08-08 14:21:37 -0700646 seq_putc(m, '\n');
Neil Hormand85f50d2007-10-18 23:40:37 -0700647 }
648
Alexey Dobriyan1c963eb2014-08-08 14:21:37 -0700649 return 0;
Neil Hormand85f50d2007-10-18 23:40:37 -0700650}
651
Roland McGrathebcb6732008-07-25 19:46:00 -0700652#ifdef CONFIG_HAVE_ARCH_TRACEHOOK
Alexey Dobriyan09d93bd2014-08-08 14:21:39 -0700653static int proc_pid_syscall(struct seq_file *m, struct pid_namespace *ns,
654 struct pid *pid, struct task_struct *task)
Roland McGrathebcb6732008-07-25 19:46:00 -0700655{
Steven Rostedt (Red Hat)631b7ab2016-11-07 16:26:35 -0500656 struct syscall_info info;
657 u64 *args = &info.data.args[0];
Joe Perches25ce3192015-04-15 16:18:17 -0700658 int res;
659
660 res = lock_trace(task);
Al Viroa9712bc2011-03-23 15:52:50 -0400661 if (res)
662 return res;
Roland McGrathebcb6732008-07-25 19:46:00 -0700663
Steven Rostedt (Red Hat)631b7ab2016-11-07 16:26:35 -0500664 if (task_current_syscall(task, &info))
Alexey Dobriyan09d93bd2014-08-08 14:21:39 -0700665 seq_puts(m, "running\n");
Steven Rostedt (Red Hat)631b7ab2016-11-07 16:26:35 -0500666 else if (info.data.nr < 0)
667 seq_printf(m, "%d 0x%llx 0x%llx\n",
668 info.data.nr, info.sp, info.data.instruction_pointer);
Al Viroa9712bc2011-03-23 15:52:50 -0400669 else
Alexey Dobriyan09d93bd2014-08-08 14:21:39 -0700670 seq_printf(m,
Steven Rostedt (Red Hat)631b7ab2016-11-07 16:26:35 -0500671 "%d 0x%llx 0x%llx 0x%llx 0x%llx 0x%llx 0x%llx 0x%llx 0x%llx\n",
672 info.data.nr,
Roland McGrathebcb6732008-07-25 19:46:00 -0700673 args[0], args[1], args[2], args[3], args[4], args[5],
Steven Rostedt (Red Hat)631b7ab2016-11-07 16:26:35 -0500674 info.sp, info.data.instruction_pointer);
Al Viroa9712bc2011-03-23 15:52:50 -0400675 unlock_trace(task);
Joe Perches25ce3192015-04-15 16:18:17 -0700676
677 return 0;
Roland McGrathebcb6732008-07-25 19:46:00 -0700678}
679#endif /* CONFIG_HAVE_ARCH_TRACEHOOK */
680
Linus Torvalds1da177e2005-04-16 15:20:36 -0700681/************************************************************************/
682/* Here the fs part begins */
683/************************************************************************/
684
685/* permission checks */
Eric W. Biederman778c1142006-06-26 00:25:58 -0700686static int proc_fd_access_allowed(struct inode *inode)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700687{
Eric W. Biederman778c1142006-06-26 00:25:58 -0700688 struct task_struct *task;
689 int allowed = 0;
Eric W. Biedermandf26c402006-06-26 00:25:59 -0700690 /* Allow access to a task's file descriptors if it is us or we
691 * may use ptrace attach to the process and find out that
692 * information.
Eric W. Biederman778c1142006-06-26 00:25:58 -0700693 */
694 task = get_proc_task(inode);
Eric W. Biedermandf26c402006-06-26 00:25:59 -0700695 if (task) {
Jann Horncaaee622016-01-20 15:00:04 -0800696 allowed = ptrace_may_access(task, PTRACE_MODE_READ_FSCREDS);
Eric W. Biederman778c1142006-06-26 00:25:58 -0700697 put_task_struct(task);
Eric W. Biedermandf26c402006-06-26 00:25:59 -0700698 }
Eric W. Biederman778c1142006-06-26 00:25:58 -0700699 return allowed;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700700}
701
Christian Brauner549c7292021-01-21 14:19:43 +0100702int proc_setattr(struct user_namespace *mnt_userns, struct dentry *dentry,
703 struct iattr *attr)
Linus Torvalds6d76fa52006-07-15 12:26:45 -0700704{
705 int error;
David Howells2b0143b2015-03-17 22:25:59 +0000706 struct inode *inode = d_inode(dentry);
Linus Torvalds6d76fa52006-07-15 12:26:45 -0700707
708 if (attr->ia_valid & ATTR_MODE)
709 return -EPERM;
710
Christian Brauner2f221d62021-01-21 14:19:26 +0100711 error = setattr_prepare(&init_user_ns, dentry, attr);
Christoph Hellwig10257742010-06-04 11:30:02 +0200712 if (error)
713 return error;
714
Christian Brauner2f221d62021-01-21 14:19:26 +0100715 setattr_copy(&init_user_ns, inode, attr);
Christoph Hellwig10257742010-06-04 11:30:02 +0200716 mark_inode_dirty(inode);
717 return 0;
Linus Torvalds6d76fa52006-07-15 12:26:45 -0700718}
719
Vasiliy Kulikov04996802012-01-10 15:11:31 -0800720/*
721 * May current process learn task's sched/cmdline info (for hide_pid_min=1)
722 * or euid/egid (for hide_pid_min=2)?
723 */
Alexey Gladkovfa10fed2020-04-19 16:10:52 +0200724static bool has_pid_permissions(struct proc_fs_info *fs_info,
Vasiliy Kulikov04996802012-01-10 15:11:31 -0800725 struct task_struct *task,
Alexey Gladkove61bb8b2020-04-19 16:10:57 +0200726 enum proc_hidepid hide_pid_min)
Vasiliy Kulikov04996802012-01-10 15:11:31 -0800727{
Alexey Gladkov24a71ce2020-04-19 16:10:53 +0200728 /*
729 * If 'hidpid' mount option is set force a ptrace check,
730 * we indicate that we are using a filesystem syscall
731 * by passing PTRACE_MODE_READ_FSCREDS
732 */
733 if (fs_info->hide_pid == HIDEPID_NOT_PTRACEABLE)
734 return ptrace_may_access(task, PTRACE_MODE_READ_FSCREDS);
735
Alexey Gladkovfa10fed2020-04-19 16:10:52 +0200736 if (fs_info->hide_pid < hide_pid_min)
Vasiliy Kulikov04996802012-01-10 15:11:31 -0800737 return true;
Alexey Gladkovfa10fed2020-04-19 16:10:52 +0200738 if (in_group_p(fs_info->pid_gid))
Vasiliy Kulikov04996802012-01-10 15:11:31 -0800739 return true;
Jann Horncaaee622016-01-20 15:00:04 -0800740 return ptrace_may_access(task, PTRACE_MODE_READ_FSCREDS);
Vasiliy Kulikov04996802012-01-10 15:11:31 -0800741}
742
743
Christian Brauner549c7292021-01-21 14:19:43 +0100744static int proc_pid_permission(struct user_namespace *mnt_userns,
745 struct inode *inode, int mask)
Vasiliy Kulikov04996802012-01-10 15:11:31 -0800746{
Alexey Gladkovfa10fed2020-04-19 16:10:52 +0200747 struct proc_fs_info *fs_info = proc_sb_info(inode->i_sb);
Vasiliy Kulikov04996802012-01-10 15:11:31 -0800748 struct task_struct *task;
749 bool has_perms;
750
751 task = get_proc_task(inode);
Xiaotian Fenga2ef9902012-01-12 17:17:08 -0800752 if (!task)
753 return -ESRCH;
Alexey Gladkovfa10fed2020-04-19 16:10:52 +0200754 has_perms = has_pid_permissions(fs_info, task, HIDEPID_NO_ACCESS);
Vasiliy Kulikov04996802012-01-10 15:11:31 -0800755 put_task_struct(task);
756
757 if (!has_perms) {
Alexey Gladkovfa10fed2020-04-19 16:10:52 +0200758 if (fs_info->hide_pid == HIDEPID_INVISIBLE) {
Vasiliy Kulikov04996802012-01-10 15:11:31 -0800759 /*
760 * Let's make getdents(), stat(), and open()
761 * consistent with each other. If a process
762 * may not stat() a file, it shouldn't be seen
763 * in procfs at all.
764 */
765 return -ENOENT;
766 }
767
768 return -EPERM;
769 }
Christian Brauner47291ba2021-01-21 14:19:24 +0100770 return generic_permission(&init_user_ns, inode, mask);
Vasiliy Kulikov04996802012-01-10 15:11:31 -0800771}
772
773
774
Arjan van de Venc5ef1c42007-02-12 00:55:40 -0800775static const struct inode_operations proc_def_inode_operations = {
Linus Torvalds6d76fa52006-07-15 12:26:45 -0700776 .setattr = proc_setattr,
777};
778
Eric W. Biedermanbe614082008-02-08 04:18:30 -0800779static int proc_single_show(struct seq_file *m, void *v)
780{
781 struct inode *inode = m->private;
Alexey Gladkov9d78ede2020-05-18 20:07:38 +0200782 struct pid_namespace *ns = proc_pid_ns(inode->i_sb);
Christoph Hellwig76f668b2018-05-16 07:19:01 +0200783 struct pid *pid = proc_pid(inode);
Eric W. Biedermanbe614082008-02-08 04:18:30 -0800784 struct task_struct *task;
785 int ret;
786
Eric W. Biedermanbe614082008-02-08 04:18:30 -0800787 task = get_pid_task(pid, PIDTYPE_PID);
788 if (!task)
789 return -ESRCH;
790
791 ret = PROC_I(inode)->op.proc_show(m, ns, pid, task);
792
793 put_task_struct(task);
794 return ret;
795}
796
797static int proc_single_open(struct inode *inode, struct file *filp)
798{
Jovi Zhangc6a34052011-01-12 17:00:34 -0800799 return single_open(filp, proc_single_show, inode);
Eric W. Biedermanbe614082008-02-08 04:18:30 -0800800}
801
802static const struct file_operations proc_single_file_operations = {
803 .open = proc_single_open,
804 .read = seq_read,
805 .llseek = seq_lseek,
806 .release = single_release,
807};
808
Oleg Nesterov5381e162014-10-09 15:25:24 -0700809
810struct mm_struct *proc_mem_open(struct inode *inode, unsigned int mode)
811{
812 struct task_struct *task = get_proc_task(inode);
813 struct mm_struct *mm = ERR_PTR(-ESRCH);
814
815 if (task) {
Jann Horncaaee622016-01-20 15:00:04 -0800816 mm = mm_access(task, mode | PTRACE_MODE_FSCREDS);
Oleg Nesterov5381e162014-10-09 15:25:24 -0700817 put_task_struct(task);
818
819 if (!IS_ERR_OR_NULL(mm)) {
820 /* ensure this mm_struct can't be freed */
Vegard Nossumf1f10072017-02-27 14:30:07 -0800821 mmgrab(mm);
Oleg Nesterov5381e162014-10-09 15:25:24 -0700822 /* but do not pin its memory */
823 mmput(mm);
824 }
825 }
826
827 return mm;
828}
829
Cong Wangb409e572012-05-31 16:26:17 -0700830static int __mem_open(struct inode *inode, struct file *file, unsigned int mode)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700831{
Oleg Nesterov5381e162014-10-09 15:25:24 -0700832 struct mm_struct *mm = proc_mem_open(inode, mode);
Linus Torvaldse2683372012-01-17 15:21:19 -0800833
834 if (IS_ERR(mm))
835 return PTR_ERR(mm);
836
Linus Torvaldse2683372012-01-17 15:21:19 -0800837 file->private_data = mm;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700838 return 0;
839}
840
Cong Wangb409e572012-05-31 16:26:17 -0700841static int mem_open(struct inode *inode, struct file *file)
842{
Djalal Harounibc452b42012-07-30 14:42:28 -0700843 int ret = __mem_open(inode, file, PTRACE_MODE_ATTACH);
844
845 /* OK to pass negative loff_t, we can catch out-of-range */
846 file->f_mode |= FMODE_UNSIGNED_OFFSET;
847
848 return ret;
Cong Wangb409e572012-05-31 16:26:17 -0700849}
850
Oleg Nesterov572d34b2012-01-31 17:14:54 +0100851static ssize_t mem_rw(struct file *file, char __user *buf,
852 size_t count, loff_t *ppos, int write)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700853{
Linus Torvaldse2683372012-01-17 15:21:19 -0800854 struct mm_struct *mm = file->private_data;
Oleg Nesterov572d34b2012-01-31 17:14:54 +0100855 unsigned long addr = *ppos;
856 ssize_t copied;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700857 char *page;
Linus Torvalds272ddc82016-10-24 19:00:44 -0700858 unsigned int flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700859
Linus Torvaldse2683372012-01-17 15:21:19 -0800860 if (!mm)
861 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700862
Michal Hocko0ee931c2017-09-13 16:28:29 -0700863 page = (char *)__get_free_page(GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700864 if (!page)
Linus Torvaldse2683372012-01-17 15:21:19 -0800865 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700866
Frederik Deweerdtf7ca54f2006-09-29 02:01:02 -0700867 copied = 0;
Vegard Nossum388f7932017-02-27 14:30:13 -0800868 if (!mmget_not_zero(mm))
Oleg Nesterov6d08f2c2012-01-31 17:15:11 +0100869 goto free;
870
Linus Torvaldsf511c0b2017-05-30 12:38:59 -0700871 flags = FOLL_FORCE | (write ? FOLL_WRITE : 0);
Lorenzo Stoakes6347e8d2016-10-13 01:20:19 +0100872
Linus Torvalds1da177e2005-04-16 15:20:36 -0700873 while (count > 0) {
Marcelo Henrique Cerrid2386922021-06-30 18:54:38 -0700874 size_t this_len = min_t(size_t, count, PAGE_SIZE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700875
Oleg Nesterov572d34b2012-01-31 17:14:54 +0100876 if (write && copy_from_user(page, buf, this_len)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700877 copied = -EFAULT;
878 break;
879 }
Oleg Nesterov572d34b2012-01-31 17:14:54 +0100880
Lorenzo Stoakes6347e8d2016-10-13 01:20:19 +0100881 this_len = access_remote_vm(mm, addr, page, this_len, flags);
Oleg Nesterov572d34b2012-01-31 17:14:54 +0100882 if (!this_len) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700883 if (!copied)
884 copied = -EIO;
885 break;
886 }
Oleg Nesterov572d34b2012-01-31 17:14:54 +0100887
888 if (!write && copy_to_user(buf, page, this_len)) {
889 copied = -EFAULT;
890 break;
891 }
892
893 buf += this_len;
894 addr += this_len;
895 copied += this_len;
896 count -= this_len;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700897 }
Oleg Nesterov572d34b2012-01-31 17:14:54 +0100898 *ppos = addr;
KOSAKI Motohiro30cd8902011-05-26 16:25:52 -0700899
Oleg Nesterov6d08f2c2012-01-31 17:15:11 +0100900 mmput(mm);
901free:
KOSAKI Motohiro30cd8902011-05-26 16:25:52 -0700902 free_page((unsigned long) page);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700903 return copied;
904}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700905
Oleg Nesterov572d34b2012-01-31 17:14:54 +0100906static ssize_t mem_read(struct file *file, char __user *buf,
907 size_t count, loff_t *ppos)
908{
909 return mem_rw(file, buf, count, ppos, 0);
910}
911
912static ssize_t mem_write(struct file *file, const char __user *buf,
913 size_t count, loff_t *ppos)
914{
915 return mem_rw(file, (char __user*)buf, count, ppos, 1);
916}
917
Matt Mackall85863e42008-02-04 22:29:04 -0800918loff_t mem_lseek(struct file *file, loff_t offset, int orig)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700919{
920 switch (orig) {
921 case 0:
922 file->f_pos = offset;
923 break;
924 case 1:
925 file->f_pos += offset;
926 break;
927 default:
928 return -EINVAL;
929 }
930 force_successful_syscall_return();
931 return file->f_pos;
932}
933
Linus Torvaldse2683372012-01-17 15:21:19 -0800934static int mem_release(struct inode *inode, struct file *file)
935{
936 struct mm_struct *mm = file->private_data;
Oleg Nesterov71879d32012-01-31 17:14:38 +0100937 if (mm)
Oleg Nesterov6d08f2c2012-01-31 17:15:11 +0100938 mmdrop(mm);
Linus Torvaldse2683372012-01-17 15:21:19 -0800939 return 0;
940}
941
Arjan van de Ven00977a52007-02-12 00:55:34 -0800942static const struct file_operations proc_mem_operations = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700943 .llseek = mem_lseek,
944 .read = mem_read,
945 .write = mem_write,
946 .open = mem_open,
Linus Torvaldse2683372012-01-17 15:21:19 -0800947 .release = mem_release,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700948};
949
Cong Wangb409e572012-05-31 16:26:17 -0700950static int environ_open(struct inode *inode, struct file *file)
951{
952 return __mem_open(inode, file, PTRACE_MODE_READ);
953}
954
James Pearson315e28c2007-10-16 23:30:17 -0700955static ssize_t environ_read(struct file *file, char __user *buf,
956 size_t count, loff_t *ppos)
957{
James Pearson315e28c2007-10-16 23:30:17 -0700958 char *page;
959 unsigned long src = *ppos;
Cong Wangb409e572012-05-31 16:26:17 -0700960 int ret = 0;
961 struct mm_struct *mm = file->private_data;
Mateusz Guzika3b609e2016-01-20 15:01:05 -0800962 unsigned long env_start, env_end;
James Pearson315e28c2007-10-16 23:30:17 -0700963
Mathias Krause8148a732016-05-05 16:22:26 -0700964 /* Ensure the process spawned far enough to have an environment. */
965 if (!mm || !mm->env_end)
Cong Wangb409e572012-05-31 16:26:17 -0700966 return 0;
James Pearson315e28c2007-10-16 23:30:17 -0700967
Michal Hocko0ee931c2017-09-13 16:28:29 -0700968 page = (char *)__get_free_page(GFP_KERNEL);
James Pearson315e28c2007-10-16 23:30:17 -0700969 if (!page)
Cong Wangb409e572012-05-31 16:26:17 -0700970 return -ENOMEM;
James Pearson315e28c2007-10-16 23:30:17 -0700971
Al Virod6f64b82011-02-15 22:26:01 -0500972 ret = 0;
Vegard Nossum388f7932017-02-27 14:30:13 -0800973 if (!mmget_not_zero(mm))
Cong Wangb409e572012-05-31 16:26:17 -0700974 goto free;
Mateusz Guzika3b609e2016-01-20 15:01:05 -0800975
Yang Shi88aa7cc2018-06-07 17:05:28 -0700976 spin_lock(&mm->arg_lock);
Mateusz Guzika3b609e2016-01-20 15:01:05 -0800977 env_start = mm->env_start;
978 env_end = mm->env_end;
Yang Shi88aa7cc2018-06-07 17:05:28 -0700979 spin_unlock(&mm->arg_lock);
Mateusz Guzika3b609e2016-01-20 15:01:05 -0800980
James Pearson315e28c2007-10-16 23:30:17 -0700981 while (count > 0) {
Djalal Harounie8905ec2012-07-30 14:42:26 -0700982 size_t this_len, max_len;
983 int retval;
984
Mateusz Guzika3b609e2016-01-20 15:01:05 -0800985 if (src >= (env_end - env_start))
Djalal Harounie8905ec2012-07-30 14:42:26 -0700986 break;
James Pearson315e28c2007-10-16 23:30:17 -0700987
Mateusz Guzika3b609e2016-01-20 15:01:05 -0800988 this_len = env_end - (env_start + src);
James Pearson315e28c2007-10-16 23:30:17 -0700989
Djalal Harounie8905ec2012-07-30 14:42:26 -0700990 max_len = min_t(size_t, PAGE_SIZE, count);
991 this_len = min(max_len, this_len);
James Pearson315e28c2007-10-16 23:30:17 -0700992
Willy Tarreau7f7ccc22018-05-11 08:11:44 +0200993 retval = access_remote_vm(mm, (env_start + src), page, this_len, FOLL_ANON);
James Pearson315e28c2007-10-16 23:30:17 -0700994
995 if (retval <= 0) {
996 ret = retval;
997 break;
998 }
999
1000 if (copy_to_user(buf, page, retval)) {
1001 ret = -EFAULT;
1002 break;
1003 }
1004
1005 ret += retval;
1006 src += retval;
1007 buf += retval;
1008 count -= retval;
1009 }
1010 *ppos = src;
James Pearson315e28c2007-10-16 23:30:17 -07001011 mmput(mm);
Cong Wangb409e572012-05-31 16:26:17 -07001012
1013free:
James Pearson315e28c2007-10-16 23:30:17 -07001014 free_page((unsigned long) page);
James Pearson315e28c2007-10-16 23:30:17 -07001015 return ret;
1016}
1017
1018static const struct file_operations proc_environ_operations = {
Cong Wangb409e572012-05-31 16:26:17 -07001019 .open = environ_open,
James Pearson315e28c2007-10-16 23:30:17 -07001020 .read = environ_read,
Arnd Bergmann87df8422010-03-17 23:06:02 +01001021 .llseek = generic_file_llseek,
Cong Wangb409e572012-05-31 16:26:17 -07001022 .release = mem_release,
James Pearson315e28c2007-10-16 23:30:17 -07001023};
1024
Al Viroc5317162016-10-05 18:43:43 -04001025static int auxv_open(struct inode *inode, struct file *file)
1026{
1027 return __mem_open(inode, file, PTRACE_MODE_READ_FSCREDS);
1028}
1029
1030static ssize_t auxv_read(struct file *file, char __user *buf,
1031 size_t count, loff_t *ppos)
1032{
1033 struct mm_struct *mm = file->private_data;
1034 unsigned int nwords = 0;
Leon Yu06b28492016-10-27 17:46:50 -07001035
1036 if (!mm)
1037 return 0;
Al Viroc5317162016-10-05 18:43:43 -04001038 do {
1039 nwords += 2;
1040 } while (mm->saved_auxv[nwords - 2] != 0); /* AT_NULL */
1041 return simple_read_from_buffer(buf, count, ppos, mm->saved_auxv,
1042 nwords * sizeof(mm->saved_auxv[0]));
1043}
1044
1045static const struct file_operations proc_auxv_operations = {
1046 .open = auxv_open,
1047 .read = auxv_read,
1048 .llseek = generic_file_llseek,
1049 .release = mem_release,
1050};
1051
David Rientjesfa0cbbf2012-11-12 17:53:04 -08001052static ssize_t oom_adj_read(struct file *file, char __user *buf, size_t count,
1053 loff_t *ppos)
1054{
Al Viro496ad9a2013-01-23 17:07:38 -05001055 struct task_struct *task = get_proc_task(file_inode(file));
David Rientjesfa0cbbf2012-11-12 17:53:04 -08001056 char buffer[PROC_NUMBUF];
1057 int oom_adj = OOM_ADJUST_MIN;
1058 size_t len;
David Rientjesfa0cbbf2012-11-12 17:53:04 -08001059
1060 if (!task)
1061 return -ESRCH;
Michal Hockof913da52016-07-28 15:44:37 -07001062 if (task->signal->oom_score_adj == OOM_SCORE_ADJ_MAX)
1063 oom_adj = OOM_ADJUST_MAX;
1064 else
1065 oom_adj = (task->signal->oom_score_adj * -OOM_DISABLE) /
1066 OOM_SCORE_ADJ_MAX;
David Rientjesfa0cbbf2012-11-12 17:53:04 -08001067 put_task_struct(task);
Charles Haithcock66606562020-11-01 17:07:56 -08001068 if (oom_adj > OOM_ADJUST_MAX)
1069 oom_adj = OOM_ADJUST_MAX;
David Rientjesfa0cbbf2012-11-12 17:53:04 -08001070 len = snprintf(buffer, sizeof(buffer), "%d\n", oom_adj);
1071 return simple_read_from_buffer(buf, count, ppos, buffer, len);
1072}
1073
Michal Hocko1d5f0ac2016-07-28 15:44:40 -07001074static int __set_oom_adj(struct file *file, int oom_adj, bool legacy)
1075{
Michal Hocko44a70ade2016-07-28 15:44:43 -07001076 struct mm_struct *mm = NULL;
Michal Hocko1d5f0ac2016-07-28 15:44:40 -07001077 struct task_struct *task;
1078 int err = 0;
1079
1080 task = get_proc_task(file_inode(file));
1081 if (!task)
1082 return -ESRCH;
1083
1084 mutex_lock(&oom_adj_mutex);
1085 if (legacy) {
1086 if (oom_adj < task->signal->oom_score_adj &&
1087 !capable(CAP_SYS_RESOURCE)) {
1088 err = -EACCES;
1089 goto err_unlock;
1090 }
1091 /*
1092 * /proc/pid/oom_adj is provided for legacy purposes, ask users to use
1093 * /proc/pid/oom_score_adj instead.
1094 */
1095 pr_warn_once("%s (%d): /proc/%d/oom_adj is deprecated, please use /proc/%d/oom_score_adj instead.\n",
1096 current->comm, task_pid_nr(current), task_pid_nr(task),
1097 task_pid_nr(task));
1098 } else {
1099 if ((short)oom_adj < task->signal->oom_score_adj_min &&
1100 !capable(CAP_SYS_RESOURCE)) {
1101 err = -EACCES;
1102 goto err_unlock;
1103 }
1104 }
1105
Michal Hocko44a70ade2016-07-28 15:44:43 -07001106 /*
1107 * Make sure we will check other processes sharing the mm if this is
1108 * not vfrok which wants its own oom_score_adj.
1109 * pin the mm so it doesn't go away and get reused after task_unlock
1110 */
1111 if (!task->vfork_done) {
1112 struct task_struct *p = find_lock_task_mm(task);
1113
1114 if (p) {
Suren Baghdasaryan67197a42020-10-13 16:58:35 -07001115 if (test_bit(MMF_MULTIPROCESS, &p->mm->flags)) {
Michal Hocko44a70ade2016-07-28 15:44:43 -07001116 mm = p->mm;
Vegard Nossumf1f10072017-02-27 14:30:07 -08001117 mmgrab(mm);
Michal Hocko44a70ade2016-07-28 15:44:43 -07001118 }
1119 task_unlock(p);
1120 }
1121 }
1122
Michal Hocko1d5f0ac2016-07-28 15:44:40 -07001123 task->signal->oom_score_adj = oom_adj;
1124 if (!legacy && has_capability_noaudit(current, CAP_SYS_RESOURCE))
1125 task->signal->oom_score_adj_min = (short)oom_adj;
1126 trace_oom_score_adj_update(task);
Michal Hocko44a70ade2016-07-28 15:44:43 -07001127
1128 if (mm) {
1129 struct task_struct *p;
1130
1131 rcu_read_lock();
1132 for_each_process(p) {
1133 if (same_thread_group(task, p))
1134 continue;
1135
1136 /* do not touch kernel threads or the global init */
1137 if (p->flags & PF_KTHREAD || is_global_init(p))
1138 continue;
1139
1140 task_lock(p);
1141 if (!p->vfork_done && process_shares_mm(p, mm)) {
Michal Hocko44a70ade2016-07-28 15:44:43 -07001142 p->signal->oom_score_adj = oom_adj;
1143 if (!legacy && has_capability_noaudit(current, CAP_SYS_RESOURCE))
1144 p->signal->oom_score_adj_min = (short)oom_adj;
1145 }
1146 task_unlock(p);
1147 }
1148 rcu_read_unlock();
1149 mmdrop(mm);
1150 }
Michal Hocko1d5f0ac2016-07-28 15:44:40 -07001151err_unlock:
1152 mutex_unlock(&oom_adj_mutex);
1153 put_task_struct(task);
1154 return err;
1155}
Michal Hockof913da52016-07-28 15:44:37 -07001156
David Rientjesb72bdfa2015-11-05 18:50:32 -08001157/*
1158 * /proc/pid/oom_adj exists solely for backwards compatibility with previous
1159 * kernels. The effective policy is defined by oom_score_adj, which has a
1160 * different scale: oom_adj grew exponentially and oom_score_adj grows linearly.
1161 * Values written to oom_adj are simply mapped linearly to oom_score_adj.
1162 * Processes that become oom disabled via oom_adj will still be oom disabled
1163 * with this implementation.
1164 *
1165 * oom_adj cannot be removed since existing userspace binaries use it.
1166 */
David Rientjesfa0cbbf2012-11-12 17:53:04 -08001167static ssize_t oom_adj_write(struct file *file, const char __user *buf,
1168 size_t count, loff_t *ppos)
1169{
David Rientjesfa0cbbf2012-11-12 17:53:04 -08001170 char buffer[PROC_NUMBUF];
1171 int oom_adj;
David Rientjesfa0cbbf2012-11-12 17:53:04 -08001172 int err;
1173
1174 memset(buffer, 0, sizeof(buffer));
1175 if (count > sizeof(buffer) - 1)
1176 count = sizeof(buffer) - 1;
1177 if (copy_from_user(buffer, buf, count)) {
1178 err = -EFAULT;
1179 goto out;
1180 }
1181
1182 err = kstrtoint(strstrip(buffer), 0, &oom_adj);
1183 if (err)
1184 goto out;
1185 if ((oom_adj < OOM_ADJUST_MIN || oom_adj > OOM_ADJUST_MAX) &&
1186 oom_adj != OOM_DISABLE) {
1187 err = -EINVAL;
1188 goto out;
1189 }
1190
David Rientjesfa0cbbf2012-11-12 17:53:04 -08001191 /*
1192 * Scale /proc/pid/oom_score_adj appropriately ensuring that a maximum
1193 * value is always attainable.
1194 */
1195 if (oom_adj == OOM_ADJUST_MAX)
1196 oom_adj = OOM_SCORE_ADJ_MAX;
1197 else
1198 oom_adj = (oom_adj * OOM_SCORE_ADJ_MAX) / -OOM_DISABLE;
1199
Michal Hocko1d5f0ac2016-07-28 15:44:40 -07001200 err = __set_oom_adj(file, oom_adj, true);
David Rientjesfa0cbbf2012-11-12 17:53:04 -08001201out:
1202 return err < 0 ? err : count;
1203}
1204
1205static const struct file_operations proc_oom_adj_operations = {
1206 .read = oom_adj_read,
1207 .write = oom_adj_write,
1208 .llseek = generic_file_llseek,
1209};
1210
David Rientjesa63d83f2010-08-09 17:19:46 -07001211static ssize_t oom_score_adj_read(struct file *file, char __user *buf,
1212 size_t count, loff_t *ppos)
1213{
Al Viro496ad9a2013-01-23 17:07:38 -05001214 struct task_struct *task = get_proc_task(file_inode(file));
David Rientjesa63d83f2010-08-09 17:19:46 -07001215 char buffer[PROC_NUMBUF];
David Rientjesa9c58b902012-12-11 16:02:54 -08001216 short oom_score_adj = OOM_SCORE_ADJ_MIN;
David Rientjesa63d83f2010-08-09 17:19:46 -07001217 size_t len;
1218
1219 if (!task)
1220 return -ESRCH;
Michal Hockof913da52016-07-28 15:44:37 -07001221 oom_score_adj = task->signal->oom_score_adj;
David Rientjesa63d83f2010-08-09 17:19:46 -07001222 put_task_struct(task);
David Rientjesa9c58b902012-12-11 16:02:54 -08001223 len = snprintf(buffer, sizeof(buffer), "%hd\n", oom_score_adj);
David Rientjesa63d83f2010-08-09 17:19:46 -07001224 return simple_read_from_buffer(buf, count, ppos, buffer, len);
1225}
1226
1227static ssize_t oom_score_adj_write(struct file *file, const char __user *buf,
1228 size_t count, loff_t *ppos)
1229{
David Rientjesa63d83f2010-08-09 17:19:46 -07001230 char buffer[PROC_NUMBUF];
Alexey Dobriyan0a8cb8e2011-05-26 16:25:50 -07001231 int oom_score_adj;
David Rientjesa63d83f2010-08-09 17:19:46 -07001232 int err;
1233
1234 memset(buffer, 0, sizeof(buffer));
1235 if (count > sizeof(buffer) - 1)
1236 count = sizeof(buffer) - 1;
David Rientjes723548b2010-10-26 14:21:25 -07001237 if (copy_from_user(buffer, buf, count)) {
1238 err = -EFAULT;
1239 goto out;
1240 }
David Rientjesa63d83f2010-08-09 17:19:46 -07001241
Alexey Dobriyan0a8cb8e2011-05-26 16:25:50 -07001242 err = kstrtoint(strstrip(buffer), 0, &oom_score_adj);
David Rientjesa63d83f2010-08-09 17:19:46 -07001243 if (err)
David Rientjes723548b2010-10-26 14:21:25 -07001244 goto out;
David Rientjesa63d83f2010-08-09 17:19:46 -07001245 if (oom_score_adj < OOM_SCORE_ADJ_MIN ||
David Rientjes723548b2010-10-26 14:21:25 -07001246 oom_score_adj > OOM_SCORE_ADJ_MAX) {
1247 err = -EINVAL;
1248 goto out;
1249 }
David Rientjesa63d83f2010-08-09 17:19:46 -07001250
Michal Hocko1d5f0ac2016-07-28 15:44:40 -07001251 err = __set_oom_adj(file, oom_score_adj, false);
David Rientjes723548b2010-10-26 14:21:25 -07001252out:
1253 return err < 0 ? err : count;
David Rientjesa63d83f2010-08-09 17:19:46 -07001254}
1255
1256static const struct file_operations proc_oom_score_adj_operations = {
1257 .read = oom_score_adj_read,
1258 .write = oom_score_adj_write,
Arnd Bergmann6038f372010-08-15 18:52:59 +02001259 .llseek = default_llseek,
David Rientjesa63d83f2010-08-09 17:19:46 -07001260};
1261
Richard Guy Briggs4b7d2482019-01-22 17:06:39 -05001262#ifdef CONFIG_AUDIT
Alexey Dobriyanb4eb4f72016-10-29 19:04:39 +03001263#define TMPBUFLEN 11
Linus Torvalds1da177e2005-04-16 15:20:36 -07001264static ssize_t proc_loginuid_read(struct file * file, char __user * buf,
1265 size_t count, loff_t *ppos)
1266{
Al Viro496ad9a2013-01-23 17:07:38 -05001267 struct inode * inode = file_inode(file);
Eric W. Biederman99f89552006-06-26 00:25:55 -07001268 struct task_struct *task = get_proc_task(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001269 ssize_t length;
1270 char tmpbuf[TMPBUFLEN];
1271
Eric W. Biederman99f89552006-06-26 00:25:55 -07001272 if (!task)
1273 return -ESRCH;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001274 length = scnprintf(tmpbuf, TMPBUFLEN, "%u",
Eric W. Biedermane1760bd2012-09-10 22:39:43 -07001275 from_kuid(file->f_cred->user_ns,
1276 audit_get_loginuid(task)));
Eric W. Biederman99f89552006-06-26 00:25:55 -07001277 put_task_struct(task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001278 return simple_read_from_buffer(buf, count, ppos, tmpbuf, length);
1279}
1280
1281static ssize_t proc_loginuid_write(struct file * file, const char __user * buf,
1282 size_t count, loff_t *ppos)
1283{
Al Viro496ad9a2013-01-23 17:07:38 -05001284 struct inode * inode = file_inode(file);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001285 uid_t loginuid;
Eric W. Biedermane1760bd2012-09-10 22:39:43 -07001286 kuid_t kloginuid;
Alexey Dobriyan774636e2015-09-09 15:36:59 -07001287 int rv;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001288
Jens Axboe4ea33a92020-10-15 13:46:44 -06001289 /* Don't let kthreads write their own loginuid */
1290 if (current->flags & PF_KTHREAD)
1291 return -EPERM;
1292
Paul E. McKenney7dc52152010-02-22 17:04:52 -08001293 rcu_read_lock();
1294 if (current != pid_task(proc_pid(inode), PIDTYPE_PID)) {
1295 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001296 return -EPERM;
Paul E. McKenney7dc52152010-02-22 17:04:52 -08001297 }
1298 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001299
Linus Torvalds1da177e2005-04-16 15:20:36 -07001300 if (*ppos != 0) {
1301 /* No partial writes. */
1302 return -EINVAL;
1303 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001304
Alexey Dobriyan774636e2015-09-09 15:36:59 -07001305 rv = kstrtou32_from_user(buf, count, 10, &loginuid);
1306 if (rv < 0)
1307 return rv;
Eric Paris81407c82013-05-24 09:49:14 -04001308
1309 /* is userspace tring to explicitly UNSET the loginuid? */
1310 if (loginuid == AUDIT_UID_UNSET) {
1311 kloginuid = INVALID_UID;
1312 } else {
1313 kloginuid = make_kuid(file->f_cred->user_ns, loginuid);
Alexey Dobriyan774636e2015-09-09 15:36:59 -07001314 if (!uid_valid(kloginuid))
1315 return -EINVAL;
Eric W. Biedermane1760bd2012-09-10 22:39:43 -07001316 }
1317
Alexey Dobriyan774636e2015-09-09 15:36:59 -07001318 rv = audit_set_loginuid(kloginuid);
1319 if (rv < 0)
1320 return rv;
1321 return count;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001322}
1323
Arjan van de Ven00977a52007-02-12 00:55:34 -08001324static const struct file_operations proc_loginuid_operations = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001325 .read = proc_loginuid_read,
1326 .write = proc_loginuid_write,
Arnd Bergmann87df8422010-03-17 23:06:02 +01001327 .llseek = generic_file_llseek,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001328};
Eric Paris1e0bd752008-03-13 08:15:31 -04001329
1330static ssize_t proc_sessionid_read(struct file * file, char __user * buf,
1331 size_t count, loff_t *ppos)
1332{
Al Viro496ad9a2013-01-23 17:07:38 -05001333 struct inode * inode = file_inode(file);
Eric Paris1e0bd752008-03-13 08:15:31 -04001334 struct task_struct *task = get_proc_task(inode);
1335 ssize_t length;
1336 char tmpbuf[TMPBUFLEN];
1337
1338 if (!task)
1339 return -ESRCH;
1340 length = scnprintf(tmpbuf, TMPBUFLEN, "%u",
1341 audit_get_sessionid(task));
1342 put_task_struct(task);
1343 return simple_read_from_buffer(buf, count, ppos, tmpbuf, length);
1344}
1345
1346static const struct file_operations proc_sessionid_operations = {
1347 .read = proc_sessionid_read,
Arnd Bergmann87df8422010-03-17 23:06:02 +01001348 .llseek = generic_file_llseek,
Eric Paris1e0bd752008-03-13 08:15:31 -04001349};
Linus Torvalds1da177e2005-04-16 15:20:36 -07001350#endif
1351
Akinobu Mitaf4f154f2006-12-08 02:39:47 -08001352#ifdef CONFIG_FAULT_INJECTION
1353static ssize_t proc_fault_inject_read(struct file * file, char __user * buf,
1354 size_t count, loff_t *ppos)
1355{
Al Viro496ad9a2013-01-23 17:07:38 -05001356 struct task_struct *task = get_proc_task(file_inode(file));
Akinobu Mitaf4f154f2006-12-08 02:39:47 -08001357 char buffer[PROC_NUMBUF];
1358 size_t len;
1359 int make_it_fail;
Akinobu Mitaf4f154f2006-12-08 02:39:47 -08001360
1361 if (!task)
1362 return -ESRCH;
1363 make_it_fail = task->make_it_fail;
1364 put_task_struct(task);
1365
1366 len = snprintf(buffer, sizeof(buffer), "%i\n", make_it_fail);
Akinobu Mita0c28f282007-05-08 00:31:41 -07001367
1368 return simple_read_from_buffer(buf, count, ppos, buffer, len);
Akinobu Mitaf4f154f2006-12-08 02:39:47 -08001369}
1370
1371static ssize_t proc_fault_inject_write(struct file * file,
1372 const char __user * buf, size_t count, loff_t *ppos)
1373{
1374 struct task_struct *task;
Alexey Dobriyan774636e2015-09-09 15:36:59 -07001375 char buffer[PROC_NUMBUF];
Akinobu Mitaf4f154f2006-12-08 02:39:47 -08001376 int make_it_fail;
Alexey Dobriyan774636e2015-09-09 15:36:59 -07001377 int rv;
Akinobu Mitaf4f154f2006-12-08 02:39:47 -08001378
1379 if (!capable(CAP_SYS_RESOURCE))
1380 return -EPERM;
1381 memset(buffer, 0, sizeof(buffer));
1382 if (count > sizeof(buffer) - 1)
1383 count = sizeof(buffer) - 1;
1384 if (copy_from_user(buffer, buf, count))
1385 return -EFAULT;
Alexey Dobriyan774636e2015-09-09 15:36:59 -07001386 rv = kstrtoint(strstrip(buffer), 0, &make_it_fail);
1387 if (rv < 0)
1388 return rv;
Dave Jones16caed32014-04-07 15:39:15 -07001389 if (make_it_fail < 0 || make_it_fail > 1)
1390 return -EINVAL;
1391
Al Viro496ad9a2013-01-23 17:07:38 -05001392 task = get_proc_task(file_inode(file));
Akinobu Mitaf4f154f2006-12-08 02:39:47 -08001393 if (!task)
1394 return -ESRCH;
1395 task->make_it_fail = make_it_fail;
1396 put_task_struct(task);
Vincent Licba8aaf2009-09-22 16:45:38 -07001397
1398 return count;
Akinobu Mitaf4f154f2006-12-08 02:39:47 -08001399}
1400
Arjan van de Ven00977a52007-02-12 00:55:34 -08001401static const struct file_operations proc_fault_inject_operations = {
Akinobu Mitaf4f154f2006-12-08 02:39:47 -08001402 .read = proc_fault_inject_read,
1403 .write = proc_fault_inject_write,
Arnd Bergmann87df8422010-03-17 23:06:02 +01001404 .llseek = generic_file_llseek,
Akinobu Mitaf4f154f2006-12-08 02:39:47 -08001405};
Dmitry Vyukove41d58182017-07-12 14:34:35 -07001406
1407static ssize_t proc_fail_nth_write(struct file *file, const char __user *buf,
1408 size_t count, loff_t *ppos)
1409{
1410 struct task_struct *task;
Akinobu Mita9049f2f2017-07-14 14:49:52 -07001411 int err;
1412 unsigned int n;
Dmitry Vyukove41d58182017-07-12 14:34:35 -07001413
Akinobu Mita9049f2f2017-07-14 14:49:52 -07001414 err = kstrtouint_from_user(buf, count, 0, &n);
Dmitry Vyukove41d58182017-07-12 14:34:35 -07001415 if (err)
1416 return err;
Akinobu Mita1203c8e2017-07-14 14:49:57 -07001417
1418 task = get_proc_task(file_inode(file));
1419 if (!task)
1420 return -ESRCH;
Alexey Dobriyan9f7118b2018-02-06 15:36:55 -08001421 task->fail_nth = n;
Akinobu Mita1203c8e2017-07-14 14:49:57 -07001422 put_task_struct(task);
1423
Dmitry Vyukove41d58182017-07-12 14:34:35 -07001424 return count;
1425}
1426
1427static ssize_t proc_fail_nth_read(struct file *file, char __user *buf,
1428 size_t count, loff_t *ppos)
1429{
1430 struct task_struct *task;
Akinobu Mitabfc74092017-07-14 14:49:54 -07001431 char numbuf[PROC_NUMBUF];
1432 ssize_t len;
Dmitry Vyukove41d58182017-07-12 14:34:35 -07001433
1434 task = get_proc_task(file_inode(file));
1435 if (!task)
1436 return -ESRCH;
Alexey Dobriyan9f7118b2018-02-06 15:36:55 -08001437 len = snprintf(numbuf, sizeof(numbuf), "%u\n", task->fail_nth);
Akinobu Mita1203c8e2017-07-14 14:49:57 -07001438 put_task_struct(task);
Alexey Dobriyana44937f2018-08-21 21:54:27 -07001439 return simple_read_from_buffer(buf, count, ppos, numbuf, len);
Dmitry Vyukove41d58182017-07-12 14:34:35 -07001440}
1441
1442static const struct file_operations proc_fail_nth_operations = {
1443 .read = proc_fail_nth_read,
1444 .write = proc_fail_nth_write,
1445};
Akinobu Mitaf4f154f2006-12-08 02:39:47 -08001446#endif
1447
Arjan van de Ven97455122008-01-25 21:08:34 +01001448
Ingo Molnar43ae34c2007-07-09 18:52:00 +02001449#ifdef CONFIG_SCHED_DEBUG
1450/*
1451 * Print out various scheduling related per-task fields:
1452 */
1453static int sched_show(struct seq_file *m, void *v)
1454{
1455 struct inode *inode = m->private;
Alexey Gladkov9d78ede2020-05-18 20:07:38 +02001456 struct pid_namespace *ns = proc_pid_ns(inode->i_sb);
Ingo Molnar43ae34c2007-07-09 18:52:00 +02001457 struct task_struct *p;
1458
Ingo Molnar43ae34c2007-07-09 18:52:00 +02001459 p = get_proc_task(inode);
1460 if (!p)
1461 return -ESRCH;
Aleksa Sarai74dc3382017-08-06 14:41:41 +10001462 proc_sched_show_task(p, ns, m);
Ingo Molnar43ae34c2007-07-09 18:52:00 +02001463
1464 put_task_struct(p);
1465
1466 return 0;
1467}
1468
1469static ssize_t
1470sched_write(struct file *file, const char __user *buf,
1471 size_t count, loff_t *offset)
1472{
Al Viro496ad9a2013-01-23 17:07:38 -05001473 struct inode *inode = file_inode(file);
Ingo Molnar43ae34c2007-07-09 18:52:00 +02001474 struct task_struct *p;
1475
Ingo Molnar43ae34c2007-07-09 18:52:00 +02001476 p = get_proc_task(inode);
1477 if (!p)
1478 return -ESRCH;
1479 proc_sched_set_task(p);
1480
1481 put_task_struct(p);
1482
1483 return count;
1484}
1485
1486static int sched_open(struct inode *inode, struct file *filp)
1487{
Jovi Zhangc6a34052011-01-12 17:00:34 -08001488 return single_open(filp, sched_show, inode);
Ingo Molnar43ae34c2007-07-09 18:52:00 +02001489}
1490
1491static const struct file_operations proc_pid_sched_operations = {
1492 .open = sched_open,
1493 .read = seq_read,
1494 .write = sched_write,
1495 .llseek = seq_lseek,
Alexey Dobriyan5ea473a2007-07-31 00:38:50 -07001496 .release = single_release,
Ingo Molnar43ae34c2007-07-09 18:52:00 +02001497};
1498
1499#endif
1500
Mike Galbraith5091faa2010-11-30 14:18:03 +01001501#ifdef CONFIG_SCHED_AUTOGROUP
1502/*
1503 * Print out autogroup related information:
1504 */
1505static int sched_autogroup_show(struct seq_file *m, void *v)
1506{
1507 struct inode *inode = m->private;
1508 struct task_struct *p;
1509
1510 p = get_proc_task(inode);
1511 if (!p)
1512 return -ESRCH;
1513 proc_sched_autogroup_show_task(p, m);
1514
1515 put_task_struct(p);
1516
1517 return 0;
1518}
1519
1520static ssize_t
1521sched_autogroup_write(struct file *file, const char __user *buf,
1522 size_t count, loff_t *offset)
1523{
Al Viro496ad9a2013-01-23 17:07:38 -05001524 struct inode *inode = file_inode(file);
Mike Galbraith5091faa2010-11-30 14:18:03 +01001525 struct task_struct *p;
1526 char buffer[PROC_NUMBUF];
Alexey Dobriyan0a8cb8e2011-05-26 16:25:50 -07001527 int nice;
Mike Galbraith5091faa2010-11-30 14:18:03 +01001528 int err;
1529
1530 memset(buffer, 0, sizeof(buffer));
1531 if (count > sizeof(buffer) - 1)
1532 count = sizeof(buffer) - 1;
1533 if (copy_from_user(buffer, buf, count))
1534 return -EFAULT;
1535
Alexey Dobriyan0a8cb8e2011-05-26 16:25:50 -07001536 err = kstrtoint(strstrip(buffer), 0, &nice);
1537 if (err < 0)
1538 return err;
Mike Galbraith5091faa2010-11-30 14:18:03 +01001539
1540 p = get_proc_task(inode);
1541 if (!p)
1542 return -ESRCH;
1543
Hiroshi Shimamoto2e5b5b32012-02-23 17:41:27 +09001544 err = proc_sched_autogroup_set_nice(p, nice);
Mike Galbraith5091faa2010-11-30 14:18:03 +01001545 if (err)
1546 count = err;
1547
1548 put_task_struct(p);
1549
1550 return count;
1551}
1552
1553static int sched_autogroup_open(struct inode *inode, struct file *filp)
1554{
1555 int ret;
1556
1557 ret = single_open(filp, sched_autogroup_show, NULL);
1558 if (!ret) {
1559 struct seq_file *m = filp->private_data;
1560
1561 m->private = inode;
1562 }
1563 return ret;
1564}
1565
1566static const struct file_operations proc_pid_sched_autogroup_operations = {
1567 .open = sched_autogroup_open,
1568 .read = seq_read,
1569 .write = sched_autogroup_write,
1570 .llseek = seq_lseek,
1571 .release = single_release,
1572};
1573
1574#endif /* CONFIG_SCHED_AUTOGROUP */
1575
Andrei Vagin04a86822019-11-12 01:27:16 +00001576#ifdef CONFIG_TIME_NS
1577static int timens_offsets_show(struct seq_file *m, void *v)
1578{
1579 struct task_struct *p;
1580
1581 p = get_proc_task(file_inode(m->file));
1582 if (!p)
1583 return -ESRCH;
1584 proc_timens_show_offsets(p, m);
1585
1586 put_task_struct(p);
1587
1588 return 0;
1589}
1590
1591static ssize_t timens_offsets_write(struct file *file, const char __user *buf,
1592 size_t count, loff_t *ppos)
1593{
1594 struct inode *inode = file_inode(file);
1595 struct proc_timens_offset offsets[2];
1596 char *kbuf = NULL, *pos, *next_line;
1597 struct task_struct *p;
1598 int ret, noffsets;
1599
1600 /* Only allow < page size writes at the beginning of the file */
1601 if ((*ppos != 0) || (count >= PAGE_SIZE))
1602 return -EINVAL;
1603
1604 /* Slurp in the user data */
1605 kbuf = memdup_user_nul(buf, count);
1606 if (IS_ERR(kbuf))
1607 return PTR_ERR(kbuf);
1608
1609 /* Parse the user data */
1610 ret = -EINVAL;
1611 noffsets = 0;
1612 for (pos = kbuf; pos; pos = next_line) {
1613 struct proc_timens_offset *off = &offsets[noffsets];
Andrei Vagin94d440d2020-04-11 08:40:31 -07001614 char clock[10];
Andrei Vagin04a86822019-11-12 01:27:16 +00001615 int err;
1616
1617 /* Find the end of line and ensure we don't look past it */
1618 next_line = strchr(pos, '\n');
1619 if (next_line) {
1620 *next_line = '\0';
1621 next_line++;
1622 if (*next_line == '\0')
1623 next_line = NULL;
1624 }
1625
Andrei Vagin94d440d2020-04-11 08:40:31 -07001626 err = sscanf(pos, "%9s %lld %lu", clock,
Andrei Vagin04a86822019-11-12 01:27:16 +00001627 &off->val.tv_sec, &off->val.tv_nsec);
1628 if (err != 3 || off->val.tv_nsec >= NSEC_PER_SEC)
1629 goto out;
Andrei Vagin94d440d2020-04-11 08:40:31 -07001630
1631 clock[sizeof(clock) - 1] = 0;
1632 if (strcmp(clock, "monotonic") == 0 ||
1633 strcmp(clock, __stringify(CLOCK_MONOTONIC)) == 0)
1634 off->clockid = CLOCK_MONOTONIC;
1635 else if (strcmp(clock, "boottime") == 0 ||
1636 strcmp(clock, __stringify(CLOCK_BOOTTIME)) == 0)
1637 off->clockid = CLOCK_BOOTTIME;
1638 else
1639 goto out;
1640
Andrei Vagin04a86822019-11-12 01:27:16 +00001641 noffsets++;
1642 if (noffsets == ARRAY_SIZE(offsets)) {
1643 if (next_line)
1644 count = next_line - kbuf;
1645 break;
1646 }
1647 }
1648
1649 ret = -ESRCH;
1650 p = get_proc_task(inode);
1651 if (!p)
1652 goto out;
1653 ret = proc_timens_set_offset(file, p, offsets, noffsets);
1654 put_task_struct(p);
1655 if (ret)
1656 goto out;
1657
1658 ret = count;
1659out:
1660 kfree(kbuf);
1661 return ret;
1662}
1663
1664static int timens_offsets_open(struct inode *inode, struct file *filp)
1665{
1666 return single_open(filp, timens_offsets_show, inode);
1667}
1668
1669static const struct file_operations proc_timens_offsets_operations = {
1670 .open = timens_offsets_open,
1671 .read = seq_read,
1672 .write = timens_offsets_write,
1673 .llseek = seq_lseek,
1674 .release = single_release,
1675};
1676#endif /* CONFIG_TIME_NS */
1677
john stultz4614a696b2009-12-14 18:00:05 -08001678static ssize_t comm_write(struct file *file, const char __user *buf,
1679 size_t count, loff_t *offset)
1680{
Al Viro496ad9a2013-01-23 17:07:38 -05001681 struct inode *inode = file_inode(file);
john stultz4614a696b2009-12-14 18:00:05 -08001682 struct task_struct *p;
1683 char buffer[TASK_COMM_LEN];
David Rientjes830e0fc2013-04-30 15:28:18 -07001684 const size_t maxlen = sizeof(buffer) - 1;
john stultz4614a696b2009-12-14 18:00:05 -08001685
1686 memset(buffer, 0, sizeof(buffer));
David Rientjes830e0fc2013-04-30 15:28:18 -07001687 if (copy_from_user(buffer, buf, count > maxlen ? maxlen : count))
john stultz4614a696b2009-12-14 18:00:05 -08001688 return -EFAULT;
1689
1690 p = get_proc_task(inode);
1691 if (!p)
1692 return -ESRCH;
1693
Ohhoon Kwonc2f273e2021-09-07 19:57:35 -07001694 if (same_thread_group(current, p)) {
john stultz4614a696b2009-12-14 18:00:05 -08001695 set_task_comm(p, buffer);
Ohhoon Kwonc2f273e2021-09-07 19:57:35 -07001696 proc_comm_connector(p);
1697 }
john stultz4614a696b2009-12-14 18:00:05 -08001698 else
1699 count = -EINVAL;
1700
1701 put_task_struct(p);
1702
1703 return count;
1704}
1705
1706static int comm_show(struct seq_file *m, void *v)
1707{
1708 struct inode *inode = m->private;
1709 struct task_struct *p;
1710
1711 p = get_proc_task(inode);
1712 if (!p)
1713 return -ESRCH;
1714
Tejun Heo88b72b32018-05-18 08:47:13 -07001715 proc_task_name(m, p, false);
1716 seq_putc(m, '\n');
john stultz4614a696b2009-12-14 18:00:05 -08001717
1718 put_task_struct(p);
1719
1720 return 0;
1721}
1722
1723static int comm_open(struct inode *inode, struct file *filp)
1724{
Jovi Zhangc6a34052011-01-12 17:00:34 -08001725 return single_open(filp, comm_show, inode);
john stultz4614a696b2009-12-14 18:00:05 -08001726}
1727
1728static const struct file_operations proc_pid_set_comm_operations = {
1729 .open = comm_open,
1730 .read = seq_read,
1731 .write = comm_write,
1732 .llseek = seq_lseek,
1733 .release = single_release,
1734};
1735
Cyrill Gorcunov7773fbc2012-01-10 15:11:20 -08001736static int proc_exe_link(struct dentry *dentry, struct path *exe_path)
Matt Helsley925d1c42008-04-29 01:01:36 -07001737{
1738 struct task_struct *task;
Matt Helsley925d1c42008-04-29 01:01:36 -07001739 struct file *exe_file;
1740
David Howells2b0143b2015-03-17 22:25:59 +00001741 task = get_proc_task(d_inode(dentry));
Matt Helsley925d1c42008-04-29 01:01:36 -07001742 if (!task)
1743 return -ENOENT;
Mateusz Guzikcd81a9172016-08-23 16:20:38 +02001744 exe_file = get_task_exe_file(task);
Matt Helsley925d1c42008-04-29 01:01:36 -07001745 put_task_struct(task);
Matt Helsley925d1c42008-04-29 01:01:36 -07001746 if (exe_file) {
1747 *exe_path = exe_file->f_path;
1748 path_get(&exe_file->f_path);
1749 fput(exe_file);
1750 return 0;
1751 } else
1752 return -ENOENT;
1753}
1754
Al Viro6b255392015-11-17 10:20:54 -05001755static const char *proc_pid_get_link(struct dentry *dentry,
Al Virofceef392015-12-29 15:58:39 -05001756 struct inode *inode,
1757 struct delayed_call *done)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001758{
Christoph Hellwig408ef012012-06-18 10:47:03 -04001759 struct path path;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001760 int error = -EACCES;
1761
Al Viro6b255392015-11-17 10:20:54 -05001762 if (!dentry)
1763 return ERR_PTR(-ECHILD);
1764
Eric W. Biederman778c1142006-06-26 00:25:58 -07001765 /* Are we allowed to snoop on the tasks file descriptors? */
1766 if (!proc_fd_access_allowed(inode))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001767 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001768
Christoph Hellwig408ef012012-06-18 10:47:03 -04001769 error = PROC_I(inode)->op.proc_get_link(dentry, &path);
1770 if (error)
1771 goto out;
1772
Aleksa Sarai1bc82072019-12-07 01:13:28 +11001773 error = nd_jump_link(&path);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001774out:
Al Viro008b1502005-08-20 00:17:39 +01001775 return ERR_PTR(error);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001776}
1777
Jan Blunck3dcd25f2008-02-14 19:38:35 -08001778static int do_proc_readlink(struct path *path, char __user *buffer, int buflen)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001779{
Michal Hocko0ee931c2017-09-13 16:28:29 -07001780 char *tmp = (char *)__get_free_page(GFP_KERNEL);
Jan Blunck3dcd25f2008-02-14 19:38:35 -08001781 char *pathname;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001782 int len;
1783
1784 if (!tmp)
1785 return -ENOMEM;
Akinobu Mita0c28f282007-05-08 00:31:41 -07001786
Eric W. Biederman7b2a69b2010-12-05 15:51:21 -08001787 pathname = d_path(path, tmp, PAGE_SIZE);
Jan Blunck3dcd25f2008-02-14 19:38:35 -08001788 len = PTR_ERR(pathname);
1789 if (IS_ERR(pathname))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001790 goto out;
Jan Blunck3dcd25f2008-02-14 19:38:35 -08001791 len = tmp + PAGE_SIZE - 1 - pathname;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001792
1793 if (len > buflen)
1794 len = buflen;
Jan Blunck3dcd25f2008-02-14 19:38:35 -08001795 if (copy_to_user(buffer, pathname, len))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001796 len = -EFAULT;
1797 out:
1798 free_page((unsigned long)tmp);
1799 return len;
1800}
1801
1802static int proc_pid_readlink(struct dentry * dentry, char __user * buffer, int buflen)
1803{
1804 int error = -EACCES;
David Howells2b0143b2015-03-17 22:25:59 +00001805 struct inode *inode = d_inode(dentry);
Jan Blunck3dcd25f2008-02-14 19:38:35 -08001806 struct path path;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001807
Eric W. Biederman778c1142006-06-26 00:25:58 -07001808 /* Are we allowed to snoop on the tasks file descriptors? */
1809 if (!proc_fd_access_allowed(inode))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001810 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001811
Cyrill Gorcunov7773fbc2012-01-10 15:11:20 -08001812 error = PROC_I(inode)->op.proc_get_link(dentry, &path);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001813 if (error)
1814 goto out;
1815
Jan Blunck3dcd25f2008-02-14 19:38:35 -08001816 error = do_proc_readlink(&path, buffer, buflen);
1817 path_put(&path);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001818out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001819 return error;
1820}
1821
Cyrill Gorcunovfaf60af2012-08-23 14:43:24 +04001822const struct inode_operations proc_pid_link_inode_operations = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001823 .readlink = proc_pid_readlink,
Al Viro6b255392015-11-17 10:20:54 -05001824 .get_link = proc_pid_get_link,
Linus Torvalds6d76fa52006-07-15 12:26:45 -07001825 .setattr = proc_setattr,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001826};
1827
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001828
1829/* building an inode */
1830
Al Viroc6eb50d2017-09-30 14:45:42 -04001831void task_dump_owner(struct task_struct *task, umode_t mode,
Eric W. Biederman68eb94f2017-01-03 10:23:11 +13001832 kuid_t *ruid, kgid_t *rgid)
1833{
1834 /* Depending on the state of dumpable compute who should own a
1835 * proc file for a task.
1836 */
1837 const struct cred *cred;
1838 kuid_t uid;
1839 kgid_t gid;
1840
Alexey Dobriyan2e0ad552018-04-20 14:56:03 -07001841 if (unlikely(task->flags & PF_KTHREAD)) {
1842 *ruid = GLOBAL_ROOT_UID;
1843 *rgid = GLOBAL_ROOT_GID;
1844 return;
1845 }
1846
Eric W. Biederman68eb94f2017-01-03 10:23:11 +13001847 /* Default to the tasks effective ownership */
1848 rcu_read_lock();
1849 cred = __task_cred(task);
1850 uid = cred->euid;
1851 gid = cred->egid;
1852 rcu_read_unlock();
1853
1854 /*
1855 * Before the /proc/pid/status file was created the only way to read
1856 * the effective uid of a /process was to stat /proc/pid. Reading
1857 * /proc/pid/status is slow enough that procps and other packages
1858 * kept stating /proc/pid. To keep the rules in /proc simple I have
1859 * made this apply to all per process world readable and executable
1860 * directories.
1861 */
1862 if (mode != (S_IFDIR|S_IRUGO|S_IXUGO)) {
1863 struct mm_struct *mm;
1864 task_lock(task);
1865 mm = task->mm;
1866 /* Make non-dumpable tasks owned by some root */
1867 if (mm) {
1868 if (get_dumpable(mm) != SUID_DUMP_USER) {
1869 struct user_namespace *user_ns = mm->user_ns;
1870
1871 uid = make_kuid(user_ns, 0);
1872 if (!uid_valid(uid))
1873 uid = GLOBAL_ROOT_UID;
1874
1875 gid = make_kgid(user_ns, 0);
1876 if (!gid_valid(gid))
1877 gid = GLOBAL_ROOT_GID;
1878 }
1879 } else {
1880 uid = GLOBAL_ROOT_UID;
1881 gid = GLOBAL_ROOT_GID;
1882 }
1883 task_unlock(task);
1884 }
1885 *ruid = uid;
1886 *rgid = gid;
1887}
1888
Eric W. Biederman7bc3e6e2020-02-19 18:22:26 -06001889void proc_pid_evict_inode(struct proc_inode *ei)
1890{
1891 struct pid *pid = ei->pid;
1892
1893 if (S_ISDIR(ei->vfs_inode.i_mode)) {
Eric W. Biederman63f818f2020-04-07 09:43:04 -05001894 spin_lock(&pid->lock);
Eric W. Biederman7bc3e6e2020-02-19 18:22:26 -06001895 hlist_del_init_rcu(&ei->sibling_inodes);
Eric W. Biederman63f818f2020-04-07 09:43:04 -05001896 spin_unlock(&pid->lock);
Eric W. Biederman7bc3e6e2020-02-19 18:22:26 -06001897 }
1898
1899 put_pid(pid);
1900}
1901
Zhihao Cheng3336e7c2022-07-13 21:00:29 +08001902struct inode *proc_pid_make_inode(struct super_block *sb,
Andreas Gruenbacherdb978da2016-11-10 22:18:28 +01001903 struct task_struct *task, umode_t mode)
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001904{
1905 struct inode * inode;
1906 struct proc_inode *ei;
Eric W. Biederman7bc3e6e2020-02-19 18:22:26 -06001907 struct pid *pid;
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001908
1909 /* We need a new inode */
1910
1911 inode = new_inode(sb);
1912 if (!inode)
1913 goto out;
1914
1915 /* Common stuff */
1916 ei = PROC_I(inode);
Andreas Gruenbacherdb978da2016-11-10 22:18:28 +01001917 inode->i_mode = mode;
Christoph Hellwig85fe4022010-10-23 11:19:54 -04001918 inode->i_ino = get_next_ino();
Deepa Dinamani078cd822016-09-14 07:48:04 -07001919 inode->i_mtime = inode->i_atime = inode->i_ctime = current_time(inode);
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001920 inode->i_op = &proc_def_inode_operations;
1921
1922 /*
1923 * grab the reference to task.
1924 */
Eric W. Biederman7bc3e6e2020-02-19 18:22:26 -06001925 pid = get_task_pid(task, PIDTYPE_PID);
1926 if (!pid)
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001927 goto out_unlock;
1928
Eric W. Biederman7bc3e6e2020-02-19 18:22:26 -06001929 /* Let the pid remember us for quick removal */
1930 ei->pid = pid;
Eric W. Biederman7bc3e6e2020-02-19 18:22:26 -06001931
Eric W. Biederman68eb94f2017-01-03 10:23:11 +13001932 task_dump_owner(task, 0, &inode->i_uid, &inode->i_gid);
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001933 security_task_to_inode(task, inode);
1934
1935out:
1936 return inode;
1937
1938out_unlock:
1939 iput(inode);
1940 return NULL;
1941}
1942
Zhihao Cheng3336e7c2022-07-13 21:00:29 +08001943/*
1944 * Generating an inode and adding it into @pid->inodes, so that task will
1945 * invalidate inode's dentry before being released.
1946 *
1947 * This helper is used for creating dir-type entries under '/proc' and
1948 * '/proc/<tgid>/task'. Other entries(eg. fd, stat) under '/proc/<tgid>'
1949 * can be released by invalidating '/proc/<tgid>' dentry.
1950 * In theory, dentries under '/proc/<tgid>/task' can also be released by
1951 * invalidating '/proc/<tgid>' dentry, we reserve it to handle single
1952 * thread exiting situation: Any one of threads should invalidate its
1953 * '/proc/<tgid>/task/<pid>' dentry before released.
1954 */
1955static struct inode *proc_pid_make_base_inode(struct super_block *sb,
1956 struct task_struct *task, umode_t mode)
1957{
1958 struct inode *inode;
1959 struct proc_inode *ei;
1960 struct pid *pid;
1961
1962 inode = proc_pid_make_inode(sb, task, mode);
1963 if (!inode)
1964 return NULL;
1965
1966 /* Let proc_flush_pid find this directory inode */
1967 ei = PROC_I(inode);
1968 pid = ei->pid;
1969 spin_lock(&pid->lock);
1970 hlist_add_head_rcu(&ei->sibling_inodes, &pid->inodes);
1971 spin_unlock(&pid->lock);
1972
1973 return inode;
1974}
1975
Christian Brauner549c7292021-01-21 14:19:43 +01001976int pid_getattr(struct user_namespace *mnt_userns, const struct path *path,
1977 struct kstat *stat, u32 request_mask, unsigned int query_flags)
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001978{
David Howellsa528d352017-01-31 16:46:22 +00001979 struct inode *inode = d_inode(path->dentry);
Alexey Gladkovfa10fed2020-04-19 16:10:52 +02001980 struct proc_fs_info *fs_info = proc_sb_info(inode->i_sb);
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001981 struct task_struct *task;
David Howellsc69e8d92008-11-14 10:39:19 +11001982
Christian Brauner0d56a452021-01-21 14:19:30 +01001983 generic_fillattr(&init_user_ns, inode, stat);
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001984
Eric W. Biedermandcb0f222012-02-09 08:48:21 -08001985 stat->uid = GLOBAL_ROOT_UID;
1986 stat->gid = GLOBAL_ROOT_GID;
Alexey Dobriyan94116922018-06-07 17:10:07 -07001987 rcu_read_lock();
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001988 task = pid_task(proc_pid(inode), PIDTYPE_PID);
1989 if (task) {
Alexey Gladkovfa10fed2020-04-19 16:10:52 +02001990 if (!has_pid_permissions(fs_info, task, HIDEPID_INVISIBLE)) {
Vasiliy Kulikov04996802012-01-10 15:11:31 -08001991 rcu_read_unlock();
1992 /*
1993 * This doesn't prevent learning whether PID exists,
1994 * it only makes getattr() consistent with readdir().
1995 */
1996 return -ENOENT;
1997 }
Eric W. Biederman68eb94f2017-01-03 10:23:11 +13001998 task_dump_owner(task, inode->i_mode, &stat->uid, &stat->gid);
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001999 }
2000 rcu_read_unlock();
2001 return 0;
2002}
2003
2004/* dentry stuff */
2005
2006/*
Al Viro1bbc5512018-05-02 21:26:16 -04002007 * Set <pid>/... inode ownership (can change due to setuid(), etc.)
2008 */
2009void pid_update_inode(struct task_struct *task, struct inode *inode)
2010{
2011 task_dump_owner(task, inode->i_mode, &inode->i_uid, &inode->i_gid);
2012
2013 inode->i_mode &= ~(S_ISUID | S_ISGID);
2014 security_task_to_inode(task, inode);
2015}
2016
2017/*
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002018 * Rewrite the inode's ownerships here because the owning task may have
2019 * performed a setuid(), etc.
2020 *
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002021 */
Al Viro1bbc5512018-05-02 21:26:16 -04002022static int pid_revalidate(struct dentry *dentry, unsigned int flags)
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002023{
Nick Piggin34286d62011-01-07 17:49:57 +11002024 struct inode *inode;
2025 struct task_struct *task;
David Howellsc69e8d92008-11-14 10:39:19 +11002026
Al Viro0b728e12012-06-10 16:03:43 -04002027 if (flags & LOOKUP_RCU)
Nick Piggin34286d62011-01-07 17:49:57 +11002028 return -ECHILD;
2029
David Howells2b0143b2015-03-17 22:25:59 +00002030 inode = d_inode(dentry);
Nick Piggin34286d62011-01-07 17:49:57 +11002031 task = get_proc_task(inode);
2032
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002033 if (task) {
Al Viro1bbc5512018-05-02 21:26:16 -04002034 pid_update_inode(task, inode);
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002035 put_task_struct(task);
2036 return 1;
2037 }
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002038 return 0;
2039}
2040
Oleg Nesterovd855a4b2014-01-23 15:55:39 -08002041static inline bool proc_inode_is_dead(struct inode *inode)
2042{
2043 return !proc_pid(inode)->tasks[PIDTYPE_PID].first;
2044}
2045
David Howells1dd704b2013-04-12 01:08:50 +01002046int pid_delete_dentry(const struct dentry *dentry)
2047{
2048 /* Is the task we represent dead?
2049 * If so, then don't put the dentry on the lru list,
2050 * kill it immediately.
2051 */
David Howells2b0143b2015-03-17 22:25:59 +00002052 return proc_inode_is_dead(d_inode(dentry));
David Howells1dd704b2013-04-12 01:08:50 +01002053}
2054
Eric W. Biederman6b4e3062010-03-07 16:41:34 -08002055const struct dentry_operations pid_dentry_operations =
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002056{
2057 .d_revalidate = pid_revalidate,
2058 .d_delete = pid_delete_dentry,
2059};
2060
2061/* Lookups */
2062
Eric W. Biederman1c0d04c2006-10-02 02:18:57 -07002063/*
2064 * Fill a directory entry.
2065 *
2066 * If possible create the dcache entry and derive our inode number and
2067 * file type from dcache entry.
2068 *
2069 * Since all of the proc inode numbers are dynamically generated, the inode
Randy Dunlapd2928e82020-12-15 20:42:32 -08002070 * numbers do not exist until the inode is cache. This means creating
Eric W. Biederman1c0d04c2006-10-02 02:18:57 -07002071 * the dcache entry in readdir is necessary to keep the inode numbers
2072 * reported by readdir in sync with the inode numbers reported
2073 * by stat.
2074 */
Al Virof0c3b502013-05-16 12:07:31 -04002075bool proc_fill_cache(struct file *file, struct dir_context *ctx,
Alexey Dobriyana4ef3892018-06-07 17:10:10 -07002076 const char *name, unsigned int len,
Eric Dumazetc5141e62007-05-08 00:26:15 -07002077 instantiate_t instantiate, struct task_struct *task, const void *ptr)
Eric W. Biederman61a28782006-10-02 02:18:49 -07002078{
Al Virof0c3b502013-05-16 12:07:31 -04002079 struct dentry *child, *dir = file->f_path.dentry;
Al Viro1df98b82013-06-15 11:33:10 +04002080 struct qstr qname = QSTR_INIT(name, len);
Eric W. Biederman61a28782006-10-02 02:18:49 -07002081 struct inode *inode;
Al Viro0168b9e2018-05-03 09:21:05 -04002082 unsigned type = DT_UNKNOWN;
2083 ino_t ino = 1;
Eric W. Biederman61a28782006-10-02 02:18:49 -07002084
Al Viro1df98b82013-06-15 11:33:10 +04002085 child = d_hash_and_lookup(dir, &qname);
Eric W. Biederman61a28782006-10-02 02:18:49 -07002086 if (!child) {
Al Viro37817642016-04-20 16:31:31 -04002087 DECLARE_WAIT_QUEUE_HEAD_ONSTACK(wq);
2088 child = d_alloc_parallel(dir, &qname, &wq);
2089 if (IS_ERR(child))
Al Viro1df98b82013-06-15 11:33:10 +04002090 goto end_instantiate;
Al Viro37817642016-04-20 16:31:31 -04002091 if (d_in_lookup(child)) {
Al Viro0168b9e2018-05-03 09:21:05 -04002092 struct dentry *res;
2093 res = instantiate(child, task, ptr);
Al Viro37817642016-04-20 16:31:31 -04002094 d_lookup_done(child);
Al Viro0168b9e2018-05-03 09:21:05 -04002095 if (unlikely(res)) {
2096 dput(child);
2097 child = res;
Al Virod85b3992018-06-08 01:17:11 -04002098 if (IS_ERR(child))
2099 goto end_instantiate;
Al Viro37817642016-04-20 16:31:31 -04002100 }
Eric W. Biederman61a28782006-10-02 02:18:49 -07002101 }
2102 }
David Howells2b0143b2015-03-17 22:25:59 +00002103 inode = d_inode(child);
Al Viro147ce692013-06-15 10:26:35 +04002104 ino = inode->i_ino;
2105 type = inode->i_mode >> 12;
Eric W. Biederman61a28782006-10-02 02:18:49 -07002106 dput(child);
Al Virod85b3992018-06-08 01:17:11 -04002107end_instantiate:
Al Virof0c3b502013-05-16 12:07:31 -04002108 return dir_emit(ctx, name, len, ino, type);
Eric W. Biederman61a28782006-10-02 02:18:49 -07002109}
2110
Pavel Emelyanov640708a2012-01-10 15:11:23 -08002111/*
2112 * dname_to_vma_addr - maps a dentry name into two unsigned longs
2113 * which represent vma start and end addresses.
2114 */
2115static int dname_to_vma_addr(struct dentry *dentry,
2116 unsigned long *start, unsigned long *end)
2117{
Alexey Dobriyanac7f1062018-02-06 15:36:59 -08002118 const char *str = dentry->d_name.name;
2119 unsigned long long sval, eval;
2120 unsigned int len;
2121
Alexey Dobriyan35318db2018-04-10 16:41:14 -07002122 if (str[0] == '0' && str[1] != '-')
2123 return -EINVAL;
Alexey Dobriyanac7f1062018-02-06 15:36:59 -08002124 len = _parse_integer(str, 16, &sval);
2125 if (len & KSTRTOX_OVERFLOW)
Pavel Emelyanov640708a2012-01-10 15:11:23 -08002126 return -EINVAL;
Alexey Dobriyanac7f1062018-02-06 15:36:59 -08002127 if (sval != (unsigned long)sval)
2128 return -EINVAL;
2129 str += len;
2130
2131 if (*str != '-')
2132 return -EINVAL;
2133 str++;
2134
Alexey Dobriyan35318db2018-04-10 16:41:14 -07002135 if (str[0] == '0' && str[1])
2136 return -EINVAL;
Alexey Dobriyanac7f1062018-02-06 15:36:59 -08002137 len = _parse_integer(str, 16, &eval);
2138 if (len & KSTRTOX_OVERFLOW)
2139 return -EINVAL;
2140 if (eval != (unsigned long)eval)
2141 return -EINVAL;
2142 str += len;
2143
2144 if (*str != '\0')
2145 return -EINVAL;
2146
2147 *start = sval;
2148 *end = eval;
Pavel Emelyanov640708a2012-01-10 15:11:23 -08002149
2150 return 0;
2151}
2152
Al Viro0b728e12012-06-10 16:03:43 -04002153static int map_files_d_revalidate(struct dentry *dentry, unsigned int flags)
Pavel Emelyanov640708a2012-01-10 15:11:23 -08002154{
2155 unsigned long vm_start, vm_end;
2156 bool exact_vma_exists = false;
2157 struct mm_struct *mm = NULL;
2158 struct task_struct *task;
Pavel Emelyanov640708a2012-01-10 15:11:23 -08002159 struct inode *inode;
2160 int status = 0;
2161
Al Viro0b728e12012-06-10 16:03:43 -04002162 if (flags & LOOKUP_RCU)
Pavel Emelyanov640708a2012-01-10 15:11:23 -08002163 return -ECHILD;
2164
David Howells2b0143b2015-03-17 22:25:59 +00002165 inode = d_inode(dentry);
Pavel Emelyanov640708a2012-01-10 15:11:23 -08002166 task = get_proc_task(inode);
2167 if (!task)
2168 goto out_notask;
2169
Jann Horncaaee622016-01-20 15:00:04 -08002170 mm = mm_access(task, PTRACE_MODE_READ_FSCREDS);
Cong Wang2344bec2012-05-31 16:26:18 -07002171 if (IS_ERR_OR_NULL(mm))
Pavel Emelyanov640708a2012-01-10 15:11:23 -08002172 goto out;
2173
2174 if (!dname_to_vma_addr(dentry, &vm_start, &vm_end)) {
Michel Lespinassed8ed45c2020-06-08 21:33:25 -07002175 status = mmap_read_lock_killable(mm);
Konstantin Khlebnikovcd9e2bb2019-07-11 21:00:03 -07002176 if (!status) {
2177 exact_vma_exists = !!find_exact_vma(mm, vm_start,
2178 vm_end);
Michel Lespinassed8ed45c2020-06-08 21:33:25 -07002179 mmap_read_unlock(mm);
Konstantin Khlebnikovcd9e2bb2019-07-11 21:00:03 -07002180 }
Pavel Emelyanov640708a2012-01-10 15:11:23 -08002181 }
2182
2183 mmput(mm);
2184
2185 if (exact_vma_exists) {
Eric W. Biederman68eb94f2017-01-03 10:23:11 +13002186 task_dump_owner(task, 0, &inode->i_uid, &inode->i_gid);
2187
Pavel Emelyanov640708a2012-01-10 15:11:23 -08002188 security_task_to_inode(task, inode);
2189 status = 1;
2190 }
2191
2192out:
2193 put_task_struct(task);
2194
2195out_notask:
Pavel Emelyanov640708a2012-01-10 15:11:23 -08002196 return status;
2197}
2198
2199static const struct dentry_operations tid_map_files_dentry_operations = {
2200 .d_revalidate = map_files_d_revalidate,
2201 .d_delete = pid_delete_dentry,
2202};
2203
Al Viro6b255392015-11-17 10:20:54 -05002204static int map_files_get_link(struct dentry *dentry, struct path *path)
Pavel Emelyanov640708a2012-01-10 15:11:23 -08002205{
2206 unsigned long vm_start, vm_end;
2207 struct vm_area_struct *vma;
2208 struct task_struct *task;
2209 struct mm_struct *mm;
2210 int rc;
2211
2212 rc = -ENOENT;
David Howells2b0143b2015-03-17 22:25:59 +00002213 task = get_proc_task(d_inode(dentry));
Pavel Emelyanov640708a2012-01-10 15:11:23 -08002214 if (!task)
2215 goto out;
2216
2217 mm = get_task_mm(task);
2218 put_task_struct(task);
2219 if (!mm)
2220 goto out;
2221
2222 rc = dname_to_vma_addr(dentry, &vm_start, &vm_end);
2223 if (rc)
2224 goto out_mmput;
2225
Michel Lespinassed8ed45c2020-06-08 21:33:25 -07002226 rc = mmap_read_lock_killable(mm);
Konstantin Khlebnikovcd9e2bb2019-07-11 21:00:03 -07002227 if (rc)
2228 goto out_mmput;
2229
Artem Fetishev70335ab2014-03-10 15:49:45 -07002230 rc = -ENOENT;
Pavel Emelyanov640708a2012-01-10 15:11:23 -08002231 vma = find_exact_vma(mm, vm_start, vm_end);
2232 if (vma && vma->vm_file) {
2233 *path = vma->vm_file->f_path;
2234 path_get(path);
2235 rc = 0;
2236 }
Michel Lespinassed8ed45c2020-06-08 21:33:25 -07002237 mmap_read_unlock(mm);
Pavel Emelyanov640708a2012-01-10 15:11:23 -08002238
2239out_mmput:
2240 mmput(mm);
2241out:
2242 return rc;
2243}
2244
2245struct map_files_info {
Alexey Dobriyan20d28cd2018-02-06 15:37:06 -08002246 unsigned long start;
2247 unsigned long end;
Al Viro7b540d02012-08-27 14:55:26 -04002248 fmode_t mode;
Pavel Emelyanov640708a2012-01-10 15:11:23 -08002249};
2250
Calvin Owensbdb4d102015-09-09 15:35:54 -07002251/*
Adrian Reber12886f82020-07-19 12:04:14 +02002252 * Only allow CAP_SYS_ADMIN and CAP_CHECKPOINT_RESTORE to follow the links, due
2253 * to concerns about how the symlinks may be used to bypass permissions on
2254 * ancestor directories in the path to the file in question.
Calvin Owensbdb4d102015-09-09 15:35:54 -07002255 */
2256static const char *
Al Viro6b255392015-11-17 10:20:54 -05002257proc_map_files_get_link(struct dentry *dentry,
Al Virofceef392015-12-29 15:58:39 -05002258 struct inode *inode,
2259 struct delayed_call *done)
Calvin Owensbdb4d102015-09-09 15:35:54 -07002260{
Adrian Reber12886f82020-07-19 12:04:14 +02002261 if (!checkpoint_restore_ns_capable(&init_user_ns))
Calvin Owensbdb4d102015-09-09 15:35:54 -07002262 return ERR_PTR(-EPERM);
2263
Al Virofceef392015-12-29 15:58:39 -05002264 return proc_pid_get_link(dentry, inode, done);
Calvin Owensbdb4d102015-09-09 15:35:54 -07002265}
2266
2267/*
Al Viro6b255392015-11-17 10:20:54 -05002268 * Identical to proc_pid_link_inode_operations except for get_link()
Calvin Owensbdb4d102015-09-09 15:35:54 -07002269 */
2270static const struct inode_operations proc_map_files_link_inode_operations = {
2271 .readlink = proc_pid_readlink,
Al Viro6b255392015-11-17 10:20:54 -05002272 .get_link = proc_map_files_get_link,
Calvin Owensbdb4d102015-09-09 15:35:54 -07002273 .setattr = proc_setattr,
2274};
2275
Al Viro0168b9e2018-05-03 09:21:05 -04002276static struct dentry *
2277proc_map_files_instantiate(struct dentry *dentry,
Pavel Emelyanov640708a2012-01-10 15:11:23 -08002278 struct task_struct *task, const void *ptr)
2279{
Al Viro7b540d02012-08-27 14:55:26 -04002280 fmode_t mode = (fmode_t)(unsigned long)ptr;
Pavel Emelyanov640708a2012-01-10 15:11:23 -08002281 struct proc_inode *ei;
2282 struct inode *inode;
2283
Al Viro0168b9e2018-05-03 09:21:05 -04002284 inode = proc_pid_make_inode(dentry->d_sb, task, S_IFLNK |
Andreas Gruenbacherdb978da2016-11-10 22:18:28 +01002285 ((mode & FMODE_READ ) ? S_IRUSR : 0) |
2286 ((mode & FMODE_WRITE) ? S_IWUSR : 0));
Pavel Emelyanov640708a2012-01-10 15:11:23 -08002287 if (!inode)
Al Viro0168b9e2018-05-03 09:21:05 -04002288 return ERR_PTR(-ENOENT);
Pavel Emelyanov640708a2012-01-10 15:11:23 -08002289
2290 ei = PROC_I(inode);
Al Viro6b255392015-11-17 10:20:54 -05002291 ei->op.proc_get_link = map_files_get_link;
Pavel Emelyanov640708a2012-01-10 15:11:23 -08002292
Calvin Owensbdb4d102015-09-09 15:35:54 -07002293 inode->i_op = &proc_map_files_link_inode_operations;
Pavel Emelyanov640708a2012-01-10 15:11:23 -08002294 inode->i_size = 64;
Pavel Emelyanov640708a2012-01-10 15:11:23 -08002295
2296 d_set_d_op(dentry, &tid_map_files_dentry_operations);
Al Viro0168b9e2018-05-03 09:21:05 -04002297 return d_splice_alias(inode, dentry);
Pavel Emelyanov640708a2012-01-10 15:11:23 -08002298}
2299
2300static struct dentry *proc_map_files_lookup(struct inode *dir,
Al Viro00cd8dd2012-06-10 17:13:09 -04002301 struct dentry *dentry, unsigned int flags)
Pavel Emelyanov640708a2012-01-10 15:11:23 -08002302{
2303 unsigned long vm_start, vm_end;
2304 struct vm_area_struct *vma;
2305 struct task_struct *task;
Al Viro0168b9e2018-05-03 09:21:05 -04002306 struct dentry *result;
Pavel Emelyanov640708a2012-01-10 15:11:23 -08002307 struct mm_struct *mm;
2308
Al Viro0168b9e2018-05-03 09:21:05 -04002309 result = ERR_PTR(-ENOENT);
Pavel Emelyanov640708a2012-01-10 15:11:23 -08002310 task = get_proc_task(dir);
2311 if (!task)
2312 goto out;
2313
Al Viro0168b9e2018-05-03 09:21:05 -04002314 result = ERR_PTR(-EACCES);
Jann Horncaaee622016-01-20 15:00:04 -08002315 if (!ptrace_may_access(task, PTRACE_MODE_READ_FSCREDS))
Pavel Emelyanov640708a2012-01-10 15:11:23 -08002316 goto out_put_task;
2317
Al Viro0168b9e2018-05-03 09:21:05 -04002318 result = ERR_PTR(-ENOENT);
Pavel Emelyanov640708a2012-01-10 15:11:23 -08002319 if (dname_to_vma_addr(dentry, &vm_start, &vm_end))
Cyrill Gorcunoveb94cd92012-05-17 17:03:25 -07002320 goto out_put_task;
Pavel Emelyanov640708a2012-01-10 15:11:23 -08002321
2322 mm = get_task_mm(task);
2323 if (!mm)
Cyrill Gorcunoveb94cd92012-05-17 17:03:25 -07002324 goto out_put_task;
Pavel Emelyanov640708a2012-01-10 15:11:23 -08002325
Konstantin Khlebnikovcd9e2bb2019-07-11 21:00:03 -07002326 result = ERR_PTR(-EINTR);
Michel Lespinassed8ed45c2020-06-08 21:33:25 -07002327 if (mmap_read_lock_killable(mm))
Konstantin Khlebnikovcd9e2bb2019-07-11 21:00:03 -07002328 goto out_put_mm;
2329
2330 result = ERR_PTR(-ENOENT);
Pavel Emelyanov640708a2012-01-10 15:11:23 -08002331 vma = find_exact_vma(mm, vm_start, vm_end);
2332 if (!vma)
2333 goto out_no_vma;
2334
Stanislav Kinsbursky05f56482012-11-26 16:29:42 -08002335 if (vma->vm_file)
Al Viro0168b9e2018-05-03 09:21:05 -04002336 result = proc_map_files_instantiate(dentry, task,
Stanislav Kinsbursky05f56482012-11-26 16:29:42 -08002337 (void *)(unsigned long)vma->vm_file->f_mode);
Pavel Emelyanov640708a2012-01-10 15:11:23 -08002338
2339out_no_vma:
Michel Lespinassed8ed45c2020-06-08 21:33:25 -07002340 mmap_read_unlock(mm);
Konstantin Khlebnikovcd9e2bb2019-07-11 21:00:03 -07002341out_put_mm:
Pavel Emelyanov640708a2012-01-10 15:11:23 -08002342 mmput(mm);
Pavel Emelyanov640708a2012-01-10 15:11:23 -08002343out_put_task:
2344 put_task_struct(task);
2345out:
Al Viro0168b9e2018-05-03 09:21:05 -04002346 return result;
Pavel Emelyanov640708a2012-01-10 15:11:23 -08002347}
2348
2349static const struct inode_operations proc_map_files_inode_operations = {
2350 .lookup = proc_map_files_lookup,
2351 .permission = proc_fd_permission,
2352 .setattr = proc_setattr,
2353};
2354
2355static int
Al Virof0c3b502013-05-16 12:07:31 -04002356proc_map_files_readdir(struct file *file, struct dir_context *ctx)
Pavel Emelyanov640708a2012-01-10 15:11:23 -08002357{
Pavel Emelyanov640708a2012-01-10 15:11:23 -08002358 struct vm_area_struct *vma;
2359 struct task_struct *task;
2360 struct mm_struct *mm;
Al Virof0c3b502013-05-16 12:07:31 -04002361 unsigned long nr_files, pos, i;
Kent Overstreet94f8f3b2019-03-11 23:31:18 -07002362 GENRADIX(struct map_files_info) fa;
Al Virof0c3b502013-05-16 12:07:31 -04002363 struct map_files_info *p;
Pavel Emelyanov640708a2012-01-10 15:11:23 -08002364 int ret;
2365
Kent Overstreet94f8f3b2019-03-11 23:31:18 -07002366 genradix_init(&fa);
2367
Pavel Emelyanov640708a2012-01-10 15:11:23 -08002368 ret = -ENOENT;
Al Virof0c3b502013-05-16 12:07:31 -04002369 task = get_proc_task(file_inode(file));
Pavel Emelyanov640708a2012-01-10 15:11:23 -08002370 if (!task)
2371 goto out;
2372
2373 ret = -EACCES;
Jann Horncaaee622016-01-20 15:00:04 -08002374 if (!ptrace_may_access(task, PTRACE_MODE_READ_FSCREDS))
Pavel Emelyanov640708a2012-01-10 15:11:23 -08002375 goto out_put_task;
2376
2377 ret = 0;
Al Virof0c3b502013-05-16 12:07:31 -04002378 if (!dir_emit_dots(file, ctx))
2379 goto out_put_task;
Pavel Emelyanov640708a2012-01-10 15:11:23 -08002380
Al Virof0c3b502013-05-16 12:07:31 -04002381 mm = get_task_mm(task);
2382 if (!mm)
2383 goto out_put_task;
Konstantin Khlebnikovcd9e2bb2019-07-11 21:00:03 -07002384
Michel Lespinasse89154dd2020-06-08 21:33:29 -07002385 ret = mmap_read_lock_killable(mm);
Konstantin Khlebnikovcd9e2bb2019-07-11 21:00:03 -07002386 if (ret) {
2387 mmput(mm);
2388 goto out_put_task;
2389 }
Pavel Emelyanov640708a2012-01-10 15:11:23 -08002390
Al Virof0c3b502013-05-16 12:07:31 -04002391 nr_files = 0;
Pavel Emelyanov640708a2012-01-10 15:11:23 -08002392
Al Virof0c3b502013-05-16 12:07:31 -04002393 /*
2394 * We need two passes here:
2395 *
Michel Lespinassec1e8d7c2020-06-08 21:33:54 -07002396 * 1) Collect vmas of mapped files with mmap_lock taken
2397 * 2) Release mmap_lock and instantiate entries
Al Virof0c3b502013-05-16 12:07:31 -04002398 *
2399 * otherwise we get lockdep complained, since filldir()
Michel Lespinassec1e8d7c2020-06-08 21:33:54 -07002400 * routine might require mmap_lock taken in might_fault().
Al Virof0c3b502013-05-16 12:07:31 -04002401 */
Pavel Emelyanov640708a2012-01-10 15:11:23 -08002402
Al Virof0c3b502013-05-16 12:07:31 -04002403 for (vma = mm->mmap, pos = 2; vma; vma = vma->vm_next) {
Kent Overstreet94f8f3b2019-03-11 23:31:18 -07002404 if (!vma->vm_file)
2405 continue;
2406 if (++pos <= ctx->pos)
2407 continue;
Al Virof0c3b502013-05-16 12:07:31 -04002408
Kent Overstreet94f8f3b2019-03-11 23:31:18 -07002409 p = genradix_ptr_alloc(&fa, nr_files++, GFP_KERNEL);
2410 if (!p) {
Al Virof0c3b502013-05-16 12:07:31 -04002411 ret = -ENOMEM;
Michel Lespinasse89154dd2020-06-08 21:33:29 -07002412 mmap_read_unlock(mm);
Al Virof0c3b502013-05-16 12:07:31 -04002413 mmput(mm);
2414 goto out_put_task;
2415 }
Al Virof0c3b502013-05-16 12:07:31 -04002416
Kent Overstreet94f8f3b2019-03-11 23:31:18 -07002417 p->start = vma->vm_start;
2418 p->end = vma->vm_end;
2419 p->mode = vma->vm_file->f_mode;
Pavel Emelyanov640708a2012-01-10 15:11:23 -08002420 }
Michel Lespinasse89154dd2020-06-08 21:33:29 -07002421 mmap_read_unlock(mm);
Alexey Dobriyanfe079a52018-04-10 16:32:05 -07002422 mmput(mm);
Al Virof0c3b502013-05-16 12:07:31 -04002423
2424 for (i = 0; i < nr_files; i++) {
Alexey Dobriyan20d28cd2018-02-06 15:37:06 -08002425 char buf[4 * sizeof(long) + 2]; /* max: %lx-%lx\0 */
2426 unsigned int len;
2427
Kent Overstreet94f8f3b2019-03-11 23:31:18 -07002428 p = genradix_ptr(&fa, i);
Alexey Dobriyan20d28cd2018-02-06 15:37:06 -08002429 len = snprintf(buf, sizeof(buf), "%lx-%lx", p->start, p->end);
Al Virof0c3b502013-05-16 12:07:31 -04002430 if (!proc_fill_cache(file, ctx,
Alexey Dobriyan20d28cd2018-02-06 15:37:06 -08002431 buf, len,
Al Virof0c3b502013-05-16 12:07:31 -04002432 proc_map_files_instantiate,
2433 task,
2434 (void *)(unsigned long)p->mode))
2435 break;
2436 ctx->pos++;
2437 }
Pavel Emelyanov640708a2012-01-10 15:11:23 -08002438
Pavel Emelyanov640708a2012-01-10 15:11:23 -08002439out_put_task:
2440 put_task_struct(task);
2441out:
Kent Overstreet94f8f3b2019-03-11 23:31:18 -07002442 genradix_free(&fa);
Pavel Emelyanov640708a2012-01-10 15:11:23 -08002443 return ret;
2444}
2445
2446static const struct file_operations proc_map_files_operations = {
2447 .read = generic_read_dir,
Al Virof50752e2016-04-20 17:13:54 -04002448 .iterate_shared = proc_map_files_readdir,
2449 .llseek = generic_file_llseek,
Pavel Emelyanov640708a2012-01-10 15:11:23 -08002450};
2451
Nicolas Pitreb18b6a92017-01-21 00:09:08 -05002452#if defined(CONFIG_CHECKPOINT_RESTORE) && defined(CONFIG_POSIX_TIMERS)
Pavel Emelyanov48f6a7a2013-03-11 13:12:45 +04002453struct timers_private {
2454 struct pid *pid;
2455 struct task_struct *task;
2456 struct sighand_struct *sighand;
Pavel Emelyanov57b80152013-03-11 13:13:08 +04002457 struct pid_namespace *ns;
Pavel Emelyanov48f6a7a2013-03-11 13:12:45 +04002458 unsigned long flags;
2459};
2460
2461static void *timers_start(struct seq_file *m, loff_t *pos)
2462{
2463 struct timers_private *tp = m->private;
2464
2465 tp->task = get_pid_task(tp->pid, PIDTYPE_PID);
2466 if (!tp->task)
2467 return ERR_PTR(-ESRCH);
2468
2469 tp->sighand = lock_task_sighand(tp->task, &tp->flags);
2470 if (!tp->sighand)
2471 return ERR_PTR(-ESRCH);
2472
2473 return seq_list_start(&tp->task->signal->posix_timers, *pos);
2474}
2475
2476static void *timers_next(struct seq_file *m, void *v, loff_t *pos)
2477{
2478 struct timers_private *tp = m->private;
2479 return seq_list_next(v, &tp->task->signal->posix_timers, pos);
2480}
2481
2482static void timers_stop(struct seq_file *m, void *v)
2483{
2484 struct timers_private *tp = m->private;
2485
2486 if (tp->sighand) {
2487 unlock_task_sighand(tp->task, &tp->flags);
2488 tp->sighand = NULL;
2489 }
2490
2491 if (tp->task) {
2492 put_task_struct(tp->task);
2493 tp->task = NULL;
2494 }
2495}
2496
2497static int show_timer(struct seq_file *m, void *v)
2498{
2499 struct k_itimer *timer;
Pavel Emelyanov57b80152013-03-11 13:13:08 +04002500 struct timers_private *tp = m->private;
2501 int notify;
Alexey Dobriyancedbcca2014-08-08 14:21:33 -07002502 static const char * const nstr[] = {
Pavel Emelyanov57b80152013-03-11 13:13:08 +04002503 [SIGEV_SIGNAL] = "signal",
2504 [SIGEV_NONE] = "none",
2505 [SIGEV_THREAD] = "thread",
2506 };
Pavel Emelyanov48f6a7a2013-03-11 13:12:45 +04002507
2508 timer = list_entry((struct list_head *)v, struct k_itimer, list);
Pavel Emelyanov57b80152013-03-11 13:13:08 +04002509 notify = timer->it_sigev_notify;
2510
Pavel Emelyanov48f6a7a2013-03-11 13:12:45 +04002511 seq_printf(m, "ID: %d\n", timer->it_id);
Linus Torvaldsba3edf1f2017-12-06 18:23:27 -08002512 seq_printf(m, "signal: %d/%px\n",
Joe Perches25ce3192015-04-15 16:18:17 -07002513 timer->sigq->info.si_signo,
2514 timer->sigq->info.si_value.sival_ptr);
Pavel Emelyanov57b80152013-03-11 13:13:08 +04002515 seq_printf(m, "notify: %s/%s.%d\n",
Joe Perches25ce3192015-04-15 16:18:17 -07002516 nstr[notify & ~SIGEV_THREAD_ID],
2517 (notify & SIGEV_THREAD_ID) ? "tid" : "pid",
2518 pid_nr_ns(timer->it_pid, tp->ns));
Pavel Tikhomirov15ef0292013-05-17 02:12:03 +04002519 seq_printf(m, "ClockID: %d\n", timer->it_clock);
Pavel Emelyanov48f6a7a2013-03-11 13:12:45 +04002520
2521 return 0;
2522}
2523
2524static const struct seq_operations proc_timers_seq_ops = {
2525 .start = timers_start,
2526 .next = timers_next,
2527 .stop = timers_stop,
2528 .show = show_timer,
2529};
2530
2531static int proc_timers_open(struct inode *inode, struct file *file)
2532{
2533 struct timers_private *tp;
2534
2535 tp = __seq_open_private(file, &proc_timers_seq_ops,
2536 sizeof(struct timers_private));
2537 if (!tp)
2538 return -ENOMEM;
2539
2540 tp->pid = proc_pid(inode);
Alexey Gladkov9d78ede2020-05-18 20:07:38 +02002541 tp->ns = proc_pid_ns(inode->i_sb);
Pavel Emelyanov48f6a7a2013-03-11 13:12:45 +04002542 return 0;
2543}
2544
2545static const struct file_operations proc_timers_operations = {
2546 .open = proc_timers_open,
2547 .read = seq_read,
2548 .llseek = seq_lseek,
2549 .release = seq_release_private,
2550};
Eric Engestromb5946be2016-03-17 14:20:57 -07002551#endif
Pavel Emelyanov640708a2012-01-10 15:11:23 -08002552
John Stultz5de23d42016-03-17 14:20:54 -07002553static ssize_t timerslack_ns_write(struct file *file, const char __user *buf,
2554 size_t count, loff_t *offset)
2555{
2556 struct inode *inode = file_inode(file);
2557 struct task_struct *p;
2558 u64 slack_ns;
2559 int err;
2560
2561 err = kstrtoull_from_user(buf, count, 10, &slack_ns);
2562 if (err < 0)
2563 return err;
2564
2565 p = get_proc_task(inode);
2566 if (!p)
2567 return -ESRCH;
2568
John Stultz4b2bd5f2016-10-07 17:02:33 -07002569 if (p != current) {
Benjamin Gordon8da0b4f2019-01-03 15:25:56 -08002570 rcu_read_lock();
2571 if (!ns_capable(__task_cred(p)->user_ns, CAP_SYS_NICE)) {
2572 rcu_read_unlock();
John Stultz4b2bd5f2016-10-07 17:02:33 -07002573 count = -EPERM;
2574 goto out;
2575 }
Benjamin Gordon8da0b4f2019-01-03 15:25:56 -08002576 rcu_read_unlock();
John Stultz5de23d42016-03-17 14:20:54 -07002577
John Stultz4b2bd5f2016-10-07 17:02:33 -07002578 err = security_task_setscheduler(p);
2579 if (err) {
2580 count = err;
2581 goto out;
2582 }
John Stultz904763e2016-10-07 17:02:29 -07002583 }
2584
John Stultz7abbaf92016-10-07 17:02:26 -07002585 task_lock(p);
2586 if (slack_ns == 0)
2587 p->timer_slack_ns = p->default_timer_slack_ns;
2588 else
2589 p->timer_slack_ns = slack_ns;
2590 task_unlock(p);
2591
2592out:
John Stultz5de23d42016-03-17 14:20:54 -07002593 put_task_struct(p);
2594
2595 return count;
2596}
2597
2598static int timerslack_ns_show(struct seq_file *m, void *v)
2599{
2600 struct inode *inode = m->private;
2601 struct task_struct *p;
John Stultz7abbaf92016-10-07 17:02:26 -07002602 int err = 0;
John Stultz5de23d42016-03-17 14:20:54 -07002603
2604 p = get_proc_task(inode);
2605 if (!p)
2606 return -ESRCH;
2607
John Stultz4b2bd5f2016-10-07 17:02:33 -07002608 if (p != current) {
Benjamin Gordon8da0b4f2019-01-03 15:25:56 -08002609 rcu_read_lock();
2610 if (!ns_capable(__task_cred(p)->user_ns, CAP_SYS_NICE)) {
2611 rcu_read_unlock();
John Stultz4b2bd5f2016-10-07 17:02:33 -07002612 err = -EPERM;
2613 goto out;
2614 }
Benjamin Gordon8da0b4f2019-01-03 15:25:56 -08002615 rcu_read_unlock();
2616
John Stultz4b2bd5f2016-10-07 17:02:33 -07002617 err = security_task_getscheduler(p);
2618 if (err)
2619 goto out;
2620 }
John Stultz904763e2016-10-07 17:02:29 -07002621
John Stultz7abbaf92016-10-07 17:02:26 -07002622 task_lock(p);
2623 seq_printf(m, "%llu\n", p->timer_slack_ns);
2624 task_unlock(p);
2625
2626out:
John Stultz5de23d42016-03-17 14:20:54 -07002627 put_task_struct(p);
2628
2629 return err;
2630}
2631
2632static int timerslack_ns_open(struct inode *inode, struct file *filp)
2633{
2634 return single_open(filp, timerslack_ns_show, inode);
2635}
2636
2637static const struct file_operations proc_pid_set_timerslack_ns_operations = {
2638 .open = timerslack_ns_open,
2639 .read = seq_read,
2640 .write = timerslack_ns_write,
2641 .llseek = seq_lseek,
2642 .release = single_release,
2643};
2644
Al Viro0168b9e2018-05-03 09:21:05 -04002645static struct dentry *proc_pident_instantiate(struct dentry *dentry,
2646 struct task_struct *task, const void *ptr)
Eric W. Biederman444ceed2006-10-02 02:18:49 -07002647{
Eric Dumazetc5141e62007-05-08 00:26:15 -07002648 const struct pid_entry *p = ptr;
Eric W. Biederman444ceed2006-10-02 02:18:49 -07002649 struct inode *inode;
2650 struct proc_inode *ei;
Eric W. Biederman444ceed2006-10-02 02:18:49 -07002651
Al Viro0168b9e2018-05-03 09:21:05 -04002652 inode = proc_pid_make_inode(dentry->d_sb, task, p->mode);
Eric W. Biederman444ceed2006-10-02 02:18:49 -07002653 if (!inode)
Al Viro0168b9e2018-05-03 09:21:05 -04002654 return ERR_PTR(-ENOENT);
Eric W. Biederman444ceed2006-10-02 02:18:49 -07002655
2656 ei = PROC_I(inode);
Eric W. Biederman444ceed2006-10-02 02:18:49 -07002657 if (S_ISDIR(inode->i_mode))
Miklos Szeredibfe86842011-10-28 14:13:29 +02002658 set_nlink(inode, 2); /* Use getattr to fix if necessary */
Eric W. Biederman444ceed2006-10-02 02:18:49 -07002659 if (p->iop)
2660 inode->i_op = p->iop;
2661 if (p->fop)
2662 inode->i_fop = p->fop;
2663 ei->op = p->op;
Al Viro1bbc5512018-05-02 21:26:16 -04002664 pid_update_inode(task, inode);
Nick Pigginfb045ad2011-01-07 17:49:55 +11002665 d_set_d_op(dentry, &pid_dentry_operations);
Al Viro0168b9e2018-05-03 09:21:05 -04002666 return d_splice_alias(inode, dentry);
Eric W. Biederman444ceed2006-10-02 02:18:49 -07002667}
2668
Linus Torvalds1da177e2005-04-16 15:20:36 -07002669static struct dentry *proc_pident_lookup(struct inode *dir,
2670 struct dentry *dentry,
Alexey Dobriyand5a572a2019-03-11 23:28:51 -07002671 const struct pid_entry *p,
2672 const struct pid_entry *end)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002673{
Eric W. Biederman99f89552006-06-26 00:25:55 -07002674 struct task_struct *task = get_proc_task(dir);
Al Viro0168b9e2018-05-03 09:21:05 -04002675 struct dentry *res = ERR_PTR(-ENOENT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002676
Eric W. Biederman99f89552006-06-26 00:25:55 -07002677 if (!task)
2678 goto out_no_task;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002679
Eric W. Biederman20cdc892006-10-02 02:17:07 -07002680 /*
2681 * Yes, it does not scale. And it should not. Don't add
2682 * new entries into /proc/<tgid>/ without very good reasons.
2683 */
Alexey Dobriyand5a572a2019-03-11 23:28:51 -07002684 for (; p < end; p++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002685 if (p->len != dentry->d_name.len)
2686 continue;
Alexey Dobriyan26b95132018-06-14 15:27:17 -07002687 if (!memcmp(dentry->d_name.name, p->name, p->len)) {
2688 res = proc_pident_instantiate(dentry, task, p);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002689 break;
Alexey Dobriyan26b95132018-06-14 15:27:17 -07002690 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002691 }
Eric W. Biederman99f89552006-06-26 00:25:55 -07002692 put_task_struct(task);
2693out_no_task:
Al Viro0168b9e2018-05-03 09:21:05 -04002694 return res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002695}
2696
Al Virof0c3b502013-05-16 12:07:31 -04002697static int proc_pident_readdir(struct file *file, struct dir_context *ctx,
Eric Dumazetc5141e62007-05-08 00:26:15 -07002698 const struct pid_entry *ents, unsigned int nents)
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002699{
Al Virof0c3b502013-05-16 12:07:31 -04002700 struct task_struct *task = get_proc_task(file_inode(file));
2701 const struct pid_entry *p;
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002702
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002703 if (!task)
Al Virof0c3b502013-05-16 12:07:31 -04002704 return -ENOENT;
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002705
Al Virof0c3b502013-05-16 12:07:31 -04002706 if (!dir_emit_dots(file, ctx))
2707 goto out;
2708
2709 if (ctx->pos >= nents + 2)
2710 goto out;
2711
Alexey Dobriyanbac5f5d2016-12-12 16:45:28 -08002712 for (p = ents + (ctx->pos - 2); p < ents + nents; p++) {
Al Virof0c3b502013-05-16 12:07:31 -04002713 if (!proc_fill_cache(file, ctx, p->name, p->len,
2714 proc_pident_instantiate, task, p))
2715 break;
2716 ctx->pos++;
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002717 }
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002718out:
Eric W. Biederman61a28782006-10-02 02:18:49 -07002719 put_task_struct(task);
Al Virof0c3b502013-05-16 12:07:31 -04002720 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002721}
2722
Linus Torvalds1da177e2005-04-16 15:20:36 -07002723#ifdef CONFIG_SECURITY
Kees Cook591a22c2021-06-08 10:12:21 -07002724static int proc_pid_attr_open(struct inode *inode, struct file *file)
2725{
Linus Torvalds94f0b2d2021-06-15 09:26:19 -07002726 file->private_data = NULL;
2727 __mem_open(inode, file, PTRACE_MODE_READ_FSCREDS);
2728 return 0;
Kees Cook591a22c2021-06-08 10:12:21 -07002729}
2730
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002731static ssize_t proc_pid_attr_read(struct file * file, char __user * buf,
2732 size_t count, loff_t *ppos)
2733{
Al Viro496ad9a2013-01-23 17:07:38 -05002734 struct inode * inode = file_inode(file);
Al Viro04ff9702007-03-12 16:17:58 +00002735 char *p = NULL;
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002736 ssize_t length;
2737 struct task_struct *task = get_proc_task(inode);
2738
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002739 if (!task)
Al Viro04ff9702007-03-12 16:17:58 +00002740 return -ESRCH;
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002741
Casey Schaufler6d9c9392018-09-21 17:16:59 -07002742 length = security_getprocattr(task, PROC_I(inode)->op.lsm,
Josef "Jeff" Sipek2fddfee2006-12-08 02:36:36 -08002743 (char*)file->f_path.dentry->d_name.name,
Al Viro04ff9702007-03-12 16:17:58 +00002744 &p);
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002745 put_task_struct(task);
Al Viro04ff9702007-03-12 16:17:58 +00002746 if (length > 0)
2747 length = simple_read_from_buffer(buf, count, ppos, p, length);
2748 kfree(p);
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002749 return length;
2750}
2751
2752static ssize_t proc_pid_attr_write(struct file * file, const char __user * buf,
2753 size_t count, loff_t *ppos)
2754{
Al Viro496ad9a2013-01-23 17:07:38 -05002755 struct inode * inode = file_inode(file);
Alexey Dobriyan41089b62018-08-21 21:54:30 -07002756 struct task_struct *task;
Al Virobb646cd2015-12-24 00:16:30 -05002757 void *page;
Alexey Dobriyan41089b62018-08-21 21:54:30 -07002758 int rv;
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002759
Kees Cookbfb819e2021-05-25 12:37:35 -07002760 /* A task may only write when it was the opener. */
Kees Cook591a22c2021-06-08 10:12:21 -07002761 if (file->private_data != current->mm)
Kees Cookbfb819e2021-05-25 12:37:35 -07002762 return -EPERM;
2763
Alexey Dobriyan41089b62018-08-21 21:54:30 -07002764 rcu_read_lock();
2765 task = pid_task(proc_pid(inode), PIDTYPE_PID);
2766 if (!task) {
2767 rcu_read_unlock();
2768 return -ESRCH;
2769 }
Stephen Smalleyb21507e2017-01-09 10:07:31 -05002770 /* A task may only write its own attributes. */
Alexey Dobriyan41089b62018-08-21 21:54:30 -07002771 if (current != task) {
2772 rcu_read_unlock();
2773 return -EACCES;
2774 }
Paul Moore35a196b2019-04-19 14:55:12 -04002775 /* Prevent changes to overridden credentials. */
2776 if (current_cred() != current_real_cred()) {
2777 rcu_read_unlock();
2778 return -EBUSY;
2779 }
Alexey Dobriyan41089b62018-08-21 21:54:30 -07002780 rcu_read_unlock();
Stephen Smalleyb21507e2017-01-09 10:07:31 -05002781
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002782 if (count > PAGE_SIZE)
2783 count = PAGE_SIZE;
2784
2785 /* No partial writes. */
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002786 if (*ppos != 0)
Alexey Dobriyan41089b62018-08-21 21:54:30 -07002787 return -EINVAL;
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002788
Al Virobb646cd2015-12-24 00:16:30 -05002789 page = memdup_user(buf, count);
2790 if (IS_ERR(page)) {
Alexey Dobriyan41089b62018-08-21 21:54:30 -07002791 rv = PTR_ERR(page);
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002792 goto out;
Al Virobb646cd2015-12-24 00:16:30 -05002793 }
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002794
David Howells107db7c2009-05-08 13:55:27 +01002795 /* Guard against adverse ptrace interaction */
Alexey Dobriyan41089b62018-08-21 21:54:30 -07002796 rv = mutex_lock_interruptible(&current->signal->cred_guard_mutex);
2797 if (rv < 0)
David Howells107db7c2009-05-08 13:55:27 +01002798 goto out_free;
2799
Casey Schaufler6d9c9392018-09-21 17:16:59 -07002800 rv = security_setprocattr(PROC_I(inode)->op.lsm,
2801 file->f_path.dentry->d_name.name, page,
2802 count);
Stephen Smalleyb21507e2017-01-09 10:07:31 -05002803 mutex_unlock(&current->signal->cred_guard_mutex);
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002804out_free:
Al Virobb646cd2015-12-24 00:16:30 -05002805 kfree(page);
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002806out:
Alexey Dobriyan41089b62018-08-21 21:54:30 -07002807 return rv;
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002808}
2809
Arjan van de Ven00977a52007-02-12 00:55:34 -08002810static const struct file_operations proc_pid_attr_operations = {
Kees Cook591a22c2021-06-08 10:12:21 -07002811 .open = proc_pid_attr_open,
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002812 .read = proc_pid_attr_read,
2813 .write = proc_pid_attr_write,
Arnd Bergmann87df8422010-03-17 23:06:02 +01002814 .llseek = generic_file_llseek,
Kees Cook591a22c2021-06-08 10:12:21 -07002815 .release = mem_release,
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002816};
2817
Casey Schaufler6d9c9392018-09-21 17:16:59 -07002818#define LSM_DIR_OPS(LSM) \
2819static int proc_##LSM##_attr_dir_iterate(struct file *filp, \
2820 struct dir_context *ctx) \
2821{ \
2822 return proc_pident_readdir(filp, ctx, \
2823 LSM##_attr_dir_stuff, \
2824 ARRAY_SIZE(LSM##_attr_dir_stuff)); \
2825} \
2826\
2827static const struct file_operations proc_##LSM##_attr_dir_ops = { \
2828 .read = generic_read_dir, \
2829 .iterate = proc_##LSM##_attr_dir_iterate, \
2830 .llseek = default_llseek, \
2831}; \
2832\
2833static struct dentry *proc_##LSM##_attr_dir_lookup(struct inode *dir, \
2834 struct dentry *dentry, unsigned int flags) \
2835{ \
2836 return proc_pident_lookup(dir, dentry, \
2837 LSM##_attr_dir_stuff, \
Alexey Dobriyand5a572a2019-03-11 23:28:51 -07002838 LSM##_attr_dir_stuff + ARRAY_SIZE(LSM##_attr_dir_stuff)); \
Casey Schaufler6d9c9392018-09-21 17:16:59 -07002839} \
2840\
2841static const struct inode_operations proc_##LSM##_attr_dir_inode_ops = { \
2842 .lookup = proc_##LSM##_attr_dir_lookup, \
2843 .getattr = pid_getattr, \
2844 .setattr = proc_setattr, \
2845}
2846
2847#ifdef CONFIG_SECURITY_SMACK
2848static const struct pid_entry smack_attr_dir_stuff[] = {
2849 ATTR("smack", "current", 0666),
2850};
2851LSM_DIR_OPS(smack);
2852#endif
2853
John Johansen6413f852019-02-04 05:23:14 -08002854#ifdef CONFIG_SECURITY_APPARMOR
2855static const struct pid_entry apparmor_attr_dir_stuff[] = {
2856 ATTR("apparmor", "current", 0666),
2857 ATTR("apparmor", "prev", 0444),
2858 ATTR("apparmor", "exec", 0666),
2859};
2860LSM_DIR_OPS(apparmor);
2861#endif
2862
Eric Dumazetc5141e62007-05-08 00:26:15 -07002863static const struct pid_entry attr_dir_stuff[] = {
Casey Schaufler6d9c9392018-09-21 17:16:59 -07002864 ATTR(NULL, "current", 0666),
2865 ATTR(NULL, "prev", 0444),
2866 ATTR(NULL, "exec", 0666),
2867 ATTR(NULL, "fscreate", 0666),
2868 ATTR(NULL, "keycreate", 0666),
2869 ATTR(NULL, "sockcreate", 0666),
2870#ifdef CONFIG_SECURITY_SMACK
2871 DIR("smack", 0555,
2872 proc_smack_attr_dir_inode_ops, proc_smack_attr_dir_ops),
2873#endif
John Johansen6413f852019-02-04 05:23:14 -08002874#ifdef CONFIG_SECURITY_APPARMOR
2875 DIR("apparmor", 0555,
2876 proc_apparmor_attr_dir_inode_ops, proc_apparmor_attr_dir_ops),
2877#endif
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002878};
2879
Al Virof0c3b502013-05-16 12:07:31 -04002880static int proc_attr_dir_readdir(struct file *file, struct dir_context *ctx)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002881{
Al Virof0c3b502013-05-16 12:07:31 -04002882 return proc_pident_readdir(file, ctx,
2883 attr_dir_stuff, ARRAY_SIZE(attr_dir_stuff));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002884}
2885
Arjan van de Ven00977a52007-02-12 00:55:34 -08002886static const struct file_operations proc_attr_dir_operations = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002887 .read = generic_read_dir,
Al Virof50752e2016-04-20 17:13:54 -04002888 .iterate_shared = proc_attr_dir_readdir,
2889 .llseek = generic_file_llseek,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002890};
2891
Eric W. Biederman72d9dcf2006-10-02 02:18:50 -07002892static struct dentry *proc_attr_dir_lookup(struct inode *dir,
Al Viro00cd8dd2012-06-10 17:13:09 -04002893 struct dentry *dentry, unsigned int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002894{
Eric W. Biederman7bcd6b02006-10-02 02:18:56 -07002895 return proc_pident_lookup(dir, dentry,
Alexey Dobriyand5a572a2019-03-11 23:28:51 -07002896 attr_dir_stuff,
2897 attr_dir_stuff + ARRAY_SIZE(attr_dir_stuff));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002898}
2899
Arjan van de Venc5ef1c42007-02-12 00:55:40 -08002900static const struct inode_operations proc_attr_dir_inode_operations = {
Eric W. Biederman72d9dcf2006-10-02 02:18:50 -07002901 .lookup = proc_attr_dir_lookup,
Eric W. Biederman99f89552006-06-26 00:25:55 -07002902 .getattr = pid_getattr,
Linus Torvalds6d76fa52006-07-15 12:26:45 -07002903 .setattr = proc_setattr,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002904};
2905
Linus Torvalds1da177e2005-04-16 15:20:36 -07002906#endif
2907
Christoph Hellwig698ba7b2009-12-15 16:47:37 -08002908#ifdef CONFIG_ELF_CORE
Kawai, Hidehiro3cb4a0b2007-07-19 01:48:28 -07002909static ssize_t proc_coredump_filter_read(struct file *file, char __user *buf,
2910 size_t count, loff_t *ppos)
2911{
Al Viro496ad9a2013-01-23 17:07:38 -05002912 struct task_struct *task = get_proc_task(file_inode(file));
Kawai, Hidehiro3cb4a0b2007-07-19 01:48:28 -07002913 struct mm_struct *mm;
2914 char buffer[PROC_NUMBUF];
2915 size_t len;
2916 int ret;
2917
2918 if (!task)
2919 return -ESRCH;
2920
2921 ret = 0;
2922 mm = get_task_mm(task);
2923 if (mm) {
2924 len = snprintf(buffer, sizeof(buffer), "%08lx\n",
2925 ((mm->flags & MMF_DUMP_FILTER_MASK) >>
2926 MMF_DUMP_FILTER_SHIFT));
2927 mmput(mm);
2928 ret = simple_read_from_buffer(buf, count, ppos, buffer, len);
2929 }
2930
2931 put_task_struct(task);
2932
2933 return ret;
2934}
2935
2936static ssize_t proc_coredump_filter_write(struct file *file,
2937 const char __user *buf,
2938 size_t count,
2939 loff_t *ppos)
2940{
2941 struct task_struct *task;
2942 struct mm_struct *mm;
Kawai, Hidehiro3cb4a0b2007-07-19 01:48:28 -07002943 unsigned int val;
2944 int ret;
2945 int i;
2946 unsigned long mask;
2947
Alexey Dobriyan774636e2015-09-09 15:36:59 -07002948 ret = kstrtouint_from_user(buf, count, 0, &val);
2949 if (ret < 0)
2950 return ret;
Kawai, Hidehiro3cb4a0b2007-07-19 01:48:28 -07002951
2952 ret = -ESRCH;
Al Viro496ad9a2013-01-23 17:07:38 -05002953 task = get_proc_task(file_inode(file));
Kawai, Hidehiro3cb4a0b2007-07-19 01:48:28 -07002954 if (!task)
2955 goto out_no_task;
2956
Kawai, Hidehiro3cb4a0b2007-07-19 01:48:28 -07002957 mm = get_task_mm(task);
2958 if (!mm)
2959 goto out_no_mm;
Colin Ian King41a0c2492015-12-18 14:22:01 -08002960 ret = 0;
Kawai, Hidehiro3cb4a0b2007-07-19 01:48:28 -07002961
2962 for (i = 0, mask = 1; i < MMF_DUMP_FILTER_BITS; i++, mask <<= 1) {
2963 if (val & mask)
2964 set_bit(i + MMF_DUMP_FILTER_SHIFT, &mm->flags);
2965 else
2966 clear_bit(i + MMF_DUMP_FILTER_SHIFT, &mm->flags);
2967 }
2968
2969 mmput(mm);
2970 out_no_mm:
2971 put_task_struct(task);
2972 out_no_task:
Alexey Dobriyan774636e2015-09-09 15:36:59 -07002973 if (ret < 0)
2974 return ret;
2975 return count;
Kawai, Hidehiro3cb4a0b2007-07-19 01:48:28 -07002976}
2977
2978static const struct file_operations proc_coredump_filter_operations = {
2979 .read = proc_coredump_filter_read,
2980 .write = proc_coredump_filter_write,
Arnd Bergmann87df8422010-03-17 23:06:02 +01002981 .llseek = generic_file_llseek,
Kawai, Hidehiro3cb4a0b2007-07-19 01:48:28 -07002982};
2983#endif
2984
Andrew Mortonaba76fd2006-12-10 02:19:48 -08002985#ifdef CONFIG_TASK_IO_ACCOUNTING
Alexey Dobriyan19aadc92014-08-08 14:21:50 -07002986static int do_io_accounting(struct task_struct *task, struct seq_file *m, int whole)
Andrew Mortonaba76fd2006-12-10 02:19:48 -08002987{
Andrea Righi940389b2008-07-28 00:48:12 +02002988 struct task_io_accounting acct = task->ioac;
Andrea Righi59954772008-07-27 17:29:15 +02002989 unsigned long flags;
Vasiliy Kulikov293eb1e2011-07-26 16:08:38 -07002990 int result;
Andrea Righi297c5d92008-07-25 01:48:49 -07002991
Eric W. Biedermanf7cfd872020-12-03 14:12:00 -06002992 result = down_read_killable(&task->signal->exec_update_lock);
Vasiliy Kulikov293eb1e2011-07-26 16:08:38 -07002993 if (result)
2994 return result;
2995
Jann Horncaaee622016-01-20 15:00:04 -08002996 if (!ptrace_may_access(task, PTRACE_MODE_READ_FSCREDS)) {
Vasiliy Kulikov293eb1e2011-07-26 16:08:38 -07002997 result = -EACCES;
2998 goto out_unlock;
2999 }
Vasiliy Kulikov1d1221f2011-06-24 16:08:38 +04003000
Andrea Righi59954772008-07-27 17:29:15 +02003001 if (whole && lock_task_sighand(task, &flags)) {
3002 struct task_struct *t = task;
Andrea Righib2d002d2008-07-26 15:22:27 -07003003
Andrea Righi59954772008-07-27 17:29:15 +02003004 task_io_accounting_add(&acct, &task->signal->ioac);
3005 while_each_thread(task, t)
3006 task_io_accounting_add(&acct, &t->ioac);
Andrea Righi297c5d92008-07-25 01:48:49 -07003007
Andrea Righi59954772008-07-27 17:29:15 +02003008 unlock_task_sighand(task, &flags);
Andrea Righi297c5d92008-07-25 01:48:49 -07003009 }
Joe Perches25ce3192015-04-15 16:18:17 -07003010 seq_printf(m,
3011 "rchar: %llu\n"
3012 "wchar: %llu\n"
3013 "syscr: %llu\n"
3014 "syscw: %llu\n"
3015 "read_bytes: %llu\n"
3016 "write_bytes: %llu\n"
3017 "cancelled_write_bytes: %llu\n",
3018 (unsigned long long)acct.rchar,
3019 (unsigned long long)acct.wchar,
3020 (unsigned long long)acct.syscr,
3021 (unsigned long long)acct.syscw,
3022 (unsigned long long)acct.read_bytes,
3023 (unsigned long long)acct.write_bytes,
3024 (unsigned long long)acct.cancelled_write_bytes);
3025 result = 0;
3026
Vasiliy Kulikov293eb1e2011-07-26 16:08:38 -07003027out_unlock:
Eric W. Biedermanf7cfd872020-12-03 14:12:00 -06003028 up_read(&task->signal->exec_update_lock);
Vasiliy Kulikov293eb1e2011-07-26 16:08:38 -07003029 return result;
Andrew Mortonaba76fd2006-12-10 02:19:48 -08003030}
Andrea Righi297c5d92008-07-25 01:48:49 -07003031
Alexey Dobriyan19aadc92014-08-08 14:21:50 -07003032static int proc_tid_io_accounting(struct seq_file *m, struct pid_namespace *ns,
3033 struct pid *pid, struct task_struct *task)
Andrea Righi297c5d92008-07-25 01:48:49 -07003034{
Alexey Dobriyan19aadc92014-08-08 14:21:50 -07003035 return do_io_accounting(task, m, 0);
Andrea Righi297c5d92008-07-25 01:48:49 -07003036}
3037
Alexey Dobriyan19aadc92014-08-08 14:21:50 -07003038static int proc_tgid_io_accounting(struct seq_file *m, struct pid_namespace *ns,
3039 struct pid *pid, struct task_struct *task)
Andrea Righi297c5d92008-07-25 01:48:49 -07003040{
Alexey Dobriyan19aadc92014-08-08 14:21:50 -07003041 return do_io_accounting(task, m, 1);
Andrea Righi297c5d92008-07-25 01:48:49 -07003042}
3043#endif /* CONFIG_TASK_IO_ACCOUNTING */
Andrew Mortonaba76fd2006-12-10 02:19:48 -08003044
Eric W. Biederman22d917d2011-11-17 00:11:58 -08003045#ifdef CONFIG_USER_NS
3046static int proc_id_map_open(struct inode *inode, struct file *file,
Fabian Frederickccf94f12014-08-08 14:21:22 -07003047 const struct seq_operations *seq_ops)
Eric W. Biederman22d917d2011-11-17 00:11:58 -08003048{
3049 struct user_namespace *ns = NULL;
3050 struct task_struct *task;
3051 struct seq_file *seq;
3052 int ret = -EINVAL;
3053
3054 task = get_proc_task(inode);
3055 if (task) {
3056 rcu_read_lock();
3057 ns = get_user_ns(task_cred_xxx(task, user_ns));
3058 rcu_read_unlock();
3059 put_task_struct(task);
3060 }
3061 if (!ns)
3062 goto err;
3063
3064 ret = seq_open(file, seq_ops);
3065 if (ret)
3066 goto err_put_ns;
3067
3068 seq = file->private_data;
3069 seq->private = ns;
3070
3071 return 0;
3072err_put_ns:
3073 put_user_ns(ns);
3074err:
3075 return ret;
3076}
3077
3078static int proc_id_map_release(struct inode *inode, struct file *file)
3079{
3080 struct seq_file *seq = file->private_data;
3081 struct user_namespace *ns = seq->private;
3082 put_user_ns(ns);
3083 return seq_release(inode, file);
3084}
3085
3086static int proc_uid_map_open(struct inode *inode, struct file *file)
3087{
3088 return proc_id_map_open(inode, file, &proc_uid_seq_operations);
3089}
3090
3091static int proc_gid_map_open(struct inode *inode, struct file *file)
3092{
3093 return proc_id_map_open(inode, file, &proc_gid_seq_operations);
3094}
3095
Eric W. Biedermanf76d2072012-08-30 01:24:05 -07003096static int proc_projid_map_open(struct inode *inode, struct file *file)
3097{
3098 return proc_id_map_open(inode, file, &proc_projid_seq_operations);
3099}
3100
Eric W. Biederman22d917d2011-11-17 00:11:58 -08003101static const struct file_operations proc_uid_map_operations = {
3102 .open = proc_uid_map_open,
3103 .write = proc_uid_map_write,
3104 .read = seq_read,
3105 .llseek = seq_lseek,
3106 .release = proc_id_map_release,
3107};
3108
3109static const struct file_operations proc_gid_map_operations = {
3110 .open = proc_gid_map_open,
3111 .write = proc_gid_map_write,
3112 .read = seq_read,
3113 .llseek = seq_lseek,
3114 .release = proc_id_map_release,
3115};
Eric W. Biedermanf76d2072012-08-30 01:24:05 -07003116
3117static const struct file_operations proc_projid_map_operations = {
3118 .open = proc_projid_map_open,
3119 .write = proc_projid_map_write,
3120 .read = seq_read,
3121 .llseek = seq_lseek,
3122 .release = proc_id_map_release,
3123};
Eric W. Biederman9cc46512014-12-02 12:27:26 -06003124
3125static int proc_setgroups_open(struct inode *inode, struct file *file)
3126{
3127 struct user_namespace *ns = NULL;
3128 struct task_struct *task;
3129 int ret;
3130
3131 ret = -ESRCH;
3132 task = get_proc_task(inode);
3133 if (task) {
3134 rcu_read_lock();
3135 ns = get_user_ns(task_cred_xxx(task, user_ns));
3136 rcu_read_unlock();
3137 put_task_struct(task);
3138 }
3139 if (!ns)
3140 goto err;
3141
3142 if (file->f_mode & FMODE_WRITE) {
3143 ret = -EACCES;
3144 if (!ns_capable(ns, CAP_SYS_ADMIN))
3145 goto err_put_ns;
3146 }
3147
3148 ret = single_open(file, &proc_setgroups_show, ns);
3149 if (ret)
3150 goto err_put_ns;
3151
3152 return 0;
3153err_put_ns:
3154 put_user_ns(ns);
3155err:
3156 return ret;
3157}
3158
3159static int proc_setgroups_release(struct inode *inode, struct file *file)
3160{
3161 struct seq_file *seq = file->private_data;
3162 struct user_namespace *ns = seq->private;
3163 int ret = single_release(inode, file);
3164 put_user_ns(ns);
3165 return ret;
3166}
3167
3168static const struct file_operations proc_setgroups_operations = {
3169 .open = proc_setgroups_open,
3170 .write = proc_setgroups_write,
3171 .read = seq_read,
3172 .llseek = seq_lseek,
3173 .release = proc_setgroups_release,
3174};
Eric W. Biederman22d917d2011-11-17 00:11:58 -08003175#endif /* CONFIG_USER_NS */
3176
Kees Cook47830722008-10-06 03:11:58 +04003177static int proc_pid_personality(struct seq_file *m, struct pid_namespace *ns,
3178 struct pid *pid, struct task_struct *task)
3179{
Al Viroa9712bc2011-03-23 15:52:50 -04003180 int err = lock_trace(task);
3181 if (!err) {
3182 seq_printf(m, "%08x\n", task->personality);
3183 unlock_trace(task);
3184 }
3185 return err;
Kees Cook47830722008-10-06 03:11:58 +04003186}
3187
Josh Poimboeuf7c23b332017-02-13 19:42:41 -06003188#ifdef CONFIG_LIVEPATCH
3189static int proc_pid_patch_state(struct seq_file *m, struct pid_namespace *ns,
3190 struct pid *pid, struct task_struct *task)
3191{
3192 seq_printf(m, "%d\n", task->patch_state);
3193 return 0;
3194}
3195#endif /* CONFIG_LIVEPATCH */
3196
Alexander Popovc8d12622018-08-17 01:17:01 +03003197#ifdef CONFIG_STACKLEAK_METRICS
3198static int proc_stack_depth(struct seq_file *m, struct pid_namespace *ns,
3199 struct pid *pid, struct task_struct *task)
3200{
3201 unsigned long prev_depth = THREAD_SIZE -
3202 (task->prev_lowest_stack & (THREAD_SIZE - 1));
3203 unsigned long depth = THREAD_SIZE -
3204 (task->lowest_stack & (THREAD_SIZE - 1));
3205
3206 seq_printf(m, "previous stack depth: %lu\nstack depth: %lu\n",
3207 prev_depth, depth);
3208 return 0;
3209}
3210#endif /* CONFIG_STACKLEAK_METRICS */
3211
Eric W. Biederman801199c2006-10-02 02:18:48 -07003212/*
Eric W. Biederman28a6d672006-10-02 02:17:05 -07003213 * Thread groups
3214 */
Arjan van de Ven00977a52007-02-12 00:55:34 -08003215static const struct file_operations proc_task_operations;
Arjan van de Venc5ef1c42007-02-12 00:55:40 -08003216static const struct inode_operations proc_task_inode_operations;
Eric W. Biederman20cdc892006-10-02 02:17:07 -07003217
Eric Dumazetc5141e62007-05-08 00:26:15 -07003218static const struct pid_entry tgid_base_stuff[] = {
Alexey Dobriyan631f9c12008-11-10 01:32:52 +03003219 DIR("task", S_IRUGO|S_IXUGO, proc_task_inode_operations, proc_task_operations),
3220 DIR("fd", S_IRUSR|S_IXUSR, proc_fd_inode_operations, proc_fd_operations),
Pavel Emelyanov640708a2012-01-10 15:11:23 -08003221 DIR("map_files", S_IRUSR|S_IXUSR, proc_map_files_inode_operations, proc_map_files_operations),
Kalesh Singh7bc3fa02021-06-30 18:54:44 -07003222 DIR("fdinfo", S_IRUGO|S_IXUGO, proc_fdinfo_inode_operations, proc_fdinfo_operations),
Eric W. Biederman6b4e3062010-03-07 16:41:34 -08003223 DIR("ns", S_IRUSR|S_IXUGO, proc_ns_dir_inode_operations, proc_ns_dir_operations),
Andrew Mortonb2211a32008-03-11 18:03:35 -07003224#ifdef CONFIG_NET
Alexey Dobriyan631f9c12008-11-10 01:32:52 +03003225 DIR("net", S_IRUGO|S_IXUGO, proc_net_inode_operations, proc_net_operations),
Andrew Mortonb2211a32008-03-11 18:03:35 -07003226#endif
Alexey Dobriyan631f9c12008-11-10 01:32:52 +03003227 REG("environ", S_IRUSR, proc_environ_operations),
Al Viroc5317162016-10-05 18:43:43 -04003228 REG("auxv", S_IRUSR, proc_auxv_operations),
Alexey Dobriyan631f9c12008-11-10 01:32:52 +03003229 ONE("status", S_IRUGO, proc_pid_status),
Djalal Harouni35a35042014-04-07 15:38:36 -07003230 ONE("personality", S_IRUSR, proc_pid_personality),
Alexey Dobriyan1c963eb2014-08-08 14:21:37 -07003231 ONE("limits", S_IRUGO, proc_pid_limits),
Ingo Molnar43ae34c2007-07-09 18:52:00 +02003232#ifdef CONFIG_SCHED_DEBUG
Alexey Dobriyan631f9c12008-11-10 01:32:52 +03003233 REG("sched", S_IRUGO|S_IWUSR, proc_pid_sched_operations),
Ingo Molnar43ae34c2007-07-09 18:52:00 +02003234#endif
Mike Galbraith5091faa2010-11-30 14:18:03 +01003235#ifdef CONFIG_SCHED_AUTOGROUP
3236 REG("autogroup", S_IRUGO|S_IWUSR, proc_pid_sched_autogroup_operations),
3237#endif
Andrei Vagin04a86822019-11-12 01:27:16 +00003238#ifdef CONFIG_TIME_NS
3239 REG("timens_offsets", S_IRUGO|S_IWUSR, proc_timens_offsets_operations),
3240#endif
john stultz4614a696b2009-12-14 18:00:05 -08003241 REG("comm", S_IRUGO|S_IWUSR, proc_pid_set_comm_operations),
Roland McGrathebcb6732008-07-25 19:46:00 -07003242#ifdef CONFIG_HAVE_ARCH_TRACEHOOK
Alexey Dobriyan09d93bd2014-08-08 14:21:39 -07003243 ONE("syscall", S_IRUSR, proc_pid_syscall),
Roland McGrathebcb6732008-07-25 19:46:00 -07003244#endif
Alexey Dobriyanc2c0bb42015-06-25 15:00:54 -07003245 REG("cmdline", S_IRUGO, proc_pid_cmdline_ops),
Alexey Dobriyan631f9c12008-11-10 01:32:52 +03003246 ONE("stat", S_IRUGO, proc_tgid_stat),
3247 ONE("statm", S_IRUGO, proc_pid_statm),
Siddhesh Poyarekarb7643752012-03-21 16:34:04 -07003248 REG("maps", S_IRUGO, proc_pid_maps_operations),
Eric W. Biederman28a6d672006-10-02 02:17:05 -07003249#ifdef CONFIG_NUMA
Siddhesh Poyarekarb7643752012-03-21 16:34:04 -07003250 REG("numa_maps", S_IRUGO, proc_pid_numa_maps_operations),
Eric W. Biederman28a6d672006-10-02 02:17:05 -07003251#endif
Alexey Dobriyan631f9c12008-11-10 01:32:52 +03003252 REG("mem", S_IRUSR|S_IWUSR, proc_mem_operations),
3253 LNK("cwd", proc_cwd_link),
3254 LNK("root", proc_root_link),
3255 LNK("exe", proc_exe_link),
3256 REG("mounts", S_IRUGO, proc_mounts_operations),
3257 REG("mountinfo", S_IRUGO, proc_mountinfo_operations),
3258 REG("mountstats", S_IRUSR, proc_mountstats_operations),
Matt Mackall1e883282008-02-04 22:29:07 -08003259#ifdef CONFIG_PROC_PAGE_MONITOR
Alexey Dobriyan631f9c12008-11-10 01:32:52 +03003260 REG("clear_refs", S_IWUSR, proc_clear_refs_operations),
Siddhesh Poyarekarb7643752012-03-21 16:34:04 -07003261 REG("smaps", S_IRUGO, proc_pid_smaps_operations),
Daniel Colascione493b0e92017-09-06 16:25:08 -07003262 REG("smaps_rollup", S_IRUGO, proc_pid_smaps_rollup_operations),
Djalal Harouni32ed74a2014-04-07 15:38:38 -07003263 REG("pagemap", S_IRUSR, proc_pagemap_operations),
Eric W. Biederman28a6d672006-10-02 02:17:05 -07003264#endif
3265#ifdef CONFIG_SECURITY
Alexey Dobriyan631f9c12008-11-10 01:32:52 +03003266 DIR("attr", S_IRUGO|S_IXUGO, proc_attr_dir_inode_operations, proc_attr_dir_operations),
Eric W. Biederman28a6d672006-10-02 02:17:05 -07003267#endif
3268#ifdef CONFIG_KALLSYMS
Alexey Dobriyanedfcd602014-08-08 14:21:44 -07003269 ONE("wchan", S_IRUGO, proc_pid_wchan),
Eric W. Biederman28a6d672006-10-02 02:17:05 -07003270#endif
Ken Chen2ec220e2008-11-10 11:26:08 +03003271#ifdef CONFIG_STACKTRACE
Djalal Harouni35a35042014-04-07 15:38:36 -07003272 ONE("stack", S_IRUSR, proc_pid_stack),
Eric W. Biederman28a6d672006-10-02 02:17:05 -07003273#endif
Naveen N. Rao5968cec2015-06-30 14:36:03 +05303274#ifdef CONFIG_SCHED_INFO
Alexey Dobriyanf6e826c2014-08-08 14:21:46 -07003275 ONE("schedstat", S_IRUGO, proc_pid_schedstat),
Eric W. Biederman28a6d672006-10-02 02:17:05 -07003276#endif
Arjan van de Ven97455122008-01-25 21:08:34 +01003277#ifdef CONFIG_LATENCYTOP
Alexey Dobriyan631f9c12008-11-10 01:32:52 +03003278 REG("latency", S_IRUGO, proc_lstats_operations),
Arjan van de Ven97455122008-01-25 21:08:34 +01003279#endif
Paul Menage8793d852007-10-18 23:39:39 -07003280#ifdef CONFIG_PROC_PID_CPUSET
Zefan Li52de4772014-09-18 16:03:36 +08003281 ONE("cpuset", S_IRUGO, proc_cpuset_show),
Eric W. Biederman28a6d672006-10-02 02:17:05 -07003282#endif
Paul Menagea4243162007-10-18 23:39:35 -07003283#ifdef CONFIG_CGROUPS
Zefan Li006f4ac2014-09-18 16:03:15 +08003284 ONE("cgroup", S_IRUGO, proc_cgroup_show),
Paul Menagea4243162007-10-18 23:39:35 -07003285#endif
Chen Yue79f15a2020-01-15 17:28:51 +08003286#ifdef CONFIG_PROC_CPU_RESCTRL
3287 ONE("cpu_resctrl_groups", S_IRUGO, proc_resctrl_show),
3288#endif
Alexey Dobriyan6ba51e32014-08-08 14:21:48 -07003289 ONE("oom_score", S_IRUGO, proc_oom_score),
David Rientjesfa0cbbf2012-11-12 17:53:04 -08003290 REG("oom_adj", S_IRUGO|S_IWUSR, proc_oom_adj_operations),
David Rientjesa63d83f2010-08-09 17:19:46 -07003291 REG("oom_score_adj", S_IRUGO|S_IWUSR, proc_oom_score_adj_operations),
Richard Guy Briggs4b7d2482019-01-22 17:06:39 -05003292#ifdef CONFIG_AUDIT
Alexey Dobriyan631f9c12008-11-10 01:32:52 +03003293 REG("loginuid", S_IWUSR|S_IRUGO, proc_loginuid_operations),
3294 REG("sessionid", S_IRUGO, proc_sessionid_operations),
Eric W. Biederman28a6d672006-10-02 02:17:05 -07003295#endif
Akinobu Mitaf4f154f2006-12-08 02:39:47 -08003296#ifdef CONFIG_FAULT_INJECTION
Alexey Dobriyan631f9c12008-11-10 01:32:52 +03003297 REG("make-it-fail", S_IRUGO|S_IWUSR, proc_fault_inject_operations),
Akinobu Mita168c42b2017-07-14 14:50:00 -07003298 REG("fail-nth", 0644, proc_fail_nth_operations),
Akinobu Mitaf4f154f2006-12-08 02:39:47 -08003299#endif
Christoph Hellwig698ba7b2009-12-15 16:47:37 -08003300#ifdef CONFIG_ELF_CORE
Alexey Dobriyan631f9c12008-11-10 01:32:52 +03003301 REG("coredump_filter", S_IRUGO|S_IWUSR, proc_coredump_filter_operations),
Kawai, Hidehiro3cb4a0b2007-07-19 01:48:28 -07003302#endif
Andrew Mortonaba76fd2006-12-10 02:19:48 -08003303#ifdef CONFIG_TASK_IO_ACCOUNTING
Alexey Dobriyan19aadc92014-08-08 14:21:50 -07003304 ONE("io", S_IRUSR, proc_tgid_io_accounting),
Andrew Mortonaba76fd2006-12-10 02:19:48 -08003305#endif
Eric W. Biederman22d917d2011-11-17 00:11:58 -08003306#ifdef CONFIG_USER_NS
3307 REG("uid_map", S_IRUGO|S_IWUSR, proc_uid_map_operations),
3308 REG("gid_map", S_IRUGO|S_IWUSR, proc_gid_map_operations),
Eric W. Biedermanf76d2072012-08-30 01:24:05 -07003309 REG("projid_map", S_IRUGO|S_IWUSR, proc_projid_map_operations),
Eric W. Biederman9cc46512014-12-02 12:27:26 -06003310 REG("setgroups", S_IRUGO|S_IWUSR, proc_setgroups_operations),
Eric W. Biederman22d917d2011-11-17 00:11:58 -08003311#endif
Nicolas Pitreb18b6a92017-01-21 00:09:08 -05003312#if defined(CONFIG_CHECKPOINT_RESTORE) && defined(CONFIG_POSIX_TIMERS)
Pavel Emelyanov48f6a7a2013-03-11 13:12:45 +04003313 REG("timers", S_IRUGO, proc_timers_operations),
3314#endif
John Stultz5de23d42016-03-17 14:20:54 -07003315 REG("timerslack_ns", S_IRUGO|S_IWUGO, proc_pid_set_timerslack_ns_operations),
Josh Poimboeuf7c23b332017-02-13 19:42:41 -06003316#ifdef CONFIG_LIVEPATCH
3317 ONE("patch_state", S_IRUSR, proc_pid_patch_state),
3318#endif
Connor O'Brien8bea10e2018-01-31 18:11:57 -08003319#ifdef CONFIG_CPU_FREQ_TIMES
3320 ONE("time_in_state", 0444, proc_time_in_state_show),
3321#endif
Alexander Popovc8d12622018-08-17 01:17:01 +03003322#ifdef CONFIG_STACKLEAK_METRICS
3323 ONE("stack_depth", S_IRUGO, proc_stack_depth),
3324#endif
Aubrey Li68bc30b2019-06-06 09:22:34 +08003325#ifdef CONFIG_PROC_PID_ARCH_STATUS
3326 ONE("arch_status", S_IRUGO, proc_pid_arch_status),
3327#endif
YiFei Zhu0d8315d2020-11-11 07:33:54 -06003328#ifdef CONFIG_SECCOMP_CACHE_DEBUG
3329 ONE("seccomp_cache", S_IRUSR, proc_pid_seccomp_cache),
3330#endif
Eric W. Biederman28a6d672006-10-02 02:17:05 -07003331};
3332
Al Virof0c3b502013-05-16 12:07:31 -04003333static int proc_tgid_base_readdir(struct file *file, struct dir_context *ctx)
Eric W. Biederman28a6d672006-10-02 02:17:05 -07003334{
Al Virof0c3b502013-05-16 12:07:31 -04003335 return proc_pident_readdir(file, ctx,
3336 tgid_base_stuff, ARRAY_SIZE(tgid_base_stuff));
Eric W. Biederman28a6d672006-10-02 02:17:05 -07003337}
3338
Arjan van de Ven00977a52007-02-12 00:55:34 -08003339static const struct file_operations proc_tgid_base_operations = {
Eric W. Biederman28a6d672006-10-02 02:17:05 -07003340 .read = generic_read_dir,
Al Virof50752e2016-04-20 17:13:54 -04003341 .iterate_shared = proc_tgid_base_readdir,
3342 .llseek = generic_file_llseek,
Eric W. Biederman28a6d672006-10-02 02:17:05 -07003343};
3344
Christian Brauner3eb39f42018-11-19 00:51:56 +01003345struct pid *tgid_pidfd_to_pid(const struct file *file)
3346{
Christian Brauner30d158b2019-06-27 11:35:14 +02003347 if (file->f_op != &proc_tgid_base_operations)
Christian Brauner3eb39f42018-11-19 00:51:56 +01003348 return ERR_PTR(-EBADF);
3349
3350 return proc_pid(file_inode(file));
3351}
3352
Al Viro00cd8dd2012-06-10 17:13:09 -04003353static struct dentry *proc_tgid_base_lookup(struct inode *dir, struct dentry *dentry, unsigned int flags)
3354{
Eric W. Biederman7bcd6b02006-10-02 02:18:56 -07003355 return proc_pident_lookup(dir, dentry,
Alexey Dobriyand5a572a2019-03-11 23:28:51 -07003356 tgid_base_stuff,
3357 tgid_base_stuff + ARRAY_SIZE(tgid_base_stuff));
Eric W. Biederman28a6d672006-10-02 02:17:05 -07003358}
3359
Arjan van de Venc5ef1c42007-02-12 00:55:40 -08003360static const struct inode_operations proc_tgid_base_inode_operations = {
Eric W. Biederman28a6d672006-10-02 02:17:05 -07003361 .lookup = proc_tgid_base_lookup,
3362 .getattr = pid_getattr,
3363 .setattr = proc_setattr,
Vasiliy Kulikov04996802012-01-10 15:11:31 -08003364 .permission = proc_pid_permission,
Eric W. Biederman28a6d672006-10-02 02:17:05 -07003365};
3366
Randy Dunlap0895e912007-10-21 21:00:10 -07003367/**
Eric W. Biederman7bc3e6e2020-02-19 18:22:26 -06003368 * proc_flush_pid - Remove dcache entries for @pid from the /proc dcache.
3369 * @pid: pid that should be flushed.
Randy Dunlap0895e912007-10-21 21:00:10 -07003370 *
Eric W. Biederman7bc3e6e2020-02-19 18:22:26 -06003371 * This function walks a list of inodes (that belong to any proc
3372 * filesystem) that are attached to the pid and flushes them from
3373 * the dentry cache.
Randy Dunlap0895e912007-10-21 21:00:10 -07003374 *
3375 * It is safe and reasonable to cache /proc entries for a task until
3376 * that task exits. After that they just clog up the dcache with
3377 * useless entries, possibly causing useful dcache entries to be
Eric W. Biederman7bc3e6e2020-02-19 18:22:26 -06003378 * flushed instead. This routine is provided to flush those useless
3379 * dcache entries when a process is reaped.
Randy Dunlap0895e912007-10-21 21:00:10 -07003380 *
3381 * NOTE: This routine is just an optimization so it does not guarantee
Eric W. Biederman7bc3e6e2020-02-19 18:22:26 -06003382 * that no dcache entries will exist after a process is reaped
3383 * it just makes it very unlikely that any will persist.
Pavel Emelyanov60347f62007-10-18 23:40:03 -07003384 */
3385
Eric W. Biederman7bc3e6e2020-02-19 18:22:26 -06003386void proc_flush_pid(struct pid *pid)
Pavel Emelyanov60347f62007-10-18 23:40:03 -07003387{
Eric W. Biederman63f818f2020-04-07 09:43:04 -05003388 proc_invalidate_siblings_dcache(&pid->inodes, &pid->lock);
Pavel Emelyanov60347f62007-10-18 23:40:03 -07003389}
3390
Al Viro0168b9e2018-05-03 09:21:05 -04003391static struct dentry *proc_pid_instantiate(struct dentry * dentry,
Al Viroc52a47a2013-06-15 11:15:20 +04003392 struct task_struct *task, const void *ptr)
Eric W. Biederman444ceed2006-10-02 02:18:49 -07003393{
Eric W. Biederman444ceed2006-10-02 02:18:49 -07003394 struct inode *inode;
3395
Zhihao Cheng3336e7c2022-07-13 21:00:29 +08003396 inode = proc_pid_make_base_inode(dentry->d_sb, task,
3397 S_IFDIR | S_IRUGO | S_IXUGO);
Eric W. Biederman444ceed2006-10-02 02:18:49 -07003398 if (!inode)
Al Viro0168b9e2018-05-03 09:21:05 -04003399 return ERR_PTR(-ENOENT);
Eric W. Biederman444ceed2006-10-02 02:18:49 -07003400
Eric W. Biederman444ceed2006-10-02 02:18:49 -07003401 inode->i_op = &proc_tgid_base_inode_operations;
3402 inode->i_fop = &proc_tgid_base_operations;
3403 inode->i_flags|=S_IMMUTABLE;
Vegard Nossumaed54172008-06-05 22:46:53 -07003404
Alexey Dobriyan1270dd82016-12-12 16:45:32 -08003405 set_nlink(inode, nlink_tgid);
Al Viro1bbc5512018-05-02 21:26:16 -04003406 pid_update_inode(task, inode);
Eric W. Biederman444ceed2006-10-02 02:18:49 -07003407
Nick Pigginfb045ad2011-01-07 17:49:55 +11003408 d_set_d_op(dentry, &pid_dentry_operations);
Al Viro0168b9e2018-05-03 09:21:05 -04003409 return d_splice_alias(inode, dentry);
Eric W. Biederman444ceed2006-10-02 02:18:49 -07003410}
3411
Zhikang Zhang867aacc2019-03-05 15:50:29 -08003412struct dentry *proc_pid_lookup(struct dentry *dentry, unsigned int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003413{
3414 struct task_struct *task;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003415 unsigned tgid;
Alexey Gladkovfa10fed2020-04-19 16:10:52 +02003416 struct proc_fs_info *fs_info;
Pavel Emelyanovb4888932007-10-18 23:40:14 -07003417 struct pid_namespace *ns;
Al Viro0168b9e2018-05-03 09:21:05 -04003418 struct dentry *result = ERR_PTR(-ENOENT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003419
Alexey Dobriyandbcdb502014-08-08 14:21:25 -07003420 tgid = name_to_int(&dentry->d_name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003421 if (tgid == ~0U)
3422 goto out;
3423
Alexey Gladkovfa10fed2020-04-19 16:10:52 +02003424 fs_info = proc_sb_info(dentry->d_sb);
3425 ns = fs_info->pid_ns;
Eric W. Biedermande758732006-06-26 00:25:51 -07003426 rcu_read_lock();
Pavel Emelyanovb4888932007-10-18 23:40:14 -07003427 task = find_task_by_pid_ns(tgid, ns);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003428 if (task)
3429 get_task_struct(task);
Eric W. Biedermande758732006-06-26 00:25:51 -07003430 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07003431 if (!task)
3432 goto out;
3433
Alexey Gladkov24a71ce2020-04-19 16:10:53 +02003434 /* Limit procfs to only ptraceable tasks */
3435 if (fs_info->hide_pid == HIDEPID_NOT_PTRACEABLE) {
3436 if (!has_pid_permissions(fs_info, task, HIDEPID_NO_ACCESS))
3437 goto out_put_task;
3438 }
3439
Al Viro0168b9e2018-05-03 09:21:05 -04003440 result = proc_pid_instantiate(dentry, task, NULL);
Alexey Gladkov24a71ce2020-04-19 16:10:53 +02003441out_put_task:
Eric W. Biederman48e64842006-06-26 00:25:48 -07003442 put_task_struct(task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003443out:
Al Viro0168b9e2018-05-03 09:21:05 -04003444 return result;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003445}
3446
Linus Torvalds1da177e2005-04-16 15:20:36 -07003447/*
Eric W. Biederman0804ef42006-10-02 02:17:04 -07003448 * Find the first task with tgid >= tgid
Eric W. Biederman0bc58a92006-06-26 00:25:50 -07003449 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07003450 */
Eric W. Biederman19fd4bb2007-11-28 16:21:26 -08003451struct tgid_iter {
3452 unsigned int tgid;
Eric W. Biederman0804ef42006-10-02 02:17:04 -07003453 struct task_struct *task;
Eric W. Biederman19fd4bb2007-11-28 16:21:26 -08003454};
3455static struct tgid_iter next_tgid(struct pid_namespace *ns, struct tgid_iter iter)
3456{
Eric W. Biederman0804ef42006-10-02 02:17:04 -07003457 struct pid *pid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003458
Eric W. Biederman19fd4bb2007-11-28 16:21:26 -08003459 if (iter.task)
3460 put_task_struct(iter.task);
Eric W. Biederman0804ef42006-10-02 02:17:04 -07003461 rcu_read_lock();
3462retry:
Eric W. Biederman19fd4bb2007-11-28 16:21:26 -08003463 iter.task = NULL;
3464 pid = find_ge_pid(iter.tgid, ns);
Eric W. Biederman0804ef42006-10-02 02:17:04 -07003465 if (pid) {
Eric W. Biederman19fd4bb2007-11-28 16:21:26 -08003466 iter.tgid = pid_nr_ns(pid, ns);
Eric W. Biederman3147d8a2020-02-24 18:53:09 -06003467 iter.task = pid_task(pid, PIDTYPE_TGID);
3468 if (!iter.task) {
Eric W. Biederman19fd4bb2007-11-28 16:21:26 -08003469 iter.tgid += 1;
Eric W. Biederman0804ef42006-10-02 02:17:04 -07003470 goto retry;
Eric W. Biederman19fd4bb2007-11-28 16:21:26 -08003471 }
3472 get_task_struct(iter.task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003473 }
Eric W. Biederman454cc102006-06-26 00:25:51 -07003474 rcu_read_unlock();
Eric W. Biederman19fd4bb2007-11-28 16:21:26 -08003475 return iter;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003476}
3477
Eric W. Biederman00978752014-07-31 03:10:50 -07003478#define TGID_OFFSET (FIRST_PROCESS_ENTRY + 2)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003479
3480/* for the /proc/ directory itself, after non-process stuff has been done */
Al Virof0c3b502013-05-16 12:07:31 -04003481int proc_pid_readdir(struct file *file, struct dir_context *ctx)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003482{
Eric W. Biederman19fd4bb2007-11-28 16:21:26 -08003483 struct tgid_iter iter;
Alexey Gladkovfa10fed2020-04-19 16:10:52 +02003484 struct proc_fs_info *fs_info = proc_sb_info(file_inode(file)->i_sb);
Alexey Gladkov9d78ede2020-05-18 20:07:38 +02003485 struct pid_namespace *ns = proc_pid_ns(file_inode(file)->i_sb);
Al Virof0c3b502013-05-16 12:07:31 -04003486 loff_t pos = ctx->pos;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003487
Al Viro021ada72013-03-29 19:27:05 -04003488 if (pos >= PID_MAX_LIMIT + TGID_OFFSET)
Al Virof0c3b502013-05-16 12:07:31 -04003489 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003490
Eric W. Biederman00978752014-07-31 03:10:50 -07003491 if (pos == TGID_OFFSET - 2) {
Alexey Gladkovfa10fed2020-04-19 16:10:52 +02003492 struct inode *inode = d_inode(fs_info->proc_self);
Al Virodb963162013-06-15 10:45:10 +04003493 if (!dir_emit(ctx, "self", 4, inode->i_ino, DT_LNK))
Al Virof0c3b502013-05-16 12:07:31 -04003494 return 0;
Eric W. Biederman00978752014-07-31 03:10:50 -07003495 ctx->pos = pos = pos + 1;
Al Viro021ada72013-03-29 19:27:05 -04003496 }
Eric W. Biederman00978752014-07-31 03:10:50 -07003497 if (pos == TGID_OFFSET - 1) {
Alexey Gladkovfa10fed2020-04-19 16:10:52 +02003498 struct inode *inode = d_inode(fs_info->proc_thread_self);
Eric W. Biederman00978752014-07-31 03:10:50 -07003499 if (!dir_emit(ctx, "thread-self", 11, inode->i_ino, DT_LNK))
3500 return 0;
3501 ctx->pos = pos = pos + 1;
3502 }
3503 iter.tgid = pos - TGID_OFFSET;
Eric W. Biederman19fd4bb2007-11-28 16:21:26 -08003504 iter.task = NULL;
Eric W. Biederman19fd4bb2007-11-28 16:21:26 -08003505 for (iter = next_tgid(ns, iter);
3506 iter.task;
3507 iter.tgid += 1, iter = next_tgid(ns, iter)) {
Alexey Dobriyane3912ac2018-02-06 15:36:51 -08003508 char name[10 + 1];
Alexey Dobriyana4ef3892018-06-07 17:10:10 -07003509 unsigned int len;
Eric Dumazet3ba4bce2017-01-24 15:18:07 -08003510
3511 cond_resched();
Alexey Gladkovfa10fed2020-04-19 16:10:52 +02003512 if (!has_pid_permissions(fs_info, iter.task, HIDEPID_INVISIBLE))
Al Virof0c3b502013-05-16 12:07:31 -04003513 continue;
Vasiliy Kulikov04996802012-01-10 15:11:31 -08003514
Alexey Dobriyane3912ac2018-02-06 15:36:51 -08003515 len = snprintf(name, sizeof(name), "%u", iter.tgid);
Al Virof0c3b502013-05-16 12:07:31 -04003516 ctx->pos = iter.tgid + TGID_OFFSET;
3517 if (!proc_fill_cache(file, ctx, name, len,
3518 proc_pid_instantiate, iter.task, NULL)) {
Eric W. Biederman19fd4bb2007-11-28 16:21:26 -08003519 put_task_struct(iter.task);
Al Virof0c3b502013-05-16 12:07:31 -04003520 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003521 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003522 }
Al Virof0c3b502013-05-16 12:07:31 -04003523 ctx->pos = PID_MAX_LIMIT + TGID_OFFSET;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003524 return 0;
3525}
3526
Eric W. Biederman0bc58a92006-06-26 00:25:50 -07003527/*
Janis Danisevskis1b3044e2016-05-20 17:00:08 -07003528 * proc_tid_comm_permission is a special permission function exclusively
3529 * used for the node /proc/<pid>/task/<tid>/comm.
3530 * It bypasses generic permission checks in the case where a task of the same
3531 * task group attempts to access the node.
3532 * The rationale behind this is that glibc and bionic access this node for
3533 * cross thread naming (pthread_set/getname_np(!self)). However, if
3534 * PR_SET_DUMPABLE gets set to 0 this node among others becomes uid=0 gid=0,
3535 * which locks out the cross thread naming implementation.
3536 * This function makes sure that the node is always accessible for members of
3537 * same thread group.
3538 */
Christian Brauner549c7292021-01-21 14:19:43 +01003539static int proc_tid_comm_permission(struct user_namespace *mnt_userns,
3540 struct inode *inode, int mask)
Janis Danisevskis1b3044e2016-05-20 17:00:08 -07003541{
3542 bool is_same_tgroup;
3543 struct task_struct *task;
3544
3545 task = get_proc_task(inode);
3546 if (!task)
3547 return -ESRCH;
3548 is_same_tgroup = same_thread_group(current, task);
3549 put_task_struct(task);
3550
3551 if (likely(is_same_tgroup && !(mask & MAY_EXEC))) {
3552 /* This file (/proc/<pid>/task/<tid>/comm) can always be
3553 * read or written by the members of the corresponding
3554 * thread group.
3555 */
3556 return 0;
3557 }
3558
Christian Brauner47291ba2021-01-21 14:19:24 +01003559 return generic_permission(&init_user_ns, inode, mask);
Janis Danisevskis1b3044e2016-05-20 17:00:08 -07003560}
3561
3562static const struct inode_operations proc_tid_comm_inode_operations = {
3563 .permission = proc_tid_comm_permission,
3564};
3565
3566/*
Eric W. Biederman28a6d672006-10-02 02:17:05 -07003567 * Tasks
3568 */
Eric Dumazetc5141e62007-05-08 00:26:15 -07003569static const struct pid_entry tid_base_stuff[] = {
Alexey Dobriyan631f9c12008-11-10 01:32:52 +03003570 DIR("fd", S_IRUSR|S_IXUSR, proc_fd_inode_operations, proc_fd_operations),
Kalesh Singh7bc3fa02021-06-30 18:54:44 -07003571 DIR("fdinfo", S_IRUGO|S_IXUGO, proc_fdinfo_inode_operations, proc_fdinfo_operations),
Eric W. Biederman6b4e3062010-03-07 16:41:34 -08003572 DIR("ns", S_IRUSR|S_IXUGO, proc_ns_dir_inode_operations, proc_ns_dir_operations),
Eric W. Biederman6ba8ed72014-07-31 16:27:08 -07003573#ifdef CONFIG_NET
3574 DIR("net", S_IRUGO|S_IXUGO, proc_net_inode_operations, proc_net_operations),
3575#endif
Alexey Dobriyan631f9c12008-11-10 01:32:52 +03003576 REG("environ", S_IRUSR, proc_environ_operations),
Al Viroc5317162016-10-05 18:43:43 -04003577 REG("auxv", S_IRUSR, proc_auxv_operations),
Alexey Dobriyan631f9c12008-11-10 01:32:52 +03003578 ONE("status", S_IRUGO, proc_pid_status),
Djalal Harouni35a35042014-04-07 15:38:36 -07003579 ONE("personality", S_IRUSR, proc_pid_personality),
Alexey Dobriyan1c963eb2014-08-08 14:21:37 -07003580 ONE("limits", S_IRUGO, proc_pid_limits),
Ingo Molnar43ae34c2007-07-09 18:52:00 +02003581#ifdef CONFIG_SCHED_DEBUG
Alexey Dobriyan631f9c12008-11-10 01:32:52 +03003582 REG("sched", S_IRUGO|S_IWUSR, proc_pid_sched_operations),
Ingo Molnar43ae34c2007-07-09 18:52:00 +02003583#endif
Janis Danisevskis1b3044e2016-05-20 17:00:08 -07003584 NOD("comm", S_IFREG|S_IRUGO|S_IWUSR,
3585 &proc_tid_comm_inode_operations,
3586 &proc_pid_set_comm_operations, {}),
Roland McGrathebcb6732008-07-25 19:46:00 -07003587#ifdef CONFIG_HAVE_ARCH_TRACEHOOK
Alexey Dobriyan09d93bd2014-08-08 14:21:39 -07003588 ONE("syscall", S_IRUSR, proc_pid_syscall),
Roland McGrathebcb6732008-07-25 19:46:00 -07003589#endif
Alexey Dobriyanc2c0bb42015-06-25 15:00:54 -07003590 REG("cmdline", S_IRUGO, proc_pid_cmdline_ops),
Alexey Dobriyan631f9c12008-11-10 01:32:52 +03003591 ONE("stat", S_IRUGO, proc_tid_stat),
3592 ONE("statm", S_IRUGO, proc_pid_statm),
Vlastimil Babka871305b2018-08-21 21:52:48 -07003593 REG("maps", S_IRUGO, proc_pid_maps_operations),
Iago López Galeiras2e13ba52015-06-25 15:00:57 -07003594#ifdef CONFIG_PROC_CHILDREN
Cyrill Gorcunov818411612012-05-31 16:26:43 -07003595 REG("children", S_IRUGO, proc_tid_children_operations),
3596#endif
Eric W. Biederman28a6d672006-10-02 02:17:05 -07003597#ifdef CONFIG_NUMA
Vlastimil Babka871305b2018-08-21 21:52:48 -07003598 REG("numa_maps", S_IRUGO, proc_pid_numa_maps_operations),
Eric W. Biederman28a6d672006-10-02 02:17:05 -07003599#endif
Alexey Dobriyan631f9c12008-11-10 01:32:52 +03003600 REG("mem", S_IRUSR|S_IWUSR, proc_mem_operations),
3601 LNK("cwd", proc_cwd_link),
3602 LNK("root", proc_root_link),
3603 LNK("exe", proc_exe_link),
3604 REG("mounts", S_IRUGO, proc_mounts_operations),
3605 REG("mountinfo", S_IRUGO, proc_mountinfo_operations),
Matt Mackall1e883282008-02-04 22:29:07 -08003606#ifdef CONFIG_PROC_PAGE_MONITOR
Alexey Dobriyan631f9c12008-11-10 01:32:52 +03003607 REG("clear_refs", S_IWUSR, proc_clear_refs_operations),
Vlastimil Babka871305b2018-08-21 21:52:48 -07003608 REG("smaps", S_IRUGO, proc_pid_smaps_operations),
Daniel Colascione493b0e92017-09-06 16:25:08 -07003609 REG("smaps_rollup", S_IRUGO, proc_pid_smaps_rollup_operations),
Djalal Harouni32ed74a2014-04-07 15:38:38 -07003610 REG("pagemap", S_IRUSR, proc_pagemap_operations),
Eric W. Biederman28a6d672006-10-02 02:17:05 -07003611#endif
3612#ifdef CONFIG_SECURITY
Alexey Dobriyan631f9c12008-11-10 01:32:52 +03003613 DIR("attr", S_IRUGO|S_IXUGO, proc_attr_dir_inode_operations, proc_attr_dir_operations),
Eric W. Biederman28a6d672006-10-02 02:17:05 -07003614#endif
3615#ifdef CONFIG_KALLSYMS
Alexey Dobriyanedfcd602014-08-08 14:21:44 -07003616 ONE("wchan", S_IRUGO, proc_pid_wchan),
Eric W. Biederman28a6d672006-10-02 02:17:05 -07003617#endif
Ken Chen2ec220e2008-11-10 11:26:08 +03003618#ifdef CONFIG_STACKTRACE
Djalal Harouni35a35042014-04-07 15:38:36 -07003619 ONE("stack", S_IRUSR, proc_pid_stack),
Eric W. Biederman28a6d672006-10-02 02:17:05 -07003620#endif
Naveen N. Rao5968cec2015-06-30 14:36:03 +05303621#ifdef CONFIG_SCHED_INFO
Alexey Dobriyanf6e826c2014-08-08 14:21:46 -07003622 ONE("schedstat", S_IRUGO, proc_pid_schedstat),
Eric W. Biederman28a6d672006-10-02 02:17:05 -07003623#endif
Arjan van de Ven97455122008-01-25 21:08:34 +01003624#ifdef CONFIG_LATENCYTOP
Alexey Dobriyan631f9c12008-11-10 01:32:52 +03003625 REG("latency", S_IRUGO, proc_lstats_operations),
Arjan van de Ven97455122008-01-25 21:08:34 +01003626#endif
Paul Menage8793d852007-10-18 23:39:39 -07003627#ifdef CONFIG_PROC_PID_CPUSET
Zefan Li52de4772014-09-18 16:03:36 +08003628 ONE("cpuset", S_IRUGO, proc_cpuset_show),
Eric W. Biederman28a6d672006-10-02 02:17:05 -07003629#endif
Paul Menagea4243162007-10-18 23:39:35 -07003630#ifdef CONFIG_CGROUPS
Zefan Li006f4ac2014-09-18 16:03:15 +08003631 ONE("cgroup", S_IRUGO, proc_cgroup_show),
Paul Menagea4243162007-10-18 23:39:35 -07003632#endif
Chen Yue79f15a2020-01-15 17:28:51 +08003633#ifdef CONFIG_PROC_CPU_RESCTRL
3634 ONE("cpu_resctrl_groups", S_IRUGO, proc_resctrl_show),
3635#endif
Alexey Dobriyan6ba51e32014-08-08 14:21:48 -07003636 ONE("oom_score", S_IRUGO, proc_oom_score),
David Rientjesfa0cbbf2012-11-12 17:53:04 -08003637 REG("oom_adj", S_IRUGO|S_IWUSR, proc_oom_adj_operations),
David Rientjesa63d83f2010-08-09 17:19:46 -07003638 REG("oom_score_adj", S_IRUGO|S_IWUSR, proc_oom_score_adj_operations),
Richard Guy Briggs4b7d2482019-01-22 17:06:39 -05003639#ifdef CONFIG_AUDIT
Alexey Dobriyan631f9c12008-11-10 01:32:52 +03003640 REG("loginuid", S_IWUSR|S_IRUGO, proc_loginuid_operations),
Al Viro26ec3c62011-02-15 21:24:05 -05003641 REG("sessionid", S_IRUGO, proc_sessionid_operations),
Eric W. Biederman28a6d672006-10-02 02:17:05 -07003642#endif
Akinobu Mitaf4f154f2006-12-08 02:39:47 -08003643#ifdef CONFIG_FAULT_INJECTION
Alexey Dobriyan631f9c12008-11-10 01:32:52 +03003644 REG("make-it-fail", S_IRUGO|S_IWUSR, proc_fault_inject_operations),
Akinobu Mita1203c8e2017-07-14 14:49:57 -07003645 REG("fail-nth", 0644, proc_fail_nth_operations),
Akinobu Mitaf4f154f2006-12-08 02:39:47 -08003646#endif
Andrea Righi297c5d92008-07-25 01:48:49 -07003647#ifdef CONFIG_TASK_IO_ACCOUNTING
Alexey Dobriyan19aadc92014-08-08 14:21:50 -07003648 ONE("io", S_IRUSR, proc_tid_io_accounting),
Andrea Righi297c5d92008-07-25 01:48:49 -07003649#endif
Eric W. Biederman22d917d2011-11-17 00:11:58 -08003650#ifdef CONFIG_USER_NS
3651 REG("uid_map", S_IRUGO|S_IWUSR, proc_uid_map_operations),
3652 REG("gid_map", S_IRUGO|S_IWUSR, proc_gid_map_operations),
Eric W. Biedermanf76d2072012-08-30 01:24:05 -07003653 REG("projid_map", S_IRUGO|S_IWUSR, proc_projid_map_operations),
Eric W. Biederman9cc46512014-12-02 12:27:26 -06003654 REG("setgroups", S_IRUGO|S_IWUSR, proc_setgroups_operations),
Eric W. Biederman22d917d2011-11-17 00:11:58 -08003655#endif
Josh Poimboeuf7c23b332017-02-13 19:42:41 -06003656#ifdef CONFIG_LIVEPATCH
3657 ONE("patch_state", S_IRUSR, proc_pid_patch_state),
3658#endif
Aubrey Li68bc30b2019-06-06 09:22:34 +08003659#ifdef CONFIG_PROC_PID_ARCH_STATUS
3660 ONE("arch_status", S_IRUGO, proc_pid_arch_status),
3661#endif
YiFei Zhu0d8315d2020-11-11 07:33:54 -06003662#ifdef CONFIG_SECCOMP_CACHE_DEBUG
3663 ONE("seccomp_cache", S_IRUSR, proc_pid_seccomp_cache),
3664#endif
Connor O'Brien8bea10e2018-01-31 18:11:57 -08003665#ifdef CONFIG_CPU_FREQ_TIMES
3666 ONE("time_in_state", 0444, proc_time_in_state_show),
3667#endif
Eric W. Biederman28a6d672006-10-02 02:17:05 -07003668};
3669
Al Virof0c3b502013-05-16 12:07:31 -04003670static int proc_tid_base_readdir(struct file *file, struct dir_context *ctx)
Eric W. Biederman28a6d672006-10-02 02:17:05 -07003671{
Al Virof0c3b502013-05-16 12:07:31 -04003672 return proc_pident_readdir(file, ctx,
3673 tid_base_stuff, ARRAY_SIZE(tid_base_stuff));
Eric W. Biederman28a6d672006-10-02 02:17:05 -07003674}
3675
Al Viro00cd8dd2012-06-10 17:13:09 -04003676static struct dentry *proc_tid_base_lookup(struct inode *dir, struct dentry *dentry, unsigned int flags)
3677{
Eric W. Biederman7bcd6b02006-10-02 02:18:56 -07003678 return proc_pident_lookup(dir, dentry,
Alexey Dobriyand5a572a2019-03-11 23:28:51 -07003679 tid_base_stuff,
3680 tid_base_stuff + ARRAY_SIZE(tid_base_stuff));
Eric W. Biederman28a6d672006-10-02 02:17:05 -07003681}
3682
Arjan van de Ven00977a52007-02-12 00:55:34 -08003683static const struct file_operations proc_tid_base_operations = {
Eric W. Biederman28a6d672006-10-02 02:17:05 -07003684 .read = generic_read_dir,
Al Virof50752e2016-04-20 17:13:54 -04003685 .iterate_shared = proc_tid_base_readdir,
3686 .llseek = generic_file_llseek,
Eric W. Biederman28a6d672006-10-02 02:17:05 -07003687};
3688
Arjan van de Venc5ef1c42007-02-12 00:55:40 -08003689static const struct inode_operations proc_tid_base_inode_operations = {
Eric W. Biederman28a6d672006-10-02 02:17:05 -07003690 .lookup = proc_tid_base_lookup,
3691 .getattr = pid_getattr,
3692 .setattr = proc_setattr,
3693};
3694
Al Viro0168b9e2018-05-03 09:21:05 -04003695static struct dentry *proc_task_instantiate(struct dentry *dentry,
3696 struct task_struct *task, const void *ptr)
Eric W. Biederman444ceed2006-10-02 02:18:49 -07003697{
Eric W. Biederman444ceed2006-10-02 02:18:49 -07003698 struct inode *inode;
Zhihao Cheng3336e7c2022-07-13 21:00:29 +08003699 inode = proc_pid_make_base_inode(dentry->d_sb, task,
3700 S_IFDIR | S_IRUGO | S_IXUGO);
Eric W. Biederman444ceed2006-10-02 02:18:49 -07003701 if (!inode)
Al Viro0168b9e2018-05-03 09:21:05 -04003702 return ERR_PTR(-ENOENT);
Al Viro1bbc5512018-05-02 21:26:16 -04003703
Eric W. Biederman444ceed2006-10-02 02:18:49 -07003704 inode->i_op = &proc_tid_base_inode_operations;
3705 inode->i_fop = &proc_tid_base_operations;
Al Viro1bbc5512018-05-02 21:26:16 -04003706 inode->i_flags |= S_IMMUTABLE;
Vegard Nossumaed54172008-06-05 22:46:53 -07003707
Alexey Dobriyan1270dd82016-12-12 16:45:32 -08003708 set_nlink(inode, nlink_tid);
Al Viro1bbc5512018-05-02 21:26:16 -04003709 pid_update_inode(task, inode);
Eric W. Biederman444ceed2006-10-02 02:18:49 -07003710
Nick Pigginfb045ad2011-01-07 17:49:55 +11003711 d_set_d_op(dentry, &pid_dentry_operations);
Al Viro0168b9e2018-05-03 09:21:05 -04003712 return d_splice_alias(inode, dentry);
Eric W. Biederman444ceed2006-10-02 02:18:49 -07003713}
3714
Al Viro00cd8dd2012-06-10 17:13:09 -04003715static struct dentry *proc_task_lookup(struct inode *dir, struct dentry * dentry, unsigned int flags)
Eric W. Biederman28a6d672006-10-02 02:17:05 -07003716{
Eric W. Biederman28a6d672006-10-02 02:17:05 -07003717 struct task_struct *task;
3718 struct task_struct *leader = get_proc_task(dir);
Eric W. Biederman28a6d672006-10-02 02:17:05 -07003719 unsigned tid;
Alexey Gladkovfa10fed2020-04-19 16:10:52 +02003720 struct proc_fs_info *fs_info;
Pavel Emelyanovb4888932007-10-18 23:40:14 -07003721 struct pid_namespace *ns;
Al Viro0168b9e2018-05-03 09:21:05 -04003722 struct dentry *result = ERR_PTR(-ENOENT);
Eric W. Biederman28a6d672006-10-02 02:17:05 -07003723
3724 if (!leader)
3725 goto out_no_task;
3726
Alexey Dobriyandbcdb502014-08-08 14:21:25 -07003727 tid = name_to_int(&dentry->d_name);
Eric W. Biederman28a6d672006-10-02 02:17:05 -07003728 if (tid == ~0U)
3729 goto out;
3730
Alexey Gladkovfa10fed2020-04-19 16:10:52 +02003731 fs_info = proc_sb_info(dentry->d_sb);
3732 ns = fs_info->pid_ns;
Eric W. Biederman28a6d672006-10-02 02:17:05 -07003733 rcu_read_lock();
Pavel Emelyanovb4888932007-10-18 23:40:14 -07003734 task = find_task_by_pid_ns(tid, ns);
Eric W. Biederman28a6d672006-10-02 02:17:05 -07003735 if (task)
3736 get_task_struct(task);
3737 rcu_read_unlock();
3738 if (!task)
3739 goto out;
Pavel Emelyanovbac0abd2007-10-18 23:40:18 -07003740 if (!same_thread_group(leader, task))
Eric W. Biederman28a6d672006-10-02 02:17:05 -07003741 goto out_drop_task;
3742
Al Viro0168b9e2018-05-03 09:21:05 -04003743 result = proc_task_instantiate(dentry, task, NULL);
Eric W. Biederman28a6d672006-10-02 02:17:05 -07003744out_drop_task:
3745 put_task_struct(task);
3746out:
3747 put_task_struct(leader);
3748out_no_task:
Al Viro0168b9e2018-05-03 09:21:05 -04003749 return result;
Eric W. Biederman28a6d672006-10-02 02:17:05 -07003750}
3751
3752/*
Eric W. Biederman0bc58a92006-06-26 00:25:50 -07003753 * Find the first tid of a thread group to return to user space.
3754 *
3755 * Usually this is just the thread group leader, but if the users
3756 * buffer was too small or there was a seek into the middle of the
3757 * directory we have more work todo.
3758 *
3759 * In the case of a short read we start with find_task_by_pid.
3760 *
3761 * In the case of a seek we start with the leader and walk nr
3762 * threads past it.
3763 */
Oleg Nesterov9f6e9632014-01-23 15:55:40 -08003764static struct task_struct *first_tid(struct pid *pid, int tid, loff_t f_pos,
3765 struct pid_namespace *ns)
Eric W. Biederman0bc58a92006-06-26 00:25:50 -07003766{
Oleg Nesterovd855a4b2014-01-23 15:55:39 -08003767 struct task_struct *pos, *task;
Oleg Nesterov9f6e9632014-01-23 15:55:40 -08003768 unsigned long nr = f_pos;
3769
3770 if (nr != f_pos) /* 32bit overflow? */
3771 return NULL;
Eric W. Biederman0bc58a92006-06-26 00:25:50 -07003772
Eric W. Biedermancc288732006-06-26 00:26:01 -07003773 rcu_read_lock();
Oleg Nesterovd855a4b2014-01-23 15:55:39 -08003774 task = pid_task(pid, PIDTYPE_PID);
3775 if (!task)
3776 goto fail;
3777
3778 /* Attempt to start with the tid of a thread */
Oleg Nesterov9f6e9632014-01-23 15:55:40 -08003779 if (tid && nr) {
Pavel Emelyanovb4888932007-10-18 23:40:14 -07003780 pos = find_task_by_pid_ns(tid, ns);
Oleg Nesterovd855a4b2014-01-23 15:55:39 -08003781 if (pos && same_thread_group(pos, task))
Oleg Nesterova872ff02006-06-26 00:26:01 -07003782 goto found;
Eric W. Biederman0bc58a92006-06-26 00:25:50 -07003783 }
3784
3785 /* If nr exceeds the number of threads there is nothing todo */
Oleg Nesterov9f6e9632014-01-23 15:55:40 -08003786 if (nr >= get_nr_threads(task))
Oleg Nesterovc986c142014-01-23 15:55:38 -08003787 goto fail;
Oleg Nesterova872ff02006-06-26 00:26:01 -07003788
3789 /* If we haven't found our starting place yet start
3790 * with the leader and walk nr threads forward.
3791 */
Oleg Nesterovd855a4b2014-01-23 15:55:39 -08003792 pos = task = task->group_leader;
Oleg Nesterovc986c142014-01-23 15:55:38 -08003793 do {
Oleg Nesterov9f6e9632014-01-23 15:55:40 -08003794 if (!nr--)
Oleg Nesterovc986c142014-01-23 15:55:38 -08003795 goto found;
Oleg Nesterovd855a4b2014-01-23 15:55:39 -08003796 } while_each_thread(task, pos);
Oleg Nesterovc986c142014-01-23 15:55:38 -08003797fail:
3798 pos = NULL;
3799 goto out;
Oleg Nesterova872ff02006-06-26 00:26:01 -07003800found:
3801 get_task_struct(pos);
3802out:
Eric W. Biedermancc288732006-06-26 00:26:01 -07003803 rcu_read_unlock();
Eric W. Biederman0bc58a92006-06-26 00:25:50 -07003804 return pos;
3805}
3806
3807/*
3808 * Find the next thread in the thread list.
3809 * Return NULL if there is an error or no next thread.
3810 *
3811 * The reference to the input task_struct is released.
3812 */
3813static struct task_struct *next_tid(struct task_struct *start)
3814{
Oleg Nesterovc1df7fb2006-06-26 00:26:02 -07003815 struct task_struct *pos = NULL;
Eric W. Biedermancc288732006-06-26 00:26:01 -07003816 rcu_read_lock();
Oleg Nesterovc1df7fb2006-06-26 00:26:02 -07003817 if (pid_alive(start)) {
Eric W. Biederman0bc58a92006-06-26 00:25:50 -07003818 pos = next_thread(start);
Oleg Nesterovc1df7fb2006-06-26 00:26:02 -07003819 if (thread_group_leader(pos))
3820 pos = NULL;
3821 else
3822 get_task_struct(pos);
3823 }
Eric W. Biedermancc288732006-06-26 00:26:01 -07003824 rcu_read_unlock();
Eric W. Biederman0bc58a92006-06-26 00:25:50 -07003825 put_task_struct(start);
3826 return pos;
3827}
3828
Linus Torvalds1da177e2005-04-16 15:20:36 -07003829/* for the /proc/TGID/task/ directories */
Al Virof0c3b502013-05-16 12:07:31 -04003830static int proc_task_readdir(struct file *file, struct dir_context *ctx)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003831{
Oleg Nesterovd855a4b2014-01-23 15:55:39 -08003832 struct inode *inode = file_inode(file);
3833 struct task_struct *task;
Pavel Emelyanovb4888932007-10-18 23:40:14 -07003834 struct pid_namespace *ns;
Al Virof0c3b502013-05-16 12:07:31 -04003835 int tid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003836
Oleg Nesterovd855a4b2014-01-23 15:55:39 -08003837 if (proc_inode_is_dead(inode))
Al Virof0c3b502013-05-16 12:07:31 -04003838 return -ENOENT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003839
Al Virof0c3b502013-05-16 12:07:31 -04003840 if (!dir_emit_dots(file, ctx))
Oleg Nesterovd855a4b2014-01-23 15:55:39 -08003841 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003842
Eric W. Biederman0bc58a92006-06-26 00:25:50 -07003843 /* f_version caches the tgid value that the last readdir call couldn't
3844 * return. lseek aka telldir automagically resets f_version to 0.
3845 */
Alexey Gladkov9d78ede2020-05-18 20:07:38 +02003846 ns = proc_pid_ns(inode->i_sb);
Al Virof0c3b502013-05-16 12:07:31 -04003847 tid = (int)file->f_version;
3848 file->f_version = 0;
Oleg Nesterovd855a4b2014-01-23 15:55:39 -08003849 for (task = first_tid(proc_pid(inode), tid, ctx->pos - 2, ns);
Eric W. Biederman0bc58a92006-06-26 00:25:50 -07003850 task;
Al Virof0c3b502013-05-16 12:07:31 -04003851 task = next_tid(task), ctx->pos++) {
Alexey Dobriyane3912ac2018-02-06 15:36:51 -08003852 char name[10 + 1];
Alexey Dobriyana4ef3892018-06-07 17:10:10 -07003853 unsigned int len;
Pavel Emelyanovb4888932007-10-18 23:40:14 -07003854 tid = task_pid_nr_ns(task, ns);
Alexey Dobriyane3912ac2018-02-06 15:36:51 -08003855 len = snprintf(name, sizeof(name), "%u", tid);
Al Virof0c3b502013-05-16 12:07:31 -04003856 if (!proc_fill_cache(file, ctx, name, len,
3857 proc_task_instantiate, task, NULL)) {
Eric W. Biederman0bc58a92006-06-26 00:25:50 -07003858 /* returning this tgid failed, save it as the first
3859 * pid for the next readir call */
Al Virof0c3b502013-05-16 12:07:31 -04003860 file->f_version = (u64)tid;
Eric W. Biederman0bc58a92006-06-26 00:25:50 -07003861 put_task_struct(task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003862 break;
Eric W. Biederman0bc58a92006-06-26 00:25:50 -07003863 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003864 }
Oleg Nesterovd855a4b2014-01-23 15:55:39 -08003865
Al Virof0c3b502013-05-16 12:07:31 -04003866 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003867}
Eric W. Biederman6e66b522006-06-26 00:25:47 -07003868
Christian Brauner549c7292021-01-21 14:19:43 +01003869static int proc_task_getattr(struct user_namespace *mnt_userns,
3870 const struct path *path, struct kstat *stat,
David Howellsa528d352017-01-31 16:46:22 +00003871 u32 request_mask, unsigned int query_flags)
Eric W. Biederman6e66b522006-06-26 00:25:47 -07003872{
David Howellsa528d352017-01-31 16:46:22 +00003873 struct inode *inode = d_inode(path->dentry);
Eric W. Biederman99f89552006-06-26 00:25:55 -07003874 struct task_struct *p = get_proc_task(inode);
Christian Brauner0d56a452021-01-21 14:19:30 +01003875 generic_fillattr(&init_user_ns, inode, stat);
Eric W. Biederman6e66b522006-06-26 00:25:47 -07003876
Eric W. Biederman99f89552006-06-26 00:25:55 -07003877 if (p) {
Eric W. Biederman99f89552006-06-26 00:25:55 -07003878 stat->nlink += get_nr_threads(p);
Eric W. Biederman99f89552006-06-26 00:25:55 -07003879 put_task_struct(p);
Eric W. Biederman6e66b522006-06-26 00:25:47 -07003880 }
3881
3882 return 0;
3883}
Eric W. Biederman28a6d672006-10-02 02:17:05 -07003884
Arjan van de Venc5ef1c42007-02-12 00:55:40 -08003885static const struct inode_operations proc_task_inode_operations = {
Eric W. Biederman28a6d672006-10-02 02:17:05 -07003886 .lookup = proc_task_lookup,
3887 .getattr = proc_task_getattr,
3888 .setattr = proc_setattr,
Vasiliy Kulikov04996802012-01-10 15:11:31 -08003889 .permission = proc_pid_permission,
Eric W. Biederman28a6d672006-10-02 02:17:05 -07003890};
3891
Arjan van de Ven00977a52007-02-12 00:55:34 -08003892static const struct file_operations proc_task_operations = {
Eric W. Biederman28a6d672006-10-02 02:17:05 -07003893 .read = generic_read_dir,
Al Virof50752e2016-04-20 17:13:54 -04003894 .iterate_shared = proc_task_readdir,
3895 .llseek = generic_file_llseek,
Eric W. Biederman28a6d672006-10-02 02:17:05 -07003896};
Alexey Dobriyan1270dd82016-12-12 16:45:32 -08003897
3898void __init set_proc_pid_nlink(void)
3899{
3900 nlink_tid = pid_entry_nlink(tid_base_stuff, ARRAY_SIZE(tid_base_stuff));
3901 nlink_tgid = pid_entry_nlink(tgid_base_stuff, ARRAY_SIZE(tgid_base_stuff));
3902}