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)
132static inline struct ipc_namespace *to_ipc_ns(struct ns_common *ns)
133{
134 return container_of(ns, struct ipc_namespace, ns);
135}
136
137extern struct ipc_namespace *copy_ipcs(u64 flags,
138 struct user_namespace *user_ns, struct ipc_namespace *ns);
139
140static inline struct ipc_namespace *get_ipc_ns(struct ipc_namespace *ns)
141{
142 if (ns)
143 ns_ref_inc(ns);
144 return ns;
145}
146
147static inline struct ipc_namespace *get_ipc_ns_not_zero(struct ipc_namespace *ns)
148{
149 if (ns) {
150 if (ns_ref_get(ns))
151 return ns;
152 }
153
154 return NULL;
155}
156
157extern void put_ipc_ns(struct ipc_namespace *ns);
158#else
159static inline struct ipc_namespace *copy_ipcs(u64 flags,
160 struct user_namespace *user_ns, struct ipc_namespace *ns)
161{
162 if (flags & CLONE_NEWIPC)
163 return ERR_PTR(-EINVAL);
164
165 return ns;
166}
167
168static inline struct ipc_namespace *get_ipc_ns(struct ipc_namespace *ns)
169{
170 return ns;
171}
172
173static inline struct ipc_namespace *get_ipc_ns_not_zero(struct ipc_namespace *ns)
174{
175 return ns;
176}
177
178static inline void put_ipc_ns(struct ipc_namespace *ns)
179{
180}
181#endif
182
183#ifdef CONFIG_POSIX_MQUEUE_SYSCTL
184
185void retire_mq_sysctls(struct ipc_namespace *ns);
186bool setup_mq_sysctls(struct ipc_namespace *ns);
187
188#else /* CONFIG_POSIX_MQUEUE_SYSCTL */
189
190static inline void retire_mq_sysctls(struct ipc_namespace *ns)
191{
192}
193
194static inline bool setup_mq_sysctls(struct ipc_namespace *ns)
195{
196 return true;
197}
198
199#endif /* CONFIG_POSIX_MQUEUE_SYSCTL */
200
201#ifdef CONFIG_SYSVIPC_SYSCTL
202
203bool setup_ipc_sysctls(struct ipc_namespace *ns);
204void retire_ipc_sysctls(struct ipc_namespace *ns);
205
206#else /* CONFIG_SYSVIPC_SYSCTL */
207
208static inline void retire_ipc_sysctls(struct ipc_namespace *ns)
209{
210}
211
212static inline bool setup_ipc_sysctls(struct ipc_namespace *ns)
213{
214 return true;
215}
216
217#endif /* CONFIG_SYSVIPC_SYSCTL */
218#endif
219