util.h 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293
  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 <[email protected]>
  7. * namespaces support. 2006 OpenVZ, SWsoft Inc.
  8. * Pavel Emelianov <[email protected]>
  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. int 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 int msg_init_ns(struct ipc_namespace *ns) { return 0; }
  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. * The function returns the highest assigned index for @ids. The function
  125. * doesn't scan the idr tree, it uses a cached value.
  126. *
  127. * Called with ipc_ids.rwsem held for reading.
  128. */
  129. static inline int ipc_get_maxidx(struct ipc_ids *ids)
  130. {
  131. if (ids->in_use == 0)
  132. return -1;
  133. if (ids->in_use == ipc_mni)
  134. return ipc_mni - 1;
  135. return ids->max_idx;
  136. }
  137. /*
  138. * For allocation that need to be freed by RCU.
  139. * Objects are reference counted, they start with reference count 1.
  140. * getref increases the refcount, the putref call that reduces the recount
  141. * to 0 schedules the rcu destruction. Caller must guarantee locking.
  142. *
  143. * refcount is initialized by ipc_addid(), before that point call_rcu()
  144. * must be used.
  145. */
  146. bool ipc_rcu_getref(struct kern_ipc_perm *ptr);
  147. void ipc_rcu_putref(struct kern_ipc_perm *ptr,
  148. void (*func)(struct rcu_head *head));
  149. struct kern_ipc_perm *ipc_obtain_object_idr(struct ipc_ids *ids, int id);
  150. void kernel_to_ipc64_perm(struct kern_ipc_perm *in, struct ipc64_perm *out);
  151. void ipc64_perm_to_ipc_perm(struct ipc64_perm *in, struct ipc_perm *out);
  152. int ipc_update_perm(struct ipc64_perm *in, struct kern_ipc_perm *out);
  153. struct kern_ipc_perm *ipcctl_obtain_check(struct ipc_namespace *ns,
  154. struct ipc_ids *ids, int id, int cmd,
  155. struct ipc64_perm *perm, int extra_perm);
  156. static inline void ipc_update_pid(struct pid **pos, struct pid *pid)
  157. {
  158. struct pid *old = *pos;
  159. if (old != pid) {
  160. *pos = get_pid(pid);
  161. put_pid(old);
  162. }
  163. }
  164. #ifdef CONFIG_ARCH_WANT_IPC_PARSE_VERSION
  165. int ipc_parse_version(int *cmd);
  166. #endif
  167. extern void free_msg(struct msg_msg *msg);
  168. extern struct msg_msg *load_msg(const void __user *src, size_t len);
  169. extern struct msg_msg *copy_msg(struct msg_msg *src, struct msg_msg *dst);
  170. extern int store_msg(void __user *dest, struct msg_msg *msg, size_t len);
  171. static inline int ipc_checkid(struct kern_ipc_perm *ipcp, int id)
  172. {
  173. return ipcid_to_seqx(id) != ipcp->seq;
  174. }
  175. static inline void ipc_lock_object(struct kern_ipc_perm *perm)
  176. {
  177. spin_lock(&perm->lock);
  178. }
  179. static inline void ipc_unlock_object(struct kern_ipc_perm *perm)
  180. {
  181. spin_unlock(&perm->lock);
  182. }
  183. static inline void ipc_assert_locked_object(struct kern_ipc_perm *perm)
  184. {
  185. assert_spin_locked(&perm->lock);
  186. }
  187. static inline void ipc_unlock(struct kern_ipc_perm *perm)
  188. {
  189. ipc_unlock_object(perm);
  190. rcu_read_unlock();
  191. }
  192. /*
  193. * ipc_valid_object() - helper to sort out IPC_RMID races for codepaths
  194. * where the respective ipc_ids.rwsem is not being held down.
  195. * Checks whether the ipc object is still around or if it's gone already, as
  196. * ipc_rmid() may have already freed the ID while the ipc lock was spinning.
  197. * Needs to be called with kern_ipc_perm.lock held -- exception made for one
  198. * checkpoint case at sys_semtimedop() as noted in code commentary.
  199. */
  200. static inline bool ipc_valid_object(struct kern_ipc_perm *perm)
  201. {
  202. return !perm->deleted;
  203. }
  204. struct kern_ipc_perm *ipc_obtain_object_check(struct ipc_ids *ids, int id);
  205. int ipcget(struct ipc_namespace *ns, struct ipc_ids *ids,
  206. const struct ipc_ops *ops, struct ipc_params *params);
  207. void free_ipcs(struct ipc_namespace *ns, struct ipc_ids *ids,
  208. void (*free)(struct ipc_namespace *, struct kern_ipc_perm *));
  209. static inline int sem_check_semmni(struct ipc_namespace *ns) {
  210. /*
  211. * Check semmni range [0, ipc_mni]
  212. * semmni is the last element of sem_ctls[4] array
  213. */
  214. return ((ns->sem_ctls[3] < 0) || (ns->sem_ctls[3] > ipc_mni))
  215. ? -ERANGE : 0;
  216. }
  217. #ifdef CONFIG_COMPAT
  218. #include <linux/compat.h>
  219. struct compat_ipc_perm {
  220. key_t key;
  221. __compat_uid_t uid;
  222. __compat_gid_t gid;
  223. __compat_uid_t cuid;
  224. __compat_gid_t cgid;
  225. compat_mode_t mode;
  226. unsigned short seq;
  227. };
  228. void to_compat_ipc_perm(struct compat_ipc_perm *, struct ipc64_perm *);
  229. void to_compat_ipc64_perm(struct compat_ipc64_perm *, struct ipc64_perm *);
  230. int get_compat_ipc_perm(struct ipc64_perm *, struct compat_ipc_perm __user *);
  231. int get_compat_ipc64_perm(struct ipc64_perm *,
  232. struct compat_ipc64_perm __user *);
  233. static inline int compat_ipc_parse_version(int *cmd)
  234. {
  235. int version = *cmd & IPC_64;
  236. *cmd &= ~IPC_64;
  237. return version;
  238. }
  239. long compat_ksys_old_semctl(int semid, int semnum, int cmd, int arg);
  240. long compat_ksys_old_msgctl(int msqid, int cmd, void __user *uptr);
  241. long compat_ksys_msgrcv(int msqid, compat_uptr_t msgp, compat_ssize_t msgsz,
  242. compat_long_t msgtyp, int msgflg);
  243. long compat_ksys_msgsnd(int msqid, compat_uptr_t msgp,
  244. compat_ssize_t msgsz, int msgflg);
  245. long compat_ksys_old_shmctl(int shmid, int cmd, void __user *uptr);
  246. #endif
  247. #endif