1b2441318SGreg Kroah-Hartman/* SPDX-License-Identifier: GPL-2.0 */
2ae5e1b22SPavel Emelyanov#ifndef __IPC_NAMESPACE_H__
3ae5e1b22SPavel Emelyanov#define __IPC_NAMESPACE_H__
4ae5e1b22SPavel Emelyanov
5ae5e1b22SPavel Emelyanov#include <linux/err.h>
6ed2ddbf8SPierre Peiffer#include <linux/idr.h>
7ed2ddbf8SPierre Peiffer#include <linux/rwsem.h>
8b6b337adSNadia Derbey#include <linux/notifier.h>
9b0e77598SSerge E. Hallyn#include <linux/nsproxy.h>
10435d5f4bSAl Viro#include <linux/ns_common.h>
11a2e0602cSElena Reshetova#include <linux/refcount.h>
120eb71a9dSNeilBrown#include <linux/rhashtable-types.h>
13b6b337adSNadia Derbey
14b515498fSSerge E. Hallynstruct user_namespace;
15ed2ddbf8SPierre Peiffer
16ed2ddbf8SPierre Peifferstruct ipc_ids {
17ed2ddbf8SPierre Peiffer	int in_use;
18ed2ddbf8SPierre Peiffer	unsigned short seq;
19d9a605e4SDavidlohr Bueso	struct rw_semaphore rwsem;
20ed2ddbf8SPierre Peiffer	struct idr ipcs_idr;
2127c331a1SManfred Spraul	int max_idx;
223278a2c2SManfred Spraul	int last_idx;	/* For wrap around detection */
23b8fd9983SDavidlohr Bueso#ifdef CONFIG_CHECKPOINT_RESTORE
2403f59566SStanislav Kinsbursky	int next_id;
25b8fd9983SDavidlohr Bueso#endif
260cfb6aeeSGuillaume Knispel	struct rhashtable key_ht;
27ed2ddbf8SPierre Peiffer};
28ae5e1b22SPavel Emelyanov
29ae5e1b22SPavel Emelyanovstruct ipc_namespace {
30ed2ddbf8SPierre Peiffer	struct ipc_ids	ids[3];
31ae5e1b22SPavel Emelyanov
32ae5e1b22SPavel Emelyanov	int		sem_ctls[4];
33ae5e1b22SPavel Emelyanov	int		used_sems;
34ae5e1b22SPavel Emelyanov
359bf76ca3SMathias Krause	unsigned int	msg_ctlmax;
369bf76ca3SMathias Krause	unsigned int	msg_ctlmnb;
379bf76ca3SMathias Krause	unsigned int	msg_ctlmni;
38ae5e1b22SPavel Emelyanov	atomic_t	msg_bytes;
39ae5e1b22SPavel Emelyanov	atomic_t	msg_hdrs;
40ae5e1b22SPavel Emelyanov
41ae5e1b22SPavel Emelyanov	size_t		shm_ctlmax;
42ae5e1b22SPavel Emelyanov	size_t		shm_ctlall;
43d69f3badSRobin Holt	unsigned long	shm_tot;
44ae5e1b22SPavel Emelyanov	int		shm_ctlmni;
45b34a6b1dSVasiliy Kulikov	/*
46b34a6b1dSVasiliy Kulikov	 * Defines whether IPC_RMID is forced for _all_ shm segments regardless
47b34a6b1dSVasiliy Kulikov	 * of shmctl()
48b34a6b1dSVasiliy Kulikov	 */
49b34a6b1dSVasiliy Kulikov	int		shm_rmid_forced;
50b6b337adSNadia Derbey
51b6b337adSNadia Derbey	struct notifier_block ipcns_nb;
52614b84cfSSerge E. Hallyn
53614b84cfSSerge E. Hallyn	/* The kern_mount of the mqueuefs sb.  We take a ref on it */
54614b84cfSSerge E. Hallyn	struct vfsmount	*mq_mnt;
55614b84cfSSerge E. Hallyn
56614b84cfSSerge E. Hallyn	/* # queues in this ns, protected by mq_lock */
57614b84cfSSerge E. Hallyn	unsigned int    mq_queues_count;
58614b84cfSSerge E. Hallyn
59614b84cfSSerge E. Hallyn	/* next fields are set through sysctl */
60614b84cfSSerge E. Hallyn	unsigned int    mq_queues_max;   /* initialized to DFLT_QUEUESMAX */
61614b84cfSSerge E. Hallyn	unsigned int    mq_msg_max;      /* initialized to DFLT_MSGMAX */
62614b84cfSSerge E. Hallyn	unsigned int    mq_msgsize_max;  /* initialized to DFLT_MSGSIZEMAX */
63cef0184cSKOSAKI Motohiro	unsigned int    mq_msg_default;
64cef0184cSKOSAKI Motohiro	unsigned int    mq_msgsize_default;
65614b84cfSSerge E. Hallyn
66b515498fSSerge E. Hallyn	/* user_ns which owns the ipc ns */
67b515498fSSerge E. Hallyn	struct user_namespace *user_ns;
68aba35661SEric W. Biederman	struct ucounts *ucounts;
6998f842e6SEric W. Biederman
70e1eb26faSGiuseppe Scrivano	struct llist_node mnt_llist;
71e1eb26faSGiuseppe Scrivano
72435d5f4bSAl Viro	struct ns_common ns;
733859a271SKees Cook} __randomize_layout;
74ae5e1b22SPavel Emelyanov
75ae5e1b22SPavel Emelyanovextern struct ipc_namespace init_ipc_ns;
767eafd7c7SSerge E. Hallynextern spinlock_t mq_lock;
77b6b337adSNadia Derbey
78614b84cfSSerge E. Hallyn#ifdef CONFIG_SYSVIPC
79b34a6b1dSVasiliy Kulikovextern void shm_destroy_orphaned(struct ipc_namespace *ns);
80b6b337adSNadia Derbey#else /* CONFIG_SYSVIPC */
81b34a6b1dSVasiliy Kulikovstatic inline void shm_destroy_orphaned(struct ipc_namespace *ns) {}
82b6b337adSNadia Derbey#endif /* CONFIG_SYSVIPC */
83ae5e1b22SPavel Emelyanov
84614b84cfSSerge E. Hallyn#ifdef CONFIG_POSIX_MQUEUE
857eafd7c7SSerge E. Hallynextern int mq_init_ns(struct ipc_namespace *ns);
865b5c4d1aSDoug Ledford/*
875b5c4d1aSDoug Ledford * POSIX Message Queue default values:
885b5c4d1aSDoug Ledford *
895b5c4d1aSDoug Ledford * MIN_*: Lowest value an admin can set the maximum unprivileged limit to
905b5c4d1aSDoug Ledford * DFLT_*MAX: Default values for the maximum unprivileged limits
915b5c4d1aSDoug Ledford * DFLT_{MSG,MSGSIZE}: Default values used when the user doesn't supply
925b5c4d1aSDoug Ledford *   an attribute to the open call and the queue must be created
935b5c4d1aSDoug Ledford * HARD_*: Highest value the maximums can be set to.  These are enforced
945b5c4d1aSDoug Ledford *   on CAP_SYS_RESOURCE apps as well making them inviolate (so make them
955b5c4d1aSDoug Ledford *   suitably high)
965b5c4d1aSDoug Ledford *
975b5c4d1aSDoug Ledford * POSIX Requirements:
985b5c4d1aSDoug Ledford *   Per app minimum openable message queues - 8.  This does not map well
995b5c4d1aSDoug Ledford *     to the fact that we limit the number of queues on a per namespace
1005b5c4d1aSDoug Ledford *     basis instead of a per app basis.  So, make the default high enough
1015b5c4d1aSDoug Ledford *     that no given app should have a hard time opening 8 queues.
1025b5c4d1aSDoug Ledford *   Minimum maximum for HARD_MSGMAX - 32767.  I bumped this to 65536.
1035b5c4d1aSDoug Ledford *   Minimum maximum for HARD_MSGSIZEMAX - POSIX is silent on this.  However,
1045b5c4d1aSDoug Ledford *     we have run into a situation where running applications in the wild
1055b5c4d1aSDoug Ledford *     require this to be at least 5MB, and preferably 10MB, so I set the
1065b5c4d1aSDoug Ledford *     value to 16MB in hopes that this user is the worst of the bunch and
1075b5c4d1aSDoug Ledford *     the new maximum will handle anyone else.  I may have to revisit this
1085b5c4d1aSDoug Ledford *     in the future.
1095b5c4d1aSDoug Ledford */
1105b5c4d1aSDoug Ledford#define DFLT_QUEUESMAX		      256
1115b5c4d1aSDoug Ledford#define MIN_MSGMAX			1
112e6315bb1SKOSAKI Motohiro#define DFLT_MSG		       10U
113e6315bb1SKOSAKI Motohiro#define DFLT_MSGMAX		       10
1145b5c4d1aSDoug Ledford#define HARD_MSGMAX		    65536
1155b5c4d1aSDoug Ledford#define MIN_MSGSIZEMAX		      128
1165b5c4d1aSDoug Ledford#define DFLT_MSGSIZE		     8192U
117e6315bb1SKOSAKI Motohiro#define DFLT_MSGSIZEMAX		     8192
1185b5c4d1aSDoug Ledford#define HARD_MSGSIZEMAX	    (16*1024*1024)
119614b84cfSSerge E. Hallyn#else
1207eafd7c7SSerge E. Hallynstatic inline int mq_init_ns(struct ipc_namespace *ns) { return 0; }
121614b84cfSSerge E. Hallyn#endif
122614b84cfSSerge E. Hallyn
123614b84cfSSerge E. Hallyn#if defined(CONFIG_IPC_NS)
124ae5e1b22SPavel Emelyanovextern struct ipc_namespace *copy_ipcs(unsigned long flags,
125bcf58e72SEric W. Biederman	struct user_namespace *user_ns, struct ipc_namespace *ns);
126bcf58e72SEric W. Biederman
127ae5e1b22SPavel Emelyanovstatic inline struct ipc_namespace *get_ipc_ns(struct ipc_namespace *ns)
128ae5e1b22SPavel Emelyanov{
129ae5e1b22SPavel Emelyanov	if (ns)
130137ec390SKirill Tkhai		refcount_inc(&ns->ns.count);
131ae5e1b22SPavel Emelyanov	return ns;
132ae5e1b22SPavel Emelyanov}
133ae5e1b22SPavel Emelyanov
13485b6d246SAlexander Mikhalitsynstatic inline struct ipc_namespace *get_ipc_ns_not_zero(struct ipc_namespace *ns)
13585b6d246SAlexander Mikhalitsyn{
13685b6d246SAlexander Mikhalitsyn	if (ns) {
13785b6d246SAlexander Mikhalitsyn		if (refcount_inc_not_zero(&ns->ns.count))
13885b6d246SAlexander Mikhalitsyn			return ns;
13985b6d246SAlexander Mikhalitsyn	}
14085b6d246SAlexander Mikhalitsyn
14185b6d246SAlexander Mikhalitsyn	return NULL;
14285b6d246SAlexander Mikhalitsyn}
14385b6d246SAlexander Mikhalitsyn
1447eafd7c7SSerge E. Hallynextern void put_ipc_ns(struct ipc_namespace *ns);
145ae5e1b22SPavel Emelyanov#else
146ae5e1b22SPavel Emelyanovstatic inline struct ipc_namespace *copy_ipcs(unsigned long flags,
147bcf58e72SEric W. Biederman	struct user_namespace *user_ns, struct ipc_namespace *ns)
148ae5e1b22SPavel Emelyanov{
149ae5e1b22SPavel Emelyanov	if (flags & CLONE_NEWIPC)
150ae5e1b22SPavel Emelyanov		return ERR_PTR(-EINVAL);
151ae5e1b22SPavel Emelyanov
152bcf58e72SEric W. Biederman	return ns;
153ae5e1b22SPavel Emelyanov}
154ae5e1b22SPavel Emelyanov
155ae5e1b22SPavel Emelyanovstatic inline struct ipc_namespace *get_ipc_ns(struct ipc_namespace *ns)
156ae5e1b22SPavel Emelyanov{
157ae5e1b22SPavel Emelyanov	return ns;
158ae5e1b22SPavel Emelyanov}
159ae5e1b22SPavel Emelyanov
16085b6d246SAlexander Mikhalitsynstatic inline struct ipc_namespace *get_ipc_ns_not_zero(struct ipc_namespace *ns)
16185b6d246SAlexander Mikhalitsyn{
16285b6d246SAlexander Mikhalitsyn	return ns;
16385b6d246SAlexander Mikhalitsyn}
16485b6d246SAlexander Mikhalitsyn
165ae5e1b22SPavel Emelyanovstatic inline void put_ipc_ns(struct ipc_namespace *ns)
166ae5e1b22SPavel Emelyanov{
167ae5e1b22SPavel Emelyanov}
168ae5e1b22SPavel Emelyanov#endif
169bdc8e5f8SSerge E. Hallyn
170bdc8e5f8SSerge E. Hallyn#ifdef CONFIG_POSIX_MQUEUE_SYSCTL
171bdc8e5f8SSerge E. Hallyn
172bdc8e5f8SSerge E. Hallynstruct ctl_table_header;
173bdc8e5f8SSerge E. Hallynextern struct ctl_table_header *mq_register_sysctl_table(void);
174bdc8e5f8SSerge E. Hallyn
175bdc8e5f8SSerge E. Hallyn#else /* CONFIG_POSIX_MQUEUE_SYSCTL */
176bdc8e5f8SSerge E. Hallyn
177bdc8e5f8SSerge E. Hallynstatic inline struct ctl_table_header *mq_register_sysctl_table(void)
178bdc8e5f8SSerge E. Hallyn{
179bdc8e5f8SSerge E. Hallyn	return NULL;
180bdc8e5f8SSerge E. Hallyn}
181bdc8e5f8SSerge E. Hallyn
182bdc8e5f8SSerge E. Hallyn#endif /* CONFIG_POSIX_MQUEUE_SYSCTL */
183ae5e1b22SPavel Emelyanov#endif