util.h 8.8 KB

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