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