b.liu | e958203 | 2025-04-17 19:18:16 +0800 | [diff] [blame^] | 1 | // SPDX-License-Identifier: GPL-2.0 |
| 2 | #include <linux/sched/signal.h> |
| 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/ptrace.h> |
| 10 | #include <linux/security.h> |
| 11 | #include <linux/file.h> |
| 12 | #include <linux/seq_file.h> |
| 13 | #include <linux/fs.h> |
| 14 | |
| 15 | #include <linux/proc_fs.h> |
| 16 | |
| 17 | #include "../mount.h" |
| 18 | #include "internal.h" |
| 19 | #include "fd.h" |
| 20 | |
| 21 | static int seq_show(struct seq_file *m, void *v) |
| 22 | { |
| 23 | struct files_struct *files = NULL; |
| 24 | int f_flags = 0, ret = -ENOENT; |
| 25 | struct file *file = NULL; |
| 26 | struct task_struct *task; |
| 27 | |
| 28 | task = get_proc_task(m->private); |
| 29 | if (!task) |
| 30 | return -ENOENT; |
| 31 | |
| 32 | files = get_files_struct(task); |
| 33 | put_task_struct(task); |
| 34 | |
| 35 | if (files) { |
| 36 | unsigned int fd = proc_fd(m->private); |
| 37 | |
| 38 | spin_lock(&files->file_lock); |
| 39 | file = fcheck_files(files, fd); |
| 40 | if (file) { |
| 41 | struct fdtable *fdt = files_fdtable(files); |
| 42 | |
| 43 | f_flags = file->f_flags; |
| 44 | if (close_on_exec(fd, fdt)) |
| 45 | f_flags |= O_CLOEXEC; |
| 46 | |
| 47 | get_file(file); |
| 48 | ret = 0; |
| 49 | } |
| 50 | spin_unlock(&files->file_lock); |
| 51 | put_files_struct(files); |
| 52 | } |
| 53 | |
| 54 | if (ret) |
| 55 | return ret; |
| 56 | |
| 57 | seq_printf(m, "pos:\t%lli\nflags:\t0%o\nmnt_id:\t%i\nino:\t%lu\n", |
| 58 | (long long)file->f_pos, f_flags, |
| 59 | real_mount(file->f_path.mnt)->mnt_id, |
| 60 | file_inode(file)->i_ino); |
| 61 | |
| 62 | show_fd_locks(m, file, files); |
| 63 | if (seq_has_overflowed(m)) |
| 64 | goto out; |
| 65 | |
| 66 | if (file->f_op->show_fdinfo) |
| 67 | file->f_op->show_fdinfo(m, file); |
| 68 | |
| 69 | out: |
| 70 | fput(file); |
| 71 | return 0; |
| 72 | } |
| 73 | |
| 74 | static int proc_fdinfo_access_allowed(struct inode *inode) |
| 75 | { |
| 76 | bool allowed = false; |
| 77 | struct task_struct *task = get_proc_task(inode); |
| 78 | |
| 79 | if (!task) |
| 80 | return -ESRCH; |
| 81 | |
| 82 | allowed = ptrace_may_access(task, PTRACE_MODE_READ_FSCREDS); |
| 83 | put_task_struct(task); |
| 84 | |
| 85 | if (!allowed) |
| 86 | return -EACCES; |
| 87 | |
| 88 | return 0; |
| 89 | } |
| 90 | |
| 91 | static int seq_fdinfo_open(struct inode *inode, struct file *file) |
| 92 | { |
| 93 | int ret = proc_fdinfo_access_allowed(inode); |
| 94 | |
| 95 | if (ret) |
| 96 | return ret; |
| 97 | |
| 98 | return single_open(file, seq_show, inode); |
| 99 | } |
| 100 | |
| 101 | static const struct file_operations proc_fdinfo_file_operations = { |
| 102 | .open = seq_fdinfo_open, |
| 103 | .read = seq_read, |
| 104 | .llseek = seq_lseek, |
| 105 | .release = single_release, |
| 106 | }; |
| 107 | |
| 108 | static bool tid_fd_mode(struct task_struct *task, unsigned fd, fmode_t *mode) |
| 109 | { |
| 110 | struct files_struct *files = get_files_struct(task); |
| 111 | struct file *file; |
| 112 | |
| 113 | if (!files) |
| 114 | return false; |
| 115 | |
| 116 | rcu_read_lock(); |
| 117 | file = fcheck_files(files, fd); |
| 118 | if (file) |
| 119 | *mode = file->f_mode; |
| 120 | rcu_read_unlock(); |
| 121 | put_files_struct(files); |
| 122 | return !!file; |
| 123 | } |
| 124 | |
| 125 | static void tid_fd_update_inode(struct task_struct *task, struct inode *inode, |
| 126 | fmode_t f_mode) |
| 127 | { |
| 128 | task_dump_owner(task, 0, &inode->i_uid, &inode->i_gid); |
| 129 | |
| 130 | if (S_ISLNK(inode->i_mode)) { |
| 131 | unsigned i_mode = S_IFLNK; |
| 132 | if (f_mode & FMODE_READ) |
| 133 | i_mode |= S_IRUSR | S_IXUSR; |
| 134 | if (f_mode & FMODE_WRITE) |
| 135 | i_mode |= S_IWUSR | S_IXUSR; |
| 136 | inode->i_mode = i_mode; |
| 137 | } |
| 138 | security_task_to_inode(task, inode); |
| 139 | } |
| 140 | |
| 141 | static int tid_fd_revalidate(struct dentry *dentry, unsigned int flags) |
| 142 | { |
| 143 | struct task_struct *task; |
| 144 | struct inode *inode; |
| 145 | unsigned int fd; |
| 146 | |
| 147 | if (flags & LOOKUP_RCU) |
| 148 | return -ECHILD; |
| 149 | |
| 150 | inode = d_inode(dentry); |
| 151 | task = get_proc_task(inode); |
| 152 | fd = proc_fd(inode); |
| 153 | |
| 154 | if (task) { |
| 155 | fmode_t f_mode; |
| 156 | if (tid_fd_mode(task, fd, &f_mode)) { |
| 157 | tid_fd_update_inode(task, inode, f_mode); |
| 158 | put_task_struct(task); |
| 159 | return 1; |
| 160 | } |
| 161 | put_task_struct(task); |
| 162 | } |
| 163 | return 0; |
| 164 | } |
| 165 | |
| 166 | static const struct dentry_operations tid_fd_dentry_operations = { |
| 167 | .d_revalidate = tid_fd_revalidate, |
| 168 | .d_delete = pid_delete_dentry, |
| 169 | }; |
| 170 | |
| 171 | static int proc_fd_link(struct dentry *dentry, struct path *path) |
| 172 | { |
| 173 | struct files_struct *files = NULL; |
| 174 | struct task_struct *task; |
| 175 | int ret = -ENOENT; |
| 176 | |
| 177 | task = get_proc_task(d_inode(dentry)); |
| 178 | if (task) { |
| 179 | files = get_files_struct(task); |
| 180 | put_task_struct(task); |
| 181 | } |
| 182 | |
| 183 | if (files) { |
| 184 | unsigned int fd = proc_fd(d_inode(dentry)); |
| 185 | struct file *fd_file; |
| 186 | |
| 187 | spin_lock(&files->file_lock); |
| 188 | fd_file = fcheck_files(files, fd); |
| 189 | if (fd_file) { |
| 190 | *path = fd_file->f_path; |
| 191 | path_get(&fd_file->f_path); |
| 192 | ret = 0; |
| 193 | } |
| 194 | spin_unlock(&files->file_lock); |
| 195 | put_files_struct(files); |
| 196 | } |
| 197 | |
| 198 | return ret; |
| 199 | } |
| 200 | |
| 201 | struct fd_data { |
| 202 | fmode_t mode; |
| 203 | unsigned fd; |
| 204 | }; |
| 205 | |
| 206 | static struct dentry *proc_fd_instantiate(struct dentry *dentry, |
| 207 | struct task_struct *task, const void *ptr) |
| 208 | { |
| 209 | const struct fd_data *data = ptr; |
| 210 | struct proc_inode *ei; |
| 211 | struct inode *inode; |
| 212 | |
| 213 | inode = proc_pid_make_inode(dentry->d_sb, task, S_IFLNK); |
| 214 | if (!inode) |
| 215 | return ERR_PTR(-ENOENT); |
| 216 | |
| 217 | ei = PROC_I(inode); |
| 218 | ei->fd = data->fd; |
| 219 | |
| 220 | inode->i_op = &proc_pid_link_inode_operations; |
| 221 | inode->i_size = 64; |
| 222 | |
| 223 | ei->op.proc_get_link = proc_fd_link; |
| 224 | tid_fd_update_inode(task, inode, data->mode); |
| 225 | |
| 226 | d_set_d_op(dentry, &tid_fd_dentry_operations); |
| 227 | return d_splice_alias(inode, dentry); |
| 228 | } |
| 229 | |
| 230 | static struct dentry *proc_lookupfd_common(struct inode *dir, |
| 231 | struct dentry *dentry, |
| 232 | instantiate_t instantiate) |
| 233 | { |
| 234 | struct task_struct *task = get_proc_task(dir); |
| 235 | struct fd_data data = {.fd = name_to_int(&dentry->d_name)}; |
| 236 | struct dentry *result = ERR_PTR(-ENOENT); |
| 237 | |
| 238 | if (!task) |
| 239 | goto out_no_task; |
| 240 | if (data.fd == ~0U) |
| 241 | goto out; |
| 242 | if (!tid_fd_mode(task, data.fd, &data.mode)) |
| 243 | goto out; |
| 244 | |
| 245 | result = instantiate(dentry, task, &data); |
| 246 | out: |
| 247 | put_task_struct(task); |
| 248 | out_no_task: |
| 249 | return result; |
| 250 | } |
| 251 | |
| 252 | static int proc_readfd_common(struct file *file, struct dir_context *ctx, |
| 253 | instantiate_t instantiate) |
| 254 | { |
| 255 | struct task_struct *p = get_proc_task(file_inode(file)); |
| 256 | struct files_struct *files; |
| 257 | unsigned int fd; |
| 258 | |
| 259 | if (!p) |
| 260 | return -ENOENT; |
| 261 | |
| 262 | if (!dir_emit_dots(file, ctx)) |
| 263 | goto out; |
| 264 | files = get_files_struct(p); |
| 265 | if (!files) |
| 266 | goto out; |
| 267 | |
| 268 | rcu_read_lock(); |
| 269 | for (fd = ctx->pos - 2; |
| 270 | fd < files_fdtable(files)->max_fds; |
| 271 | fd++, ctx->pos++) { |
| 272 | struct file *f; |
| 273 | struct fd_data data; |
| 274 | char name[10 + 1]; |
| 275 | unsigned int len; |
| 276 | |
| 277 | f = fcheck_files(files, fd); |
| 278 | if (!f) |
| 279 | continue; |
| 280 | data.mode = f->f_mode; |
| 281 | rcu_read_unlock(); |
| 282 | data.fd = fd; |
| 283 | |
| 284 | len = snprintf(name, sizeof(name), "%u", fd); |
| 285 | if (!proc_fill_cache(file, ctx, |
| 286 | name, len, instantiate, p, |
| 287 | &data)) |
| 288 | goto out_fd_loop; |
| 289 | cond_resched(); |
| 290 | rcu_read_lock(); |
| 291 | } |
| 292 | rcu_read_unlock(); |
| 293 | out_fd_loop: |
| 294 | put_files_struct(files); |
| 295 | out: |
| 296 | put_task_struct(p); |
| 297 | return 0; |
| 298 | } |
| 299 | |
| 300 | static int proc_readfd(struct file *file, struct dir_context *ctx) |
| 301 | { |
| 302 | return proc_readfd_common(file, ctx, proc_fd_instantiate); |
| 303 | } |
| 304 | |
| 305 | const struct file_operations proc_fd_operations = { |
| 306 | .read = generic_read_dir, |
| 307 | .iterate_shared = proc_readfd, |
| 308 | .llseek = generic_file_llseek, |
| 309 | }; |
| 310 | |
| 311 | static struct dentry *proc_lookupfd(struct inode *dir, struct dentry *dentry, |
| 312 | unsigned int flags) |
| 313 | { |
| 314 | return proc_lookupfd_common(dir, dentry, proc_fd_instantiate); |
| 315 | } |
| 316 | |
| 317 | /* |
| 318 | * /proc/pid/fd needs a special permission handler so that a process can still |
| 319 | * access /proc/self/fd after it has executed a setuid(). |
| 320 | */ |
| 321 | int proc_fd_permission(struct inode *inode, int mask) |
| 322 | { |
| 323 | struct task_struct *p; |
| 324 | int rv; |
| 325 | |
| 326 | rv = generic_permission(inode, mask); |
| 327 | if (rv == 0) |
| 328 | return rv; |
| 329 | |
| 330 | rcu_read_lock(); |
| 331 | p = pid_task(proc_pid(inode), PIDTYPE_PID); |
| 332 | if (p && same_thread_group(p, current)) |
| 333 | rv = 0; |
| 334 | rcu_read_unlock(); |
| 335 | |
| 336 | return rv; |
| 337 | } |
| 338 | |
| 339 | const struct inode_operations proc_fd_inode_operations = { |
| 340 | .lookup = proc_lookupfd, |
| 341 | .permission = proc_fd_permission, |
| 342 | .setattr = proc_setattr, |
| 343 | }; |
| 344 | |
| 345 | static struct dentry *proc_fdinfo_instantiate(struct dentry *dentry, |
| 346 | struct task_struct *task, const void *ptr) |
| 347 | { |
| 348 | const struct fd_data *data = ptr; |
| 349 | struct proc_inode *ei; |
| 350 | struct inode *inode; |
| 351 | |
| 352 | inode = proc_pid_make_inode(dentry->d_sb, task, S_IFREG | S_IRUGO); |
| 353 | if (!inode) |
| 354 | return ERR_PTR(-ENOENT); |
| 355 | |
| 356 | ei = PROC_I(inode); |
| 357 | ei->fd = data->fd; |
| 358 | |
| 359 | inode->i_fop = &proc_fdinfo_file_operations; |
| 360 | tid_fd_update_inode(task, inode, 0); |
| 361 | |
| 362 | d_set_d_op(dentry, &tid_fd_dentry_operations); |
| 363 | return d_splice_alias(inode, dentry); |
| 364 | } |
| 365 | |
| 366 | static struct dentry * |
| 367 | proc_lookupfdinfo(struct inode *dir, struct dentry *dentry, unsigned int flags) |
| 368 | { |
| 369 | return proc_lookupfd_common(dir, dentry, proc_fdinfo_instantiate); |
| 370 | } |
| 371 | |
| 372 | static int proc_readfdinfo(struct file *file, struct dir_context *ctx) |
| 373 | { |
| 374 | return proc_readfd_common(file, ctx, |
| 375 | proc_fdinfo_instantiate); |
| 376 | } |
| 377 | |
| 378 | static int proc_open_fdinfo(struct inode *inode, struct file *file) |
| 379 | { |
| 380 | int ret = proc_fdinfo_access_allowed(inode); |
| 381 | |
| 382 | if (ret) |
| 383 | return ret; |
| 384 | |
| 385 | return 0; |
| 386 | } |
| 387 | |
| 388 | const struct inode_operations proc_fdinfo_inode_operations = { |
| 389 | .lookup = proc_lookupfdinfo, |
| 390 | .setattr = proc_setattr, |
| 391 | }; |
| 392 | |
| 393 | const struct file_operations proc_fdinfo_operations = { |
| 394 | .open = proc_open_fdinfo, |
| 395 | .read = generic_read_dir, |
| 396 | .iterate_shared = proc_readfdinfo, |
| 397 | .llseek = generic_file_llseek, |
| 398 | }; |