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