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