1faf60af1SCyrill Gorcunov #include <linux/sched.h> 2faf60af1SCyrill Gorcunov #include <linux/errno.h> 3faf60af1SCyrill Gorcunov #include <linux/dcache.h> 4faf60af1SCyrill Gorcunov #include <linux/path.h> 5faf60af1SCyrill Gorcunov #include <linux/fdtable.h> 6faf60af1SCyrill Gorcunov #include <linux/namei.h> 7faf60af1SCyrill Gorcunov #include <linux/pid.h> 8faf60af1SCyrill Gorcunov #include <linux/security.h> 9ddd3e077SCyrill Gorcunov #include <linux/file.h> 10ddd3e077SCyrill Gorcunov #include <linux/seq_file.h> 11faf60af1SCyrill Gorcunov 12faf60af1SCyrill Gorcunov #include <linux/proc_fs.h> 13faf60af1SCyrill Gorcunov 14faf60af1SCyrill Gorcunov #include "internal.h" 15faf60af1SCyrill Gorcunov #include "fd.h" 16faf60af1SCyrill Gorcunov 17ddd3e077SCyrill Gorcunov static int seq_show(struct seq_file *m, void *v) 18faf60af1SCyrill Gorcunov { 19faf60af1SCyrill Gorcunov struct files_struct *files = NULL; 20ddd3e077SCyrill Gorcunov int f_flags = 0, ret = -ENOENT; 21ddd3e077SCyrill Gorcunov struct file *file = NULL; 22ddd3e077SCyrill Gorcunov struct task_struct *task; 23faf60af1SCyrill Gorcunov 24ddd3e077SCyrill Gorcunov task = get_proc_task(m->private); 25ddd3e077SCyrill Gorcunov if (!task) 26ddd3e077SCyrill Gorcunov return -ENOENT; 27ddd3e077SCyrill Gorcunov 28faf60af1SCyrill Gorcunov files = get_files_struct(task); 29faf60af1SCyrill Gorcunov put_task_struct(task); 30ddd3e077SCyrill Gorcunov 31faf60af1SCyrill Gorcunov if (files) { 32ddd3e077SCyrill Gorcunov int fd = proc_fd(m->private); 33ddd3e077SCyrill Gorcunov 34faf60af1SCyrill Gorcunov spin_lock(&files->file_lock); 35faf60af1SCyrill Gorcunov file = fcheck_files(files, fd); 36faf60af1SCyrill Gorcunov if (file) { 37ddd3e077SCyrill Gorcunov struct fdtable *fdt = files_fdtable(files); 38faf60af1SCyrill Gorcunov 39*c6f3d811SAl Viro f_flags = file->f_flags; 40faf60af1SCyrill Gorcunov if (close_on_exec(fd, fdt)) 41faf60af1SCyrill Gorcunov f_flags |= O_CLOEXEC; 42faf60af1SCyrill Gorcunov 43ddd3e077SCyrill Gorcunov get_file(file); 44ddd3e077SCyrill Gorcunov ret = 0; 45faf60af1SCyrill Gorcunov } 46faf60af1SCyrill Gorcunov spin_unlock(&files->file_lock); 47faf60af1SCyrill Gorcunov put_files_struct(files); 48faf60af1SCyrill Gorcunov } 49ddd3e077SCyrill Gorcunov 50ddd3e077SCyrill Gorcunov if (!ret) { 51ddd3e077SCyrill Gorcunov seq_printf(m, "pos:\t%lli\nflags:\t0%o\n", 52ddd3e077SCyrill Gorcunov (long long)file->f_pos, f_flags); 53ddd3e077SCyrill Gorcunov fput(file); 54faf60af1SCyrill Gorcunov } 55faf60af1SCyrill Gorcunov 56ddd3e077SCyrill Gorcunov return ret; 57ddd3e077SCyrill Gorcunov } 58ddd3e077SCyrill Gorcunov 59ddd3e077SCyrill Gorcunov static int seq_fdinfo_open(struct inode *inode, struct file *file) 60ddd3e077SCyrill Gorcunov { 61ddd3e077SCyrill Gorcunov return single_open(file, seq_show, inode); 62ddd3e077SCyrill Gorcunov } 63ddd3e077SCyrill Gorcunov 64ddd3e077SCyrill Gorcunov static const struct file_operations proc_fdinfo_file_operations = { 65ddd3e077SCyrill Gorcunov .open = seq_fdinfo_open, 66ddd3e077SCyrill Gorcunov .read = seq_read, 67ddd3e077SCyrill Gorcunov .llseek = seq_lseek, 68ddd3e077SCyrill Gorcunov .release = single_release, 69ddd3e077SCyrill Gorcunov }; 70ddd3e077SCyrill Gorcunov 71faf60af1SCyrill Gorcunov static int tid_fd_revalidate(struct dentry *dentry, unsigned int flags) 72faf60af1SCyrill Gorcunov { 73faf60af1SCyrill Gorcunov struct files_struct *files; 74faf60af1SCyrill Gorcunov struct task_struct *task; 75faf60af1SCyrill Gorcunov const struct cred *cred; 76faf60af1SCyrill Gorcunov struct inode *inode; 77faf60af1SCyrill Gorcunov int fd; 78faf60af1SCyrill Gorcunov 79faf60af1SCyrill Gorcunov if (flags & LOOKUP_RCU) 80faf60af1SCyrill Gorcunov return -ECHILD; 81faf60af1SCyrill Gorcunov 82faf60af1SCyrill Gorcunov inode = dentry->d_inode; 83faf60af1SCyrill Gorcunov task = get_proc_task(inode); 84faf60af1SCyrill Gorcunov fd = proc_fd(inode); 85faf60af1SCyrill Gorcunov 86faf60af1SCyrill Gorcunov if (task) { 87faf60af1SCyrill Gorcunov files = get_files_struct(task); 88faf60af1SCyrill Gorcunov if (files) { 89faf60af1SCyrill Gorcunov struct file *file; 90faf60af1SCyrill Gorcunov 91faf60af1SCyrill Gorcunov rcu_read_lock(); 92faf60af1SCyrill Gorcunov file = fcheck_files(files, fd); 93faf60af1SCyrill Gorcunov if (file) { 94faf60af1SCyrill Gorcunov unsigned f_mode = file->f_mode; 95faf60af1SCyrill Gorcunov 96faf60af1SCyrill Gorcunov rcu_read_unlock(); 97faf60af1SCyrill Gorcunov put_files_struct(files); 98faf60af1SCyrill Gorcunov 99faf60af1SCyrill Gorcunov if (task_dumpable(task)) { 100faf60af1SCyrill Gorcunov rcu_read_lock(); 101faf60af1SCyrill Gorcunov cred = __task_cred(task); 102faf60af1SCyrill Gorcunov inode->i_uid = cred->euid; 103faf60af1SCyrill Gorcunov inode->i_gid = cred->egid; 104faf60af1SCyrill Gorcunov rcu_read_unlock(); 105faf60af1SCyrill Gorcunov } else { 106faf60af1SCyrill Gorcunov inode->i_uid = GLOBAL_ROOT_UID; 107faf60af1SCyrill Gorcunov inode->i_gid = GLOBAL_ROOT_GID; 108faf60af1SCyrill Gorcunov } 109faf60af1SCyrill Gorcunov 110faf60af1SCyrill Gorcunov if (S_ISLNK(inode->i_mode)) { 111faf60af1SCyrill Gorcunov unsigned i_mode = S_IFLNK; 112faf60af1SCyrill Gorcunov if (f_mode & FMODE_READ) 113faf60af1SCyrill Gorcunov i_mode |= S_IRUSR | S_IXUSR; 114faf60af1SCyrill Gorcunov if (f_mode & FMODE_WRITE) 115faf60af1SCyrill Gorcunov i_mode |= S_IWUSR | S_IXUSR; 116faf60af1SCyrill Gorcunov inode->i_mode = i_mode; 117faf60af1SCyrill Gorcunov } 118faf60af1SCyrill Gorcunov 119faf60af1SCyrill Gorcunov security_task_to_inode(task, inode); 120faf60af1SCyrill Gorcunov put_task_struct(task); 121faf60af1SCyrill Gorcunov return 1; 122faf60af1SCyrill Gorcunov } 123faf60af1SCyrill Gorcunov rcu_read_unlock(); 124faf60af1SCyrill Gorcunov put_files_struct(files); 125faf60af1SCyrill Gorcunov } 126faf60af1SCyrill Gorcunov put_task_struct(task); 127faf60af1SCyrill Gorcunov } 128faf60af1SCyrill Gorcunov 129faf60af1SCyrill Gorcunov d_drop(dentry); 130faf60af1SCyrill Gorcunov return 0; 131faf60af1SCyrill Gorcunov } 132faf60af1SCyrill Gorcunov 133faf60af1SCyrill Gorcunov static const struct dentry_operations tid_fd_dentry_operations = { 134faf60af1SCyrill Gorcunov .d_revalidate = tid_fd_revalidate, 135faf60af1SCyrill Gorcunov .d_delete = pid_delete_dentry, 136faf60af1SCyrill Gorcunov }; 137faf60af1SCyrill Gorcunov 138faf60af1SCyrill Gorcunov static int proc_fd_link(struct dentry *dentry, struct path *path) 139faf60af1SCyrill Gorcunov { 140ddd3e077SCyrill Gorcunov struct files_struct *files = NULL; 141ddd3e077SCyrill Gorcunov struct task_struct *task; 142ddd3e077SCyrill Gorcunov int ret = -ENOENT; 143ddd3e077SCyrill Gorcunov 144ddd3e077SCyrill Gorcunov task = get_proc_task(dentry->d_inode); 145ddd3e077SCyrill Gorcunov if (task) { 146ddd3e077SCyrill Gorcunov files = get_files_struct(task); 147ddd3e077SCyrill Gorcunov put_task_struct(task); 148ddd3e077SCyrill Gorcunov } 149ddd3e077SCyrill Gorcunov 150ddd3e077SCyrill Gorcunov if (files) { 151ddd3e077SCyrill Gorcunov int fd = proc_fd(dentry->d_inode); 152ddd3e077SCyrill Gorcunov struct file *fd_file; 153ddd3e077SCyrill Gorcunov 154ddd3e077SCyrill Gorcunov spin_lock(&files->file_lock); 155ddd3e077SCyrill Gorcunov fd_file = fcheck_files(files, fd); 156ddd3e077SCyrill Gorcunov if (fd_file) { 157ddd3e077SCyrill Gorcunov *path = fd_file->f_path; 158ddd3e077SCyrill Gorcunov path_get(&fd_file->f_path); 159ddd3e077SCyrill Gorcunov ret = 0; 160ddd3e077SCyrill Gorcunov } 161ddd3e077SCyrill Gorcunov spin_unlock(&files->file_lock); 162ddd3e077SCyrill Gorcunov put_files_struct(files); 163ddd3e077SCyrill Gorcunov } 164ddd3e077SCyrill Gorcunov 165ddd3e077SCyrill Gorcunov return ret; 166faf60af1SCyrill Gorcunov } 167faf60af1SCyrill Gorcunov 168faf60af1SCyrill Gorcunov static struct dentry * 169faf60af1SCyrill Gorcunov proc_fd_instantiate(struct inode *dir, struct dentry *dentry, 170faf60af1SCyrill Gorcunov struct task_struct *task, const void *ptr) 171faf60af1SCyrill Gorcunov { 172faf60af1SCyrill Gorcunov struct dentry *error = ERR_PTR(-ENOENT); 173faf60af1SCyrill Gorcunov unsigned fd = (unsigned long)ptr; 174faf60af1SCyrill Gorcunov struct proc_inode *ei; 175faf60af1SCyrill Gorcunov struct inode *inode; 176faf60af1SCyrill Gorcunov 177faf60af1SCyrill Gorcunov inode = proc_pid_make_inode(dir->i_sb, task); 178faf60af1SCyrill Gorcunov if (!inode) 179faf60af1SCyrill Gorcunov goto out; 180faf60af1SCyrill Gorcunov 181faf60af1SCyrill Gorcunov ei = PROC_I(inode); 182faf60af1SCyrill Gorcunov ei->fd = fd; 183faf60af1SCyrill Gorcunov 184faf60af1SCyrill Gorcunov inode->i_mode = S_IFLNK; 185faf60af1SCyrill Gorcunov inode->i_op = &proc_pid_link_inode_operations; 186faf60af1SCyrill Gorcunov inode->i_size = 64; 187faf60af1SCyrill Gorcunov 188faf60af1SCyrill Gorcunov ei->op.proc_get_link = proc_fd_link; 189faf60af1SCyrill Gorcunov 190faf60af1SCyrill Gorcunov d_set_d_op(dentry, &tid_fd_dentry_operations); 191faf60af1SCyrill Gorcunov d_add(dentry, inode); 192faf60af1SCyrill Gorcunov 193faf60af1SCyrill Gorcunov /* Close the race of the process dying before we return the dentry */ 194faf60af1SCyrill Gorcunov if (tid_fd_revalidate(dentry, 0)) 195faf60af1SCyrill Gorcunov error = NULL; 196faf60af1SCyrill Gorcunov out: 197faf60af1SCyrill Gorcunov return error; 198faf60af1SCyrill Gorcunov } 199faf60af1SCyrill Gorcunov 200faf60af1SCyrill Gorcunov static struct dentry *proc_lookupfd_common(struct inode *dir, 201faf60af1SCyrill Gorcunov struct dentry *dentry, 202faf60af1SCyrill Gorcunov instantiate_t instantiate) 203faf60af1SCyrill Gorcunov { 204faf60af1SCyrill Gorcunov struct task_struct *task = get_proc_task(dir); 205faf60af1SCyrill Gorcunov struct dentry *result = ERR_PTR(-ENOENT); 206faf60af1SCyrill Gorcunov unsigned fd = name_to_int(dentry); 207faf60af1SCyrill Gorcunov 208faf60af1SCyrill Gorcunov if (!task) 209faf60af1SCyrill Gorcunov goto out_no_task; 210faf60af1SCyrill Gorcunov if (fd == ~0U) 211faf60af1SCyrill Gorcunov goto out; 212faf60af1SCyrill Gorcunov 213faf60af1SCyrill Gorcunov result = instantiate(dir, dentry, task, (void *)(unsigned long)fd); 214faf60af1SCyrill Gorcunov out: 215faf60af1SCyrill Gorcunov put_task_struct(task); 216faf60af1SCyrill Gorcunov out_no_task: 217faf60af1SCyrill Gorcunov return result; 218faf60af1SCyrill Gorcunov } 219faf60af1SCyrill Gorcunov 220faf60af1SCyrill Gorcunov static int proc_readfd_common(struct file * filp, void * dirent, 221faf60af1SCyrill Gorcunov filldir_t filldir, instantiate_t instantiate) 222faf60af1SCyrill Gorcunov { 223faf60af1SCyrill Gorcunov struct dentry *dentry = filp->f_path.dentry; 224faf60af1SCyrill Gorcunov struct inode *inode = dentry->d_inode; 225faf60af1SCyrill Gorcunov struct task_struct *p = get_proc_task(inode); 226faf60af1SCyrill Gorcunov struct files_struct *files; 227faf60af1SCyrill Gorcunov unsigned int fd, ino; 228faf60af1SCyrill Gorcunov int retval; 229faf60af1SCyrill Gorcunov 230faf60af1SCyrill Gorcunov retval = -ENOENT; 231faf60af1SCyrill Gorcunov if (!p) 232faf60af1SCyrill Gorcunov goto out_no_task; 233faf60af1SCyrill Gorcunov retval = 0; 234faf60af1SCyrill Gorcunov 235faf60af1SCyrill Gorcunov fd = filp->f_pos; 236faf60af1SCyrill Gorcunov switch (fd) { 237faf60af1SCyrill Gorcunov case 0: 238faf60af1SCyrill Gorcunov if (filldir(dirent, ".", 1, 0, inode->i_ino, DT_DIR) < 0) 239faf60af1SCyrill Gorcunov goto out; 240faf60af1SCyrill Gorcunov filp->f_pos++; 241faf60af1SCyrill Gorcunov case 1: 242faf60af1SCyrill Gorcunov ino = parent_ino(dentry); 243faf60af1SCyrill Gorcunov if (filldir(dirent, "..", 2, 1, ino, DT_DIR) < 0) 244faf60af1SCyrill Gorcunov goto out; 245faf60af1SCyrill Gorcunov filp->f_pos++; 246faf60af1SCyrill Gorcunov default: 247faf60af1SCyrill Gorcunov files = get_files_struct(p); 248faf60af1SCyrill Gorcunov if (!files) 249faf60af1SCyrill Gorcunov goto out; 250faf60af1SCyrill Gorcunov rcu_read_lock(); 251faf60af1SCyrill Gorcunov for (fd = filp->f_pos - 2; 252faf60af1SCyrill Gorcunov fd < files_fdtable(files)->max_fds; 253faf60af1SCyrill Gorcunov fd++, filp->f_pos++) { 254faf60af1SCyrill Gorcunov char name[PROC_NUMBUF]; 255faf60af1SCyrill Gorcunov int len; 256faf60af1SCyrill Gorcunov int rv; 257faf60af1SCyrill Gorcunov 258faf60af1SCyrill Gorcunov if (!fcheck_files(files, fd)) 259faf60af1SCyrill Gorcunov continue; 260faf60af1SCyrill Gorcunov rcu_read_unlock(); 261faf60af1SCyrill Gorcunov 262faf60af1SCyrill Gorcunov len = snprintf(name, sizeof(name), "%d", fd); 263faf60af1SCyrill Gorcunov rv = proc_fill_cache(filp, dirent, filldir, 264faf60af1SCyrill Gorcunov name, len, instantiate, p, 265faf60af1SCyrill Gorcunov (void *)(unsigned long)fd); 266faf60af1SCyrill Gorcunov if (rv < 0) 267faf60af1SCyrill Gorcunov goto out_fd_loop; 268faf60af1SCyrill Gorcunov rcu_read_lock(); 269faf60af1SCyrill Gorcunov } 270faf60af1SCyrill Gorcunov rcu_read_unlock(); 271faf60af1SCyrill Gorcunov out_fd_loop: 272faf60af1SCyrill Gorcunov put_files_struct(files); 273faf60af1SCyrill Gorcunov } 274faf60af1SCyrill Gorcunov out: 275faf60af1SCyrill Gorcunov put_task_struct(p); 276faf60af1SCyrill Gorcunov out_no_task: 277faf60af1SCyrill Gorcunov return retval; 278faf60af1SCyrill Gorcunov } 279faf60af1SCyrill Gorcunov 280faf60af1SCyrill Gorcunov static int proc_readfd(struct file *filp, void *dirent, filldir_t filldir) 281faf60af1SCyrill Gorcunov { 282faf60af1SCyrill Gorcunov return proc_readfd_common(filp, dirent, filldir, proc_fd_instantiate); 283faf60af1SCyrill Gorcunov } 284faf60af1SCyrill Gorcunov 285faf60af1SCyrill Gorcunov const struct file_operations proc_fd_operations = { 286faf60af1SCyrill Gorcunov .read = generic_read_dir, 287faf60af1SCyrill Gorcunov .readdir = proc_readfd, 288faf60af1SCyrill Gorcunov .llseek = default_llseek, 289faf60af1SCyrill Gorcunov }; 290faf60af1SCyrill Gorcunov 291faf60af1SCyrill Gorcunov static struct dentry *proc_lookupfd(struct inode *dir, struct dentry *dentry, 292faf60af1SCyrill Gorcunov unsigned int flags) 293faf60af1SCyrill Gorcunov { 294faf60af1SCyrill Gorcunov return proc_lookupfd_common(dir, dentry, proc_fd_instantiate); 295faf60af1SCyrill Gorcunov } 296faf60af1SCyrill Gorcunov 297faf60af1SCyrill Gorcunov /* 298faf60af1SCyrill Gorcunov * /proc/pid/fd needs a special permission handler so that a process can still 299faf60af1SCyrill Gorcunov * access /proc/self/fd after it has executed a setuid(). 300faf60af1SCyrill Gorcunov */ 301faf60af1SCyrill Gorcunov int proc_fd_permission(struct inode *inode, int mask) 302faf60af1SCyrill Gorcunov { 303faf60af1SCyrill Gorcunov int rv = generic_permission(inode, mask); 304faf60af1SCyrill Gorcunov if (rv == 0) 305faf60af1SCyrill Gorcunov return 0; 306faf60af1SCyrill Gorcunov if (task_pid(current) == proc_pid(inode)) 307faf60af1SCyrill Gorcunov rv = 0; 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 317faf60af1SCyrill Gorcunov static struct dentry * 318faf60af1SCyrill Gorcunov proc_fdinfo_instantiate(struct inode *dir, struct dentry *dentry, 319faf60af1SCyrill Gorcunov struct task_struct *task, const void *ptr) 320faf60af1SCyrill Gorcunov { 321faf60af1SCyrill Gorcunov struct dentry *error = ERR_PTR(-ENOENT); 322faf60af1SCyrill Gorcunov unsigned fd = (unsigned long)ptr; 323faf60af1SCyrill Gorcunov struct proc_inode *ei; 324faf60af1SCyrill Gorcunov struct inode *inode; 325faf60af1SCyrill Gorcunov 326faf60af1SCyrill Gorcunov inode = proc_pid_make_inode(dir->i_sb, task); 327faf60af1SCyrill Gorcunov if (!inode) 328faf60af1SCyrill Gorcunov goto out; 329faf60af1SCyrill Gorcunov 330faf60af1SCyrill Gorcunov ei = PROC_I(inode); 331faf60af1SCyrill Gorcunov ei->fd = fd; 332faf60af1SCyrill Gorcunov 333faf60af1SCyrill Gorcunov inode->i_mode = S_IFREG | S_IRUSR; 334faf60af1SCyrill Gorcunov inode->i_fop = &proc_fdinfo_file_operations; 335faf60af1SCyrill Gorcunov 336faf60af1SCyrill Gorcunov d_set_d_op(dentry, &tid_fd_dentry_operations); 337faf60af1SCyrill Gorcunov d_add(dentry, inode); 338faf60af1SCyrill Gorcunov 339faf60af1SCyrill Gorcunov /* Close the race of the process dying before we return the dentry */ 340faf60af1SCyrill Gorcunov if (tid_fd_revalidate(dentry, 0)) 341faf60af1SCyrill Gorcunov error = NULL; 342faf60af1SCyrill Gorcunov out: 343faf60af1SCyrill Gorcunov return error; 344faf60af1SCyrill Gorcunov } 345faf60af1SCyrill Gorcunov 346faf60af1SCyrill Gorcunov static struct dentry * 347faf60af1SCyrill Gorcunov proc_lookupfdinfo(struct inode *dir, struct dentry *dentry, unsigned int flags) 348faf60af1SCyrill Gorcunov { 349faf60af1SCyrill Gorcunov return proc_lookupfd_common(dir, dentry, proc_fdinfo_instantiate); 350faf60af1SCyrill Gorcunov } 351faf60af1SCyrill Gorcunov 352faf60af1SCyrill Gorcunov static int proc_readfdinfo(struct file *filp, void *dirent, filldir_t filldir) 353faf60af1SCyrill Gorcunov { 354faf60af1SCyrill Gorcunov return proc_readfd_common(filp, dirent, filldir, 355faf60af1SCyrill Gorcunov proc_fdinfo_instantiate); 356faf60af1SCyrill Gorcunov } 357faf60af1SCyrill Gorcunov 358faf60af1SCyrill Gorcunov const struct inode_operations proc_fdinfo_inode_operations = { 359faf60af1SCyrill Gorcunov .lookup = proc_lookupfdinfo, 360faf60af1SCyrill Gorcunov .setattr = proc_setattr, 361faf60af1SCyrill Gorcunov }; 362faf60af1SCyrill Gorcunov 363faf60af1SCyrill Gorcunov const struct file_operations proc_fdinfo_operations = { 364faf60af1SCyrill Gorcunov .read = generic_read_dir, 365faf60af1SCyrill Gorcunov .readdir = proc_readfdinfo, 366faf60af1SCyrill Gorcunov .llseek = default_llseek, 367faf60af1SCyrill Gorcunov }; 368