]> git.ipfire.org Git - people/ms/linux.git/blame - fs/proc/proc_net.c
Merge tag 'soc-fixes-6.0-rc7' of git://git.kernel.org/pub/scm/linux/kernel/git/soc/soc
[people/ms/linux.git] / fs / proc / proc_net.c
CommitLineData
457c8996 1// SPDX-License-Identifier: GPL-2.0-only
3c12afe7
DM
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 */
3c12afe7
DM
11#include <linux/errno.h>
12#include <linux/time.h>
13#include <linux/proc_fs.h>
14#include <linux/stat.h>
5a0e3ad6 15#include <linux/slab.h>
3c12afe7
DM
16#include <linux/init.h>
17#include <linux/sched.h>
f719ff9b 18#include <linux/sched/task.h>
3c12afe7
DM
19#include <linux/module.h>
20#include <linux/bitops.h>
3c12afe7
DM
21#include <linux/mount.h>
22#include <linux/nsproxy.h>
c110486f 23#include <linux/uidgid.h>
3c12afe7 24#include <net/net_namespace.h>
e372c414 25#include <linux/seq_file.h>
3c12afe7
DM
26
27#include "internal.h"
28
4abfd029
DH
29static inline struct net *PDE_NET(struct proc_dir_entry *pde)
30{
31 return pde->parent->data;
32}
3c12afe7 33
8086cd45
AB
34static struct net *get_proc_net(const struct inode *inode)
35{
36 return maybe_get_net(PDE_NET(PDE(inode)));
37}
38
c3506372 39static int seq_open_net(struct inode *inode, struct file *file)
e372c414 40{
c3506372 41 unsigned int state_size = PDE(inode)->state_size;
e372c414 42 struct seq_net_private *p;
c3506372 43 struct net *net;
e372c414 44
c3506372 45 WARN_ON_ONCE(state_size < sizeof(*p));
e372c414 46
564def71
DH
47 if (file->f_mode & FMODE_WRITE && !PDE(inode)->write)
48 return -EACCES;
49
c3506372
CH
50 net = get_proc_net(inode);
51 if (!net)
e372c414
DL
52 return -ENXIO;
53
c3506372
CH
54 p = __seq_open_private(file, PDE(inode)->seq_ops, state_size);
55 if (!p) {
e372c414
DL
56 put_net(net);
57 return -ENOMEM;
58 }
1218854a 59#ifdef CONFIG_NET_NS
e372c414 60 p->net = net;
04a931e5 61 netns_tracker_alloc(net, &p->ns_tracker, GFP_KERNEL);
1218854a 62#endif
e372c414
DL
63 return 0;
64}
c3506372 65
04a931e5
ED
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
c3506372
CH
77static int seq_release_net(struct inode *ino, struct file *f)
78{
79 struct seq_file *seq = f->private_data;
80
04a931e5 81 seq_file_net_put_net(seq);
c3506372
CH
82 seq_release_private(ino, f);
83 return 0;
84}
85
d56c0d45
AD
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,
c3506372
CH
92};
93
f9c79272 94int bpf_iter_init_seq_net(void *priv_data, struct bpf_iter_aux_info *aux)
138d0be3
YS
95{
96#ifdef CONFIG_NET_NS
97 struct seq_net_private *p = priv_data;
98
04a931e5
ED
99 p->net = get_net_track(current->nsproxy->net_ns, &p->ns_tracker,
100 GFP_KERNEL);
138d0be3
YS
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
04a931e5 110 put_net_track(p->net, &p->ns_tracker);
138d0be3
YS
111#endif
112}
113
c3506372
CH
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;
1fde6f21 123 pde_force_lookup(p);
d56c0d45 124 p->proc_ops = &proc_net_seq_ops;
c3506372
CH
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);
e372c414 130
564def71
DH
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.
d2928e85 137 * @write: The write method with which to 'modify' the file.
359745d7 138 * @data: Data for retrieval by pde_data().
564def71
DH
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
359745d7 153 * pde_data() on the file inode. The network namespace must be accessed by
564def71
DH
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;
1fde6f21 167 pde_force_lookup(p);
d56c0d45 168 p->proc_ops = &proc_net_seq_ops;
564def71
DH
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
3617d949 176static int single_open_net(struct inode *inode, struct file *file)
de05c557 177{
3617d949 178 struct proc_dir_entry *de = PDE(inode);
de05c557 179 struct net *net;
3617d949 180 int err;
de05c557 181
de05c557 182 net = get_proc_net(inode);
3617d949
CH
183 if (!net)
184 return -ENXIO;
de05c557 185
3617d949
CH
186 err = single_open(file, de->single_show, net);
187 if (err)
188 put_net(net);
de05c557
PE
189 return err;
190}
de05c557 191
3617d949 192static int single_release_net(struct inode *ino, struct file *f)
b6fcbdb4
PE
193{
194 struct seq_file *seq = f->private_data;
195 put_net(seq->private);
196 return single_release(ino, f);
197}
3617d949 198
d56c0d45
AD
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,
3617d949
CH
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;
1fde6f21 216 pde_force_lookup(p);
d56c0d45 217 p->proc_ops = &proc_net_single_ops;
3617d949
CH
218 p->single_show = show;
219 return proc_register(parent, p);
220}
221EXPORT_SYMBOL_GPL(proc_create_net_single);
b6fcbdb4 222
564def71
DH
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.
d2928e85 229 * @write: The write method with which to 'modify' the file.
359745d7 230 * @data: Data for retrieval by pde_data().
564def71
DH
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
359745d7 245 * pde_data() on the file inode. The network namespace must be accessed by
564def71
DH
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;
1fde6f21 259 pde_force_lookup(p);
d56c0d45 260 p->proc_ops = &proc_net_single_ops;
564def71
DH
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
e9720acd
PE
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) {
728dba3a
EB
276 task_lock(task);
277 ns = task->nsproxy;
e9720acd
PE
278 if (ns != NULL)
279 net = get_net(ns->net_ns);
728dba3a 280 task_unlock(task);
e9720acd
PE
281 }
282 rcu_read_unlock();
283
284 return net;
285}
286
287static struct dentry *proc_tgid_net_lookup(struct inode *dir,
00cd8dd3 288 struct dentry *dentry, unsigned int flags)
e9720acd
PE
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) {
93ad5bc6 296 de = proc_lookup_de(dir, dentry, net->proc_net);
e9720acd
PE
297 put_net(net);
298 }
299 return de;
300}
301
549c7297
CB
302static int proc_tgid_net_getattr(struct user_namespace *mnt_userns,
303 const struct path *path, struct kstat *stat,
a528d35e 304 u32 request_mask, unsigned int query_flags)
e9720acd 305{
a528d35e 306 struct inode *inode = d_inode(path->dentry);
e9720acd
PE
307 struct net *net;
308
309 net = get_proc_task_net(inode);
310
0d56a451 311 generic_fillattr(&init_user_ns, inode, stat);
e9720acd
PE
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};
325
f0c3b509 326static int proc_tgid_net_readdir(struct file *file, struct dir_context *ctx)
e9720acd
PE
327{
328 int ret;
329 struct net *net;
330
331 ret = -EINVAL;
f0c3b509 332 net = get_proc_task_net(file_inode(file));
e9720acd 333 if (net != NULL) {
93ad5bc6 334 ret = proc_readdir_de(file, ctx, net->proc_net);
e9720acd
PE
335 put_net(net);
336 }
337 return ret;
338}
339
340const struct file_operations proc_net_operations = {
b4df2b92 341 .llseek = generic_file_llseek,
e9720acd 342 .read = generic_read_dir,
f50752ea 343 .iterate_shared = proc_tgid_net_readdir,
e9720acd
PE
344};
345
4665079c 346static __net_init int proc_net_ns_init(struct net *net)
3c12afe7 347{
e9720acd 348 struct proc_dir_entry *netd, *net_statd;
c110486f
DT
349 kuid_t uid;
350 kgid_t gid;
3c12afe7
DM
351 int err;
352
ed8fb78d
AD
353 /*
354 * This PDE acts only as an anchor for /proc/${pid}/net hierarchy.
355 * Corresponding inode (PDE(inode) == net->proc_net) is never
356 * instantiated therefore blanket zeroing is fine.
357 * net->proc_net_stat inode is instantiated normally.
358 */
3c12afe7 359 err = -ENOMEM;
b4884f23 360 netd = kmem_cache_zalloc(proc_dir_entry_cache, GFP_KERNEL);
e9720acd 361 if (!netd)
3c12afe7
DM
362 goto out;
363
4f113437 364 netd->subdir = RB_ROOT;
e9720acd
PE
365 netd->data = net;
366 netd->nlink = 2;
e9720acd
PE
367 netd->namelen = 3;
368 netd->parent = &proc_root;
b4884f23 369 netd->name = netd->inline_name;
09570f91 370 memcpy(netd->name, "net", 4);
3c12afe7 371
c110486f
DT
372 uid = make_kuid(net->user_ns, 0);
373 if (!uid_valid(uid))
374 uid = netd->uid;
375
376 gid = make_kgid(net->user_ns, 0);
377 if (!gid_valid(gid))
378 gid = netd->gid;
379
380 proc_set_user(netd, uid, gid);
381
70551977
AD
382 /* Seed dentry revalidation for /proc/${pid}/net */
383 pde_force_lookup(netd);
384
3c12afe7 385 err = -EEXIST;
e5d69b9f 386 net_statd = proc_net_mkdir(net, "stat", netd);
3c12afe7
DM
387 if (!net_statd)
388 goto free_net;
389
3c12afe7
DM
390 net->proc_net = netd;
391 net->proc_net_stat = net_statd;
e9720acd 392 return 0;
3c12afe7 393
e9720acd 394free_net:
b4884f23 395 pde_free(netd);
3c12afe7
DM
396out:
397 return err;
3c12afe7
DM
398}
399
4665079c 400static __net_exit void proc_net_ns_exit(struct net *net)
3c12afe7
DM
401{
402 remove_proc_entry("stat", net->proc_net);
b4884f23 403 pde_free(net->proc_net);
3c12afe7
DM
404}
405
022cbae6 406static struct pernet_operations __net_initdata proc_net_ns_ops = {
3c12afe7
DM
407 .init = proc_net_ns_init,
408 .exit = proc_net_ns_exit,
409};
410
4665079c 411int __init proc_net_init(void)
3c12afe7 412{
155134fe 413 proc_symlink("net", NULL, "self/net");
3c12afe7
DM
414
415 return register_pernet_subsys(&proc_net_ns_ops);
416}