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