1b2441318SGreg Kroah-Hartman // SPDX-License-Identifier: GPL-2.0
2efb1a57dSAlexey Dobriyan #include <linux/cache.h>
30097875bSEric W. Biederman #include <linux/sched.h>
40097875bSEric W. Biederman #include <linux/slab.h>
50097875bSEric W. Biederman #include <linux/pid_namespace.h>
60097875bSEric W. Biederman #include "internal.h"
70097875bSEric W. Biederman
80097875bSEric W. Biederman /*
90097875bSEric W. Biederman * /proc/thread_self:
100097875bSEric W. Biederman */
proc_thread_self_get_link(struct dentry * dentry,struct inode * inode,struct delayed_call * done)116b255391SAl Viro static const char *proc_thread_self_get_link(struct dentry *dentry,
12fceef393SAl Viro struct inode *inode,
13fceef393SAl Viro struct delayed_call *done)
140097875bSEric W. Biederman {
159d78edeaSAlexey Gladkov struct pid_namespace *ns = proc_pid_ns(inode->i_sb);
160097875bSEric W. Biederman pid_t tgid = task_tgid_nr_ns(current, ns);
170097875bSEric W. Biederman pid_t pid = task_pid_nr_ns(current, ns);
18680baacbSAl Viro char *name;
19680baacbSAl Viro
20680baacbSAl Viro if (!pid)
21680baacbSAl Viro return ERR_PTR(-ENOENT);
22e3912ac3SAlexey Dobriyan name = kmalloc(10 + 6 + 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/task/%u", tgid, pid);
26fceef393SAl Viro set_delayed_call(done, kfree_link, name);
27fceef393SAl Viro return name;
280097875bSEric W. Biederman }
290097875bSEric W. Biederman
300097875bSEric W. Biederman static const struct inode_operations proc_thread_self_inode_operations = {
316b255391SAl Viro .get_link = proc_thread_self_get_link,
320097875bSEric W. Biederman };
330097875bSEric W. Biederman
34efb1a57dSAlexey Dobriyan static unsigned thread_self_inum __ro_after_init;
350097875bSEric W. Biederman
proc_setup_thread_self(struct super_block * s)360097875bSEric W. Biederman int proc_setup_thread_self(struct super_block *s)
370097875bSEric W. Biederman {
382b0143b5SDavid Howells struct inode *root_inode = d_inode(s->s_root);
39fa10fed3SAlexey Gladkov struct proc_fs_info *fs_info = proc_sb_info(s);
400097875bSEric W. Biederman struct dentry *thread_self;
4145f68ab5SChengguang Xu int ret = -ENOMEM;
420097875bSEric W. Biederman
435955102cSAl Viro inode_lock(root_inode);
440097875bSEric W. Biederman thread_self = d_alloc_name(s->s_root, "thread-self");
450097875bSEric W. Biederman if (thread_self) {
46ef1548adSEric W. Biederman struct inode *inode = new_inode(s);
470097875bSEric W. Biederman if (inode) {
480097875bSEric W. Biederman inode->i_ino = thread_self_inum;
49*e9d7d3cbSJeff Layton inode->i_mtime = inode->i_atime = inode_set_ctime_current(inode);
500097875bSEric W. Biederman inode->i_mode = S_IFLNK | S_IRWXUGO;
510097875bSEric W. Biederman inode->i_uid = GLOBAL_ROOT_UID;
520097875bSEric W. Biederman inode->i_gid = GLOBAL_ROOT_GID;
530097875bSEric W. Biederman inode->i_op = &proc_thread_self_inode_operations;
540097875bSEric W. Biederman d_add(thread_self, inode);
5545f68ab5SChengguang Xu ret = 0;
560097875bSEric W. Biederman } else {
570097875bSEric W. Biederman dput(thread_self);
580097875bSEric W. Biederman }
590097875bSEric W. Biederman }
605955102cSAl Viro inode_unlock(root_inode);
6145f68ab5SChengguang Xu
6245f68ab5SChengguang Xu if (ret)
63fa10fed3SAlexey Gladkov pr_err("proc_fill_super: can't allocate /proc/thread-self\n");
6445f68ab5SChengguang Xu else
65fa10fed3SAlexey Gladkov fs_info->proc_thread_self = thread_self;
6645f68ab5SChengguang Xu
6745f68ab5SChengguang Xu return ret;
680097875bSEric W. Biederman }
690097875bSEric W. Biederman
proc_thread_self_init(void)700097875bSEric W. Biederman void __init proc_thread_self_init(void)
710097875bSEric W. Biederman {
720097875bSEric W. Biederman proc_alloc_inum(&thread_self_inum);
730097875bSEric W. Biederman }
74