1b2441318SGreg Kroah-Hartman // SPDX-License-Identifier: GPL-2.0
2efb1a57dSAlexey Dobriyan #include <linux/cache.h>
3e656d8a6SEric W. Biederman #include <linux/sched.h>
40d01ff25SDavid Howells #include <linux/slab.h>
5021ada7dSAl Viro #include <linux/pid_namespace.h>
6021ada7dSAl Viro #include "internal.h"
7e656d8a6SEric W. Biederman
8e656d8a6SEric W. Biederman /*
9e656d8a6SEric W. Biederman * /proc/self:
10e656d8a6SEric W. Biederman */
proc_self_get_link(struct dentry * dentry,struct inode * inode,struct delayed_call * done)116b255391SAl Viro static const char *proc_self_get_link(struct dentry *dentry,
12fceef393SAl Viro struct inode *inode,
13fceef393SAl Viro struct delayed_call *done)
14e656d8a6SEric W. Biederman {
159d78edeaSAlexey Gladkov struct pid_namespace *ns = proc_pid_ns(inode->i_sb);
16e656d8a6SEric W. Biederman pid_t tgid = task_tgid_nr_ns(current, ns);
17680baacbSAl Viro char *name;
18680baacbSAl Viro
19680baacbSAl Viro if (!tgid)
20680baacbSAl Viro return ERR_PTR(-ENOENT);
21e3912ac3SAlexey Dobriyan /* max length of unsigned int in decimal + NULL term */
22e3912ac3SAlexey Dobriyan name = kmalloc(10 + 1, dentry ? GFP_KERNEL : GFP_ATOMIC);
231a384eaaSAl Viro if (unlikely(!name))
241a384eaaSAl Viro return dentry ? ERR_PTR(-ENOMEM) : ERR_PTR(-ECHILD);
25e3912ac3SAlexey Dobriyan sprintf(name, "%u", tgid);
26fceef393SAl Viro set_delayed_call(done, kfree_link, name);
27fceef393SAl Viro return name;
28e656d8a6SEric W. Biederman }
29e656d8a6SEric W. Biederman
30e656d8a6SEric W. Biederman static const struct inode_operations proc_self_inode_operations = {
316b255391SAl Viro .get_link = proc_self_get_link,
32e656d8a6SEric W. Biederman };
33e656d8a6SEric W. Biederman
34efb1a57dSAlexey Dobriyan static unsigned self_inum __ro_after_init;
35021ada7dSAl Viro
proc_setup_self(struct super_block * s)36021ada7dSAl Viro int proc_setup_self(struct super_block *s)
37021ada7dSAl Viro {
382b0143b5SDavid Howells struct inode *root_inode = d_inode(s->s_root);
39fa10fed3SAlexey Gladkov struct proc_fs_info *fs_info = proc_sb_info(s);
40021ada7dSAl Viro struct dentry *self;
41756ca74cSChengguang Xu int ret = -ENOMEM;
42021ada7dSAl Viro
435955102cSAl Viro inode_lock(root_inode);
44021ada7dSAl Viro self = d_alloc_name(s->s_root, "self");
45021ada7dSAl Viro if (self) {
46ef1548adSEric W. Biederman struct inode *inode = new_inode(s);
47021ada7dSAl Viro if (inode) {
48021ada7dSAl Viro inode->i_ino = self_inum;
49*e9d7d3cbSJeff Layton inode->i_mtime = inode->i_atime = inode_set_ctime_current(inode);
50021ada7dSAl Viro inode->i_mode = S_IFLNK | S_IRWXUGO;
51021ada7dSAl Viro inode->i_uid = GLOBAL_ROOT_UID;
52021ada7dSAl Viro inode->i_gid = GLOBAL_ROOT_GID;
53021ada7dSAl Viro inode->i_op = &proc_self_inode_operations;
54021ada7dSAl Viro d_add(self, inode);
55756ca74cSChengguang Xu ret = 0;
56021ada7dSAl Viro } else {
57021ada7dSAl Viro dput(self);
58021ada7dSAl Viro }
59021ada7dSAl Viro }
605955102cSAl Viro inode_unlock(root_inode);
61756ca74cSChengguang Xu
62756ca74cSChengguang Xu if (ret)
63021ada7dSAl Viro pr_err("proc_fill_super: can't allocate /proc/self\n");
64756ca74cSChengguang Xu else
65fa10fed3SAlexey Gladkov fs_info->proc_self = self;
66756ca74cSChengguang Xu
67756ca74cSChengguang Xu return ret;
68021ada7dSAl Viro }
69021ada7dSAl Viro
proc_self_init(void)70e656d8a6SEric W. Biederman void __init proc_self_init(void)
71e656d8a6SEric W. Biederman {
72021ada7dSAl Viro proc_alloc_inum(&self_inum);
73e656d8a6SEric W. Biederman }
74