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