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