util.h 8.8 KB

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