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