proc_net.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416
  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 <[email protected]>
  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. * @data: Data for retrieval by pde_data().
  119. *
  120. * Create a network namespaced proc file in the @parent directory with the
  121. * specified @name and @mode that allows reading of a file that displays a
  122. * series of elements and also provides for the file accepting writes that have
  123. * some arbitrary effect.
  124. *
  125. * The functions in the @ops table are used to iterate over items to be
  126. * presented and extract the readable content using the seq_file interface.
  127. *
  128. * The @write function is called with the data copied into a kernel space
  129. * scratch buffer and has a NUL appended for convenience. The buffer may be
  130. * modified by the @write function. @write should return 0 on success.
  131. *
  132. * The @data value is accessible from the @show and @write functions by calling
  133. * pde_data() on the file inode. The network namespace must be accessed by
  134. * calling seq_file_net() on the seq_file struct.
  135. */
  136. struct proc_dir_entry *proc_create_net_data_write(const char *name, umode_t mode,
  137. struct proc_dir_entry *parent,
  138. const struct seq_operations *ops,
  139. proc_write_t write,
  140. unsigned int state_size, void *data)
  141. {
  142. struct proc_dir_entry *p;
  143. p = proc_create_reg(name, mode, &parent, data);
  144. if (!p)
  145. return NULL;
  146. pde_force_lookup(p);
  147. p->proc_ops = &proc_net_seq_ops;
  148. p->seq_ops = ops;
  149. p->state_size = state_size;
  150. p->write = write;
  151. return proc_register(parent, p);
  152. }
  153. EXPORT_SYMBOL_GPL(proc_create_net_data_write);
  154. static int single_open_net(struct inode *inode, struct file *file)
  155. {
  156. struct proc_dir_entry *de = PDE(inode);
  157. struct net *net;
  158. int err;
  159. net = get_proc_net(inode);
  160. if (!net)
  161. return -ENXIO;
  162. err = single_open(file, de->single_show, net);
  163. if (err)
  164. put_net(net);
  165. return err;
  166. }
  167. static int single_release_net(struct inode *ino, struct file *f)
  168. {
  169. struct seq_file *seq = f->private_data;
  170. put_net(seq->private);
  171. return single_release(ino, f);
  172. }
  173. static const struct proc_ops proc_net_single_ops = {
  174. .proc_open = single_open_net,
  175. .proc_read = seq_read,
  176. .proc_write = proc_simple_write,
  177. .proc_lseek = seq_lseek,
  178. .proc_release = single_release_net,
  179. };
  180. struct proc_dir_entry *proc_create_net_single(const char *name, umode_t mode,
  181. struct proc_dir_entry *parent,
  182. int (*show)(struct seq_file *, void *), void *data)
  183. {
  184. struct proc_dir_entry *p;
  185. p = proc_create_reg(name, mode, &parent, data);
  186. if (!p)
  187. return NULL;
  188. pde_force_lookup(p);
  189. p->proc_ops = &proc_net_single_ops;
  190. p->single_show = show;
  191. return proc_register(parent, p);
  192. }
  193. EXPORT_SYMBOL_GPL(proc_create_net_single);
  194. /**
  195. * proc_create_net_single_write - Create a writable net_ns-specific proc file
  196. * @name: The name of the file.
  197. * @mode: The file's access mode.
  198. * @parent: The parent directory in which to create.
  199. * @show: The seqfile show method with which to read the file.
  200. * @write: The write method with which to 'modify' the file.
  201. * @data: Data for retrieval by pde_data().
  202. *
  203. * Create a network-namespaced proc file in the @parent directory with the
  204. * specified @name and @mode that allows reading of a file that displays a
  205. * single element rather than a series and also provides for the file accepting
  206. * writes that have some arbitrary effect.
  207. *
  208. * The @show function is called to extract the readable content via the
  209. * seq_file interface.
  210. *
  211. * The @write function is called with the data copied into a kernel space
  212. * scratch buffer and has a NUL appended for convenience. The buffer may be
  213. * modified by the @write function. @write should return 0 on success.
  214. *
  215. * The @data value is accessible from the @show and @write functions by calling
  216. * pde_data() on the file inode. The network namespace must be accessed by
  217. * calling seq_file_single_net() on the seq_file struct.
  218. */
  219. struct proc_dir_entry *proc_create_net_single_write(const char *name, umode_t mode,
  220. struct proc_dir_entry *parent,
  221. int (*show)(struct seq_file *, void *),
  222. proc_write_t write,
  223. void *data)
  224. {
  225. struct proc_dir_entry *p;
  226. p = proc_create_reg(name, mode, &parent, data);
  227. if (!p)
  228. return NULL;
  229. pde_force_lookup(p);
  230. p->proc_ops = &proc_net_single_ops;
  231. p->single_show = show;
  232. p->write = write;
  233. return proc_register(parent, p);
  234. }
  235. EXPORT_SYMBOL_GPL(proc_create_net_single_write);
  236. static struct net *get_proc_task_net(struct inode *dir)
  237. {
  238. struct task_struct *task;
  239. struct nsproxy *ns;
  240. struct net *net = NULL;
  241. rcu_read_lock();
  242. task = pid_task(proc_pid(dir), PIDTYPE_PID);
  243. if (task != NULL) {
  244. task_lock(task);
  245. ns = task->nsproxy;
  246. if (ns != NULL)
  247. net = get_net(ns->net_ns);
  248. task_unlock(task);
  249. }
  250. rcu_read_unlock();
  251. return net;
  252. }
  253. static struct dentry *proc_tgid_net_lookup(struct inode *dir,
  254. struct dentry *dentry, unsigned int flags)
  255. {
  256. struct dentry *de;
  257. struct net *net;
  258. de = ERR_PTR(-ENOENT);
  259. net = get_proc_task_net(dir);
  260. if (net != NULL) {
  261. de = proc_lookup_de(dir, dentry, net->proc_net);
  262. put_net(net);
  263. }
  264. return de;
  265. }
  266. static int proc_tgid_net_getattr(struct user_namespace *mnt_userns,
  267. const struct path *path, struct kstat *stat,
  268. u32 request_mask, unsigned int query_flags)
  269. {
  270. struct inode *inode = d_inode(path->dentry);
  271. struct net *net;
  272. net = get_proc_task_net(inode);
  273. generic_fillattr(&init_user_ns, inode, stat);
  274. if (net != NULL) {
  275. stat->nlink = net->proc_net->nlink;
  276. put_net(net);
  277. }
  278. return 0;
  279. }
  280. const struct inode_operations proc_net_inode_operations = {
  281. .lookup = proc_tgid_net_lookup,
  282. .getattr = proc_tgid_net_getattr,
  283. };
  284. static int proc_tgid_net_readdir(struct file *file, struct dir_context *ctx)
  285. {
  286. int ret;
  287. struct net *net;
  288. ret = -EINVAL;
  289. net = get_proc_task_net(file_inode(file));
  290. if (net != NULL) {
  291. ret = proc_readdir_de(file, ctx, net->proc_net);
  292. put_net(net);
  293. }
  294. return ret;
  295. }
  296. const struct file_operations proc_net_operations = {
  297. .llseek = generic_file_llseek,
  298. .read = generic_read_dir,
  299. .iterate_shared = proc_tgid_net_readdir,
  300. };
  301. static __net_init int proc_net_ns_init(struct net *net)
  302. {
  303. struct proc_dir_entry *netd, *net_statd;
  304. kuid_t uid;
  305. kgid_t gid;
  306. int err;
  307. /*
  308. * This PDE acts only as an anchor for /proc/${pid}/net hierarchy.
  309. * Corresponding inode (PDE(inode) == net->proc_net) is never
  310. * instantiated therefore blanket zeroing is fine.
  311. * net->proc_net_stat inode is instantiated normally.
  312. */
  313. err = -ENOMEM;
  314. netd = kmem_cache_zalloc(proc_dir_entry_cache, GFP_KERNEL);
  315. if (!netd)
  316. goto out;
  317. netd->subdir = RB_ROOT;
  318. netd->data = net;
  319. netd->nlink = 2;
  320. netd->namelen = 3;
  321. netd->parent = &proc_root;
  322. netd->name = netd->inline_name;
  323. memcpy(netd->name, "net", 4);
  324. uid = make_kuid(net->user_ns, 0);
  325. if (!uid_valid(uid))
  326. uid = netd->uid;
  327. gid = make_kgid(net->user_ns, 0);
  328. if (!gid_valid(gid))
  329. gid = netd->gid;
  330. proc_set_user(netd, uid, gid);
  331. /* Seed dentry revalidation for /proc/${pid}/net */
  332. pde_force_lookup(netd);
  333. err = -EEXIST;
  334. net_statd = proc_net_mkdir(net, "stat", netd);
  335. if (!net_statd)
  336. goto free_net;
  337. net->proc_net = netd;
  338. net->proc_net_stat = net_statd;
  339. return 0;
  340. free_net:
  341. pde_free(netd);
  342. out:
  343. return err;
  344. }
  345. static __net_exit void proc_net_ns_exit(struct net *net)
  346. {
  347. remove_proc_entry("stat", net->proc_net);
  348. pde_free(net->proc_net);
  349. }
  350. static struct pernet_operations __net_initdata proc_net_ns_ops = {
  351. .init = proc_net_ns_init,
  352. .exit = proc_net_ns_exit,
  353. };
  354. int __init proc_net_init(void)
  355. {
  356. proc_symlink("net", NULL, "self/net");
  357. return register_pernet_subsys(&proc_net_ns_ops);
  358. }