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