Back to home page

OSCL-LXR

 
 

    


0001 // SPDX-License-Identifier: GPL-2.0
0002 #include <linux/proc_fs.h>
0003 #include <linux/nsproxy.h>
0004 #include <linux/ptrace.h>
0005 #include <linux/namei.h>
0006 #include <linux/file.h>
0007 #include <linux/utsname.h>
0008 #include <net/net_namespace.h>
0009 #include <linux/ipc_namespace.h>
0010 #include <linux/pid_namespace.h>
0011 #include <linux/user_namespace.h>
0012 #include "internal.h"
0013 
0014 
0015 static const struct proc_ns_operations *ns_entries[] = {
0016 #ifdef CONFIG_NET_NS
0017     &netns_operations,
0018 #endif
0019 #ifdef CONFIG_UTS_NS
0020     &utsns_operations,
0021 #endif
0022 #ifdef CONFIG_IPC_NS
0023     &ipcns_operations,
0024 #endif
0025 #ifdef CONFIG_PID_NS
0026     &pidns_operations,
0027     &pidns_for_children_operations,
0028 #endif
0029 #ifdef CONFIG_USER_NS
0030     &userns_operations,
0031 #endif
0032     &mntns_operations,
0033 #ifdef CONFIG_CGROUPS
0034     &cgroupns_operations,
0035 #endif
0036 #ifdef CONFIG_TIME_NS
0037     &timens_operations,
0038     &timens_for_children_operations,
0039 #endif
0040 };
0041 
0042 static const char *proc_ns_get_link(struct dentry *dentry,
0043                     struct inode *inode,
0044                     struct delayed_call *done)
0045 {
0046     const struct proc_ns_operations *ns_ops = PROC_I(inode)->ns_ops;
0047     struct task_struct *task;
0048     struct path ns_path;
0049     int error = -EACCES;
0050 
0051     if (!dentry)
0052         return ERR_PTR(-ECHILD);
0053 
0054     task = get_proc_task(inode);
0055     if (!task)
0056         return ERR_PTR(-EACCES);
0057 
0058     if (!ptrace_may_access(task, PTRACE_MODE_READ_FSCREDS))
0059         goto out;
0060 
0061     error = ns_get_path(&ns_path, task, ns_ops);
0062     if (error)
0063         goto out;
0064 
0065     error = nd_jump_link(&ns_path);
0066 out:
0067     put_task_struct(task);
0068     return ERR_PTR(error);
0069 }
0070 
0071 static int proc_ns_readlink(struct dentry *dentry, char __user *buffer, int buflen)
0072 {
0073     struct inode *inode = d_inode(dentry);
0074     const struct proc_ns_operations *ns_ops = PROC_I(inode)->ns_ops;
0075     struct task_struct *task;
0076     char name[50];
0077     int res = -EACCES;
0078 
0079     task = get_proc_task(inode);
0080     if (!task)
0081         return res;
0082 
0083     if (ptrace_may_access(task, PTRACE_MODE_READ_FSCREDS)) {
0084         res = ns_get_name(name, sizeof(name), task, ns_ops);
0085         if (res >= 0)
0086             res = readlink_copy(buffer, buflen, name);
0087     }
0088     put_task_struct(task);
0089     return res;
0090 }
0091 
0092 static const struct inode_operations proc_ns_link_inode_operations = {
0093     .readlink   = proc_ns_readlink,
0094     .get_link   = proc_ns_get_link,
0095     .setattr    = proc_setattr,
0096 };
0097 
0098 static struct dentry *proc_ns_instantiate(struct dentry *dentry,
0099     struct task_struct *task, const void *ptr)
0100 {
0101     const struct proc_ns_operations *ns_ops = ptr;
0102     struct inode *inode;
0103     struct proc_inode *ei;
0104 
0105     inode = proc_pid_make_inode(dentry->d_sb, task, S_IFLNK | S_IRWXUGO);
0106     if (!inode)
0107         return ERR_PTR(-ENOENT);
0108 
0109     ei = PROC_I(inode);
0110     inode->i_op = &proc_ns_link_inode_operations;
0111     ei->ns_ops = ns_ops;
0112     pid_update_inode(task, inode);
0113 
0114     d_set_d_op(dentry, &pid_dentry_operations);
0115     return d_splice_alias(inode, dentry);
0116 }
0117 
0118 static int proc_ns_dir_readdir(struct file *file, struct dir_context *ctx)
0119 {
0120     struct task_struct *task = get_proc_task(file_inode(file));
0121     const struct proc_ns_operations **entry, **last;
0122 
0123     if (!task)
0124         return -ENOENT;
0125 
0126     if (!dir_emit_dots(file, ctx))
0127         goto out;
0128     if (ctx->pos >= 2 + ARRAY_SIZE(ns_entries))
0129         goto out;
0130     entry = ns_entries + (ctx->pos - 2);
0131     last = &ns_entries[ARRAY_SIZE(ns_entries) - 1];
0132     while (entry <= last) {
0133         const struct proc_ns_operations *ops = *entry;
0134         if (!proc_fill_cache(file, ctx, ops->name, strlen(ops->name),
0135                      proc_ns_instantiate, task, ops))
0136             break;
0137         ctx->pos++;
0138         entry++;
0139     }
0140 out:
0141     put_task_struct(task);
0142     return 0;
0143 }
0144 
0145 const struct file_operations proc_ns_dir_operations = {
0146     .read       = generic_read_dir,
0147     .iterate_shared = proc_ns_dir_readdir,
0148     .llseek     = generic_file_llseek,
0149 };
0150 
0151 static struct dentry *proc_ns_dir_lookup(struct inode *dir,
0152                 struct dentry *dentry, unsigned int flags)
0153 {
0154     struct task_struct *task = get_proc_task(dir);
0155     const struct proc_ns_operations **entry, **last;
0156     unsigned int len = dentry->d_name.len;
0157     struct dentry *res = ERR_PTR(-ENOENT);
0158 
0159     if (!task)
0160         goto out_no_task;
0161 
0162     last = &ns_entries[ARRAY_SIZE(ns_entries)];
0163     for (entry = ns_entries; entry < last; entry++) {
0164         if (strlen((*entry)->name) != len)
0165             continue;
0166         if (!memcmp(dentry->d_name.name, (*entry)->name, len))
0167             break;
0168     }
0169     if (entry == last)
0170         goto out;
0171 
0172     res = proc_ns_instantiate(dentry, task, *entry);
0173 out:
0174     put_task_struct(task);
0175 out_no_task:
0176     return res;
0177 }
0178 
0179 const struct inode_operations proc_ns_dir_inode_operations = {
0180     .lookup     = proc_ns_dir_lookup,
0181     .getattr    = pid_getattr,
0182     .setattr    = proc_setattr,
0183 };