ipc_sysctl.c 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289
  1. /*
  2. * Copyright (C) 2007
  3. *
  4. * Author: Eric Biederman <ebiederm@xmision.com>
  5. *
  6. * This program is free software; you can redistribute it and/or
  7. * modify it under the terms of the GNU General Public License as
  8. * published by the Free Software Foundation, version 2 of the
  9. * License.
  10. */
  11. #include <linux/module.h>
  12. #include <linux/ipc.h>
  13. #include <linux/nsproxy.h>
  14. #include <linux/sysctl.h>
  15. #include <linux/uaccess.h>
  16. #include <linux/ipc_namespace.h>
  17. #include <linux/msg.h>
  18. #include "util.h"
  19. static void *get_ipc(ctl_table *table)
  20. {
  21. char *which = table->data;
  22. struct ipc_namespace *ipc_ns = current->nsproxy->ipc_ns;
  23. which = (which - (char *)&init_ipc_ns) + (char *)ipc_ns;
  24. return which;
  25. }
  26. #ifdef CONFIG_PROC_SYSCTL
  27. static int proc_ipc_dointvec(ctl_table *table, int write, struct file *filp,
  28. void __user *buffer, size_t *lenp, loff_t *ppos)
  29. {
  30. struct ctl_table ipc_table;
  31. memcpy(&ipc_table, table, sizeof(ipc_table));
  32. ipc_table.data = get_ipc(table);
  33. return proc_dointvec(&ipc_table, write, filp, buffer, lenp, ppos);
  34. }
  35. static int proc_ipc_callback_dointvec(ctl_table *table, int write,
  36. struct file *filp, void __user *buffer, size_t *lenp, loff_t *ppos)
  37. {
  38. struct ctl_table ipc_table;
  39. size_t lenp_bef = *lenp;
  40. int rc;
  41. memcpy(&ipc_table, table, sizeof(ipc_table));
  42. ipc_table.data = get_ipc(table);
  43. rc = proc_dointvec(&ipc_table, write, filp, buffer, lenp, ppos);
  44. if (write && !rc && lenp_bef == *lenp)
  45. /*
  46. * Tunable has successfully been changed by hand. Disable its
  47. * automatic adjustment. This simply requires unregistering
  48. * the notifiers that trigger recalculation.
  49. */
  50. unregister_ipcns_notifier(current->nsproxy->ipc_ns);
  51. return rc;
  52. }
  53. static int proc_ipc_doulongvec_minmax(ctl_table *table, int write,
  54. struct file *filp, void __user *buffer, size_t *lenp, loff_t *ppos)
  55. {
  56. struct ctl_table ipc_table;
  57. memcpy(&ipc_table, table, sizeof(ipc_table));
  58. ipc_table.data = get_ipc(table);
  59. return proc_doulongvec_minmax(&ipc_table, write, filp, buffer,
  60. lenp, ppos);
  61. }
  62. /*
  63. * Routine that is called when the file "auto_msgmni" has successfully been
  64. * written.
  65. * Two values are allowed:
  66. * 0: unregister msgmni's callback routine from the ipc namespace notifier
  67. * chain. This means that msgmni won't be recomputed anymore upon memory
  68. * add/remove or ipc namespace creation/removal.
  69. * 1: register back the callback routine.
  70. */
  71. static void ipc_auto_callback(int val)
  72. {
  73. if (!val)
  74. unregister_ipcns_notifier(current->nsproxy->ipc_ns);
  75. else {
  76. /*
  77. * Re-enable automatic recomputing only if not already
  78. * enabled.
  79. */
  80. recompute_msgmni(current->nsproxy->ipc_ns);
  81. cond_register_ipcns_notifier(current->nsproxy->ipc_ns);
  82. }
  83. }
  84. static int proc_ipcauto_dointvec_minmax(ctl_table *table, int write,
  85. struct file *filp, void __user *buffer, size_t *lenp, loff_t *ppos)
  86. {
  87. struct ctl_table ipc_table;
  88. size_t lenp_bef = *lenp;
  89. int oldval;
  90. int rc;
  91. memcpy(&ipc_table, table, sizeof(ipc_table));
  92. ipc_table.data = get_ipc(table);
  93. oldval = *((int *)(ipc_table.data));
  94. rc = proc_dointvec_minmax(&ipc_table, write, filp, buffer, lenp, ppos);
  95. if (write && !rc && lenp_bef == *lenp) {
  96. int newval = *((int *)(ipc_table.data));
  97. /*
  98. * The file "auto_msgmni" has correctly been set.
  99. * React by (un)registering the corresponding tunable, if the
  100. * value has changed.
  101. */
  102. if (newval != oldval)
  103. ipc_auto_callback(newval);
  104. }
  105. return rc;
  106. }
  107. #else
  108. #define proc_ipc_doulongvec_minmax NULL
  109. #define proc_ipc_dointvec NULL
  110. #define proc_ipc_callback_dointvec NULL
  111. #define proc_ipcauto_dointvec_minmax NULL
  112. #endif
  113. #ifdef CONFIG_SYSCTL_SYSCALL
  114. /* The generic sysctl ipc data routine. */
  115. static int sysctl_ipc_data(ctl_table *table,
  116. void __user *oldval, size_t __user *oldlenp,
  117. void __user *newval, size_t newlen)
  118. {
  119. size_t len;
  120. void *data;
  121. /* Get out of I don't have a variable */
  122. if (!table->data || !table->maxlen)
  123. return -ENOTDIR;
  124. data = get_ipc(table);
  125. if (!data)
  126. return -ENOTDIR;
  127. if (oldval && oldlenp) {
  128. if (get_user(len, oldlenp))
  129. return -EFAULT;
  130. if (len) {
  131. if (len > table->maxlen)
  132. len = table->maxlen;
  133. if (copy_to_user(oldval, data, len))
  134. return -EFAULT;
  135. if (put_user(len, oldlenp))
  136. return -EFAULT;
  137. }
  138. }
  139. if (newval && newlen) {
  140. if (newlen > table->maxlen)
  141. newlen = table->maxlen;
  142. if (copy_from_user(data, newval, newlen))
  143. return -EFAULT;
  144. }
  145. return 1;
  146. }
  147. static int sysctl_ipc_registered_data(ctl_table *table,
  148. void __user *oldval, size_t __user *oldlenp,
  149. void __user *newval, size_t newlen)
  150. {
  151. int rc;
  152. rc = sysctl_ipc_data(table, oldval, oldlenp, newval, newlen);
  153. if (newval && newlen && rc > 0)
  154. /*
  155. * Tunable has successfully been changed from userland
  156. */
  157. unregister_ipcns_notifier(current->nsproxy->ipc_ns);
  158. return rc;
  159. }
  160. #else
  161. #define sysctl_ipc_data NULL
  162. #define sysctl_ipc_registered_data NULL
  163. #endif
  164. static int zero;
  165. static int one = 1;
  166. static struct ctl_table ipc_kern_table[] = {
  167. {
  168. .ctl_name = KERN_SHMMAX,
  169. .procname = "shmmax",
  170. .data = &init_ipc_ns.shm_ctlmax,
  171. .maxlen = sizeof (init_ipc_ns.shm_ctlmax),
  172. .mode = 0644,
  173. .proc_handler = proc_ipc_doulongvec_minmax,
  174. .strategy = sysctl_ipc_data,
  175. },
  176. {
  177. .ctl_name = KERN_SHMALL,
  178. .procname = "shmall",
  179. .data = &init_ipc_ns.shm_ctlall,
  180. .maxlen = sizeof (init_ipc_ns.shm_ctlall),
  181. .mode = 0644,
  182. .proc_handler = proc_ipc_doulongvec_minmax,
  183. .strategy = sysctl_ipc_data,
  184. },
  185. {
  186. .ctl_name = KERN_SHMMNI,
  187. .procname = "shmmni",
  188. .data = &init_ipc_ns.shm_ctlmni,
  189. .maxlen = sizeof (init_ipc_ns.shm_ctlmni),
  190. .mode = 0644,
  191. .proc_handler = proc_ipc_dointvec,
  192. .strategy = sysctl_ipc_data,
  193. },
  194. {
  195. .ctl_name = KERN_MSGMAX,
  196. .procname = "msgmax",
  197. .data = &init_ipc_ns.msg_ctlmax,
  198. .maxlen = sizeof (init_ipc_ns.msg_ctlmax),
  199. .mode = 0644,
  200. .proc_handler = proc_ipc_dointvec,
  201. .strategy = sysctl_ipc_data,
  202. },
  203. {
  204. .ctl_name = KERN_MSGMNI,
  205. .procname = "msgmni",
  206. .data = &init_ipc_ns.msg_ctlmni,
  207. .maxlen = sizeof (init_ipc_ns.msg_ctlmni),
  208. .mode = 0644,
  209. .proc_handler = proc_ipc_callback_dointvec,
  210. .strategy = sysctl_ipc_registered_data,
  211. },
  212. {
  213. .ctl_name = KERN_MSGMNB,
  214. .procname = "msgmnb",
  215. .data = &init_ipc_ns.msg_ctlmnb,
  216. .maxlen = sizeof (init_ipc_ns.msg_ctlmnb),
  217. .mode = 0644,
  218. .proc_handler = proc_ipc_dointvec,
  219. .strategy = sysctl_ipc_data,
  220. },
  221. {
  222. .ctl_name = KERN_SEM,
  223. .procname = "sem",
  224. .data = &init_ipc_ns.sem_ctls,
  225. .maxlen = 4*sizeof (int),
  226. .mode = 0644,
  227. .proc_handler = proc_ipc_dointvec,
  228. .strategy = sysctl_ipc_data,
  229. },
  230. {
  231. .ctl_name = CTL_UNNUMBERED,
  232. .procname = "auto_msgmni",
  233. .data = &init_ipc_ns.auto_msgmni,
  234. .maxlen = sizeof(int),
  235. .mode = 0644,
  236. .proc_handler = proc_ipcauto_dointvec_minmax,
  237. .extra1 = &zero,
  238. .extra2 = &one,
  239. },
  240. {}
  241. };
  242. static struct ctl_table ipc_root_table[] = {
  243. {
  244. .ctl_name = CTL_KERN,
  245. .procname = "kernel",
  246. .mode = 0555,
  247. .child = ipc_kern_table,
  248. },
  249. {}
  250. };
  251. static int __init ipc_sysctl_init(void)
  252. {
  253. register_sysctl_table(ipc_root_table);
  254. return 0;
  255. }
  256. __initcall(ipc_sysctl_init);