ipc_namespace.h 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213
  1. /* SPDX-License-Identifier: GPL-2.0 */
  2. #ifndef __IPC_NAMESPACE_H__
  3. #define __IPC_NAMESPACE_H__
  4. #include <linux/err.h>
  5. #include <linux/idr.h>
  6. #include <linux/rwsem.h>
  7. #include <linux/notifier.h>
  8. #include <linux/nsproxy.h>
  9. #include <linux/ns_common.h>
  10. #include <linux/refcount.h>
  11. #include <linux/rhashtable-types.h>
  12. #include <linux/sysctl.h>
  13. #include <linux/percpu_counter.h>
  14. struct user_namespace;
  15. struct ipc_ids {
  16. int in_use;
  17. unsigned short seq;
  18. struct rw_semaphore rwsem;
  19. struct idr ipcs_idr;
  20. int max_idx;
  21. int last_idx; /* For wrap around detection */
  22. #ifdef CONFIG_CHECKPOINT_RESTORE
  23. int next_id;
  24. #endif
  25. struct rhashtable key_ht;
  26. };
  27. struct ipc_namespace {
  28. struct ipc_ids ids[3];
  29. int sem_ctls[4];
  30. int used_sems;
  31. unsigned int msg_ctlmax;
  32. unsigned int msg_ctlmnb;
  33. unsigned int msg_ctlmni;
  34. struct percpu_counter percpu_msg_bytes;
  35. struct percpu_counter percpu_msg_hdrs;
  36. size_t shm_ctlmax;
  37. size_t shm_ctlall;
  38. unsigned long shm_tot;
  39. int shm_ctlmni;
  40. /*
  41. * Defines whether IPC_RMID is forced for _all_ shm segments regardless
  42. * of shmctl()
  43. */
  44. int shm_rmid_forced;
  45. struct notifier_block ipcns_nb;
  46. /* The kern_mount of the mqueuefs sb. We take a ref on it */
  47. struct vfsmount *mq_mnt;
  48. /* # queues in this ns, protected by mq_lock */
  49. unsigned int mq_queues_count;
  50. /* next fields are set through sysctl */
  51. unsigned int mq_queues_max; /* initialized to DFLT_QUEUESMAX */
  52. unsigned int mq_msg_max; /* initialized to DFLT_MSGMAX */
  53. unsigned int mq_msgsize_max; /* initialized to DFLT_MSGSIZEMAX */
  54. unsigned int mq_msg_default;
  55. unsigned int mq_msgsize_default;
  56. struct ctl_table_set mq_set;
  57. struct ctl_table_header *mq_sysctls;
  58. struct ctl_table_set ipc_set;
  59. struct ctl_table_header *ipc_sysctls;
  60. /* user_ns which owns the ipc ns */
  61. struct user_namespace *user_ns;
  62. struct ucounts *ucounts;
  63. struct llist_node mnt_llist;
  64. struct ns_common ns;
  65. } __randomize_layout;
  66. extern struct ipc_namespace init_ipc_ns;
  67. extern spinlock_t mq_lock;
  68. #ifdef CONFIG_SYSVIPC
  69. extern void shm_destroy_orphaned(struct ipc_namespace *ns);
  70. #else /* CONFIG_SYSVIPC */
  71. static inline void shm_destroy_orphaned(struct ipc_namespace *ns) {}
  72. #endif /* CONFIG_SYSVIPC */
  73. #ifdef CONFIG_POSIX_MQUEUE
  74. extern int mq_init_ns(struct ipc_namespace *ns);
  75. /*
  76. * POSIX Message Queue default values:
  77. *
  78. * MIN_*: Lowest value an admin can set the maximum unprivileged limit to
  79. * DFLT_*MAX: Default values for the maximum unprivileged limits
  80. * DFLT_{MSG,MSGSIZE}: Default values used when the user doesn't supply
  81. * an attribute to the open call and the queue must be created
  82. * HARD_*: Highest value the maximums can be set to. These are enforced
  83. * on CAP_SYS_RESOURCE apps as well making them inviolate (so make them
  84. * suitably high)
  85. *
  86. * POSIX Requirements:
  87. * Per app minimum openable message queues - 8. This does not map well
  88. * to the fact that we limit the number of queues on a per namespace
  89. * basis instead of a per app basis. So, make the default high enough
  90. * that no given app should have a hard time opening 8 queues.
  91. * Minimum maximum for HARD_MSGMAX - 32767. I bumped this to 65536.
  92. * Minimum maximum for HARD_MSGSIZEMAX - POSIX is silent on this. However,
  93. * we have run into a situation where running applications in the wild
  94. * require this to be at least 5MB, and preferably 10MB, so I set the
  95. * value to 16MB in hopes that this user is the worst of the bunch and
  96. * the new maximum will handle anyone else. I may have to revisit this
  97. * in the future.
  98. */
  99. #define DFLT_QUEUESMAX 256
  100. #define MIN_MSGMAX 1
  101. #define DFLT_MSG 10U
  102. #define DFLT_MSGMAX 10
  103. #define HARD_MSGMAX 65536
  104. #define MIN_MSGSIZEMAX 128
  105. #define DFLT_MSGSIZE 8192U
  106. #define DFLT_MSGSIZEMAX 8192
  107. #define HARD_MSGSIZEMAX (16*1024*1024)
  108. #else
  109. static inline int mq_init_ns(struct ipc_namespace *ns) { return 0; }
  110. #endif
  111. #if defined(CONFIG_IPC_NS)
  112. extern struct ipc_namespace *copy_ipcs(unsigned long flags,
  113. struct user_namespace *user_ns, struct ipc_namespace *ns);
  114. static inline struct ipc_namespace *get_ipc_ns(struct ipc_namespace *ns)
  115. {
  116. if (ns)
  117. refcount_inc(&ns->ns.count);
  118. return ns;
  119. }
  120. static inline struct ipc_namespace *get_ipc_ns_not_zero(struct ipc_namespace *ns)
  121. {
  122. if (ns) {
  123. if (refcount_inc_not_zero(&ns->ns.count))
  124. return ns;
  125. }
  126. return NULL;
  127. }
  128. extern void put_ipc_ns(struct ipc_namespace *ns);
  129. #else
  130. static inline struct ipc_namespace *copy_ipcs(unsigned long flags,
  131. struct user_namespace *user_ns, struct ipc_namespace *ns)
  132. {
  133. if (flags & CLONE_NEWIPC)
  134. return ERR_PTR(-EINVAL);
  135. return ns;
  136. }
  137. static inline struct ipc_namespace *get_ipc_ns(struct ipc_namespace *ns)
  138. {
  139. return ns;
  140. }
  141. static inline struct ipc_namespace *get_ipc_ns_not_zero(struct ipc_namespace *ns)
  142. {
  143. return ns;
  144. }
  145. static inline void put_ipc_ns(struct ipc_namespace *ns)
  146. {
  147. }
  148. #endif
  149. #ifdef CONFIG_POSIX_MQUEUE_SYSCTL
  150. void retire_mq_sysctls(struct ipc_namespace *ns);
  151. bool setup_mq_sysctls(struct ipc_namespace *ns);
  152. #else /* CONFIG_POSIX_MQUEUE_SYSCTL */
  153. static inline void retire_mq_sysctls(struct ipc_namespace *ns)
  154. {
  155. }
  156. static inline bool setup_mq_sysctls(struct ipc_namespace *ns)
  157. {
  158. return true;
  159. }
  160. #endif /* CONFIG_POSIX_MQUEUE_SYSCTL */
  161. #ifdef CONFIG_SYSVIPC_SYSCTL
  162. bool setup_ipc_sysctls(struct ipc_namespace *ns);
  163. void retire_ipc_sysctls(struct ipc_namespace *ns);
  164. #else /* CONFIG_SYSVIPC_SYSCTL */
  165. static inline void retire_ipc_sysctls(struct ipc_namespace *ns)
  166. {
  167. }
  168. static inline bool setup_ipc_sysctls(struct ipc_namespace *ns)
  169. {
  170. return true;
  171. }
  172. #endif /* CONFIG_SYSVIPC_SYSCTL */
  173. #endif