1b2441318SGreg Kroah-Hartman // SPDX-License-Identifier: GPL-2.0 23f07c014SIngo Molnar #include <linux/sched/signal.h> 3faf60af1SCyrill Gorcunov #include <linux/errno.h> 4faf60af1SCyrill Gorcunov #include <linux/dcache.h> 5faf60af1SCyrill Gorcunov #include <linux/path.h> 6faf60af1SCyrill Gorcunov #include <linux/fdtable.h> 7faf60af1SCyrill Gorcunov #include <linux/namei.h> 8faf60af1SCyrill Gorcunov #include <linux/pid.h> 9*7bc3fa01SKalesh Singh #include <linux/ptrace.h> 10faf60af1SCyrill Gorcunov #include <linux/security.h> 11ddd3e077SCyrill Gorcunov #include <linux/file.h> 12ddd3e077SCyrill Gorcunov #include <linux/seq_file.h> 136c8c9031SAndrey Vagin #include <linux/fs.h> 14faf60af1SCyrill Gorcunov 15faf60af1SCyrill Gorcunov #include <linux/proc_fs.h> 16faf60af1SCyrill Gorcunov 1749d063cbSAndrey Vagin #include "../mount.h" 18faf60af1SCyrill Gorcunov #include "internal.h" 19faf60af1SCyrill Gorcunov #include "fd.h" 20faf60af1SCyrill Gorcunov 21ddd3e077SCyrill Gorcunov static int seq_show(struct seq_file *m, void *v) 22faf60af1SCyrill Gorcunov { 23faf60af1SCyrill Gorcunov struct files_struct *files = NULL; 24ddd3e077SCyrill Gorcunov int f_flags = 0, ret = -ENOENT; 25ddd3e077SCyrill Gorcunov struct file *file = NULL; 26ddd3e077SCyrill Gorcunov struct task_struct *task; 27faf60af1SCyrill Gorcunov 28ddd3e077SCyrill Gorcunov task = get_proc_task(m->private); 29ddd3e077SCyrill Gorcunov if (!task) 30ddd3e077SCyrill Gorcunov return -ENOENT; 31ddd3e077SCyrill Gorcunov 32775e0656SEric W. Biederman task_lock(task); 33775e0656SEric W. Biederman files = task->files; 34faf60af1SCyrill Gorcunov if (files) { 35771187d6SAlexey Dobriyan unsigned int fd = proc_fd(m->private); 36ddd3e077SCyrill Gorcunov 37faf60af1SCyrill Gorcunov spin_lock(&files->file_lock); 38120ce2b0SEric W. Biederman file = files_lookup_fd_locked(files, fd); 39faf60af1SCyrill Gorcunov if (file) { 40ddd3e077SCyrill Gorcunov struct fdtable *fdt = files_fdtable(files); 41faf60af1SCyrill Gorcunov 42c6f3d811SAl Viro f_flags = file->f_flags; 43faf60af1SCyrill Gorcunov if (close_on_exec(fd, fdt)) 44faf60af1SCyrill Gorcunov f_flags |= O_CLOEXEC; 45faf60af1SCyrill Gorcunov 46ddd3e077SCyrill Gorcunov get_file(file); 47ddd3e077SCyrill Gorcunov ret = 0; 48faf60af1SCyrill Gorcunov } 49faf60af1SCyrill Gorcunov spin_unlock(&files->file_lock); 50faf60af1SCyrill Gorcunov } 51775e0656SEric W. Biederman task_unlock(task); 52775e0656SEric W. Biederman put_task_struct(task); 53ddd3e077SCyrill Gorcunov 546c8c9031SAndrey Vagin if (ret) 556c8c9031SAndrey Vagin return ret; 566c8c9031SAndrey Vagin 5749d063cbSAndrey Vagin seq_printf(m, "pos:\t%lli\nflags:\t0%o\nmnt_id:\t%i\n", 5849d063cbSAndrey Vagin (long long)file->f_pos, f_flags, 5949d063cbSAndrey Vagin real_mount(file->f_path.mnt)->mnt_id); 606c8c9031SAndrey Vagin 61775e0656SEric W. Biederman /* show_fd_locks() never deferences files so a stale value is safe */ 626c8c9031SAndrey Vagin show_fd_locks(m, file, files); 636c8c9031SAndrey Vagin if (seq_has_overflowed(m)) 646c8c9031SAndrey Vagin goto out; 656c8c9031SAndrey Vagin 6655985dd7SCyrill Gorcunov if (file->f_op->show_fdinfo) 67a3816ab0SJoe Perches file->f_op->show_fdinfo(m, file); 68faf60af1SCyrill Gorcunov 696c8c9031SAndrey Vagin out: 706c8c9031SAndrey Vagin fput(file); 716c8c9031SAndrey Vagin return 0; 72ddd3e077SCyrill Gorcunov } 73ddd3e077SCyrill Gorcunov 74ddd3e077SCyrill Gorcunov static int seq_fdinfo_open(struct inode *inode, struct file *file) 75ddd3e077SCyrill Gorcunov { 76*7bc3fa01SKalesh Singh bool allowed = false; 77*7bc3fa01SKalesh Singh struct task_struct *task = get_proc_task(inode); 78*7bc3fa01SKalesh Singh 79*7bc3fa01SKalesh Singh if (!task) 80*7bc3fa01SKalesh Singh return -ESRCH; 81*7bc3fa01SKalesh Singh 82*7bc3fa01SKalesh Singh allowed = ptrace_may_access(task, PTRACE_MODE_READ_FSCREDS); 83*7bc3fa01SKalesh Singh put_task_struct(task); 84*7bc3fa01SKalesh Singh 85*7bc3fa01SKalesh Singh if (!allowed) 86*7bc3fa01SKalesh Singh return -EACCES; 87*7bc3fa01SKalesh Singh 88ddd3e077SCyrill Gorcunov return single_open(file, seq_show, inode); 89ddd3e077SCyrill Gorcunov } 90ddd3e077SCyrill Gorcunov 91ddd3e077SCyrill Gorcunov static const struct file_operations proc_fdinfo_file_operations = { 92ddd3e077SCyrill Gorcunov .open = seq_fdinfo_open, 93ddd3e077SCyrill Gorcunov .read = seq_read, 94ddd3e077SCyrill Gorcunov .llseek = seq_lseek, 95ddd3e077SCyrill Gorcunov .release = single_release, 96ddd3e077SCyrill Gorcunov }; 97ddd3e077SCyrill Gorcunov 981ae9bd8bSAl Viro static bool tid_fd_mode(struct task_struct *task, unsigned fd, fmode_t *mode) 991ae9bd8bSAl Viro { 1001ae9bd8bSAl Viro struct file *file; 1011ae9bd8bSAl Viro 1021ae9bd8bSAl Viro rcu_read_lock(); 10364eb661fSEric W. Biederman file = task_lookup_fd_rcu(task, fd); 1041ae9bd8bSAl Viro if (file) 1051ae9bd8bSAl Viro *mode = file->f_mode; 1061ae9bd8bSAl Viro rcu_read_unlock(); 1071ae9bd8bSAl Viro return !!file; 1081ae9bd8bSAl Viro } 1091ae9bd8bSAl Viro 11098836386SAl Viro static void tid_fd_update_inode(struct task_struct *task, struct inode *inode, 11198836386SAl Viro fmode_t f_mode) 112faf60af1SCyrill Gorcunov { 11368eb94f1SEric W. Biederman task_dump_owner(task, 0, &inode->i_uid, &inode->i_gid); 114faf60af1SCyrill Gorcunov 115faf60af1SCyrill Gorcunov if (S_ISLNK(inode->i_mode)) { 116faf60af1SCyrill Gorcunov unsigned i_mode = S_IFLNK; 117faf60af1SCyrill Gorcunov if (f_mode & FMODE_READ) 118faf60af1SCyrill Gorcunov i_mode |= S_IRUSR | S_IXUSR; 119faf60af1SCyrill Gorcunov if (f_mode & FMODE_WRITE) 120faf60af1SCyrill Gorcunov i_mode |= S_IWUSR | S_IXUSR; 121faf60af1SCyrill Gorcunov inode->i_mode = i_mode; 122faf60af1SCyrill Gorcunov } 123faf60af1SCyrill Gorcunov security_task_to_inode(task, inode); 12498836386SAl Viro } 12598836386SAl Viro 12698836386SAl Viro static int tid_fd_revalidate(struct dentry *dentry, unsigned int flags) 12798836386SAl Viro { 12898836386SAl Viro struct task_struct *task; 12998836386SAl Viro struct inode *inode; 13098836386SAl Viro unsigned int fd; 13198836386SAl Viro 13298836386SAl Viro if (flags & LOOKUP_RCU) 13398836386SAl Viro return -ECHILD; 13498836386SAl Viro 13598836386SAl Viro inode = d_inode(dentry); 13698836386SAl Viro task = get_proc_task(inode); 13798836386SAl Viro fd = proc_fd(inode); 13898836386SAl Viro 13998836386SAl Viro if (task) { 14098836386SAl Viro fmode_t f_mode; 14198836386SAl Viro if (tid_fd_mode(task, fd, &f_mode)) { 14298836386SAl Viro tid_fd_update_inode(task, inode, f_mode); 143faf60af1SCyrill Gorcunov put_task_struct(task); 144faf60af1SCyrill Gorcunov return 1; 145faf60af1SCyrill Gorcunov } 146faf60af1SCyrill Gorcunov put_task_struct(task); 147faf60af1SCyrill Gorcunov } 148faf60af1SCyrill Gorcunov return 0; 149faf60af1SCyrill Gorcunov } 150faf60af1SCyrill Gorcunov 151faf60af1SCyrill Gorcunov static const struct dentry_operations tid_fd_dentry_operations = { 152faf60af1SCyrill Gorcunov .d_revalidate = tid_fd_revalidate, 153faf60af1SCyrill Gorcunov .d_delete = pid_delete_dentry, 154faf60af1SCyrill Gorcunov }; 155faf60af1SCyrill Gorcunov 156faf60af1SCyrill Gorcunov static int proc_fd_link(struct dentry *dentry, struct path *path) 157faf60af1SCyrill Gorcunov { 158ddd3e077SCyrill Gorcunov struct task_struct *task; 159ddd3e077SCyrill Gorcunov int ret = -ENOENT; 160ddd3e077SCyrill Gorcunov 1612b0143b5SDavid Howells task = get_proc_task(d_inode(dentry)); 162ddd3e077SCyrill Gorcunov if (task) { 163771187d6SAlexey Dobriyan unsigned int fd = proc_fd(d_inode(dentry)); 164ddd3e077SCyrill Gorcunov struct file *fd_file; 165ddd3e077SCyrill Gorcunov 166439be326SEric W. Biederman fd_file = fget_task(task, fd); 167ddd3e077SCyrill Gorcunov if (fd_file) { 168ddd3e077SCyrill Gorcunov *path = fd_file->f_path; 169ddd3e077SCyrill Gorcunov path_get(&fd_file->f_path); 170ddd3e077SCyrill Gorcunov ret = 0; 171439be326SEric W. Biederman fput(fd_file); 172ddd3e077SCyrill Gorcunov } 173439be326SEric W. Biederman put_task_struct(task); 174ddd3e077SCyrill Gorcunov } 175ddd3e077SCyrill Gorcunov 176ddd3e077SCyrill Gorcunov return ret; 177faf60af1SCyrill Gorcunov } 178faf60af1SCyrill Gorcunov 17998836386SAl Viro struct fd_data { 18098836386SAl Viro fmode_t mode; 18198836386SAl Viro unsigned fd; 18298836386SAl Viro }; 18398836386SAl Viro 1840168b9e3SAl Viro static struct dentry *proc_fd_instantiate(struct dentry *dentry, 185faf60af1SCyrill Gorcunov struct task_struct *task, const void *ptr) 186faf60af1SCyrill Gorcunov { 18798836386SAl Viro const struct fd_data *data = ptr; 188faf60af1SCyrill Gorcunov struct proc_inode *ei; 189faf60af1SCyrill Gorcunov struct inode *inode; 190faf60af1SCyrill Gorcunov 1910168b9e3SAl Viro inode = proc_pid_make_inode(dentry->d_sb, task, S_IFLNK); 192faf60af1SCyrill Gorcunov if (!inode) 1930168b9e3SAl Viro return ERR_PTR(-ENOENT); 194faf60af1SCyrill Gorcunov 195faf60af1SCyrill Gorcunov ei = PROC_I(inode); 19698836386SAl Viro ei->fd = data->fd; 197faf60af1SCyrill Gorcunov 198faf60af1SCyrill Gorcunov inode->i_op = &proc_pid_link_inode_operations; 199faf60af1SCyrill Gorcunov inode->i_size = 64; 200faf60af1SCyrill Gorcunov 201faf60af1SCyrill Gorcunov ei->op.proc_get_link = proc_fd_link; 20298836386SAl Viro tid_fd_update_inode(task, inode, data->mode); 203faf60af1SCyrill Gorcunov 204faf60af1SCyrill Gorcunov d_set_d_op(dentry, &tid_fd_dentry_operations); 2050168b9e3SAl Viro return d_splice_alias(inode, dentry); 206faf60af1SCyrill Gorcunov } 207faf60af1SCyrill Gorcunov 208faf60af1SCyrill Gorcunov static struct dentry *proc_lookupfd_common(struct inode *dir, 209faf60af1SCyrill Gorcunov struct dentry *dentry, 210faf60af1SCyrill Gorcunov instantiate_t instantiate) 211faf60af1SCyrill Gorcunov { 212faf60af1SCyrill Gorcunov struct task_struct *task = get_proc_task(dir); 21398836386SAl Viro struct fd_data data = {.fd = name_to_int(&dentry->d_name)}; 2140168b9e3SAl Viro struct dentry *result = ERR_PTR(-ENOENT); 215faf60af1SCyrill Gorcunov 216faf60af1SCyrill Gorcunov if (!task) 217faf60af1SCyrill Gorcunov goto out_no_task; 21898836386SAl Viro if (data.fd == ~0U) 219faf60af1SCyrill Gorcunov goto out; 22098836386SAl Viro if (!tid_fd_mode(task, data.fd, &data.mode)) 2211ae9bd8bSAl Viro goto out; 222faf60af1SCyrill Gorcunov 2230168b9e3SAl Viro result = instantiate(dentry, task, &data); 224faf60af1SCyrill Gorcunov out: 225faf60af1SCyrill Gorcunov put_task_struct(task); 226faf60af1SCyrill Gorcunov out_no_task: 2270168b9e3SAl Viro return result; 228faf60af1SCyrill Gorcunov } 229faf60af1SCyrill Gorcunov 230f0c3b509SAl Viro static int proc_readfd_common(struct file *file, struct dir_context *ctx, 231f0c3b509SAl Viro instantiate_t instantiate) 232faf60af1SCyrill Gorcunov { 233f0c3b509SAl Viro struct task_struct *p = get_proc_task(file_inode(file)); 234f0c3b509SAl Viro unsigned int fd; 235faf60af1SCyrill Gorcunov 236faf60af1SCyrill Gorcunov if (!p) 237f0c3b509SAl Viro return -ENOENT; 238faf60af1SCyrill Gorcunov 239f0c3b509SAl Viro if (!dir_emit_dots(file, ctx)) 240faf60af1SCyrill Gorcunov goto out; 241f0c3b509SAl Viro 242faf60af1SCyrill Gorcunov rcu_read_lock(); 2435b17b618SEric W. Biederman for (fd = ctx->pos - 2;; fd++) { 24498836386SAl Viro struct file *f; 24598836386SAl Viro struct fd_data data; 246e3912ac3SAlexey Dobriyan char name[10 + 1]; 247a4ef3895SAlexey Dobriyan unsigned int len; 248faf60af1SCyrill Gorcunov 2495b17b618SEric W. Biederman f = task_lookup_next_fd_rcu(p, &fd); 2505b17b618SEric W. Biederman ctx->pos = fd + 2LL; 25198836386SAl Viro if (!f) 2525b17b618SEric W. Biederman break; 25398836386SAl Viro data.mode = f->f_mode; 254faf60af1SCyrill Gorcunov rcu_read_unlock(); 25598836386SAl Viro data.fd = fd; 256faf60af1SCyrill Gorcunov 257771187d6SAlexey Dobriyan len = snprintf(name, sizeof(name), "%u", fd); 258f0c3b509SAl Viro if (!proc_fill_cache(file, ctx, 259faf60af1SCyrill Gorcunov name, len, instantiate, p, 26098836386SAl Viro &data)) 2615b17b618SEric W. Biederman goto out; 2623cc4a84eSEric Dumazet cond_resched(); 263faf60af1SCyrill Gorcunov rcu_read_lock(); 264faf60af1SCyrill Gorcunov } 265faf60af1SCyrill Gorcunov rcu_read_unlock(); 266faf60af1SCyrill Gorcunov out: 267faf60af1SCyrill Gorcunov put_task_struct(p); 268f0c3b509SAl Viro return 0; 269faf60af1SCyrill Gorcunov } 270faf60af1SCyrill Gorcunov 271f0c3b509SAl Viro static int proc_readfd(struct file *file, struct dir_context *ctx) 272faf60af1SCyrill Gorcunov { 273f0c3b509SAl Viro return proc_readfd_common(file, ctx, proc_fd_instantiate); 274faf60af1SCyrill Gorcunov } 275faf60af1SCyrill Gorcunov 276faf60af1SCyrill Gorcunov const struct file_operations proc_fd_operations = { 277faf60af1SCyrill Gorcunov .read = generic_read_dir, 278f50752eaSAl Viro .iterate_shared = proc_readfd, 279f50752eaSAl Viro .llseek = generic_file_llseek, 280faf60af1SCyrill Gorcunov }; 281faf60af1SCyrill Gorcunov 282faf60af1SCyrill Gorcunov static struct dentry *proc_lookupfd(struct inode *dir, struct dentry *dentry, 283faf60af1SCyrill Gorcunov unsigned int flags) 284faf60af1SCyrill Gorcunov { 285faf60af1SCyrill Gorcunov return proc_lookupfd_common(dir, dentry, proc_fd_instantiate); 286faf60af1SCyrill Gorcunov } 287faf60af1SCyrill Gorcunov 288faf60af1SCyrill Gorcunov /* 289faf60af1SCyrill Gorcunov * /proc/pid/fd needs a special permission handler so that a process can still 290faf60af1SCyrill Gorcunov * access /proc/self/fd after it has executed a setuid(). 291faf60af1SCyrill Gorcunov */ 292549c7297SChristian Brauner int proc_fd_permission(struct user_namespace *mnt_userns, 293549c7297SChristian Brauner struct inode *inode, int mask) 294faf60af1SCyrill Gorcunov { 29554708d28SOleg Nesterov struct task_struct *p; 29654708d28SOleg Nesterov int rv; 29754708d28SOleg Nesterov 29847291baaSChristian Brauner rv = generic_permission(&init_user_ns, inode, mask); 299faf60af1SCyrill Gorcunov if (rv == 0) 30054708d28SOleg Nesterov return rv; 30154708d28SOleg Nesterov 30254708d28SOleg Nesterov rcu_read_lock(); 30354708d28SOleg Nesterov p = pid_task(proc_pid(inode), PIDTYPE_PID); 30454708d28SOleg Nesterov if (p && same_thread_group(p, current)) 305faf60af1SCyrill Gorcunov rv = 0; 30654708d28SOleg Nesterov rcu_read_unlock(); 30754708d28SOleg Nesterov 308faf60af1SCyrill Gorcunov return rv; 309faf60af1SCyrill Gorcunov } 310faf60af1SCyrill Gorcunov 311faf60af1SCyrill Gorcunov const struct inode_operations proc_fd_inode_operations = { 312faf60af1SCyrill Gorcunov .lookup = proc_lookupfd, 313faf60af1SCyrill Gorcunov .permission = proc_fd_permission, 314faf60af1SCyrill Gorcunov .setattr = proc_setattr, 315faf60af1SCyrill Gorcunov }; 316faf60af1SCyrill Gorcunov 3170168b9e3SAl Viro static struct dentry *proc_fdinfo_instantiate(struct dentry *dentry, 318faf60af1SCyrill Gorcunov struct task_struct *task, const void *ptr) 319faf60af1SCyrill Gorcunov { 32098836386SAl Viro const struct fd_data *data = ptr; 321faf60af1SCyrill Gorcunov struct proc_inode *ei; 322faf60af1SCyrill Gorcunov struct inode *inode; 323faf60af1SCyrill Gorcunov 324*7bc3fa01SKalesh Singh inode = proc_pid_make_inode(dentry->d_sb, task, S_IFREG | S_IRUGO); 325faf60af1SCyrill Gorcunov if (!inode) 3260168b9e3SAl Viro return ERR_PTR(-ENOENT); 327faf60af1SCyrill Gorcunov 328faf60af1SCyrill Gorcunov ei = PROC_I(inode); 32998836386SAl Viro ei->fd = data->fd; 330faf60af1SCyrill Gorcunov 331faf60af1SCyrill Gorcunov inode->i_fop = &proc_fdinfo_file_operations; 33298836386SAl Viro tid_fd_update_inode(task, inode, 0); 333faf60af1SCyrill Gorcunov 334faf60af1SCyrill Gorcunov d_set_d_op(dentry, &tid_fd_dentry_operations); 3350168b9e3SAl Viro return d_splice_alias(inode, dentry); 336faf60af1SCyrill Gorcunov } 337faf60af1SCyrill Gorcunov 338faf60af1SCyrill Gorcunov static struct dentry * 339faf60af1SCyrill Gorcunov proc_lookupfdinfo(struct inode *dir, struct dentry *dentry, unsigned int flags) 340faf60af1SCyrill Gorcunov { 341faf60af1SCyrill Gorcunov return proc_lookupfd_common(dir, dentry, proc_fdinfo_instantiate); 342faf60af1SCyrill Gorcunov } 343faf60af1SCyrill Gorcunov 344f0c3b509SAl Viro static int proc_readfdinfo(struct file *file, struct dir_context *ctx) 345faf60af1SCyrill Gorcunov { 346f0c3b509SAl Viro return proc_readfd_common(file, ctx, 347faf60af1SCyrill Gorcunov proc_fdinfo_instantiate); 348faf60af1SCyrill Gorcunov } 349faf60af1SCyrill Gorcunov 350faf60af1SCyrill Gorcunov const struct inode_operations proc_fdinfo_inode_operations = { 351faf60af1SCyrill Gorcunov .lookup = proc_lookupfdinfo, 352faf60af1SCyrill Gorcunov .setattr = proc_setattr, 353faf60af1SCyrill Gorcunov }; 354faf60af1SCyrill Gorcunov 355faf60af1SCyrill Gorcunov const struct file_operations proc_fdinfo_operations = { 356faf60af1SCyrill Gorcunov .read = generic_read_dir, 357f50752eaSAl Viro .iterate_shared = proc_readfdinfo, 358f50752eaSAl Viro .llseek = generic_file_llseek, 359faf60af1SCyrill Gorcunov }; 360