1 /*
2  *  linux/fs/proc/net.c
3  *
4  *  Copyright (C) 2007
5  *
6  *  Author: Eric Biederman <ebiederm@xmission.com>
7  *
8  *  proc net directory handling functions
9  */
10 
11 #include <linux/uaccess.h>
12 
13 #include <linux/errno.h>
14 #include <linux/time.h>
15 #include <linux/proc_fs.h>
16 #include <linux/stat.h>
17 #include <linux/slab.h>
18 #include <linux/init.h>
19 #include <linux/sched.h>
20 #include <linux/sched/task.h>
21 #include <linux/module.h>
22 #include <linux/bitops.h>
23 #include <linux/mount.h>
24 #include <linux/nsproxy.h>
25 #include <linux/uidgid.h>
26 #include <net/net_namespace.h>
27 #include <linux/seq_file.h>
28 
29 #include "internal.h"
30 
PDE_NET(struct proc_dir_entry * pde)31 static inline struct net *PDE_NET(struct proc_dir_entry *pde)
32 {
33 	return pde->parent->data;
34 }
35 
get_proc_net(const struct inode * inode)36 static struct net *get_proc_net(const struct inode *inode)
37 {
38 	return maybe_get_net(PDE_NET(PDE(inode)));
39 }
40 
seq_open_net(struct inode * inode,struct file * file)41 static int seq_open_net(struct inode *inode, struct file *file)
42 {
43 	unsigned int state_size = PDE(inode)->state_size;
44 	struct seq_net_private *p;
45 	struct net *net;
46 
47 	WARN_ON_ONCE(state_size < sizeof(*p));
48 
49 	if (file->f_mode & FMODE_WRITE && !PDE(inode)->write)
50 		return -EACCES;
51 
52 	net = get_proc_net(inode);
53 	if (!net)
54 		return -ENXIO;
55 
56 	p = __seq_open_private(file, PDE(inode)->seq_ops, state_size);
57 	if (!p) {
58 		put_net(net);
59 		return -ENOMEM;
60 	}
61 #ifdef CONFIG_NET_NS
62 	p->net = net;
63 #endif
64 	return 0;
65 }
66 
seq_release_net(struct inode * ino,struct file * f)67 static int seq_release_net(struct inode *ino, struct file *f)
68 {
69 	struct seq_file *seq = f->private_data;
70 
71 	put_net(seq_file_net(seq));
72 	seq_release_private(ino, f);
73 	return 0;
74 }
75 
76 static const struct file_operations proc_net_seq_fops = {
77 	.open		= seq_open_net,
78 	.read		= seq_read,
79 	.write		= proc_simple_write,
80 	.llseek		= seq_lseek,
81 	.release	= seq_release_net,
82 };
83 
proc_create_net_data(const char * name,umode_t mode,struct proc_dir_entry * parent,const struct seq_operations * ops,unsigned int state_size,void * data)84 struct proc_dir_entry *proc_create_net_data(const char *name, umode_t mode,
85 		struct proc_dir_entry *parent, const struct seq_operations *ops,
86 		unsigned int state_size, void *data)
87 {
88 	struct proc_dir_entry *p;
89 
90 	p = proc_create_reg(name, mode, &parent, data);
91 	if (!p)
92 		return NULL;
93 	pde_force_lookup(p);
94 	p->proc_fops = &proc_net_seq_fops;
95 	p->seq_ops = ops;
96 	p->state_size = state_size;
97 	return proc_register(parent, p);
98 }
99 EXPORT_SYMBOL_GPL(proc_create_net_data);
100 
101 /**
102  * proc_create_net_data_write - Create a writable net_ns-specific proc file
103  * @name: The name of the file.
104  * @mode: The file's access mode.
105  * @parent: The parent directory in which to create.
106  * @ops: The seq_file ops with which to read the file.
107  * @write: The write method which which to 'modify' the file.
108  * @data: Data for retrieval by PDE_DATA().
109  *
110  * Create a network namespaced proc file in the @parent directory with the
111  * specified @name and @mode that allows reading of a file that displays a
112  * series of elements and also provides for the file accepting writes that have
113  * some arbitrary effect.
114  *
115  * The functions in the @ops table are used to iterate over items to be
116  * presented and extract the readable content using the seq_file interface.
117  *
118  * The @write function is called with the data copied into a kernel space
119  * scratch buffer and has a NUL appended for convenience.  The buffer may be
120  * modified by the @write function.  @write should return 0 on success.
121  *
122  * The @data value is accessible from the @show and @write functions by calling
123  * PDE_DATA() on the file inode.  The network namespace must be accessed by
124  * calling seq_file_net() on the seq_file struct.
125  */
proc_create_net_data_write(const char * name,umode_t mode,struct proc_dir_entry * parent,const struct seq_operations * ops,proc_write_t write,unsigned int state_size,void * data)126 struct proc_dir_entry *proc_create_net_data_write(const char *name, umode_t mode,
127 						  struct proc_dir_entry *parent,
128 						  const struct seq_operations *ops,
129 						  proc_write_t write,
130 						  unsigned int state_size, void *data)
131 {
132 	struct proc_dir_entry *p;
133 
134 	p = proc_create_reg(name, mode, &parent, data);
135 	if (!p)
136 		return NULL;
137 	pde_force_lookup(p);
138 	p->proc_fops = &proc_net_seq_fops;
139 	p->seq_ops = ops;
140 	p->state_size = state_size;
141 	p->write = write;
142 	return proc_register(parent, p);
143 }
144 EXPORT_SYMBOL_GPL(proc_create_net_data_write);
145 
single_open_net(struct inode * inode,struct file * file)146 static int single_open_net(struct inode *inode, struct file *file)
147 {
148 	struct proc_dir_entry *de = PDE(inode);
149 	struct net *net;
150 	int err;
151 
152 	net = get_proc_net(inode);
153 	if (!net)
154 		return -ENXIO;
155 
156 	err = single_open(file, de->single_show, net);
157 	if (err)
158 		put_net(net);
159 	return err;
160 }
161 
single_release_net(struct inode * ino,struct file * f)162 static int single_release_net(struct inode *ino, struct file *f)
163 {
164 	struct seq_file *seq = f->private_data;
165 	put_net(seq->private);
166 	return single_release(ino, f);
167 }
168 
169 static const struct file_operations proc_net_single_fops = {
170 	.open		= single_open_net,
171 	.read		= seq_read,
172 	.write		= proc_simple_write,
173 	.llseek		= seq_lseek,
174 	.release	= single_release_net,
175 };
176 
proc_create_net_single(const char * name,umode_t mode,struct proc_dir_entry * parent,int (* show)(struct seq_file *,void *),void * data)177 struct proc_dir_entry *proc_create_net_single(const char *name, umode_t mode,
178 		struct proc_dir_entry *parent,
179 		int (*show)(struct seq_file *, void *), void *data)
180 {
181 	struct proc_dir_entry *p;
182 
183 	p = proc_create_reg(name, mode, &parent, data);
184 	if (!p)
185 		return NULL;
186 	pde_force_lookup(p);
187 	p->proc_fops = &proc_net_single_fops;
188 	p->single_show = show;
189 	return proc_register(parent, p);
190 }
191 EXPORT_SYMBOL_GPL(proc_create_net_single);
192 
193 /**
194  * proc_create_net_single_write - Create a writable net_ns-specific proc file
195  * @name: The name of the file.
196  * @mode: The file's access mode.
197  * @parent: The parent directory in which to create.
198  * @show: The seqfile show method with which to read the file.
199  * @write: The write method which which to 'modify' the file.
200  * @data: Data for retrieval by PDE_DATA().
201  *
202  * Create a network-namespaced proc file in the @parent directory with the
203  * specified @name and @mode that allows reading of a file that displays a
204  * single element rather than a series and also provides for the file accepting
205  * writes that have some arbitrary effect.
206  *
207  * The @show function is called to extract the readable content via the
208  * seq_file interface.
209  *
210  * The @write function is called with the data copied into a kernel space
211  * scratch buffer and has a NUL appended for convenience.  The buffer may be
212  * modified by the @write function.  @write should return 0 on success.
213  *
214  * The @data value is accessible from the @show and @write functions by calling
215  * PDE_DATA() on the file inode.  The network namespace must be accessed by
216  * calling seq_file_single_net() on the seq_file struct.
217  */
proc_create_net_single_write(const char * name,umode_t mode,struct proc_dir_entry * parent,int (* show)(struct seq_file *,void *),proc_write_t write,void * data)218 struct proc_dir_entry *proc_create_net_single_write(const char *name, umode_t mode,
219 						    struct proc_dir_entry *parent,
220 						    int (*show)(struct seq_file *, void *),
221 						    proc_write_t write,
222 						    void *data)
223 {
224 	struct proc_dir_entry *p;
225 
226 	p = proc_create_reg(name, mode, &parent, data);
227 	if (!p)
228 		return NULL;
229 	pde_force_lookup(p);
230 	p->proc_fops = &proc_net_single_fops;
231 	p->single_show = show;
232 	p->write = write;
233 	return proc_register(parent, p);
234 }
235 EXPORT_SYMBOL_GPL(proc_create_net_single_write);
236 
get_proc_task_net(struct inode * dir)237 static struct net *get_proc_task_net(struct inode *dir)
238 {
239 	struct task_struct *task;
240 	struct nsproxy *ns;
241 	struct net *net = NULL;
242 
243 	rcu_read_lock();
244 	task = pid_task(proc_pid(dir), PIDTYPE_PID);
245 	if (task != NULL) {
246 		task_lock(task);
247 		ns = task->nsproxy;
248 		if (ns != NULL)
249 			net = get_net(ns->net_ns);
250 		task_unlock(task);
251 	}
252 	rcu_read_unlock();
253 
254 	return net;
255 }
256 
proc_tgid_net_lookup(struct inode * dir,struct dentry * dentry,unsigned int flags)257 static struct dentry *proc_tgid_net_lookup(struct inode *dir,
258 		struct dentry *dentry, unsigned int flags)
259 {
260 	struct dentry *de;
261 	struct net *net;
262 
263 	de = ERR_PTR(-ENOENT);
264 	net = get_proc_task_net(dir);
265 	if (net != NULL) {
266 		de = proc_lookup_de(dir, dentry, net->proc_net);
267 		put_net(net);
268 	}
269 	return de;
270 }
271 
proc_tgid_net_getattr(const struct path * path,struct kstat * stat,u32 request_mask,unsigned int query_flags)272 static int proc_tgid_net_getattr(const struct path *path, struct kstat *stat,
273 				 u32 request_mask, unsigned int query_flags)
274 {
275 	struct inode *inode = d_inode(path->dentry);
276 	struct net *net;
277 
278 	net = get_proc_task_net(inode);
279 
280 	generic_fillattr(inode, stat);
281 
282 	if (net != NULL) {
283 		stat->nlink = net->proc_net->nlink;
284 		put_net(net);
285 	}
286 
287 	return 0;
288 }
289 
290 const struct inode_operations proc_net_inode_operations = {
291 	.lookup		= proc_tgid_net_lookup,
292 	.getattr	= proc_tgid_net_getattr,
293 };
294 
proc_tgid_net_readdir(struct file * file,struct dir_context * ctx)295 static int proc_tgid_net_readdir(struct file *file, struct dir_context *ctx)
296 {
297 	int ret;
298 	struct net *net;
299 
300 	ret = -EINVAL;
301 	net = get_proc_task_net(file_inode(file));
302 	if (net != NULL) {
303 		ret = proc_readdir_de(file, ctx, net->proc_net);
304 		put_net(net);
305 	}
306 	return ret;
307 }
308 
309 const struct file_operations proc_net_operations = {
310 	.llseek		= generic_file_llseek,
311 	.read		= generic_read_dir,
312 	.iterate_shared	= proc_tgid_net_readdir,
313 };
314 
proc_net_ns_init(struct net * net)315 static __net_init int proc_net_ns_init(struct net *net)
316 {
317 	struct proc_dir_entry *netd, *net_statd;
318 	kuid_t uid;
319 	kgid_t gid;
320 	int err;
321 
322 	err = -ENOMEM;
323 	netd = kmem_cache_zalloc(proc_dir_entry_cache, GFP_KERNEL);
324 	if (!netd)
325 		goto out;
326 
327 	netd->subdir = RB_ROOT;
328 	netd->data = net;
329 	netd->nlink = 2;
330 	netd->namelen = 3;
331 	netd->parent = &proc_root;
332 	netd->name = netd->inline_name;
333 	memcpy(netd->name, "net", 4);
334 
335 	uid = make_kuid(net->user_ns, 0);
336 	if (!uid_valid(uid))
337 		uid = netd->uid;
338 
339 	gid = make_kgid(net->user_ns, 0);
340 	if (!gid_valid(gid))
341 		gid = netd->gid;
342 
343 	proc_set_user(netd, uid, gid);
344 
345 	/* Seed dentry revalidation for /proc/${pid}/net */
346 	pde_force_lookup(netd);
347 
348 	err = -EEXIST;
349 	net_statd = proc_net_mkdir(net, "stat", netd);
350 	if (!net_statd)
351 		goto free_net;
352 
353 	net->proc_net = netd;
354 	net->proc_net_stat = net_statd;
355 	return 0;
356 
357 free_net:
358 	pde_free(netd);
359 out:
360 	return err;
361 }
362 
proc_net_ns_exit(struct net * net)363 static __net_exit void proc_net_ns_exit(struct net *net)
364 {
365 	remove_proc_entry("stat", net->proc_net);
366 	pde_free(net->proc_net);
367 }
368 
369 static struct pernet_operations __net_initdata proc_net_ns_ops = {
370 	.init = proc_net_ns_init,
371 	.exit = proc_net_ns_exit,
372 };
373 
proc_net_init(void)374 int __init proc_net_init(void)
375 {
376 	proc_symlink("net", NULL, "self/net");
377 
378 	return register_pernet_subsys(&proc_net_ns_ops);
379 }
380