sysctl_net.c 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /* -*- linux-c -*-
  3. * sysctl_net.c: sysctl interface to net subsystem.
  4. *
  5. * Begun April 1, 1996, Mike Shaver.
  6. * Added /proc/sys/net directories for each protocol family. [MS]
  7. *
  8. * Revision 1.2 1996/05/08 20:24:40 shaver
  9. * Added bits for NET_BRIDGE and the NET_IPV4_ARP stuff and
  10. * NET_IPV4_IP_FORWARD.
  11. *
  12. *
  13. */
  14. #include <linux/mm.h>
  15. #include <linux/export.h>
  16. #include <linux/sysctl.h>
  17. #include <linux/nsproxy.h>
  18. #include <net/sock.h>
  19. #ifdef CONFIG_INET
  20. #include <net/ip.h>
  21. #endif
  22. #ifdef CONFIG_NET
  23. #include <linux/if_ether.h>
  24. #endif
  25. static struct ctl_table_set *
  26. net_ctl_header_lookup(struct ctl_table_root *root)
  27. {
  28. return &current->nsproxy->net_ns->sysctls;
  29. }
  30. static int is_seen(struct ctl_table_set *set)
  31. {
  32. return &current->nsproxy->net_ns->sysctls == set;
  33. }
  34. /* Return standard mode bits for table entry. */
  35. static int net_ctl_permissions(struct ctl_table_header *head,
  36. const struct ctl_table *table)
  37. {
  38. struct net *net = container_of(head->set, struct net, sysctls);
  39. /* Allow network administrator to have same access as root. */
  40. if (ns_capable_noaudit(net->user_ns, CAP_NET_ADMIN)) {
  41. int mode = (table->mode >> 6) & 7;
  42. return (mode << 6) | (mode << 3) | mode;
  43. }
  44. return table->mode;
  45. }
  46. static void net_ctl_set_ownership(struct ctl_table_header *head,
  47. kuid_t *uid, kgid_t *gid)
  48. {
  49. struct net *net = container_of(head->set, struct net, sysctls);
  50. kuid_t ns_root_uid;
  51. kgid_t ns_root_gid;
  52. ns_root_uid = make_kuid(net->user_ns, 0);
  53. if (uid_valid(ns_root_uid))
  54. *uid = ns_root_uid;
  55. ns_root_gid = make_kgid(net->user_ns, 0);
  56. if (gid_valid(ns_root_gid))
  57. *gid = ns_root_gid;
  58. }
  59. static struct ctl_table_root net_sysctl_root = {
  60. .lookup = net_ctl_header_lookup,
  61. .permissions = net_ctl_permissions,
  62. .set_ownership = net_ctl_set_ownership,
  63. };
  64. static int __net_init sysctl_net_init(struct net *net)
  65. {
  66. setup_sysctl_set(&net->sysctls, &net_sysctl_root, is_seen);
  67. return 0;
  68. }
  69. static void __net_exit sysctl_net_exit(struct net *net)
  70. {
  71. retire_sysctl_set(&net->sysctls);
  72. }
  73. static struct pernet_operations sysctl_pernet_ops = {
  74. .init = sysctl_net_init,
  75. .exit = sysctl_net_exit,
  76. };
  77. static struct ctl_table_header *net_header;
  78. __init int net_sysctl_init(void)
  79. {
  80. static struct ctl_table empty[1];
  81. int ret = -ENOMEM;
  82. /* Avoid limitations in the sysctl implementation by
  83. * registering "/proc/sys/net" as an empty directory not in a
  84. * network namespace.
  85. */
  86. net_header = register_sysctl_sz("net", empty, 0);
  87. if (!net_header)
  88. goto out;
  89. ret = register_pernet_subsys(&sysctl_pernet_ops);
  90. if (ret)
  91. goto out1;
  92. out:
  93. return ret;
  94. out1:
  95. unregister_sysctl_table(net_header);
  96. net_header = NULL;
  97. goto out;
  98. }
  99. /* Verify that sysctls for non-init netns are safe by either:
  100. * 1) being read-only, or
  101. * 2) having a data pointer which points outside of the global kernel/module
  102. * data segment, and rather into the heap where a per-net object was
  103. * allocated.
  104. */
  105. static void ensure_safe_net_sysctl(struct net *net, const char *path,
  106. struct ctl_table *table, size_t table_size)
  107. {
  108. struct ctl_table *ent;
  109. pr_debug("Registering net sysctl (net %p): %s\n", net, path);
  110. ent = table;
  111. for (size_t i = 0; i < table_size; ent++, i++) {
  112. unsigned long addr;
  113. const char *where;
  114. pr_debug(" procname=%s mode=%o proc_handler=%ps data=%p\n",
  115. ent->procname, ent->mode, ent->proc_handler, ent->data);
  116. /* If it's not writable inside the netns, then it can't hurt. */
  117. if ((ent->mode & 0222) == 0) {
  118. pr_debug(" Not writable by anyone\n");
  119. continue;
  120. }
  121. /* Where does data point? */
  122. addr = (unsigned long)ent->data;
  123. if (is_module_address(addr))
  124. where = "module";
  125. else if (is_kernel_core_data(addr))
  126. where = "kernel";
  127. else
  128. continue;
  129. /* If it is writable and points to kernel/module global
  130. * data, then it's probably a netns leak.
  131. */
  132. WARN(1, "sysctl %s/%s: data points to %s global data: %ps\n",
  133. path, ent->procname, where, ent->data);
  134. /* Make it "safe" by dropping writable perms */
  135. ent->mode &= ~0222;
  136. }
  137. }
  138. struct ctl_table_header *register_net_sysctl_sz(struct net *net,
  139. const char *path,
  140. struct ctl_table *table,
  141. size_t table_size)
  142. {
  143. if (!net_eq(net, &init_net))
  144. ensure_safe_net_sysctl(net, path, table, table_size);
  145. return __register_sysctl_table(&net->sysctls, path, table, table_size);
  146. }
  147. EXPORT_SYMBOL_GPL(register_net_sysctl_sz);
  148. void unregister_net_sysctl_table(struct ctl_table_header *header)
  149. {
  150. unregister_sysctl_table(header);
  151. }
  152. EXPORT_SYMBOL_GPL(unregister_net_sysctl_table);