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