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