ipc_sysctl.c 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244
  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. /*
  27. * Routine that is called when a tunable has successfully been changed by
  28. * hand and it has a callback routine registered on the ipc namespace notifier
  29. * chain: we don't want such tunables to be recomputed anymore upon memory
  30. * add/remove or ipc namespace creation/removal.
  31. * They can come back to a recomputable state by being set to a <0 value.
  32. */
  33. static void tunable_set_callback(int val)
  34. {
  35. if (val >= 0)
  36. unregister_ipcns_notifier(current->nsproxy->ipc_ns);
  37. else {
  38. /*
  39. * Re-enable automatic recomputing only if not already
  40. * enabled.
  41. */
  42. recompute_msgmni(current->nsproxy->ipc_ns);
  43. cond_register_ipcns_notifier(current->nsproxy->ipc_ns);
  44. }
  45. }
  46. #ifdef CONFIG_PROC_FS
  47. static int proc_ipc_dointvec(ctl_table *table, int write, struct file *filp,
  48. void __user *buffer, size_t *lenp, loff_t *ppos)
  49. {
  50. struct ctl_table ipc_table;
  51. memcpy(&ipc_table, table, sizeof(ipc_table));
  52. ipc_table.data = get_ipc(table);
  53. return proc_dointvec(&ipc_table, write, filp, buffer, lenp, ppos);
  54. }
  55. static int proc_ipc_callback_dointvec(ctl_table *table, int write,
  56. struct file *filp, void __user *buffer, size_t *lenp, loff_t *ppos)
  57. {
  58. struct ctl_table ipc_table;
  59. size_t lenp_bef = *lenp;
  60. int rc;
  61. memcpy(&ipc_table, table, sizeof(ipc_table));
  62. ipc_table.data = get_ipc(table);
  63. rc = proc_dointvec(&ipc_table, write, filp, buffer, lenp, ppos);
  64. if (write && !rc && lenp_bef == *lenp)
  65. tunable_set_callback(*((int *)(ipc_table.data)));
  66. return rc;
  67. }
  68. static int proc_ipc_doulongvec_minmax(ctl_table *table, int write,
  69. struct file *filp, void __user *buffer, size_t *lenp, loff_t *ppos)
  70. {
  71. struct ctl_table ipc_table;
  72. memcpy(&ipc_table, table, sizeof(ipc_table));
  73. ipc_table.data = get_ipc(table);
  74. return proc_doulongvec_minmax(&ipc_table, write, filp, buffer,
  75. lenp, ppos);
  76. }
  77. #else
  78. #define proc_ipc_doulongvec_minmax NULL
  79. #define proc_ipc_dointvec NULL
  80. #define proc_ipc_callback_dointvec NULL
  81. #endif
  82. #ifdef CONFIG_SYSCTL_SYSCALL
  83. /* The generic sysctl ipc data routine. */
  84. static int sysctl_ipc_data(ctl_table *table, int __user *name, int nlen,
  85. void __user *oldval, size_t __user *oldlenp,
  86. void __user *newval, size_t newlen)
  87. {
  88. size_t len;
  89. void *data;
  90. /* Get out of I don't have a variable */
  91. if (!table->data || !table->maxlen)
  92. return -ENOTDIR;
  93. data = get_ipc(table);
  94. if (!data)
  95. return -ENOTDIR;
  96. if (oldval && oldlenp) {
  97. if (get_user(len, oldlenp))
  98. return -EFAULT;
  99. if (len) {
  100. if (len > table->maxlen)
  101. len = table->maxlen;
  102. if (copy_to_user(oldval, data, len))
  103. return -EFAULT;
  104. if (put_user(len, oldlenp))
  105. return -EFAULT;
  106. }
  107. }
  108. if (newval && newlen) {
  109. if (newlen > table->maxlen)
  110. newlen = table->maxlen;
  111. if (copy_from_user(data, newval, newlen))
  112. return -EFAULT;
  113. }
  114. return 1;
  115. }
  116. static int sysctl_ipc_registered_data(ctl_table *table, int __user *name,
  117. int nlen, void __user *oldval, size_t __user *oldlenp,
  118. void __user *newval, size_t newlen)
  119. {
  120. int rc;
  121. rc = sysctl_ipc_data(table, name, nlen, oldval, oldlenp, newval,
  122. newlen);
  123. if (newval && newlen && rc > 0) {
  124. /*
  125. * Tunable has successfully been changed from userland
  126. */
  127. int *data = get_ipc(table);
  128. tunable_set_callback(*data);
  129. }
  130. return rc;
  131. }
  132. #else
  133. #define sysctl_ipc_data NULL
  134. #define sysctl_ipc_registered_data NULL
  135. #endif
  136. static struct ctl_table ipc_kern_table[] = {
  137. {
  138. .ctl_name = KERN_SHMMAX,
  139. .procname = "shmmax",
  140. .data = &init_ipc_ns.shm_ctlmax,
  141. .maxlen = sizeof (init_ipc_ns.shm_ctlmax),
  142. .mode = 0644,
  143. .proc_handler = proc_ipc_doulongvec_minmax,
  144. .strategy = sysctl_ipc_data,
  145. },
  146. {
  147. .ctl_name = KERN_SHMALL,
  148. .procname = "shmall",
  149. .data = &init_ipc_ns.shm_ctlall,
  150. .maxlen = sizeof (init_ipc_ns.shm_ctlall),
  151. .mode = 0644,
  152. .proc_handler = proc_ipc_doulongvec_minmax,
  153. .strategy = sysctl_ipc_data,
  154. },
  155. {
  156. .ctl_name = KERN_SHMMNI,
  157. .procname = "shmmni",
  158. .data = &init_ipc_ns.shm_ctlmni,
  159. .maxlen = sizeof (init_ipc_ns.shm_ctlmni),
  160. .mode = 0644,
  161. .proc_handler = proc_ipc_dointvec,
  162. .strategy = sysctl_ipc_data,
  163. },
  164. {
  165. .ctl_name = KERN_MSGMAX,
  166. .procname = "msgmax",
  167. .data = &init_ipc_ns.msg_ctlmax,
  168. .maxlen = sizeof (init_ipc_ns.msg_ctlmax),
  169. .mode = 0644,
  170. .proc_handler = proc_ipc_dointvec,
  171. .strategy = sysctl_ipc_data,
  172. },
  173. {
  174. .ctl_name = KERN_MSGMNI,
  175. .procname = "msgmni",
  176. .data = &init_ipc_ns.msg_ctlmni,
  177. .maxlen = sizeof (init_ipc_ns.msg_ctlmni),
  178. .mode = 0644,
  179. .proc_handler = proc_ipc_callback_dointvec,
  180. .strategy = sysctl_ipc_registered_data,
  181. },
  182. {
  183. .ctl_name = KERN_MSGMNB,
  184. .procname = "msgmnb",
  185. .data = &init_ipc_ns.msg_ctlmnb,
  186. .maxlen = sizeof (init_ipc_ns.msg_ctlmnb),
  187. .mode = 0644,
  188. .proc_handler = proc_ipc_dointvec,
  189. .strategy = sysctl_ipc_data,
  190. },
  191. {
  192. .ctl_name = KERN_SEM,
  193. .procname = "sem",
  194. .data = &init_ipc_ns.sem_ctls,
  195. .maxlen = 4*sizeof (int),
  196. .mode = 0644,
  197. .proc_handler = proc_ipc_dointvec,
  198. .strategy = sysctl_ipc_data,
  199. },
  200. {}
  201. };
  202. static struct ctl_table ipc_root_table[] = {
  203. {
  204. .ctl_name = CTL_KERN,
  205. .procname = "kernel",
  206. .mode = 0555,
  207. .child = ipc_kern_table,
  208. },
  209. {}
  210. };
  211. static int __init ipc_sysctl_init(void)
  212. {
  213. register_sysctl_table(ipc_root_table);
  214. return 0;
  215. }
  216. __initcall(ipc_sysctl_init);