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