Linux Audio

Check our new training course

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