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