xref: /openbmc/linux/fs/proc/namespaces.c (revision 6b2553918d8b4e6de9853fd6315bec7271a2e592)
1 #include <linux/proc_fs.h>
2 #include <linux/nsproxy.h>
3 #include <linux/ptrace.h>
4 #include <linux/namei.h>
5 #include <linux/file.h>
6 #include <linux/utsname.h>
7 #include <net/net_namespace.h>
8 #include <linux/ipc_namespace.h>
9 #include <linux/pid_namespace.h>
10 #include <linux/user_namespace.h>
11 #include "internal.h"
12 
13 
14 static const struct proc_ns_operations *ns_entries[] = {
15 #ifdef CONFIG_NET_NS
16 	&netns_operations,
17 #endif
18 #ifdef CONFIG_UTS_NS
19 	&utsns_operations,
20 #endif
21 #ifdef CONFIG_IPC_NS
22 	&ipcns_operations,
23 #endif
24 #ifdef CONFIG_PID_NS
25 	&pidns_operations,
26 #endif
27 #ifdef CONFIG_USER_NS
28 	&userns_operations,
29 #endif
30 	&mntns_operations,
31 };
32 
33 static const char *proc_ns_get_link(struct dentry *dentry,
34 				    struct inode *inode, void **cookie)
35 {
36 	const struct proc_ns_operations *ns_ops = PROC_I(inode)->ns_ops;
37 	struct task_struct *task;
38 	struct path ns_path;
39 	void *error = ERR_PTR(-EACCES);
40 
41 	if (!dentry)
42 		return ERR_PTR(-ECHILD);
43 
44 	task = get_proc_task(inode);
45 	if (!task)
46 		return error;
47 
48 	if (ptrace_may_access(task, PTRACE_MODE_READ)) {
49 		error = ns_get_path(&ns_path, task, ns_ops);
50 		if (!error)
51 			nd_jump_link(&ns_path);
52 	}
53 	put_task_struct(task);
54 	return error;
55 }
56 
57 static int proc_ns_readlink(struct dentry *dentry, char __user *buffer, int buflen)
58 {
59 	struct inode *inode = d_inode(dentry);
60 	const struct proc_ns_operations *ns_ops = PROC_I(inode)->ns_ops;
61 	struct task_struct *task;
62 	char name[50];
63 	int res = -EACCES;
64 
65 	task = get_proc_task(inode);
66 	if (!task)
67 		return res;
68 
69 	if (ptrace_may_access(task, PTRACE_MODE_READ)) {
70 		res = ns_get_name(name, sizeof(name), task, ns_ops);
71 		if (res >= 0)
72 			res = readlink_copy(buffer, buflen, name);
73 	}
74 	put_task_struct(task);
75 	return res;
76 }
77 
78 static const struct inode_operations proc_ns_link_inode_operations = {
79 	.readlink	= proc_ns_readlink,
80 	.get_link	= proc_ns_get_link,
81 	.setattr	= proc_setattr,
82 };
83 
84 static int proc_ns_instantiate(struct inode *dir,
85 	struct dentry *dentry, struct task_struct *task, const void *ptr)
86 {
87 	const struct proc_ns_operations *ns_ops = ptr;
88 	struct inode *inode;
89 	struct proc_inode *ei;
90 
91 	inode = proc_pid_make_inode(dir->i_sb, task);
92 	if (!inode)
93 		goto out;
94 
95 	ei = PROC_I(inode);
96 	inode->i_mode = S_IFLNK|S_IRWXUGO;
97 	inode->i_op = &proc_ns_link_inode_operations;
98 	ei->ns_ops = ns_ops;
99 
100 	d_set_d_op(dentry, &pid_dentry_operations);
101 	d_add(dentry, inode);
102 	/* Close the race of the process dying before we return the dentry */
103 	if (pid_revalidate(dentry, 0))
104 		return 0;
105 out:
106 	return -ENOENT;
107 }
108 
109 static int proc_ns_dir_readdir(struct file *file, struct dir_context *ctx)
110 {
111 	struct task_struct *task = get_proc_task(file_inode(file));
112 	const struct proc_ns_operations **entry, **last;
113 
114 	if (!task)
115 		return -ENOENT;
116 
117 	if (!dir_emit_dots(file, ctx))
118 		goto out;
119 	if (ctx->pos >= 2 + ARRAY_SIZE(ns_entries))
120 		goto out;
121 	entry = ns_entries + (ctx->pos - 2);
122 	last = &ns_entries[ARRAY_SIZE(ns_entries) - 1];
123 	while (entry <= last) {
124 		const struct proc_ns_operations *ops = *entry;
125 		if (!proc_fill_cache(file, ctx, ops->name, strlen(ops->name),
126 				     proc_ns_instantiate, task, ops))
127 			break;
128 		ctx->pos++;
129 		entry++;
130 	}
131 out:
132 	put_task_struct(task);
133 	return 0;
134 }
135 
136 const struct file_operations proc_ns_dir_operations = {
137 	.read		= generic_read_dir,
138 	.iterate	= proc_ns_dir_readdir,
139 };
140 
141 static struct dentry *proc_ns_dir_lookup(struct inode *dir,
142 				struct dentry *dentry, unsigned int flags)
143 {
144 	int error;
145 	struct task_struct *task = get_proc_task(dir);
146 	const struct proc_ns_operations **entry, **last;
147 	unsigned int len = dentry->d_name.len;
148 
149 	error = -ENOENT;
150 
151 	if (!task)
152 		goto out_no_task;
153 
154 	last = &ns_entries[ARRAY_SIZE(ns_entries)];
155 	for (entry = ns_entries; entry < last; entry++) {
156 		if (strlen((*entry)->name) != len)
157 			continue;
158 		if (!memcmp(dentry->d_name.name, (*entry)->name, len))
159 			break;
160 	}
161 	if (entry == last)
162 		goto out;
163 
164 	error = proc_ns_instantiate(dir, dentry, task, *entry);
165 out:
166 	put_task_struct(task);
167 out_no_task:
168 	return ERR_PTR(error);
169 }
170 
171 const struct inode_operations proc_ns_dir_inode_operations = {
172 	.lookup		= proc_ns_dir_lookup,
173 	.getattr	= pid_getattr,
174 	.setattr	= proc_setattr,
175 };
176