ipc_sysctl.c 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * Copyright (C) 2007
  4. *
  5. * Author: Eric Biederman <ebiederm@xmision.com>
  6. */
  7. #include <linux/module.h>
  8. #include <linux/ipc.h>
  9. #include <linux/nsproxy.h>
  10. #include <linux/sysctl.h>
  11. #include <linux/uaccess.h>
  12. #include <linux/ipc_namespace.h>
  13. #include <linux/msg.h>
  14. #include "util.h"
  15. static void *get_ipc(struct ctl_table *table)
  16. {
  17. char *which = table->data;
  18. struct ipc_namespace *ipc_ns = current->nsproxy->ipc_ns;
  19. which = (which - (char *)&init_ipc_ns) + (char *)ipc_ns;
  20. return which;
  21. }
  22. #ifdef CONFIG_PROC_SYSCTL
  23. static int proc_ipc_dointvec(struct ctl_table *table, int write,
  24. void *buffer, size_t *lenp, loff_t *ppos)
  25. {
  26. struct ctl_table ipc_table;
  27. memcpy(&ipc_table, table, sizeof(ipc_table));
  28. ipc_table.data = get_ipc(table);
  29. return proc_dointvec(&ipc_table, write, buffer, lenp, ppos);
  30. }
  31. static int proc_ipc_dointvec_minmax(struct ctl_table *table, int write,
  32. void *buffer, size_t *lenp, loff_t *ppos)
  33. {
  34. struct ctl_table ipc_table;
  35. memcpy(&ipc_table, table, sizeof(ipc_table));
  36. ipc_table.data = get_ipc(table);
  37. return proc_dointvec_minmax(&ipc_table, write, buffer, lenp, ppos);
  38. }
  39. static int proc_ipc_dointvec_minmax_orphans(struct ctl_table *table, int write,
  40. void *buffer, size_t *lenp, loff_t *ppos)
  41. {
  42. struct ipc_namespace *ns = current->nsproxy->ipc_ns;
  43. int err = proc_ipc_dointvec_minmax(table, write, buffer, lenp, ppos);
  44. if (err < 0)
  45. return err;
  46. if (ns->shm_rmid_forced)
  47. shm_destroy_orphaned(ns);
  48. return err;
  49. }
  50. static int proc_ipc_doulongvec_minmax(struct ctl_table *table, int write,
  51. void *buffer, size_t *lenp, loff_t *ppos)
  52. {
  53. struct ctl_table ipc_table;
  54. memcpy(&ipc_table, table, sizeof(ipc_table));
  55. ipc_table.data = get_ipc(table);
  56. return proc_doulongvec_minmax(&ipc_table, write, buffer,
  57. lenp, ppos);
  58. }
  59. static int proc_ipc_auto_msgmni(struct ctl_table *table, int write,
  60. void *buffer, size_t *lenp, loff_t *ppos)
  61. {
  62. struct ctl_table ipc_table;
  63. int dummy = 0;
  64. memcpy(&ipc_table, table, sizeof(ipc_table));
  65. ipc_table.data = &dummy;
  66. if (write)
  67. pr_info_once("writing to auto_msgmni has no effect");
  68. return proc_dointvec_minmax(&ipc_table, write, buffer, lenp, ppos);
  69. }
  70. static int proc_ipc_sem_dointvec(struct ctl_table *table, int write,
  71. void *buffer, size_t *lenp, loff_t *ppos)
  72. {
  73. int ret, semmni;
  74. struct ipc_namespace *ns = current->nsproxy->ipc_ns;
  75. semmni = ns->sem_ctls[3];
  76. ret = proc_ipc_dointvec(table, write, buffer, lenp, ppos);
  77. if (!ret)
  78. ret = sem_check_semmni(current->nsproxy->ipc_ns);
  79. /*
  80. * Reset the semmni value if an error happens.
  81. */
  82. if (ret)
  83. ns->sem_ctls[3] = semmni;
  84. return ret;
  85. }
  86. #else
  87. #define proc_ipc_doulongvec_minmax NULL
  88. #define proc_ipc_dointvec NULL
  89. #define proc_ipc_dointvec_minmax NULL
  90. #define proc_ipc_dointvec_minmax_orphans NULL
  91. #define proc_ipc_auto_msgmni NULL
  92. #define proc_ipc_sem_dointvec NULL
  93. #endif
  94. int ipc_mni = IPCMNI;
  95. int ipc_mni_shift = IPCMNI_SHIFT;
  96. int ipc_min_cycle = RADIX_TREE_MAP_SIZE;
  97. static struct ctl_table ipc_kern_table[] = {
  98. {
  99. .procname = "shmmax",
  100. .data = &init_ipc_ns.shm_ctlmax,
  101. .maxlen = sizeof(init_ipc_ns.shm_ctlmax),
  102. .mode = 0644,
  103. .proc_handler = proc_ipc_doulongvec_minmax,
  104. },
  105. {
  106. .procname = "shmall",
  107. .data = &init_ipc_ns.shm_ctlall,
  108. .maxlen = sizeof(init_ipc_ns.shm_ctlall),
  109. .mode = 0644,
  110. .proc_handler = proc_ipc_doulongvec_minmax,
  111. },
  112. {
  113. .procname = "shmmni",
  114. .data = &init_ipc_ns.shm_ctlmni,
  115. .maxlen = sizeof(init_ipc_ns.shm_ctlmni),
  116. .mode = 0644,
  117. .proc_handler = proc_ipc_dointvec_minmax,
  118. .extra1 = SYSCTL_ZERO,
  119. .extra2 = &ipc_mni,
  120. },
  121. {
  122. .procname = "shm_rmid_forced",
  123. .data = &init_ipc_ns.shm_rmid_forced,
  124. .maxlen = sizeof(init_ipc_ns.shm_rmid_forced),
  125. .mode = 0644,
  126. .proc_handler = proc_ipc_dointvec_minmax_orphans,
  127. .extra1 = SYSCTL_ZERO,
  128. .extra2 = SYSCTL_ONE,
  129. },
  130. {
  131. .procname = "msgmax",
  132. .data = &init_ipc_ns.msg_ctlmax,
  133. .maxlen = sizeof(init_ipc_ns.msg_ctlmax),
  134. .mode = 0644,
  135. .proc_handler = proc_ipc_dointvec_minmax,
  136. .extra1 = SYSCTL_ZERO,
  137. .extra2 = SYSCTL_INT_MAX,
  138. },
  139. {
  140. .procname = "msgmni",
  141. .data = &init_ipc_ns.msg_ctlmni,
  142. .maxlen = sizeof(init_ipc_ns.msg_ctlmni),
  143. .mode = 0644,
  144. .proc_handler = proc_ipc_dointvec_minmax,
  145. .extra1 = SYSCTL_ZERO,
  146. .extra2 = &ipc_mni,
  147. },
  148. {
  149. .procname = "auto_msgmni",
  150. .data = NULL,
  151. .maxlen = sizeof(int),
  152. .mode = 0644,
  153. .proc_handler = proc_ipc_auto_msgmni,
  154. .extra1 = SYSCTL_ZERO,
  155. .extra2 = SYSCTL_ONE,
  156. },
  157. {
  158. .procname = "msgmnb",
  159. .data = &init_ipc_ns.msg_ctlmnb,
  160. .maxlen = sizeof(init_ipc_ns.msg_ctlmnb),
  161. .mode = 0644,
  162. .proc_handler = proc_ipc_dointvec_minmax,
  163. .extra1 = SYSCTL_ZERO,
  164. .extra2 = SYSCTL_INT_MAX,
  165. },
  166. {
  167. .procname = "sem",
  168. .data = &init_ipc_ns.sem_ctls,
  169. .maxlen = 4*sizeof(int),
  170. .mode = 0644,
  171. .proc_handler = proc_ipc_sem_dointvec,
  172. },
  173. #ifdef CONFIG_CHECKPOINT_RESTORE
  174. {
  175. .procname = "sem_next_id",
  176. .data = &init_ipc_ns.ids[IPC_SEM_IDS].next_id,
  177. .maxlen = sizeof(init_ipc_ns.ids[IPC_SEM_IDS].next_id),
  178. .mode = 0644,
  179. .proc_handler = proc_ipc_dointvec_minmax,
  180. .extra1 = SYSCTL_ZERO,
  181. .extra2 = SYSCTL_INT_MAX,
  182. },
  183. {
  184. .procname = "msg_next_id",
  185. .data = &init_ipc_ns.ids[IPC_MSG_IDS].next_id,
  186. .maxlen = sizeof(init_ipc_ns.ids[IPC_MSG_IDS].next_id),
  187. .mode = 0644,
  188. .proc_handler = proc_ipc_dointvec_minmax,
  189. .extra1 = SYSCTL_ZERO,
  190. .extra2 = SYSCTL_INT_MAX,
  191. },
  192. {
  193. .procname = "shm_next_id",
  194. .data = &init_ipc_ns.ids[IPC_SHM_IDS].next_id,
  195. .maxlen = sizeof(init_ipc_ns.ids[IPC_SHM_IDS].next_id),
  196. .mode = 0644,
  197. .proc_handler = proc_ipc_dointvec_minmax,
  198. .extra1 = SYSCTL_ZERO,
  199. .extra2 = SYSCTL_INT_MAX,
  200. },
  201. #endif
  202. {}
  203. };
  204. static struct ctl_table ipc_root_table[] = {
  205. {
  206. .procname = "kernel",
  207. .mode = 0555,
  208. .child = ipc_kern_table,
  209. },
  210. {}
  211. };
  212. static int __init ipc_sysctl_init(void)
  213. {
  214. register_sysctl_table(ipc_root_table);
  215. return 0;
  216. }
  217. device_initcall(ipc_sysctl_init);
  218. static int __init ipc_mni_extend(char *str)
  219. {
  220. ipc_mni = IPCMNI_EXTEND;
  221. ipc_mni_shift = IPCMNI_EXTEND_SHIFT;
  222. ipc_min_cycle = IPCMNI_EXTEND_MIN_CYCLE;
  223. pr_info("IPCMNI extended to %d.\n", ipc_mni);
  224. return 0;
  225. }
  226. early_param("ipcmni_extend", ipc_mni_extend);