^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1) // SPDX-License-Identifier: GPL-2.0
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2) #include <linux/cache.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3) #include <linux/sched.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4) #include <linux/slab.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5) #include <linux/pid_namespace.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6) #include "internal.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 7)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 8) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 9) * /proc/self:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 10) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 11) static const char *proc_self_get_link(struct dentry *dentry,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 12) struct inode *inode,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 13) struct delayed_call *done)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 14) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 15) struct pid_namespace *ns = proc_pid_ns(inode->i_sb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 16) pid_t tgid = task_tgid_nr_ns(current, ns);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 17) char *name;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 18)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 19) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 20) * Not currently supported. Once we can inherit all of struct pid,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 21) * we can allow this.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 22) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 23) if (current->flags & PF_IO_WORKER)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 24) return ERR_PTR(-EOPNOTSUPP);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 25)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 26) if (!tgid)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 27) return ERR_PTR(-ENOENT);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 28) /* max length of unsigned int in decimal + NULL term */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 29) name = kmalloc(10 + 1, dentry ? GFP_KERNEL : GFP_ATOMIC);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 30) if (unlikely(!name))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 31) return dentry ? ERR_PTR(-ENOMEM) : ERR_PTR(-ECHILD);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 32) sprintf(name, "%u", tgid);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 33) set_delayed_call(done, kfree_link, name);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 34) return name;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 35) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 36)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 37) static const struct inode_operations proc_self_inode_operations = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 38) .get_link = proc_self_get_link,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 39) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 40)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 41) static unsigned self_inum __ro_after_init;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 42)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 43) int proc_setup_self(struct super_block *s)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 44) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 45) struct inode *root_inode = d_inode(s->s_root);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 46) struct proc_fs_info *fs_info = proc_sb_info(s);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 47) struct dentry *self;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 48) int ret = -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 49)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 50) inode_lock(root_inode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 51) self = d_alloc_name(s->s_root, "self");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 52) if (self) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 53) struct inode *inode = new_inode(s);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 54) if (inode) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 55) inode->i_ino = self_inum;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 56) inode->i_mtime = inode->i_atime = inode->i_ctime = current_time(inode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 57) inode->i_mode = S_IFLNK | S_IRWXUGO;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 58) inode->i_uid = GLOBAL_ROOT_UID;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 59) inode->i_gid = GLOBAL_ROOT_GID;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 60) inode->i_op = &proc_self_inode_operations;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 61) d_add(self, inode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 62) ret = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 63) } else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 64) dput(self);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 65) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 66) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 67) inode_unlock(root_inode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 68)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 69) if (ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 70) pr_err("proc_fill_super: can't allocate /proc/self\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 71) else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 72) fs_info->proc_self = self;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 73)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 74) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 75) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 76)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 77) void __init proc_self_init(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 78) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 79) proc_alloc_inum(&self_inum);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 80) }