util.h 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290
  1. /* SPDX-License-Identifier: GPL-2.0 */
  2. /*
  3. * linux/ipc/util.h
  4. * Copyright (C) 1999 Christoph Rohland
  5. *
  6. * ipc helper functions (c) 1999 Manfred Spraul <manfred@colorfullife.com>
  7. * namespaces support. 2006 OpenVZ, SWsoft Inc.
  8. * Pavel Emelianov <xemul@openvz.org>
  9. */
  10. #ifndef _IPC_UTIL_H
  11. #define _IPC_UTIL_H
  12. #include <linux/unistd.h>
  13. #include <linux/err.h>
  14. #include <linux/ipc_namespace.h>
  15. /*
  16. * The IPC ID contains 2 separate numbers - index and sequence number.
  17. * By default,
  18. * bits 0-14: index (32k, 15 bits)
  19. * bits 15-30: sequence number (64k, 16 bits)
  20. *
  21. * When IPCMNI extension mode is turned on, the composition changes:
  22. * bits 0-23: index (16M, 24 bits)
  23. * bits 24-30: sequence number (128, 7 bits)
  24. */
  25. #define IPCMNI_SHIFT 15
  26. #define IPCMNI_EXTEND_SHIFT 24
  27. #define IPCMNI_EXTEND_MIN_CYCLE (RADIX_TREE_MAP_SIZE * RADIX_TREE_MAP_SIZE)
  28. #define IPCMNI (1 << IPCMNI_SHIFT)
  29. #define IPCMNI_EXTEND (1 << IPCMNI_EXTEND_SHIFT)
  30. #ifdef CONFIG_SYSVIPC_SYSCTL
  31. extern int ipc_mni;
  32. extern int ipc_mni_shift;
  33. extern int ipc_min_cycle;
  34. #define ipcmni_seq_shift() ipc_mni_shift
  35. #define IPCMNI_IDX_MASK ((1 << ipc_mni_shift) - 1)
  36. #else /* CONFIG_SYSVIPC_SYSCTL */
  37. #define ipc_mni IPCMNI
  38. #define ipc_min_cycle ((int)RADIX_TREE_MAP_SIZE)
  39. #define ipcmni_seq_shift() IPCMNI_SHIFT
  40. #define IPCMNI_IDX_MASK ((1 << IPCMNI_SHIFT) - 1)
  41. #endif /* CONFIG_SYSVIPC_SYSCTL */
  42. void sem_init(void);
  43. void msg_init(void);
  44. void shm_init(void);
  45. struct ipc_namespace;
  46. struct pid_namespace;
  47. #ifdef CONFIG_POSIX_MQUEUE
  48. extern void mq_clear_sbinfo(struct ipc_namespace *ns);
  49. extern void mq_put_mnt(struct ipc_namespace *ns);
  50. #else
  51. static inline void mq_clear_sbinfo(struct ipc_namespace *ns) { }
  52. static inline void mq_put_mnt(struct ipc_namespace *ns) { }
  53. #endif
  54. #ifdef CONFIG_SYSVIPC
  55. void sem_init_ns(struct ipc_namespace *ns);
  56. void msg_init_ns(struct ipc_namespace *ns);
  57. void shm_init_ns(struct ipc_namespace *ns);
  58. void sem_exit_ns(struct ipc_namespace *ns);
  59. void msg_exit_ns(struct ipc_namespace *ns);
  60. void shm_exit_ns(struct ipc_namespace *ns);
  61. #else
  62. static inline void sem_init_ns(struct ipc_namespace *ns) { }
  63. static inline void msg_init_ns(struct ipc_namespace *ns) { }
  64. static inline void shm_init_ns(struct ipc_namespace *ns) { }
  65. static inline void sem_exit_ns(struct ipc_namespace *ns) { }
  66. static inline void msg_exit_ns(struct ipc_namespace *ns) { }
  67. static inline void shm_exit_ns(struct ipc_namespace *ns) { }
  68. #endif
  69. /*
  70. * Structure that holds the parameters needed by the ipc operations
  71. * (see after)
  72. */
  73. struct ipc_params {
  74. key_t key;
  75. int flg;
  76. union {
  77. size_t size; /* for shared memories */
  78. int nsems; /* for semaphores */
  79. } u; /* holds the getnew() specific param */
  80. };
  81. /*
  82. * Structure that holds some ipc operations. This structure is used to unify
  83. * the calls to sys_msgget(), sys_semget(), sys_shmget()
  84. * . routine to call to create a new ipc object. Can be one of newque,
  85. * newary, newseg
  86. * . routine to call to check permissions for a new ipc object.
  87. * Can be one of security_msg_associate, security_sem_associate,
  88. * security_shm_associate
  89. * . routine to call for an extra check if needed
  90. */
  91. struct ipc_ops {
  92. int (*getnew)(struct ipc_namespace *, struct ipc_params *);
  93. int (*associate)(struct kern_ipc_perm *, int);
  94. int (*more_checks)(struct kern_ipc_perm *, struct ipc_params *);
  95. };
  96. struct seq_file;
  97. struct ipc_ids;
  98. void ipc_init_ids(struct ipc_ids *ids);
  99. #ifdef CONFIG_PROC_FS
  100. void __init ipc_init_proc_interface(const char *path, const char *header,
  101. int ids, int (*show)(struct seq_file *, void *));
  102. struct pid_namespace *ipc_seq_pid_ns(struct seq_file *);
  103. #else
  104. #define ipc_init_proc_interface(path, header, ids, show) do {} while (0)
  105. #endif
  106. #define IPC_SEM_IDS 0
  107. #define IPC_MSG_IDS 1
  108. #define IPC_SHM_IDS 2
  109. #define ipcid_to_idx(id) ((id) & IPCMNI_IDX_MASK)
  110. #define ipcid_to_seqx(id) ((id) >> ipcmni_seq_shift())
  111. #define ipcid_seq_max() (INT_MAX >> ipcmni_seq_shift())
  112. /* must be called with ids->rwsem acquired for writing */
  113. int ipc_addid(struct ipc_ids *, struct kern_ipc_perm *, int);
  114. /* must be called with both locks acquired. */
  115. void ipc_rmid(struct ipc_ids *, struct kern_ipc_perm *);
  116. /* must be called with both locks acquired. */
  117. void ipc_set_key_private(struct ipc_ids *, struct kern_ipc_perm *);
  118. /* must be called with ipcp locked */
  119. int ipcperms(struct ipc_namespace *ns, struct kern_ipc_perm *ipcp, short flg);
  120. /**
  121. * ipc_get_maxidx - get the highest assigned index
  122. * @ids: ipc identifier set
  123. *
  124. * Called with ipc_ids.rwsem held for reading.
  125. */
  126. static inline int ipc_get_maxidx(struct ipc_ids *ids)
  127. {
  128. if (ids->in_use == 0)
  129. return -1;
  130. if (ids->in_use == ipc_mni)
  131. return ipc_mni - 1;
  132. return ids->max_idx;
  133. }
  134. /*
  135. * For allocation that need to be freed by RCU.
  136. * Objects are reference counted, they start with reference count 1.
  137. * getref increases the refcount, the putref call that reduces the recount
  138. * to 0 schedules the rcu destruction. Caller must guarantee locking.
  139. *
  140. * refcount is initialized by ipc_addid(), before that point call_rcu()
  141. * must be used.
  142. */
  143. bool ipc_rcu_getref(struct kern_ipc_perm *ptr);
  144. void ipc_rcu_putref(struct kern_ipc_perm *ptr,
  145. void (*func)(struct rcu_head *head));
  146. struct kern_ipc_perm *ipc_obtain_object_idr(struct ipc_ids *ids, int id);
  147. void kernel_to_ipc64_perm(struct kern_ipc_perm *in, struct ipc64_perm *out);
  148. void ipc64_perm_to_ipc_perm(struct ipc64_perm *in, struct ipc_perm *out);
  149. int ipc_update_perm(struct ipc64_perm *in, struct kern_ipc_perm *out);
  150. struct kern_ipc_perm *ipcctl_obtain_check(struct ipc_namespace *ns,
  151. struct ipc_ids *ids, int id, int cmd,
  152. struct ipc64_perm *perm, int extra_perm);
  153. static inline void ipc_update_pid(struct pid **pos, struct pid *pid)
  154. {
  155. struct pid *old = *pos;
  156. if (old != pid) {
  157. *pos = get_pid(pid);
  158. put_pid(old);
  159. }
  160. }
  161. #ifdef CONFIG_ARCH_WANT_IPC_PARSE_VERSION
  162. int ipc_parse_version(int *cmd);
  163. #endif
  164. extern void free_msg(struct msg_msg *msg);
  165. extern struct msg_msg *load_msg(const void __user *src, size_t len);
  166. extern struct msg_msg *copy_msg(struct msg_msg *src, struct msg_msg *dst);
  167. extern int store_msg(void __user *dest, struct msg_msg *msg, size_t len);
  168. static inline int ipc_checkid(struct kern_ipc_perm *ipcp, int id)
  169. {
  170. return ipcid_to_seqx(id) != ipcp->seq;
  171. }
  172. static inline void ipc_lock_object(struct kern_ipc_perm *perm)
  173. {
  174. spin_lock(&perm->lock);
  175. }
  176. static inline void ipc_unlock_object(struct kern_ipc_perm *perm)
  177. {
  178. spin_unlock(&perm->lock);
  179. }
  180. static inline void ipc_assert_locked_object(struct kern_ipc_perm *perm)
  181. {
  182. assert_spin_locked(&perm->lock);
  183. }
  184. static inline void ipc_unlock(struct kern_ipc_perm *perm)
  185. {
  186. ipc_unlock_object(perm);
  187. rcu_read_unlock();
  188. }
  189. /*
  190. * ipc_valid_object() - helper to sort out IPC_RMID races for codepaths
  191. * where the respective ipc_ids.rwsem is not being held down.
  192. * Checks whether the ipc object is still around or if it's gone already, as
  193. * ipc_rmid() may have already freed the ID while the ipc lock was spinning.
  194. * Needs to be called with kern_ipc_perm.lock held -- exception made for one
  195. * checkpoint case at sys_semtimedop() as noted in code commentary.
  196. */
  197. static inline bool ipc_valid_object(struct kern_ipc_perm *perm)
  198. {
  199. return !perm->deleted;
  200. }
  201. struct kern_ipc_perm *ipc_obtain_object_check(struct ipc_ids *ids, int id);
  202. int ipcget(struct ipc_namespace *ns, struct ipc_ids *ids,
  203. const struct ipc_ops *ops, struct ipc_params *params);
  204. void free_ipcs(struct ipc_namespace *ns, struct ipc_ids *ids,
  205. void (*free)(struct ipc_namespace *, struct kern_ipc_perm *));
  206. static inline int sem_check_semmni(struct ipc_namespace *ns) {
  207. /*
  208. * Check semmni range [0, ipc_mni]
  209. * semmni is the last element of sem_ctls[4] array
  210. */
  211. return ((ns->sem_ctls[3] < 0) || (ns->sem_ctls[3] > ipc_mni))
  212. ? -ERANGE : 0;
  213. }
  214. #ifdef CONFIG_COMPAT
  215. #include <linux/compat.h>
  216. struct compat_ipc_perm {
  217. key_t key;
  218. __compat_uid_t uid;
  219. __compat_gid_t gid;
  220. __compat_uid_t cuid;
  221. __compat_gid_t cgid;
  222. compat_mode_t mode;
  223. unsigned short seq;
  224. };
  225. void to_compat_ipc_perm(struct compat_ipc_perm *, struct ipc64_perm *);
  226. void to_compat_ipc64_perm(struct compat_ipc64_perm *, struct ipc64_perm *);
  227. int get_compat_ipc_perm(struct ipc64_perm *, struct compat_ipc_perm __user *);
  228. int get_compat_ipc64_perm(struct ipc64_perm *,
  229. struct compat_ipc64_perm __user *);
  230. static inline int compat_ipc_parse_version(int *cmd)
  231. {
  232. int version = *cmd & IPC_64;
  233. *cmd &= ~IPC_64;
  234. return version;
  235. }
  236. long compat_ksys_old_semctl(int semid, int semnum, int cmd, int arg);
  237. long compat_ksys_old_msgctl(int msqid, int cmd, void __user *uptr);
  238. long compat_ksys_msgrcv(int msqid, compat_uptr_t msgp, compat_ssize_t msgsz,
  239. compat_long_t msgtyp, int msgflg);
  240. long compat_ksys_msgsnd(int msqid, compat_uptr_t msgp,
  241. compat_ssize_t msgsz, int msgflg);
  242. long compat_ksys_old_shmctl(int shmid, int cmd, void __user *uptr);
  243. #endif
  244. #endif