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 39c6f3d811SAl 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); 5355985dd7SCyrill Gorcunov if (file->f_op->show_fdinfo) 5455985dd7SCyrill Gorcunov ret = file->f_op->show_fdinfo(m, file); 55ddd3e077SCyrill Gorcunov fput(file); 56faf60af1SCyrill Gorcunov } 57faf60af1SCyrill Gorcunov 58ddd3e077SCyrill Gorcunov return ret; 59ddd3e077SCyrill Gorcunov } 60ddd3e077SCyrill Gorcunov 61ddd3e077SCyrill Gorcunov static int seq_fdinfo_open(struct inode *inode, struct file *file) 62ddd3e077SCyrill Gorcunov { 63ddd3e077SCyrill Gorcunov return single_open(file, seq_show, inode); 64ddd3e077SCyrill Gorcunov } 65ddd3e077SCyrill Gorcunov 66ddd3e077SCyrill Gorcunov static const struct file_operations proc_fdinfo_file_operations = { 67ddd3e077SCyrill Gorcunov .open = seq_fdinfo_open, 68ddd3e077SCyrill Gorcunov .read = seq_read, 69ddd3e077SCyrill Gorcunov .llseek = seq_lseek, 70ddd3e077SCyrill Gorcunov .release = single_release, 71ddd3e077SCyrill Gorcunov }; 72ddd3e077SCyrill Gorcunov 73faf60af1SCyrill Gorcunov static int tid_fd_revalidate(struct dentry *dentry, unsigned int flags) 74faf60af1SCyrill Gorcunov { 75faf60af1SCyrill Gorcunov struct files_struct *files; 76faf60af1SCyrill Gorcunov struct task_struct *task; 77faf60af1SCyrill Gorcunov const struct cred *cred; 78faf60af1SCyrill Gorcunov struct inode *inode; 79faf60af1SCyrill Gorcunov int fd; 80faf60af1SCyrill Gorcunov 81faf60af1SCyrill Gorcunov if (flags & LOOKUP_RCU) 82faf60af1SCyrill Gorcunov return -ECHILD; 83faf60af1SCyrill Gorcunov 84faf60af1SCyrill Gorcunov inode = dentry->d_inode; 85faf60af1SCyrill Gorcunov task = get_proc_task(inode); 86faf60af1SCyrill Gorcunov fd = proc_fd(inode); 87faf60af1SCyrill Gorcunov 88faf60af1SCyrill Gorcunov if (task) { 89faf60af1SCyrill Gorcunov files = get_files_struct(task); 90faf60af1SCyrill Gorcunov if (files) { 91faf60af1SCyrill Gorcunov struct file *file; 92faf60af1SCyrill Gorcunov 93faf60af1SCyrill Gorcunov rcu_read_lock(); 94faf60af1SCyrill Gorcunov file = fcheck_files(files, fd); 95faf60af1SCyrill Gorcunov if (file) { 96faf60af1SCyrill Gorcunov unsigned f_mode = file->f_mode; 97faf60af1SCyrill Gorcunov 98faf60af1SCyrill Gorcunov rcu_read_unlock(); 99faf60af1SCyrill Gorcunov put_files_struct(files); 100faf60af1SCyrill Gorcunov 101faf60af1SCyrill Gorcunov if (task_dumpable(task)) { 102faf60af1SCyrill Gorcunov rcu_read_lock(); 103faf60af1SCyrill Gorcunov cred = __task_cred(task); 104faf60af1SCyrill Gorcunov inode->i_uid = cred->euid; 105faf60af1SCyrill Gorcunov inode->i_gid = cred->egid; 106faf60af1SCyrill Gorcunov rcu_read_unlock(); 107faf60af1SCyrill Gorcunov } else { 108faf60af1SCyrill Gorcunov inode->i_uid = GLOBAL_ROOT_UID; 109faf60af1SCyrill Gorcunov inode->i_gid = GLOBAL_ROOT_GID; 110faf60af1SCyrill Gorcunov } 111faf60af1SCyrill Gorcunov 112faf60af1SCyrill Gorcunov if (S_ISLNK(inode->i_mode)) { 113faf60af1SCyrill Gorcunov unsigned i_mode = S_IFLNK; 114faf60af1SCyrill Gorcunov if (f_mode & FMODE_READ) 115faf60af1SCyrill Gorcunov i_mode |= S_IRUSR | S_IXUSR; 116faf60af1SCyrill Gorcunov if (f_mode & FMODE_WRITE) 117faf60af1SCyrill Gorcunov i_mode |= S_IWUSR | S_IXUSR; 118faf60af1SCyrill Gorcunov inode->i_mode = i_mode; 119faf60af1SCyrill Gorcunov } 120faf60af1SCyrill Gorcunov 121faf60af1SCyrill Gorcunov security_task_to_inode(task, inode); 122faf60af1SCyrill Gorcunov put_task_struct(task); 123faf60af1SCyrill Gorcunov return 1; 124faf60af1SCyrill Gorcunov } 125faf60af1SCyrill Gorcunov rcu_read_unlock(); 126faf60af1SCyrill Gorcunov put_files_struct(files); 127faf60af1SCyrill Gorcunov } 128faf60af1SCyrill Gorcunov put_task_struct(task); 129faf60af1SCyrill Gorcunov } 130faf60af1SCyrill Gorcunov 131faf60af1SCyrill Gorcunov d_drop(dentry); 132faf60af1SCyrill Gorcunov return 0; 133faf60af1SCyrill Gorcunov } 134faf60af1SCyrill Gorcunov 135faf60af1SCyrill Gorcunov static const struct dentry_operations tid_fd_dentry_operations = { 136faf60af1SCyrill Gorcunov .d_revalidate = tid_fd_revalidate, 137faf60af1SCyrill Gorcunov .d_delete = pid_delete_dentry, 138faf60af1SCyrill Gorcunov }; 139faf60af1SCyrill Gorcunov 140faf60af1SCyrill Gorcunov static int proc_fd_link(struct dentry *dentry, struct path *path) 141faf60af1SCyrill Gorcunov { 142ddd3e077SCyrill Gorcunov struct files_struct *files = NULL; 143ddd3e077SCyrill Gorcunov struct task_struct *task; 144ddd3e077SCyrill Gorcunov int ret = -ENOENT; 145ddd3e077SCyrill Gorcunov 146ddd3e077SCyrill Gorcunov task = get_proc_task(dentry->d_inode); 147ddd3e077SCyrill Gorcunov if (task) { 148ddd3e077SCyrill Gorcunov files = get_files_struct(task); 149ddd3e077SCyrill Gorcunov put_task_struct(task); 150ddd3e077SCyrill Gorcunov } 151ddd3e077SCyrill Gorcunov 152ddd3e077SCyrill Gorcunov if (files) { 153ddd3e077SCyrill Gorcunov int fd = proc_fd(dentry->d_inode); 154ddd3e077SCyrill Gorcunov struct file *fd_file; 155ddd3e077SCyrill Gorcunov 156ddd3e077SCyrill Gorcunov spin_lock(&files->file_lock); 157ddd3e077SCyrill Gorcunov fd_file = fcheck_files(files, fd); 158ddd3e077SCyrill Gorcunov if (fd_file) { 159ddd3e077SCyrill Gorcunov *path = fd_file->f_path; 160ddd3e077SCyrill Gorcunov path_get(&fd_file->f_path); 161ddd3e077SCyrill Gorcunov ret = 0; 162ddd3e077SCyrill Gorcunov } 163ddd3e077SCyrill Gorcunov spin_unlock(&files->file_lock); 164ddd3e077SCyrill Gorcunov put_files_struct(files); 165ddd3e077SCyrill Gorcunov } 166ddd3e077SCyrill Gorcunov 167ddd3e077SCyrill Gorcunov return ret; 168faf60af1SCyrill Gorcunov } 169faf60af1SCyrill Gorcunov 170*c52a47acSAl Viro static int 171faf60af1SCyrill Gorcunov proc_fd_instantiate(struct inode *dir, struct dentry *dentry, 172faf60af1SCyrill Gorcunov struct task_struct *task, const void *ptr) 173faf60af1SCyrill Gorcunov { 174faf60af1SCyrill Gorcunov unsigned fd = (unsigned long)ptr; 175faf60af1SCyrill Gorcunov struct proc_inode *ei; 176faf60af1SCyrill Gorcunov struct inode *inode; 177faf60af1SCyrill Gorcunov 178faf60af1SCyrill Gorcunov inode = proc_pid_make_inode(dir->i_sb, task); 179faf60af1SCyrill Gorcunov if (!inode) 180faf60af1SCyrill Gorcunov goto out; 181faf60af1SCyrill Gorcunov 182faf60af1SCyrill Gorcunov ei = PROC_I(inode); 183faf60af1SCyrill Gorcunov ei->fd = fd; 184faf60af1SCyrill Gorcunov 185faf60af1SCyrill Gorcunov inode->i_mode = S_IFLNK; 186faf60af1SCyrill Gorcunov inode->i_op = &proc_pid_link_inode_operations; 187faf60af1SCyrill Gorcunov inode->i_size = 64; 188faf60af1SCyrill Gorcunov 189faf60af1SCyrill Gorcunov ei->op.proc_get_link = proc_fd_link; 190faf60af1SCyrill Gorcunov 191faf60af1SCyrill Gorcunov d_set_d_op(dentry, &tid_fd_dentry_operations); 192faf60af1SCyrill Gorcunov d_add(dentry, inode); 193faf60af1SCyrill Gorcunov 194faf60af1SCyrill Gorcunov /* Close the race of the process dying before we return the dentry */ 195faf60af1SCyrill Gorcunov if (tid_fd_revalidate(dentry, 0)) 196*c52a47acSAl Viro return 0; 197faf60af1SCyrill Gorcunov out: 198*c52a47acSAl Viro return -ENOENT; 199faf60af1SCyrill Gorcunov } 200faf60af1SCyrill Gorcunov 201faf60af1SCyrill Gorcunov static struct dentry *proc_lookupfd_common(struct inode *dir, 202faf60af1SCyrill Gorcunov struct dentry *dentry, 203faf60af1SCyrill Gorcunov instantiate_t instantiate) 204faf60af1SCyrill Gorcunov { 205faf60af1SCyrill Gorcunov struct task_struct *task = get_proc_task(dir); 206*c52a47acSAl Viro int result = -ENOENT; 207faf60af1SCyrill Gorcunov unsigned fd = name_to_int(dentry); 208faf60af1SCyrill Gorcunov 209faf60af1SCyrill Gorcunov if (!task) 210faf60af1SCyrill Gorcunov goto out_no_task; 211faf60af1SCyrill Gorcunov if (fd == ~0U) 212faf60af1SCyrill Gorcunov goto out; 213faf60af1SCyrill Gorcunov 214faf60af1SCyrill Gorcunov result = instantiate(dir, dentry, task, (void *)(unsigned long)fd); 215faf60af1SCyrill Gorcunov out: 216faf60af1SCyrill Gorcunov put_task_struct(task); 217faf60af1SCyrill Gorcunov out_no_task: 218*c52a47acSAl Viro return ERR_PTR(result); 219faf60af1SCyrill Gorcunov } 220faf60af1SCyrill Gorcunov 221f0c3b509SAl Viro static int proc_readfd_common(struct file *file, struct dir_context *ctx, 222f0c3b509SAl Viro instantiate_t instantiate) 223faf60af1SCyrill Gorcunov { 224f0c3b509SAl Viro struct task_struct *p = get_proc_task(file_inode(file)); 225faf60af1SCyrill Gorcunov struct files_struct *files; 226f0c3b509SAl Viro unsigned int fd; 227faf60af1SCyrill Gorcunov 228faf60af1SCyrill Gorcunov if (!p) 229f0c3b509SAl Viro return -ENOENT; 230faf60af1SCyrill Gorcunov 231f0c3b509SAl Viro if (!dir_emit_dots(file, ctx)) 232faf60af1SCyrill Gorcunov goto out; 233f0c3b509SAl Viro if (!dir_emit_dots(file, ctx)) 234faf60af1SCyrill Gorcunov goto out; 235faf60af1SCyrill Gorcunov files = get_files_struct(p); 236faf60af1SCyrill Gorcunov if (!files) 237faf60af1SCyrill Gorcunov goto out; 238f0c3b509SAl Viro 239faf60af1SCyrill Gorcunov rcu_read_lock(); 240f0c3b509SAl Viro for (fd = ctx->pos - 2; 241faf60af1SCyrill Gorcunov fd < files_fdtable(files)->max_fds; 242f0c3b509SAl Viro fd++, ctx->pos++) { 243faf60af1SCyrill Gorcunov char name[PROC_NUMBUF]; 244faf60af1SCyrill Gorcunov int len; 245faf60af1SCyrill Gorcunov 246faf60af1SCyrill Gorcunov if (!fcheck_files(files, fd)) 247faf60af1SCyrill Gorcunov continue; 248faf60af1SCyrill Gorcunov rcu_read_unlock(); 249faf60af1SCyrill Gorcunov 250faf60af1SCyrill Gorcunov len = snprintf(name, sizeof(name), "%d", fd); 251f0c3b509SAl Viro if (!proc_fill_cache(file, ctx, 252faf60af1SCyrill Gorcunov name, len, instantiate, p, 253f0c3b509SAl Viro (void *)(unsigned long)fd)) 254faf60af1SCyrill Gorcunov goto out_fd_loop; 255faf60af1SCyrill Gorcunov rcu_read_lock(); 256faf60af1SCyrill Gorcunov } 257faf60af1SCyrill Gorcunov rcu_read_unlock(); 258faf60af1SCyrill Gorcunov out_fd_loop: 259faf60af1SCyrill Gorcunov put_files_struct(files); 260faf60af1SCyrill Gorcunov out: 261faf60af1SCyrill Gorcunov put_task_struct(p); 262f0c3b509SAl Viro return 0; 263faf60af1SCyrill Gorcunov } 264faf60af1SCyrill Gorcunov 265f0c3b509SAl Viro static int proc_readfd(struct file *file, struct dir_context *ctx) 266faf60af1SCyrill Gorcunov { 267f0c3b509SAl Viro return proc_readfd_common(file, ctx, proc_fd_instantiate); 268faf60af1SCyrill Gorcunov } 269faf60af1SCyrill Gorcunov 270faf60af1SCyrill Gorcunov const struct file_operations proc_fd_operations = { 271faf60af1SCyrill Gorcunov .read = generic_read_dir, 272f0c3b509SAl Viro .iterate = proc_readfd, 273faf60af1SCyrill Gorcunov .llseek = default_llseek, 274faf60af1SCyrill Gorcunov }; 275faf60af1SCyrill Gorcunov 276faf60af1SCyrill Gorcunov static struct dentry *proc_lookupfd(struct inode *dir, struct dentry *dentry, 277faf60af1SCyrill Gorcunov unsigned int flags) 278faf60af1SCyrill Gorcunov { 279faf60af1SCyrill Gorcunov return proc_lookupfd_common(dir, dentry, proc_fd_instantiate); 280faf60af1SCyrill Gorcunov } 281faf60af1SCyrill Gorcunov 282faf60af1SCyrill Gorcunov /* 283faf60af1SCyrill Gorcunov * /proc/pid/fd needs a special permission handler so that a process can still 284faf60af1SCyrill Gorcunov * access /proc/self/fd after it has executed a setuid(). 285faf60af1SCyrill Gorcunov */ 286faf60af1SCyrill Gorcunov int proc_fd_permission(struct inode *inode, int mask) 287faf60af1SCyrill Gorcunov { 288faf60af1SCyrill Gorcunov int rv = generic_permission(inode, mask); 289faf60af1SCyrill Gorcunov if (rv == 0) 290faf60af1SCyrill Gorcunov return 0; 291faf60af1SCyrill Gorcunov if (task_pid(current) == proc_pid(inode)) 292faf60af1SCyrill Gorcunov rv = 0; 293faf60af1SCyrill Gorcunov return rv; 294faf60af1SCyrill Gorcunov } 295faf60af1SCyrill Gorcunov 296faf60af1SCyrill Gorcunov const struct inode_operations proc_fd_inode_operations = { 297faf60af1SCyrill Gorcunov .lookup = proc_lookupfd, 298faf60af1SCyrill Gorcunov .permission = proc_fd_permission, 299faf60af1SCyrill Gorcunov .setattr = proc_setattr, 300faf60af1SCyrill Gorcunov }; 301faf60af1SCyrill Gorcunov 302*c52a47acSAl Viro static int 303faf60af1SCyrill Gorcunov proc_fdinfo_instantiate(struct inode *dir, struct dentry *dentry, 304faf60af1SCyrill Gorcunov struct task_struct *task, const void *ptr) 305faf60af1SCyrill Gorcunov { 306faf60af1SCyrill Gorcunov unsigned fd = (unsigned long)ptr; 307faf60af1SCyrill Gorcunov struct proc_inode *ei; 308faf60af1SCyrill Gorcunov struct inode *inode; 309faf60af1SCyrill Gorcunov 310faf60af1SCyrill Gorcunov inode = proc_pid_make_inode(dir->i_sb, task); 311faf60af1SCyrill Gorcunov if (!inode) 312faf60af1SCyrill Gorcunov goto out; 313faf60af1SCyrill Gorcunov 314faf60af1SCyrill Gorcunov ei = PROC_I(inode); 315faf60af1SCyrill Gorcunov ei->fd = fd; 316faf60af1SCyrill Gorcunov 317faf60af1SCyrill Gorcunov inode->i_mode = S_IFREG | S_IRUSR; 318faf60af1SCyrill Gorcunov inode->i_fop = &proc_fdinfo_file_operations; 319faf60af1SCyrill Gorcunov 320faf60af1SCyrill Gorcunov d_set_d_op(dentry, &tid_fd_dentry_operations); 321faf60af1SCyrill Gorcunov d_add(dentry, inode); 322faf60af1SCyrill Gorcunov 323faf60af1SCyrill Gorcunov /* Close the race of the process dying before we return the dentry */ 324faf60af1SCyrill Gorcunov if (tid_fd_revalidate(dentry, 0)) 325*c52a47acSAl Viro return 0; 326faf60af1SCyrill Gorcunov out: 327*c52a47acSAl Viro return -ENOENT; 328faf60af1SCyrill Gorcunov } 329faf60af1SCyrill Gorcunov 330faf60af1SCyrill Gorcunov static struct dentry * 331faf60af1SCyrill Gorcunov proc_lookupfdinfo(struct inode *dir, struct dentry *dentry, unsigned int flags) 332faf60af1SCyrill Gorcunov { 333faf60af1SCyrill Gorcunov return proc_lookupfd_common(dir, dentry, proc_fdinfo_instantiate); 334faf60af1SCyrill Gorcunov } 335faf60af1SCyrill Gorcunov 336f0c3b509SAl Viro static int proc_readfdinfo(struct file *file, struct dir_context *ctx) 337faf60af1SCyrill Gorcunov { 338f0c3b509SAl Viro return proc_readfd_common(file, ctx, 339faf60af1SCyrill Gorcunov proc_fdinfo_instantiate); 340faf60af1SCyrill Gorcunov } 341faf60af1SCyrill Gorcunov 342faf60af1SCyrill Gorcunov const struct inode_operations proc_fdinfo_inode_operations = { 343faf60af1SCyrill Gorcunov .lookup = proc_lookupfdinfo, 344faf60af1SCyrill Gorcunov .setattr = proc_setattr, 345faf60af1SCyrill Gorcunov }; 346faf60af1SCyrill Gorcunov 347faf60af1SCyrill Gorcunov const struct file_operations proc_fdinfo_operations = { 348faf60af1SCyrill Gorcunov .read = generic_read_dir, 349f0c3b509SAl Viro .iterate = proc_readfdinfo, 350faf60af1SCyrill Gorcunov .llseek = default_llseek, 351faf60af1SCyrill Gorcunov }; 352