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