Greg Kroah-Hartman | b244131 | 2017-11-01 15:07:57 +0100 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0 |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2 | /* |
| 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 Lin | e070ad4 | 2005-09-03 15:55:10 -0700 | [diff] [blame] | 15 | * |
| 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 Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 49 | */ |
| 50 | |
Linus Torvalds | 7c0f6ba | 2016-12-24 11:46:01 -0800 | [diff] [blame] | 51 | #include <linux/uaccess.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 52 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 53 | #include <linux/errno.h> |
| 54 | #include <linux/time.h> |
| 55 | #include <linux/proc_fs.h> |
| 56 | #include <linux/stat.h> |
Andrea Righi | 5995477 | 2008-07-27 17:29:15 +0200 | [diff] [blame] | 57 | #include <linux/task_io_accounting_ops.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 58 | #include <linux/init.h> |
Randy Dunlap | 16f7e0f | 2006-01-11 12:17:46 -0800 | [diff] [blame] | 59 | #include <linux/capability.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 60 | #include <linux/file.h> |
Al Viro | 9f3acc3 | 2008-04-24 07:44:08 -0400 | [diff] [blame] | 61 | #include <linux/fdtable.h> |
Kent Overstreet | 94f8f3b | 2019-03-11 23:31:18 -0700 | [diff] [blame] | 62 | #include <linux/generic-radix-tree.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 63 | #include <linux/string.h> |
| 64 | #include <linux/seq_file.h> |
| 65 | #include <linux/namei.h> |
Kirill Korotaev | 6b3286e | 2006-12-08 02:37:56 -0800 | [diff] [blame] | 66 | #include <linux/mnt_namespace.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 67 | #include <linux/mm.h> |
David Rientjes | a63d83f | 2010-08-09 17:19:46 -0700 | [diff] [blame] | 68 | #include <linux/swap.h> |
Dipankar Sarma | b835996 | 2005-09-09 13:04:14 -0700 | [diff] [blame] | 69 | #include <linux/rcupdate.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 70 | #include <linux/kallsyms.h> |
Ken Chen | 2ec220e | 2008-11-10 11:26:08 +0300 | [diff] [blame] | 71 | #include <linux/stacktrace.h> |
Neil Horman | d85f50d | 2007-10-18 23:40:37 -0700 | [diff] [blame] | 72 | #include <linux/resource.h> |
Kees Cook | 5096add | 2007-05-08 00:26:04 -0700 | [diff] [blame] | 73 | #include <linux/module.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 74 | #include <linux/mount.h> |
| 75 | #include <linux/security.h> |
| 76 | #include <linux/ptrace.h> |
Roland McGrath | 0d094ef | 2008-07-25 19:45:49 -0700 | [diff] [blame] | 77 | #include <linux/tracehook.h> |
Andrew Morton | 87ebdc0 | 2013-02-27 17:03:16 -0800 | [diff] [blame] | 78 | #include <linux/printk.h> |
Alexey Dobriyan | efb1a57 | 2018-02-06 15:37:24 -0800 | [diff] [blame] | 79 | #include <linux/cache.h> |
Paul Menage | a424316 | 2007-10-18 23:39:35 -0700 | [diff] [blame] | 80 | #include <linux/cgroup.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 81 | #include <linux/cpuset.h> |
| 82 | #include <linux/audit.h> |
Al Viro | 5addc5d | 2005-11-07 17:15:49 -0500 | [diff] [blame] | 83 | #include <linux/poll.h> |
Serge E. Hallyn | 1651e14 | 2006-10-02 02:18:08 -0700 | [diff] [blame] | 84 | #include <linux/nsproxy.h> |
Alexey Dobriyan | 8ac773b | 2006-10-19 23:28:32 -0700 | [diff] [blame] | 85 | #include <linux/oom.h> |
Kawai, Hidehiro | 3cb4a0b | 2007-07-19 01:48:28 -0700 | [diff] [blame] | 86 | #include <linux/elf.h> |
Pavel Emelyanov | 60347f6 | 2007-10-18 23:40:03 -0700 | [diff] [blame] | 87 | #include <linux/pid_namespace.h> |
Eric W. Biederman | 22d917d | 2011-11-17 00:11:58 -0800 | [diff] [blame] | 88 | #include <linux/user_namespace.h> |
Al Viro | 5ad4e53 | 2009-03-29 19:50:06 -0400 | [diff] [blame] | 89 | #include <linux/fs_struct.h> |
Tejun Heo | 5a0e3ad | 2010-03-24 17:04:11 +0900 | [diff] [blame] | 90 | #include <linux/slab.h> |
Ingo Molnar | 4eb5aaa | 2017-02-08 18:51:29 +0100 | [diff] [blame] | 91 | #include <linux/sched/autogroup.h> |
Ingo Molnar | 6e84f31 | 2017-02-08 18:51:29 +0100 | [diff] [blame] | 92 | #include <linux/sched/mm.h> |
Ingo Molnar | f7ccbae | 2017-02-08 18:51:30 +0100 | [diff] [blame] | 93 | #include <linux/sched/coredump.h> |
Ingo Molnar | b17b015 | 2017-02-08 18:51:35 +0100 | [diff] [blame] | 94 | #include <linux/sched/debug.h> |
Ingo Molnar | 3905f9a | 2017-02-05 12:07:04 +0100 | [diff] [blame] | 95 | #include <linux/sched/stat.h> |
Pavel Emelyanov | 48f6a7a | 2013-03-11 13:12:45 +0400 | [diff] [blame] | 96 | #include <linux/posix-timers.h> |
KAMEZAWA Hiroyuki | 43d2b11 | 2012-01-10 15:08:09 -0800 | [diff] [blame] | 97 | #include <trace/events/oom.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 98 | #include "internal.h" |
Cyrill Gorcunov | faf60af | 2012-08-23 14:43:24 +0400 | [diff] [blame] | 99 | #include "fd.h" |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 100 | |
Alexey Dobriyan | ac7f106 | 2018-02-06 15:36:59 -0800 | [diff] [blame] | 101 | #include "../../lib/kstrtox.h" |
| 102 | |
Eric W. Biederman | 0f2fe20 | 2006-06-26 00:25:46 -0700 | [diff] [blame] | 103 | /* NOTE: |
| 104 | * Implementing inode permission operations in /proc is almost |
| 105 | * certainly an error. Permission checks need to happen during |
| 106 | * each system call not at open time. The reason is that most of |
| 107 | * what we wish to check for permissions in /proc varies at runtime. |
| 108 | * |
| 109 | * The classic example of a problem is opening file descriptors |
| 110 | * in /proc for a task before it execs a suid executable. |
| 111 | */ |
| 112 | |
Alexey Dobriyan | efb1a57 | 2018-02-06 15:37:24 -0800 | [diff] [blame] | 113 | static u8 nlink_tid __ro_after_init; |
| 114 | static u8 nlink_tgid __ro_after_init; |
Alexey Dobriyan | 1270dd8 | 2016-12-12 16:45:32 -0800 | [diff] [blame] | 115 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 116 | struct pid_entry { |
Alexey Dobriyan | cedbcca | 2014-08-08 14:21:33 -0700 | [diff] [blame] | 117 | const char *name; |
Alexey Dobriyan | 623f594 | 2016-12-12 16:45:08 -0800 | [diff] [blame] | 118 | unsigned int len; |
Al Viro | d161a13 | 2011-07-24 03:36:29 -0400 | [diff] [blame] | 119 | umode_t mode; |
Arjan van de Ven | c5ef1c4 | 2007-02-12 00:55:40 -0800 | [diff] [blame] | 120 | const struct inode_operations *iop; |
Arjan van de Ven | 00977a5 | 2007-02-12 00:55:34 -0800 | [diff] [blame] | 121 | const struct file_operations *fop; |
Eric W. Biederman | 20cdc89 | 2006-10-02 02:17:07 -0700 | [diff] [blame] | 122 | union proc_op op; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 123 | }; |
| 124 | |
Eric W. Biederman | 61a2878 | 2006-10-02 02:18:49 -0700 | [diff] [blame] | 125 | #define NOD(NAME, MODE, IOP, FOP, OP) { \ |
Eric W. Biederman | 20cdc89 | 2006-10-02 02:17:07 -0700 | [diff] [blame] | 126 | .name = (NAME), \ |
Eric Dumazet | c5141e6 | 2007-05-08 00:26:15 -0700 | [diff] [blame] | 127 | .len = sizeof(NAME) - 1, \ |
Eric W. Biederman | 20cdc89 | 2006-10-02 02:17:07 -0700 | [diff] [blame] | 128 | .mode = MODE, \ |
| 129 | .iop = IOP, \ |
| 130 | .fop = FOP, \ |
| 131 | .op = OP, \ |
| 132 | } |
| 133 | |
Alexey Dobriyan | 631f9c1 | 2008-11-10 01:32:52 +0300 | [diff] [blame] | 134 | #define DIR(NAME, MODE, iops, fops) \ |
| 135 | NOD(NAME, (S_IFDIR|(MODE)), &iops, &fops, {} ) |
| 136 | #define LNK(NAME, get_link) \ |
Eric W. Biederman | 61a2878 | 2006-10-02 02:18:49 -0700 | [diff] [blame] | 137 | NOD(NAME, (S_IFLNK|S_IRWXUGO), \ |
Eric W. Biederman | 20cdc89 | 2006-10-02 02:17:07 -0700 | [diff] [blame] | 138 | &proc_pid_link_inode_operations, NULL, \ |
Alexey Dobriyan | 631f9c1 | 2008-11-10 01:32:52 +0300 | [diff] [blame] | 139 | { .proc_get_link = get_link } ) |
| 140 | #define REG(NAME, MODE, fops) \ |
| 141 | NOD(NAME, (S_IFREG|(MODE)), NULL, &fops, {}) |
Alexey Dobriyan | 631f9c1 | 2008-11-10 01:32:52 +0300 | [diff] [blame] | 142 | #define ONE(NAME, MODE, show) \ |
Casey Schaufler | 6d9c939 | 2018-09-21 17:16:59 -0700 | [diff] [blame] | 143 | NOD(NAME, (S_IFREG|(MODE)), \ |
Eric W. Biederman | be61408 | 2008-02-08 04:18:30 -0800 | [diff] [blame] | 144 | NULL, &proc_single_file_operations, \ |
Alexey Dobriyan | 631f9c1 | 2008-11-10 01:32:52 +0300 | [diff] [blame] | 145 | { .proc_show = show } ) |
Casey Schaufler | 6d9c939 | 2018-09-21 17:16:59 -0700 | [diff] [blame] | 146 | #define ATTR(LSM, NAME, MODE) \ |
| 147 | NOD(NAME, (S_IFREG|(MODE)), \ |
| 148 | NULL, &proc_pid_attr_operations, \ |
| 149 | { .lsm = LSM }) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 150 | |
Vegard Nossum | aed5417 | 2008-06-05 22:46:53 -0700 | [diff] [blame] | 151 | /* |
| 152 | * Count the number of hardlinks for the pid_entry table, excluding the . |
| 153 | * and .. links. |
| 154 | */ |
Alexey Dobriyan | 1270dd8 | 2016-12-12 16:45:32 -0800 | [diff] [blame] | 155 | static unsigned int __init pid_entry_nlink(const struct pid_entry *entries, |
Vegard Nossum | aed5417 | 2008-06-05 22:46:53 -0700 | [diff] [blame] | 156 | unsigned int n) |
| 157 | { |
| 158 | unsigned int i; |
| 159 | unsigned int count; |
| 160 | |
Alexey Dobriyan | 1270dd8 | 2016-12-12 16:45:32 -0800 | [diff] [blame] | 161 | count = 2; |
Vegard Nossum | aed5417 | 2008-06-05 22:46:53 -0700 | [diff] [blame] | 162 | for (i = 0; i < n; ++i) { |
| 163 | if (S_ISDIR(entries[i].mode)) |
| 164 | ++count; |
| 165 | } |
| 166 | |
| 167 | return count; |
| 168 | } |
| 169 | |
Miklos Szeredi | f7ad3c6 | 2010-08-10 11:41:36 +0200 | [diff] [blame] | 170 | static int get_task_root(struct task_struct *task, struct path *root) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 171 | { |
Hugh Dickins | 7c2c7d9 | 2009-03-28 23:21:27 +0000 | [diff] [blame] | 172 | int result = -ENOENT; |
| 173 | |
Miklos Szeredi | 0494f6e | 2005-09-06 15:18:22 -0700 | [diff] [blame] | 174 | task_lock(task); |
Miklos Szeredi | f7ad3c6 | 2010-08-10 11:41:36 +0200 | [diff] [blame] | 175 | if (task->fs) { |
| 176 | get_fs_root(task->fs, root); |
Hugh Dickins | 7c2c7d9 | 2009-03-28 23:21:27 +0000 | [diff] [blame] | 177 | result = 0; |
| 178 | } |
Miklos Szeredi | 0494f6e | 2005-09-06 15:18:22 -0700 | [diff] [blame] | 179 | task_unlock(task); |
Hugh Dickins | 7c2c7d9 | 2009-03-28 23:21:27 +0000 | [diff] [blame] | 180 | return result; |
Miklos Szeredi | 0494f6e | 2005-09-06 15:18:22 -0700 | [diff] [blame] | 181 | } |
| 182 | |
Cyrill Gorcunov | 7773fbc | 2012-01-10 15:11:20 -0800 | [diff] [blame] | 183 | static int proc_cwd_link(struct dentry *dentry, struct path *path) |
Miklos Szeredi | 0494f6e | 2005-09-06 15:18:22 -0700 | [diff] [blame] | 184 | { |
David Howells | 2b0143b | 2015-03-17 22:25:59 +0000 | [diff] [blame] | 185 | struct task_struct *task = get_proc_task(d_inode(dentry)); |
Miklos Szeredi | 0494f6e | 2005-09-06 15:18:22 -0700 | [diff] [blame] | 186 | int result = -ENOENT; |
Eric W. Biederman | 99f8955 | 2006-06-26 00:25:55 -0700 | [diff] [blame] | 187 | |
| 188 | if (task) { |
Miklos Szeredi | f7ad3c6 | 2010-08-10 11:41:36 +0200 | [diff] [blame] | 189 | task_lock(task); |
| 190 | if (task->fs) { |
| 191 | get_fs_pwd(task->fs, path); |
| 192 | result = 0; |
| 193 | } |
| 194 | task_unlock(task); |
Eric W. Biederman | 99f8955 | 2006-06-26 00:25:55 -0700 | [diff] [blame] | 195 | put_task_struct(task); |
| 196 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 197 | return result; |
| 198 | } |
| 199 | |
Cyrill Gorcunov | 7773fbc | 2012-01-10 15:11:20 -0800 | [diff] [blame] | 200 | static int proc_root_link(struct dentry *dentry, struct path *path) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 201 | { |
David Howells | 2b0143b | 2015-03-17 22:25:59 +0000 | [diff] [blame] | 202 | struct task_struct *task = get_proc_task(d_inode(dentry)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 203 | int result = -ENOENT; |
Eric W. Biederman | 99f8955 | 2006-06-26 00:25:55 -0700 | [diff] [blame] | 204 | |
| 205 | if (task) { |
Miklos Szeredi | f7ad3c6 | 2010-08-10 11:41:36 +0200 | [diff] [blame] | 206 | result = get_task_root(task, path); |
Eric W. Biederman | 99f8955 | 2006-06-26 00:25:55 -0700 | [diff] [blame] | 207 | put_task_struct(task); |
| 208 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 209 | return result; |
| 210 | } |
| 211 | |
Linus Torvalds | e4b4e44 | 2018-05-17 13:04:17 -0700 | [diff] [blame] | 212 | static ssize_t get_mm_cmdline(struct mm_struct *mm, char __user *buf, |
Linus Torvalds | 5ab8271 | 2018-05-17 15:17:33 -0700 | [diff] [blame] | 213 | size_t count, loff_t *ppos) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 214 | { |
Alexey Dobriyan | c2c0bb4 | 2015-06-25 15:00:54 -0700 | [diff] [blame] | 215 | unsigned long arg_start, arg_end, env_start, env_end; |
Linus Torvalds | 5ab8271 | 2018-05-17 15:17:33 -0700 | [diff] [blame] | 216 | unsigned long pos, len; |
| 217 | char *page; |
Alexey Dobriyan | c2c0bb4 | 2015-06-25 15:00:54 -0700 | [diff] [blame] | 218 | |
Alexey Dobriyan | c2c0bb4 | 2015-06-25 15:00:54 -0700 | [diff] [blame] | 219 | /* Check if process spawned far enough to have cmdline. */ |
Linus Torvalds | e4b4e44 | 2018-05-17 13:04:17 -0700 | [diff] [blame] | 220 | if (!mm->env_end) |
| 221 | return 0; |
Alexey Dobriyan | c2c0bb4 | 2015-06-25 15:00:54 -0700 | [diff] [blame] | 222 | |
Yang Shi | 88aa7cc | 2018-06-07 17:05:28 -0700 | [diff] [blame] | 223 | spin_lock(&mm->arg_lock); |
Alexey Dobriyan | c2c0bb4 | 2015-06-25 15:00:54 -0700 | [diff] [blame] | 224 | arg_start = mm->arg_start; |
| 225 | arg_end = mm->arg_end; |
| 226 | env_start = mm->env_start; |
| 227 | env_end = mm->env_end; |
Yang Shi | 88aa7cc | 2018-06-07 17:05:28 -0700 | [diff] [blame] | 228 | spin_unlock(&mm->arg_lock); |
Alexey Dobriyan | c2c0bb4 | 2015-06-25 15:00:54 -0700 | [diff] [blame] | 229 | |
Linus Torvalds | 5ab8271 | 2018-05-17 15:17:33 -0700 | [diff] [blame] | 230 | if (arg_start >= arg_end) |
| 231 | return 0; |
Alexey Dobriyan | 6a6cbe7 | 2018-06-07 17:09:59 -0700 | [diff] [blame] | 232 | |
Alexey Dobriyan | 2ca66ff | 2014-08-08 14:21:41 -0700 | [diff] [blame] | 233 | /* |
Linus Torvalds | 5ab8271 | 2018-05-17 15:17:33 -0700 | [diff] [blame] | 234 | * We have traditionally allowed the user to re-write |
| 235 | * the argument strings and overflow the end result |
| 236 | * into the environment section. But only do that if |
| 237 | * the environment area is contiguous to the arguments. |
Alexey Dobriyan | 2ca66ff | 2014-08-08 14:21:41 -0700 | [diff] [blame] | 238 | */ |
Linus Torvalds | 5ab8271 | 2018-05-17 15:17:33 -0700 | [diff] [blame] | 239 | if (env_start != arg_end || env_start >= env_end) |
| 240 | env_start = env_end = arg_end; |
Alexey Dobriyan | c2c0bb4 | 2015-06-25 15:00:54 -0700 | [diff] [blame] | 241 | |
Linus Torvalds | f5b6534 | 2018-06-20 09:47:20 +0900 | [diff] [blame] | 242 | /* .. and limit it to a maximum of one page of slop */ |
| 243 | if (env_end >= arg_end + PAGE_SIZE) |
| 244 | env_end = arg_end + PAGE_SIZE - 1; |
| 245 | |
Linus Torvalds | 5ab8271 | 2018-05-17 15:17:33 -0700 | [diff] [blame] | 246 | /* We're not going to care if "*ppos" has high bits set */ |
| 247 | pos = arg_start + *ppos; |
Alexey Dobriyan | c2c0bb4 | 2015-06-25 15:00:54 -0700 | [diff] [blame] | 248 | |
Linus Torvalds | 5ab8271 | 2018-05-17 15:17:33 -0700 | [diff] [blame] | 249 | /* .. but we do check the result is in the proper range */ |
| 250 | if (pos < arg_start || pos >= env_end) |
| 251 | return 0; |
Alexey Dobriyan | c2c0bb4 | 2015-06-25 15:00:54 -0700 | [diff] [blame] | 252 | |
Linus Torvalds | 5ab8271 | 2018-05-17 15:17:33 -0700 | [diff] [blame] | 253 | /* .. and we never go past env_end */ |
| 254 | if (env_end - pos < count) |
| 255 | count = env_end - pos; |
Alexey Dobriyan | c2c0bb4 | 2015-06-25 15:00:54 -0700 | [diff] [blame] | 256 | |
Linus Torvalds | 5ab8271 | 2018-05-17 15:17:33 -0700 | [diff] [blame] | 257 | page = (char *)__get_free_page(GFP_KERNEL); |
| 258 | if (!page) |
| 259 | return -ENOMEM; |
Alexey Dobriyan | c2c0bb4 | 2015-06-25 15:00:54 -0700 | [diff] [blame] | 260 | |
Linus Torvalds | 5ab8271 | 2018-05-17 15:17:33 -0700 | [diff] [blame] | 261 | len = 0; |
| 262 | while (count) { |
| 263 | int got; |
| 264 | size_t size = min_t(size_t, PAGE_SIZE, count); |
Linus Torvalds | f5b6534 | 2018-06-20 09:47:20 +0900 | [diff] [blame] | 265 | long offset; |
Alexey Dobriyan | c2c0bb4 | 2015-06-25 15:00:54 -0700 | [diff] [blame] | 266 | |
Linus Torvalds | f5b6534 | 2018-06-20 09:47:20 +0900 | [diff] [blame] | 267 | /* |
| 268 | * Are we already starting past the official end? |
| 269 | * We always include the last byte that is *supposed* |
| 270 | * to be NUL |
| 271 | */ |
| 272 | offset = (pos >= arg_end) ? pos - arg_end + 1 : 0; |
| 273 | |
| 274 | got = access_remote_vm(mm, pos - offset, page, size + offset, FOLL_ANON); |
| 275 | if (got <= offset) |
Linus Torvalds | 5ab8271 | 2018-05-17 15:17:33 -0700 | [diff] [blame] | 276 | break; |
Linus Torvalds | f5b6534 | 2018-06-20 09:47:20 +0900 | [diff] [blame] | 277 | got -= offset; |
Linus Torvalds | 5ab8271 | 2018-05-17 15:17:33 -0700 | [diff] [blame] | 278 | |
| 279 | /* Don't walk past a NUL character once you hit arg_end */ |
| 280 | if (pos + got >= arg_end) { |
| 281 | int n = 0; |
| 282 | |
| 283 | /* |
| 284 | * If we started before 'arg_end' but ended up |
| 285 | * at or after it, we start the NUL character |
| 286 | * check at arg_end-1 (where we expect the normal |
| 287 | * EOF to be). |
| 288 | * |
| 289 | * NOTE! This is smaller than 'got', because |
| 290 | * pos + got >= arg_end |
| 291 | */ |
| 292 | if (pos < arg_end) |
| 293 | n = arg_end - pos - 1; |
| 294 | |
| 295 | /* Cut off at first NUL after 'n' */ |
Linus Torvalds | f5b6534 | 2018-06-20 09:47:20 +0900 | [diff] [blame] | 296 | got = n + strnlen(page+n, offset+got-n); |
| 297 | if (got < offset) |
Linus Torvalds | 5ab8271 | 2018-05-17 15:17:33 -0700 | [diff] [blame] | 298 | break; |
Linus Torvalds | f5b6534 | 2018-06-20 09:47:20 +0900 | [diff] [blame] | 299 | got -= offset; |
| 300 | |
| 301 | /* Include the NUL if it existed */ |
| 302 | if (got < size) |
| 303 | got++; |
Alexey Dobriyan | c2c0bb4 | 2015-06-25 15:00:54 -0700 | [diff] [blame] | 304 | } |
Alexey Dobriyan | a0a07b8 | 2017-02-24 15:00:20 -0800 | [diff] [blame] | 305 | |
Linus Torvalds | f5b6534 | 2018-06-20 09:47:20 +0900 | [diff] [blame] | 306 | got -= copy_to_user(buf, page+offset, got); |
Linus Torvalds | 5ab8271 | 2018-05-17 15:17:33 -0700 | [diff] [blame] | 307 | if (unlikely(!got)) { |
| 308 | if (!len) |
| 309 | len = -EFAULT; |
| 310 | break; |
Alexey Dobriyan | c2c0bb4 | 2015-06-25 15:00:54 -0700 | [diff] [blame] | 311 | } |
Linus Torvalds | 5ab8271 | 2018-05-17 15:17:33 -0700 | [diff] [blame] | 312 | pos += got; |
| 313 | buf += got; |
| 314 | len += got; |
| 315 | count -= got; |
Alexey Dobriyan | 3cb4e16 | 2018-06-07 17:10:02 -0700 | [diff] [blame] | 316 | } |
| 317 | |
Alexey Dobriyan | c2c0bb4 | 2015-06-25 15:00:54 -0700 | [diff] [blame] | 318 | free_page((unsigned long)page); |
Linus Torvalds | 5ab8271 | 2018-05-17 15:17:33 -0700 | [diff] [blame] | 319 | return len; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 320 | } |
| 321 | |
Linus Torvalds | e4b4e44 | 2018-05-17 13:04:17 -0700 | [diff] [blame] | 322 | static ssize_t get_task_cmdline(struct task_struct *tsk, char __user *buf, |
| 323 | size_t count, loff_t *pos) |
| 324 | { |
| 325 | struct mm_struct *mm; |
| 326 | ssize_t ret; |
| 327 | |
| 328 | mm = get_task_mm(tsk); |
| 329 | if (!mm) |
| 330 | return 0; |
| 331 | |
| 332 | ret = get_mm_cmdline(mm, buf, count, pos); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 333 | mmput(mm); |
Linus Torvalds | e4b4e44 | 2018-05-17 13:04:17 -0700 | [diff] [blame] | 334 | return ret; |
| 335 | } |
| 336 | |
| 337 | static ssize_t proc_pid_cmdline_read(struct file *file, char __user *buf, |
| 338 | size_t count, loff_t *pos) |
| 339 | { |
| 340 | struct task_struct *tsk; |
| 341 | ssize_t ret; |
| 342 | |
| 343 | BUG_ON(*pos < 0); |
| 344 | |
| 345 | tsk = get_proc_task(file_inode(file)); |
| 346 | if (!tsk) |
| 347 | return -ESRCH; |
| 348 | ret = get_task_cmdline(tsk, buf, count, pos); |
| 349 | put_task_struct(tsk); |
| 350 | if (ret > 0) |
| 351 | *pos += ret; |
| 352 | return ret; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 353 | } |
| 354 | |
Alexey Dobriyan | c2c0bb4 | 2015-06-25 15:00:54 -0700 | [diff] [blame] | 355 | static const struct file_operations proc_pid_cmdline_ops = { |
| 356 | .read = proc_pid_cmdline_read, |
| 357 | .llseek = generic_file_llseek, |
| 358 | }; |
| 359 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 360 | #ifdef CONFIG_KALLSYMS |
| 361 | /* |
| 362 | * Provides a wchan file via kallsyms in a proper one-value-per-file format. |
| 363 | * Returns the resolved symbol. If that fails, simply return the address. |
| 364 | */ |
Alexey Dobriyan | edfcd60 | 2014-08-08 14:21:44 -0700 | [diff] [blame] | 365 | static int proc_pid_wchan(struct seq_file *m, struct pid_namespace *ns, |
| 366 | struct pid *pid, struct task_struct *task) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 367 | { |
Alexey Dobriyan | ffb4512 | 2007-05-08 00:28:41 -0700 | [diff] [blame] | 368 | unsigned long wchan; |
Tejun Heo | 9281ace | 2007-07-17 04:03:51 -0700 | [diff] [blame] | 369 | char symname[KSYM_NAME_LEN]; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 370 | |
Alexey Dobriyan | 24b2ec2 | 2018-04-10 16:31:30 -0700 | [diff] [blame] | 371 | if (!ptrace_may_access(task, PTRACE_MODE_READ_FSCREDS)) |
| 372 | goto print0; |
| 373 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 374 | wchan = get_wchan(task); |
Alexey Dobriyan | 24b2ec2 | 2018-04-10 16:31:30 -0700 | [diff] [blame] | 375 | if (wchan && !lookup_symbol_name(wchan, symname)) { |
Alexey Dobriyan | 21dae0a | 2018-04-10 16:31:34 -0700 | [diff] [blame] | 376 | seq_puts(m, symname); |
Alexey Dobriyan | 24b2ec2 | 2018-04-10 16:31:30 -0700 | [diff] [blame] | 377 | return 0; |
| 378 | } |
Joe Perches | 25ce319 | 2015-04-15 16:18:17 -0700 | [diff] [blame] | 379 | |
Alexey Dobriyan | 24b2ec2 | 2018-04-10 16:31:30 -0700 | [diff] [blame] | 380 | print0: |
| 381 | seq_putc(m, '0'); |
Joe Perches | 25ce319 | 2015-04-15 16:18:17 -0700 | [diff] [blame] | 382 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 383 | } |
| 384 | #endif /* CONFIG_KALLSYMS */ |
| 385 | |
Al Viro | a9712bc | 2011-03-23 15:52:50 -0400 | [diff] [blame] | 386 | static int lock_trace(struct task_struct *task) |
| 387 | { |
| 388 | int err = mutex_lock_killable(&task->signal->cred_guard_mutex); |
| 389 | if (err) |
| 390 | return err; |
Jann Horn | caaee62 | 2016-01-20 15:00:04 -0800 | [diff] [blame] | 391 | if (!ptrace_may_access(task, PTRACE_MODE_ATTACH_FSCREDS)) { |
Al Viro | a9712bc | 2011-03-23 15:52:50 -0400 | [diff] [blame] | 392 | mutex_unlock(&task->signal->cred_guard_mutex); |
| 393 | return -EPERM; |
| 394 | } |
| 395 | return 0; |
| 396 | } |
| 397 | |
| 398 | static void unlock_trace(struct task_struct *task) |
| 399 | { |
| 400 | mutex_unlock(&task->signal->cred_guard_mutex); |
| 401 | } |
| 402 | |
Ken Chen | 2ec220e | 2008-11-10 11:26:08 +0300 | [diff] [blame] | 403 | #ifdef CONFIG_STACKTRACE |
| 404 | |
| 405 | #define MAX_STACK_TRACE_DEPTH 64 |
| 406 | |
| 407 | static int proc_pid_stack(struct seq_file *m, struct pid_namespace *ns, |
| 408 | struct pid *pid, struct task_struct *task) |
| 409 | { |
Ken Chen | 2ec220e | 2008-11-10 11:26:08 +0300 | [diff] [blame] | 410 | unsigned long *entries; |
Al Viro | a9712bc | 2011-03-23 15:52:50 -0400 | [diff] [blame] | 411 | int err; |
Ken Chen | 2ec220e | 2008-11-10 11:26:08 +0300 | [diff] [blame] | 412 | |
Jann Horn | f8a00ce | 2018-10-05 15:51:58 -0700 | [diff] [blame] | 413 | /* |
| 414 | * The ability to racily run the kernel stack unwinder on a running task |
| 415 | * and then observe the unwinder output is scary; while it is useful for |
| 416 | * debugging kernel issues, it can also allow an attacker to leak kernel |
| 417 | * stack contents. |
| 418 | * Doing this in a manner that is at least safe from races would require |
| 419 | * some work to ensure that the remote task can not be scheduled; and |
| 420 | * even then, this would still expose the unwinder as local attack |
| 421 | * surface. |
| 422 | * Therefore, this interface is restricted to root. |
| 423 | */ |
| 424 | if (!file_ns_capable(m->file, &init_user_ns, CAP_SYS_ADMIN)) |
| 425 | return -EACCES; |
| 426 | |
Kees Cook | 6da2ec5 | 2018-06-12 13:55:00 -0700 | [diff] [blame] | 427 | entries = kmalloc_array(MAX_STACK_TRACE_DEPTH, sizeof(*entries), |
| 428 | GFP_KERNEL); |
Ken Chen | 2ec220e | 2008-11-10 11:26:08 +0300 | [diff] [blame] | 429 | if (!entries) |
| 430 | return -ENOMEM; |
| 431 | |
Al Viro | a9712bc | 2011-03-23 15:52:50 -0400 | [diff] [blame] | 432 | err = lock_trace(task); |
| 433 | if (!err) { |
Thomas Gleixner | e988e5e | 2019-04-25 11:44:58 +0200 | [diff] [blame] | 434 | unsigned int i, nr_entries; |
Alexey Dobriyan | 5d008fb | 2018-06-07 17:10:17 -0700 | [diff] [blame] | 435 | |
Thomas Gleixner | e988e5e | 2019-04-25 11:44:58 +0200 | [diff] [blame] | 436 | nr_entries = stack_trace_save_tsk(task, entries, |
| 437 | MAX_STACK_TRACE_DEPTH, 0); |
Al Viro | a9712bc | 2011-03-23 15:52:50 -0400 | [diff] [blame] | 438 | |
Thomas Gleixner | e988e5e | 2019-04-25 11:44:58 +0200 | [diff] [blame] | 439 | for (i = 0; i < nr_entries; i++) { |
Linus Torvalds | 8f5abe8 | 2017-11-27 16:45:56 -0800 | [diff] [blame] | 440 | seq_printf(m, "[<0>] %pB\n", (void *)entries[i]); |
Al Viro | a9712bc | 2011-03-23 15:52:50 -0400 | [diff] [blame] | 441 | } |
Thomas Gleixner | e988e5e | 2019-04-25 11:44:58 +0200 | [diff] [blame] | 442 | |
Al Viro | a9712bc | 2011-03-23 15:52:50 -0400 | [diff] [blame] | 443 | unlock_trace(task); |
Ken Chen | 2ec220e | 2008-11-10 11:26:08 +0300 | [diff] [blame] | 444 | } |
| 445 | kfree(entries); |
| 446 | |
Al Viro | a9712bc | 2011-03-23 15:52:50 -0400 | [diff] [blame] | 447 | return err; |
Ken Chen | 2ec220e | 2008-11-10 11:26:08 +0300 | [diff] [blame] | 448 | } |
| 449 | #endif |
| 450 | |
Naveen N. Rao | 5968cec | 2015-06-30 14:36:03 +0530 | [diff] [blame] | 451 | #ifdef CONFIG_SCHED_INFO |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 452 | /* |
| 453 | * Provides /proc/PID/schedstat |
| 454 | */ |
Alexey Dobriyan | f6e826c | 2014-08-08 14:21:46 -0700 | [diff] [blame] | 455 | static int proc_pid_schedstat(struct seq_file *m, struct pid_namespace *ns, |
| 456 | struct pid *pid, struct task_struct *task) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 457 | { |
Naveen N. Rao | 5968cec | 2015-06-30 14:36:03 +0530 | [diff] [blame] | 458 | if (unlikely(!sched_info_on())) |
Alexey Dobriyan | 08b5577 | 2019-03-05 15:50:35 -0800 | [diff] [blame] | 459 | seq_puts(m, "0 0 0\n"); |
Naveen N. Rao | 5968cec | 2015-06-30 14:36:03 +0530 | [diff] [blame] | 460 | else |
| 461 | seq_printf(m, "%llu %llu %lu\n", |
Joe Perches | 25ce319 | 2015-04-15 16:18:17 -0700 | [diff] [blame] | 462 | (unsigned long long)task->se.sum_exec_runtime, |
| 463 | (unsigned long long)task->sched_info.run_delay, |
| 464 | task->sched_info.pcount); |
| 465 | |
| 466 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 467 | } |
| 468 | #endif |
| 469 | |
Arjan van de Ven | 9745512 | 2008-01-25 21:08:34 +0100 | [diff] [blame] | 470 | #ifdef CONFIG_LATENCYTOP |
| 471 | static int lstats_show_proc(struct seq_file *m, void *v) |
| 472 | { |
| 473 | int i; |
Hiroshi Shimamoto | 13d77c3 | 2008-02-20 16:53:29 -0800 | [diff] [blame] | 474 | struct inode *inode = m->private; |
| 475 | struct task_struct *task = get_proc_task(inode); |
Arjan van de Ven | 9745512 | 2008-01-25 21:08:34 +0100 | [diff] [blame] | 476 | |
Hiroshi Shimamoto | 13d77c3 | 2008-02-20 16:53:29 -0800 | [diff] [blame] | 477 | if (!task) |
| 478 | return -ESRCH; |
| 479 | seq_puts(m, "Latency Top version : v0.1\n"); |
Alexey Dobriyan | f6d2f58 | 2018-08-21 21:54:34 -0700 | [diff] [blame] | 480 | for (i = 0; i < LT_SAVECOUNT; i++) { |
Joe Perches | 34e49d4 | 2011-01-12 17:00:30 -0800 | [diff] [blame] | 481 | struct latency_record *lr = &task->latency_record[i]; |
| 482 | if (lr->backtrace[0]) { |
Arjan van de Ven | 9745512 | 2008-01-25 21:08:34 +0100 | [diff] [blame] | 483 | int q; |
Joe Perches | 34e49d4 | 2011-01-12 17:00:30 -0800 | [diff] [blame] | 484 | seq_printf(m, "%i %li %li", |
| 485 | lr->count, lr->time, lr->max); |
Arjan van de Ven | 9745512 | 2008-01-25 21:08:34 +0100 | [diff] [blame] | 486 | for (q = 0; q < LT_BACKTRACEDEPTH; q++) { |
Joe Perches | 34e49d4 | 2011-01-12 17:00:30 -0800 | [diff] [blame] | 487 | unsigned long bt = lr->backtrace[q]; |
Thomas Gleixner | accddc4 | 2019-04-10 12:28:08 +0200 | [diff] [blame] | 488 | |
Joe Perches | 34e49d4 | 2011-01-12 17:00:30 -0800 | [diff] [blame] | 489 | if (!bt) |
Arjan van de Ven | 9745512 | 2008-01-25 21:08:34 +0100 | [diff] [blame] | 490 | break; |
Joe Perches | 34e49d4 | 2011-01-12 17:00:30 -0800 | [diff] [blame] | 491 | seq_printf(m, " %ps", (void *)bt); |
Arjan van de Ven | 9745512 | 2008-01-25 21:08:34 +0100 | [diff] [blame] | 492 | } |
Alexey Dobriyan | 9d6de12 | 2011-01-12 17:00:32 -0800 | [diff] [blame] | 493 | seq_putc(m, '\n'); |
Arjan van de Ven | 9745512 | 2008-01-25 21:08:34 +0100 | [diff] [blame] | 494 | } |
| 495 | |
| 496 | } |
Hiroshi Shimamoto | 13d77c3 | 2008-02-20 16:53:29 -0800 | [diff] [blame] | 497 | put_task_struct(task); |
Arjan van de Ven | 9745512 | 2008-01-25 21:08:34 +0100 | [diff] [blame] | 498 | return 0; |
| 499 | } |
| 500 | |
| 501 | static int lstats_open(struct inode *inode, struct file *file) |
| 502 | { |
Hiroshi Shimamoto | 13d77c3 | 2008-02-20 16:53:29 -0800 | [diff] [blame] | 503 | return single_open(file, lstats_show_proc, inode); |
Hiroshi Shimamoto | d6643d1 | 2008-02-14 10:27:00 -0800 | [diff] [blame] | 504 | } |
| 505 | |
Arjan van de Ven | 9745512 | 2008-01-25 21:08:34 +0100 | [diff] [blame] | 506 | static ssize_t lstats_write(struct file *file, const char __user *buf, |
| 507 | size_t count, loff_t *offs) |
| 508 | { |
Al Viro | 496ad9a | 2013-01-23 17:07:38 -0500 | [diff] [blame] | 509 | struct task_struct *task = get_proc_task(file_inode(file)); |
Arjan van de Ven | 9745512 | 2008-01-25 21:08:34 +0100 | [diff] [blame] | 510 | |
Hiroshi Shimamoto | 13d77c3 | 2008-02-20 16:53:29 -0800 | [diff] [blame] | 511 | if (!task) |
| 512 | return -ESRCH; |
Lin Feng | e02c9b0 | 2019-05-14 15:42:34 -0700 | [diff] [blame] | 513 | clear_tsk_latency_tracing(task); |
Hiroshi Shimamoto | 13d77c3 | 2008-02-20 16:53:29 -0800 | [diff] [blame] | 514 | put_task_struct(task); |
Arjan van de Ven | 9745512 | 2008-01-25 21:08:34 +0100 | [diff] [blame] | 515 | |
| 516 | return count; |
| 517 | } |
| 518 | |
| 519 | static const struct file_operations proc_lstats_operations = { |
| 520 | .open = lstats_open, |
| 521 | .read = seq_read, |
| 522 | .write = lstats_write, |
| 523 | .llseek = seq_lseek, |
Hiroshi Shimamoto | 13d77c3 | 2008-02-20 16:53:29 -0800 | [diff] [blame] | 524 | .release = single_release, |
Arjan van de Ven | 9745512 | 2008-01-25 21:08:34 +0100 | [diff] [blame] | 525 | }; |
| 526 | |
| 527 | #endif |
| 528 | |
Alexey Dobriyan | 6ba51e3 | 2014-08-08 14:21:48 -0700 | [diff] [blame] | 529 | static int proc_oom_score(struct seq_file *m, struct pid_namespace *ns, |
| 530 | struct pid *pid, struct task_struct *task) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 531 | { |
Arun KS | ca79b0c | 2018-12-28 00:34:29 -0800 | [diff] [blame] | 532 | unsigned long totalpages = totalram_pages() + total_swap_pages; |
Oleg Nesterov | b95c35e | 2010-04-01 15:13:57 +0200 | [diff] [blame] | 533 | unsigned long points = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 534 | |
Oleg Nesterov | ef41939 | 2016-08-02 14:03:19 -0700 | [diff] [blame] | 535 | points = oom_badness(task, NULL, NULL, totalpages) * |
| 536 | 1000 / totalpages; |
Joe Perches | 25ce319 | 2015-04-15 16:18:17 -0700 | [diff] [blame] | 537 | seq_printf(m, "%lu\n", points); |
| 538 | |
| 539 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 540 | } |
| 541 | |
Neil Horman | d85f50d | 2007-10-18 23:40:37 -0700 | [diff] [blame] | 542 | struct limit_names { |
Alexey Dobriyan | cedbcca | 2014-08-08 14:21:33 -0700 | [diff] [blame] | 543 | const char *name; |
| 544 | const char *unit; |
Neil Horman | d85f50d | 2007-10-18 23:40:37 -0700 | [diff] [blame] | 545 | }; |
| 546 | |
| 547 | static const struct limit_names lnames[RLIM_NLIMITS] = { |
Kees Cook | cff4edb | 2009-09-22 16:45:32 -0700 | [diff] [blame] | 548 | [RLIMIT_CPU] = {"Max cpu time", "seconds"}, |
Neil Horman | d85f50d | 2007-10-18 23:40:37 -0700 | [diff] [blame] | 549 | [RLIMIT_FSIZE] = {"Max file size", "bytes"}, |
| 550 | [RLIMIT_DATA] = {"Max data size", "bytes"}, |
| 551 | [RLIMIT_STACK] = {"Max stack size", "bytes"}, |
| 552 | [RLIMIT_CORE] = {"Max core file size", "bytes"}, |
| 553 | [RLIMIT_RSS] = {"Max resident set", "bytes"}, |
| 554 | [RLIMIT_NPROC] = {"Max processes", "processes"}, |
| 555 | [RLIMIT_NOFILE] = {"Max open files", "files"}, |
| 556 | [RLIMIT_MEMLOCK] = {"Max locked memory", "bytes"}, |
| 557 | [RLIMIT_AS] = {"Max address space", "bytes"}, |
| 558 | [RLIMIT_LOCKS] = {"Max file locks", "locks"}, |
| 559 | [RLIMIT_SIGPENDING] = {"Max pending signals", "signals"}, |
| 560 | [RLIMIT_MSGQUEUE] = {"Max msgqueue size", "bytes"}, |
| 561 | [RLIMIT_NICE] = {"Max nice priority", NULL}, |
| 562 | [RLIMIT_RTPRIO] = {"Max realtime priority", NULL}, |
Eugene Teo | 8808117 | 2008-02-23 15:23:52 -0800 | [diff] [blame] | 563 | [RLIMIT_RTTIME] = {"Max realtime timeout", "us"}, |
Neil Horman | d85f50d | 2007-10-18 23:40:37 -0700 | [diff] [blame] | 564 | }; |
| 565 | |
| 566 | /* Display limits for a process */ |
Alexey Dobriyan | 1c963eb | 2014-08-08 14:21:37 -0700 | [diff] [blame] | 567 | static int proc_pid_limits(struct seq_file *m, struct pid_namespace *ns, |
| 568 | struct pid *pid, struct task_struct *task) |
Neil Horman | d85f50d | 2007-10-18 23:40:37 -0700 | [diff] [blame] | 569 | { |
| 570 | unsigned int i; |
Neil Horman | d85f50d | 2007-10-18 23:40:37 -0700 | [diff] [blame] | 571 | unsigned long flags; |
Neil Horman | d85f50d | 2007-10-18 23:40:37 -0700 | [diff] [blame] | 572 | |
| 573 | struct rlimit rlim[RLIM_NLIMITS]; |
| 574 | |
Lai Jiangshan | a6bebbc | 2008-10-05 00:51:15 +0400 | [diff] [blame] | 575 | if (!lock_task_sighand(task, &flags)) |
Neil Horman | d85f50d | 2007-10-18 23:40:37 -0700 | [diff] [blame] | 576 | return 0; |
Neil Horman | d85f50d | 2007-10-18 23:40:37 -0700 | [diff] [blame] | 577 | memcpy(rlim, task->signal->rlim, sizeof(struct rlimit) * RLIM_NLIMITS); |
| 578 | unlock_task_sighand(task, &flags); |
Neil Horman | d85f50d | 2007-10-18 23:40:37 -0700 | [diff] [blame] | 579 | |
| 580 | /* |
| 581 | * print the file header |
| 582 | */ |
Alexey Dobriyan | afe922c | 2019-01-03 15:26:09 -0800 | [diff] [blame] | 583 | seq_puts(m, "Limit " |
| 584 | "Soft Limit " |
| 585 | "Hard Limit " |
| 586 | "Units \n"); |
Neil Horman | d85f50d | 2007-10-18 23:40:37 -0700 | [diff] [blame] | 587 | |
| 588 | for (i = 0; i < RLIM_NLIMITS; i++) { |
| 589 | if (rlim[i].rlim_cur == RLIM_INFINITY) |
Alexey Dobriyan | 1c963eb | 2014-08-08 14:21:37 -0700 | [diff] [blame] | 590 | seq_printf(m, "%-25s %-20s ", |
Joe Perches | 25ce319 | 2015-04-15 16:18:17 -0700 | [diff] [blame] | 591 | lnames[i].name, "unlimited"); |
Neil Horman | d85f50d | 2007-10-18 23:40:37 -0700 | [diff] [blame] | 592 | else |
Alexey Dobriyan | 1c963eb | 2014-08-08 14:21:37 -0700 | [diff] [blame] | 593 | seq_printf(m, "%-25s %-20lu ", |
Joe Perches | 25ce319 | 2015-04-15 16:18:17 -0700 | [diff] [blame] | 594 | lnames[i].name, rlim[i].rlim_cur); |
Neil Horman | d85f50d | 2007-10-18 23:40:37 -0700 | [diff] [blame] | 595 | |
| 596 | if (rlim[i].rlim_max == RLIM_INFINITY) |
Alexey Dobriyan | 1c963eb | 2014-08-08 14:21:37 -0700 | [diff] [blame] | 597 | seq_printf(m, "%-20s ", "unlimited"); |
Neil Horman | d85f50d | 2007-10-18 23:40:37 -0700 | [diff] [blame] | 598 | else |
Alexey Dobriyan | 1c963eb | 2014-08-08 14:21:37 -0700 | [diff] [blame] | 599 | seq_printf(m, "%-20lu ", rlim[i].rlim_max); |
Neil Horman | d85f50d | 2007-10-18 23:40:37 -0700 | [diff] [blame] | 600 | |
| 601 | if (lnames[i].unit) |
Alexey Dobriyan | 1c963eb | 2014-08-08 14:21:37 -0700 | [diff] [blame] | 602 | seq_printf(m, "%-10s\n", lnames[i].unit); |
Neil Horman | d85f50d | 2007-10-18 23:40:37 -0700 | [diff] [blame] | 603 | else |
Alexey Dobriyan | 1c963eb | 2014-08-08 14:21:37 -0700 | [diff] [blame] | 604 | seq_putc(m, '\n'); |
Neil Horman | d85f50d | 2007-10-18 23:40:37 -0700 | [diff] [blame] | 605 | } |
| 606 | |
Alexey Dobriyan | 1c963eb | 2014-08-08 14:21:37 -0700 | [diff] [blame] | 607 | return 0; |
Neil Horman | d85f50d | 2007-10-18 23:40:37 -0700 | [diff] [blame] | 608 | } |
| 609 | |
Roland McGrath | ebcb673 | 2008-07-25 19:46:00 -0700 | [diff] [blame] | 610 | #ifdef CONFIG_HAVE_ARCH_TRACEHOOK |
Alexey Dobriyan | 09d93bd | 2014-08-08 14:21:39 -0700 | [diff] [blame] | 611 | static int proc_pid_syscall(struct seq_file *m, struct pid_namespace *ns, |
| 612 | struct pid *pid, struct task_struct *task) |
Roland McGrath | ebcb673 | 2008-07-25 19:46:00 -0700 | [diff] [blame] | 613 | { |
Steven Rostedt (Red Hat) | 631b7ab | 2016-11-07 16:26:35 -0500 | [diff] [blame] | 614 | struct syscall_info info; |
| 615 | u64 *args = &info.data.args[0]; |
Joe Perches | 25ce319 | 2015-04-15 16:18:17 -0700 | [diff] [blame] | 616 | int res; |
| 617 | |
| 618 | res = lock_trace(task); |
Al Viro | a9712bc | 2011-03-23 15:52:50 -0400 | [diff] [blame] | 619 | if (res) |
| 620 | return res; |
Roland McGrath | ebcb673 | 2008-07-25 19:46:00 -0700 | [diff] [blame] | 621 | |
Steven Rostedt (Red Hat) | 631b7ab | 2016-11-07 16:26:35 -0500 | [diff] [blame] | 622 | if (task_current_syscall(task, &info)) |
Alexey Dobriyan | 09d93bd | 2014-08-08 14:21:39 -0700 | [diff] [blame] | 623 | seq_puts(m, "running\n"); |
Steven Rostedt (Red Hat) | 631b7ab | 2016-11-07 16:26:35 -0500 | [diff] [blame] | 624 | else if (info.data.nr < 0) |
| 625 | seq_printf(m, "%d 0x%llx 0x%llx\n", |
| 626 | info.data.nr, info.sp, info.data.instruction_pointer); |
Al Viro | a9712bc | 2011-03-23 15:52:50 -0400 | [diff] [blame] | 627 | else |
Alexey Dobriyan | 09d93bd | 2014-08-08 14:21:39 -0700 | [diff] [blame] | 628 | seq_printf(m, |
Steven Rostedt (Red Hat) | 631b7ab | 2016-11-07 16:26:35 -0500 | [diff] [blame] | 629 | "%d 0x%llx 0x%llx 0x%llx 0x%llx 0x%llx 0x%llx 0x%llx 0x%llx\n", |
| 630 | info.data.nr, |
Roland McGrath | ebcb673 | 2008-07-25 19:46:00 -0700 | [diff] [blame] | 631 | args[0], args[1], args[2], args[3], args[4], args[5], |
Steven Rostedt (Red Hat) | 631b7ab | 2016-11-07 16:26:35 -0500 | [diff] [blame] | 632 | info.sp, info.data.instruction_pointer); |
Al Viro | a9712bc | 2011-03-23 15:52:50 -0400 | [diff] [blame] | 633 | unlock_trace(task); |
Joe Perches | 25ce319 | 2015-04-15 16:18:17 -0700 | [diff] [blame] | 634 | |
| 635 | return 0; |
Roland McGrath | ebcb673 | 2008-07-25 19:46:00 -0700 | [diff] [blame] | 636 | } |
| 637 | #endif /* CONFIG_HAVE_ARCH_TRACEHOOK */ |
| 638 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 639 | /************************************************************************/ |
| 640 | /* Here the fs part begins */ |
| 641 | /************************************************************************/ |
| 642 | |
| 643 | /* permission checks */ |
Eric W. Biederman | 778c114 | 2006-06-26 00:25:58 -0700 | [diff] [blame] | 644 | static int proc_fd_access_allowed(struct inode *inode) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 645 | { |
Eric W. Biederman | 778c114 | 2006-06-26 00:25:58 -0700 | [diff] [blame] | 646 | struct task_struct *task; |
| 647 | int allowed = 0; |
Eric W. Biederman | df26c40 | 2006-06-26 00:25:59 -0700 | [diff] [blame] | 648 | /* Allow access to a task's file descriptors if it is us or we |
| 649 | * may use ptrace attach to the process and find out that |
| 650 | * information. |
Eric W. Biederman | 778c114 | 2006-06-26 00:25:58 -0700 | [diff] [blame] | 651 | */ |
| 652 | task = get_proc_task(inode); |
Eric W. Biederman | df26c40 | 2006-06-26 00:25:59 -0700 | [diff] [blame] | 653 | if (task) { |
Jann Horn | caaee62 | 2016-01-20 15:00:04 -0800 | [diff] [blame] | 654 | allowed = ptrace_may_access(task, PTRACE_MODE_READ_FSCREDS); |
Eric W. Biederman | 778c114 | 2006-06-26 00:25:58 -0700 | [diff] [blame] | 655 | put_task_struct(task); |
Eric W. Biederman | df26c40 | 2006-06-26 00:25:59 -0700 | [diff] [blame] | 656 | } |
Eric W. Biederman | 778c114 | 2006-06-26 00:25:58 -0700 | [diff] [blame] | 657 | return allowed; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 658 | } |
| 659 | |
Eric W. Biederman | 6b4e306 | 2010-03-07 16:41:34 -0800 | [diff] [blame] | 660 | int proc_setattr(struct dentry *dentry, struct iattr *attr) |
Linus Torvalds | 6d76fa5 | 2006-07-15 12:26:45 -0700 | [diff] [blame] | 661 | { |
| 662 | int error; |
David Howells | 2b0143b | 2015-03-17 22:25:59 +0000 | [diff] [blame] | 663 | struct inode *inode = d_inode(dentry); |
Linus Torvalds | 6d76fa5 | 2006-07-15 12:26:45 -0700 | [diff] [blame] | 664 | |
| 665 | if (attr->ia_valid & ATTR_MODE) |
| 666 | return -EPERM; |
| 667 | |
Jan Kara | 31051c8 | 2016-05-26 16:55:18 +0200 | [diff] [blame] | 668 | error = setattr_prepare(dentry, attr); |
Christoph Hellwig | 1025774 | 2010-06-04 11:30:02 +0200 | [diff] [blame] | 669 | if (error) |
| 670 | return error; |
| 671 | |
Christoph Hellwig | 1025774 | 2010-06-04 11:30:02 +0200 | [diff] [blame] | 672 | setattr_copy(inode, attr); |
| 673 | mark_inode_dirty(inode); |
| 674 | return 0; |
Linus Torvalds | 6d76fa5 | 2006-07-15 12:26:45 -0700 | [diff] [blame] | 675 | } |
| 676 | |
Vasiliy Kulikov | 0499680 | 2012-01-10 15:11:31 -0800 | [diff] [blame] | 677 | /* |
| 678 | * May current process learn task's sched/cmdline info (for hide_pid_min=1) |
| 679 | * or euid/egid (for hide_pid_min=2)? |
| 680 | */ |
| 681 | static bool has_pid_permissions(struct pid_namespace *pid, |
| 682 | struct task_struct *task, |
| 683 | int hide_pid_min) |
| 684 | { |
| 685 | if (pid->hide_pid < hide_pid_min) |
| 686 | return true; |
| 687 | if (in_group_p(pid->pid_gid)) |
| 688 | return true; |
Jann Horn | caaee62 | 2016-01-20 15:00:04 -0800 | [diff] [blame] | 689 | return ptrace_may_access(task, PTRACE_MODE_READ_FSCREDS); |
Vasiliy Kulikov | 0499680 | 2012-01-10 15:11:31 -0800 | [diff] [blame] | 690 | } |
| 691 | |
| 692 | |
| 693 | static int proc_pid_permission(struct inode *inode, int mask) |
| 694 | { |
Christoph Hellwig | 76f668b | 2018-05-16 07:19:01 +0200 | [diff] [blame] | 695 | struct pid_namespace *pid = proc_pid_ns(inode); |
Vasiliy Kulikov | 0499680 | 2012-01-10 15:11:31 -0800 | [diff] [blame] | 696 | struct task_struct *task; |
| 697 | bool has_perms; |
| 698 | |
| 699 | task = get_proc_task(inode); |
Xiaotian Feng | a2ef990 | 2012-01-12 17:17:08 -0800 | [diff] [blame] | 700 | if (!task) |
| 701 | return -ESRCH; |
Lafcadio Wluiki | 796f571 | 2017-02-24 15:00:23 -0800 | [diff] [blame] | 702 | has_perms = has_pid_permissions(pid, task, HIDEPID_NO_ACCESS); |
Vasiliy Kulikov | 0499680 | 2012-01-10 15:11:31 -0800 | [diff] [blame] | 703 | put_task_struct(task); |
| 704 | |
| 705 | if (!has_perms) { |
Lafcadio Wluiki | 796f571 | 2017-02-24 15:00:23 -0800 | [diff] [blame] | 706 | if (pid->hide_pid == HIDEPID_INVISIBLE) { |
Vasiliy Kulikov | 0499680 | 2012-01-10 15:11:31 -0800 | [diff] [blame] | 707 | /* |
| 708 | * Let's make getdents(), stat(), and open() |
| 709 | * consistent with each other. If a process |
| 710 | * may not stat() a file, it shouldn't be seen |
| 711 | * in procfs at all. |
| 712 | */ |
| 713 | return -ENOENT; |
| 714 | } |
| 715 | |
| 716 | return -EPERM; |
| 717 | } |
| 718 | return generic_permission(inode, mask); |
| 719 | } |
| 720 | |
| 721 | |
| 722 | |
Arjan van de Ven | c5ef1c4 | 2007-02-12 00:55:40 -0800 | [diff] [blame] | 723 | static const struct inode_operations proc_def_inode_operations = { |
Linus Torvalds | 6d76fa5 | 2006-07-15 12:26:45 -0700 | [diff] [blame] | 724 | .setattr = proc_setattr, |
| 725 | }; |
| 726 | |
Eric W. Biederman | be61408 | 2008-02-08 04:18:30 -0800 | [diff] [blame] | 727 | static int proc_single_show(struct seq_file *m, void *v) |
| 728 | { |
| 729 | struct inode *inode = m->private; |
Christoph Hellwig | 76f668b | 2018-05-16 07:19:01 +0200 | [diff] [blame] | 730 | struct pid_namespace *ns = proc_pid_ns(inode); |
| 731 | struct pid *pid = proc_pid(inode); |
Eric W. Biederman | be61408 | 2008-02-08 04:18:30 -0800 | [diff] [blame] | 732 | struct task_struct *task; |
| 733 | int ret; |
| 734 | |
Eric W. Biederman | be61408 | 2008-02-08 04:18:30 -0800 | [diff] [blame] | 735 | task = get_pid_task(pid, PIDTYPE_PID); |
| 736 | if (!task) |
| 737 | return -ESRCH; |
| 738 | |
| 739 | ret = PROC_I(inode)->op.proc_show(m, ns, pid, task); |
| 740 | |
| 741 | put_task_struct(task); |
| 742 | return ret; |
| 743 | } |
| 744 | |
| 745 | static int proc_single_open(struct inode *inode, struct file *filp) |
| 746 | { |
Jovi Zhang | c6a3405 | 2011-01-12 17:00:34 -0800 | [diff] [blame] | 747 | return single_open(filp, proc_single_show, inode); |
Eric W. Biederman | be61408 | 2008-02-08 04:18:30 -0800 | [diff] [blame] | 748 | } |
| 749 | |
| 750 | static const struct file_operations proc_single_file_operations = { |
| 751 | .open = proc_single_open, |
| 752 | .read = seq_read, |
| 753 | .llseek = seq_lseek, |
| 754 | .release = single_release, |
| 755 | }; |
| 756 | |
Oleg Nesterov | 5381e16 | 2014-10-09 15:25:24 -0700 | [diff] [blame] | 757 | |
| 758 | struct mm_struct *proc_mem_open(struct inode *inode, unsigned int mode) |
| 759 | { |
| 760 | struct task_struct *task = get_proc_task(inode); |
| 761 | struct mm_struct *mm = ERR_PTR(-ESRCH); |
| 762 | |
| 763 | if (task) { |
Jann Horn | caaee62 | 2016-01-20 15:00:04 -0800 | [diff] [blame] | 764 | mm = mm_access(task, mode | PTRACE_MODE_FSCREDS); |
Oleg Nesterov | 5381e16 | 2014-10-09 15:25:24 -0700 | [diff] [blame] | 765 | put_task_struct(task); |
| 766 | |
| 767 | if (!IS_ERR_OR_NULL(mm)) { |
| 768 | /* ensure this mm_struct can't be freed */ |
Vegard Nossum | f1f1007 | 2017-02-27 14:30:07 -0800 | [diff] [blame] | 769 | mmgrab(mm); |
Oleg Nesterov | 5381e16 | 2014-10-09 15:25:24 -0700 | [diff] [blame] | 770 | /* but do not pin its memory */ |
| 771 | mmput(mm); |
| 772 | } |
| 773 | } |
| 774 | |
| 775 | return mm; |
| 776 | } |
| 777 | |
Cong Wang | b409e57 | 2012-05-31 16:26:17 -0700 | [diff] [blame] | 778 | static int __mem_open(struct inode *inode, struct file *file, unsigned int mode) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 779 | { |
Oleg Nesterov | 5381e16 | 2014-10-09 15:25:24 -0700 | [diff] [blame] | 780 | struct mm_struct *mm = proc_mem_open(inode, mode); |
Linus Torvalds | e268337 | 2012-01-17 15:21:19 -0800 | [diff] [blame] | 781 | |
| 782 | if (IS_ERR(mm)) |
| 783 | return PTR_ERR(mm); |
| 784 | |
Linus Torvalds | e268337 | 2012-01-17 15:21:19 -0800 | [diff] [blame] | 785 | file->private_data = mm; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 786 | return 0; |
| 787 | } |
| 788 | |
Cong Wang | b409e57 | 2012-05-31 16:26:17 -0700 | [diff] [blame] | 789 | static int mem_open(struct inode *inode, struct file *file) |
| 790 | { |
Djalal Harouni | bc452b4 | 2012-07-30 14:42:28 -0700 | [diff] [blame] | 791 | int ret = __mem_open(inode, file, PTRACE_MODE_ATTACH); |
| 792 | |
| 793 | /* OK to pass negative loff_t, we can catch out-of-range */ |
| 794 | file->f_mode |= FMODE_UNSIGNED_OFFSET; |
| 795 | |
| 796 | return ret; |
Cong Wang | b409e57 | 2012-05-31 16:26:17 -0700 | [diff] [blame] | 797 | } |
| 798 | |
Oleg Nesterov | 572d34b | 2012-01-31 17:14:54 +0100 | [diff] [blame] | 799 | static ssize_t mem_rw(struct file *file, char __user *buf, |
| 800 | size_t count, loff_t *ppos, int write) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 801 | { |
Linus Torvalds | e268337 | 2012-01-17 15:21:19 -0800 | [diff] [blame] | 802 | struct mm_struct *mm = file->private_data; |
Oleg Nesterov | 572d34b | 2012-01-31 17:14:54 +0100 | [diff] [blame] | 803 | unsigned long addr = *ppos; |
| 804 | ssize_t copied; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 805 | char *page; |
Linus Torvalds | 272ddc8 | 2016-10-24 19:00:44 -0700 | [diff] [blame] | 806 | unsigned int flags; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 807 | |
Linus Torvalds | e268337 | 2012-01-17 15:21:19 -0800 | [diff] [blame] | 808 | if (!mm) |
| 809 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 810 | |
Michal Hocko | 0ee931c | 2017-09-13 16:28:29 -0700 | [diff] [blame] | 811 | page = (char *)__get_free_page(GFP_KERNEL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 812 | if (!page) |
Linus Torvalds | e268337 | 2012-01-17 15:21:19 -0800 | [diff] [blame] | 813 | return -ENOMEM; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 814 | |
Frederik Deweerdt | f7ca54f | 2006-09-29 02:01:02 -0700 | [diff] [blame] | 815 | copied = 0; |
Vegard Nossum | 388f793 | 2017-02-27 14:30:13 -0800 | [diff] [blame] | 816 | if (!mmget_not_zero(mm)) |
Oleg Nesterov | 6d08f2c | 2012-01-31 17:15:11 +0100 | [diff] [blame] | 817 | goto free; |
| 818 | |
Linus Torvalds | f511c0b | 2017-05-30 12:38:59 -0700 | [diff] [blame] | 819 | flags = FOLL_FORCE | (write ? FOLL_WRITE : 0); |
Lorenzo Stoakes | 6347e8d | 2016-10-13 01:20:19 +0100 | [diff] [blame] | 820 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 821 | while (count > 0) { |
Oleg Nesterov | 572d34b | 2012-01-31 17:14:54 +0100 | [diff] [blame] | 822 | int this_len = min_t(int, count, PAGE_SIZE); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 823 | |
Oleg Nesterov | 572d34b | 2012-01-31 17:14:54 +0100 | [diff] [blame] | 824 | if (write && copy_from_user(page, buf, this_len)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 825 | copied = -EFAULT; |
| 826 | break; |
| 827 | } |
Oleg Nesterov | 572d34b | 2012-01-31 17:14:54 +0100 | [diff] [blame] | 828 | |
Lorenzo Stoakes | 6347e8d | 2016-10-13 01:20:19 +0100 | [diff] [blame] | 829 | this_len = access_remote_vm(mm, addr, page, this_len, flags); |
Oleg Nesterov | 572d34b | 2012-01-31 17:14:54 +0100 | [diff] [blame] | 830 | if (!this_len) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 831 | if (!copied) |
| 832 | copied = -EIO; |
| 833 | break; |
| 834 | } |
Oleg Nesterov | 572d34b | 2012-01-31 17:14:54 +0100 | [diff] [blame] | 835 | |
| 836 | if (!write && copy_to_user(buf, page, this_len)) { |
| 837 | copied = -EFAULT; |
| 838 | break; |
| 839 | } |
| 840 | |
| 841 | buf += this_len; |
| 842 | addr += this_len; |
| 843 | copied += this_len; |
| 844 | count -= this_len; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 845 | } |
Oleg Nesterov | 572d34b | 2012-01-31 17:14:54 +0100 | [diff] [blame] | 846 | *ppos = addr; |
KOSAKI Motohiro | 30cd890 | 2011-05-26 16:25:52 -0700 | [diff] [blame] | 847 | |
Oleg Nesterov | 6d08f2c | 2012-01-31 17:15:11 +0100 | [diff] [blame] | 848 | mmput(mm); |
| 849 | free: |
KOSAKI Motohiro | 30cd890 | 2011-05-26 16:25:52 -0700 | [diff] [blame] | 850 | free_page((unsigned long) page); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 851 | return copied; |
| 852 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 853 | |
Oleg Nesterov | 572d34b | 2012-01-31 17:14:54 +0100 | [diff] [blame] | 854 | static ssize_t mem_read(struct file *file, char __user *buf, |
| 855 | size_t count, loff_t *ppos) |
| 856 | { |
| 857 | return mem_rw(file, buf, count, ppos, 0); |
| 858 | } |
| 859 | |
| 860 | static ssize_t mem_write(struct file *file, const char __user *buf, |
| 861 | size_t count, loff_t *ppos) |
| 862 | { |
| 863 | return mem_rw(file, (char __user*)buf, count, ppos, 1); |
| 864 | } |
| 865 | |
Matt Mackall | 85863e4 | 2008-02-04 22:29:04 -0800 | [diff] [blame] | 866 | loff_t mem_lseek(struct file *file, loff_t offset, int orig) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 867 | { |
| 868 | switch (orig) { |
| 869 | case 0: |
| 870 | file->f_pos = offset; |
| 871 | break; |
| 872 | case 1: |
| 873 | file->f_pos += offset; |
| 874 | break; |
| 875 | default: |
| 876 | return -EINVAL; |
| 877 | } |
| 878 | force_successful_syscall_return(); |
| 879 | return file->f_pos; |
| 880 | } |
| 881 | |
Linus Torvalds | e268337 | 2012-01-17 15:21:19 -0800 | [diff] [blame] | 882 | static int mem_release(struct inode *inode, struct file *file) |
| 883 | { |
| 884 | struct mm_struct *mm = file->private_data; |
Oleg Nesterov | 71879d3 | 2012-01-31 17:14:38 +0100 | [diff] [blame] | 885 | if (mm) |
Oleg Nesterov | 6d08f2c | 2012-01-31 17:15:11 +0100 | [diff] [blame] | 886 | mmdrop(mm); |
Linus Torvalds | e268337 | 2012-01-17 15:21:19 -0800 | [diff] [blame] | 887 | return 0; |
| 888 | } |
| 889 | |
Arjan van de Ven | 00977a5 | 2007-02-12 00:55:34 -0800 | [diff] [blame] | 890 | static const struct file_operations proc_mem_operations = { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 891 | .llseek = mem_lseek, |
| 892 | .read = mem_read, |
| 893 | .write = mem_write, |
| 894 | .open = mem_open, |
Linus Torvalds | e268337 | 2012-01-17 15:21:19 -0800 | [diff] [blame] | 895 | .release = mem_release, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 896 | }; |
| 897 | |
Cong Wang | b409e57 | 2012-05-31 16:26:17 -0700 | [diff] [blame] | 898 | static int environ_open(struct inode *inode, struct file *file) |
| 899 | { |
| 900 | return __mem_open(inode, file, PTRACE_MODE_READ); |
| 901 | } |
| 902 | |
James Pearson | 315e28c | 2007-10-16 23:30:17 -0700 | [diff] [blame] | 903 | static ssize_t environ_read(struct file *file, char __user *buf, |
| 904 | size_t count, loff_t *ppos) |
| 905 | { |
James Pearson | 315e28c | 2007-10-16 23:30:17 -0700 | [diff] [blame] | 906 | char *page; |
| 907 | unsigned long src = *ppos; |
Cong Wang | b409e57 | 2012-05-31 16:26:17 -0700 | [diff] [blame] | 908 | int ret = 0; |
| 909 | struct mm_struct *mm = file->private_data; |
Mateusz Guzik | a3b609e | 2016-01-20 15:01:05 -0800 | [diff] [blame] | 910 | unsigned long env_start, env_end; |
James Pearson | 315e28c | 2007-10-16 23:30:17 -0700 | [diff] [blame] | 911 | |
Mathias Krause | 8148a73 | 2016-05-05 16:22:26 -0700 | [diff] [blame] | 912 | /* Ensure the process spawned far enough to have an environment. */ |
| 913 | if (!mm || !mm->env_end) |
Cong Wang | b409e57 | 2012-05-31 16:26:17 -0700 | [diff] [blame] | 914 | return 0; |
James Pearson | 315e28c | 2007-10-16 23:30:17 -0700 | [diff] [blame] | 915 | |
Michal Hocko | 0ee931c | 2017-09-13 16:28:29 -0700 | [diff] [blame] | 916 | page = (char *)__get_free_page(GFP_KERNEL); |
James Pearson | 315e28c | 2007-10-16 23:30:17 -0700 | [diff] [blame] | 917 | if (!page) |
Cong Wang | b409e57 | 2012-05-31 16:26:17 -0700 | [diff] [blame] | 918 | return -ENOMEM; |
James Pearson | 315e28c | 2007-10-16 23:30:17 -0700 | [diff] [blame] | 919 | |
Al Viro | d6f64b8 | 2011-02-15 22:26:01 -0500 | [diff] [blame] | 920 | ret = 0; |
Vegard Nossum | 388f793 | 2017-02-27 14:30:13 -0800 | [diff] [blame] | 921 | if (!mmget_not_zero(mm)) |
Cong Wang | b409e57 | 2012-05-31 16:26:17 -0700 | [diff] [blame] | 922 | goto free; |
Mateusz Guzik | a3b609e | 2016-01-20 15:01:05 -0800 | [diff] [blame] | 923 | |
Yang Shi | 88aa7cc | 2018-06-07 17:05:28 -0700 | [diff] [blame] | 924 | spin_lock(&mm->arg_lock); |
Mateusz Guzik | a3b609e | 2016-01-20 15:01:05 -0800 | [diff] [blame] | 925 | env_start = mm->env_start; |
| 926 | env_end = mm->env_end; |
Yang Shi | 88aa7cc | 2018-06-07 17:05:28 -0700 | [diff] [blame] | 927 | spin_unlock(&mm->arg_lock); |
Mateusz Guzik | a3b609e | 2016-01-20 15:01:05 -0800 | [diff] [blame] | 928 | |
James Pearson | 315e28c | 2007-10-16 23:30:17 -0700 | [diff] [blame] | 929 | while (count > 0) { |
Djalal Harouni | e8905ec | 2012-07-30 14:42:26 -0700 | [diff] [blame] | 930 | size_t this_len, max_len; |
| 931 | int retval; |
| 932 | |
Mateusz Guzik | a3b609e | 2016-01-20 15:01:05 -0800 | [diff] [blame] | 933 | if (src >= (env_end - env_start)) |
Djalal Harouni | e8905ec | 2012-07-30 14:42:26 -0700 | [diff] [blame] | 934 | break; |
James Pearson | 315e28c | 2007-10-16 23:30:17 -0700 | [diff] [blame] | 935 | |
Mateusz Guzik | a3b609e | 2016-01-20 15:01:05 -0800 | [diff] [blame] | 936 | this_len = env_end - (env_start + src); |
James Pearson | 315e28c | 2007-10-16 23:30:17 -0700 | [diff] [blame] | 937 | |
Djalal Harouni | e8905ec | 2012-07-30 14:42:26 -0700 | [diff] [blame] | 938 | max_len = min_t(size_t, PAGE_SIZE, count); |
| 939 | this_len = min(max_len, this_len); |
James Pearson | 315e28c | 2007-10-16 23:30:17 -0700 | [diff] [blame] | 940 | |
Willy Tarreau | 7f7ccc2 | 2018-05-11 08:11:44 +0200 | [diff] [blame] | 941 | retval = access_remote_vm(mm, (env_start + src), page, this_len, FOLL_ANON); |
James Pearson | 315e28c | 2007-10-16 23:30:17 -0700 | [diff] [blame] | 942 | |
| 943 | if (retval <= 0) { |
| 944 | ret = retval; |
| 945 | break; |
| 946 | } |
| 947 | |
| 948 | if (copy_to_user(buf, page, retval)) { |
| 949 | ret = -EFAULT; |
| 950 | break; |
| 951 | } |
| 952 | |
| 953 | ret += retval; |
| 954 | src += retval; |
| 955 | buf += retval; |
| 956 | count -= retval; |
| 957 | } |
| 958 | *ppos = src; |
James Pearson | 315e28c | 2007-10-16 23:30:17 -0700 | [diff] [blame] | 959 | mmput(mm); |
Cong Wang | b409e57 | 2012-05-31 16:26:17 -0700 | [diff] [blame] | 960 | |
| 961 | free: |
James Pearson | 315e28c | 2007-10-16 23:30:17 -0700 | [diff] [blame] | 962 | free_page((unsigned long) page); |
James Pearson | 315e28c | 2007-10-16 23:30:17 -0700 | [diff] [blame] | 963 | return ret; |
| 964 | } |
| 965 | |
| 966 | static const struct file_operations proc_environ_operations = { |
Cong Wang | b409e57 | 2012-05-31 16:26:17 -0700 | [diff] [blame] | 967 | .open = environ_open, |
James Pearson | 315e28c | 2007-10-16 23:30:17 -0700 | [diff] [blame] | 968 | .read = environ_read, |
Arnd Bergmann | 87df842 | 2010-03-17 23:06:02 +0100 | [diff] [blame] | 969 | .llseek = generic_file_llseek, |
Cong Wang | b409e57 | 2012-05-31 16:26:17 -0700 | [diff] [blame] | 970 | .release = mem_release, |
James Pearson | 315e28c | 2007-10-16 23:30:17 -0700 | [diff] [blame] | 971 | }; |
| 972 | |
Al Viro | c531716 | 2016-10-05 18:43:43 -0400 | [diff] [blame] | 973 | static int auxv_open(struct inode *inode, struct file *file) |
| 974 | { |
| 975 | return __mem_open(inode, file, PTRACE_MODE_READ_FSCREDS); |
| 976 | } |
| 977 | |
| 978 | static ssize_t auxv_read(struct file *file, char __user *buf, |
| 979 | size_t count, loff_t *ppos) |
| 980 | { |
| 981 | struct mm_struct *mm = file->private_data; |
| 982 | unsigned int nwords = 0; |
Leon Yu | 06b2849 | 2016-10-27 17:46:50 -0700 | [diff] [blame] | 983 | |
| 984 | if (!mm) |
| 985 | return 0; |
Al Viro | c531716 | 2016-10-05 18:43:43 -0400 | [diff] [blame] | 986 | do { |
| 987 | nwords += 2; |
| 988 | } while (mm->saved_auxv[nwords - 2] != 0); /* AT_NULL */ |
| 989 | return simple_read_from_buffer(buf, count, ppos, mm->saved_auxv, |
| 990 | nwords * sizeof(mm->saved_auxv[0])); |
| 991 | } |
| 992 | |
| 993 | static const struct file_operations proc_auxv_operations = { |
| 994 | .open = auxv_open, |
| 995 | .read = auxv_read, |
| 996 | .llseek = generic_file_llseek, |
| 997 | .release = mem_release, |
| 998 | }; |
| 999 | |
David Rientjes | fa0cbbf | 2012-11-12 17:53:04 -0800 | [diff] [blame] | 1000 | static ssize_t oom_adj_read(struct file *file, char __user *buf, size_t count, |
| 1001 | loff_t *ppos) |
| 1002 | { |
Al Viro | 496ad9a | 2013-01-23 17:07:38 -0500 | [diff] [blame] | 1003 | struct task_struct *task = get_proc_task(file_inode(file)); |
David Rientjes | fa0cbbf | 2012-11-12 17:53:04 -0800 | [diff] [blame] | 1004 | char buffer[PROC_NUMBUF]; |
| 1005 | int oom_adj = OOM_ADJUST_MIN; |
| 1006 | size_t len; |
David Rientjes | fa0cbbf | 2012-11-12 17:53:04 -0800 | [diff] [blame] | 1007 | |
| 1008 | if (!task) |
| 1009 | return -ESRCH; |
Michal Hocko | f913da5 | 2016-07-28 15:44:37 -0700 | [diff] [blame] | 1010 | if (task->signal->oom_score_adj == OOM_SCORE_ADJ_MAX) |
| 1011 | oom_adj = OOM_ADJUST_MAX; |
| 1012 | else |
| 1013 | oom_adj = (task->signal->oom_score_adj * -OOM_DISABLE) / |
| 1014 | OOM_SCORE_ADJ_MAX; |
David Rientjes | fa0cbbf | 2012-11-12 17:53:04 -0800 | [diff] [blame] | 1015 | put_task_struct(task); |
| 1016 | len = snprintf(buffer, sizeof(buffer), "%d\n", oom_adj); |
| 1017 | return simple_read_from_buffer(buf, count, ppos, buffer, len); |
| 1018 | } |
| 1019 | |
Michal Hocko | 1d5f0ac | 2016-07-28 15:44:40 -0700 | [diff] [blame] | 1020 | static int __set_oom_adj(struct file *file, int oom_adj, bool legacy) |
| 1021 | { |
| 1022 | static DEFINE_MUTEX(oom_adj_mutex); |
Michal Hocko | 44a70ade | 2016-07-28 15:44:43 -0700 | [diff] [blame] | 1023 | struct mm_struct *mm = NULL; |
Michal Hocko | 1d5f0ac | 2016-07-28 15:44:40 -0700 | [diff] [blame] | 1024 | struct task_struct *task; |
| 1025 | int err = 0; |
| 1026 | |
| 1027 | task = get_proc_task(file_inode(file)); |
| 1028 | if (!task) |
| 1029 | return -ESRCH; |
| 1030 | |
| 1031 | mutex_lock(&oom_adj_mutex); |
| 1032 | if (legacy) { |
| 1033 | if (oom_adj < task->signal->oom_score_adj && |
| 1034 | !capable(CAP_SYS_RESOURCE)) { |
| 1035 | err = -EACCES; |
| 1036 | goto err_unlock; |
| 1037 | } |
| 1038 | /* |
| 1039 | * /proc/pid/oom_adj is provided for legacy purposes, ask users to use |
| 1040 | * /proc/pid/oom_score_adj instead. |
| 1041 | */ |
| 1042 | pr_warn_once("%s (%d): /proc/%d/oom_adj is deprecated, please use /proc/%d/oom_score_adj instead.\n", |
| 1043 | current->comm, task_pid_nr(current), task_pid_nr(task), |
| 1044 | task_pid_nr(task)); |
| 1045 | } else { |
| 1046 | if ((short)oom_adj < task->signal->oom_score_adj_min && |
| 1047 | !capable(CAP_SYS_RESOURCE)) { |
| 1048 | err = -EACCES; |
| 1049 | goto err_unlock; |
| 1050 | } |
| 1051 | } |
| 1052 | |
Michal Hocko | 44a70ade | 2016-07-28 15:44:43 -0700 | [diff] [blame] | 1053 | /* |
| 1054 | * Make sure we will check other processes sharing the mm if this is |
| 1055 | * not vfrok which wants its own oom_score_adj. |
| 1056 | * pin the mm so it doesn't go away and get reused after task_unlock |
| 1057 | */ |
| 1058 | if (!task->vfork_done) { |
| 1059 | struct task_struct *p = find_lock_task_mm(task); |
| 1060 | |
| 1061 | if (p) { |
| 1062 | if (atomic_read(&p->mm->mm_users) > 1) { |
| 1063 | mm = p->mm; |
Vegard Nossum | f1f1007 | 2017-02-27 14:30:07 -0800 | [diff] [blame] | 1064 | mmgrab(mm); |
Michal Hocko | 44a70ade | 2016-07-28 15:44:43 -0700 | [diff] [blame] | 1065 | } |
| 1066 | task_unlock(p); |
| 1067 | } |
| 1068 | } |
| 1069 | |
Michal Hocko | 1d5f0ac | 2016-07-28 15:44:40 -0700 | [diff] [blame] | 1070 | task->signal->oom_score_adj = oom_adj; |
| 1071 | if (!legacy && has_capability_noaudit(current, CAP_SYS_RESOURCE)) |
| 1072 | task->signal->oom_score_adj_min = (short)oom_adj; |
| 1073 | trace_oom_score_adj_update(task); |
Michal Hocko | 44a70ade | 2016-07-28 15:44:43 -0700 | [diff] [blame] | 1074 | |
| 1075 | if (mm) { |
| 1076 | struct task_struct *p; |
| 1077 | |
| 1078 | rcu_read_lock(); |
| 1079 | for_each_process(p) { |
| 1080 | if (same_thread_group(task, p)) |
| 1081 | continue; |
| 1082 | |
| 1083 | /* do not touch kernel threads or the global init */ |
| 1084 | if (p->flags & PF_KTHREAD || is_global_init(p)) |
| 1085 | continue; |
| 1086 | |
| 1087 | task_lock(p); |
| 1088 | if (!p->vfork_done && process_shares_mm(p, mm)) { |
Michal Hocko | 44a70ade | 2016-07-28 15:44:43 -0700 | [diff] [blame] | 1089 | p->signal->oom_score_adj = oom_adj; |
| 1090 | if (!legacy && has_capability_noaudit(current, CAP_SYS_RESOURCE)) |
| 1091 | p->signal->oom_score_adj_min = (short)oom_adj; |
| 1092 | } |
| 1093 | task_unlock(p); |
| 1094 | } |
| 1095 | rcu_read_unlock(); |
| 1096 | mmdrop(mm); |
| 1097 | } |
Michal Hocko | 1d5f0ac | 2016-07-28 15:44:40 -0700 | [diff] [blame] | 1098 | err_unlock: |
| 1099 | mutex_unlock(&oom_adj_mutex); |
| 1100 | put_task_struct(task); |
| 1101 | return err; |
| 1102 | } |
Michal Hocko | f913da5 | 2016-07-28 15:44:37 -0700 | [diff] [blame] | 1103 | |
David Rientjes | b72bdfa | 2015-11-05 18:50:32 -0800 | [diff] [blame] | 1104 | /* |
| 1105 | * /proc/pid/oom_adj exists solely for backwards compatibility with previous |
| 1106 | * kernels. The effective policy is defined by oom_score_adj, which has a |
| 1107 | * different scale: oom_adj grew exponentially and oom_score_adj grows linearly. |
| 1108 | * Values written to oom_adj are simply mapped linearly to oom_score_adj. |
| 1109 | * Processes that become oom disabled via oom_adj will still be oom disabled |
| 1110 | * with this implementation. |
| 1111 | * |
| 1112 | * oom_adj cannot be removed since existing userspace binaries use it. |
| 1113 | */ |
David Rientjes | fa0cbbf | 2012-11-12 17:53:04 -0800 | [diff] [blame] | 1114 | static ssize_t oom_adj_write(struct file *file, const char __user *buf, |
| 1115 | size_t count, loff_t *ppos) |
| 1116 | { |
David Rientjes | fa0cbbf | 2012-11-12 17:53:04 -0800 | [diff] [blame] | 1117 | char buffer[PROC_NUMBUF]; |
| 1118 | int oom_adj; |
David Rientjes | fa0cbbf | 2012-11-12 17:53:04 -0800 | [diff] [blame] | 1119 | int err; |
| 1120 | |
| 1121 | memset(buffer, 0, sizeof(buffer)); |
| 1122 | if (count > sizeof(buffer) - 1) |
| 1123 | count = sizeof(buffer) - 1; |
| 1124 | if (copy_from_user(buffer, buf, count)) { |
| 1125 | err = -EFAULT; |
| 1126 | goto out; |
| 1127 | } |
| 1128 | |
| 1129 | err = kstrtoint(strstrip(buffer), 0, &oom_adj); |
| 1130 | if (err) |
| 1131 | goto out; |
| 1132 | if ((oom_adj < OOM_ADJUST_MIN || oom_adj > OOM_ADJUST_MAX) && |
| 1133 | oom_adj != OOM_DISABLE) { |
| 1134 | err = -EINVAL; |
| 1135 | goto out; |
| 1136 | } |
| 1137 | |
David Rientjes | fa0cbbf | 2012-11-12 17:53:04 -0800 | [diff] [blame] | 1138 | /* |
| 1139 | * Scale /proc/pid/oom_score_adj appropriately ensuring that a maximum |
| 1140 | * value is always attainable. |
| 1141 | */ |
| 1142 | if (oom_adj == OOM_ADJUST_MAX) |
| 1143 | oom_adj = OOM_SCORE_ADJ_MAX; |
| 1144 | else |
| 1145 | oom_adj = (oom_adj * OOM_SCORE_ADJ_MAX) / -OOM_DISABLE; |
| 1146 | |
Michal Hocko | 1d5f0ac | 2016-07-28 15:44:40 -0700 | [diff] [blame] | 1147 | err = __set_oom_adj(file, oom_adj, true); |
David Rientjes | fa0cbbf | 2012-11-12 17:53:04 -0800 | [diff] [blame] | 1148 | out: |
| 1149 | return err < 0 ? err : count; |
| 1150 | } |
| 1151 | |
| 1152 | static const struct file_operations proc_oom_adj_operations = { |
| 1153 | .read = oom_adj_read, |
| 1154 | .write = oom_adj_write, |
| 1155 | .llseek = generic_file_llseek, |
| 1156 | }; |
| 1157 | |
David Rientjes | a63d83f | 2010-08-09 17:19:46 -0700 | [diff] [blame] | 1158 | static ssize_t oom_score_adj_read(struct file *file, char __user *buf, |
| 1159 | size_t count, loff_t *ppos) |
| 1160 | { |
Al Viro | 496ad9a | 2013-01-23 17:07:38 -0500 | [diff] [blame] | 1161 | struct task_struct *task = get_proc_task(file_inode(file)); |
David Rientjes | a63d83f | 2010-08-09 17:19:46 -0700 | [diff] [blame] | 1162 | char buffer[PROC_NUMBUF]; |
David Rientjes | a9c58b90 | 2012-12-11 16:02:54 -0800 | [diff] [blame] | 1163 | short oom_score_adj = OOM_SCORE_ADJ_MIN; |
David Rientjes | a63d83f | 2010-08-09 17:19:46 -0700 | [diff] [blame] | 1164 | size_t len; |
| 1165 | |
| 1166 | if (!task) |
| 1167 | return -ESRCH; |
Michal Hocko | f913da5 | 2016-07-28 15:44:37 -0700 | [diff] [blame] | 1168 | oom_score_adj = task->signal->oom_score_adj; |
David Rientjes | a63d83f | 2010-08-09 17:19:46 -0700 | [diff] [blame] | 1169 | put_task_struct(task); |
David Rientjes | a9c58b90 | 2012-12-11 16:02:54 -0800 | [diff] [blame] | 1170 | len = snprintf(buffer, sizeof(buffer), "%hd\n", oom_score_adj); |
David Rientjes | a63d83f | 2010-08-09 17:19:46 -0700 | [diff] [blame] | 1171 | return simple_read_from_buffer(buf, count, ppos, buffer, len); |
| 1172 | } |
| 1173 | |
| 1174 | static ssize_t oom_score_adj_write(struct file *file, const char __user *buf, |
| 1175 | size_t count, loff_t *ppos) |
| 1176 | { |
David Rientjes | a63d83f | 2010-08-09 17:19:46 -0700 | [diff] [blame] | 1177 | char buffer[PROC_NUMBUF]; |
Alexey Dobriyan | 0a8cb8e | 2011-05-26 16:25:50 -0700 | [diff] [blame] | 1178 | int oom_score_adj; |
David Rientjes | a63d83f | 2010-08-09 17:19:46 -0700 | [diff] [blame] | 1179 | int err; |
| 1180 | |
| 1181 | memset(buffer, 0, sizeof(buffer)); |
| 1182 | if (count > sizeof(buffer) - 1) |
| 1183 | count = sizeof(buffer) - 1; |
David Rientjes | 723548b | 2010-10-26 14:21:25 -0700 | [diff] [blame] | 1184 | if (copy_from_user(buffer, buf, count)) { |
| 1185 | err = -EFAULT; |
| 1186 | goto out; |
| 1187 | } |
David Rientjes | a63d83f | 2010-08-09 17:19:46 -0700 | [diff] [blame] | 1188 | |
Alexey Dobriyan | 0a8cb8e | 2011-05-26 16:25:50 -0700 | [diff] [blame] | 1189 | err = kstrtoint(strstrip(buffer), 0, &oom_score_adj); |
David Rientjes | a63d83f | 2010-08-09 17:19:46 -0700 | [diff] [blame] | 1190 | if (err) |
David Rientjes | 723548b | 2010-10-26 14:21:25 -0700 | [diff] [blame] | 1191 | goto out; |
David Rientjes | a63d83f | 2010-08-09 17:19:46 -0700 | [diff] [blame] | 1192 | if (oom_score_adj < OOM_SCORE_ADJ_MIN || |
David Rientjes | 723548b | 2010-10-26 14:21:25 -0700 | [diff] [blame] | 1193 | oom_score_adj > OOM_SCORE_ADJ_MAX) { |
| 1194 | err = -EINVAL; |
| 1195 | goto out; |
| 1196 | } |
David Rientjes | a63d83f | 2010-08-09 17:19:46 -0700 | [diff] [blame] | 1197 | |
Michal Hocko | 1d5f0ac | 2016-07-28 15:44:40 -0700 | [diff] [blame] | 1198 | err = __set_oom_adj(file, oom_score_adj, false); |
David Rientjes | 723548b | 2010-10-26 14:21:25 -0700 | [diff] [blame] | 1199 | out: |
| 1200 | return err < 0 ? err : count; |
David Rientjes | a63d83f | 2010-08-09 17:19:46 -0700 | [diff] [blame] | 1201 | } |
| 1202 | |
| 1203 | static const struct file_operations proc_oom_score_adj_operations = { |
| 1204 | .read = oom_score_adj_read, |
| 1205 | .write = oom_score_adj_write, |
Arnd Bergmann | 6038f37 | 2010-08-15 18:52:59 +0200 | [diff] [blame] | 1206 | .llseek = default_llseek, |
David Rientjes | a63d83f | 2010-08-09 17:19:46 -0700 | [diff] [blame] | 1207 | }; |
| 1208 | |
Richard Guy Briggs | 4b7d248 | 2019-01-22 17:06:39 -0500 | [diff] [blame] | 1209 | #ifdef CONFIG_AUDIT |
Alexey Dobriyan | b4eb4f7 | 2016-10-29 19:04:39 +0300 | [diff] [blame] | 1210 | #define TMPBUFLEN 11 |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1211 | static ssize_t proc_loginuid_read(struct file * file, char __user * buf, |
| 1212 | size_t count, loff_t *ppos) |
| 1213 | { |
Al Viro | 496ad9a | 2013-01-23 17:07:38 -0500 | [diff] [blame] | 1214 | struct inode * inode = file_inode(file); |
Eric W. Biederman | 99f8955 | 2006-06-26 00:25:55 -0700 | [diff] [blame] | 1215 | struct task_struct *task = get_proc_task(inode); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1216 | ssize_t length; |
| 1217 | char tmpbuf[TMPBUFLEN]; |
| 1218 | |
Eric W. Biederman | 99f8955 | 2006-06-26 00:25:55 -0700 | [diff] [blame] | 1219 | if (!task) |
| 1220 | return -ESRCH; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1221 | length = scnprintf(tmpbuf, TMPBUFLEN, "%u", |
Eric W. Biederman | e1760bd | 2012-09-10 22:39:43 -0700 | [diff] [blame] | 1222 | from_kuid(file->f_cred->user_ns, |
| 1223 | audit_get_loginuid(task))); |
Eric W. Biederman | 99f8955 | 2006-06-26 00:25:55 -0700 | [diff] [blame] | 1224 | put_task_struct(task); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1225 | return simple_read_from_buffer(buf, count, ppos, tmpbuf, length); |
| 1226 | } |
| 1227 | |
| 1228 | static ssize_t proc_loginuid_write(struct file * file, const char __user * buf, |
| 1229 | size_t count, loff_t *ppos) |
| 1230 | { |
Al Viro | 496ad9a | 2013-01-23 17:07:38 -0500 | [diff] [blame] | 1231 | struct inode * inode = file_inode(file); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1232 | uid_t loginuid; |
Eric W. Biederman | e1760bd | 2012-09-10 22:39:43 -0700 | [diff] [blame] | 1233 | kuid_t kloginuid; |
Alexey Dobriyan | 774636e | 2015-09-09 15:36:59 -0700 | [diff] [blame] | 1234 | int rv; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1235 | |
Paul E. McKenney | 7dc5215 | 2010-02-22 17:04:52 -0800 | [diff] [blame] | 1236 | rcu_read_lock(); |
| 1237 | if (current != pid_task(proc_pid(inode), PIDTYPE_PID)) { |
| 1238 | rcu_read_unlock(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1239 | return -EPERM; |
Paul E. McKenney | 7dc5215 | 2010-02-22 17:04:52 -0800 | [diff] [blame] | 1240 | } |
| 1241 | rcu_read_unlock(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1242 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1243 | if (*ppos != 0) { |
| 1244 | /* No partial writes. */ |
| 1245 | return -EINVAL; |
| 1246 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1247 | |
Alexey Dobriyan | 774636e | 2015-09-09 15:36:59 -0700 | [diff] [blame] | 1248 | rv = kstrtou32_from_user(buf, count, 10, &loginuid); |
| 1249 | if (rv < 0) |
| 1250 | return rv; |
Eric Paris | 81407c8 | 2013-05-24 09:49:14 -0400 | [diff] [blame] | 1251 | |
| 1252 | /* is userspace tring to explicitly UNSET the loginuid? */ |
| 1253 | if (loginuid == AUDIT_UID_UNSET) { |
| 1254 | kloginuid = INVALID_UID; |
| 1255 | } else { |
| 1256 | kloginuid = make_kuid(file->f_cred->user_ns, loginuid); |
Alexey Dobriyan | 774636e | 2015-09-09 15:36:59 -0700 | [diff] [blame] | 1257 | if (!uid_valid(kloginuid)) |
| 1258 | return -EINVAL; |
Eric W. Biederman | e1760bd | 2012-09-10 22:39:43 -0700 | [diff] [blame] | 1259 | } |
| 1260 | |
Alexey Dobriyan | 774636e | 2015-09-09 15:36:59 -0700 | [diff] [blame] | 1261 | rv = audit_set_loginuid(kloginuid); |
| 1262 | if (rv < 0) |
| 1263 | return rv; |
| 1264 | return count; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1265 | } |
| 1266 | |
Arjan van de Ven | 00977a5 | 2007-02-12 00:55:34 -0800 | [diff] [blame] | 1267 | static const struct file_operations proc_loginuid_operations = { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1268 | .read = proc_loginuid_read, |
| 1269 | .write = proc_loginuid_write, |
Arnd Bergmann | 87df842 | 2010-03-17 23:06:02 +0100 | [diff] [blame] | 1270 | .llseek = generic_file_llseek, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1271 | }; |
Eric Paris | 1e0bd75 | 2008-03-13 08:15:31 -0400 | [diff] [blame] | 1272 | |
| 1273 | static ssize_t proc_sessionid_read(struct file * file, char __user * buf, |
| 1274 | size_t count, loff_t *ppos) |
| 1275 | { |
Al Viro | 496ad9a | 2013-01-23 17:07:38 -0500 | [diff] [blame] | 1276 | struct inode * inode = file_inode(file); |
Eric Paris | 1e0bd75 | 2008-03-13 08:15:31 -0400 | [diff] [blame] | 1277 | struct task_struct *task = get_proc_task(inode); |
| 1278 | ssize_t length; |
| 1279 | char tmpbuf[TMPBUFLEN]; |
| 1280 | |
| 1281 | if (!task) |
| 1282 | return -ESRCH; |
| 1283 | length = scnprintf(tmpbuf, TMPBUFLEN, "%u", |
| 1284 | audit_get_sessionid(task)); |
| 1285 | put_task_struct(task); |
| 1286 | return simple_read_from_buffer(buf, count, ppos, tmpbuf, length); |
| 1287 | } |
| 1288 | |
| 1289 | static const struct file_operations proc_sessionid_operations = { |
| 1290 | .read = proc_sessionid_read, |
Arnd Bergmann | 87df842 | 2010-03-17 23:06:02 +0100 | [diff] [blame] | 1291 | .llseek = generic_file_llseek, |
Eric Paris | 1e0bd75 | 2008-03-13 08:15:31 -0400 | [diff] [blame] | 1292 | }; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1293 | #endif |
| 1294 | |
Akinobu Mita | f4f154f | 2006-12-08 02:39:47 -0800 | [diff] [blame] | 1295 | #ifdef CONFIG_FAULT_INJECTION |
| 1296 | static ssize_t proc_fault_inject_read(struct file * file, char __user * buf, |
| 1297 | size_t count, loff_t *ppos) |
| 1298 | { |
Al Viro | 496ad9a | 2013-01-23 17:07:38 -0500 | [diff] [blame] | 1299 | struct task_struct *task = get_proc_task(file_inode(file)); |
Akinobu Mita | f4f154f | 2006-12-08 02:39:47 -0800 | [diff] [blame] | 1300 | char buffer[PROC_NUMBUF]; |
| 1301 | size_t len; |
| 1302 | int make_it_fail; |
Akinobu Mita | f4f154f | 2006-12-08 02:39:47 -0800 | [diff] [blame] | 1303 | |
| 1304 | if (!task) |
| 1305 | return -ESRCH; |
| 1306 | make_it_fail = task->make_it_fail; |
| 1307 | put_task_struct(task); |
| 1308 | |
| 1309 | len = snprintf(buffer, sizeof(buffer), "%i\n", make_it_fail); |
Akinobu Mita | 0c28f28 | 2007-05-08 00:31:41 -0700 | [diff] [blame] | 1310 | |
| 1311 | return simple_read_from_buffer(buf, count, ppos, buffer, len); |
Akinobu Mita | f4f154f | 2006-12-08 02:39:47 -0800 | [diff] [blame] | 1312 | } |
| 1313 | |
| 1314 | static ssize_t proc_fault_inject_write(struct file * file, |
| 1315 | const char __user * buf, size_t count, loff_t *ppos) |
| 1316 | { |
| 1317 | struct task_struct *task; |
Alexey Dobriyan | 774636e | 2015-09-09 15:36:59 -0700 | [diff] [blame] | 1318 | char buffer[PROC_NUMBUF]; |
Akinobu Mita | f4f154f | 2006-12-08 02:39:47 -0800 | [diff] [blame] | 1319 | int make_it_fail; |
Alexey Dobriyan | 774636e | 2015-09-09 15:36:59 -0700 | [diff] [blame] | 1320 | int rv; |
Akinobu Mita | f4f154f | 2006-12-08 02:39:47 -0800 | [diff] [blame] | 1321 | |
| 1322 | if (!capable(CAP_SYS_RESOURCE)) |
| 1323 | return -EPERM; |
| 1324 | memset(buffer, 0, sizeof(buffer)); |
| 1325 | if (count > sizeof(buffer) - 1) |
| 1326 | count = sizeof(buffer) - 1; |
| 1327 | if (copy_from_user(buffer, buf, count)) |
| 1328 | return -EFAULT; |
Alexey Dobriyan | 774636e | 2015-09-09 15:36:59 -0700 | [diff] [blame] | 1329 | rv = kstrtoint(strstrip(buffer), 0, &make_it_fail); |
| 1330 | if (rv < 0) |
| 1331 | return rv; |
Dave Jones | 16caed3 | 2014-04-07 15:39:15 -0700 | [diff] [blame] | 1332 | if (make_it_fail < 0 || make_it_fail > 1) |
| 1333 | return -EINVAL; |
| 1334 | |
Al Viro | 496ad9a | 2013-01-23 17:07:38 -0500 | [diff] [blame] | 1335 | task = get_proc_task(file_inode(file)); |
Akinobu Mita | f4f154f | 2006-12-08 02:39:47 -0800 | [diff] [blame] | 1336 | if (!task) |
| 1337 | return -ESRCH; |
| 1338 | task->make_it_fail = make_it_fail; |
| 1339 | put_task_struct(task); |
Vincent Li | cba8aaf | 2009-09-22 16:45:38 -0700 | [diff] [blame] | 1340 | |
| 1341 | return count; |
Akinobu Mita | f4f154f | 2006-12-08 02:39:47 -0800 | [diff] [blame] | 1342 | } |
| 1343 | |
Arjan van de Ven | 00977a5 | 2007-02-12 00:55:34 -0800 | [diff] [blame] | 1344 | static const struct file_operations proc_fault_inject_operations = { |
Akinobu Mita | f4f154f | 2006-12-08 02:39:47 -0800 | [diff] [blame] | 1345 | .read = proc_fault_inject_read, |
| 1346 | .write = proc_fault_inject_write, |
Arnd Bergmann | 87df842 | 2010-03-17 23:06:02 +0100 | [diff] [blame] | 1347 | .llseek = generic_file_llseek, |
Akinobu Mita | f4f154f | 2006-12-08 02:39:47 -0800 | [diff] [blame] | 1348 | }; |
Dmitry Vyukov | e41d5818 | 2017-07-12 14:34:35 -0700 | [diff] [blame] | 1349 | |
| 1350 | static ssize_t proc_fail_nth_write(struct file *file, const char __user *buf, |
| 1351 | size_t count, loff_t *ppos) |
| 1352 | { |
| 1353 | struct task_struct *task; |
Akinobu Mita | 9049f2f | 2017-07-14 14:49:52 -0700 | [diff] [blame] | 1354 | int err; |
| 1355 | unsigned int n; |
Dmitry Vyukov | e41d5818 | 2017-07-12 14:34:35 -0700 | [diff] [blame] | 1356 | |
Akinobu Mita | 9049f2f | 2017-07-14 14:49:52 -0700 | [diff] [blame] | 1357 | err = kstrtouint_from_user(buf, count, 0, &n); |
Dmitry Vyukov | e41d5818 | 2017-07-12 14:34:35 -0700 | [diff] [blame] | 1358 | if (err) |
| 1359 | return err; |
Akinobu Mita | 1203c8e | 2017-07-14 14:49:57 -0700 | [diff] [blame] | 1360 | |
| 1361 | task = get_proc_task(file_inode(file)); |
| 1362 | if (!task) |
| 1363 | return -ESRCH; |
Alexey Dobriyan | 9f7118b | 2018-02-06 15:36:55 -0800 | [diff] [blame] | 1364 | task->fail_nth = n; |
Akinobu Mita | 1203c8e | 2017-07-14 14:49:57 -0700 | [diff] [blame] | 1365 | put_task_struct(task); |
| 1366 | |
Dmitry Vyukov | e41d5818 | 2017-07-12 14:34:35 -0700 | [diff] [blame] | 1367 | return count; |
| 1368 | } |
| 1369 | |
| 1370 | static ssize_t proc_fail_nth_read(struct file *file, char __user *buf, |
| 1371 | size_t count, loff_t *ppos) |
| 1372 | { |
| 1373 | struct task_struct *task; |
Akinobu Mita | bfc7409 | 2017-07-14 14:49:54 -0700 | [diff] [blame] | 1374 | char numbuf[PROC_NUMBUF]; |
| 1375 | ssize_t len; |
Dmitry Vyukov | e41d5818 | 2017-07-12 14:34:35 -0700 | [diff] [blame] | 1376 | |
| 1377 | task = get_proc_task(file_inode(file)); |
| 1378 | if (!task) |
| 1379 | return -ESRCH; |
Alexey Dobriyan | 9f7118b | 2018-02-06 15:36:55 -0800 | [diff] [blame] | 1380 | len = snprintf(numbuf, sizeof(numbuf), "%u\n", task->fail_nth); |
Akinobu Mita | 1203c8e | 2017-07-14 14:49:57 -0700 | [diff] [blame] | 1381 | put_task_struct(task); |
Alexey Dobriyan | a44937f | 2018-08-21 21:54:27 -0700 | [diff] [blame] | 1382 | return simple_read_from_buffer(buf, count, ppos, numbuf, len); |
Dmitry Vyukov | e41d5818 | 2017-07-12 14:34:35 -0700 | [diff] [blame] | 1383 | } |
| 1384 | |
| 1385 | static const struct file_operations proc_fail_nth_operations = { |
| 1386 | .read = proc_fail_nth_read, |
| 1387 | .write = proc_fail_nth_write, |
| 1388 | }; |
Akinobu Mita | f4f154f | 2006-12-08 02:39:47 -0800 | [diff] [blame] | 1389 | #endif |
| 1390 | |
Arjan van de Ven | 9745512 | 2008-01-25 21:08:34 +0100 | [diff] [blame] | 1391 | |
Ingo Molnar | 43ae34c | 2007-07-09 18:52:00 +0200 | [diff] [blame] | 1392 | #ifdef CONFIG_SCHED_DEBUG |
| 1393 | /* |
| 1394 | * Print out various scheduling related per-task fields: |
| 1395 | */ |
| 1396 | static int sched_show(struct seq_file *m, void *v) |
| 1397 | { |
| 1398 | struct inode *inode = m->private; |
Christoph Hellwig | 76f668b | 2018-05-16 07:19:01 +0200 | [diff] [blame] | 1399 | struct pid_namespace *ns = proc_pid_ns(inode); |
Ingo Molnar | 43ae34c | 2007-07-09 18:52:00 +0200 | [diff] [blame] | 1400 | struct task_struct *p; |
| 1401 | |
Ingo Molnar | 43ae34c | 2007-07-09 18:52:00 +0200 | [diff] [blame] | 1402 | p = get_proc_task(inode); |
| 1403 | if (!p) |
| 1404 | return -ESRCH; |
Aleksa Sarai | 74dc338 | 2017-08-06 14:41:41 +1000 | [diff] [blame] | 1405 | proc_sched_show_task(p, ns, m); |
Ingo Molnar | 43ae34c | 2007-07-09 18:52:00 +0200 | [diff] [blame] | 1406 | |
| 1407 | put_task_struct(p); |
| 1408 | |
| 1409 | return 0; |
| 1410 | } |
| 1411 | |
| 1412 | static ssize_t |
| 1413 | sched_write(struct file *file, const char __user *buf, |
| 1414 | size_t count, loff_t *offset) |
| 1415 | { |
Al Viro | 496ad9a | 2013-01-23 17:07:38 -0500 | [diff] [blame] | 1416 | struct inode *inode = file_inode(file); |
Ingo Molnar | 43ae34c | 2007-07-09 18:52:00 +0200 | [diff] [blame] | 1417 | struct task_struct *p; |
| 1418 | |
Ingo Molnar | 43ae34c | 2007-07-09 18:52:00 +0200 | [diff] [blame] | 1419 | p = get_proc_task(inode); |
| 1420 | if (!p) |
| 1421 | return -ESRCH; |
| 1422 | proc_sched_set_task(p); |
| 1423 | |
| 1424 | put_task_struct(p); |
| 1425 | |
| 1426 | return count; |
| 1427 | } |
| 1428 | |
| 1429 | static int sched_open(struct inode *inode, struct file *filp) |
| 1430 | { |
Jovi Zhang | c6a3405 | 2011-01-12 17:00:34 -0800 | [diff] [blame] | 1431 | return single_open(filp, sched_show, inode); |
Ingo Molnar | 43ae34c | 2007-07-09 18:52:00 +0200 | [diff] [blame] | 1432 | } |
| 1433 | |
| 1434 | static const struct file_operations proc_pid_sched_operations = { |
| 1435 | .open = sched_open, |
| 1436 | .read = seq_read, |
| 1437 | .write = sched_write, |
| 1438 | .llseek = seq_lseek, |
Alexey Dobriyan | 5ea473a | 2007-07-31 00:38:50 -0700 | [diff] [blame] | 1439 | .release = single_release, |
Ingo Molnar | 43ae34c | 2007-07-09 18:52:00 +0200 | [diff] [blame] | 1440 | }; |
| 1441 | |
| 1442 | #endif |
| 1443 | |
Mike Galbraith | 5091faa | 2010-11-30 14:18:03 +0100 | [diff] [blame] | 1444 | #ifdef CONFIG_SCHED_AUTOGROUP |
| 1445 | /* |
| 1446 | * Print out autogroup related information: |
| 1447 | */ |
| 1448 | static int sched_autogroup_show(struct seq_file *m, void *v) |
| 1449 | { |
| 1450 | struct inode *inode = m->private; |
| 1451 | struct task_struct *p; |
| 1452 | |
| 1453 | p = get_proc_task(inode); |
| 1454 | if (!p) |
| 1455 | return -ESRCH; |
| 1456 | proc_sched_autogroup_show_task(p, m); |
| 1457 | |
| 1458 | put_task_struct(p); |
| 1459 | |
| 1460 | return 0; |
| 1461 | } |
| 1462 | |
| 1463 | static ssize_t |
| 1464 | sched_autogroup_write(struct file *file, const char __user *buf, |
| 1465 | size_t count, loff_t *offset) |
| 1466 | { |
Al Viro | 496ad9a | 2013-01-23 17:07:38 -0500 | [diff] [blame] | 1467 | struct inode *inode = file_inode(file); |
Mike Galbraith | 5091faa | 2010-11-30 14:18:03 +0100 | [diff] [blame] | 1468 | struct task_struct *p; |
| 1469 | char buffer[PROC_NUMBUF]; |
Alexey Dobriyan | 0a8cb8e | 2011-05-26 16:25:50 -0700 | [diff] [blame] | 1470 | int nice; |
Mike Galbraith | 5091faa | 2010-11-30 14:18:03 +0100 | [diff] [blame] | 1471 | int err; |
| 1472 | |
| 1473 | memset(buffer, 0, sizeof(buffer)); |
| 1474 | if (count > sizeof(buffer) - 1) |
| 1475 | count = sizeof(buffer) - 1; |
| 1476 | if (copy_from_user(buffer, buf, count)) |
| 1477 | return -EFAULT; |
| 1478 | |
Alexey Dobriyan | 0a8cb8e | 2011-05-26 16:25:50 -0700 | [diff] [blame] | 1479 | err = kstrtoint(strstrip(buffer), 0, &nice); |
| 1480 | if (err < 0) |
| 1481 | return err; |
Mike Galbraith | 5091faa | 2010-11-30 14:18:03 +0100 | [diff] [blame] | 1482 | |
| 1483 | p = get_proc_task(inode); |
| 1484 | if (!p) |
| 1485 | return -ESRCH; |
| 1486 | |
Hiroshi Shimamoto | 2e5b5b3 | 2012-02-23 17:41:27 +0900 | [diff] [blame] | 1487 | err = proc_sched_autogroup_set_nice(p, nice); |
Mike Galbraith | 5091faa | 2010-11-30 14:18:03 +0100 | [diff] [blame] | 1488 | if (err) |
| 1489 | count = err; |
| 1490 | |
| 1491 | put_task_struct(p); |
| 1492 | |
| 1493 | return count; |
| 1494 | } |
| 1495 | |
| 1496 | static int sched_autogroup_open(struct inode *inode, struct file *filp) |
| 1497 | { |
| 1498 | int ret; |
| 1499 | |
| 1500 | ret = single_open(filp, sched_autogroup_show, NULL); |
| 1501 | if (!ret) { |
| 1502 | struct seq_file *m = filp->private_data; |
| 1503 | |
| 1504 | m->private = inode; |
| 1505 | } |
| 1506 | return ret; |
| 1507 | } |
| 1508 | |
| 1509 | static const struct file_operations proc_pid_sched_autogroup_operations = { |
| 1510 | .open = sched_autogroup_open, |
| 1511 | .read = seq_read, |
| 1512 | .write = sched_autogroup_write, |
| 1513 | .llseek = seq_lseek, |
| 1514 | .release = single_release, |
| 1515 | }; |
| 1516 | |
| 1517 | #endif /* CONFIG_SCHED_AUTOGROUP */ |
| 1518 | |
john stultz | 4614a696b | 2009-12-14 18:00:05 -0800 | [diff] [blame] | 1519 | static ssize_t comm_write(struct file *file, const char __user *buf, |
| 1520 | size_t count, loff_t *offset) |
| 1521 | { |
Al Viro | 496ad9a | 2013-01-23 17:07:38 -0500 | [diff] [blame] | 1522 | struct inode *inode = file_inode(file); |
john stultz | 4614a696b | 2009-12-14 18:00:05 -0800 | [diff] [blame] | 1523 | struct task_struct *p; |
| 1524 | char buffer[TASK_COMM_LEN]; |
David Rientjes | 830e0fc | 2013-04-30 15:28:18 -0700 | [diff] [blame] | 1525 | const size_t maxlen = sizeof(buffer) - 1; |
john stultz | 4614a696b | 2009-12-14 18:00:05 -0800 | [diff] [blame] | 1526 | |
| 1527 | memset(buffer, 0, sizeof(buffer)); |
David Rientjes | 830e0fc | 2013-04-30 15:28:18 -0700 | [diff] [blame] | 1528 | if (copy_from_user(buffer, buf, count > maxlen ? maxlen : count)) |
john stultz | 4614a696b | 2009-12-14 18:00:05 -0800 | [diff] [blame] | 1529 | return -EFAULT; |
| 1530 | |
| 1531 | p = get_proc_task(inode); |
| 1532 | if (!p) |
| 1533 | return -ESRCH; |
| 1534 | |
| 1535 | if (same_thread_group(current, p)) |
| 1536 | set_task_comm(p, buffer); |
| 1537 | else |
| 1538 | count = -EINVAL; |
| 1539 | |
| 1540 | put_task_struct(p); |
| 1541 | |
| 1542 | return count; |
| 1543 | } |
| 1544 | |
| 1545 | static int comm_show(struct seq_file *m, void *v) |
| 1546 | { |
| 1547 | struct inode *inode = m->private; |
| 1548 | struct task_struct *p; |
| 1549 | |
| 1550 | p = get_proc_task(inode); |
| 1551 | if (!p) |
| 1552 | return -ESRCH; |
| 1553 | |
Tejun Heo | 88b72b3 | 2018-05-18 08:47:13 -0700 | [diff] [blame] | 1554 | proc_task_name(m, p, false); |
| 1555 | seq_putc(m, '\n'); |
john stultz | 4614a696b | 2009-12-14 18:00:05 -0800 | [diff] [blame] | 1556 | |
| 1557 | put_task_struct(p); |
| 1558 | |
| 1559 | return 0; |
| 1560 | } |
| 1561 | |
| 1562 | static int comm_open(struct inode *inode, struct file *filp) |
| 1563 | { |
Jovi Zhang | c6a3405 | 2011-01-12 17:00:34 -0800 | [diff] [blame] | 1564 | return single_open(filp, comm_show, inode); |
john stultz | 4614a696b | 2009-12-14 18:00:05 -0800 | [diff] [blame] | 1565 | } |
| 1566 | |
| 1567 | static const struct file_operations proc_pid_set_comm_operations = { |
| 1568 | .open = comm_open, |
| 1569 | .read = seq_read, |
| 1570 | .write = comm_write, |
| 1571 | .llseek = seq_lseek, |
| 1572 | .release = single_release, |
| 1573 | }; |
| 1574 | |
Cyrill Gorcunov | 7773fbc | 2012-01-10 15:11:20 -0800 | [diff] [blame] | 1575 | static int proc_exe_link(struct dentry *dentry, struct path *exe_path) |
Matt Helsley | 925d1c4 | 2008-04-29 01:01:36 -0700 | [diff] [blame] | 1576 | { |
| 1577 | struct task_struct *task; |
Matt Helsley | 925d1c4 | 2008-04-29 01:01:36 -0700 | [diff] [blame] | 1578 | struct file *exe_file; |
| 1579 | |
David Howells | 2b0143b | 2015-03-17 22:25:59 +0000 | [diff] [blame] | 1580 | task = get_proc_task(d_inode(dentry)); |
Matt Helsley | 925d1c4 | 2008-04-29 01:01:36 -0700 | [diff] [blame] | 1581 | if (!task) |
| 1582 | return -ENOENT; |
Mateusz Guzik | cd81a917 | 2016-08-23 16:20:38 +0200 | [diff] [blame] | 1583 | exe_file = get_task_exe_file(task); |
Matt Helsley | 925d1c4 | 2008-04-29 01:01:36 -0700 | [diff] [blame] | 1584 | put_task_struct(task); |
Matt Helsley | 925d1c4 | 2008-04-29 01:01:36 -0700 | [diff] [blame] | 1585 | if (exe_file) { |
| 1586 | *exe_path = exe_file->f_path; |
| 1587 | path_get(&exe_file->f_path); |
| 1588 | fput(exe_file); |
| 1589 | return 0; |
| 1590 | } else |
| 1591 | return -ENOENT; |
| 1592 | } |
| 1593 | |
Al Viro | 6b25539 | 2015-11-17 10:20:54 -0500 | [diff] [blame] | 1594 | static const char *proc_pid_get_link(struct dentry *dentry, |
Al Viro | fceef39 | 2015-12-29 15:58:39 -0500 | [diff] [blame] | 1595 | struct inode *inode, |
| 1596 | struct delayed_call *done) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1597 | { |
Christoph Hellwig | 408ef01 | 2012-06-18 10:47:03 -0400 | [diff] [blame] | 1598 | struct path path; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1599 | int error = -EACCES; |
| 1600 | |
Al Viro | 6b25539 | 2015-11-17 10:20:54 -0500 | [diff] [blame] | 1601 | if (!dentry) |
| 1602 | return ERR_PTR(-ECHILD); |
| 1603 | |
Eric W. Biederman | 778c114 | 2006-06-26 00:25:58 -0700 | [diff] [blame] | 1604 | /* Are we allowed to snoop on the tasks file descriptors? */ |
| 1605 | if (!proc_fd_access_allowed(inode)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1606 | goto out; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1607 | |
Christoph Hellwig | 408ef01 | 2012-06-18 10:47:03 -0400 | [diff] [blame] | 1608 | error = PROC_I(inode)->op.proc_get_link(dentry, &path); |
| 1609 | if (error) |
| 1610 | goto out; |
| 1611 | |
Al Viro | 6e77137b | 2015-05-02 13:37:52 -0400 | [diff] [blame] | 1612 | nd_jump_link(&path); |
Christoph Hellwig | 408ef01 | 2012-06-18 10:47:03 -0400 | [diff] [blame] | 1613 | return NULL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1614 | out: |
Al Viro | 008b150 | 2005-08-20 00:17:39 +0100 | [diff] [blame] | 1615 | return ERR_PTR(error); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1616 | } |
| 1617 | |
Jan Blunck | 3dcd25f | 2008-02-14 19:38:35 -0800 | [diff] [blame] | 1618 | static int do_proc_readlink(struct path *path, char __user *buffer, int buflen) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1619 | { |
Michal Hocko | 0ee931c | 2017-09-13 16:28:29 -0700 | [diff] [blame] | 1620 | char *tmp = (char *)__get_free_page(GFP_KERNEL); |
Jan Blunck | 3dcd25f | 2008-02-14 19:38:35 -0800 | [diff] [blame] | 1621 | char *pathname; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1622 | int len; |
| 1623 | |
| 1624 | if (!tmp) |
| 1625 | return -ENOMEM; |
Akinobu Mita | 0c28f28 | 2007-05-08 00:31:41 -0700 | [diff] [blame] | 1626 | |
Eric W. Biederman | 7b2a69b | 2010-12-05 15:51:21 -0800 | [diff] [blame] | 1627 | pathname = d_path(path, tmp, PAGE_SIZE); |
Jan Blunck | 3dcd25f | 2008-02-14 19:38:35 -0800 | [diff] [blame] | 1628 | len = PTR_ERR(pathname); |
| 1629 | if (IS_ERR(pathname)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1630 | goto out; |
Jan Blunck | 3dcd25f | 2008-02-14 19:38:35 -0800 | [diff] [blame] | 1631 | len = tmp + PAGE_SIZE - 1 - pathname; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1632 | |
| 1633 | if (len > buflen) |
| 1634 | len = buflen; |
Jan Blunck | 3dcd25f | 2008-02-14 19:38:35 -0800 | [diff] [blame] | 1635 | if (copy_to_user(buffer, pathname, len)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1636 | len = -EFAULT; |
| 1637 | out: |
| 1638 | free_page((unsigned long)tmp); |
| 1639 | return len; |
| 1640 | } |
| 1641 | |
| 1642 | static int proc_pid_readlink(struct dentry * dentry, char __user * buffer, int buflen) |
| 1643 | { |
| 1644 | int error = -EACCES; |
David Howells | 2b0143b | 2015-03-17 22:25:59 +0000 | [diff] [blame] | 1645 | struct inode *inode = d_inode(dentry); |
Jan Blunck | 3dcd25f | 2008-02-14 19:38:35 -0800 | [diff] [blame] | 1646 | struct path path; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1647 | |
Eric W. Biederman | 778c114 | 2006-06-26 00:25:58 -0700 | [diff] [blame] | 1648 | /* Are we allowed to snoop on the tasks file descriptors? */ |
| 1649 | if (!proc_fd_access_allowed(inode)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1650 | goto out; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1651 | |
Cyrill Gorcunov | 7773fbc | 2012-01-10 15:11:20 -0800 | [diff] [blame] | 1652 | error = PROC_I(inode)->op.proc_get_link(dentry, &path); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1653 | if (error) |
| 1654 | goto out; |
| 1655 | |
Jan Blunck | 3dcd25f | 2008-02-14 19:38:35 -0800 | [diff] [blame] | 1656 | error = do_proc_readlink(&path, buffer, buflen); |
| 1657 | path_put(&path); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1658 | out: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1659 | return error; |
| 1660 | } |
| 1661 | |
Cyrill Gorcunov | faf60af | 2012-08-23 14:43:24 +0400 | [diff] [blame] | 1662 | const struct inode_operations proc_pid_link_inode_operations = { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1663 | .readlink = proc_pid_readlink, |
Al Viro | 6b25539 | 2015-11-17 10:20:54 -0500 | [diff] [blame] | 1664 | .get_link = proc_pid_get_link, |
Linus Torvalds | 6d76fa5 | 2006-07-15 12:26:45 -0700 | [diff] [blame] | 1665 | .setattr = proc_setattr, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1666 | }; |
| 1667 | |
Eric W. Biederman | 28a6d67 | 2006-10-02 02:17:05 -0700 | [diff] [blame] | 1668 | |
| 1669 | /* building an inode */ |
| 1670 | |
Al Viro | c6eb50d | 2017-09-30 14:45:42 -0400 | [diff] [blame] | 1671 | void task_dump_owner(struct task_struct *task, umode_t mode, |
Eric W. Biederman | 68eb94f | 2017-01-03 10:23:11 +1300 | [diff] [blame] | 1672 | kuid_t *ruid, kgid_t *rgid) |
| 1673 | { |
| 1674 | /* Depending on the state of dumpable compute who should own a |
| 1675 | * proc file for a task. |
| 1676 | */ |
| 1677 | const struct cred *cred; |
| 1678 | kuid_t uid; |
| 1679 | kgid_t gid; |
| 1680 | |
Alexey Dobriyan | 2e0ad55 | 2018-04-20 14:56:03 -0700 | [diff] [blame] | 1681 | if (unlikely(task->flags & PF_KTHREAD)) { |
| 1682 | *ruid = GLOBAL_ROOT_UID; |
| 1683 | *rgid = GLOBAL_ROOT_GID; |
| 1684 | return; |
| 1685 | } |
| 1686 | |
Eric W. Biederman | 68eb94f | 2017-01-03 10:23:11 +1300 | [diff] [blame] | 1687 | /* Default to the tasks effective ownership */ |
| 1688 | rcu_read_lock(); |
| 1689 | cred = __task_cred(task); |
| 1690 | uid = cred->euid; |
| 1691 | gid = cred->egid; |
| 1692 | rcu_read_unlock(); |
| 1693 | |
| 1694 | /* |
| 1695 | * Before the /proc/pid/status file was created the only way to read |
| 1696 | * the effective uid of a /process was to stat /proc/pid. Reading |
| 1697 | * /proc/pid/status is slow enough that procps and other packages |
| 1698 | * kept stating /proc/pid. To keep the rules in /proc simple I have |
| 1699 | * made this apply to all per process world readable and executable |
| 1700 | * directories. |
| 1701 | */ |
| 1702 | if (mode != (S_IFDIR|S_IRUGO|S_IXUGO)) { |
| 1703 | struct mm_struct *mm; |
| 1704 | task_lock(task); |
| 1705 | mm = task->mm; |
| 1706 | /* Make non-dumpable tasks owned by some root */ |
| 1707 | if (mm) { |
| 1708 | if (get_dumpable(mm) != SUID_DUMP_USER) { |
| 1709 | struct user_namespace *user_ns = mm->user_ns; |
| 1710 | |
| 1711 | uid = make_kuid(user_ns, 0); |
| 1712 | if (!uid_valid(uid)) |
| 1713 | uid = GLOBAL_ROOT_UID; |
| 1714 | |
| 1715 | gid = make_kgid(user_ns, 0); |
| 1716 | if (!gid_valid(gid)) |
| 1717 | gid = GLOBAL_ROOT_GID; |
| 1718 | } |
| 1719 | } else { |
| 1720 | uid = GLOBAL_ROOT_UID; |
| 1721 | gid = GLOBAL_ROOT_GID; |
| 1722 | } |
| 1723 | task_unlock(task); |
| 1724 | } |
| 1725 | *ruid = uid; |
| 1726 | *rgid = gid; |
| 1727 | } |
| 1728 | |
Andreas Gruenbacher | db978da | 2016-11-10 22:18:28 +0100 | [diff] [blame] | 1729 | struct inode *proc_pid_make_inode(struct super_block * sb, |
| 1730 | struct task_struct *task, umode_t mode) |
Eric W. Biederman | 28a6d67 | 2006-10-02 02:17:05 -0700 | [diff] [blame] | 1731 | { |
| 1732 | struct inode * inode; |
| 1733 | struct proc_inode *ei; |
| 1734 | |
| 1735 | /* We need a new inode */ |
| 1736 | |
| 1737 | inode = new_inode(sb); |
| 1738 | if (!inode) |
| 1739 | goto out; |
| 1740 | |
| 1741 | /* Common stuff */ |
| 1742 | ei = PROC_I(inode); |
Andreas Gruenbacher | db978da | 2016-11-10 22:18:28 +0100 | [diff] [blame] | 1743 | inode->i_mode = mode; |
Christoph Hellwig | 85fe402 | 2010-10-23 11:19:54 -0400 | [diff] [blame] | 1744 | inode->i_ino = get_next_ino(); |
Deepa Dinamani | 078cd82 | 2016-09-14 07:48:04 -0700 | [diff] [blame] | 1745 | inode->i_mtime = inode->i_atime = inode->i_ctime = current_time(inode); |
Eric W. Biederman | 28a6d67 | 2006-10-02 02:17:05 -0700 | [diff] [blame] | 1746 | inode->i_op = &proc_def_inode_operations; |
| 1747 | |
| 1748 | /* |
| 1749 | * grab the reference to task. |
| 1750 | */ |
Oleg Nesterov | 1a657f78 | 2006-10-02 02:18:59 -0700 | [diff] [blame] | 1751 | ei->pid = get_task_pid(task, PIDTYPE_PID); |
Eric W. Biederman | 28a6d67 | 2006-10-02 02:17:05 -0700 | [diff] [blame] | 1752 | if (!ei->pid) |
| 1753 | goto out_unlock; |
| 1754 | |
Eric W. Biederman | 68eb94f | 2017-01-03 10:23:11 +1300 | [diff] [blame] | 1755 | task_dump_owner(task, 0, &inode->i_uid, &inode->i_gid); |
Eric W. Biederman | 28a6d67 | 2006-10-02 02:17:05 -0700 | [diff] [blame] | 1756 | security_task_to_inode(task, inode); |
| 1757 | |
| 1758 | out: |
| 1759 | return inode; |
| 1760 | |
| 1761 | out_unlock: |
| 1762 | iput(inode); |
| 1763 | return NULL; |
| 1764 | } |
| 1765 | |
David Howells | a528d35 | 2017-01-31 16:46:22 +0000 | [diff] [blame] | 1766 | int pid_getattr(const struct path *path, struct kstat *stat, |
| 1767 | u32 request_mask, unsigned int query_flags) |
Eric W. Biederman | 28a6d67 | 2006-10-02 02:17:05 -0700 | [diff] [blame] | 1768 | { |
David Howells | a528d35 | 2017-01-31 16:46:22 +0000 | [diff] [blame] | 1769 | struct inode *inode = d_inode(path->dentry); |
Christoph Hellwig | 76f668b | 2018-05-16 07:19:01 +0200 | [diff] [blame] | 1770 | struct pid_namespace *pid = proc_pid_ns(inode); |
Eric W. Biederman | 28a6d67 | 2006-10-02 02:17:05 -0700 | [diff] [blame] | 1771 | struct task_struct *task; |
David Howells | c69e8d9 | 2008-11-14 10:39:19 +1100 | [diff] [blame] | 1772 | |
Eric W. Biederman | 28a6d67 | 2006-10-02 02:17:05 -0700 | [diff] [blame] | 1773 | generic_fillattr(inode, stat); |
| 1774 | |
Eric W. Biederman | dcb0f22 | 2012-02-09 08:48:21 -0800 | [diff] [blame] | 1775 | stat->uid = GLOBAL_ROOT_UID; |
| 1776 | stat->gid = GLOBAL_ROOT_GID; |
Alexey Dobriyan | 9411692 | 2018-06-07 17:10:07 -0700 | [diff] [blame] | 1777 | rcu_read_lock(); |
Eric W. Biederman | 28a6d67 | 2006-10-02 02:17:05 -0700 | [diff] [blame] | 1778 | task = pid_task(proc_pid(inode), PIDTYPE_PID); |
| 1779 | if (task) { |
Lafcadio Wluiki | 796f571 | 2017-02-24 15:00:23 -0800 | [diff] [blame] | 1780 | if (!has_pid_permissions(pid, task, HIDEPID_INVISIBLE)) { |
Vasiliy Kulikov | 0499680 | 2012-01-10 15:11:31 -0800 | [diff] [blame] | 1781 | rcu_read_unlock(); |
| 1782 | /* |
| 1783 | * This doesn't prevent learning whether PID exists, |
| 1784 | * it only makes getattr() consistent with readdir(). |
| 1785 | */ |
| 1786 | return -ENOENT; |
| 1787 | } |
Eric W. Biederman | 68eb94f | 2017-01-03 10:23:11 +1300 | [diff] [blame] | 1788 | task_dump_owner(task, inode->i_mode, &stat->uid, &stat->gid); |
Eric W. Biederman | 28a6d67 | 2006-10-02 02:17:05 -0700 | [diff] [blame] | 1789 | } |
| 1790 | rcu_read_unlock(); |
| 1791 | return 0; |
| 1792 | } |
| 1793 | |
| 1794 | /* dentry stuff */ |
| 1795 | |
| 1796 | /* |
Al Viro | 1bbc551 | 2018-05-02 21:26:16 -0400 | [diff] [blame] | 1797 | * Set <pid>/... inode ownership (can change due to setuid(), etc.) |
| 1798 | */ |
| 1799 | void pid_update_inode(struct task_struct *task, struct inode *inode) |
| 1800 | { |
| 1801 | task_dump_owner(task, inode->i_mode, &inode->i_uid, &inode->i_gid); |
| 1802 | |
| 1803 | inode->i_mode &= ~(S_ISUID | S_ISGID); |
| 1804 | security_task_to_inode(task, inode); |
| 1805 | } |
| 1806 | |
| 1807 | /* |
Eric W. Biederman | 28a6d67 | 2006-10-02 02:17:05 -0700 | [diff] [blame] | 1808 | * Rewrite the inode's ownerships here because the owning task may have |
| 1809 | * performed a setuid(), etc. |
| 1810 | * |
Eric W. Biederman | 28a6d67 | 2006-10-02 02:17:05 -0700 | [diff] [blame] | 1811 | */ |
Al Viro | 1bbc551 | 2018-05-02 21:26:16 -0400 | [diff] [blame] | 1812 | static int pid_revalidate(struct dentry *dentry, unsigned int flags) |
Eric W. Biederman | 28a6d67 | 2006-10-02 02:17:05 -0700 | [diff] [blame] | 1813 | { |
Nick Piggin | 34286d6 | 2011-01-07 17:49:57 +1100 | [diff] [blame] | 1814 | struct inode *inode; |
| 1815 | struct task_struct *task; |
David Howells | c69e8d9 | 2008-11-14 10:39:19 +1100 | [diff] [blame] | 1816 | |
Al Viro | 0b728e1 | 2012-06-10 16:03:43 -0400 | [diff] [blame] | 1817 | if (flags & LOOKUP_RCU) |
Nick Piggin | 34286d6 | 2011-01-07 17:49:57 +1100 | [diff] [blame] | 1818 | return -ECHILD; |
| 1819 | |
David Howells | 2b0143b | 2015-03-17 22:25:59 +0000 | [diff] [blame] | 1820 | inode = d_inode(dentry); |
Nick Piggin | 34286d6 | 2011-01-07 17:49:57 +1100 | [diff] [blame] | 1821 | task = get_proc_task(inode); |
| 1822 | |
Eric W. Biederman | 28a6d67 | 2006-10-02 02:17:05 -0700 | [diff] [blame] | 1823 | if (task) { |
Al Viro | 1bbc551 | 2018-05-02 21:26:16 -0400 | [diff] [blame] | 1824 | pid_update_inode(task, inode); |
Eric W. Biederman | 28a6d67 | 2006-10-02 02:17:05 -0700 | [diff] [blame] | 1825 | put_task_struct(task); |
| 1826 | return 1; |
| 1827 | } |
Eric W. Biederman | 28a6d67 | 2006-10-02 02:17:05 -0700 | [diff] [blame] | 1828 | return 0; |
| 1829 | } |
| 1830 | |
Oleg Nesterov | d855a4b | 2014-01-23 15:55:39 -0800 | [diff] [blame] | 1831 | static inline bool proc_inode_is_dead(struct inode *inode) |
| 1832 | { |
| 1833 | return !proc_pid(inode)->tasks[PIDTYPE_PID].first; |
| 1834 | } |
| 1835 | |
David Howells | 1dd704b | 2013-04-12 01:08:50 +0100 | [diff] [blame] | 1836 | int pid_delete_dentry(const struct dentry *dentry) |
| 1837 | { |
| 1838 | /* Is the task we represent dead? |
| 1839 | * If so, then don't put the dentry on the lru list, |
| 1840 | * kill it immediately. |
| 1841 | */ |
David Howells | 2b0143b | 2015-03-17 22:25:59 +0000 | [diff] [blame] | 1842 | return proc_inode_is_dead(d_inode(dentry)); |
David Howells | 1dd704b | 2013-04-12 01:08:50 +0100 | [diff] [blame] | 1843 | } |
| 1844 | |
Eric W. Biederman | 6b4e306 | 2010-03-07 16:41:34 -0800 | [diff] [blame] | 1845 | const struct dentry_operations pid_dentry_operations = |
Eric W. Biederman | 28a6d67 | 2006-10-02 02:17:05 -0700 | [diff] [blame] | 1846 | { |
| 1847 | .d_revalidate = pid_revalidate, |
| 1848 | .d_delete = pid_delete_dentry, |
| 1849 | }; |
| 1850 | |
| 1851 | /* Lookups */ |
| 1852 | |
Eric W. Biederman | 1c0d04c | 2006-10-02 02:18:57 -0700 | [diff] [blame] | 1853 | /* |
| 1854 | * Fill a directory entry. |
| 1855 | * |
| 1856 | * If possible create the dcache entry and derive our inode number and |
| 1857 | * file type from dcache entry. |
| 1858 | * |
| 1859 | * Since all of the proc inode numbers are dynamically generated, the inode |
| 1860 | * numbers do not exist until the inode is cache. This means creating the |
| 1861 | * the dcache entry in readdir is necessary to keep the inode numbers |
| 1862 | * reported by readdir in sync with the inode numbers reported |
| 1863 | * by stat. |
| 1864 | */ |
Al Viro | f0c3b50 | 2013-05-16 12:07:31 -0400 | [diff] [blame] | 1865 | bool proc_fill_cache(struct file *file, struct dir_context *ctx, |
Alexey Dobriyan | a4ef389 | 2018-06-07 17:10:10 -0700 | [diff] [blame] | 1866 | const char *name, unsigned int len, |
Eric Dumazet | c5141e6 | 2007-05-08 00:26:15 -0700 | [diff] [blame] | 1867 | instantiate_t instantiate, struct task_struct *task, const void *ptr) |
Eric W. Biederman | 61a2878 | 2006-10-02 02:18:49 -0700 | [diff] [blame] | 1868 | { |
Al Viro | f0c3b50 | 2013-05-16 12:07:31 -0400 | [diff] [blame] | 1869 | struct dentry *child, *dir = file->f_path.dentry; |
Al Viro | 1df98b8 | 2013-06-15 11:33:10 +0400 | [diff] [blame] | 1870 | struct qstr qname = QSTR_INIT(name, len); |
Eric W. Biederman | 61a2878 | 2006-10-02 02:18:49 -0700 | [diff] [blame] | 1871 | struct inode *inode; |
Al Viro | 0168b9e | 2018-05-03 09:21:05 -0400 | [diff] [blame] | 1872 | unsigned type = DT_UNKNOWN; |
| 1873 | ino_t ino = 1; |
Eric W. Biederman | 61a2878 | 2006-10-02 02:18:49 -0700 | [diff] [blame] | 1874 | |
Al Viro | 1df98b8 | 2013-06-15 11:33:10 +0400 | [diff] [blame] | 1875 | child = d_hash_and_lookup(dir, &qname); |
Eric W. Biederman | 61a2878 | 2006-10-02 02:18:49 -0700 | [diff] [blame] | 1876 | if (!child) { |
Al Viro | 3781764 | 2016-04-20 16:31:31 -0400 | [diff] [blame] | 1877 | DECLARE_WAIT_QUEUE_HEAD_ONSTACK(wq); |
| 1878 | child = d_alloc_parallel(dir, &qname, &wq); |
| 1879 | if (IS_ERR(child)) |
Al Viro | 1df98b8 | 2013-06-15 11:33:10 +0400 | [diff] [blame] | 1880 | goto end_instantiate; |
Al Viro | 3781764 | 2016-04-20 16:31:31 -0400 | [diff] [blame] | 1881 | if (d_in_lookup(child)) { |
Al Viro | 0168b9e | 2018-05-03 09:21:05 -0400 | [diff] [blame] | 1882 | struct dentry *res; |
| 1883 | res = instantiate(child, task, ptr); |
Al Viro | 3781764 | 2016-04-20 16:31:31 -0400 | [diff] [blame] | 1884 | d_lookup_done(child); |
Al Viro | 0168b9e | 2018-05-03 09:21:05 -0400 | [diff] [blame] | 1885 | if (unlikely(res)) { |
| 1886 | dput(child); |
| 1887 | child = res; |
Al Viro | d85b399 | 2018-06-08 01:17:11 -0400 | [diff] [blame] | 1888 | if (IS_ERR(child)) |
| 1889 | goto end_instantiate; |
Al Viro | 3781764 | 2016-04-20 16:31:31 -0400 | [diff] [blame] | 1890 | } |
Eric W. Biederman | 61a2878 | 2006-10-02 02:18:49 -0700 | [diff] [blame] | 1891 | } |
| 1892 | } |
David Howells | 2b0143b | 2015-03-17 22:25:59 +0000 | [diff] [blame] | 1893 | inode = d_inode(child); |
Al Viro | 147ce69 | 2013-06-15 10:26:35 +0400 | [diff] [blame] | 1894 | ino = inode->i_ino; |
| 1895 | type = inode->i_mode >> 12; |
Eric W. Biederman | 61a2878 | 2006-10-02 02:18:49 -0700 | [diff] [blame] | 1896 | dput(child); |
Al Viro | d85b399 | 2018-06-08 01:17:11 -0400 | [diff] [blame] | 1897 | end_instantiate: |
Al Viro | f0c3b50 | 2013-05-16 12:07:31 -0400 | [diff] [blame] | 1898 | return dir_emit(ctx, name, len, ino, type); |
Eric W. Biederman | 61a2878 | 2006-10-02 02:18:49 -0700 | [diff] [blame] | 1899 | } |
| 1900 | |
Pavel Emelyanov | 640708a | 2012-01-10 15:11:23 -0800 | [diff] [blame] | 1901 | /* |
| 1902 | * dname_to_vma_addr - maps a dentry name into two unsigned longs |
| 1903 | * which represent vma start and end addresses. |
| 1904 | */ |
| 1905 | static int dname_to_vma_addr(struct dentry *dentry, |
| 1906 | unsigned long *start, unsigned long *end) |
| 1907 | { |
Alexey Dobriyan | ac7f106 | 2018-02-06 15:36:59 -0800 | [diff] [blame] | 1908 | const char *str = dentry->d_name.name; |
| 1909 | unsigned long long sval, eval; |
| 1910 | unsigned int len; |
| 1911 | |
Alexey Dobriyan | 35318db | 2018-04-10 16:41:14 -0700 | [diff] [blame] | 1912 | if (str[0] == '0' && str[1] != '-') |
| 1913 | return -EINVAL; |
Alexey Dobriyan | ac7f106 | 2018-02-06 15:36:59 -0800 | [diff] [blame] | 1914 | len = _parse_integer(str, 16, &sval); |
| 1915 | if (len & KSTRTOX_OVERFLOW) |
Pavel Emelyanov | 640708a | 2012-01-10 15:11:23 -0800 | [diff] [blame] | 1916 | return -EINVAL; |
Alexey Dobriyan | ac7f106 | 2018-02-06 15:36:59 -0800 | [diff] [blame] | 1917 | if (sval != (unsigned long)sval) |
| 1918 | return -EINVAL; |
| 1919 | str += len; |
| 1920 | |
| 1921 | if (*str != '-') |
| 1922 | return -EINVAL; |
| 1923 | str++; |
| 1924 | |
Alexey Dobriyan | 35318db | 2018-04-10 16:41:14 -0700 | [diff] [blame] | 1925 | if (str[0] == '0' && str[1]) |
| 1926 | return -EINVAL; |
Alexey Dobriyan | ac7f106 | 2018-02-06 15:36:59 -0800 | [diff] [blame] | 1927 | len = _parse_integer(str, 16, &eval); |
| 1928 | if (len & KSTRTOX_OVERFLOW) |
| 1929 | return -EINVAL; |
| 1930 | if (eval != (unsigned long)eval) |
| 1931 | return -EINVAL; |
| 1932 | str += len; |
| 1933 | |
| 1934 | if (*str != '\0') |
| 1935 | return -EINVAL; |
| 1936 | |
| 1937 | *start = sval; |
| 1938 | *end = eval; |
Pavel Emelyanov | 640708a | 2012-01-10 15:11:23 -0800 | [diff] [blame] | 1939 | |
| 1940 | return 0; |
| 1941 | } |
| 1942 | |
Al Viro | 0b728e1 | 2012-06-10 16:03:43 -0400 | [diff] [blame] | 1943 | static int map_files_d_revalidate(struct dentry *dentry, unsigned int flags) |
Pavel Emelyanov | 640708a | 2012-01-10 15:11:23 -0800 | [diff] [blame] | 1944 | { |
| 1945 | unsigned long vm_start, vm_end; |
| 1946 | bool exact_vma_exists = false; |
| 1947 | struct mm_struct *mm = NULL; |
| 1948 | struct task_struct *task; |
Pavel Emelyanov | 640708a | 2012-01-10 15:11:23 -0800 | [diff] [blame] | 1949 | struct inode *inode; |
| 1950 | int status = 0; |
| 1951 | |
Al Viro | 0b728e1 | 2012-06-10 16:03:43 -0400 | [diff] [blame] | 1952 | if (flags & LOOKUP_RCU) |
Pavel Emelyanov | 640708a | 2012-01-10 15:11:23 -0800 | [diff] [blame] | 1953 | return -ECHILD; |
| 1954 | |
David Howells | 2b0143b | 2015-03-17 22:25:59 +0000 | [diff] [blame] | 1955 | inode = d_inode(dentry); |
Pavel Emelyanov | 640708a | 2012-01-10 15:11:23 -0800 | [diff] [blame] | 1956 | task = get_proc_task(inode); |
| 1957 | if (!task) |
| 1958 | goto out_notask; |
| 1959 | |
Jann Horn | caaee62 | 2016-01-20 15:00:04 -0800 | [diff] [blame] | 1960 | mm = mm_access(task, PTRACE_MODE_READ_FSCREDS); |
Cong Wang | 2344bec | 2012-05-31 16:26:18 -0700 | [diff] [blame] | 1961 | if (IS_ERR_OR_NULL(mm)) |
Pavel Emelyanov | 640708a | 2012-01-10 15:11:23 -0800 | [diff] [blame] | 1962 | goto out; |
| 1963 | |
| 1964 | if (!dname_to_vma_addr(dentry, &vm_start, &vm_end)) { |
| 1965 | down_read(&mm->mmap_sem); |
| 1966 | exact_vma_exists = !!find_exact_vma(mm, vm_start, vm_end); |
| 1967 | up_read(&mm->mmap_sem); |
| 1968 | } |
| 1969 | |
| 1970 | mmput(mm); |
| 1971 | |
| 1972 | if (exact_vma_exists) { |
Eric W. Biederman | 68eb94f | 2017-01-03 10:23:11 +1300 | [diff] [blame] | 1973 | task_dump_owner(task, 0, &inode->i_uid, &inode->i_gid); |
| 1974 | |
Pavel Emelyanov | 640708a | 2012-01-10 15:11:23 -0800 | [diff] [blame] | 1975 | security_task_to_inode(task, inode); |
| 1976 | status = 1; |
| 1977 | } |
| 1978 | |
| 1979 | out: |
| 1980 | put_task_struct(task); |
| 1981 | |
| 1982 | out_notask: |
Pavel Emelyanov | 640708a | 2012-01-10 15:11:23 -0800 | [diff] [blame] | 1983 | return status; |
| 1984 | } |
| 1985 | |
| 1986 | static const struct dentry_operations tid_map_files_dentry_operations = { |
| 1987 | .d_revalidate = map_files_d_revalidate, |
| 1988 | .d_delete = pid_delete_dentry, |
| 1989 | }; |
| 1990 | |
Al Viro | 6b25539 | 2015-11-17 10:20:54 -0500 | [diff] [blame] | 1991 | static int map_files_get_link(struct dentry *dentry, struct path *path) |
Pavel Emelyanov | 640708a | 2012-01-10 15:11:23 -0800 | [diff] [blame] | 1992 | { |
| 1993 | unsigned long vm_start, vm_end; |
| 1994 | struct vm_area_struct *vma; |
| 1995 | struct task_struct *task; |
| 1996 | struct mm_struct *mm; |
| 1997 | int rc; |
| 1998 | |
| 1999 | rc = -ENOENT; |
David Howells | 2b0143b | 2015-03-17 22:25:59 +0000 | [diff] [blame] | 2000 | task = get_proc_task(d_inode(dentry)); |
Pavel Emelyanov | 640708a | 2012-01-10 15:11:23 -0800 | [diff] [blame] | 2001 | if (!task) |
| 2002 | goto out; |
| 2003 | |
| 2004 | mm = get_task_mm(task); |
| 2005 | put_task_struct(task); |
| 2006 | if (!mm) |
| 2007 | goto out; |
| 2008 | |
| 2009 | rc = dname_to_vma_addr(dentry, &vm_start, &vm_end); |
| 2010 | if (rc) |
| 2011 | goto out_mmput; |
| 2012 | |
Artem Fetishev | 70335ab | 2014-03-10 15:49:45 -0700 | [diff] [blame] | 2013 | rc = -ENOENT; |
Pavel Emelyanov | 640708a | 2012-01-10 15:11:23 -0800 | [diff] [blame] | 2014 | down_read(&mm->mmap_sem); |
| 2015 | vma = find_exact_vma(mm, vm_start, vm_end); |
| 2016 | if (vma && vma->vm_file) { |
| 2017 | *path = vma->vm_file->f_path; |
| 2018 | path_get(path); |
| 2019 | rc = 0; |
| 2020 | } |
| 2021 | up_read(&mm->mmap_sem); |
| 2022 | |
| 2023 | out_mmput: |
| 2024 | mmput(mm); |
| 2025 | out: |
| 2026 | return rc; |
| 2027 | } |
| 2028 | |
| 2029 | struct map_files_info { |
Alexey Dobriyan | 20d28cd | 2018-02-06 15:37:06 -0800 | [diff] [blame] | 2030 | unsigned long start; |
| 2031 | unsigned long end; |
Al Viro | 7b540d0 | 2012-08-27 14:55:26 -0400 | [diff] [blame] | 2032 | fmode_t mode; |
Pavel Emelyanov | 640708a | 2012-01-10 15:11:23 -0800 | [diff] [blame] | 2033 | }; |
| 2034 | |
Calvin Owens | bdb4d10 | 2015-09-09 15:35:54 -0700 | [diff] [blame] | 2035 | /* |
| 2036 | * Only allow CAP_SYS_ADMIN to follow the links, due to concerns about how the |
| 2037 | * symlinks may be used to bypass permissions on ancestor directories in the |
| 2038 | * path to the file in question. |
| 2039 | */ |
| 2040 | static const char * |
Al Viro | 6b25539 | 2015-11-17 10:20:54 -0500 | [diff] [blame] | 2041 | proc_map_files_get_link(struct dentry *dentry, |
Al Viro | fceef39 | 2015-12-29 15:58:39 -0500 | [diff] [blame] | 2042 | struct inode *inode, |
| 2043 | struct delayed_call *done) |
Calvin Owens | bdb4d10 | 2015-09-09 15:35:54 -0700 | [diff] [blame] | 2044 | { |
| 2045 | if (!capable(CAP_SYS_ADMIN)) |
| 2046 | return ERR_PTR(-EPERM); |
| 2047 | |
Al Viro | fceef39 | 2015-12-29 15:58:39 -0500 | [diff] [blame] | 2048 | return proc_pid_get_link(dentry, inode, done); |
Calvin Owens | bdb4d10 | 2015-09-09 15:35:54 -0700 | [diff] [blame] | 2049 | } |
| 2050 | |
| 2051 | /* |
Al Viro | 6b25539 | 2015-11-17 10:20:54 -0500 | [diff] [blame] | 2052 | * Identical to proc_pid_link_inode_operations except for get_link() |
Calvin Owens | bdb4d10 | 2015-09-09 15:35:54 -0700 | [diff] [blame] | 2053 | */ |
| 2054 | static const struct inode_operations proc_map_files_link_inode_operations = { |
| 2055 | .readlink = proc_pid_readlink, |
Al Viro | 6b25539 | 2015-11-17 10:20:54 -0500 | [diff] [blame] | 2056 | .get_link = proc_map_files_get_link, |
Calvin Owens | bdb4d10 | 2015-09-09 15:35:54 -0700 | [diff] [blame] | 2057 | .setattr = proc_setattr, |
| 2058 | }; |
| 2059 | |
Al Viro | 0168b9e | 2018-05-03 09:21:05 -0400 | [diff] [blame] | 2060 | static struct dentry * |
| 2061 | proc_map_files_instantiate(struct dentry *dentry, |
Pavel Emelyanov | 640708a | 2012-01-10 15:11:23 -0800 | [diff] [blame] | 2062 | struct task_struct *task, const void *ptr) |
| 2063 | { |
Al Viro | 7b540d0 | 2012-08-27 14:55:26 -0400 | [diff] [blame] | 2064 | fmode_t mode = (fmode_t)(unsigned long)ptr; |
Pavel Emelyanov | 640708a | 2012-01-10 15:11:23 -0800 | [diff] [blame] | 2065 | struct proc_inode *ei; |
| 2066 | struct inode *inode; |
| 2067 | |
Al Viro | 0168b9e | 2018-05-03 09:21:05 -0400 | [diff] [blame] | 2068 | inode = proc_pid_make_inode(dentry->d_sb, task, S_IFLNK | |
Andreas Gruenbacher | db978da | 2016-11-10 22:18:28 +0100 | [diff] [blame] | 2069 | ((mode & FMODE_READ ) ? S_IRUSR : 0) | |
| 2070 | ((mode & FMODE_WRITE) ? S_IWUSR : 0)); |
Pavel Emelyanov | 640708a | 2012-01-10 15:11:23 -0800 | [diff] [blame] | 2071 | if (!inode) |
Al Viro | 0168b9e | 2018-05-03 09:21:05 -0400 | [diff] [blame] | 2072 | return ERR_PTR(-ENOENT); |
Pavel Emelyanov | 640708a | 2012-01-10 15:11:23 -0800 | [diff] [blame] | 2073 | |
| 2074 | ei = PROC_I(inode); |
Al Viro | 6b25539 | 2015-11-17 10:20:54 -0500 | [diff] [blame] | 2075 | ei->op.proc_get_link = map_files_get_link; |
Pavel Emelyanov | 640708a | 2012-01-10 15:11:23 -0800 | [diff] [blame] | 2076 | |
Calvin Owens | bdb4d10 | 2015-09-09 15:35:54 -0700 | [diff] [blame] | 2077 | inode->i_op = &proc_map_files_link_inode_operations; |
Pavel Emelyanov | 640708a | 2012-01-10 15:11:23 -0800 | [diff] [blame] | 2078 | inode->i_size = 64; |
Pavel Emelyanov | 640708a | 2012-01-10 15:11:23 -0800 | [diff] [blame] | 2079 | |
| 2080 | d_set_d_op(dentry, &tid_map_files_dentry_operations); |
Al Viro | 0168b9e | 2018-05-03 09:21:05 -0400 | [diff] [blame] | 2081 | return d_splice_alias(inode, dentry); |
Pavel Emelyanov | 640708a | 2012-01-10 15:11:23 -0800 | [diff] [blame] | 2082 | } |
| 2083 | |
| 2084 | static struct dentry *proc_map_files_lookup(struct inode *dir, |
Al Viro | 00cd8dd | 2012-06-10 17:13:09 -0400 | [diff] [blame] | 2085 | struct dentry *dentry, unsigned int flags) |
Pavel Emelyanov | 640708a | 2012-01-10 15:11:23 -0800 | [diff] [blame] | 2086 | { |
| 2087 | unsigned long vm_start, vm_end; |
| 2088 | struct vm_area_struct *vma; |
| 2089 | struct task_struct *task; |
Al Viro | 0168b9e | 2018-05-03 09:21:05 -0400 | [diff] [blame] | 2090 | struct dentry *result; |
Pavel Emelyanov | 640708a | 2012-01-10 15:11:23 -0800 | [diff] [blame] | 2091 | struct mm_struct *mm; |
| 2092 | |
Al Viro | 0168b9e | 2018-05-03 09:21:05 -0400 | [diff] [blame] | 2093 | result = ERR_PTR(-ENOENT); |
Pavel Emelyanov | 640708a | 2012-01-10 15:11:23 -0800 | [diff] [blame] | 2094 | task = get_proc_task(dir); |
| 2095 | if (!task) |
| 2096 | goto out; |
| 2097 | |
Al Viro | 0168b9e | 2018-05-03 09:21:05 -0400 | [diff] [blame] | 2098 | result = ERR_PTR(-EACCES); |
Jann Horn | caaee62 | 2016-01-20 15:00:04 -0800 | [diff] [blame] | 2099 | if (!ptrace_may_access(task, PTRACE_MODE_READ_FSCREDS)) |
Pavel Emelyanov | 640708a | 2012-01-10 15:11:23 -0800 | [diff] [blame] | 2100 | goto out_put_task; |
| 2101 | |
Al Viro | 0168b9e | 2018-05-03 09:21:05 -0400 | [diff] [blame] | 2102 | result = ERR_PTR(-ENOENT); |
Pavel Emelyanov | 640708a | 2012-01-10 15:11:23 -0800 | [diff] [blame] | 2103 | if (dname_to_vma_addr(dentry, &vm_start, &vm_end)) |
Cyrill Gorcunov | eb94cd9 | 2012-05-17 17:03:25 -0700 | [diff] [blame] | 2104 | goto out_put_task; |
Pavel Emelyanov | 640708a | 2012-01-10 15:11:23 -0800 | [diff] [blame] | 2105 | |
| 2106 | mm = get_task_mm(task); |
| 2107 | if (!mm) |
Cyrill Gorcunov | eb94cd9 | 2012-05-17 17:03:25 -0700 | [diff] [blame] | 2108 | goto out_put_task; |
Pavel Emelyanov | 640708a | 2012-01-10 15:11:23 -0800 | [diff] [blame] | 2109 | |
| 2110 | down_read(&mm->mmap_sem); |
| 2111 | vma = find_exact_vma(mm, vm_start, vm_end); |
| 2112 | if (!vma) |
| 2113 | goto out_no_vma; |
| 2114 | |
Stanislav Kinsbursky | 05f5648 | 2012-11-26 16:29:42 -0800 | [diff] [blame] | 2115 | if (vma->vm_file) |
Al Viro | 0168b9e | 2018-05-03 09:21:05 -0400 | [diff] [blame] | 2116 | result = proc_map_files_instantiate(dentry, task, |
Stanislav Kinsbursky | 05f5648 | 2012-11-26 16:29:42 -0800 | [diff] [blame] | 2117 | (void *)(unsigned long)vma->vm_file->f_mode); |
Pavel Emelyanov | 640708a | 2012-01-10 15:11:23 -0800 | [diff] [blame] | 2118 | |
| 2119 | out_no_vma: |
| 2120 | up_read(&mm->mmap_sem); |
| 2121 | mmput(mm); |
Pavel Emelyanov | 640708a | 2012-01-10 15:11:23 -0800 | [diff] [blame] | 2122 | out_put_task: |
| 2123 | put_task_struct(task); |
| 2124 | out: |
Al Viro | 0168b9e | 2018-05-03 09:21:05 -0400 | [diff] [blame] | 2125 | return result; |
Pavel Emelyanov | 640708a | 2012-01-10 15:11:23 -0800 | [diff] [blame] | 2126 | } |
| 2127 | |
| 2128 | static const struct inode_operations proc_map_files_inode_operations = { |
| 2129 | .lookup = proc_map_files_lookup, |
| 2130 | .permission = proc_fd_permission, |
| 2131 | .setattr = proc_setattr, |
| 2132 | }; |
| 2133 | |
| 2134 | static int |
Al Viro | f0c3b50 | 2013-05-16 12:07:31 -0400 | [diff] [blame] | 2135 | proc_map_files_readdir(struct file *file, struct dir_context *ctx) |
Pavel Emelyanov | 640708a | 2012-01-10 15:11:23 -0800 | [diff] [blame] | 2136 | { |
Pavel Emelyanov | 640708a | 2012-01-10 15:11:23 -0800 | [diff] [blame] | 2137 | struct vm_area_struct *vma; |
| 2138 | struct task_struct *task; |
| 2139 | struct mm_struct *mm; |
Al Viro | f0c3b50 | 2013-05-16 12:07:31 -0400 | [diff] [blame] | 2140 | unsigned long nr_files, pos, i; |
Kent Overstreet | 94f8f3b | 2019-03-11 23:31:18 -0700 | [diff] [blame] | 2141 | GENRADIX(struct map_files_info) fa; |
Al Viro | f0c3b50 | 2013-05-16 12:07:31 -0400 | [diff] [blame] | 2142 | struct map_files_info *p; |
Pavel Emelyanov | 640708a | 2012-01-10 15:11:23 -0800 | [diff] [blame] | 2143 | int ret; |
| 2144 | |
Kent Overstreet | 94f8f3b | 2019-03-11 23:31:18 -0700 | [diff] [blame] | 2145 | genradix_init(&fa); |
| 2146 | |
Pavel Emelyanov | 640708a | 2012-01-10 15:11:23 -0800 | [diff] [blame] | 2147 | ret = -ENOENT; |
Al Viro | f0c3b50 | 2013-05-16 12:07:31 -0400 | [diff] [blame] | 2148 | task = get_proc_task(file_inode(file)); |
Pavel Emelyanov | 640708a | 2012-01-10 15:11:23 -0800 | [diff] [blame] | 2149 | if (!task) |
| 2150 | goto out; |
| 2151 | |
| 2152 | ret = -EACCES; |
Jann Horn | caaee62 | 2016-01-20 15:00:04 -0800 | [diff] [blame] | 2153 | if (!ptrace_may_access(task, PTRACE_MODE_READ_FSCREDS)) |
Pavel Emelyanov | 640708a | 2012-01-10 15:11:23 -0800 | [diff] [blame] | 2154 | goto out_put_task; |
| 2155 | |
| 2156 | ret = 0; |
Al Viro | f0c3b50 | 2013-05-16 12:07:31 -0400 | [diff] [blame] | 2157 | if (!dir_emit_dots(file, ctx)) |
| 2158 | goto out_put_task; |
Pavel Emelyanov | 640708a | 2012-01-10 15:11:23 -0800 | [diff] [blame] | 2159 | |
Al Viro | f0c3b50 | 2013-05-16 12:07:31 -0400 | [diff] [blame] | 2160 | mm = get_task_mm(task); |
| 2161 | if (!mm) |
| 2162 | goto out_put_task; |
| 2163 | down_read(&mm->mmap_sem); |
Pavel Emelyanov | 640708a | 2012-01-10 15:11:23 -0800 | [diff] [blame] | 2164 | |
Al Viro | f0c3b50 | 2013-05-16 12:07:31 -0400 | [diff] [blame] | 2165 | nr_files = 0; |
Pavel Emelyanov | 640708a | 2012-01-10 15:11:23 -0800 | [diff] [blame] | 2166 | |
Al Viro | f0c3b50 | 2013-05-16 12:07:31 -0400 | [diff] [blame] | 2167 | /* |
| 2168 | * We need two passes here: |
| 2169 | * |
| 2170 | * 1) Collect vmas of mapped files with mmap_sem taken |
| 2171 | * 2) Release mmap_sem and instantiate entries |
| 2172 | * |
| 2173 | * otherwise we get lockdep complained, since filldir() |
| 2174 | * routine might require mmap_sem taken in might_fault(). |
| 2175 | */ |
Pavel Emelyanov | 640708a | 2012-01-10 15:11:23 -0800 | [diff] [blame] | 2176 | |
Al Viro | f0c3b50 | 2013-05-16 12:07:31 -0400 | [diff] [blame] | 2177 | for (vma = mm->mmap, pos = 2; vma; vma = vma->vm_next) { |
Kent Overstreet | 94f8f3b | 2019-03-11 23:31:18 -0700 | [diff] [blame] | 2178 | if (!vma->vm_file) |
| 2179 | continue; |
| 2180 | if (++pos <= ctx->pos) |
| 2181 | continue; |
Al Viro | f0c3b50 | 2013-05-16 12:07:31 -0400 | [diff] [blame] | 2182 | |
Kent Overstreet | 94f8f3b | 2019-03-11 23:31:18 -0700 | [diff] [blame] | 2183 | p = genradix_ptr_alloc(&fa, nr_files++, GFP_KERNEL); |
| 2184 | if (!p) { |
Al Viro | f0c3b50 | 2013-05-16 12:07:31 -0400 | [diff] [blame] | 2185 | ret = -ENOMEM; |
Al Viro | f0c3b50 | 2013-05-16 12:07:31 -0400 | [diff] [blame] | 2186 | up_read(&mm->mmap_sem); |
| 2187 | mmput(mm); |
| 2188 | goto out_put_task; |
| 2189 | } |
Al Viro | f0c3b50 | 2013-05-16 12:07:31 -0400 | [diff] [blame] | 2190 | |
Kent Overstreet | 94f8f3b | 2019-03-11 23:31:18 -0700 | [diff] [blame] | 2191 | p->start = vma->vm_start; |
| 2192 | p->end = vma->vm_end; |
| 2193 | p->mode = vma->vm_file->f_mode; |
Pavel Emelyanov | 640708a | 2012-01-10 15:11:23 -0800 | [diff] [blame] | 2194 | } |
Al Viro | f0c3b50 | 2013-05-16 12:07:31 -0400 | [diff] [blame] | 2195 | up_read(&mm->mmap_sem); |
Alexey Dobriyan | fe079a5 | 2018-04-10 16:32:05 -0700 | [diff] [blame] | 2196 | mmput(mm); |
Al Viro | f0c3b50 | 2013-05-16 12:07:31 -0400 | [diff] [blame] | 2197 | |
| 2198 | for (i = 0; i < nr_files; i++) { |
Alexey Dobriyan | 20d28cd | 2018-02-06 15:37:06 -0800 | [diff] [blame] | 2199 | char buf[4 * sizeof(long) + 2]; /* max: %lx-%lx\0 */ |
| 2200 | unsigned int len; |
| 2201 | |
Kent Overstreet | 94f8f3b | 2019-03-11 23:31:18 -0700 | [diff] [blame] | 2202 | p = genradix_ptr(&fa, i); |
Alexey Dobriyan | 20d28cd | 2018-02-06 15:37:06 -0800 | [diff] [blame] | 2203 | len = snprintf(buf, sizeof(buf), "%lx-%lx", p->start, p->end); |
Al Viro | f0c3b50 | 2013-05-16 12:07:31 -0400 | [diff] [blame] | 2204 | if (!proc_fill_cache(file, ctx, |
Alexey Dobriyan | 20d28cd | 2018-02-06 15:37:06 -0800 | [diff] [blame] | 2205 | buf, len, |
Al Viro | f0c3b50 | 2013-05-16 12:07:31 -0400 | [diff] [blame] | 2206 | proc_map_files_instantiate, |
| 2207 | task, |
| 2208 | (void *)(unsigned long)p->mode)) |
| 2209 | break; |
| 2210 | ctx->pos++; |
| 2211 | } |
Pavel Emelyanov | 640708a | 2012-01-10 15:11:23 -0800 | [diff] [blame] | 2212 | |
Pavel Emelyanov | 640708a | 2012-01-10 15:11:23 -0800 | [diff] [blame] | 2213 | out_put_task: |
| 2214 | put_task_struct(task); |
| 2215 | out: |
Kent Overstreet | 94f8f3b | 2019-03-11 23:31:18 -0700 | [diff] [blame] | 2216 | genradix_free(&fa); |
Pavel Emelyanov | 640708a | 2012-01-10 15:11:23 -0800 | [diff] [blame] | 2217 | return ret; |
| 2218 | } |
| 2219 | |
| 2220 | static const struct file_operations proc_map_files_operations = { |
| 2221 | .read = generic_read_dir, |
Al Viro | f50752e | 2016-04-20 17:13:54 -0400 | [diff] [blame] | 2222 | .iterate_shared = proc_map_files_readdir, |
| 2223 | .llseek = generic_file_llseek, |
Pavel Emelyanov | 640708a | 2012-01-10 15:11:23 -0800 | [diff] [blame] | 2224 | }; |
| 2225 | |
Nicolas Pitre | b18b6a9 | 2017-01-21 00:09:08 -0500 | [diff] [blame] | 2226 | #if defined(CONFIG_CHECKPOINT_RESTORE) && defined(CONFIG_POSIX_TIMERS) |
Pavel Emelyanov | 48f6a7a | 2013-03-11 13:12:45 +0400 | [diff] [blame] | 2227 | struct timers_private { |
| 2228 | struct pid *pid; |
| 2229 | struct task_struct *task; |
| 2230 | struct sighand_struct *sighand; |
Pavel Emelyanov | 57b8015 | 2013-03-11 13:13:08 +0400 | [diff] [blame] | 2231 | struct pid_namespace *ns; |
Pavel Emelyanov | 48f6a7a | 2013-03-11 13:12:45 +0400 | [diff] [blame] | 2232 | unsigned long flags; |
| 2233 | }; |
| 2234 | |
| 2235 | static void *timers_start(struct seq_file *m, loff_t *pos) |
| 2236 | { |
| 2237 | struct timers_private *tp = m->private; |
| 2238 | |
| 2239 | tp->task = get_pid_task(tp->pid, PIDTYPE_PID); |
| 2240 | if (!tp->task) |
| 2241 | return ERR_PTR(-ESRCH); |
| 2242 | |
| 2243 | tp->sighand = lock_task_sighand(tp->task, &tp->flags); |
| 2244 | if (!tp->sighand) |
| 2245 | return ERR_PTR(-ESRCH); |
| 2246 | |
| 2247 | return seq_list_start(&tp->task->signal->posix_timers, *pos); |
| 2248 | } |
| 2249 | |
| 2250 | static void *timers_next(struct seq_file *m, void *v, loff_t *pos) |
| 2251 | { |
| 2252 | struct timers_private *tp = m->private; |
| 2253 | return seq_list_next(v, &tp->task->signal->posix_timers, pos); |
| 2254 | } |
| 2255 | |
| 2256 | static void timers_stop(struct seq_file *m, void *v) |
| 2257 | { |
| 2258 | struct timers_private *tp = m->private; |
| 2259 | |
| 2260 | if (tp->sighand) { |
| 2261 | unlock_task_sighand(tp->task, &tp->flags); |
| 2262 | tp->sighand = NULL; |
| 2263 | } |
| 2264 | |
| 2265 | if (tp->task) { |
| 2266 | put_task_struct(tp->task); |
| 2267 | tp->task = NULL; |
| 2268 | } |
| 2269 | } |
| 2270 | |
| 2271 | static int show_timer(struct seq_file *m, void *v) |
| 2272 | { |
| 2273 | struct k_itimer *timer; |
Pavel Emelyanov | 57b8015 | 2013-03-11 13:13:08 +0400 | [diff] [blame] | 2274 | struct timers_private *tp = m->private; |
| 2275 | int notify; |
Alexey Dobriyan | cedbcca | 2014-08-08 14:21:33 -0700 | [diff] [blame] | 2276 | static const char * const nstr[] = { |
Pavel Emelyanov | 57b8015 | 2013-03-11 13:13:08 +0400 | [diff] [blame] | 2277 | [SIGEV_SIGNAL] = "signal", |
| 2278 | [SIGEV_NONE] = "none", |
| 2279 | [SIGEV_THREAD] = "thread", |
| 2280 | }; |
Pavel Emelyanov | 48f6a7a | 2013-03-11 13:12:45 +0400 | [diff] [blame] | 2281 | |
| 2282 | timer = list_entry((struct list_head *)v, struct k_itimer, list); |
Pavel Emelyanov | 57b8015 | 2013-03-11 13:13:08 +0400 | [diff] [blame] | 2283 | notify = timer->it_sigev_notify; |
| 2284 | |
Pavel Emelyanov | 48f6a7a | 2013-03-11 13:12:45 +0400 | [diff] [blame] | 2285 | seq_printf(m, "ID: %d\n", timer->it_id); |
Linus Torvalds | ba3edf1f | 2017-12-06 18:23:27 -0800 | [diff] [blame] | 2286 | seq_printf(m, "signal: %d/%px\n", |
Joe Perches | 25ce319 | 2015-04-15 16:18:17 -0700 | [diff] [blame] | 2287 | timer->sigq->info.si_signo, |
| 2288 | timer->sigq->info.si_value.sival_ptr); |
Pavel Emelyanov | 57b8015 | 2013-03-11 13:13:08 +0400 | [diff] [blame] | 2289 | seq_printf(m, "notify: %s/%s.%d\n", |
Joe Perches | 25ce319 | 2015-04-15 16:18:17 -0700 | [diff] [blame] | 2290 | nstr[notify & ~SIGEV_THREAD_ID], |
| 2291 | (notify & SIGEV_THREAD_ID) ? "tid" : "pid", |
| 2292 | pid_nr_ns(timer->it_pid, tp->ns)); |
Pavel Tikhomirov | 15ef029 | 2013-05-17 02:12:03 +0400 | [diff] [blame] | 2293 | seq_printf(m, "ClockID: %d\n", timer->it_clock); |
Pavel Emelyanov | 48f6a7a | 2013-03-11 13:12:45 +0400 | [diff] [blame] | 2294 | |
| 2295 | return 0; |
| 2296 | } |
| 2297 | |
| 2298 | static const struct seq_operations proc_timers_seq_ops = { |
| 2299 | .start = timers_start, |
| 2300 | .next = timers_next, |
| 2301 | .stop = timers_stop, |
| 2302 | .show = show_timer, |
| 2303 | }; |
| 2304 | |
| 2305 | static int proc_timers_open(struct inode *inode, struct file *file) |
| 2306 | { |
| 2307 | struct timers_private *tp; |
| 2308 | |
| 2309 | tp = __seq_open_private(file, &proc_timers_seq_ops, |
| 2310 | sizeof(struct timers_private)); |
| 2311 | if (!tp) |
| 2312 | return -ENOMEM; |
| 2313 | |
| 2314 | tp->pid = proc_pid(inode); |
Christoph Hellwig | 76f668b | 2018-05-16 07:19:01 +0200 | [diff] [blame] | 2315 | tp->ns = proc_pid_ns(inode); |
Pavel Emelyanov | 48f6a7a | 2013-03-11 13:12:45 +0400 | [diff] [blame] | 2316 | return 0; |
| 2317 | } |
| 2318 | |
| 2319 | static const struct file_operations proc_timers_operations = { |
| 2320 | .open = proc_timers_open, |
| 2321 | .read = seq_read, |
| 2322 | .llseek = seq_lseek, |
| 2323 | .release = seq_release_private, |
| 2324 | }; |
Eric Engestrom | b5946be | 2016-03-17 14:20:57 -0700 | [diff] [blame] | 2325 | #endif |
Pavel Emelyanov | 640708a | 2012-01-10 15:11:23 -0800 | [diff] [blame] | 2326 | |
John Stultz | 5de23d4 | 2016-03-17 14:20:54 -0700 | [diff] [blame] | 2327 | static ssize_t timerslack_ns_write(struct file *file, const char __user *buf, |
| 2328 | size_t count, loff_t *offset) |
| 2329 | { |
| 2330 | struct inode *inode = file_inode(file); |
| 2331 | struct task_struct *p; |
| 2332 | u64 slack_ns; |
| 2333 | int err; |
| 2334 | |
| 2335 | err = kstrtoull_from_user(buf, count, 10, &slack_ns); |
| 2336 | if (err < 0) |
| 2337 | return err; |
| 2338 | |
| 2339 | p = get_proc_task(inode); |
| 2340 | if (!p) |
| 2341 | return -ESRCH; |
| 2342 | |
John Stultz | 4b2bd5f | 2016-10-07 17:02:33 -0700 | [diff] [blame] | 2343 | if (p != current) { |
Benjamin Gordon | 8da0b4f | 2019-01-03 15:25:56 -0800 | [diff] [blame] | 2344 | rcu_read_lock(); |
| 2345 | if (!ns_capable(__task_cred(p)->user_ns, CAP_SYS_NICE)) { |
| 2346 | rcu_read_unlock(); |
John Stultz | 4b2bd5f | 2016-10-07 17:02:33 -0700 | [diff] [blame] | 2347 | count = -EPERM; |
| 2348 | goto out; |
| 2349 | } |
Benjamin Gordon | 8da0b4f | 2019-01-03 15:25:56 -0800 | [diff] [blame] | 2350 | rcu_read_unlock(); |
John Stultz | 5de23d4 | 2016-03-17 14:20:54 -0700 | [diff] [blame] | 2351 | |
John Stultz | 4b2bd5f | 2016-10-07 17:02:33 -0700 | [diff] [blame] | 2352 | err = security_task_setscheduler(p); |
| 2353 | if (err) { |
| 2354 | count = err; |
| 2355 | goto out; |
| 2356 | } |
John Stultz | 904763e | 2016-10-07 17:02:29 -0700 | [diff] [blame] | 2357 | } |
| 2358 | |
John Stultz | 7abbaf9 | 2016-10-07 17:02:26 -0700 | [diff] [blame] | 2359 | task_lock(p); |
| 2360 | if (slack_ns == 0) |
| 2361 | p->timer_slack_ns = p->default_timer_slack_ns; |
| 2362 | else |
| 2363 | p->timer_slack_ns = slack_ns; |
| 2364 | task_unlock(p); |
| 2365 | |
| 2366 | out: |
John Stultz | 5de23d4 | 2016-03-17 14:20:54 -0700 | [diff] [blame] | 2367 | put_task_struct(p); |
| 2368 | |
| 2369 | return count; |
| 2370 | } |
| 2371 | |
| 2372 | static int timerslack_ns_show(struct seq_file *m, void *v) |
| 2373 | { |
| 2374 | struct inode *inode = m->private; |
| 2375 | struct task_struct *p; |
John Stultz | 7abbaf9 | 2016-10-07 17:02:26 -0700 | [diff] [blame] | 2376 | int err = 0; |
John Stultz | 5de23d4 | 2016-03-17 14:20:54 -0700 | [diff] [blame] | 2377 | |
| 2378 | p = get_proc_task(inode); |
| 2379 | if (!p) |
| 2380 | return -ESRCH; |
| 2381 | |
John Stultz | 4b2bd5f | 2016-10-07 17:02:33 -0700 | [diff] [blame] | 2382 | if (p != current) { |
Benjamin Gordon | 8da0b4f | 2019-01-03 15:25:56 -0800 | [diff] [blame] | 2383 | rcu_read_lock(); |
| 2384 | if (!ns_capable(__task_cred(p)->user_ns, CAP_SYS_NICE)) { |
| 2385 | rcu_read_unlock(); |
John Stultz | 4b2bd5f | 2016-10-07 17:02:33 -0700 | [diff] [blame] | 2386 | err = -EPERM; |
| 2387 | goto out; |
| 2388 | } |
Benjamin Gordon | 8da0b4f | 2019-01-03 15:25:56 -0800 | [diff] [blame] | 2389 | rcu_read_unlock(); |
| 2390 | |
John Stultz | 4b2bd5f | 2016-10-07 17:02:33 -0700 | [diff] [blame] | 2391 | err = security_task_getscheduler(p); |
| 2392 | if (err) |
| 2393 | goto out; |
| 2394 | } |
John Stultz | 904763e | 2016-10-07 17:02:29 -0700 | [diff] [blame] | 2395 | |
John Stultz | 7abbaf9 | 2016-10-07 17:02:26 -0700 | [diff] [blame] | 2396 | task_lock(p); |
| 2397 | seq_printf(m, "%llu\n", p->timer_slack_ns); |
| 2398 | task_unlock(p); |
| 2399 | |
| 2400 | out: |
John Stultz | 5de23d4 | 2016-03-17 14:20:54 -0700 | [diff] [blame] | 2401 | put_task_struct(p); |
| 2402 | |
| 2403 | return err; |
| 2404 | } |
| 2405 | |
| 2406 | static int timerslack_ns_open(struct inode *inode, struct file *filp) |
| 2407 | { |
| 2408 | return single_open(filp, timerslack_ns_show, inode); |
| 2409 | } |
| 2410 | |
| 2411 | static const struct file_operations proc_pid_set_timerslack_ns_operations = { |
| 2412 | .open = timerslack_ns_open, |
| 2413 | .read = seq_read, |
| 2414 | .write = timerslack_ns_write, |
| 2415 | .llseek = seq_lseek, |
| 2416 | .release = single_release, |
| 2417 | }; |
| 2418 | |
Al Viro | 0168b9e | 2018-05-03 09:21:05 -0400 | [diff] [blame] | 2419 | static struct dentry *proc_pident_instantiate(struct dentry *dentry, |
| 2420 | struct task_struct *task, const void *ptr) |
Eric W. Biederman | 444ceed | 2006-10-02 02:18:49 -0700 | [diff] [blame] | 2421 | { |
Eric Dumazet | c5141e6 | 2007-05-08 00:26:15 -0700 | [diff] [blame] | 2422 | const struct pid_entry *p = ptr; |
Eric W. Biederman | 444ceed | 2006-10-02 02:18:49 -0700 | [diff] [blame] | 2423 | struct inode *inode; |
| 2424 | struct proc_inode *ei; |
Eric W. Biederman | 444ceed | 2006-10-02 02:18:49 -0700 | [diff] [blame] | 2425 | |
Al Viro | 0168b9e | 2018-05-03 09:21:05 -0400 | [diff] [blame] | 2426 | inode = proc_pid_make_inode(dentry->d_sb, task, p->mode); |
Eric W. Biederman | 444ceed | 2006-10-02 02:18:49 -0700 | [diff] [blame] | 2427 | if (!inode) |
Al Viro | 0168b9e | 2018-05-03 09:21:05 -0400 | [diff] [blame] | 2428 | return ERR_PTR(-ENOENT); |
Eric W. Biederman | 444ceed | 2006-10-02 02:18:49 -0700 | [diff] [blame] | 2429 | |
| 2430 | ei = PROC_I(inode); |
Eric W. Biederman | 444ceed | 2006-10-02 02:18:49 -0700 | [diff] [blame] | 2431 | if (S_ISDIR(inode->i_mode)) |
Miklos Szeredi | bfe8684 | 2011-10-28 14:13:29 +0200 | [diff] [blame] | 2432 | set_nlink(inode, 2); /* Use getattr to fix if necessary */ |
Eric W. Biederman | 444ceed | 2006-10-02 02:18:49 -0700 | [diff] [blame] | 2433 | if (p->iop) |
| 2434 | inode->i_op = p->iop; |
| 2435 | if (p->fop) |
| 2436 | inode->i_fop = p->fop; |
| 2437 | ei->op = p->op; |
Al Viro | 1bbc551 | 2018-05-02 21:26:16 -0400 | [diff] [blame] | 2438 | pid_update_inode(task, inode); |
Nick Piggin | fb045ad | 2011-01-07 17:49:55 +1100 | [diff] [blame] | 2439 | d_set_d_op(dentry, &pid_dentry_operations); |
Al Viro | 0168b9e | 2018-05-03 09:21:05 -0400 | [diff] [blame] | 2440 | return d_splice_alias(inode, dentry); |
Eric W. Biederman | 444ceed | 2006-10-02 02:18:49 -0700 | [diff] [blame] | 2441 | } |
| 2442 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2443 | static struct dentry *proc_pident_lookup(struct inode *dir, |
| 2444 | struct dentry *dentry, |
Alexey Dobriyan | d5a572a | 2019-03-11 23:28:51 -0700 | [diff] [blame] | 2445 | const struct pid_entry *p, |
| 2446 | const struct pid_entry *end) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2447 | { |
Eric W. Biederman | 99f8955 | 2006-06-26 00:25:55 -0700 | [diff] [blame] | 2448 | struct task_struct *task = get_proc_task(dir); |
Al Viro | 0168b9e | 2018-05-03 09:21:05 -0400 | [diff] [blame] | 2449 | struct dentry *res = ERR_PTR(-ENOENT); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2450 | |
Eric W. Biederman | 99f8955 | 2006-06-26 00:25:55 -0700 | [diff] [blame] | 2451 | if (!task) |
| 2452 | goto out_no_task; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2453 | |
Eric W. Biederman | 20cdc89 | 2006-10-02 02:17:07 -0700 | [diff] [blame] | 2454 | /* |
| 2455 | * Yes, it does not scale. And it should not. Don't add |
| 2456 | * new entries into /proc/<tgid>/ without very good reasons. |
| 2457 | */ |
Alexey Dobriyan | d5a572a | 2019-03-11 23:28:51 -0700 | [diff] [blame] | 2458 | for (; p < end; p++) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2459 | if (p->len != dentry->d_name.len) |
| 2460 | continue; |
Alexey Dobriyan | 26b9513 | 2018-06-14 15:27:17 -0700 | [diff] [blame] | 2461 | if (!memcmp(dentry->d_name.name, p->name, p->len)) { |
| 2462 | res = proc_pident_instantiate(dentry, task, p); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2463 | break; |
Alexey Dobriyan | 26b9513 | 2018-06-14 15:27:17 -0700 | [diff] [blame] | 2464 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2465 | } |
Eric W. Biederman | 99f8955 | 2006-06-26 00:25:55 -0700 | [diff] [blame] | 2466 | put_task_struct(task); |
| 2467 | out_no_task: |
Al Viro | 0168b9e | 2018-05-03 09:21:05 -0400 | [diff] [blame] | 2468 | return res; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2469 | } |
| 2470 | |
Al Viro | f0c3b50 | 2013-05-16 12:07:31 -0400 | [diff] [blame] | 2471 | static int proc_pident_readdir(struct file *file, struct dir_context *ctx, |
Eric Dumazet | c5141e6 | 2007-05-08 00:26:15 -0700 | [diff] [blame] | 2472 | const struct pid_entry *ents, unsigned int nents) |
Eric W. Biederman | 28a6d67 | 2006-10-02 02:17:05 -0700 | [diff] [blame] | 2473 | { |
Al Viro | f0c3b50 | 2013-05-16 12:07:31 -0400 | [diff] [blame] | 2474 | struct task_struct *task = get_proc_task(file_inode(file)); |
| 2475 | const struct pid_entry *p; |
Eric W. Biederman | 28a6d67 | 2006-10-02 02:17:05 -0700 | [diff] [blame] | 2476 | |
Eric W. Biederman | 28a6d67 | 2006-10-02 02:17:05 -0700 | [diff] [blame] | 2477 | if (!task) |
Al Viro | f0c3b50 | 2013-05-16 12:07:31 -0400 | [diff] [blame] | 2478 | return -ENOENT; |
Eric W. Biederman | 28a6d67 | 2006-10-02 02:17:05 -0700 | [diff] [blame] | 2479 | |
Al Viro | f0c3b50 | 2013-05-16 12:07:31 -0400 | [diff] [blame] | 2480 | if (!dir_emit_dots(file, ctx)) |
| 2481 | goto out; |
| 2482 | |
| 2483 | if (ctx->pos >= nents + 2) |
| 2484 | goto out; |
| 2485 | |
Alexey Dobriyan | bac5f5d | 2016-12-12 16:45:28 -0800 | [diff] [blame] | 2486 | for (p = ents + (ctx->pos - 2); p < ents + nents; p++) { |
Al Viro | f0c3b50 | 2013-05-16 12:07:31 -0400 | [diff] [blame] | 2487 | if (!proc_fill_cache(file, ctx, p->name, p->len, |
| 2488 | proc_pident_instantiate, task, p)) |
| 2489 | break; |
| 2490 | ctx->pos++; |
Eric W. Biederman | 28a6d67 | 2006-10-02 02:17:05 -0700 | [diff] [blame] | 2491 | } |
Eric W. Biederman | 28a6d67 | 2006-10-02 02:17:05 -0700 | [diff] [blame] | 2492 | out: |
Eric W. Biederman | 61a2878 | 2006-10-02 02:18:49 -0700 | [diff] [blame] | 2493 | put_task_struct(task); |
Al Viro | f0c3b50 | 2013-05-16 12:07:31 -0400 | [diff] [blame] | 2494 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2495 | } |
| 2496 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2497 | #ifdef CONFIG_SECURITY |
Eric W. Biederman | 28a6d67 | 2006-10-02 02:17:05 -0700 | [diff] [blame] | 2498 | static ssize_t proc_pid_attr_read(struct file * file, char __user * buf, |
| 2499 | size_t count, loff_t *ppos) |
| 2500 | { |
Al Viro | 496ad9a | 2013-01-23 17:07:38 -0500 | [diff] [blame] | 2501 | struct inode * inode = file_inode(file); |
Al Viro | 04ff970 | 2007-03-12 16:17:58 +0000 | [diff] [blame] | 2502 | char *p = NULL; |
Eric W. Biederman | 28a6d67 | 2006-10-02 02:17:05 -0700 | [diff] [blame] | 2503 | ssize_t length; |
| 2504 | struct task_struct *task = get_proc_task(inode); |
| 2505 | |
Eric W. Biederman | 28a6d67 | 2006-10-02 02:17:05 -0700 | [diff] [blame] | 2506 | if (!task) |
Al Viro | 04ff970 | 2007-03-12 16:17:58 +0000 | [diff] [blame] | 2507 | return -ESRCH; |
Eric W. Biederman | 28a6d67 | 2006-10-02 02:17:05 -0700 | [diff] [blame] | 2508 | |
Casey Schaufler | 6d9c939 | 2018-09-21 17:16:59 -0700 | [diff] [blame] | 2509 | length = security_getprocattr(task, PROC_I(inode)->op.lsm, |
Josef "Jeff" Sipek | 2fddfee | 2006-12-08 02:36:36 -0800 | [diff] [blame] | 2510 | (char*)file->f_path.dentry->d_name.name, |
Al Viro | 04ff970 | 2007-03-12 16:17:58 +0000 | [diff] [blame] | 2511 | &p); |
Eric W. Biederman | 28a6d67 | 2006-10-02 02:17:05 -0700 | [diff] [blame] | 2512 | put_task_struct(task); |
Al Viro | 04ff970 | 2007-03-12 16:17:58 +0000 | [diff] [blame] | 2513 | if (length > 0) |
| 2514 | length = simple_read_from_buffer(buf, count, ppos, p, length); |
| 2515 | kfree(p); |
Eric W. Biederman | 28a6d67 | 2006-10-02 02:17:05 -0700 | [diff] [blame] | 2516 | return length; |
| 2517 | } |
| 2518 | |
| 2519 | static ssize_t proc_pid_attr_write(struct file * file, const char __user * buf, |
| 2520 | size_t count, loff_t *ppos) |
| 2521 | { |
Al Viro | 496ad9a | 2013-01-23 17:07:38 -0500 | [diff] [blame] | 2522 | struct inode * inode = file_inode(file); |
Alexey Dobriyan | 41089b6 | 2018-08-21 21:54:30 -0700 | [diff] [blame] | 2523 | struct task_struct *task; |
Al Viro | bb646cd | 2015-12-24 00:16:30 -0500 | [diff] [blame] | 2524 | void *page; |
Alexey Dobriyan | 41089b6 | 2018-08-21 21:54:30 -0700 | [diff] [blame] | 2525 | int rv; |
Eric W. Biederman | 28a6d67 | 2006-10-02 02:17:05 -0700 | [diff] [blame] | 2526 | |
Alexey Dobriyan | 41089b6 | 2018-08-21 21:54:30 -0700 | [diff] [blame] | 2527 | rcu_read_lock(); |
| 2528 | task = pid_task(proc_pid(inode), PIDTYPE_PID); |
| 2529 | if (!task) { |
| 2530 | rcu_read_unlock(); |
| 2531 | return -ESRCH; |
| 2532 | } |
Stephen Smalley | b21507e | 2017-01-09 10:07:31 -0500 | [diff] [blame] | 2533 | /* A task may only write its own attributes. */ |
Alexey Dobriyan | 41089b6 | 2018-08-21 21:54:30 -0700 | [diff] [blame] | 2534 | if (current != task) { |
| 2535 | rcu_read_unlock(); |
| 2536 | return -EACCES; |
| 2537 | } |
Paul Moore | 35a196b | 2019-04-19 14:55:12 -0400 | [diff] [blame] | 2538 | /* Prevent changes to overridden credentials. */ |
| 2539 | if (current_cred() != current_real_cred()) { |
| 2540 | rcu_read_unlock(); |
| 2541 | return -EBUSY; |
| 2542 | } |
Alexey Dobriyan | 41089b6 | 2018-08-21 21:54:30 -0700 | [diff] [blame] | 2543 | rcu_read_unlock(); |
Stephen Smalley | b21507e | 2017-01-09 10:07:31 -0500 | [diff] [blame] | 2544 | |
Eric W. Biederman | 28a6d67 | 2006-10-02 02:17:05 -0700 | [diff] [blame] | 2545 | if (count > PAGE_SIZE) |
| 2546 | count = PAGE_SIZE; |
| 2547 | |
| 2548 | /* No partial writes. */ |
Eric W. Biederman | 28a6d67 | 2006-10-02 02:17:05 -0700 | [diff] [blame] | 2549 | if (*ppos != 0) |
Alexey Dobriyan | 41089b6 | 2018-08-21 21:54:30 -0700 | [diff] [blame] | 2550 | return -EINVAL; |
Eric W. Biederman | 28a6d67 | 2006-10-02 02:17:05 -0700 | [diff] [blame] | 2551 | |
Al Viro | bb646cd | 2015-12-24 00:16:30 -0500 | [diff] [blame] | 2552 | page = memdup_user(buf, count); |
| 2553 | if (IS_ERR(page)) { |
Alexey Dobriyan | 41089b6 | 2018-08-21 21:54:30 -0700 | [diff] [blame] | 2554 | rv = PTR_ERR(page); |
Eric W. Biederman | 28a6d67 | 2006-10-02 02:17:05 -0700 | [diff] [blame] | 2555 | goto out; |
Al Viro | bb646cd | 2015-12-24 00:16:30 -0500 | [diff] [blame] | 2556 | } |
Eric W. Biederman | 28a6d67 | 2006-10-02 02:17:05 -0700 | [diff] [blame] | 2557 | |
David Howells | 107db7c | 2009-05-08 13:55:27 +0100 | [diff] [blame] | 2558 | /* Guard against adverse ptrace interaction */ |
Alexey Dobriyan | 41089b6 | 2018-08-21 21:54:30 -0700 | [diff] [blame] | 2559 | rv = mutex_lock_interruptible(¤t->signal->cred_guard_mutex); |
| 2560 | if (rv < 0) |
David Howells | 107db7c | 2009-05-08 13:55:27 +0100 | [diff] [blame] | 2561 | goto out_free; |
| 2562 | |
Casey Schaufler | 6d9c939 | 2018-09-21 17:16:59 -0700 | [diff] [blame] | 2563 | rv = security_setprocattr(PROC_I(inode)->op.lsm, |
| 2564 | file->f_path.dentry->d_name.name, page, |
| 2565 | count); |
Stephen Smalley | b21507e | 2017-01-09 10:07:31 -0500 | [diff] [blame] | 2566 | mutex_unlock(¤t->signal->cred_guard_mutex); |
Eric W. Biederman | 28a6d67 | 2006-10-02 02:17:05 -0700 | [diff] [blame] | 2567 | out_free: |
Al Viro | bb646cd | 2015-12-24 00:16:30 -0500 | [diff] [blame] | 2568 | kfree(page); |
Eric W. Biederman | 28a6d67 | 2006-10-02 02:17:05 -0700 | [diff] [blame] | 2569 | out: |
Alexey Dobriyan | 41089b6 | 2018-08-21 21:54:30 -0700 | [diff] [blame] | 2570 | return rv; |
Eric W. Biederman | 28a6d67 | 2006-10-02 02:17:05 -0700 | [diff] [blame] | 2571 | } |
| 2572 | |
Arjan van de Ven | 00977a5 | 2007-02-12 00:55:34 -0800 | [diff] [blame] | 2573 | static const struct file_operations proc_pid_attr_operations = { |
Eric W. Biederman | 28a6d67 | 2006-10-02 02:17:05 -0700 | [diff] [blame] | 2574 | .read = proc_pid_attr_read, |
| 2575 | .write = proc_pid_attr_write, |
Arnd Bergmann | 87df842 | 2010-03-17 23:06:02 +0100 | [diff] [blame] | 2576 | .llseek = generic_file_llseek, |
Eric W. Biederman | 28a6d67 | 2006-10-02 02:17:05 -0700 | [diff] [blame] | 2577 | }; |
| 2578 | |
Casey Schaufler | 6d9c939 | 2018-09-21 17:16:59 -0700 | [diff] [blame] | 2579 | #define LSM_DIR_OPS(LSM) \ |
| 2580 | static int proc_##LSM##_attr_dir_iterate(struct file *filp, \ |
| 2581 | struct dir_context *ctx) \ |
| 2582 | { \ |
| 2583 | return proc_pident_readdir(filp, ctx, \ |
| 2584 | LSM##_attr_dir_stuff, \ |
| 2585 | ARRAY_SIZE(LSM##_attr_dir_stuff)); \ |
| 2586 | } \ |
| 2587 | \ |
| 2588 | static const struct file_operations proc_##LSM##_attr_dir_ops = { \ |
| 2589 | .read = generic_read_dir, \ |
| 2590 | .iterate = proc_##LSM##_attr_dir_iterate, \ |
| 2591 | .llseek = default_llseek, \ |
| 2592 | }; \ |
| 2593 | \ |
| 2594 | static struct dentry *proc_##LSM##_attr_dir_lookup(struct inode *dir, \ |
| 2595 | struct dentry *dentry, unsigned int flags) \ |
| 2596 | { \ |
| 2597 | return proc_pident_lookup(dir, dentry, \ |
| 2598 | LSM##_attr_dir_stuff, \ |
Alexey Dobriyan | d5a572a | 2019-03-11 23:28:51 -0700 | [diff] [blame] | 2599 | LSM##_attr_dir_stuff + ARRAY_SIZE(LSM##_attr_dir_stuff)); \ |
Casey Schaufler | 6d9c939 | 2018-09-21 17:16:59 -0700 | [diff] [blame] | 2600 | } \ |
| 2601 | \ |
| 2602 | static const struct inode_operations proc_##LSM##_attr_dir_inode_ops = { \ |
| 2603 | .lookup = proc_##LSM##_attr_dir_lookup, \ |
| 2604 | .getattr = pid_getattr, \ |
| 2605 | .setattr = proc_setattr, \ |
| 2606 | } |
| 2607 | |
| 2608 | #ifdef CONFIG_SECURITY_SMACK |
| 2609 | static const struct pid_entry smack_attr_dir_stuff[] = { |
| 2610 | ATTR("smack", "current", 0666), |
| 2611 | }; |
| 2612 | LSM_DIR_OPS(smack); |
| 2613 | #endif |
| 2614 | |
Eric Dumazet | c5141e6 | 2007-05-08 00:26:15 -0700 | [diff] [blame] | 2615 | static const struct pid_entry attr_dir_stuff[] = { |
Casey Schaufler | 6d9c939 | 2018-09-21 17:16:59 -0700 | [diff] [blame] | 2616 | ATTR(NULL, "current", 0666), |
| 2617 | ATTR(NULL, "prev", 0444), |
| 2618 | ATTR(NULL, "exec", 0666), |
| 2619 | ATTR(NULL, "fscreate", 0666), |
| 2620 | ATTR(NULL, "keycreate", 0666), |
| 2621 | ATTR(NULL, "sockcreate", 0666), |
| 2622 | #ifdef CONFIG_SECURITY_SMACK |
| 2623 | DIR("smack", 0555, |
| 2624 | proc_smack_attr_dir_inode_ops, proc_smack_attr_dir_ops), |
| 2625 | #endif |
Eric W. Biederman | 28a6d67 | 2006-10-02 02:17:05 -0700 | [diff] [blame] | 2626 | }; |
| 2627 | |
Al Viro | f0c3b50 | 2013-05-16 12:07:31 -0400 | [diff] [blame] | 2628 | static int proc_attr_dir_readdir(struct file *file, struct dir_context *ctx) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2629 | { |
Al Viro | f0c3b50 | 2013-05-16 12:07:31 -0400 | [diff] [blame] | 2630 | return proc_pident_readdir(file, ctx, |
| 2631 | attr_dir_stuff, ARRAY_SIZE(attr_dir_stuff)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2632 | } |
| 2633 | |
Arjan van de Ven | 00977a5 | 2007-02-12 00:55:34 -0800 | [diff] [blame] | 2634 | static const struct file_operations proc_attr_dir_operations = { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2635 | .read = generic_read_dir, |
Al Viro | f50752e | 2016-04-20 17:13:54 -0400 | [diff] [blame] | 2636 | .iterate_shared = proc_attr_dir_readdir, |
| 2637 | .llseek = generic_file_llseek, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2638 | }; |
| 2639 | |
Eric W. Biederman | 72d9dcf | 2006-10-02 02:18:50 -0700 | [diff] [blame] | 2640 | static struct dentry *proc_attr_dir_lookup(struct inode *dir, |
Al Viro | 00cd8dd | 2012-06-10 17:13:09 -0400 | [diff] [blame] | 2641 | struct dentry *dentry, unsigned int flags) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2642 | { |
Eric W. Biederman | 7bcd6b0 | 2006-10-02 02:18:56 -0700 | [diff] [blame] | 2643 | return proc_pident_lookup(dir, dentry, |
Alexey Dobriyan | d5a572a | 2019-03-11 23:28:51 -0700 | [diff] [blame] | 2644 | attr_dir_stuff, |
| 2645 | attr_dir_stuff + ARRAY_SIZE(attr_dir_stuff)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2646 | } |
| 2647 | |
Arjan van de Ven | c5ef1c4 | 2007-02-12 00:55:40 -0800 | [diff] [blame] | 2648 | static const struct inode_operations proc_attr_dir_inode_operations = { |
Eric W. Biederman | 72d9dcf | 2006-10-02 02:18:50 -0700 | [diff] [blame] | 2649 | .lookup = proc_attr_dir_lookup, |
Eric W. Biederman | 99f8955 | 2006-06-26 00:25:55 -0700 | [diff] [blame] | 2650 | .getattr = pid_getattr, |
Linus Torvalds | 6d76fa5 | 2006-07-15 12:26:45 -0700 | [diff] [blame] | 2651 | .setattr = proc_setattr, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2652 | }; |
| 2653 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2654 | #endif |
| 2655 | |
Christoph Hellwig | 698ba7b | 2009-12-15 16:47:37 -0800 | [diff] [blame] | 2656 | #ifdef CONFIG_ELF_CORE |
Kawai, Hidehiro | 3cb4a0b | 2007-07-19 01:48:28 -0700 | [diff] [blame] | 2657 | static ssize_t proc_coredump_filter_read(struct file *file, char __user *buf, |
| 2658 | size_t count, loff_t *ppos) |
| 2659 | { |
Al Viro | 496ad9a | 2013-01-23 17:07:38 -0500 | [diff] [blame] | 2660 | struct task_struct *task = get_proc_task(file_inode(file)); |
Kawai, Hidehiro | 3cb4a0b | 2007-07-19 01:48:28 -0700 | [diff] [blame] | 2661 | struct mm_struct *mm; |
| 2662 | char buffer[PROC_NUMBUF]; |
| 2663 | size_t len; |
| 2664 | int ret; |
| 2665 | |
| 2666 | if (!task) |
| 2667 | return -ESRCH; |
| 2668 | |
| 2669 | ret = 0; |
| 2670 | mm = get_task_mm(task); |
| 2671 | if (mm) { |
| 2672 | len = snprintf(buffer, sizeof(buffer), "%08lx\n", |
| 2673 | ((mm->flags & MMF_DUMP_FILTER_MASK) >> |
| 2674 | MMF_DUMP_FILTER_SHIFT)); |
| 2675 | mmput(mm); |
| 2676 | ret = simple_read_from_buffer(buf, count, ppos, buffer, len); |
| 2677 | } |
| 2678 | |
| 2679 | put_task_struct(task); |
| 2680 | |
| 2681 | return ret; |
| 2682 | } |
| 2683 | |
| 2684 | static ssize_t proc_coredump_filter_write(struct file *file, |
| 2685 | const char __user *buf, |
| 2686 | size_t count, |
| 2687 | loff_t *ppos) |
| 2688 | { |
| 2689 | struct task_struct *task; |
| 2690 | struct mm_struct *mm; |
Kawai, Hidehiro | 3cb4a0b | 2007-07-19 01:48:28 -0700 | [diff] [blame] | 2691 | unsigned int val; |
| 2692 | int ret; |
| 2693 | int i; |
| 2694 | unsigned long mask; |
| 2695 | |
Alexey Dobriyan | 774636e | 2015-09-09 15:36:59 -0700 | [diff] [blame] | 2696 | ret = kstrtouint_from_user(buf, count, 0, &val); |
| 2697 | if (ret < 0) |
| 2698 | return ret; |
Kawai, Hidehiro | 3cb4a0b | 2007-07-19 01:48:28 -0700 | [diff] [blame] | 2699 | |
| 2700 | ret = -ESRCH; |
Al Viro | 496ad9a | 2013-01-23 17:07:38 -0500 | [diff] [blame] | 2701 | task = get_proc_task(file_inode(file)); |
Kawai, Hidehiro | 3cb4a0b | 2007-07-19 01:48:28 -0700 | [diff] [blame] | 2702 | if (!task) |
| 2703 | goto out_no_task; |
| 2704 | |
Kawai, Hidehiro | 3cb4a0b | 2007-07-19 01:48:28 -0700 | [diff] [blame] | 2705 | mm = get_task_mm(task); |
| 2706 | if (!mm) |
| 2707 | goto out_no_mm; |
Colin Ian King | 41a0c249 | 2015-12-18 14:22:01 -0800 | [diff] [blame] | 2708 | ret = 0; |
Kawai, Hidehiro | 3cb4a0b | 2007-07-19 01:48:28 -0700 | [diff] [blame] | 2709 | |
| 2710 | for (i = 0, mask = 1; i < MMF_DUMP_FILTER_BITS; i++, mask <<= 1) { |
| 2711 | if (val & mask) |
| 2712 | set_bit(i + MMF_DUMP_FILTER_SHIFT, &mm->flags); |
| 2713 | else |
| 2714 | clear_bit(i + MMF_DUMP_FILTER_SHIFT, &mm->flags); |
| 2715 | } |
| 2716 | |
| 2717 | mmput(mm); |
| 2718 | out_no_mm: |
| 2719 | put_task_struct(task); |
| 2720 | out_no_task: |
Alexey Dobriyan | 774636e | 2015-09-09 15:36:59 -0700 | [diff] [blame] | 2721 | if (ret < 0) |
| 2722 | return ret; |
| 2723 | return count; |
Kawai, Hidehiro | 3cb4a0b | 2007-07-19 01:48:28 -0700 | [diff] [blame] | 2724 | } |
| 2725 | |
| 2726 | static const struct file_operations proc_coredump_filter_operations = { |
| 2727 | .read = proc_coredump_filter_read, |
| 2728 | .write = proc_coredump_filter_write, |
Arnd Bergmann | 87df842 | 2010-03-17 23:06:02 +0100 | [diff] [blame] | 2729 | .llseek = generic_file_llseek, |
Kawai, Hidehiro | 3cb4a0b | 2007-07-19 01:48:28 -0700 | [diff] [blame] | 2730 | }; |
| 2731 | #endif |
| 2732 | |
Andrew Morton | aba76fd | 2006-12-10 02:19:48 -0800 | [diff] [blame] | 2733 | #ifdef CONFIG_TASK_IO_ACCOUNTING |
Alexey Dobriyan | 19aadc9 | 2014-08-08 14:21:50 -0700 | [diff] [blame] | 2734 | static int do_io_accounting(struct task_struct *task, struct seq_file *m, int whole) |
Andrew Morton | aba76fd | 2006-12-10 02:19:48 -0800 | [diff] [blame] | 2735 | { |
Andrea Righi | 940389b | 2008-07-28 00:48:12 +0200 | [diff] [blame] | 2736 | struct task_io_accounting acct = task->ioac; |
Andrea Righi | 5995477 | 2008-07-27 17:29:15 +0200 | [diff] [blame] | 2737 | unsigned long flags; |
Vasiliy Kulikov | 293eb1e | 2011-07-26 16:08:38 -0700 | [diff] [blame] | 2738 | int result; |
Andrea Righi | 297c5d9 | 2008-07-25 01:48:49 -0700 | [diff] [blame] | 2739 | |
Vasiliy Kulikov | 293eb1e | 2011-07-26 16:08:38 -0700 | [diff] [blame] | 2740 | result = mutex_lock_killable(&task->signal->cred_guard_mutex); |
| 2741 | if (result) |
| 2742 | return result; |
| 2743 | |
Jann Horn | caaee62 | 2016-01-20 15:00:04 -0800 | [diff] [blame] | 2744 | if (!ptrace_may_access(task, PTRACE_MODE_READ_FSCREDS)) { |
Vasiliy Kulikov | 293eb1e | 2011-07-26 16:08:38 -0700 | [diff] [blame] | 2745 | result = -EACCES; |
| 2746 | goto out_unlock; |
| 2747 | } |
Vasiliy Kulikov | 1d1221f | 2011-06-24 16:08:38 +0400 | [diff] [blame] | 2748 | |
Andrea Righi | 5995477 | 2008-07-27 17:29:15 +0200 | [diff] [blame] | 2749 | if (whole && lock_task_sighand(task, &flags)) { |
| 2750 | struct task_struct *t = task; |
Andrea Righi | b2d002d | 2008-07-26 15:22:27 -0700 | [diff] [blame] | 2751 | |
Andrea Righi | 5995477 | 2008-07-27 17:29:15 +0200 | [diff] [blame] | 2752 | task_io_accounting_add(&acct, &task->signal->ioac); |
| 2753 | while_each_thread(task, t) |
| 2754 | task_io_accounting_add(&acct, &t->ioac); |
Andrea Righi | 297c5d9 | 2008-07-25 01:48:49 -0700 | [diff] [blame] | 2755 | |
Andrea Righi | 5995477 | 2008-07-27 17:29:15 +0200 | [diff] [blame] | 2756 | unlock_task_sighand(task, &flags); |
Andrea Righi | 297c5d9 | 2008-07-25 01:48:49 -0700 | [diff] [blame] | 2757 | } |
Joe Perches | 25ce319 | 2015-04-15 16:18:17 -0700 | [diff] [blame] | 2758 | seq_printf(m, |
| 2759 | "rchar: %llu\n" |
| 2760 | "wchar: %llu\n" |
| 2761 | "syscr: %llu\n" |
| 2762 | "syscw: %llu\n" |
| 2763 | "read_bytes: %llu\n" |
| 2764 | "write_bytes: %llu\n" |
| 2765 | "cancelled_write_bytes: %llu\n", |
| 2766 | (unsigned long long)acct.rchar, |
| 2767 | (unsigned long long)acct.wchar, |
| 2768 | (unsigned long long)acct.syscr, |
| 2769 | (unsigned long long)acct.syscw, |
| 2770 | (unsigned long long)acct.read_bytes, |
| 2771 | (unsigned long long)acct.write_bytes, |
| 2772 | (unsigned long long)acct.cancelled_write_bytes); |
| 2773 | result = 0; |
| 2774 | |
Vasiliy Kulikov | 293eb1e | 2011-07-26 16:08:38 -0700 | [diff] [blame] | 2775 | out_unlock: |
| 2776 | mutex_unlock(&task->signal->cred_guard_mutex); |
| 2777 | return result; |
Andrew Morton | aba76fd | 2006-12-10 02:19:48 -0800 | [diff] [blame] | 2778 | } |
Andrea Righi | 297c5d9 | 2008-07-25 01:48:49 -0700 | [diff] [blame] | 2779 | |
Alexey Dobriyan | 19aadc9 | 2014-08-08 14:21:50 -0700 | [diff] [blame] | 2780 | static int proc_tid_io_accounting(struct seq_file *m, struct pid_namespace *ns, |
| 2781 | struct pid *pid, struct task_struct *task) |
Andrea Righi | 297c5d9 | 2008-07-25 01:48:49 -0700 | [diff] [blame] | 2782 | { |
Alexey Dobriyan | 19aadc9 | 2014-08-08 14:21:50 -0700 | [diff] [blame] | 2783 | return do_io_accounting(task, m, 0); |
Andrea Righi | 297c5d9 | 2008-07-25 01:48:49 -0700 | [diff] [blame] | 2784 | } |
| 2785 | |
Alexey Dobriyan | 19aadc9 | 2014-08-08 14:21:50 -0700 | [diff] [blame] | 2786 | static int proc_tgid_io_accounting(struct seq_file *m, struct pid_namespace *ns, |
| 2787 | struct pid *pid, struct task_struct *task) |
Andrea Righi | 297c5d9 | 2008-07-25 01:48:49 -0700 | [diff] [blame] | 2788 | { |
Alexey Dobriyan | 19aadc9 | 2014-08-08 14:21:50 -0700 | [diff] [blame] | 2789 | return do_io_accounting(task, m, 1); |
Andrea Righi | 297c5d9 | 2008-07-25 01:48:49 -0700 | [diff] [blame] | 2790 | } |
| 2791 | #endif /* CONFIG_TASK_IO_ACCOUNTING */ |
Andrew Morton | aba76fd | 2006-12-10 02:19:48 -0800 | [diff] [blame] | 2792 | |
Eric W. Biederman | 22d917d | 2011-11-17 00:11:58 -0800 | [diff] [blame] | 2793 | #ifdef CONFIG_USER_NS |
| 2794 | static int proc_id_map_open(struct inode *inode, struct file *file, |
Fabian Frederick | ccf94f1 | 2014-08-08 14:21:22 -0700 | [diff] [blame] | 2795 | const struct seq_operations *seq_ops) |
Eric W. Biederman | 22d917d | 2011-11-17 00:11:58 -0800 | [diff] [blame] | 2796 | { |
| 2797 | struct user_namespace *ns = NULL; |
| 2798 | struct task_struct *task; |
| 2799 | struct seq_file *seq; |
| 2800 | int ret = -EINVAL; |
| 2801 | |
| 2802 | task = get_proc_task(inode); |
| 2803 | if (task) { |
| 2804 | rcu_read_lock(); |
| 2805 | ns = get_user_ns(task_cred_xxx(task, user_ns)); |
| 2806 | rcu_read_unlock(); |
| 2807 | put_task_struct(task); |
| 2808 | } |
| 2809 | if (!ns) |
| 2810 | goto err; |
| 2811 | |
| 2812 | ret = seq_open(file, seq_ops); |
| 2813 | if (ret) |
| 2814 | goto err_put_ns; |
| 2815 | |
| 2816 | seq = file->private_data; |
| 2817 | seq->private = ns; |
| 2818 | |
| 2819 | return 0; |
| 2820 | err_put_ns: |
| 2821 | put_user_ns(ns); |
| 2822 | err: |
| 2823 | return ret; |
| 2824 | } |
| 2825 | |
| 2826 | static int proc_id_map_release(struct inode *inode, struct file *file) |
| 2827 | { |
| 2828 | struct seq_file *seq = file->private_data; |
| 2829 | struct user_namespace *ns = seq->private; |
| 2830 | put_user_ns(ns); |
| 2831 | return seq_release(inode, file); |
| 2832 | } |
| 2833 | |
| 2834 | static int proc_uid_map_open(struct inode *inode, struct file *file) |
| 2835 | { |
| 2836 | return proc_id_map_open(inode, file, &proc_uid_seq_operations); |
| 2837 | } |
| 2838 | |
| 2839 | static int proc_gid_map_open(struct inode *inode, struct file *file) |
| 2840 | { |
| 2841 | return proc_id_map_open(inode, file, &proc_gid_seq_operations); |
| 2842 | } |
| 2843 | |
Eric W. Biederman | f76d207 | 2012-08-30 01:24:05 -0700 | [diff] [blame] | 2844 | static int proc_projid_map_open(struct inode *inode, struct file *file) |
| 2845 | { |
| 2846 | return proc_id_map_open(inode, file, &proc_projid_seq_operations); |
| 2847 | } |
| 2848 | |
Eric W. Biederman | 22d917d | 2011-11-17 00:11:58 -0800 | [diff] [blame] | 2849 | static const struct file_operations proc_uid_map_operations = { |
| 2850 | .open = proc_uid_map_open, |
| 2851 | .write = proc_uid_map_write, |
| 2852 | .read = seq_read, |
| 2853 | .llseek = seq_lseek, |
| 2854 | .release = proc_id_map_release, |
| 2855 | }; |
| 2856 | |
| 2857 | static const struct file_operations proc_gid_map_operations = { |
| 2858 | .open = proc_gid_map_open, |
| 2859 | .write = proc_gid_map_write, |
| 2860 | .read = seq_read, |
| 2861 | .llseek = seq_lseek, |
| 2862 | .release = proc_id_map_release, |
| 2863 | }; |
Eric W. Biederman | f76d207 | 2012-08-30 01:24:05 -0700 | [diff] [blame] | 2864 | |
| 2865 | static const struct file_operations proc_projid_map_operations = { |
| 2866 | .open = proc_projid_map_open, |
| 2867 | .write = proc_projid_map_write, |
| 2868 | .read = seq_read, |
| 2869 | .llseek = seq_lseek, |
| 2870 | .release = proc_id_map_release, |
| 2871 | }; |
Eric W. Biederman | 9cc4651 | 2014-12-02 12:27:26 -0600 | [diff] [blame] | 2872 | |
| 2873 | static int proc_setgroups_open(struct inode *inode, struct file *file) |
| 2874 | { |
| 2875 | struct user_namespace *ns = NULL; |
| 2876 | struct task_struct *task; |
| 2877 | int ret; |
| 2878 | |
| 2879 | ret = -ESRCH; |
| 2880 | task = get_proc_task(inode); |
| 2881 | if (task) { |
| 2882 | rcu_read_lock(); |
| 2883 | ns = get_user_ns(task_cred_xxx(task, user_ns)); |
| 2884 | rcu_read_unlock(); |
| 2885 | put_task_struct(task); |
| 2886 | } |
| 2887 | if (!ns) |
| 2888 | goto err; |
| 2889 | |
| 2890 | if (file->f_mode & FMODE_WRITE) { |
| 2891 | ret = -EACCES; |
| 2892 | if (!ns_capable(ns, CAP_SYS_ADMIN)) |
| 2893 | goto err_put_ns; |
| 2894 | } |
| 2895 | |
| 2896 | ret = single_open(file, &proc_setgroups_show, ns); |
| 2897 | if (ret) |
| 2898 | goto err_put_ns; |
| 2899 | |
| 2900 | return 0; |
| 2901 | err_put_ns: |
| 2902 | put_user_ns(ns); |
| 2903 | err: |
| 2904 | return ret; |
| 2905 | } |
| 2906 | |
| 2907 | static int proc_setgroups_release(struct inode *inode, struct file *file) |
| 2908 | { |
| 2909 | struct seq_file *seq = file->private_data; |
| 2910 | struct user_namespace *ns = seq->private; |
| 2911 | int ret = single_release(inode, file); |
| 2912 | put_user_ns(ns); |
| 2913 | return ret; |
| 2914 | } |
| 2915 | |
| 2916 | static const struct file_operations proc_setgroups_operations = { |
| 2917 | .open = proc_setgroups_open, |
| 2918 | .write = proc_setgroups_write, |
| 2919 | .read = seq_read, |
| 2920 | .llseek = seq_lseek, |
| 2921 | .release = proc_setgroups_release, |
| 2922 | }; |
Eric W. Biederman | 22d917d | 2011-11-17 00:11:58 -0800 | [diff] [blame] | 2923 | #endif /* CONFIG_USER_NS */ |
| 2924 | |
Kees Cook | 4783072 | 2008-10-06 03:11:58 +0400 | [diff] [blame] | 2925 | static int proc_pid_personality(struct seq_file *m, struct pid_namespace *ns, |
| 2926 | struct pid *pid, struct task_struct *task) |
| 2927 | { |
Al Viro | a9712bc | 2011-03-23 15:52:50 -0400 | [diff] [blame] | 2928 | int err = lock_trace(task); |
| 2929 | if (!err) { |
| 2930 | seq_printf(m, "%08x\n", task->personality); |
| 2931 | unlock_trace(task); |
| 2932 | } |
| 2933 | return err; |
Kees Cook | 4783072 | 2008-10-06 03:11:58 +0400 | [diff] [blame] | 2934 | } |
| 2935 | |
Josh Poimboeuf | 7c23b33 | 2017-02-13 19:42:41 -0600 | [diff] [blame] | 2936 | #ifdef CONFIG_LIVEPATCH |
| 2937 | static int proc_pid_patch_state(struct seq_file *m, struct pid_namespace *ns, |
| 2938 | struct pid *pid, struct task_struct *task) |
| 2939 | { |
| 2940 | seq_printf(m, "%d\n", task->patch_state); |
| 2941 | return 0; |
| 2942 | } |
| 2943 | #endif /* CONFIG_LIVEPATCH */ |
| 2944 | |
Alexander Popov | c8d1262 | 2018-08-17 01:17:01 +0300 | [diff] [blame] | 2945 | #ifdef CONFIG_STACKLEAK_METRICS |
| 2946 | static int proc_stack_depth(struct seq_file *m, struct pid_namespace *ns, |
| 2947 | struct pid *pid, struct task_struct *task) |
| 2948 | { |
| 2949 | unsigned long prev_depth = THREAD_SIZE - |
| 2950 | (task->prev_lowest_stack & (THREAD_SIZE - 1)); |
| 2951 | unsigned long depth = THREAD_SIZE - |
| 2952 | (task->lowest_stack & (THREAD_SIZE - 1)); |
| 2953 | |
| 2954 | seq_printf(m, "previous stack depth: %lu\nstack depth: %lu\n", |
| 2955 | prev_depth, depth); |
| 2956 | return 0; |
| 2957 | } |
| 2958 | #endif /* CONFIG_STACKLEAK_METRICS */ |
| 2959 | |
Eric W. Biederman | 801199c | 2006-10-02 02:18:48 -0700 | [diff] [blame] | 2960 | /* |
Eric W. Biederman | 28a6d67 | 2006-10-02 02:17:05 -0700 | [diff] [blame] | 2961 | * Thread groups |
| 2962 | */ |
Arjan van de Ven | 00977a5 | 2007-02-12 00:55:34 -0800 | [diff] [blame] | 2963 | static const struct file_operations proc_task_operations; |
Arjan van de Ven | c5ef1c4 | 2007-02-12 00:55:40 -0800 | [diff] [blame] | 2964 | static const struct inode_operations proc_task_inode_operations; |
Eric W. Biederman | 20cdc89 | 2006-10-02 02:17:07 -0700 | [diff] [blame] | 2965 | |
Eric Dumazet | c5141e6 | 2007-05-08 00:26:15 -0700 | [diff] [blame] | 2966 | static const struct pid_entry tgid_base_stuff[] = { |
Alexey Dobriyan | 631f9c1 | 2008-11-10 01:32:52 +0300 | [diff] [blame] | 2967 | DIR("task", S_IRUGO|S_IXUGO, proc_task_inode_operations, proc_task_operations), |
| 2968 | DIR("fd", S_IRUSR|S_IXUSR, proc_fd_inode_operations, proc_fd_operations), |
Pavel Emelyanov | 640708a | 2012-01-10 15:11:23 -0800 | [diff] [blame] | 2969 | DIR("map_files", S_IRUSR|S_IXUSR, proc_map_files_inode_operations, proc_map_files_operations), |
Alexey Dobriyan | 631f9c1 | 2008-11-10 01:32:52 +0300 | [diff] [blame] | 2970 | DIR("fdinfo", S_IRUSR|S_IXUSR, proc_fdinfo_inode_operations, proc_fdinfo_operations), |
Eric W. Biederman | 6b4e306 | 2010-03-07 16:41:34 -0800 | [diff] [blame] | 2971 | DIR("ns", S_IRUSR|S_IXUGO, proc_ns_dir_inode_operations, proc_ns_dir_operations), |
Andrew Morton | b2211a3 | 2008-03-11 18:03:35 -0700 | [diff] [blame] | 2972 | #ifdef CONFIG_NET |
Alexey Dobriyan | 631f9c1 | 2008-11-10 01:32:52 +0300 | [diff] [blame] | 2973 | DIR("net", S_IRUGO|S_IXUGO, proc_net_inode_operations, proc_net_operations), |
Andrew Morton | b2211a3 | 2008-03-11 18:03:35 -0700 | [diff] [blame] | 2974 | #endif |
Alexey Dobriyan | 631f9c1 | 2008-11-10 01:32:52 +0300 | [diff] [blame] | 2975 | REG("environ", S_IRUSR, proc_environ_operations), |
Al Viro | c531716 | 2016-10-05 18:43:43 -0400 | [diff] [blame] | 2976 | REG("auxv", S_IRUSR, proc_auxv_operations), |
Alexey Dobriyan | 631f9c1 | 2008-11-10 01:32:52 +0300 | [diff] [blame] | 2977 | ONE("status", S_IRUGO, proc_pid_status), |
Djalal Harouni | 35a3504 | 2014-04-07 15:38:36 -0700 | [diff] [blame] | 2978 | ONE("personality", S_IRUSR, proc_pid_personality), |
Alexey Dobriyan | 1c963eb | 2014-08-08 14:21:37 -0700 | [diff] [blame] | 2979 | ONE("limits", S_IRUGO, proc_pid_limits), |
Ingo Molnar | 43ae34c | 2007-07-09 18:52:00 +0200 | [diff] [blame] | 2980 | #ifdef CONFIG_SCHED_DEBUG |
Alexey Dobriyan | 631f9c1 | 2008-11-10 01:32:52 +0300 | [diff] [blame] | 2981 | REG("sched", S_IRUGO|S_IWUSR, proc_pid_sched_operations), |
Ingo Molnar | 43ae34c | 2007-07-09 18:52:00 +0200 | [diff] [blame] | 2982 | #endif |
Mike Galbraith | 5091faa | 2010-11-30 14:18:03 +0100 | [diff] [blame] | 2983 | #ifdef CONFIG_SCHED_AUTOGROUP |
| 2984 | REG("autogroup", S_IRUGO|S_IWUSR, proc_pid_sched_autogroup_operations), |
| 2985 | #endif |
john stultz | 4614a696b | 2009-12-14 18:00:05 -0800 | [diff] [blame] | 2986 | REG("comm", S_IRUGO|S_IWUSR, proc_pid_set_comm_operations), |
Roland McGrath | ebcb673 | 2008-07-25 19:46:00 -0700 | [diff] [blame] | 2987 | #ifdef CONFIG_HAVE_ARCH_TRACEHOOK |
Alexey Dobriyan | 09d93bd | 2014-08-08 14:21:39 -0700 | [diff] [blame] | 2988 | ONE("syscall", S_IRUSR, proc_pid_syscall), |
Roland McGrath | ebcb673 | 2008-07-25 19:46:00 -0700 | [diff] [blame] | 2989 | #endif |
Alexey Dobriyan | c2c0bb4 | 2015-06-25 15:00:54 -0700 | [diff] [blame] | 2990 | REG("cmdline", S_IRUGO, proc_pid_cmdline_ops), |
Alexey Dobriyan | 631f9c1 | 2008-11-10 01:32:52 +0300 | [diff] [blame] | 2991 | ONE("stat", S_IRUGO, proc_tgid_stat), |
| 2992 | ONE("statm", S_IRUGO, proc_pid_statm), |
Siddhesh Poyarekar | b764375 | 2012-03-21 16:34:04 -0700 | [diff] [blame] | 2993 | REG("maps", S_IRUGO, proc_pid_maps_operations), |
Eric W. Biederman | 28a6d67 | 2006-10-02 02:17:05 -0700 | [diff] [blame] | 2994 | #ifdef CONFIG_NUMA |
Siddhesh Poyarekar | b764375 | 2012-03-21 16:34:04 -0700 | [diff] [blame] | 2995 | REG("numa_maps", S_IRUGO, proc_pid_numa_maps_operations), |
Eric W. Biederman | 28a6d67 | 2006-10-02 02:17:05 -0700 | [diff] [blame] | 2996 | #endif |
Alexey Dobriyan | 631f9c1 | 2008-11-10 01:32:52 +0300 | [diff] [blame] | 2997 | REG("mem", S_IRUSR|S_IWUSR, proc_mem_operations), |
| 2998 | LNK("cwd", proc_cwd_link), |
| 2999 | LNK("root", proc_root_link), |
| 3000 | LNK("exe", proc_exe_link), |
| 3001 | REG("mounts", S_IRUGO, proc_mounts_operations), |
| 3002 | REG("mountinfo", S_IRUGO, proc_mountinfo_operations), |
| 3003 | REG("mountstats", S_IRUSR, proc_mountstats_operations), |
Matt Mackall | 1e88328 | 2008-02-04 22:29:07 -0800 | [diff] [blame] | 3004 | #ifdef CONFIG_PROC_PAGE_MONITOR |
Alexey Dobriyan | 631f9c1 | 2008-11-10 01:32:52 +0300 | [diff] [blame] | 3005 | REG("clear_refs", S_IWUSR, proc_clear_refs_operations), |
Siddhesh Poyarekar | b764375 | 2012-03-21 16:34:04 -0700 | [diff] [blame] | 3006 | REG("smaps", S_IRUGO, proc_pid_smaps_operations), |
Daniel Colascione | 493b0e9 | 2017-09-06 16:25:08 -0700 | [diff] [blame] | 3007 | REG("smaps_rollup", S_IRUGO, proc_pid_smaps_rollup_operations), |
Djalal Harouni | 32ed74a | 2014-04-07 15:38:38 -0700 | [diff] [blame] | 3008 | REG("pagemap", S_IRUSR, proc_pagemap_operations), |
Eric W. Biederman | 28a6d67 | 2006-10-02 02:17:05 -0700 | [diff] [blame] | 3009 | #endif |
| 3010 | #ifdef CONFIG_SECURITY |
Alexey Dobriyan | 631f9c1 | 2008-11-10 01:32:52 +0300 | [diff] [blame] | 3011 | DIR("attr", S_IRUGO|S_IXUGO, proc_attr_dir_inode_operations, proc_attr_dir_operations), |
Eric W. Biederman | 28a6d67 | 2006-10-02 02:17:05 -0700 | [diff] [blame] | 3012 | #endif |
| 3013 | #ifdef CONFIG_KALLSYMS |
Alexey Dobriyan | edfcd60 | 2014-08-08 14:21:44 -0700 | [diff] [blame] | 3014 | ONE("wchan", S_IRUGO, proc_pid_wchan), |
Eric W. Biederman | 28a6d67 | 2006-10-02 02:17:05 -0700 | [diff] [blame] | 3015 | #endif |
Ken Chen | 2ec220e | 2008-11-10 11:26:08 +0300 | [diff] [blame] | 3016 | #ifdef CONFIG_STACKTRACE |
Djalal Harouni | 35a3504 | 2014-04-07 15:38:36 -0700 | [diff] [blame] | 3017 | ONE("stack", S_IRUSR, proc_pid_stack), |
Eric W. Biederman | 28a6d67 | 2006-10-02 02:17:05 -0700 | [diff] [blame] | 3018 | #endif |
Naveen N. Rao | 5968cec | 2015-06-30 14:36:03 +0530 | [diff] [blame] | 3019 | #ifdef CONFIG_SCHED_INFO |
Alexey Dobriyan | f6e826c | 2014-08-08 14:21:46 -0700 | [diff] [blame] | 3020 | ONE("schedstat", S_IRUGO, proc_pid_schedstat), |
Eric W. Biederman | 28a6d67 | 2006-10-02 02:17:05 -0700 | [diff] [blame] | 3021 | #endif |
Arjan van de Ven | 9745512 | 2008-01-25 21:08:34 +0100 | [diff] [blame] | 3022 | #ifdef CONFIG_LATENCYTOP |
Alexey Dobriyan | 631f9c1 | 2008-11-10 01:32:52 +0300 | [diff] [blame] | 3023 | REG("latency", S_IRUGO, proc_lstats_operations), |
Arjan van de Ven | 9745512 | 2008-01-25 21:08:34 +0100 | [diff] [blame] | 3024 | #endif |
Paul Menage | 8793d85 | 2007-10-18 23:39:39 -0700 | [diff] [blame] | 3025 | #ifdef CONFIG_PROC_PID_CPUSET |
Zefan Li | 52de477 | 2014-09-18 16:03:36 +0800 | [diff] [blame] | 3026 | ONE("cpuset", S_IRUGO, proc_cpuset_show), |
Eric W. Biederman | 28a6d67 | 2006-10-02 02:17:05 -0700 | [diff] [blame] | 3027 | #endif |
Paul Menage | a424316 | 2007-10-18 23:39:35 -0700 | [diff] [blame] | 3028 | #ifdef CONFIG_CGROUPS |
Zefan Li | 006f4ac | 2014-09-18 16:03:15 +0800 | [diff] [blame] | 3029 | ONE("cgroup", S_IRUGO, proc_cgroup_show), |
Paul Menage | a424316 | 2007-10-18 23:39:35 -0700 | [diff] [blame] | 3030 | #endif |
Alexey Dobriyan | 6ba51e3 | 2014-08-08 14:21:48 -0700 | [diff] [blame] | 3031 | ONE("oom_score", S_IRUGO, proc_oom_score), |
David Rientjes | fa0cbbf | 2012-11-12 17:53:04 -0800 | [diff] [blame] | 3032 | REG("oom_adj", S_IRUGO|S_IWUSR, proc_oom_adj_operations), |
David Rientjes | a63d83f | 2010-08-09 17:19:46 -0700 | [diff] [blame] | 3033 | REG("oom_score_adj", S_IRUGO|S_IWUSR, proc_oom_score_adj_operations), |
Richard Guy Briggs | 4b7d248 | 2019-01-22 17:06:39 -0500 | [diff] [blame] | 3034 | #ifdef CONFIG_AUDIT |
Alexey Dobriyan | 631f9c1 | 2008-11-10 01:32:52 +0300 | [diff] [blame] | 3035 | REG("loginuid", S_IWUSR|S_IRUGO, proc_loginuid_operations), |
| 3036 | REG("sessionid", S_IRUGO, proc_sessionid_operations), |
Eric W. Biederman | 28a6d67 | 2006-10-02 02:17:05 -0700 | [diff] [blame] | 3037 | #endif |
Akinobu Mita | f4f154f | 2006-12-08 02:39:47 -0800 | [diff] [blame] | 3038 | #ifdef CONFIG_FAULT_INJECTION |
Alexey Dobriyan | 631f9c1 | 2008-11-10 01:32:52 +0300 | [diff] [blame] | 3039 | REG("make-it-fail", S_IRUGO|S_IWUSR, proc_fault_inject_operations), |
Akinobu Mita | 168c42b | 2017-07-14 14:50:00 -0700 | [diff] [blame] | 3040 | REG("fail-nth", 0644, proc_fail_nth_operations), |
Akinobu Mita | f4f154f | 2006-12-08 02:39:47 -0800 | [diff] [blame] | 3041 | #endif |
Christoph Hellwig | 698ba7b | 2009-12-15 16:47:37 -0800 | [diff] [blame] | 3042 | #ifdef CONFIG_ELF_CORE |
Alexey Dobriyan | 631f9c1 | 2008-11-10 01:32:52 +0300 | [diff] [blame] | 3043 | REG("coredump_filter", S_IRUGO|S_IWUSR, proc_coredump_filter_operations), |
Kawai, Hidehiro | 3cb4a0b | 2007-07-19 01:48:28 -0700 | [diff] [blame] | 3044 | #endif |
Andrew Morton | aba76fd | 2006-12-10 02:19:48 -0800 | [diff] [blame] | 3045 | #ifdef CONFIG_TASK_IO_ACCOUNTING |
Alexey Dobriyan | 19aadc9 | 2014-08-08 14:21:50 -0700 | [diff] [blame] | 3046 | ONE("io", S_IRUSR, proc_tgid_io_accounting), |
Andrew Morton | aba76fd | 2006-12-10 02:19:48 -0800 | [diff] [blame] | 3047 | #endif |
Eric W. Biederman | 22d917d | 2011-11-17 00:11:58 -0800 | [diff] [blame] | 3048 | #ifdef CONFIG_USER_NS |
| 3049 | REG("uid_map", S_IRUGO|S_IWUSR, proc_uid_map_operations), |
| 3050 | REG("gid_map", S_IRUGO|S_IWUSR, proc_gid_map_operations), |
Eric W. Biederman | f76d207 | 2012-08-30 01:24:05 -0700 | [diff] [blame] | 3051 | REG("projid_map", S_IRUGO|S_IWUSR, proc_projid_map_operations), |
Eric W. Biederman | 9cc4651 | 2014-12-02 12:27:26 -0600 | [diff] [blame] | 3052 | REG("setgroups", S_IRUGO|S_IWUSR, proc_setgroups_operations), |
Eric W. Biederman | 22d917d | 2011-11-17 00:11:58 -0800 | [diff] [blame] | 3053 | #endif |
Nicolas Pitre | b18b6a9 | 2017-01-21 00:09:08 -0500 | [diff] [blame] | 3054 | #if defined(CONFIG_CHECKPOINT_RESTORE) && defined(CONFIG_POSIX_TIMERS) |
Pavel Emelyanov | 48f6a7a | 2013-03-11 13:12:45 +0400 | [diff] [blame] | 3055 | REG("timers", S_IRUGO, proc_timers_operations), |
| 3056 | #endif |
John Stultz | 5de23d4 | 2016-03-17 14:20:54 -0700 | [diff] [blame] | 3057 | REG("timerslack_ns", S_IRUGO|S_IWUGO, proc_pid_set_timerslack_ns_operations), |
Josh Poimboeuf | 7c23b33 | 2017-02-13 19:42:41 -0600 | [diff] [blame] | 3058 | #ifdef CONFIG_LIVEPATCH |
| 3059 | ONE("patch_state", S_IRUSR, proc_pid_patch_state), |
| 3060 | #endif |
Alexander Popov | c8d1262 | 2018-08-17 01:17:01 +0300 | [diff] [blame] | 3061 | #ifdef CONFIG_STACKLEAK_METRICS |
| 3062 | ONE("stack_depth", S_IRUGO, proc_stack_depth), |
| 3063 | #endif |
Eric W. Biederman | 28a6d67 | 2006-10-02 02:17:05 -0700 | [diff] [blame] | 3064 | }; |
| 3065 | |
Al Viro | f0c3b50 | 2013-05-16 12:07:31 -0400 | [diff] [blame] | 3066 | static int proc_tgid_base_readdir(struct file *file, struct dir_context *ctx) |
Eric W. Biederman | 28a6d67 | 2006-10-02 02:17:05 -0700 | [diff] [blame] | 3067 | { |
Al Viro | f0c3b50 | 2013-05-16 12:07:31 -0400 | [diff] [blame] | 3068 | return proc_pident_readdir(file, ctx, |
| 3069 | tgid_base_stuff, ARRAY_SIZE(tgid_base_stuff)); |
Eric W. Biederman | 28a6d67 | 2006-10-02 02:17:05 -0700 | [diff] [blame] | 3070 | } |
| 3071 | |
Arjan van de Ven | 00977a5 | 2007-02-12 00:55:34 -0800 | [diff] [blame] | 3072 | static const struct file_operations proc_tgid_base_operations = { |
Eric W. Biederman | 28a6d67 | 2006-10-02 02:17:05 -0700 | [diff] [blame] | 3073 | .read = generic_read_dir, |
Al Viro | f50752e | 2016-04-20 17:13:54 -0400 | [diff] [blame] | 3074 | .iterate_shared = proc_tgid_base_readdir, |
| 3075 | .llseek = generic_file_llseek, |
Eric W. Biederman | 28a6d67 | 2006-10-02 02:17:05 -0700 | [diff] [blame] | 3076 | }; |
| 3077 | |
Christian Brauner | 3eb39f4 | 2018-11-19 00:51:56 +0100 | [diff] [blame] | 3078 | struct pid *tgid_pidfd_to_pid(const struct file *file) |
| 3079 | { |
Christian Brauner | 30d158b | 2019-06-27 11:35:14 +0200 | [diff] [blame] | 3080 | if (file->f_op != &proc_tgid_base_operations) |
Christian Brauner | 3eb39f4 | 2018-11-19 00:51:56 +0100 | [diff] [blame] | 3081 | return ERR_PTR(-EBADF); |
| 3082 | |
| 3083 | return proc_pid(file_inode(file)); |
| 3084 | } |
| 3085 | |
Al Viro | 00cd8dd | 2012-06-10 17:13:09 -0400 | [diff] [blame] | 3086 | static struct dentry *proc_tgid_base_lookup(struct inode *dir, struct dentry *dentry, unsigned int flags) |
| 3087 | { |
Eric W. Biederman | 7bcd6b0 | 2006-10-02 02:18:56 -0700 | [diff] [blame] | 3088 | return proc_pident_lookup(dir, dentry, |
Alexey Dobriyan | d5a572a | 2019-03-11 23:28:51 -0700 | [diff] [blame] | 3089 | tgid_base_stuff, |
| 3090 | tgid_base_stuff + ARRAY_SIZE(tgid_base_stuff)); |
Eric W. Biederman | 28a6d67 | 2006-10-02 02:17:05 -0700 | [diff] [blame] | 3091 | } |
| 3092 | |
Arjan van de Ven | c5ef1c4 | 2007-02-12 00:55:40 -0800 | [diff] [blame] | 3093 | static const struct inode_operations proc_tgid_base_inode_operations = { |
Eric W. Biederman | 28a6d67 | 2006-10-02 02:17:05 -0700 | [diff] [blame] | 3094 | .lookup = proc_tgid_base_lookup, |
| 3095 | .getattr = pid_getattr, |
| 3096 | .setattr = proc_setattr, |
Vasiliy Kulikov | 0499680 | 2012-01-10 15:11:31 -0800 | [diff] [blame] | 3097 | .permission = proc_pid_permission, |
Eric W. Biederman | 28a6d67 | 2006-10-02 02:17:05 -0700 | [diff] [blame] | 3098 | }; |
| 3099 | |
Pavel Emelyanov | 60347f6 | 2007-10-18 23:40:03 -0700 | [diff] [blame] | 3100 | static void proc_flush_task_mnt(struct vfsmount *mnt, pid_t pid, pid_t tgid) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3101 | { |
Eric W. Biederman | 48e6484 | 2006-06-26 00:25:48 -0700 | [diff] [blame] | 3102 | struct dentry *dentry, *leader, *dir; |
Alexey Dobriyan | e3912ac | 2018-02-06 15:36:51 -0800 | [diff] [blame] | 3103 | char buf[10 + 1]; |
Eric W. Biederman | 48e6484 | 2006-06-26 00:25:48 -0700 | [diff] [blame] | 3104 | struct qstr name; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3105 | |
Eric W. Biederman | 48e6484 | 2006-06-26 00:25:48 -0700 | [diff] [blame] | 3106 | name.name = buf; |
Alexey Dobriyan | e3912ac | 2018-02-06 15:36:51 -0800 | [diff] [blame] | 3107 | name.len = snprintf(buf, sizeof(buf), "%u", pid); |
Al Viro | 4f522a2 | 2013-02-11 23:20:37 -0500 | [diff] [blame] | 3108 | /* no ->d_hash() rejects on procfs */ |
Pavel Emelyanov | 60347f6 | 2007-10-18 23:40:03 -0700 | [diff] [blame] | 3109 | dentry = d_hash_and_lookup(mnt->mnt_root, &name); |
Eric W. Biederman | 48e6484 | 2006-06-26 00:25:48 -0700 | [diff] [blame] | 3110 | if (dentry) { |
Eric W. Biederman | bbd5192 | 2014-02-13 10:24:23 -0800 | [diff] [blame] | 3111 | d_invalidate(dentry); |
Eric W. Biederman | 48e6484 | 2006-06-26 00:25:48 -0700 | [diff] [blame] | 3112 | dput(dentry); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3113 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3114 | |
Oleg Nesterov | c35a7f1 | 2014-12-10 15:54:56 -0800 | [diff] [blame] | 3115 | if (pid == tgid) |
| 3116 | return; |
| 3117 | |
Eric W. Biederman | 48e6484 | 2006-06-26 00:25:48 -0700 | [diff] [blame] | 3118 | name.name = buf; |
Alexey Dobriyan | e3912ac | 2018-02-06 15:36:51 -0800 | [diff] [blame] | 3119 | name.len = snprintf(buf, sizeof(buf), "%u", tgid); |
Pavel Emelyanov | 60347f6 | 2007-10-18 23:40:03 -0700 | [diff] [blame] | 3120 | leader = d_hash_and_lookup(mnt->mnt_root, &name); |
Eric W. Biederman | 48e6484 | 2006-06-26 00:25:48 -0700 | [diff] [blame] | 3121 | if (!leader) |
| 3122 | goto out; |
| 3123 | |
| 3124 | name.name = "task"; |
| 3125 | name.len = strlen(name.name); |
| 3126 | dir = d_hash_and_lookup(leader, &name); |
| 3127 | if (!dir) |
| 3128 | goto out_put_leader; |
| 3129 | |
| 3130 | name.name = buf; |
Alexey Dobriyan | e3912ac | 2018-02-06 15:36:51 -0800 | [diff] [blame] | 3131 | name.len = snprintf(buf, sizeof(buf), "%u", pid); |
Eric W. Biederman | 48e6484 | 2006-06-26 00:25:48 -0700 | [diff] [blame] | 3132 | dentry = d_hash_and_lookup(dir, &name); |
| 3133 | if (dentry) { |
Eric W. Biederman | bbd5192 | 2014-02-13 10:24:23 -0800 | [diff] [blame] | 3134 | d_invalidate(dentry); |
Eric W. Biederman | 48e6484 | 2006-06-26 00:25:48 -0700 | [diff] [blame] | 3135 | dput(dentry); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3136 | } |
Eric W. Biederman | 48e6484 | 2006-06-26 00:25:48 -0700 | [diff] [blame] | 3137 | |
| 3138 | dput(dir); |
| 3139 | out_put_leader: |
| 3140 | dput(leader); |
| 3141 | out: |
| 3142 | return; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3143 | } |
| 3144 | |
Randy Dunlap | 0895e91 | 2007-10-21 21:00:10 -0700 | [diff] [blame] | 3145 | /** |
| 3146 | * proc_flush_task - Remove dcache entries for @task from the /proc dcache. |
| 3147 | * @task: task that should be flushed. |
| 3148 | * |
| 3149 | * When flushing dentries from proc, one needs to flush them from global |
Pavel Emelyanov | 60347f6 | 2007-10-18 23:40:03 -0700 | [diff] [blame] | 3150 | * proc (proc_mnt) and from all the namespaces' procs this task was seen |
Randy Dunlap | 0895e91 | 2007-10-21 21:00:10 -0700 | [diff] [blame] | 3151 | * in. This call is supposed to do all of this job. |
| 3152 | * |
| 3153 | * Looks in the dcache for |
| 3154 | * /proc/@pid |
| 3155 | * /proc/@tgid/task/@pid |
| 3156 | * if either directory is present flushes it and all of it'ts children |
| 3157 | * from the dcache. |
| 3158 | * |
| 3159 | * It is safe and reasonable to cache /proc entries for a task until |
| 3160 | * that task exits. After that they just clog up the dcache with |
| 3161 | * useless entries, possibly causing useful dcache entries to be |
| 3162 | * flushed instead. This routine is proved to flush those useless |
| 3163 | * dcache entries at process exit time. |
| 3164 | * |
| 3165 | * NOTE: This routine is just an optimization so it does not guarantee |
| 3166 | * that no dcache entries will exist at process exit time it |
| 3167 | * just makes it very unlikely that any will persist. |
Pavel Emelyanov | 60347f6 | 2007-10-18 23:40:03 -0700 | [diff] [blame] | 3168 | */ |
| 3169 | |
| 3170 | void proc_flush_task(struct task_struct *task) |
| 3171 | { |
Eric W. Biederman | 9fcc2d1 | 2007-11-14 17:00:07 -0800 | [diff] [blame] | 3172 | int i; |
Oleg Nesterov | 9b4d1cbe | 2009-09-22 16:45:34 -0700 | [diff] [blame] | 3173 | struct pid *pid, *tgid; |
Pavel Emelyanov | 130f77e | 2007-10-18 23:40:11 -0700 | [diff] [blame] | 3174 | struct upid *upid; |
| 3175 | |
Pavel Emelyanov | 130f77e | 2007-10-18 23:40:11 -0700 | [diff] [blame] | 3176 | pid = task_pid(task); |
Oleg Nesterov | 9b4d1cbe | 2009-09-22 16:45:34 -0700 | [diff] [blame] | 3177 | tgid = task_tgid(task); |
Pavel Emelyanov | 130f77e | 2007-10-18 23:40:11 -0700 | [diff] [blame] | 3178 | |
Eric W. Biederman | 9fcc2d1 | 2007-11-14 17:00:07 -0800 | [diff] [blame] | 3179 | for (i = 0; i <= pid->level; i++) { |
Pavel Emelyanov | 130f77e | 2007-10-18 23:40:11 -0700 | [diff] [blame] | 3180 | upid = &pid->numbers[i]; |
| 3181 | proc_flush_task_mnt(upid->ns->proc_mnt, upid->nr, |
Oleg Nesterov | 9b4d1cbe | 2009-09-22 16:45:34 -0700 | [diff] [blame] | 3182 | tgid->numbers[i].nr); |
Pavel Emelyanov | 130f77e | 2007-10-18 23:40:11 -0700 | [diff] [blame] | 3183 | } |
Pavel Emelyanov | 60347f6 | 2007-10-18 23:40:03 -0700 | [diff] [blame] | 3184 | } |
| 3185 | |
Al Viro | 0168b9e | 2018-05-03 09:21:05 -0400 | [diff] [blame] | 3186 | static struct dentry *proc_pid_instantiate(struct dentry * dentry, |
Al Viro | c52a47a | 2013-06-15 11:15:20 +0400 | [diff] [blame] | 3187 | struct task_struct *task, const void *ptr) |
Eric W. Biederman | 444ceed | 2006-10-02 02:18:49 -0700 | [diff] [blame] | 3188 | { |
Eric W. Biederman | 444ceed | 2006-10-02 02:18:49 -0700 | [diff] [blame] | 3189 | struct inode *inode; |
| 3190 | |
Al Viro | 0168b9e | 2018-05-03 09:21:05 -0400 | [diff] [blame] | 3191 | inode = proc_pid_make_inode(dentry->d_sb, task, S_IFDIR | S_IRUGO | S_IXUGO); |
Eric W. Biederman | 444ceed | 2006-10-02 02:18:49 -0700 | [diff] [blame] | 3192 | if (!inode) |
Al Viro | 0168b9e | 2018-05-03 09:21:05 -0400 | [diff] [blame] | 3193 | return ERR_PTR(-ENOENT); |
Eric W. Biederman | 444ceed | 2006-10-02 02:18:49 -0700 | [diff] [blame] | 3194 | |
Eric W. Biederman | 444ceed | 2006-10-02 02:18:49 -0700 | [diff] [blame] | 3195 | inode->i_op = &proc_tgid_base_inode_operations; |
| 3196 | inode->i_fop = &proc_tgid_base_operations; |
| 3197 | inode->i_flags|=S_IMMUTABLE; |
Vegard Nossum | aed5417 | 2008-06-05 22:46:53 -0700 | [diff] [blame] | 3198 | |
Alexey Dobriyan | 1270dd8 | 2016-12-12 16:45:32 -0800 | [diff] [blame] | 3199 | set_nlink(inode, nlink_tgid); |
Al Viro | 1bbc551 | 2018-05-02 21:26:16 -0400 | [diff] [blame] | 3200 | pid_update_inode(task, inode); |
Eric W. Biederman | 444ceed | 2006-10-02 02:18:49 -0700 | [diff] [blame] | 3201 | |
Nick Piggin | fb045ad | 2011-01-07 17:49:55 +1100 | [diff] [blame] | 3202 | d_set_d_op(dentry, &pid_dentry_operations); |
Al Viro | 0168b9e | 2018-05-03 09:21:05 -0400 | [diff] [blame] | 3203 | return d_splice_alias(inode, dentry); |
Eric W. Biederman | 444ceed | 2006-10-02 02:18:49 -0700 | [diff] [blame] | 3204 | } |
| 3205 | |
Zhikang Zhang | 867aacc | 2019-03-05 15:50:29 -0800 | [diff] [blame] | 3206 | struct dentry *proc_pid_lookup(struct dentry *dentry, unsigned int flags) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3207 | { |
| 3208 | struct task_struct *task; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3209 | unsigned tgid; |
Pavel Emelyanov | b488893 | 2007-10-18 23:40:14 -0700 | [diff] [blame] | 3210 | struct pid_namespace *ns; |
Al Viro | 0168b9e | 2018-05-03 09:21:05 -0400 | [diff] [blame] | 3211 | struct dentry *result = ERR_PTR(-ENOENT); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3212 | |
Alexey Dobriyan | dbcdb50 | 2014-08-08 14:21:25 -0700 | [diff] [blame] | 3213 | tgid = name_to_int(&dentry->d_name); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3214 | if (tgid == ~0U) |
| 3215 | goto out; |
| 3216 | |
Pavel Emelyanov | b488893 | 2007-10-18 23:40:14 -0700 | [diff] [blame] | 3217 | ns = dentry->d_sb->s_fs_info; |
Eric W. Biederman | de75873 | 2006-06-26 00:25:51 -0700 | [diff] [blame] | 3218 | rcu_read_lock(); |
Pavel Emelyanov | b488893 | 2007-10-18 23:40:14 -0700 | [diff] [blame] | 3219 | task = find_task_by_pid_ns(tgid, ns); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3220 | if (task) |
| 3221 | get_task_struct(task); |
Eric W. Biederman | de75873 | 2006-06-26 00:25:51 -0700 | [diff] [blame] | 3222 | rcu_read_unlock(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3223 | if (!task) |
| 3224 | goto out; |
| 3225 | |
Al Viro | 0168b9e | 2018-05-03 09:21:05 -0400 | [diff] [blame] | 3226 | result = proc_pid_instantiate(dentry, task, NULL); |
Eric W. Biederman | 48e6484 | 2006-06-26 00:25:48 -0700 | [diff] [blame] | 3227 | put_task_struct(task); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3228 | out: |
Al Viro | 0168b9e | 2018-05-03 09:21:05 -0400 | [diff] [blame] | 3229 | return result; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3230 | } |
| 3231 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3232 | /* |
Eric W. Biederman | 0804ef4 | 2006-10-02 02:17:04 -0700 | [diff] [blame] | 3233 | * Find the first task with tgid >= tgid |
Eric W. Biederman | 0bc58a9 | 2006-06-26 00:25:50 -0700 | [diff] [blame] | 3234 | * |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3235 | */ |
Eric W. Biederman | 19fd4bb | 2007-11-28 16:21:26 -0800 | [diff] [blame] | 3236 | struct tgid_iter { |
| 3237 | unsigned int tgid; |
Eric W. Biederman | 0804ef4 | 2006-10-02 02:17:04 -0700 | [diff] [blame] | 3238 | struct task_struct *task; |
Eric W. Biederman | 19fd4bb | 2007-11-28 16:21:26 -0800 | [diff] [blame] | 3239 | }; |
| 3240 | static struct tgid_iter next_tgid(struct pid_namespace *ns, struct tgid_iter iter) |
| 3241 | { |
Eric W. Biederman | 0804ef4 | 2006-10-02 02:17:04 -0700 | [diff] [blame] | 3242 | struct pid *pid; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3243 | |
Eric W. Biederman | 19fd4bb | 2007-11-28 16:21:26 -0800 | [diff] [blame] | 3244 | if (iter.task) |
| 3245 | put_task_struct(iter.task); |
Eric W. Biederman | 0804ef4 | 2006-10-02 02:17:04 -0700 | [diff] [blame] | 3246 | rcu_read_lock(); |
| 3247 | retry: |
Eric W. Biederman | 19fd4bb | 2007-11-28 16:21:26 -0800 | [diff] [blame] | 3248 | iter.task = NULL; |
| 3249 | pid = find_ge_pid(iter.tgid, ns); |
Eric W. Biederman | 0804ef4 | 2006-10-02 02:17:04 -0700 | [diff] [blame] | 3250 | if (pid) { |
Eric W. Biederman | 19fd4bb | 2007-11-28 16:21:26 -0800 | [diff] [blame] | 3251 | iter.tgid = pid_nr_ns(pid, ns); |
| 3252 | iter.task = pid_task(pid, PIDTYPE_PID); |
Eric W. Biederman | 0804ef4 | 2006-10-02 02:17:04 -0700 | [diff] [blame] | 3253 | /* What we to know is if the pid we have find is the |
| 3254 | * pid of a thread_group_leader. Testing for task |
| 3255 | * being a thread_group_leader is the obvious thing |
| 3256 | * todo but there is a window when it fails, due to |
| 3257 | * the pid transfer logic in de_thread. |
| 3258 | * |
| 3259 | * So we perform the straight forward test of seeing |
| 3260 | * if the pid we have found is the pid of a thread |
| 3261 | * group leader, and don't worry if the task we have |
| 3262 | * found doesn't happen to be a thread group leader. |
| 3263 | * As we don't care in the case of readdir. |
| 3264 | */ |
Eric W. Biederman | 19fd4bb | 2007-11-28 16:21:26 -0800 | [diff] [blame] | 3265 | if (!iter.task || !has_group_leader_pid(iter.task)) { |
| 3266 | iter.tgid += 1; |
Eric W. Biederman | 0804ef4 | 2006-10-02 02:17:04 -0700 | [diff] [blame] | 3267 | goto retry; |
Eric W. Biederman | 19fd4bb | 2007-11-28 16:21:26 -0800 | [diff] [blame] | 3268 | } |
| 3269 | get_task_struct(iter.task); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3270 | } |
Eric W. Biederman | 454cc10 | 2006-06-26 00:25:51 -0700 | [diff] [blame] | 3271 | rcu_read_unlock(); |
Eric W. Biederman | 19fd4bb | 2007-11-28 16:21:26 -0800 | [diff] [blame] | 3272 | return iter; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3273 | } |
| 3274 | |
Eric W. Biederman | 0097875 | 2014-07-31 03:10:50 -0700 | [diff] [blame] | 3275 | #define TGID_OFFSET (FIRST_PROCESS_ENTRY + 2) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3276 | |
| 3277 | /* for the /proc/ directory itself, after non-process stuff has been done */ |
Al Viro | f0c3b50 | 2013-05-16 12:07:31 -0400 | [diff] [blame] | 3278 | int proc_pid_readdir(struct file *file, struct dir_context *ctx) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3279 | { |
Eric W. Biederman | 19fd4bb | 2007-11-28 16:21:26 -0800 | [diff] [blame] | 3280 | struct tgid_iter iter; |
Christoph Hellwig | 76f668b | 2018-05-16 07:19:01 +0200 | [diff] [blame] | 3281 | struct pid_namespace *ns = proc_pid_ns(file_inode(file)); |
Al Viro | f0c3b50 | 2013-05-16 12:07:31 -0400 | [diff] [blame] | 3282 | loff_t pos = ctx->pos; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3283 | |
Al Viro | 021ada7 | 2013-03-29 19:27:05 -0400 | [diff] [blame] | 3284 | if (pos >= PID_MAX_LIMIT + TGID_OFFSET) |
Al Viro | f0c3b50 | 2013-05-16 12:07:31 -0400 | [diff] [blame] | 3285 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3286 | |
Eric W. Biederman | 0097875 | 2014-07-31 03:10:50 -0700 | [diff] [blame] | 3287 | if (pos == TGID_OFFSET - 2) { |
David Howells | 2b0143b | 2015-03-17 22:25:59 +0000 | [diff] [blame] | 3288 | struct inode *inode = d_inode(ns->proc_self); |
Al Viro | db96316 | 2013-06-15 10:45:10 +0400 | [diff] [blame] | 3289 | if (!dir_emit(ctx, "self", 4, inode->i_ino, DT_LNK)) |
Al Viro | f0c3b50 | 2013-05-16 12:07:31 -0400 | [diff] [blame] | 3290 | return 0; |
Eric W. Biederman | 0097875 | 2014-07-31 03:10:50 -0700 | [diff] [blame] | 3291 | ctx->pos = pos = pos + 1; |
Al Viro | 021ada7 | 2013-03-29 19:27:05 -0400 | [diff] [blame] | 3292 | } |
Eric W. Biederman | 0097875 | 2014-07-31 03:10:50 -0700 | [diff] [blame] | 3293 | if (pos == TGID_OFFSET - 1) { |
David Howells | 2b0143b | 2015-03-17 22:25:59 +0000 | [diff] [blame] | 3294 | struct inode *inode = d_inode(ns->proc_thread_self); |
Eric W. Biederman | 0097875 | 2014-07-31 03:10:50 -0700 | [diff] [blame] | 3295 | if (!dir_emit(ctx, "thread-self", 11, inode->i_ino, DT_LNK)) |
| 3296 | return 0; |
| 3297 | ctx->pos = pos = pos + 1; |
| 3298 | } |
| 3299 | iter.tgid = pos - TGID_OFFSET; |
Eric W. Biederman | 19fd4bb | 2007-11-28 16:21:26 -0800 | [diff] [blame] | 3300 | iter.task = NULL; |
Eric W. Biederman | 19fd4bb | 2007-11-28 16:21:26 -0800 | [diff] [blame] | 3301 | for (iter = next_tgid(ns, iter); |
| 3302 | iter.task; |
| 3303 | iter.tgid += 1, iter = next_tgid(ns, iter)) { |
Alexey Dobriyan | e3912ac | 2018-02-06 15:36:51 -0800 | [diff] [blame] | 3304 | char name[10 + 1]; |
Alexey Dobriyan | a4ef389 | 2018-06-07 17:10:10 -0700 | [diff] [blame] | 3305 | unsigned int len; |
Eric Dumazet | 3ba4bce | 2017-01-24 15:18:07 -0800 | [diff] [blame] | 3306 | |
| 3307 | cond_resched(); |
Lafcadio Wluiki | 796f571 | 2017-02-24 15:00:23 -0800 | [diff] [blame] | 3308 | if (!has_pid_permissions(ns, iter.task, HIDEPID_INVISIBLE)) |
Al Viro | f0c3b50 | 2013-05-16 12:07:31 -0400 | [diff] [blame] | 3309 | continue; |
Vasiliy Kulikov | 0499680 | 2012-01-10 15:11:31 -0800 | [diff] [blame] | 3310 | |
Alexey Dobriyan | e3912ac | 2018-02-06 15:36:51 -0800 | [diff] [blame] | 3311 | len = snprintf(name, sizeof(name), "%u", iter.tgid); |
Al Viro | f0c3b50 | 2013-05-16 12:07:31 -0400 | [diff] [blame] | 3312 | ctx->pos = iter.tgid + TGID_OFFSET; |
| 3313 | if (!proc_fill_cache(file, ctx, name, len, |
| 3314 | proc_pid_instantiate, iter.task, NULL)) { |
Eric W. Biederman | 19fd4bb | 2007-11-28 16:21:26 -0800 | [diff] [blame] | 3315 | put_task_struct(iter.task); |
Al Viro | f0c3b50 | 2013-05-16 12:07:31 -0400 | [diff] [blame] | 3316 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3317 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3318 | } |
Al Viro | f0c3b50 | 2013-05-16 12:07:31 -0400 | [diff] [blame] | 3319 | ctx->pos = PID_MAX_LIMIT + TGID_OFFSET; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3320 | return 0; |
| 3321 | } |
| 3322 | |
Eric W. Biederman | 0bc58a9 | 2006-06-26 00:25:50 -0700 | [diff] [blame] | 3323 | /* |
Janis Danisevskis | 1b3044e | 2016-05-20 17:00:08 -0700 | [diff] [blame] | 3324 | * proc_tid_comm_permission is a special permission function exclusively |
| 3325 | * used for the node /proc/<pid>/task/<tid>/comm. |
| 3326 | * It bypasses generic permission checks in the case where a task of the same |
| 3327 | * task group attempts to access the node. |
| 3328 | * The rationale behind this is that glibc and bionic access this node for |
| 3329 | * cross thread naming (pthread_set/getname_np(!self)). However, if |
| 3330 | * PR_SET_DUMPABLE gets set to 0 this node among others becomes uid=0 gid=0, |
| 3331 | * which locks out the cross thread naming implementation. |
| 3332 | * This function makes sure that the node is always accessible for members of |
| 3333 | * same thread group. |
| 3334 | */ |
| 3335 | static int proc_tid_comm_permission(struct inode *inode, int mask) |
| 3336 | { |
| 3337 | bool is_same_tgroup; |
| 3338 | struct task_struct *task; |
| 3339 | |
| 3340 | task = get_proc_task(inode); |
| 3341 | if (!task) |
| 3342 | return -ESRCH; |
| 3343 | is_same_tgroup = same_thread_group(current, task); |
| 3344 | put_task_struct(task); |
| 3345 | |
| 3346 | if (likely(is_same_tgroup && !(mask & MAY_EXEC))) { |
| 3347 | /* This file (/proc/<pid>/task/<tid>/comm) can always be |
| 3348 | * read or written by the members of the corresponding |
| 3349 | * thread group. |
| 3350 | */ |
| 3351 | return 0; |
| 3352 | } |
| 3353 | |
| 3354 | return generic_permission(inode, mask); |
| 3355 | } |
| 3356 | |
| 3357 | static const struct inode_operations proc_tid_comm_inode_operations = { |
| 3358 | .permission = proc_tid_comm_permission, |
| 3359 | }; |
| 3360 | |
| 3361 | /* |
Eric W. Biederman | 28a6d67 | 2006-10-02 02:17:05 -0700 | [diff] [blame] | 3362 | * Tasks |
| 3363 | */ |
Eric Dumazet | c5141e6 | 2007-05-08 00:26:15 -0700 | [diff] [blame] | 3364 | static const struct pid_entry tid_base_stuff[] = { |
Alexey Dobriyan | 631f9c1 | 2008-11-10 01:32:52 +0300 | [diff] [blame] | 3365 | DIR("fd", S_IRUSR|S_IXUSR, proc_fd_inode_operations, proc_fd_operations), |
Jerome Marchand | 3835541 | 2010-04-27 13:13:06 -0700 | [diff] [blame] | 3366 | DIR("fdinfo", S_IRUSR|S_IXUSR, proc_fdinfo_inode_operations, proc_fdinfo_operations), |
Eric W. Biederman | 6b4e306 | 2010-03-07 16:41:34 -0800 | [diff] [blame] | 3367 | DIR("ns", S_IRUSR|S_IXUGO, proc_ns_dir_inode_operations, proc_ns_dir_operations), |
Eric W. Biederman | 6ba8ed7 | 2014-07-31 16:27:08 -0700 | [diff] [blame] | 3368 | #ifdef CONFIG_NET |
| 3369 | DIR("net", S_IRUGO|S_IXUGO, proc_net_inode_operations, proc_net_operations), |
| 3370 | #endif |
Alexey Dobriyan | 631f9c1 | 2008-11-10 01:32:52 +0300 | [diff] [blame] | 3371 | REG("environ", S_IRUSR, proc_environ_operations), |
Al Viro | c531716 | 2016-10-05 18:43:43 -0400 | [diff] [blame] | 3372 | REG("auxv", S_IRUSR, proc_auxv_operations), |
Alexey Dobriyan | 631f9c1 | 2008-11-10 01:32:52 +0300 | [diff] [blame] | 3373 | ONE("status", S_IRUGO, proc_pid_status), |
Djalal Harouni | 35a3504 | 2014-04-07 15:38:36 -0700 | [diff] [blame] | 3374 | ONE("personality", S_IRUSR, proc_pid_personality), |
Alexey Dobriyan | 1c963eb | 2014-08-08 14:21:37 -0700 | [diff] [blame] | 3375 | ONE("limits", S_IRUGO, proc_pid_limits), |
Ingo Molnar | 43ae34c | 2007-07-09 18:52:00 +0200 | [diff] [blame] | 3376 | #ifdef CONFIG_SCHED_DEBUG |
Alexey Dobriyan | 631f9c1 | 2008-11-10 01:32:52 +0300 | [diff] [blame] | 3377 | REG("sched", S_IRUGO|S_IWUSR, proc_pid_sched_operations), |
Ingo Molnar | 43ae34c | 2007-07-09 18:52:00 +0200 | [diff] [blame] | 3378 | #endif |
Janis Danisevskis | 1b3044e | 2016-05-20 17:00:08 -0700 | [diff] [blame] | 3379 | NOD("comm", S_IFREG|S_IRUGO|S_IWUSR, |
| 3380 | &proc_tid_comm_inode_operations, |
| 3381 | &proc_pid_set_comm_operations, {}), |
Roland McGrath | ebcb673 | 2008-07-25 19:46:00 -0700 | [diff] [blame] | 3382 | #ifdef CONFIG_HAVE_ARCH_TRACEHOOK |
Alexey Dobriyan | 09d93bd | 2014-08-08 14:21:39 -0700 | [diff] [blame] | 3383 | ONE("syscall", S_IRUSR, proc_pid_syscall), |
Roland McGrath | ebcb673 | 2008-07-25 19:46:00 -0700 | [diff] [blame] | 3384 | #endif |
Alexey Dobriyan | c2c0bb4 | 2015-06-25 15:00:54 -0700 | [diff] [blame] | 3385 | REG("cmdline", S_IRUGO, proc_pid_cmdline_ops), |
Alexey Dobriyan | 631f9c1 | 2008-11-10 01:32:52 +0300 | [diff] [blame] | 3386 | ONE("stat", S_IRUGO, proc_tid_stat), |
| 3387 | ONE("statm", S_IRUGO, proc_pid_statm), |
Vlastimil Babka | 871305b | 2018-08-21 21:52:48 -0700 | [diff] [blame] | 3388 | REG("maps", S_IRUGO, proc_pid_maps_operations), |
Iago López Galeiras | 2e13ba5 | 2015-06-25 15:00:57 -0700 | [diff] [blame] | 3389 | #ifdef CONFIG_PROC_CHILDREN |
Cyrill Gorcunov | 81841161 | 2012-05-31 16:26:43 -0700 | [diff] [blame] | 3390 | REG("children", S_IRUGO, proc_tid_children_operations), |
| 3391 | #endif |
Eric W. Biederman | 28a6d67 | 2006-10-02 02:17:05 -0700 | [diff] [blame] | 3392 | #ifdef CONFIG_NUMA |
Vlastimil Babka | 871305b | 2018-08-21 21:52:48 -0700 | [diff] [blame] | 3393 | REG("numa_maps", S_IRUGO, proc_pid_numa_maps_operations), |
Eric W. Biederman | 28a6d67 | 2006-10-02 02:17:05 -0700 | [diff] [blame] | 3394 | #endif |
Alexey Dobriyan | 631f9c1 | 2008-11-10 01:32:52 +0300 | [diff] [blame] | 3395 | REG("mem", S_IRUSR|S_IWUSR, proc_mem_operations), |
| 3396 | LNK("cwd", proc_cwd_link), |
| 3397 | LNK("root", proc_root_link), |
| 3398 | LNK("exe", proc_exe_link), |
| 3399 | REG("mounts", S_IRUGO, proc_mounts_operations), |
| 3400 | REG("mountinfo", S_IRUGO, proc_mountinfo_operations), |
Matt Mackall | 1e88328 | 2008-02-04 22:29:07 -0800 | [diff] [blame] | 3401 | #ifdef CONFIG_PROC_PAGE_MONITOR |
Alexey Dobriyan | 631f9c1 | 2008-11-10 01:32:52 +0300 | [diff] [blame] | 3402 | REG("clear_refs", S_IWUSR, proc_clear_refs_operations), |
Vlastimil Babka | 871305b | 2018-08-21 21:52:48 -0700 | [diff] [blame] | 3403 | REG("smaps", S_IRUGO, proc_pid_smaps_operations), |
Daniel Colascione | 493b0e9 | 2017-09-06 16:25:08 -0700 | [diff] [blame] | 3404 | REG("smaps_rollup", S_IRUGO, proc_pid_smaps_rollup_operations), |
Djalal Harouni | 32ed74a | 2014-04-07 15:38:38 -0700 | [diff] [blame] | 3405 | REG("pagemap", S_IRUSR, proc_pagemap_operations), |
Eric W. Biederman | 28a6d67 | 2006-10-02 02:17:05 -0700 | [diff] [blame] | 3406 | #endif |
| 3407 | #ifdef CONFIG_SECURITY |
Alexey Dobriyan | 631f9c1 | 2008-11-10 01:32:52 +0300 | [diff] [blame] | 3408 | DIR("attr", S_IRUGO|S_IXUGO, proc_attr_dir_inode_operations, proc_attr_dir_operations), |
Eric W. Biederman | 28a6d67 | 2006-10-02 02:17:05 -0700 | [diff] [blame] | 3409 | #endif |
| 3410 | #ifdef CONFIG_KALLSYMS |
Alexey Dobriyan | edfcd60 | 2014-08-08 14:21:44 -0700 | [diff] [blame] | 3411 | ONE("wchan", S_IRUGO, proc_pid_wchan), |
Eric W. Biederman | 28a6d67 | 2006-10-02 02:17:05 -0700 | [diff] [blame] | 3412 | #endif |
Ken Chen | 2ec220e | 2008-11-10 11:26:08 +0300 | [diff] [blame] | 3413 | #ifdef CONFIG_STACKTRACE |
Djalal Harouni | 35a3504 | 2014-04-07 15:38:36 -0700 | [diff] [blame] | 3414 | ONE("stack", S_IRUSR, proc_pid_stack), |
Eric W. Biederman | 28a6d67 | 2006-10-02 02:17:05 -0700 | [diff] [blame] | 3415 | #endif |
Naveen N. Rao | 5968cec | 2015-06-30 14:36:03 +0530 | [diff] [blame] | 3416 | #ifdef CONFIG_SCHED_INFO |
Alexey Dobriyan | f6e826c | 2014-08-08 14:21:46 -0700 | [diff] [blame] | 3417 | ONE("schedstat", S_IRUGO, proc_pid_schedstat), |
Eric W. Biederman | 28a6d67 | 2006-10-02 02:17:05 -0700 | [diff] [blame] | 3418 | #endif |
Arjan van de Ven | 9745512 | 2008-01-25 21:08:34 +0100 | [diff] [blame] | 3419 | #ifdef CONFIG_LATENCYTOP |
Alexey Dobriyan | 631f9c1 | 2008-11-10 01:32:52 +0300 | [diff] [blame] | 3420 | REG("latency", S_IRUGO, proc_lstats_operations), |
Arjan van de Ven | 9745512 | 2008-01-25 21:08:34 +0100 | [diff] [blame] | 3421 | #endif |
Paul Menage | 8793d85 | 2007-10-18 23:39:39 -0700 | [diff] [blame] | 3422 | #ifdef CONFIG_PROC_PID_CPUSET |
Zefan Li | 52de477 | 2014-09-18 16:03:36 +0800 | [diff] [blame] | 3423 | ONE("cpuset", S_IRUGO, proc_cpuset_show), |
Eric W. Biederman | 28a6d67 | 2006-10-02 02:17:05 -0700 | [diff] [blame] | 3424 | #endif |
Paul Menage | a424316 | 2007-10-18 23:39:35 -0700 | [diff] [blame] | 3425 | #ifdef CONFIG_CGROUPS |
Zefan Li | 006f4ac | 2014-09-18 16:03:15 +0800 | [diff] [blame] | 3426 | ONE("cgroup", S_IRUGO, proc_cgroup_show), |
Paul Menage | a424316 | 2007-10-18 23:39:35 -0700 | [diff] [blame] | 3427 | #endif |
Alexey Dobriyan | 6ba51e3 | 2014-08-08 14:21:48 -0700 | [diff] [blame] | 3428 | ONE("oom_score", S_IRUGO, proc_oom_score), |
David Rientjes | fa0cbbf | 2012-11-12 17:53:04 -0800 | [diff] [blame] | 3429 | REG("oom_adj", S_IRUGO|S_IWUSR, proc_oom_adj_operations), |
David Rientjes | a63d83f | 2010-08-09 17:19:46 -0700 | [diff] [blame] | 3430 | REG("oom_score_adj", S_IRUGO|S_IWUSR, proc_oom_score_adj_operations), |
Richard Guy Briggs | 4b7d248 | 2019-01-22 17:06:39 -0500 | [diff] [blame] | 3431 | #ifdef CONFIG_AUDIT |
Alexey Dobriyan | 631f9c1 | 2008-11-10 01:32:52 +0300 | [diff] [blame] | 3432 | REG("loginuid", S_IWUSR|S_IRUGO, proc_loginuid_operations), |
Al Viro | 26ec3c6 | 2011-02-15 21:24:05 -0500 | [diff] [blame] | 3433 | REG("sessionid", S_IRUGO, proc_sessionid_operations), |
Eric W. Biederman | 28a6d67 | 2006-10-02 02:17:05 -0700 | [diff] [blame] | 3434 | #endif |
Akinobu Mita | f4f154f | 2006-12-08 02:39:47 -0800 | [diff] [blame] | 3435 | #ifdef CONFIG_FAULT_INJECTION |
Alexey Dobriyan | 631f9c1 | 2008-11-10 01:32:52 +0300 | [diff] [blame] | 3436 | REG("make-it-fail", S_IRUGO|S_IWUSR, proc_fault_inject_operations), |
Akinobu Mita | 1203c8e | 2017-07-14 14:49:57 -0700 | [diff] [blame] | 3437 | REG("fail-nth", 0644, proc_fail_nth_operations), |
Akinobu Mita | f4f154f | 2006-12-08 02:39:47 -0800 | [diff] [blame] | 3438 | #endif |
Andrea Righi | 297c5d9 | 2008-07-25 01:48:49 -0700 | [diff] [blame] | 3439 | #ifdef CONFIG_TASK_IO_ACCOUNTING |
Alexey Dobriyan | 19aadc9 | 2014-08-08 14:21:50 -0700 | [diff] [blame] | 3440 | ONE("io", S_IRUSR, proc_tid_io_accounting), |
Andrea Righi | 297c5d9 | 2008-07-25 01:48:49 -0700 | [diff] [blame] | 3441 | #endif |
Eric W. Biederman | 22d917d | 2011-11-17 00:11:58 -0800 | [diff] [blame] | 3442 | #ifdef CONFIG_USER_NS |
| 3443 | REG("uid_map", S_IRUGO|S_IWUSR, proc_uid_map_operations), |
| 3444 | REG("gid_map", S_IRUGO|S_IWUSR, proc_gid_map_operations), |
Eric W. Biederman | f76d207 | 2012-08-30 01:24:05 -0700 | [diff] [blame] | 3445 | REG("projid_map", S_IRUGO|S_IWUSR, proc_projid_map_operations), |
Eric W. Biederman | 9cc4651 | 2014-12-02 12:27:26 -0600 | [diff] [blame] | 3446 | REG("setgroups", S_IRUGO|S_IWUSR, proc_setgroups_operations), |
Eric W. Biederman | 22d917d | 2011-11-17 00:11:58 -0800 | [diff] [blame] | 3447 | #endif |
Josh Poimboeuf | 7c23b33 | 2017-02-13 19:42:41 -0600 | [diff] [blame] | 3448 | #ifdef CONFIG_LIVEPATCH |
| 3449 | ONE("patch_state", S_IRUSR, proc_pid_patch_state), |
| 3450 | #endif |
Eric W. Biederman | 28a6d67 | 2006-10-02 02:17:05 -0700 | [diff] [blame] | 3451 | }; |
| 3452 | |
Al Viro | f0c3b50 | 2013-05-16 12:07:31 -0400 | [diff] [blame] | 3453 | static int proc_tid_base_readdir(struct file *file, struct dir_context *ctx) |
Eric W. Biederman | 28a6d67 | 2006-10-02 02:17:05 -0700 | [diff] [blame] | 3454 | { |
Al Viro | f0c3b50 | 2013-05-16 12:07:31 -0400 | [diff] [blame] | 3455 | return proc_pident_readdir(file, ctx, |
| 3456 | tid_base_stuff, ARRAY_SIZE(tid_base_stuff)); |
Eric W. Biederman | 28a6d67 | 2006-10-02 02:17:05 -0700 | [diff] [blame] | 3457 | } |
| 3458 | |
Al Viro | 00cd8dd | 2012-06-10 17:13:09 -0400 | [diff] [blame] | 3459 | static struct dentry *proc_tid_base_lookup(struct inode *dir, struct dentry *dentry, unsigned int flags) |
| 3460 | { |
Eric W. Biederman | 7bcd6b0 | 2006-10-02 02:18:56 -0700 | [diff] [blame] | 3461 | return proc_pident_lookup(dir, dentry, |
Alexey Dobriyan | d5a572a | 2019-03-11 23:28:51 -0700 | [diff] [blame] | 3462 | tid_base_stuff, |
| 3463 | tid_base_stuff + ARRAY_SIZE(tid_base_stuff)); |
Eric W. Biederman | 28a6d67 | 2006-10-02 02:17:05 -0700 | [diff] [blame] | 3464 | } |
| 3465 | |
Arjan van de Ven | 00977a5 | 2007-02-12 00:55:34 -0800 | [diff] [blame] | 3466 | static const struct file_operations proc_tid_base_operations = { |
Eric W. Biederman | 28a6d67 | 2006-10-02 02:17:05 -0700 | [diff] [blame] | 3467 | .read = generic_read_dir, |
Al Viro | f50752e | 2016-04-20 17:13:54 -0400 | [diff] [blame] | 3468 | .iterate_shared = proc_tid_base_readdir, |
| 3469 | .llseek = generic_file_llseek, |
Eric W. Biederman | 28a6d67 | 2006-10-02 02:17:05 -0700 | [diff] [blame] | 3470 | }; |
| 3471 | |
Arjan van de Ven | c5ef1c4 | 2007-02-12 00:55:40 -0800 | [diff] [blame] | 3472 | static const struct inode_operations proc_tid_base_inode_operations = { |
Eric W. Biederman | 28a6d67 | 2006-10-02 02:17:05 -0700 | [diff] [blame] | 3473 | .lookup = proc_tid_base_lookup, |
| 3474 | .getattr = pid_getattr, |
| 3475 | .setattr = proc_setattr, |
| 3476 | }; |
| 3477 | |
Al Viro | 0168b9e | 2018-05-03 09:21:05 -0400 | [diff] [blame] | 3478 | static struct dentry *proc_task_instantiate(struct dentry *dentry, |
| 3479 | struct task_struct *task, const void *ptr) |
Eric W. Biederman | 444ceed | 2006-10-02 02:18:49 -0700 | [diff] [blame] | 3480 | { |
Eric W. Biederman | 444ceed | 2006-10-02 02:18:49 -0700 | [diff] [blame] | 3481 | struct inode *inode; |
Al Viro | 0168b9e | 2018-05-03 09:21:05 -0400 | [diff] [blame] | 3482 | inode = proc_pid_make_inode(dentry->d_sb, task, S_IFDIR | S_IRUGO | S_IXUGO); |
Eric W. Biederman | 444ceed | 2006-10-02 02:18:49 -0700 | [diff] [blame] | 3483 | if (!inode) |
Al Viro | 0168b9e | 2018-05-03 09:21:05 -0400 | [diff] [blame] | 3484 | return ERR_PTR(-ENOENT); |
Al Viro | 1bbc551 | 2018-05-02 21:26:16 -0400 | [diff] [blame] | 3485 | |
Eric W. Biederman | 444ceed | 2006-10-02 02:18:49 -0700 | [diff] [blame] | 3486 | inode->i_op = &proc_tid_base_inode_operations; |
| 3487 | inode->i_fop = &proc_tid_base_operations; |
Al Viro | 1bbc551 | 2018-05-02 21:26:16 -0400 | [diff] [blame] | 3488 | inode->i_flags |= S_IMMUTABLE; |
Vegard Nossum | aed5417 | 2008-06-05 22:46:53 -0700 | [diff] [blame] | 3489 | |
Alexey Dobriyan | 1270dd8 | 2016-12-12 16:45:32 -0800 | [diff] [blame] | 3490 | set_nlink(inode, nlink_tid); |
Al Viro | 1bbc551 | 2018-05-02 21:26:16 -0400 | [diff] [blame] | 3491 | pid_update_inode(task, inode); |
Eric W. Biederman | 444ceed | 2006-10-02 02:18:49 -0700 | [diff] [blame] | 3492 | |
Nick Piggin | fb045ad | 2011-01-07 17:49:55 +1100 | [diff] [blame] | 3493 | d_set_d_op(dentry, &pid_dentry_operations); |
Al Viro | 0168b9e | 2018-05-03 09:21:05 -0400 | [diff] [blame] | 3494 | return d_splice_alias(inode, dentry); |
Eric W. Biederman | 444ceed | 2006-10-02 02:18:49 -0700 | [diff] [blame] | 3495 | } |
| 3496 | |
Al Viro | 00cd8dd | 2012-06-10 17:13:09 -0400 | [diff] [blame] | 3497 | static struct dentry *proc_task_lookup(struct inode *dir, struct dentry * dentry, unsigned int flags) |
Eric W. Biederman | 28a6d67 | 2006-10-02 02:17:05 -0700 | [diff] [blame] | 3498 | { |
Eric W. Biederman | 28a6d67 | 2006-10-02 02:17:05 -0700 | [diff] [blame] | 3499 | struct task_struct *task; |
| 3500 | struct task_struct *leader = get_proc_task(dir); |
Eric W. Biederman | 28a6d67 | 2006-10-02 02:17:05 -0700 | [diff] [blame] | 3501 | unsigned tid; |
Pavel Emelyanov | b488893 | 2007-10-18 23:40:14 -0700 | [diff] [blame] | 3502 | struct pid_namespace *ns; |
Al Viro | 0168b9e | 2018-05-03 09:21:05 -0400 | [diff] [blame] | 3503 | struct dentry *result = ERR_PTR(-ENOENT); |
Eric W. Biederman | 28a6d67 | 2006-10-02 02:17:05 -0700 | [diff] [blame] | 3504 | |
| 3505 | if (!leader) |
| 3506 | goto out_no_task; |
| 3507 | |
Alexey Dobriyan | dbcdb50 | 2014-08-08 14:21:25 -0700 | [diff] [blame] | 3508 | tid = name_to_int(&dentry->d_name); |
Eric W. Biederman | 28a6d67 | 2006-10-02 02:17:05 -0700 | [diff] [blame] | 3509 | if (tid == ~0U) |
| 3510 | goto out; |
| 3511 | |
Pavel Emelyanov | b488893 | 2007-10-18 23:40:14 -0700 | [diff] [blame] | 3512 | ns = dentry->d_sb->s_fs_info; |
Eric W. Biederman | 28a6d67 | 2006-10-02 02:17:05 -0700 | [diff] [blame] | 3513 | rcu_read_lock(); |
Pavel Emelyanov | b488893 | 2007-10-18 23:40:14 -0700 | [diff] [blame] | 3514 | task = find_task_by_pid_ns(tid, ns); |
Eric W. Biederman | 28a6d67 | 2006-10-02 02:17:05 -0700 | [diff] [blame] | 3515 | if (task) |
| 3516 | get_task_struct(task); |
| 3517 | rcu_read_unlock(); |
| 3518 | if (!task) |
| 3519 | goto out; |
Pavel Emelyanov | bac0abd | 2007-10-18 23:40:18 -0700 | [diff] [blame] | 3520 | if (!same_thread_group(leader, task)) |
Eric W. Biederman | 28a6d67 | 2006-10-02 02:17:05 -0700 | [diff] [blame] | 3521 | goto out_drop_task; |
| 3522 | |
Al Viro | 0168b9e | 2018-05-03 09:21:05 -0400 | [diff] [blame] | 3523 | result = proc_task_instantiate(dentry, task, NULL); |
Eric W. Biederman | 28a6d67 | 2006-10-02 02:17:05 -0700 | [diff] [blame] | 3524 | out_drop_task: |
| 3525 | put_task_struct(task); |
| 3526 | out: |
| 3527 | put_task_struct(leader); |
| 3528 | out_no_task: |
Al Viro | 0168b9e | 2018-05-03 09:21:05 -0400 | [diff] [blame] | 3529 | return result; |
Eric W. Biederman | 28a6d67 | 2006-10-02 02:17:05 -0700 | [diff] [blame] | 3530 | } |
| 3531 | |
| 3532 | /* |
Eric W. Biederman | 0bc58a9 | 2006-06-26 00:25:50 -0700 | [diff] [blame] | 3533 | * Find the first tid of a thread group to return to user space. |
| 3534 | * |
| 3535 | * Usually this is just the thread group leader, but if the users |
| 3536 | * buffer was too small or there was a seek into the middle of the |
| 3537 | * directory we have more work todo. |
| 3538 | * |
| 3539 | * In the case of a short read we start with find_task_by_pid. |
| 3540 | * |
| 3541 | * In the case of a seek we start with the leader and walk nr |
| 3542 | * threads past it. |
| 3543 | */ |
Oleg Nesterov | 9f6e963 | 2014-01-23 15:55:40 -0800 | [diff] [blame] | 3544 | static struct task_struct *first_tid(struct pid *pid, int tid, loff_t f_pos, |
| 3545 | struct pid_namespace *ns) |
Eric W. Biederman | 0bc58a9 | 2006-06-26 00:25:50 -0700 | [diff] [blame] | 3546 | { |
Oleg Nesterov | d855a4b | 2014-01-23 15:55:39 -0800 | [diff] [blame] | 3547 | struct task_struct *pos, *task; |
Oleg Nesterov | 9f6e963 | 2014-01-23 15:55:40 -0800 | [diff] [blame] | 3548 | unsigned long nr = f_pos; |
| 3549 | |
| 3550 | if (nr != f_pos) /* 32bit overflow? */ |
| 3551 | return NULL; |
Eric W. Biederman | 0bc58a9 | 2006-06-26 00:25:50 -0700 | [diff] [blame] | 3552 | |
Eric W. Biederman | cc28873 | 2006-06-26 00:26:01 -0700 | [diff] [blame] | 3553 | rcu_read_lock(); |
Oleg Nesterov | d855a4b | 2014-01-23 15:55:39 -0800 | [diff] [blame] | 3554 | task = pid_task(pid, PIDTYPE_PID); |
| 3555 | if (!task) |
| 3556 | goto fail; |
| 3557 | |
| 3558 | /* Attempt to start with the tid of a thread */ |
Oleg Nesterov | 9f6e963 | 2014-01-23 15:55:40 -0800 | [diff] [blame] | 3559 | if (tid && nr) { |
Pavel Emelyanov | b488893 | 2007-10-18 23:40:14 -0700 | [diff] [blame] | 3560 | pos = find_task_by_pid_ns(tid, ns); |
Oleg Nesterov | d855a4b | 2014-01-23 15:55:39 -0800 | [diff] [blame] | 3561 | if (pos && same_thread_group(pos, task)) |
Oleg Nesterov | a872ff0 | 2006-06-26 00:26:01 -0700 | [diff] [blame] | 3562 | goto found; |
Eric W. Biederman | 0bc58a9 | 2006-06-26 00:25:50 -0700 | [diff] [blame] | 3563 | } |
| 3564 | |
| 3565 | /* If nr exceeds the number of threads there is nothing todo */ |
Oleg Nesterov | 9f6e963 | 2014-01-23 15:55:40 -0800 | [diff] [blame] | 3566 | if (nr >= get_nr_threads(task)) |
Oleg Nesterov | c986c14 | 2014-01-23 15:55:38 -0800 | [diff] [blame] | 3567 | goto fail; |
Oleg Nesterov | a872ff0 | 2006-06-26 00:26:01 -0700 | [diff] [blame] | 3568 | |
| 3569 | /* If we haven't found our starting place yet start |
| 3570 | * with the leader and walk nr threads forward. |
| 3571 | */ |
Oleg Nesterov | d855a4b | 2014-01-23 15:55:39 -0800 | [diff] [blame] | 3572 | pos = task = task->group_leader; |
Oleg Nesterov | c986c14 | 2014-01-23 15:55:38 -0800 | [diff] [blame] | 3573 | do { |
Oleg Nesterov | 9f6e963 | 2014-01-23 15:55:40 -0800 | [diff] [blame] | 3574 | if (!nr--) |
Oleg Nesterov | c986c14 | 2014-01-23 15:55:38 -0800 | [diff] [blame] | 3575 | goto found; |
Oleg Nesterov | d855a4b | 2014-01-23 15:55:39 -0800 | [diff] [blame] | 3576 | } while_each_thread(task, pos); |
Oleg Nesterov | c986c14 | 2014-01-23 15:55:38 -0800 | [diff] [blame] | 3577 | fail: |
| 3578 | pos = NULL; |
| 3579 | goto out; |
Oleg Nesterov | a872ff0 | 2006-06-26 00:26:01 -0700 | [diff] [blame] | 3580 | found: |
| 3581 | get_task_struct(pos); |
| 3582 | out: |
Eric W. Biederman | cc28873 | 2006-06-26 00:26:01 -0700 | [diff] [blame] | 3583 | rcu_read_unlock(); |
Eric W. Biederman | 0bc58a9 | 2006-06-26 00:25:50 -0700 | [diff] [blame] | 3584 | return pos; |
| 3585 | } |
| 3586 | |
| 3587 | /* |
| 3588 | * Find the next thread in the thread list. |
| 3589 | * Return NULL if there is an error or no next thread. |
| 3590 | * |
| 3591 | * The reference to the input task_struct is released. |
| 3592 | */ |
| 3593 | static struct task_struct *next_tid(struct task_struct *start) |
| 3594 | { |
Oleg Nesterov | c1df7fb | 2006-06-26 00:26:02 -0700 | [diff] [blame] | 3595 | struct task_struct *pos = NULL; |
Eric W. Biederman | cc28873 | 2006-06-26 00:26:01 -0700 | [diff] [blame] | 3596 | rcu_read_lock(); |
Oleg Nesterov | c1df7fb | 2006-06-26 00:26:02 -0700 | [diff] [blame] | 3597 | if (pid_alive(start)) { |
Eric W. Biederman | 0bc58a9 | 2006-06-26 00:25:50 -0700 | [diff] [blame] | 3598 | pos = next_thread(start); |
Oleg Nesterov | c1df7fb | 2006-06-26 00:26:02 -0700 | [diff] [blame] | 3599 | if (thread_group_leader(pos)) |
| 3600 | pos = NULL; |
| 3601 | else |
| 3602 | get_task_struct(pos); |
| 3603 | } |
Eric W. Biederman | cc28873 | 2006-06-26 00:26:01 -0700 | [diff] [blame] | 3604 | rcu_read_unlock(); |
Eric W. Biederman | 0bc58a9 | 2006-06-26 00:25:50 -0700 | [diff] [blame] | 3605 | put_task_struct(start); |
| 3606 | return pos; |
| 3607 | } |
| 3608 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3609 | /* for the /proc/TGID/task/ directories */ |
Al Viro | f0c3b50 | 2013-05-16 12:07:31 -0400 | [diff] [blame] | 3610 | static int proc_task_readdir(struct file *file, struct dir_context *ctx) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3611 | { |
Oleg Nesterov | d855a4b | 2014-01-23 15:55:39 -0800 | [diff] [blame] | 3612 | struct inode *inode = file_inode(file); |
| 3613 | struct task_struct *task; |
Pavel Emelyanov | b488893 | 2007-10-18 23:40:14 -0700 | [diff] [blame] | 3614 | struct pid_namespace *ns; |
Al Viro | f0c3b50 | 2013-05-16 12:07:31 -0400 | [diff] [blame] | 3615 | int tid; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3616 | |
Oleg Nesterov | d855a4b | 2014-01-23 15:55:39 -0800 | [diff] [blame] | 3617 | if (proc_inode_is_dead(inode)) |
Al Viro | f0c3b50 | 2013-05-16 12:07:31 -0400 | [diff] [blame] | 3618 | return -ENOENT; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3619 | |
Al Viro | f0c3b50 | 2013-05-16 12:07:31 -0400 | [diff] [blame] | 3620 | if (!dir_emit_dots(file, ctx)) |
Oleg Nesterov | d855a4b | 2014-01-23 15:55:39 -0800 | [diff] [blame] | 3621 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3622 | |
Eric W. Biederman | 0bc58a9 | 2006-06-26 00:25:50 -0700 | [diff] [blame] | 3623 | /* f_version caches the tgid value that the last readdir call couldn't |
| 3624 | * return. lseek aka telldir automagically resets f_version to 0. |
| 3625 | */ |
Christoph Hellwig | 76f668b | 2018-05-16 07:19:01 +0200 | [diff] [blame] | 3626 | ns = proc_pid_ns(inode); |
Al Viro | f0c3b50 | 2013-05-16 12:07:31 -0400 | [diff] [blame] | 3627 | tid = (int)file->f_version; |
| 3628 | file->f_version = 0; |
Oleg Nesterov | d855a4b | 2014-01-23 15:55:39 -0800 | [diff] [blame] | 3629 | for (task = first_tid(proc_pid(inode), tid, ctx->pos - 2, ns); |
Eric W. Biederman | 0bc58a9 | 2006-06-26 00:25:50 -0700 | [diff] [blame] | 3630 | task; |
Al Viro | f0c3b50 | 2013-05-16 12:07:31 -0400 | [diff] [blame] | 3631 | task = next_tid(task), ctx->pos++) { |
Alexey Dobriyan | e3912ac | 2018-02-06 15:36:51 -0800 | [diff] [blame] | 3632 | char name[10 + 1]; |
Alexey Dobriyan | a4ef389 | 2018-06-07 17:10:10 -0700 | [diff] [blame] | 3633 | unsigned int len; |
Pavel Emelyanov | b488893 | 2007-10-18 23:40:14 -0700 | [diff] [blame] | 3634 | tid = task_pid_nr_ns(task, ns); |
Alexey Dobriyan | e3912ac | 2018-02-06 15:36:51 -0800 | [diff] [blame] | 3635 | len = snprintf(name, sizeof(name), "%u", tid); |
Al Viro | f0c3b50 | 2013-05-16 12:07:31 -0400 | [diff] [blame] | 3636 | if (!proc_fill_cache(file, ctx, name, len, |
| 3637 | proc_task_instantiate, task, NULL)) { |
Eric W. Biederman | 0bc58a9 | 2006-06-26 00:25:50 -0700 | [diff] [blame] | 3638 | /* returning this tgid failed, save it as the first |
| 3639 | * pid for the next readir call */ |
Al Viro | f0c3b50 | 2013-05-16 12:07:31 -0400 | [diff] [blame] | 3640 | file->f_version = (u64)tid; |
Eric W. Biederman | 0bc58a9 | 2006-06-26 00:25:50 -0700 | [diff] [blame] | 3641 | put_task_struct(task); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3642 | break; |
Eric W. Biederman | 0bc58a9 | 2006-06-26 00:25:50 -0700 | [diff] [blame] | 3643 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3644 | } |
Oleg Nesterov | d855a4b | 2014-01-23 15:55:39 -0800 | [diff] [blame] | 3645 | |
Al Viro | f0c3b50 | 2013-05-16 12:07:31 -0400 | [diff] [blame] | 3646 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3647 | } |
Eric W. Biederman | 6e66b52 | 2006-06-26 00:25:47 -0700 | [diff] [blame] | 3648 | |
David Howells | a528d35 | 2017-01-31 16:46:22 +0000 | [diff] [blame] | 3649 | static int proc_task_getattr(const struct path *path, struct kstat *stat, |
| 3650 | u32 request_mask, unsigned int query_flags) |
Eric W. Biederman | 6e66b52 | 2006-06-26 00:25:47 -0700 | [diff] [blame] | 3651 | { |
David Howells | a528d35 | 2017-01-31 16:46:22 +0000 | [diff] [blame] | 3652 | struct inode *inode = d_inode(path->dentry); |
Eric W. Biederman | 99f8955 | 2006-06-26 00:25:55 -0700 | [diff] [blame] | 3653 | struct task_struct *p = get_proc_task(inode); |
Eric W. Biederman | 6e66b52 | 2006-06-26 00:25:47 -0700 | [diff] [blame] | 3654 | generic_fillattr(inode, stat); |
| 3655 | |
Eric W. Biederman | 99f8955 | 2006-06-26 00:25:55 -0700 | [diff] [blame] | 3656 | if (p) { |
Eric W. Biederman | 99f8955 | 2006-06-26 00:25:55 -0700 | [diff] [blame] | 3657 | stat->nlink += get_nr_threads(p); |
Eric W. Biederman | 99f8955 | 2006-06-26 00:25:55 -0700 | [diff] [blame] | 3658 | put_task_struct(p); |
Eric W. Biederman | 6e66b52 | 2006-06-26 00:25:47 -0700 | [diff] [blame] | 3659 | } |
| 3660 | |
| 3661 | return 0; |
| 3662 | } |
Eric W. Biederman | 28a6d67 | 2006-10-02 02:17:05 -0700 | [diff] [blame] | 3663 | |
Arjan van de Ven | c5ef1c4 | 2007-02-12 00:55:40 -0800 | [diff] [blame] | 3664 | static const struct inode_operations proc_task_inode_operations = { |
Eric W. Biederman | 28a6d67 | 2006-10-02 02:17:05 -0700 | [diff] [blame] | 3665 | .lookup = proc_task_lookup, |
| 3666 | .getattr = proc_task_getattr, |
| 3667 | .setattr = proc_setattr, |
Vasiliy Kulikov | 0499680 | 2012-01-10 15:11:31 -0800 | [diff] [blame] | 3668 | .permission = proc_pid_permission, |
Eric W. Biederman | 28a6d67 | 2006-10-02 02:17:05 -0700 | [diff] [blame] | 3669 | }; |
| 3670 | |
Arjan van de Ven | 00977a5 | 2007-02-12 00:55:34 -0800 | [diff] [blame] | 3671 | static const struct file_operations proc_task_operations = { |
Eric W. Biederman | 28a6d67 | 2006-10-02 02:17:05 -0700 | [diff] [blame] | 3672 | .read = generic_read_dir, |
Al Viro | f50752e | 2016-04-20 17:13:54 -0400 | [diff] [blame] | 3673 | .iterate_shared = proc_task_readdir, |
| 3674 | .llseek = generic_file_llseek, |
Eric W. Biederman | 28a6d67 | 2006-10-02 02:17:05 -0700 | [diff] [blame] | 3675 | }; |
Alexey Dobriyan | 1270dd8 | 2016-12-12 16:45:32 -0800 | [diff] [blame] | 3676 | |
| 3677 | void __init set_proc_pid_nlink(void) |
| 3678 | { |
| 3679 | nlink_tid = pid_entry_nlink(tid_base_stuff, ARRAY_SIZE(tid_base_stuff)); |
| 3680 | nlink_tgid = pid_entry_nlink(tgid_base_stuff, ARRAY_SIZE(tgid_base_stuff)); |
| 3681 | } |