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