proc_net.c 10 KB

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