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 14*49d063cbSAndrey Vagin #include "../mount.h" 15faf60af1SCyrill Gorcunov #include "internal.h" 16faf60af1SCyrill Gorcunov #include "fd.h" 17faf60af1SCyrill Gorcunov 18ddd3e077SCyrill Gorcunov static int seq_show(struct seq_file *m, void *v) 19faf60af1SCyrill Gorcunov { 20faf60af1SCyrill Gorcunov struct files_struct *files = NULL; 21ddd3e077SCyrill Gorcunov int f_flags = 0, ret = -ENOENT; 22ddd3e077SCyrill Gorcunov struct file *file = NULL; 23ddd3e077SCyrill Gorcunov struct task_struct *task; 24faf60af1SCyrill Gorcunov 25ddd3e077SCyrill Gorcunov task = get_proc_task(m->private); 26ddd3e077SCyrill Gorcunov if (!task) 27ddd3e077SCyrill Gorcunov return -ENOENT; 28ddd3e077SCyrill Gorcunov 29faf60af1SCyrill Gorcunov files = get_files_struct(task); 30faf60af1SCyrill Gorcunov put_task_struct(task); 31ddd3e077SCyrill Gorcunov 32faf60af1SCyrill Gorcunov if (files) { 33ddd3e077SCyrill Gorcunov int fd = proc_fd(m->private); 34ddd3e077SCyrill Gorcunov 35faf60af1SCyrill Gorcunov spin_lock(&files->file_lock); 36faf60af1SCyrill Gorcunov file = fcheck_files(files, fd); 37faf60af1SCyrill Gorcunov if (file) { 38ddd3e077SCyrill Gorcunov struct fdtable *fdt = files_fdtable(files); 39faf60af1SCyrill Gorcunov 40c6f3d811SAl Viro f_flags = file->f_flags; 41faf60af1SCyrill Gorcunov if (close_on_exec(fd, fdt)) 42faf60af1SCyrill Gorcunov f_flags |= O_CLOEXEC; 43faf60af1SCyrill Gorcunov 44ddd3e077SCyrill Gorcunov get_file(file); 45ddd3e077SCyrill Gorcunov ret = 0; 46faf60af1SCyrill Gorcunov } 47faf60af1SCyrill Gorcunov spin_unlock(&files->file_lock); 48faf60af1SCyrill Gorcunov put_files_struct(files); 49faf60af1SCyrill Gorcunov } 50ddd3e077SCyrill Gorcunov 51ddd3e077SCyrill Gorcunov if (!ret) { 52*49d063cbSAndrey Vagin seq_printf(m, "pos:\t%lli\nflags:\t0%o\nmnt_id:\t%i\n", 53*49d063cbSAndrey Vagin (long long)file->f_pos, f_flags, 54*49d063cbSAndrey Vagin real_mount(file->f_path.mnt)->mnt_id); 5555985dd7SCyrill Gorcunov if (file->f_op->show_fdinfo) 5655985dd7SCyrill Gorcunov ret = file->f_op->show_fdinfo(m, file); 57ddd3e077SCyrill Gorcunov fput(file); 58faf60af1SCyrill Gorcunov } 59faf60af1SCyrill Gorcunov 60ddd3e077SCyrill Gorcunov return ret; 61ddd3e077SCyrill Gorcunov } 62ddd3e077SCyrill Gorcunov 63ddd3e077SCyrill Gorcunov static int seq_fdinfo_open(struct inode *inode, struct file *file) 64ddd3e077SCyrill Gorcunov { 65ddd3e077SCyrill Gorcunov return single_open(file, seq_show, inode); 66ddd3e077SCyrill Gorcunov } 67ddd3e077SCyrill Gorcunov 68ddd3e077SCyrill Gorcunov static const struct file_operations proc_fdinfo_file_operations = { 69ddd3e077SCyrill Gorcunov .open = seq_fdinfo_open, 70ddd3e077SCyrill Gorcunov .read = seq_read, 71ddd3e077SCyrill Gorcunov .llseek = seq_lseek, 72ddd3e077SCyrill Gorcunov .release = single_release, 73ddd3e077SCyrill Gorcunov }; 74ddd3e077SCyrill Gorcunov 75faf60af1SCyrill Gorcunov static int tid_fd_revalidate(struct dentry *dentry, unsigned int flags) 76faf60af1SCyrill Gorcunov { 77faf60af1SCyrill Gorcunov struct files_struct *files; 78faf60af1SCyrill Gorcunov struct task_struct *task; 79faf60af1SCyrill Gorcunov const struct cred *cred; 80faf60af1SCyrill Gorcunov struct inode *inode; 81faf60af1SCyrill Gorcunov int fd; 82faf60af1SCyrill Gorcunov 83faf60af1SCyrill Gorcunov if (flags & LOOKUP_RCU) 84faf60af1SCyrill Gorcunov return -ECHILD; 85faf60af1SCyrill Gorcunov 86faf60af1SCyrill Gorcunov inode = dentry->d_inode; 87faf60af1SCyrill Gorcunov task = get_proc_task(inode); 88faf60af1SCyrill Gorcunov fd = proc_fd(inode); 89faf60af1SCyrill Gorcunov 90faf60af1SCyrill Gorcunov if (task) { 91faf60af1SCyrill Gorcunov files = get_files_struct(task); 92faf60af1SCyrill Gorcunov if (files) { 93faf60af1SCyrill Gorcunov struct file *file; 94faf60af1SCyrill Gorcunov 95faf60af1SCyrill Gorcunov rcu_read_lock(); 96faf60af1SCyrill Gorcunov file = fcheck_files(files, fd); 97faf60af1SCyrill Gorcunov if (file) { 98faf60af1SCyrill Gorcunov unsigned f_mode = file->f_mode; 99faf60af1SCyrill Gorcunov 100faf60af1SCyrill Gorcunov rcu_read_unlock(); 101faf60af1SCyrill Gorcunov put_files_struct(files); 102faf60af1SCyrill Gorcunov 103faf60af1SCyrill Gorcunov if (task_dumpable(task)) { 104faf60af1SCyrill Gorcunov rcu_read_lock(); 105faf60af1SCyrill Gorcunov cred = __task_cred(task); 106faf60af1SCyrill Gorcunov inode->i_uid = cred->euid; 107faf60af1SCyrill Gorcunov inode->i_gid = cred->egid; 108faf60af1SCyrill Gorcunov rcu_read_unlock(); 109faf60af1SCyrill Gorcunov } else { 110faf60af1SCyrill Gorcunov inode->i_uid = GLOBAL_ROOT_UID; 111faf60af1SCyrill Gorcunov inode->i_gid = GLOBAL_ROOT_GID; 112faf60af1SCyrill Gorcunov } 113faf60af1SCyrill Gorcunov 114faf60af1SCyrill Gorcunov if (S_ISLNK(inode->i_mode)) { 115faf60af1SCyrill Gorcunov unsigned i_mode = S_IFLNK; 116faf60af1SCyrill Gorcunov if (f_mode & FMODE_READ) 117faf60af1SCyrill Gorcunov i_mode |= S_IRUSR | S_IXUSR; 118faf60af1SCyrill Gorcunov if (f_mode & FMODE_WRITE) 119faf60af1SCyrill Gorcunov i_mode |= S_IWUSR | S_IXUSR; 120faf60af1SCyrill Gorcunov inode->i_mode = i_mode; 121faf60af1SCyrill Gorcunov } 122faf60af1SCyrill Gorcunov 123faf60af1SCyrill Gorcunov security_task_to_inode(task, inode); 124faf60af1SCyrill Gorcunov put_task_struct(task); 125faf60af1SCyrill Gorcunov return 1; 126faf60af1SCyrill Gorcunov } 127faf60af1SCyrill Gorcunov rcu_read_unlock(); 128faf60af1SCyrill Gorcunov put_files_struct(files); 129faf60af1SCyrill Gorcunov } 130faf60af1SCyrill Gorcunov put_task_struct(task); 131faf60af1SCyrill Gorcunov } 132faf60af1SCyrill Gorcunov 133faf60af1SCyrill Gorcunov d_drop(dentry); 134faf60af1SCyrill Gorcunov return 0; 135faf60af1SCyrill Gorcunov } 136faf60af1SCyrill Gorcunov 137faf60af1SCyrill Gorcunov static const struct dentry_operations tid_fd_dentry_operations = { 138faf60af1SCyrill Gorcunov .d_revalidate = tid_fd_revalidate, 139faf60af1SCyrill Gorcunov .d_delete = pid_delete_dentry, 140faf60af1SCyrill Gorcunov }; 141faf60af1SCyrill Gorcunov 142faf60af1SCyrill Gorcunov static int proc_fd_link(struct dentry *dentry, struct path *path) 143faf60af1SCyrill Gorcunov { 144ddd3e077SCyrill Gorcunov struct files_struct *files = NULL; 145ddd3e077SCyrill Gorcunov struct task_struct *task; 146ddd3e077SCyrill Gorcunov int ret = -ENOENT; 147ddd3e077SCyrill Gorcunov 148ddd3e077SCyrill Gorcunov task = get_proc_task(dentry->d_inode); 149ddd3e077SCyrill Gorcunov if (task) { 150ddd3e077SCyrill Gorcunov files = get_files_struct(task); 151ddd3e077SCyrill Gorcunov put_task_struct(task); 152ddd3e077SCyrill Gorcunov } 153ddd3e077SCyrill Gorcunov 154ddd3e077SCyrill Gorcunov if (files) { 155ddd3e077SCyrill Gorcunov int fd = proc_fd(dentry->d_inode); 156ddd3e077SCyrill Gorcunov struct file *fd_file; 157ddd3e077SCyrill Gorcunov 158ddd3e077SCyrill Gorcunov spin_lock(&files->file_lock); 159ddd3e077SCyrill Gorcunov fd_file = fcheck_files(files, fd); 160ddd3e077SCyrill Gorcunov if (fd_file) { 161ddd3e077SCyrill Gorcunov *path = fd_file->f_path; 162ddd3e077SCyrill Gorcunov path_get(&fd_file->f_path); 163ddd3e077SCyrill Gorcunov ret = 0; 164ddd3e077SCyrill Gorcunov } 165ddd3e077SCyrill Gorcunov spin_unlock(&files->file_lock); 166ddd3e077SCyrill Gorcunov put_files_struct(files); 167ddd3e077SCyrill Gorcunov } 168ddd3e077SCyrill Gorcunov 169ddd3e077SCyrill Gorcunov return ret; 170faf60af1SCyrill Gorcunov } 171faf60af1SCyrill Gorcunov 172c52a47acSAl Viro static int 173faf60af1SCyrill Gorcunov proc_fd_instantiate(struct inode *dir, struct dentry *dentry, 174faf60af1SCyrill Gorcunov struct task_struct *task, const void *ptr) 175faf60af1SCyrill Gorcunov { 176faf60af1SCyrill Gorcunov unsigned fd = (unsigned long)ptr; 177faf60af1SCyrill Gorcunov struct proc_inode *ei; 178faf60af1SCyrill Gorcunov struct inode *inode; 179faf60af1SCyrill Gorcunov 180faf60af1SCyrill Gorcunov inode = proc_pid_make_inode(dir->i_sb, task); 181faf60af1SCyrill Gorcunov if (!inode) 182faf60af1SCyrill Gorcunov goto out; 183faf60af1SCyrill Gorcunov 184faf60af1SCyrill Gorcunov ei = PROC_I(inode); 185faf60af1SCyrill Gorcunov ei->fd = fd; 186faf60af1SCyrill Gorcunov 187faf60af1SCyrill Gorcunov inode->i_mode = S_IFLNK; 188faf60af1SCyrill Gorcunov inode->i_op = &proc_pid_link_inode_operations; 189faf60af1SCyrill Gorcunov inode->i_size = 64; 190faf60af1SCyrill Gorcunov 191faf60af1SCyrill Gorcunov ei->op.proc_get_link = proc_fd_link; 192faf60af1SCyrill Gorcunov 193faf60af1SCyrill Gorcunov d_set_d_op(dentry, &tid_fd_dentry_operations); 194faf60af1SCyrill Gorcunov d_add(dentry, inode); 195faf60af1SCyrill Gorcunov 196faf60af1SCyrill Gorcunov /* Close the race of the process dying before we return the dentry */ 197faf60af1SCyrill Gorcunov if (tid_fd_revalidate(dentry, 0)) 198c52a47acSAl Viro return 0; 199faf60af1SCyrill Gorcunov out: 200c52a47acSAl Viro return -ENOENT; 201faf60af1SCyrill Gorcunov } 202faf60af1SCyrill Gorcunov 203faf60af1SCyrill Gorcunov static struct dentry *proc_lookupfd_common(struct inode *dir, 204faf60af1SCyrill Gorcunov struct dentry *dentry, 205faf60af1SCyrill Gorcunov instantiate_t instantiate) 206faf60af1SCyrill Gorcunov { 207faf60af1SCyrill Gorcunov struct task_struct *task = get_proc_task(dir); 208c52a47acSAl Viro int result = -ENOENT; 209faf60af1SCyrill Gorcunov unsigned fd = name_to_int(dentry); 210faf60af1SCyrill Gorcunov 211faf60af1SCyrill Gorcunov if (!task) 212faf60af1SCyrill Gorcunov goto out_no_task; 213faf60af1SCyrill Gorcunov if (fd == ~0U) 214faf60af1SCyrill Gorcunov goto out; 215faf60af1SCyrill Gorcunov 216faf60af1SCyrill Gorcunov result = instantiate(dir, dentry, task, (void *)(unsigned long)fd); 217faf60af1SCyrill Gorcunov out: 218faf60af1SCyrill Gorcunov put_task_struct(task); 219faf60af1SCyrill Gorcunov out_no_task: 220c52a47acSAl Viro return ERR_PTR(result); 221faf60af1SCyrill Gorcunov } 222faf60af1SCyrill Gorcunov 223f0c3b509SAl Viro static int proc_readfd_common(struct file *file, struct dir_context *ctx, 224f0c3b509SAl Viro instantiate_t instantiate) 225faf60af1SCyrill Gorcunov { 226f0c3b509SAl Viro struct task_struct *p = get_proc_task(file_inode(file)); 227faf60af1SCyrill Gorcunov struct files_struct *files; 228f0c3b509SAl Viro unsigned int fd; 229faf60af1SCyrill Gorcunov 230faf60af1SCyrill Gorcunov if (!p) 231f0c3b509SAl Viro return -ENOENT; 232faf60af1SCyrill Gorcunov 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; 29196d0df79SOleg Nesterov if (task_tgid(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 302c52a47acSAl 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)) 325c52a47acSAl Viro return 0; 326faf60af1SCyrill Gorcunov out: 327c52a47acSAl 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