1e656d8a6SEric W. Biederman #include <linux/sched.h> 2e656d8a6SEric W. Biederman #include <linux/namei.h> 30d01ff25SDavid Howells #include <linux/slab.h> 4021ada7dSAl Viro #include <linux/pid_namespace.h> 5021ada7dSAl Viro #include "internal.h" 6e656d8a6SEric W. Biederman 7e656d8a6SEric W. Biederman /* 8e656d8a6SEric W. Biederman * /proc/self: 9e656d8a6SEric W. Biederman */ 10e656d8a6SEric W. Biederman static int proc_self_readlink(struct dentry *dentry, char __user *buffer, 11e656d8a6SEric W. Biederman int buflen) 12e656d8a6SEric W. Biederman { 13e656d8a6SEric W. Biederman struct pid_namespace *ns = dentry->d_sb->s_fs_info; 14e656d8a6SEric W. Biederman pid_t tgid = task_tgid_nr_ns(current, ns); 15e656d8a6SEric W. Biederman char tmp[PROC_NUMBUF]; 16e656d8a6SEric W. Biederman if (!tgid) 17e656d8a6SEric W. Biederman return -ENOENT; 18e656d8a6SEric W. Biederman sprintf(tmp, "%d", tgid); 19e656d8a6SEric W. Biederman return vfs_readlink(dentry,buffer,buflen,tmp); 20e656d8a6SEric W. Biederman } 21e656d8a6SEric W. Biederman 22e656d8a6SEric W. Biederman static void *proc_self_follow_link(struct dentry *dentry, struct nameidata *nd) 23e656d8a6SEric W. Biederman { 24e656d8a6SEric W. Biederman struct pid_namespace *ns = dentry->d_sb->s_fs_info; 25e656d8a6SEric W. Biederman pid_t tgid = task_tgid_nr_ns(current, ns); 26e656d8a6SEric W. Biederman char *name = ERR_PTR(-ENOENT); 27e656d8a6SEric W. Biederman if (tgid) { 28e656d8a6SEric W. Biederman /* 11 for max length of signed int in decimal + NULL term */ 29e656d8a6SEric W. Biederman name = kmalloc(12, GFP_KERNEL); 30e656d8a6SEric W. Biederman if (!name) 31e656d8a6SEric W. Biederman name = ERR_PTR(-ENOMEM); 32e656d8a6SEric W. Biederman else 33e656d8a6SEric W. Biederman sprintf(name, "%d", tgid); 34e656d8a6SEric W. Biederman } 35e656d8a6SEric W. Biederman nd_set_link(nd, name); 36e656d8a6SEric W. Biederman return NULL; 37e656d8a6SEric W. Biederman } 38e656d8a6SEric W. Biederman 39e656d8a6SEric W. Biederman static void proc_self_put_link(struct dentry *dentry, struct nameidata *nd, 40e656d8a6SEric W. Biederman void *cookie) 41e656d8a6SEric W. Biederman { 42e656d8a6SEric W. Biederman char *s = nd_get_link(nd); 43e656d8a6SEric W. Biederman if (!IS_ERR(s)) 44e656d8a6SEric W. Biederman kfree(s); 45e656d8a6SEric W. Biederman } 46e656d8a6SEric W. Biederman 47e656d8a6SEric W. Biederman static const struct inode_operations proc_self_inode_operations = { 48e656d8a6SEric W. Biederman .readlink = proc_self_readlink, 49e656d8a6SEric W. Biederman .follow_link = proc_self_follow_link, 50e656d8a6SEric W. Biederman .put_link = proc_self_put_link, 51e656d8a6SEric W. Biederman }; 52e656d8a6SEric W. Biederman 53021ada7dSAl Viro static unsigned self_inum; 54021ada7dSAl Viro 55021ada7dSAl Viro int proc_setup_self(struct super_block *s) 56021ada7dSAl Viro { 57021ada7dSAl Viro struct inode *root_inode = s->s_root->d_inode; 58021ada7dSAl Viro struct pid_namespace *ns = s->s_fs_info; 59021ada7dSAl Viro struct dentry *self; 60021ada7dSAl Viro 61021ada7dSAl Viro mutex_lock(&root_inode->i_mutex); 62021ada7dSAl Viro self = d_alloc_name(s->s_root, "self"); 63021ada7dSAl Viro if (self) { 64021ada7dSAl Viro struct inode *inode = new_inode_pseudo(s); 65021ada7dSAl Viro if (inode) { 66021ada7dSAl Viro inode->i_ino = self_inum; 67021ada7dSAl Viro inode->i_mtime = inode->i_atime = inode->i_ctime = CURRENT_TIME; 68021ada7dSAl Viro inode->i_mode = S_IFLNK | S_IRWXUGO; 69021ada7dSAl Viro inode->i_uid = GLOBAL_ROOT_UID; 70021ada7dSAl Viro inode->i_gid = GLOBAL_ROOT_GID; 71021ada7dSAl Viro inode->i_op = &proc_self_inode_operations; 72021ada7dSAl Viro d_add(self, inode); 73021ada7dSAl Viro } else { 74021ada7dSAl Viro dput(self); 75021ada7dSAl Viro self = ERR_PTR(-ENOMEM); 76021ada7dSAl Viro } 77021ada7dSAl Viro } else { 78021ada7dSAl Viro self = ERR_PTR(-ENOMEM); 79021ada7dSAl Viro } 80021ada7dSAl Viro mutex_unlock(&root_inode->i_mutex); 81021ada7dSAl Viro if (IS_ERR(self)) { 82021ada7dSAl Viro pr_err("proc_fill_super: can't allocate /proc/self\n"); 83021ada7dSAl Viro return PTR_ERR(self); 84021ada7dSAl Viro } 85021ada7dSAl Viro ns->proc_self = self; 86021ada7dSAl Viro return 0; 87021ada7dSAl Viro } 88021ada7dSAl Viro 89e656d8a6SEric W. Biederman void __init proc_self_init(void) 90e656d8a6SEric W. Biederman { 91021ada7dSAl Viro proc_alloc_inum(&self_inum); 92e656d8a6SEric W. Biederman } 93