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