Greg Kroah-Hartman | b244131 | 2017-11-01 15:07:57 +0100 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0 |
Ingo Molnar | 3f07c01 | 2017-02-08 18:51:30 +0100 | [diff] [blame] | 2 | #include <linux/sched/signal.h> |
Cyrill Gorcunov | faf60af | 2012-08-23 14:43:24 +0400 | [diff] [blame] | 3 | #include <linux/errno.h> |
| 4 | #include <linux/dcache.h> |
| 5 | #include <linux/path.h> |
| 6 | #include <linux/fdtable.h> |
| 7 | #include <linux/namei.h> |
| 8 | #include <linux/pid.h> |
Kalesh Singh | 7bc3fa0 | 2021-06-30 18:54:44 -0700 | [diff] [blame] | 9 | #include <linux/ptrace.h> |
Cyrill Gorcunov | faf60af | 2012-08-23 14:43:24 +0400 | [diff] [blame] | 10 | #include <linux/security.h> |
Cyrill Gorcunov | ddd3e07 | 2012-08-26 18:28:20 +0400 | [diff] [blame] | 11 | #include <linux/file.h> |
| 12 | #include <linux/seq_file.h> |
Andrey Vagin | 6c8c903 | 2015-04-16 12:49:38 -0700 | [diff] [blame] | 13 | #include <linux/fs.h> |
Cyrill Gorcunov | faf60af | 2012-08-23 14:43:24 +0400 | [diff] [blame] | 14 | |
| 15 | #include <linux/proc_fs.h> |
| 16 | |
Andrey Vagin | 49d063c | 2014-04-07 15:38:34 -0700 | [diff] [blame] | 17 | #include "../mount.h" |
Cyrill Gorcunov | faf60af | 2012-08-23 14:43:24 +0400 | [diff] [blame] | 18 | #include "internal.h" |
| 19 | #include "fd.h" |
| 20 | |
Cyrill Gorcunov | ddd3e07 | 2012-08-26 18:28:20 +0400 | [diff] [blame] | 21 | static int seq_show(struct seq_file *m, void *v) |
Cyrill Gorcunov | faf60af | 2012-08-23 14:43:24 +0400 | [diff] [blame] | 22 | { |
Cyrill Gorcunov | faf60af | 2012-08-23 14:43:24 +0400 | [diff] [blame] | 23 | struct files_struct *files = NULL; |
Cyrill Gorcunov | ddd3e07 | 2012-08-26 18:28:20 +0400 | [diff] [blame] | 24 | int f_flags = 0, ret = -ENOENT; |
| 25 | struct file *file = NULL; |
| 26 | struct task_struct *task; |
Cyrill Gorcunov | faf60af | 2012-08-23 14:43:24 +0400 | [diff] [blame] | 27 | |
Cyrill Gorcunov | ddd3e07 | 2012-08-26 18:28:20 +0400 | [diff] [blame] | 28 | task = get_proc_task(m->private); |
| 29 | if (!task) |
| 30 | return -ENOENT; |
| 31 | |
Eric W. Biederman | 775e065 | 2020-11-20 17:14:34 -0600 | [diff] [blame] | 32 | task_lock(task); |
| 33 | files = task->files; |
Cyrill Gorcunov | faf60af | 2012-08-23 14:43:24 +0400 | [diff] [blame] | 34 | if (files) { |
Alexey Dobriyan | 771187d | 2016-09-02 00:42:02 +0300 | [diff] [blame] | 35 | unsigned int fd = proc_fd(m->private); |
Cyrill Gorcunov | ddd3e07 | 2012-08-26 18:28:20 +0400 | [diff] [blame] | 36 | |
Cyrill Gorcunov | faf60af | 2012-08-23 14:43:24 +0400 | [diff] [blame] | 37 | spin_lock(&files->file_lock); |
Eric W. Biederman | 120ce2b | 2020-11-20 17:14:25 -0600 | [diff] [blame] | 38 | file = files_lookup_fd_locked(files, fd); |
Cyrill Gorcunov | faf60af | 2012-08-23 14:43:24 +0400 | [diff] [blame] | 39 | if (file) { |
Cyrill Gorcunov | ddd3e07 | 2012-08-26 18:28:20 +0400 | [diff] [blame] | 40 | struct fdtable *fdt = files_fdtable(files); |
Cyrill Gorcunov | faf60af | 2012-08-23 14:43:24 +0400 | [diff] [blame] | 41 | |
Al Viro | c6f3d81 | 2012-08-26 11:01:04 -0400 | [diff] [blame] | 42 | f_flags = file->f_flags; |
Cyrill Gorcunov | faf60af | 2012-08-23 14:43:24 +0400 | [diff] [blame] | 43 | if (close_on_exec(fd, fdt)) |
| 44 | f_flags |= O_CLOEXEC; |
| 45 | |
Cyrill Gorcunov | ddd3e07 | 2012-08-26 18:28:20 +0400 | [diff] [blame] | 46 | get_file(file); |
| 47 | ret = 0; |
Cyrill Gorcunov | faf60af | 2012-08-23 14:43:24 +0400 | [diff] [blame] | 48 | } |
| 49 | spin_unlock(&files->file_lock); |
Cyrill Gorcunov | faf60af | 2012-08-23 14:43:24 +0400 | [diff] [blame] | 50 | } |
Eric W. Biederman | 775e065 | 2020-11-20 17:14:34 -0600 | [diff] [blame] | 51 | task_unlock(task); |
| 52 | put_task_struct(task); |
Cyrill Gorcunov | ddd3e07 | 2012-08-26 18:28:20 +0400 | [diff] [blame] | 53 | |
Andrey Vagin | 6c8c903 | 2015-04-16 12:49:38 -0700 | [diff] [blame] | 54 | if (ret) |
| 55 | return ret; |
Cyrill Gorcunov | ddd3e07 | 2012-08-26 18:28:20 +0400 | [diff] [blame] | 56 | |
Kalesh Singh | 3845f25 | 2021-06-30 18:54:49 -0700 | [diff] [blame] | 57 | seq_printf(m, "pos:\t%lli\nflags:\t0%o\nmnt_id:\t%i\nino:\t%lu\n", |
Andrey Vagin | 6c8c903 | 2015-04-16 12:49:38 -0700 | [diff] [blame] | 58 | (long long)file->f_pos, f_flags, |
Kalesh Singh | 3845f25 | 2021-06-30 18:54:49 -0700 | [diff] [blame] | 59 | real_mount(file->f_path.mnt)->mnt_id, |
| 60 | file_inode(file)->i_ino); |
Andrey Vagin | 6c8c903 | 2015-04-16 12:49:38 -0700 | [diff] [blame] | 61 | |
Eric W. Biederman | 775e065 | 2020-11-20 17:14:34 -0600 | [diff] [blame] | 62 | /* show_fd_locks() never deferences files so a stale value is safe */ |
Andrey Vagin | 6c8c903 | 2015-04-16 12:49:38 -0700 | [diff] [blame] | 63 | show_fd_locks(m, file, files); |
| 64 | if (seq_has_overflowed(m)) |
| 65 | goto out; |
| 66 | |
| 67 | if (file->f_op->show_fdinfo) |
| 68 | file->f_op->show_fdinfo(m, file); |
| 69 | |
| 70 | out: |
| 71 | fput(file); |
| 72 | return 0; |
Cyrill Gorcunov | faf60af | 2012-08-23 14:43:24 +0400 | [diff] [blame] | 73 | } |
| 74 | |
Cyrill Gorcunov | ddd3e07 | 2012-08-26 18:28:20 +0400 | [diff] [blame] | 75 | static int seq_fdinfo_open(struct inode *inode, struct file *file) |
| 76 | { |
Kalesh Singh | 7bc3fa0 | 2021-06-30 18:54:44 -0700 | [diff] [blame] | 77 | bool allowed = false; |
| 78 | struct task_struct *task = get_proc_task(inode); |
| 79 | |
| 80 | if (!task) |
| 81 | return -ESRCH; |
| 82 | |
| 83 | allowed = ptrace_may_access(task, PTRACE_MODE_READ_FSCREDS); |
| 84 | put_task_struct(task); |
| 85 | |
| 86 | if (!allowed) |
| 87 | return -EACCES; |
| 88 | |
Cyrill Gorcunov | ddd3e07 | 2012-08-26 18:28:20 +0400 | [diff] [blame] | 89 | return single_open(file, seq_show, inode); |
| 90 | } |
| 91 | |
| 92 | static const struct file_operations proc_fdinfo_file_operations = { |
| 93 | .open = seq_fdinfo_open, |
| 94 | .read = seq_read, |
| 95 | .llseek = seq_lseek, |
| 96 | .release = single_release, |
| 97 | }; |
| 98 | |
Al Viro | 1ae9bd8 | 2018-05-02 22:42:22 -0400 | [diff] [blame] | 99 | static bool tid_fd_mode(struct task_struct *task, unsigned fd, fmode_t *mode) |
| 100 | { |
Al Viro | 1ae9bd8 | 2018-05-02 22:42:22 -0400 | [diff] [blame] | 101 | struct file *file; |
| 102 | |
Al Viro | 1ae9bd8 | 2018-05-02 22:42:22 -0400 | [diff] [blame] | 103 | rcu_read_lock(); |
Eric W. Biederman | 64eb661 | 2020-11-20 17:14:29 -0600 | [diff] [blame] | 104 | file = task_lookup_fd_rcu(task, fd); |
Al Viro | 1ae9bd8 | 2018-05-02 22:42:22 -0400 | [diff] [blame] | 105 | if (file) |
| 106 | *mode = file->f_mode; |
| 107 | rcu_read_unlock(); |
Al Viro | 1ae9bd8 | 2018-05-02 22:42:22 -0400 | [diff] [blame] | 108 | return !!file; |
| 109 | } |
| 110 | |
Al Viro | 9883638 | 2018-05-03 09:00:33 -0400 | [diff] [blame] | 111 | static void tid_fd_update_inode(struct task_struct *task, struct inode *inode, |
| 112 | fmode_t f_mode) |
| 113 | { |
| 114 | task_dump_owner(task, 0, &inode->i_uid, &inode->i_gid); |
| 115 | |
| 116 | if (S_ISLNK(inode->i_mode)) { |
| 117 | unsigned i_mode = S_IFLNK; |
| 118 | if (f_mode & FMODE_READ) |
| 119 | i_mode |= S_IRUSR | S_IXUSR; |
| 120 | if (f_mode & FMODE_WRITE) |
| 121 | i_mode |= S_IWUSR | S_IXUSR; |
| 122 | inode->i_mode = i_mode; |
| 123 | } |
| 124 | security_task_to_inode(task, inode); |
| 125 | } |
| 126 | |
Cyrill Gorcunov | faf60af | 2012-08-23 14:43:24 +0400 | [diff] [blame] | 127 | static int tid_fd_revalidate(struct dentry *dentry, unsigned int flags) |
| 128 | { |
Cyrill Gorcunov | faf60af | 2012-08-23 14:43:24 +0400 | [diff] [blame] | 129 | struct task_struct *task; |
Cyrill Gorcunov | faf60af | 2012-08-23 14:43:24 +0400 | [diff] [blame] | 130 | struct inode *inode; |
Alexey Dobriyan | 771187d | 2016-09-02 00:42:02 +0300 | [diff] [blame] | 131 | unsigned int fd; |
Cyrill Gorcunov | faf60af | 2012-08-23 14:43:24 +0400 | [diff] [blame] | 132 | |
| 133 | if (flags & LOOKUP_RCU) |
| 134 | return -ECHILD; |
| 135 | |
David Howells | 2b0143b | 2015-03-17 22:25:59 +0000 | [diff] [blame] | 136 | inode = d_inode(dentry); |
Cyrill Gorcunov | faf60af | 2012-08-23 14:43:24 +0400 | [diff] [blame] | 137 | task = get_proc_task(inode); |
| 138 | fd = proc_fd(inode); |
| 139 | |
| 140 | if (task) { |
Al Viro | 9883638 | 2018-05-03 09:00:33 -0400 | [diff] [blame] | 141 | fmode_t f_mode; |
Al Viro | 1ae9bd8 | 2018-05-02 22:42:22 -0400 | [diff] [blame] | 142 | if (tid_fd_mode(task, fd, &f_mode)) { |
Al Viro | 9883638 | 2018-05-03 09:00:33 -0400 | [diff] [blame] | 143 | tid_fd_update_inode(task, inode, f_mode); |
Al Viro | 1ae9bd8 | 2018-05-02 22:42:22 -0400 | [diff] [blame] | 144 | put_task_struct(task); |
| 145 | return 1; |
Cyrill Gorcunov | faf60af | 2012-08-23 14:43:24 +0400 | [diff] [blame] | 146 | } |
| 147 | put_task_struct(task); |
| 148 | } |
Cyrill Gorcunov | faf60af | 2012-08-23 14:43:24 +0400 | [diff] [blame] | 149 | return 0; |
| 150 | } |
| 151 | |
| 152 | static const struct dentry_operations tid_fd_dentry_operations = { |
| 153 | .d_revalidate = tid_fd_revalidate, |
| 154 | .d_delete = pid_delete_dentry, |
| 155 | }; |
| 156 | |
| 157 | static int proc_fd_link(struct dentry *dentry, struct path *path) |
| 158 | { |
Cyrill Gorcunov | ddd3e07 | 2012-08-26 18:28:20 +0400 | [diff] [blame] | 159 | struct task_struct *task; |
| 160 | int ret = -ENOENT; |
| 161 | |
David Howells | 2b0143b | 2015-03-17 22:25:59 +0000 | [diff] [blame] | 162 | task = get_proc_task(d_inode(dentry)); |
Cyrill Gorcunov | ddd3e07 | 2012-08-26 18:28:20 +0400 | [diff] [blame] | 163 | if (task) { |
Alexey Dobriyan | 771187d | 2016-09-02 00:42:02 +0300 | [diff] [blame] | 164 | unsigned int fd = proc_fd(d_inode(dentry)); |
Cyrill Gorcunov | ddd3e07 | 2012-08-26 18:28:20 +0400 | [diff] [blame] | 165 | struct file *fd_file; |
| 166 | |
Eric W. Biederman | 439be32 | 2020-11-20 17:14:23 -0600 | [diff] [blame] | 167 | fd_file = fget_task(task, fd); |
Cyrill Gorcunov | ddd3e07 | 2012-08-26 18:28:20 +0400 | [diff] [blame] | 168 | if (fd_file) { |
| 169 | *path = fd_file->f_path; |
| 170 | path_get(&fd_file->f_path); |
| 171 | ret = 0; |
Eric W. Biederman | 439be32 | 2020-11-20 17:14:23 -0600 | [diff] [blame] | 172 | fput(fd_file); |
Cyrill Gorcunov | ddd3e07 | 2012-08-26 18:28:20 +0400 | [diff] [blame] | 173 | } |
Eric W. Biederman | 439be32 | 2020-11-20 17:14:23 -0600 | [diff] [blame] | 174 | put_task_struct(task); |
Cyrill Gorcunov | ddd3e07 | 2012-08-26 18:28:20 +0400 | [diff] [blame] | 175 | } |
| 176 | |
| 177 | return ret; |
Cyrill Gorcunov | faf60af | 2012-08-23 14:43:24 +0400 | [diff] [blame] | 178 | } |
| 179 | |
Al Viro | 9883638 | 2018-05-03 09:00:33 -0400 | [diff] [blame] | 180 | struct fd_data { |
| 181 | fmode_t mode; |
| 182 | unsigned fd; |
| 183 | }; |
| 184 | |
Al Viro | 0168b9e | 2018-05-03 09:21:05 -0400 | [diff] [blame] | 185 | static struct dentry *proc_fd_instantiate(struct dentry *dentry, |
| 186 | struct task_struct *task, const void *ptr) |
Cyrill Gorcunov | faf60af | 2012-08-23 14:43:24 +0400 | [diff] [blame] | 187 | { |
Al Viro | 9883638 | 2018-05-03 09:00:33 -0400 | [diff] [blame] | 188 | const struct fd_data *data = ptr; |
Cyrill Gorcunov | faf60af | 2012-08-23 14:43:24 +0400 | [diff] [blame] | 189 | struct proc_inode *ei; |
| 190 | struct inode *inode; |
| 191 | |
Al Viro | 0168b9e | 2018-05-03 09:21:05 -0400 | [diff] [blame] | 192 | inode = proc_pid_make_inode(dentry->d_sb, task, S_IFLNK); |
Cyrill Gorcunov | faf60af | 2012-08-23 14:43:24 +0400 | [diff] [blame] | 193 | if (!inode) |
Al Viro | 0168b9e | 2018-05-03 09:21:05 -0400 | [diff] [blame] | 194 | return ERR_PTR(-ENOENT); |
Cyrill Gorcunov | faf60af | 2012-08-23 14:43:24 +0400 | [diff] [blame] | 195 | |
| 196 | ei = PROC_I(inode); |
Al Viro | 9883638 | 2018-05-03 09:00:33 -0400 | [diff] [blame] | 197 | ei->fd = data->fd; |
Cyrill Gorcunov | faf60af | 2012-08-23 14:43:24 +0400 | [diff] [blame] | 198 | |
Cyrill Gorcunov | faf60af | 2012-08-23 14:43:24 +0400 | [diff] [blame] | 199 | inode->i_op = &proc_pid_link_inode_operations; |
| 200 | inode->i_size = 64; |
| 201 | |
| 202 | ei->op.proc_get_link = proc_fd_link; |
Al Viro | 9883638 | 2018-05-03 09:00:33 -0400 | [diff] [blame] | 203 | tid_fd_update_inode(task, inode, data->mode); |
Cyrill Gorcunov | faf60af | 2012-08-23 14:43:24 +0400 | [diff] [blame] | 204 | |
| 205 | d_set_d_op(dentry, &tid_fd_dentry_operations); |
Al Viro | 0168b9e | 2018-05-03 09:21:05 -0400 | [diff] [blame] | 206 | return d_splice_alias(inode, dentry); |
Cyrill Gorcunov | faf60af | 2012-08-23 14:43:24 +0400 | [diff] [blame] | 207 | } |
| 208 | |
| 209 | static struct dentry *proc_lookupfd_common(struct inode *dir, |
| 210 | struct dentry *dentry, |
| 211 | instantiate_t instantiate) |
| 212 | { |
| 213 | struct task_struct *task = get_proc_task(dir); |
Al Viro | 9883638 | 2018-05-03 09:00:33 -0400 | [diff] [blame] | 214 | struct fd_data data = {.fd = name_to_int(&dentry->d_name)}; |
Al Viro | 0168b9e | 2018-05-03 09:21:05 -0400 | [diff] [blame] | 215 | struct dentry *result = ERR_PTR(-ENOENT); |
Cyrill Gorcunov | faf60af | 2012-08-23 14:43:24 +0400 | [diff] [blame] | 216 | |
| 217 | if (!task) |
| 218 | goto out_no_task; |
Al Viro | 9883638 | 2018-05-03 09:00:33 -0400 | [diff] [blame] | 219 | if (data.fd == ~0U) |
Cyrill Gorcunov | faf60af | 2012-08-23 14:43:24 +0400 | [diff] [blame] | 220 | goto out; |
Al Viro | 9883638 | 2018-05-03 09:00:33 -0400 | [diff] [blame] | 221 | if (!tid_fd_mode(task, data.fd, &data.mode)) |
Al Viro | 1ae9bd8 | 2018-05-02 22:42:22 -0400 | [diff] [blame] | 222 | goto out; |
Cyrill Gorcunov | faf60af | 2012-08-23 14:43:24 +0400 | [diff] [blame] | 223 | |
Al Viro | 0168b9e | 2018-05-03 09:21:05 -0400 | [diff] [blame] | 224 | result = instantiate(dentry, task, &data); |
Cyrill Gorcunov | faf60af | 2012-08-23 14:43:24 +0400 | [diff] [blame] | 225 | out: |
| 226 | put_task_struct(task); |
| 227 | out_no_task: |
Al Viro | 0168b9e | 2018-05-03 09:21:05 -0400 | [diff] [blame] | 228 | return result; |
Cyrill Gorcunov | faf60af | 2012-08-23 14:43:24 +0400 | [diff] [blame] | 229 | } |
| 230 | |
Al Viro | f0c3b50 | 2013-05-16 12:07:31 -0400 | [diff] [blame] | 231 | static int proc_readfd_common(struct file *file, struct dir_context *ctx, |
| 232 | instantiate_t instantiate) |
Cyrill Gorcunov | faf60af | 2012-08-23 14:43:24 +0400 | [diff] [blame] | 233 | { |
Al Viro | f0c3b50 | 2013-05-16 12:07:31 -0400 | [diff] [blame] | 234 | struct task_struct *p = get_proc_task(file_inode(file)); |
Al Viro | f0c3b50 | 2013-05-16 12:07:31 -0400 | [diff] [blame] | 235 | unsigned int fd; |
Cyrill Gorcunov | faf60af | 2012-08-23 14:43:24 +0400 | [diff] [blame] | 236 | |
Cyrill Gorcunov | faf60af | 2012-08-23 14:43:24 +0400 | [diff] [blame] | 237 | if (!p) |
Al Viro | f0c3b50 | 2013-05-16 12:07:31 -0400 | [diff] [blame] | 238 | return -ENOENT; |
Cyrill Gorcunov | faf60af | 2012-08-23 14:43:24 +0400 | [diff] [blame] | 239 | |
Al Viro | f0c3b50 | 2013-05-16 12:07:31 -0400 | [diff] [blame] | 240 | if (!dir_emit_dots(file, ctx)) |
| 241 | goto out; |
Cyrill Gorcunov | faf60af | 2012-08-23 14:43:24 +0400 | [diff] [blame] | 242 | |
Al Viro | f0c3b50 | 2013-05-16 12:07:31 -0400 | [diff] [blame] | 243 | rcu_read_lock(); |
Eric W. Biederman | 5b17b61 | 2020-11-20 17:14:32 -0600 | [diff] [blame] | 244 | for (fd = ctx->pos - 2;; fd++) { |
Al Viro | 9883638 | 2018-05-03 09:00:33 -0400 | [diff] [blame] | 245 | struct file *f; |
| 246 | struct fd_data data; |
Alexey Dobriyan | e3912ac | 2018-02-06 15:36:51 -0800 | [diff] [blame] | 247 | char name[10 + 1]; |
Alexey Dobriyan | a4ef389 | 2018-06-07 17:10:10 -0700 | [diff] [blame] | 248 | unsigned int len; |
Cyrill Gorcunov | faf60af | 2012-08-23 14:43:24 +0400 | [diff] [blame] | 249 | |
Eric W. Biederman | 5b17b61 | 2020-11-20 17:14:32 -0600 | [diff] [blame] | 250 | f = task_lookup_next_fd_rcu(p, &fd); |
| 251 | ctx->pos = fd + 2LL; |
Al Viro | 9883638 | 2018-05-03 09:00:33 -0400 | [diff] [blame] | 252 | if (!f) |
Eric W. Biederman | 5b17b61 | 2020-11-20 17:14:32 -0600 | [diff] [blame] | 253 | break; |
Al Viro | 9883638 | 2018-05-03 09:00:33 -0400 | [diff] [blame] | 254 | data.mode = f->f_mode; |
Al Viro | f0c3b50 | 2013-05-16 12:07:31 -0400 | [diff] [blame] | 255 | rcu_read_unlock(); |
Al Viro | 9883638 | 2018-05-03 09:00:33 -0400 | [diff] [blame] | 256 | data.fd = fd; |
Al Viro | f0c3b50 | 2013-05-16 12:07:31 -0400 | [diff] [blame] | 257 | |
Alexey Dobriyan | 771187d | 2016-09-02 00:42:02 +0300 | [diff] [blame] | 258 | len = snprintf(name, sizeof(name), "%u", fd); |
Al Viro | f0c3b50 | 2013-05-16 12:07:31 -0400 | [diff] [blame] | 259 | if (!proc_fill_cache(file, ctx, |
| 260 | name, len, instantiate, p, |
Al Viro | 9883638 | 2018-05-03 09:00:33 -0400 | [diff] [blame] | 261 | &data)) |
Eric W. Biederman | 5b17b61 | 2020-11-20 17:14:32 -0600 | [diff] [blame] | 262 | goto out; |
Eric Dumazet | 3cc4a84 | 2015-12-03 11:12:07 -0800 | [diff] [blame] | 263 | cond_resched(); |
Al Viro | f0c3b50 | 2013-05-16 12:07:31 -0400 | [diff] [blame] | 264 | rcu_read_lock(); |
Cyrill Gorcunov | faf60af | 2012-08-23 14:43:24 +0400 | [diff] [blame] | 265 | } |
Al Viro | f0c3b50 | 2013-05-16 12:07:31 -0400 | [diff] [blame] | 266 | rcu_read_unlock(); |
Cyrill Gorcunov | faf60af | 2012-08-23 14:43:24 +0400 | [diff] [blame] | 267 | out: |
| 268 | put_task_struct(p); |
Al Viro | f0c3b50 | 2013-05-16 12:07:31 -0400 | [diff] [blame] | 269 | return 0; |
Cyrill Gorcunov | faf60af | 2012-08-23 14:43:24 +0400 | [diff] [blame] | 270 | } |
| 271 | |
Al Viro | f0c3b50 | 2013-05-16 12:07:31 -0400 | [diff] [blame] | 272 | static int proc_readfd(struct file *file, struct dir_context *ctx) |
Cyrill Gorcunov | faf60af | 2012-08-23 14:43:24 +0400 | [diff] [blame] | 273 | { |
Al Viro | f0c3b50 | 2013-05-16 12:07:31 -0400 | [diff] [blame] | 274 | return proc_readfd_common(file, ctx, proc_fd_instantiate); |
Cyrill Gorcunov | faf60af | 2012-08-23 14:43:24 +0400 | [diff] [blame] | 275 | } |
| 276 | |
| 277 | const struct file_operations proc_fd_operations = { |
| 278 | .read = generic_read_dir, |
Al Viro | f50752e | 2016-04-20 17:13:54 -0400 | [diff] [blame] | 279 | .iterate_shared = proc_readfd, |
| 280 | .llseek = generic_file_llseek, |
Cyrill Gorcunov | faf60af | 2012-08-23 14:43:24 +0400 | [diff] [blame] | 281 | }; |
| 282 | |
| 283 | static struct dentry *proc_lookupfd(struct inode *dir, struct dentry *dentry, |
| 284 | unsigned int flags) |
| 285 | { |
| 286 | return proc_lookupfd_common(dir, dentry, proc_fd_instantiate); |
| 287 | } |
| 288 | |
| 289 | /* |
| 290 | * /proc/pid/fd needs a special permission handler so that a process can still |
| 291 | * access /proc/self/fd after it has executed a setuid(). |
| 292 | */ |
Christian Brauner | 549c729 | 2021-01-21 14:19:43 +0100 | [diff] [blame] | 293 | int proc_fd_permission(struct user_namespace *mnt_userns, |
| 294 | struct inode *inode, int mask) |
Cyrill Gorcunov | faf60af | 2012-08-23 14:43:24 +0400 | [diff] [blame] | 295 | { |
Oleg Nesterov | 54708d2 | 2015-11-06 16:30:06 -0800 | [diff] [blame] | 296 | struct task_struct *p; |
| 297 | int rv; |
| 298 | |
Christian Brauner | 47291ba | 2021-01-21 14:19:24 +0100 | [diff] [blame] | 299 | rv = generic_permission(&init_user_ns, inode, mask); |
Cyrill Gorcunov | faf60af | 2012-08-23 14:43:24 +0400 | [diff] [blame] | 300 | if (rv == 0) |
Oleg Nesterov | 54708d2 | 2015-11-06 16:30:06 -0800 | [diff] [blame] | 301 | return rv; |
| 302 | |
| 303 | rcu_read_lock(); |
| 304 | p = pid_task(proc_pid(inode), PIDTYPE_PID); |
| 305 | if (p && same_thread_group(p, current)) |
Cyrill Gorcunov | faf60af | 2012-08-23 14:43:24 +0400 | [diff] [blame] | 306 | rv = 0; |
Oleg Nesterov | 54708d2 | 2015-11-06 16:30:06 -0800 | [diff] [blame] | 307 | rcu_read_unlock(); |
| 308 | |
Cyrill Gorcunov | faf60af | 2012-08-23 14:43:24 +0400 | [diff] [blame] | 309 | return rv; |
| 310 | } |
| 311 | |
| 312 | const struct inode_operations proc_fd_inode_operations = { |
| 313 | .lookup = proc_lookupfd, |
| 314 | .permission = proc_fd_permission, |
| 315 | .setattr = proc_setattr, |
| 316 | }; |
| 317 | |
Al Viro | 0168b9e | 2018-05-03 09:21:05 -0400 | [diff] [blame] | 318 | static struct dentry *proc_fdinfo_instantiate(struct dentry *dentry, |
| 319 | struct task_struct *task, const void *ptr) |
Cyrill Gorcunov | faf60af | 2012-08-23 14:43:24 +0400 | [diff] [blame] | 320 | { |
Al Viro | 9883638 | 2018-05-03 09:00:33 -0400 | [diff] [blame] | 321 | const struct fd_data *data = ptr; |
Cyrill Gorcunov | faf60af | 2012-08-23 14:43:24 +0400 | [diff] [blame] | 322 | struct proc_inode *ei; |
| 323 | struct inode *inode; |
| 324 | |
Kalesh Singh | 7bc3fa0 | 2021-06-30 18:54:44 -0700 | [diff] [blame] | 325 | inode = proc_pid_make_inode(dentry->d_sb, task, S_IFREG | S_IRUGO); |
Cyrill Gorcunov | faf60af | 2012-08-23 14:43:24 +0400 | [diff] [blame] | 326 | if (!inode) |
Al Viro | 0168b9e | 2018-05-03 09:21:05 -0400 | [diff] [blame] | 327 | return ERR_PTR(-ENOENT); |
Cyrill Gorcunov | faf60af | 2012-08-23 14:43:24 +0400 | [diff] [blame] | 328 | |
| 329 | ei = PROC_I(inode); |
Al Viro | 9883638 | 2018-05-03 09:00:33 -0400 | [diff] [blame] | 330 | ei->fd = data->fd; |
Cyrill Gorcunov | faf60af | 2012-08-23 14:43:24 +0400 | [diff] [blame] | 331 | |
Cyrill Gorcunov | faf60af | 2012-08-23 14:43:24 +0400 | [diff] [blame] | 332 | inode->i_fop = &proc_fdinfo_file_operations; |
Al Viro | 9883638 | 2018-05-03 09:00:33 -0400 | [diff] [blame] | 333 | tid_fd_update_inode(task, inode, 0); |
Cyrill Gorcunov | faf60af | 2012-08-23 14:43:24 +0400 | [diff] [blame] | 334 | |
| 335 | d_set_d_op(dentry, &tid_fd_dentry_operations); |
Al Viro | 0168b9e | 2018-05-03 09:21:05 -0400 | [diff] [blame] | 336 | return d_splice_alias(inode, dentry); |
Cyrill Gorcunov | faf60af | 2012-08-23 14:43:24 +0400 | [diff] [blame] | 337 | } |
| 338 | |
| 339 | static struct dentry * |
| 340 | proc_lookupfdinfo(struct inode *dir, struct dentry *dentry, unsigned int flags) |
| 341 | { |
| 342 | return proc_lookupfd_common(dir, dentry, proc_fdinfo_instantiate); |
| 343 | } |
| 344 | |
Al Viro | f0c3b50 | 2013-05-16 12:07:31 -0400 | [diff] [blame] | 345 | static int proc_readfdinfo(struct file *file, struct dir_context *ctx) |
Cyrill Gorcunov | faf60af | 2012-08-23 14:43:24 +0400 | [diff] [blame] | 346 | { |
Al Viro | f0c3b50 | 2013-05-16 12:07:31 -0400 | [diff] [blame] | 347 | return proc_readfd_common(file, ctx, |
Cyrill Gorcunov | faf60af | 2012-08-23 14:43:24 +0400 | [diff] [blame] | 348 | proc_fdinfo_instantiate); |
| 349 | } |
| 350 | |
| 351 | const struct inode_operations proc_fdinfo_inode_operations = { |
| 352 | .lookup = proc_lookupfdinfo, |
| 353 | .setattr = proc_setattr, |
| 354 | }; |
| 355 | |
| 356 | const struct file_operations proc_fdinfo_operations = { |
| 357 | .read = generic_read_dir, |
Al Viro | f50752e | 2016-04-20 17:13:54 -0400 | [diff] [blame] | 358 | .iterate_shared = proc_readfdinfo, |
| 359 | .llseek = generic_file_llseek, |
Cyrill Gorcunov | faf60af | 2012-08-23 14:43:24 +0400 | [diff] [blame] | 360 | }; |