ipc_sysctl.c 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248
  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(struct 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(struct ctl_table *table, int write,
  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, buffer, lenp, ppos);
  34. }
  35. static int proc_ipc_dointvec_minmax(struct ctl_table *table, int write,
  36. void __user *buffer, size_t *lenp, loff_t *ppos)
  37. {
  38. struct ctl_table ipc_table;
  39. memcpy(&ipc_table, table, sizeof(ipc_table));
  40. ipc_table.data = get_ipc(table);
  41. return proc_dointvec_minmax(&ipc_table, write, buffer, lenp, ppos);
  42. }
  43. static int proc_ipc_dointvec_minmax_orphans(struct ctl_table *table, int write,
  44. void __user *buffer, size_t *lenp, loff_t *ppos)
  45. {
  46. struct ipc_namespace *ns = current->nsproxy->ipc_ns;
  47. int err = proc_ipc_dointvec_minmax(table, write, buffer, lenp, ppos);
  48. if (err < 0)
  49. return err;
  50. if (ns->shm_rmid_forced)
  51. shm_destroy_orphaned(ns);
  52. return err;
  53. }
  54. static int proc_ipc_doulongvec_minmax(struct ctl_table *table, int write,
  55. void __user *buffer, size_t *lenp, loff_t *ppos)
  56. {
  57. struct ctl_table ipc_table;
  58. memcpy(&ipc_table, table, sizeof(ipc_table));
  59. ipc_table.data = get_ipc(table);
  60. return proc_doulongvec_minmax(&ipc_table, write, buffer,
  61. lenp, ppos);
  62. }
  63. static int proc_ipc_auto_msgmni(struct ctl_table *table, int write,
  64. void __user *buffer, size_t *lenp, loff_t *ppos)
  65. {
  66. struct ctl_table ipc_table;
  67. int dummy = 0;
  68. memcpy(&ipc_table, table, sizeof(ipc_table));
  69. ipc_table.data = &dummy;
  70. if (write)
  71. pr_info_once("writing to auto_msgmni has no effect");
  72. return proc_dointvec_minmax(&ipc_table, write, buffer, lenp, ppos);
  73. }
  74. static int proc_ipc_sem_dointvec(struct ctl_table *table, int write,
  75. void __user *buffer, size_t *lenp, loff_t *ppos)
  76. {
  77. int ret, semmni;
  78. struct ipc_namespace *ns = current->nsproxy->ipc_ns;
  79. semmni = ns->sem_ctls[3];
  80. ret = proc_ipc_dointvec(table, write, buffer, lenp, ppos);
  81. if (!ret)
  82. ret = sem_check_semmni(current->nsproxy->ipc_ns);
  83. /*
  84. * Reset the semmni value if an error happens.
  85. */
  86. if (ret)
  87. ns->sem_ctls[3] = semmni;
  88. return ret;
  89. }
  90. #else
  91. #define proc_ipc_doulongvec_minmax NULL
  92. #define proc_ipc_dointvec NULL
  93. #define proc_ipc_dointvec_minmax NULL
  94. #define proc_ipc_dointvec_minmax_orphans NULL
  95. #define proc_ipc_auto_msgmni NULL
  96. #define proc_ipc_sem_dointvec NULL
  97. #endif
  98. static int zero;
  99. static int one = 1;
  100. static int int_max = INT_MAX;
  101. static int ipc_mni = IPCMNI;
  102. static struct ctl_table ipc_kern_table[] = {
  103. {
  104. .procname = "shmmax",
  105. .data = &init_ipc_ns.shm_ctlmax,
  106. .maxlen = sizeof(init_ipc_ns.shm_ctlmax),
  107. .mode = 0644,
  108. .proc_handler = proc_ipc_doulongvec_minmax,
  109. },
  110. {
  111. .procname = "shmall",
  112. .data = &init_ipc_ns.shm_ctlall,
  113. .maxlen = sizeof(init_ipc_ns.shm_ctlall),
  114. .mode = 0644,
  115. .proc_handler = proc_ipc_doulongvec_minmax,
  116. },
  117. {
  118. .procname = "shmmni",
  119. .data = &init_ipc_ns.shm_ctlmni,
  120. .maxlen = sizeof(init_ipc_ns.shm_ctlmni),
  121. .mode = 0644,
  122. .proc_handler = proc_ipc_dointvec_minmax,
  123. .extra1 = &zero,
  124. .extra2 = &ipc_mni,
  125. },
  126. {
  127. .procname = "shm_rmid_forced",
  128. .data = &init_ipc_ns.shm_rmid_forced,
  129. .maxlen = sizeof(init_ipc_ns.shm_rmid_forced),
  130. .mode = 0644,
  131. .proc_handler = proc_ipc_dointvec_minmax_orphans,
  132. .extra1 = &zero,
  133. .extra2 = &one,
  134. },
  135. {
  136. .procname = "msgmax",
  137. .data = &init_ipc_ns.msg_ctlmax,
  138. .maxlen = sizeof(init_ipc_ns.msg_ctlmax),
  139. .mode = 0644,
  140. .proc_handler = proc_ipc_dointvec_minmax,
  141. .extra1 = &zero,
  142. .extra2 = &int_max,
  143. },
  144. {
  145. .procname = "msgmni",
  146. .data = &init_ipc_ns.msg_ctlmni,
  147. .maxlen = sizeof(init_ipc_ns.msg_ctlmni),
  148. .mode = 0644,
  149. .proc_handler = proc_ipc_dointvec_minmax,
  150. .extra1 = &zero,
  151. .extra2 = &ipc_mni,
  152. },
  153. {
  154. .procname = "auto_msgmni",
  155. .data = NULL,
  156. .maxlen = sizeof(int),
  157. .mode = 0644,
  158. .proc_handler = proc_ipc_auto_msgmni,
  159. .extra1 = &zero,
  160. .extra2 = &one,
  161. },
  162. {
  163. .procname = "msgmnb",
  164. .data = &init_ipc_ns.msg_ctlmnb,
  165. .maxlen = sizeof(init_ipc_ns.msg_ctlmnb),
  166. .mode = 0644,
  167. .proc_handler = proc_ipc_dointvec_minmax,
  168. .extra1 = &zero,
  169. .extra2 = &int_max,
  170. },
  171. {
  172. .procname = "sem",
  173. .data = &init_ipc_ns.sem_ctls,
  174. .maxlen = 4*sizeof(int),
  175. .mode = 0644,
  176. .proc_handler = proc_ipc_sem_dointvec,
  177. },
  178. #ifdef CONFIG_CHECKPOINT_RESTORE
  179. {
  180. .procname = "sem_next_id",
  181. .data = &init_ipc_ns.ids[IPC_SEM_IDS].next_id,
  182. .maxlen = sizeof(init_ipc_ns.ids[IPC_SEM_IDS].next_id),
  183. .mode = 0644,
  184. .proc_handler = proc_ipc_dointvec_minmax,
  185. .extra1 = &zero,
  186. .extra2 = &int_max,
  187. },
  188. {
  189. .procname = "msg_next_id",
  190. .data = &init_ipc_ns.ids[IPC_MSG_IDS].next_id,
  191. .maxlen = sizeof(init_ipc_ns.ids[IPC_MSG_IDS].next_id),
  192. .mode = 0644,
  193. .proc_handler = proc_ipc_dointvec_minmax,
  194. .extra1 = &zero,
  195. .extra2 = &int_max,
  196. },
  197. {
  198. .procname = "shm_next_id",
  199. .data = &init_ipc_ns.ids[IPC_SHM_IDS].next_id,
  200. .maxlen = sizeof(init_ipc_ns.ids[IPC_SHM_IDS].next_id),
  201. .mode = 0644,
  202. .proc_handler = proc_ipc_dointvec_minmax,
  203. .extra1 = &zero,
  204. .extra2 = &int_max,
  205. },
  206. #endif
  207. {}
  208. };
  209. static struct ctl_table ipc_root_table[] = {
  210. {
  211. .procname = "kernel",
  212. .mode = 0555,
  213. .child = ipc_kern_table,
  214. },
  215. {}
  216. };
  217. static int __init ipc_sysctl_init(void)
  218. {
  219. register_sysctl_table(ipc_root_table);
  220. return 0;
  221. }
  222. device_initcall(ipc_sysctl_init);