util.h 7.2 KB

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