xref: /OK3568_Linux_fs/kernel/include/linux/ipc_namespace.h (revision 4882a59341e53eb6f0b4789bf948001014eff981)
1*4882a593Smuzhiyun /* SPDX-License-Identifier: GPL-2.0 */
2*4882a593Smuzhiyun #ifndef __IPC_NAMESPACE_H__
3*4882a593Smuzhiyun #define __IPC_NAMESPACE_H__
4*4882a593Smuzhiyun 
5*4882a593Smuzhiyun #include <linux/err.h>
6*4882a593Smuzhiyun #include <linux/idr.h>
7*4882a593Smuzhiyun #include <linux/rwsem.h>
8*4882a593Smuzhiyun #include <linux/notifier.h>
9*4882a593Smuzhiyun #include <linux/nsproxy.h>
10*4882a593Smuzhiyun #include <linux/ns_common.h>
11*4882a593Smuzhiyun #include <linux/refcount.h>
12*4882a593Smuzhiyun #include <linux/rhashtable-types.h>
13*4882a593Smuzhiyun 
14*4882a593Smuzhiyun struct user_namespace;
15*4882a593Smuzhiyun 
16*4882a593Smuzhiyun struct ipc_ids {
17*4882a593Smuzhiyun 	int in_use;
18*4882a593Smuzhiyun 	unsigned short seq;
19*4882a593Smuzhiyun 	struct rw_semaphore rwsem;
20*4882a593Smuzhiyun 	struct idr ipcs_idr;
21*4882a593Smuzhiyun 	int max_idx;
22*4882a593Smuzhiyun 	int last_idx;	/* For wrap around detection */
23*4882a593Smuzhiyun #ifdef CONFIG_CHECKPOINT_RESTORE
24*4882a593Smuzhiyun 	int next_id;
25*4882a593Smuzhiyun #endif
26*4882a593Smuzhiyun 	struct rhashtable key_ht;
27*4882a593Smuzhiyun };
28*4882a593Smuzhiyun 
29*4882a593Smuzhiyun struct ipc_namespace {
30*4882a593Smuzhiyun 	refcount_t	count;
31*4882a593Smuzhiyun 	struct ipc_ids	ids[3];
32*4882a593Smuzhiyun 
33*4882a593Smuzhiyun 	int		sem_ctls[4];
34*4882a593Smuzhiyun 	int		used_sems;
35*4882a593Smuzhiyun 
36*4882a593Smuzhiyun 	unsigned int	msg_ctlmax;
37*4882a593Smuzhiyun 	unsigned int	msg_ctlmnb;
38*4882a593Smuzhiyun 	unsigned int	msg_ctlmni;
39*4882a593Smuzhiyun 	atomic_t	msg_bytes;
40*4882a593Smuzhiyun 	atomic_t	msg_hdrs;
41*4882a593Smuzhiyun 
42*4882a593Smuzhiyun 	size_t		shm_ctlmax;
43*4882a593Smuzhiyun 	size_t		shm_ctlall;
44*4882a593Smuzhiyun 	unsigned long	shm_tot;
45*4882a593Smuzhiyun 	int		shm_ctlmni;
46*4882a593Smuzhiyun 	/*
47*4882a593Smuzhiyun 	 * Defines whether IPC_RMID is forced for _all_ shm segments regardless
48*4882a593Smuzhiyun 	 * of shmctl()
49*4882a593Smuzhiyun 	 */
50*4882a593Smuzhiyun 	int		shm_rmid_forced;
51*4882a593Smuzhiyun 
52*4882a593Smuzhiyun 	struct notifier_block ipcns_nb;
53*4882a593Smuzhiyun 
54*4882a593Smuzhiyun 	/* The kern_mount of the mqueuefs sb.  We take a ref on it */
55*4882a593Smuzhiyun 	struct vfsmount	*mq_mnt;
56*4882a593Smuzhiyun 
57*4882a593Smuzhiyun 	/* # queues in this ns, protected by mq_lock */
58*4882a593Smuzhiyun 	unsigned int    mq_queues_count;
59*4882a593Smuzhiyun 
60*4882a593Smuzhiyun 	/* next fields are set through sysctl */
61*4882a593Smuzhiyun 	unsigned int    mq_queues_max;   /* initialized to DFLT_QUEUESMAX */
62*4882a593Smuzhiyun 	unsigned int    mq_msg_max;      /* initialized to DFLT_MSGMAX */
63*4882a593Smuzhiyun 	unsigned int    mq_msgsize_max;  /* initialized to DFLT_MSGSIZEMAX */
64*4882a593Smuzhiyun 	unsigned int    mq_msg_default;
65*4882a593Smuzhiyun 	unsigned int    mq_msgsize_default;
66*4882a593Smuzhiyun 
67*4882a593Smuzhiyun 	/* user_ns which owns the ipc ns */
68*4882a593Smuzhiyun 	struct user_namespace *user_ns;
69*4882a593Smuzhiyun 	struct ucounts *ucounts;
70*4882a593Smuzhiyun 
71*4882a593Smuzhiyun 	struct llist_node mnt_llist;
72*4882a593Smuzhiyun 
73*4882a593Smuzhiyun 	struct ns_common ns;
74*4882a593Smuzhiyun } __randomize_layout;
75*4882a593Smuzhiyun 
76*4882a593Smuzhiyun extern struct ipc_namespace init_ipc_ns;
77*4882a593Smuzhiyun extern spinlock_t mq_lock;
78*4882a593Smuzhiyun 
79*4882a593Smuzhiyun #ifdef CONFIG_SYSVIPC
80*4882a593Smuzhiyun extern void shm_destroy_orphaned(struct ipc_namespace *ns);
81*4882a593Smuzhiyun #else /* CONFIG_SYSVIPC */
shm_destroy_orphaned(struct ipc_namespace * ns)82*4882a593Smuzhiyun static inline void shm_destroy_orphaned(struct ipc_namespace *ns) {}
83*4882a593Smuzhiyun #endif /* CONFIG_SYSVIPC */
84*4882a593Smuzhiyun 
85*4882a593Smuzhiyun #ifdef CONFIG_POSIX_MQUEUE
86*4882a593Smuzhiyun extern int mq_init_ns(struct ipc_namespace *ns);
87*4882a593Smuzhiyun /*
88*4882a593Smuzhiyun  * POSIX Message Queue default values:
89*4882a593Smuzhiyun  *
90*4882a593Smuzhiyun  * MIN_*: Lowest value an admin can set the maximum unprivileged limit to
91*4882a593Smuzhiyun  * DFLT_*MAX: Default values for the maximum unprivileged limits
92*4882a593Smuzhiyun  * DFLT_{MSG,MSGSIZE}: Default values used when the user doesn't supply
93*4882a593Smuzhiyun  *   an attribute to the open call and the queue must be created
94*4882a593Smuzhiyun  * HARD_*: Highest value the maximums can be set to.  These are enforced
95*4882a593Smuzhiyun  *   on CAP_SYS_RESOURCE apps as well making them inviolate (so make them
96*4882a593Smuzhiyun  *   suitably high)
97*4882a593Smuzhiyun  *
98*4882a593Smuzhiyun  * POSIX Requirements:
99*4882a593Smuzhiyun  *   Per app minimum openable message queues - 8.  This does not map well
100*4882a593Smuzhiyun  *     to the fact that we limit the number of queues on a per namespace
101*4882a593Smuzhiyun  *     basis instead of a per app basis.  So, make the default high enough
102*4882a593Smuzhiyun  *     that no given app should have a hard time opening 8 queues.
103*4882a593Smuzhiyun  *   Minimum maximum for HARD_MSGMAX - 32767.  I bumped this to 65536.
104*4882a593Smuzhiyun  *   Minimum maximum for HARD_MSGSIZEMAX - POSIX is silent on this.  However,
105*4882a593Smuzhiyun  *     we have run into a situation where running applications in the wild
106*4882a593Smuzhiyun  *     require this to be at least 5MB, and preferably 10MB, so I set the
107*4882a593Smuzhiyun  *     value to 16MB in hopes that this user is the worst of the bunch and
108*4882a593Smuzhiyun  *     the new maximum will handle anyone else.  I may have to revisit this
109*4882a593Smuzhiyun  *     in the future.
110*4882a593Smuzhiyun  */
111*4882a593Smuzhiyun #define DFLT_QUEUESMAX		      256
112*4882a593Smuzhiyun #define MIN_MSGMAX			1
113*4882a593Smuzhiyun #define DFLT_MSG		       10U
114*4882a593Smuzhiyun #define DFLT_MSGMAX		       10
115*4882a593Smuzhiyun #define HARD_MSGMAX		    65536
116*4882a593Smuzhiyun #define MIN_MSGSIZEMAX		      128
117*4882a593Smuzhiyun #define DFLT_MSGSIZE		     8192U
118*4882a593Smuzhiyun #define DFLT_MSGSIZEMAX		     8192
119*4882a593Smuzhiyun #define HARD_MSGSIZEMAX	    (16*1024*1024)
120*4882a593Smuzhiyun #else
mq_init_ns(struct ipc_namespace * ns)121*4882a593Smuzhiyun static inline int mq_init_ns(struct ipc_namespace *ns) { return 0; }
122*4882a593Smuzhiyun #endif
123*4882a593Smuzhiyun 
124*4882a593Smuzhiyun #if defined(CONFIG_IPC_NS)
125*4882a593Smuzhiyun extern struct ipc_namespace *copy_ipcs(unsigned long flags,
126*4882a593Smuzhiyun 	struct user_namespace *user_ns, struct ipc_namespace *ns);
127*4882a593Smuzhiyun 
get_ipc_ns(struct ipc_namespace * ns)128*4882a593Smuzhiyun static inline struct ipc_namespace *get_ipc_ns(struct ipc_namespace *ns)
129*4882a593Smuzhiyun {
130*4882a593Smuzhiyun 	if (ns)
131*4882a593Smuzhiyun 		refcount_inc(&ns->count);
132*4882a593Smuzhiyun 	return ns;
133*4882a593Smuzhiyun }
134*4882a593Smuzhiyun 
get_ipc_ns_not_zero(struct ipc_namespace * ns)135*4882a593Smuzhiyun static inline struct ipc_namespace *get_ipc_ns_not_zero(struct ipc_namespace *ns)
136*4882a593Smuzhiyun {
137*4882a593Smuzhiyun 	if (ns) {
138*4882a593Smuzhiyun 		if (refcount_inc_not_zero(&ns->count))
139*4882a593Smuzhiyun 			return ns;
140*4882a593Smuzhiyun 	}
141*4882a593Smuzhiyun 
142*4882a593Smuzhiyun 	return NULL;
143*4882a593Smuzhiyun }
144*4882a593Smuzhiyun 
145*4882a593Smuzhiyun extern void put_ipc_ns(struct ipc_namespace *ns);
146*4882a593Smuzhiyun #else
copy_ipcs(unsigned long flags,struct user_namespace * user_ns,struct ipc_namespace * ns)147*4882a593Smuzhiyun static inline struct ipc_namespace *copy_ipcs(unsigned long flags,
148*4882a593Smuzhiyun 	struct user_namespace *user_ns, struct ipc_namespace *ns)
149*4882a593Smuzhiyun {
150*4882a593Smuzhiyun 	if (flags & CLONE_NEWIPC)
151*4882a593Smuzhiyun 		return ERR_PTR(-EINVAL);
152*4882a593Smuzhiyun 
153*4882a593Smuzhiyun 	return ns;
154*4882a593Smuzhiyun }
155*4882a593Smuzhiyun 
get_ipc_ns(struct ipc_namespace * ns)156*4882a593Smuzhiyun static inline struct ipc_namespace *get_ipc_ns(struct ipc_namespace *ns)
157*4882a593Smuzhiyun {
158*4882a593Smuzhiyun 	return ns;
159*4882a593Smuzhiyun }
160*4882a593Smuzhiyun 
get_ipc_ns_not_zero(struct ipc_namespace * ns)161*4882a593Smuzhiyun static inline struct ipc_namespace *get_ipc_ns_not_zero(struct ipc_namespace *ns)
162*4882a593Smuzhiyun {
163*4882a593Smuzhiyun 	return ns;
164*4882a593Smuzhiyun }
165*4882a593Smuzhiyun 
put_ipc_ns(struct ipc_namespace * ns)166*4882a593Smuzhiyun static inline void put_ipc_ns(struct ipc_namespace *ns)
167*4882a593Smuzhiyun {
168*4882a593Smuzhiyun }
169*4882a593Smuzhiyun #endif
170*4882a593Smuzhiyun 
171*4882a593Smuzhiyun #ifdef CONFIG_POSIX_MQUEUE_SYSCTL
172*4882a593Smuzhiyun 
173*4882a593Smuzhiyun struct ctl_table_header;
174*4882a593Smuzhiyun extern struct ctl_table_header *mq_register_sysctl_table(void);
175*4882a593Smuzhiyun 
176*4882a593Smuzhiyun #else /* CONFIG_POSIX_MQUEUE_SYSCTL */
177*4882a593Smuzhiyun 
mq_register_sysctl_table(void)178*4882a593Smuzhiyun static inline struct ctl_table_header *mq_register_sysctl_table(void)
179*4882a593Smuzhiyun {
180*4882a593Smuzhiyun 	return NULL;
181*4882a593Smuzhiyun }
182*4882a593Smuzhiyun 
183*4882a593Smuzhiyun #endif /* CONFIG_POSIX_MQUEUE_SYSCTL */
184*4882a593Smuzhiyun #endif
185