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); 195d826c84SAl Viro return readlink_copy(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 const struct inode_operations proc_self_inode_operations = { 40e656d8a6SEric W. Biederman .readlink = proc_self_readlink, 41e656d8a6SEric W. Biederman .follow_link = proc_self_follow_link, 4287dc800bSAl Viro .put_link = kfree_put_link, 43e656d8a6SEric W. Biederman }; 44e656d8a6SEric W. Biederman 45021ada7dSAl Viro static unsigned self_inum; 46021ada7dSAl Viro 47021ada7dSAl Viro int proc_setup_self(struct super_block *s) 48021ada7dSAl Viro { 492b0143b5SDavid Howells struct inode *root_inode = d_inode(s->s_root); 50021ada7dSAl Viro struct pid_namespace *ns = s->s_fs_info; 51021ada7dSAl Viro struct dentry *self; 52021ada7dSAl Viro 53021ada7dSAl Viro mutex_lock(&root_inode->i_mutex); 54021ada7dSAl Viro self = d_alloc_name(s->s_root, "self"); 55021ada7dSAl Viro if (self) { 56021ada7dSAl Viro struct inode *inode = new_inode_pseudo(s); 57021ada7dSAl Viro if (inode) { 58021ada7dSAl Viro inode->i_ino = self_inum; 59021ada7dSAl Viro inode->i_mtime = inode->i_atime = inode->i_ctime = CURRENT_TIME; 60021ada7dSAl Viro inode->i_mode = S_IFLNK | S_IRWXUGO; 61021ada7dSAl Viro inode->i_uid = GLOBAL_ROOT_UID; 62021ada7dSAl Viro inode->i_gid = GLOBAL_ROOT_GID; 63021ada7dSAl Viro inode->i_op = &proc_self_inode_operations; 64021ada7dSAl Viro d_add(self, inode); 65021ada7dSAl Viro } else { 66021ada7dSAl Viro dput(self); 67021ada7dSAl Viro self = ERR_PTR(-ENOMEM); 68021ada7dSAl Viro } 69021ada7dSAl Viro } else { 70021ada7dSAl Viro self = ERR_PTR(-ENOMEM); 71021ada7dSAl Viro } 72021ada7dSAl Viro mutex_unlock(&root_inode->i_mutex); 73021ada7dSAl Viro if (IS_ERR(self)) { 74021ada7dSAl Viro pr_err("proc_fill_super: can't allocate /proc/self\n"); 75021ada7dSAl Viro return PTR_ERR(self); 76021ada7dSAl Viro } 77021ada7dSAl Viro ns->proc_self = self; 78021ada7dSAl Viro return 0; 79021ada7dSAl Viro } 80021ada7dSAl Viro 81e656d8a6SEric W. Biederman void __init proc_self_init(void) 82e656d8a6SEric W. Biederman { 83021ada7dSAl Viro proc_alloc_inum(&self_inum); 84e656d8a6SEric W. Biederman } 85