Merge tag 'fs.xattr.simple.noaudit.v6.2' of git://git.kernel.org/pub/scm/linux/kernel...
[linux-block.git] / include / linux / ipc_namespace.h
CommitLineData
b2441318 1/* SPDX-License-Identifier: GPL-2.0 */
ae5e1b22
PE
2#ifndef __IPC_NAMESPACE_H__
3#define __IPC_NAMESPACE_H__
4
5#include <linux/err.h>
ed2ddbf8
PP
6#include <linux/idr.h>
7#include <linux/rwsem.h>
b6b337ad 8#include <linux/notifier.h>
b0e77598 9#include <linux/nsproxy.h>
435d5f4b 10#include <linux/ns_common.h>
a2e0602c 11#include <linux/refcount.h>
0eb71a9d 12#include <linux/rhashtable-types.h>
dc55e35f 13#include <linux/sysctl.h>
72d1e611 14#include <linux/percpu_counter.h>
b6b337ad 15
b515498f 16struct user_namespace;
ed2ddbf8
PP
17
18struct ipc_ids {
19 int in_use;
20 unsigned short seq;
d9a605e4 21 struct rw_semaphore rwsem;
ed2ddbf8 22 struct idr ipcs_idr;
27c331a1 23 int max_idx;
3278a2c2 24 int last_idx; /* For wrap around detection */
b8fd9983 25#ifdef CONFIG_CHECKPOINT_RESTORE
03f59566 26 int next_id;
b8fd9983 27#endif
0cfb6aee 28 struct rhashtable key_ht;
ed2ddbf8 29};
ae5e1b22 30
ae5e1b22 31struct ipc_namespace {
ed2ddbf8 32 struct ipc_ids ids[3];
ae5e1b22
PE
33
34 int sem_ctls[4];
35 int used_sems;
36
9bf76ca3
MK
37 unsigned int msg_ctlmax;
38 unsigned int msg_ctlmnb;
39 unsigned int msg_ctlmni;
72d1e611
JS
40 struct percpu_counter percpu_msg_bytes;
41 struct percpu_counter percpu_msg_hdrs;
ae5e1b22
PE
42
43 size_t shm_ctlmax;
44 size_t shm_ctlall;
d69f3bad 45 unsigned long shm_tot;
ae5e1b22 46 int shm_ctlmni;
b34a6b1d
VK
47 /*
48 * Defines whether IPC_RMID is forced for _all_ shm segments regardless
49 * of shmctl()
50 */
51 int shm_rmid_forced;
b6b337ad 52
b6b337ad 53 struct notifier_block ipcns_nb;
614b84cf
SH
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 */
cef0184c
KM
65 unsigned int mq_msg_default;
66 unsigned int mq_msgsize_default;
614b84cf 67
dc55e35f
AG
68 struct ctl_table_set mq_set;
69 struct ctl_table_header *mq_sysctls;
70
1f5c135e
AG
71 struct ctl_table_set ipc_set;
72 struct ctl_table_header *ipc_sysctls;
73
b515498f
SH
74 /* user_ns which owns the ipc ns */
75 struct user_namespace *user_ns;
aba35661 76 struct ucounts *ucounts;
98f842e6 77
e1eb26fa
GS
78 struct llist_node mnt_llist;
79
435d5f4b 80 struct ns_common ns;
3859a271 81} __randomize_layout;
ae5e1b22
PE
82
83extern struct ipc_namespace init_ipc_ns;
7eafd7c7 84extern spinlock_t mq_lock;
b6b337ad 85
614b84cf 86#ifdef CONFIG_SYSVIPC
b34a6b1d 87extern void shm_destroy_orphaned(struct ipc_namespace *ns);
b6b337ad 88#else /* CONFIG_SYSVIPC */
b34a6b1d 89static inline void shm_destroy_orphaned(struct ipc_namespace *ns) {}
b6b337ad 90#endif /* CONFIG_SYSVIPC */
ae5e1b22 91
614b84cf 92#ifdef CONFIG_POSIX_MQUEUE
7eafd7c7 93extern int mq_init_ns(struct ipc_namespace *ns);
5b5c4d1a
DL
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 */
5b5c4d1a 118#define DFLT_QUEUESMAX 256
5b5c4d1a 119#define MIN_MSGMAX 1
e6315bb1
KM
120#define DFLT_MSG 10U
121#define DFLT_MSGMAX 10
5b5c4d1a
DL
122#define HARD_MSGMAX 65536
123#define MIN_MSGSIZEMAX 128
124#define DFLT_MSGSIZE 8192U
e6315bb1 125#define DFLT_MSGSIZEMAX 8192
5b5c4d1a 126#define HARD_MSGSIZEMAX (16*1024*1024)
614b84cf 127#else
7eafd7c7 128static inline int mq_init_ns(struct ipc_namespace *ns) { return 0; }
614b84cf
SH
129#endif
130
131#if defined(CONFIG_IPC_NS)
ae5e1b22 132extern struct ipc_namespace *copy_ipcs(unsigned long flags,
bcf58e72
EB
133 struct user_namespace *user_ns, struct ipc_namespace *ns);
134
ae5e1b22
PE
135static inline struct ipc_namespace *get_ipc_ns(struct ipc_namespace *ns)
136{
137 if (ns)
137ec390 138 refcount_inc(&ns->ns.count);
ae5e1b22
PE
139 return ns;
140}
141
85b6d246
AM
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
7eafd7c7 152extern void put_ipc_ns(struct ipc_namespace *ns);
ae5e1b22
PE
153#else
154static inline struct ipc_namespace *copy_ipcs(unsigned long flags,
bcf58e72 155 struct user_namespace *user_ns, struct ipc_namespace *ns)
ae5e1b22
PE
156{
157 if (flags & CLONE_NEWIPC)
158 return ERR_PTR(-EINVAL);
159
bcf58e72 160 return ns;
ae5e1b22
PE
161}
162
163static inline struct ipc_namespace *get_ipc_ns(struct ipc_namespace *ns)
164{
165 return ns;
166}
167
85b6d246
AM
168static inline struct ipc_namespace *get_ipc_ns_not_zero(struct ipc_namespace *ns)
169{
170 return ns;
171}
172
ae5e1b22
PE
173static inline void put_ipc_ns(struct ipc_namespace *ns)
174{
175}
176#endif
bdc8e5f8
SH
177
178#ifdef CONFIG_POSIX_MQUEUE_SYSCTL
179
dc55e35f
AG
180void retire_mq_sysctls(struct ipc_namespace *ns);
181bool setup_mq_sysctls(struct ipc_namespace *ns);
bdc8e5f8
SH
182
183#else /* CONFIG_POSIX_MQUEUE_SYSCTL */
184
dc55e35f 185static inline void retire_mq_sysctls(struct ipc_namespace *ns)
bdc8e5f8 186{
dc55e35f
AG
187}
188
189static inline bool setup_mq_sysctls(struct ipc_namespace *ns)
190{
191 return true;
bdc8e5f8
SH
192}
193
194#endif /* CONFIG_POSIX_MQUEUE_SYSCTL */
1f5c135e
AG
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 */
ae5e1b22 213#endif