ipc_namespace.h 4.5 KB

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