xref: /freebsd-12.1/sys/sys/jail.h (revision b19d66fd)
1 /*-
2  * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
3  *
4  * Copyright (c) 1999 Poul-Henning Kamp.
5  * Copyright (c) 2009 James Gritton.
6  * All rights reserved.
7  *
8  * Redistribution and use in source and binary forms, with or without
9  * modification, are permitted provided that the following conditions
10  * are met:
11  * 1. Redistributions of source code must retain the above copyright
12  *    notice, this list of conditions and the following disclaimer.
13  * 2. Redistributions in binary form must reproduce the above copyright
14  *    notice, this list of conditions and the following disclaimer in the
15  *    documentation and/or other materials provided with the distribution.
16  *
17  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
18  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
21  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27  * SUCH DAMAGE.
28  *
29  * $FreeBSD$
30  */
31 
32 #ifndef _SYS_JAIL_H_
33 #define _SYS_JAIL_H_
34 
35 #ifdef _KERNEL
36 struct jail_v0 {
37 	u_int32_t	version;
38 	char		*path;
39 	char		*hostname;
40 	u_int32_t	ip_number;
41 };
42 #endif
43 
44 struct jail {
45 	uint32_t	version;
46 	char		*path;
47 	char		*hostname;
48 	char		*jailname;
49 	uint32_t	ip4s;
50 	uint32_t	ip6s;
51 	struct in_addr	*ip4;
52 	struct in6_addr	*ip6;
53 };
54 #define	JAIL_API_VERSION	2
55 
56 /*
57  * For all xprison structs, always keep the pr_version an int and
58  * the first variable so userspace can easily distinguish them.
59  */
60 #ifndef _KERNEL
61 struct xprison_v1 {
62 	int		 pr_version;
63 	int		 pr_id;
64 	char		 pr_path[MAXPATHLEN];
65 	char		 pr_host[MAXHOSTNAMELEN];
66 	u_int32_t	 pr_ip;
67 };
68 #endif
69 
70 struct xprison {
71 	int		 pr_version;
72 	int		 pr_id;
73 	int		 pr_state;
74 	cpusetid_t	 pr_cpusetid;
75 	char		 pr_path[MAXPATHLEN];
76 	char		 pr_host[MAXHOSTNAMELEN];
77 	char		 pr_name[MAXHOSTNAMELEN];
78 	uint32_t	 pr_ip4s;
79 	uint32_t	 pr_ip6s;
80 #if 0
81 	/*
82 	 * sizeof(xprison) will be malloced + size needed for all
83 	 * IPv4 and IPv6 addesses. Offsets are based numbers of addresses.
84 	 */
85 	struct in_addr	 pr_ip4[];
86 	struct in6_addr	 pr_ip6[];
87 #endif
88 };
89 #define	XPRISON_VERSION		3
90 
91 #define	PRISON_STATE_INVALID	0
92 #define	PRISON_STATE_ALIVE	1
93 #define	PRISON_STATE_DYING	2
94 
95 /*
96  * Flags for jail_set and jail_get.
97  */
98 #define	JAIL_CREATE	0x01	/* Create jail if it doesn't exist */
99 #define	JAIL_UPDATE	0x02	/* Update parameters of existing jail */
100 #define	JAIL_ATTACH	0x04	/* Attach to jail upon creation */
101 #define	JAIL_DYING	0x08	/* Allow getting a dying jail */
102 #define	JAIL_SET_MASK	0x0f
103 #define	JAIL_GET_MASK	0x08
104 
105 #define	JAIL_SYS_DISABLE	0
106 #define	JAIL_SYS_NEW		1
107 #define	JAIL_SYS_INHERIT	2
108 
109 #ifndef _KERNEL
110 
111 struct iovec;
112 
113 int jail(struct jail *);
114 int jail_set(struct iovec *, unsigned int, int);
115 int jail_get(struct iovec *, unsigned int, int);
116 int jail_attach(int);
117 int jail_remove(int);
118 
119 #else /* _KERNEL */
120 
121 #include <sys/queue.h>
122 #include <sys/sysctl.h>
123 #include <sys/lock.h>
124 #include <sys/mutex.h>
125 #include <sys/_task.h>
126 
127 #define JAIL_MAX	999999
128 
129 #ifdef MALLOC_DECLARE
130 MALLOC_DECLARE(M_PRISON);
131 #endif
132 #endif /* _KERNEL */
133 
134 #if defined(_KERNEL) || defined(_WANT_PRISON)
135 
136 #include <sys/osd.h>
137 
138 #define	HOSTUUIDLEN	64
139 #define	OSRELEASELEN	32
140 
141 struct racct;
142 struct prison_racct;
143 
144 /*
145  * This structure describes a prison.  It is pointed to by all struct
146  * ucreds's of the inmates.  pr_ref keeps track of them and is used to
147  * delete the struture when the last inmate is dead.
148  *
149  * Lock key:
150  *   (a) allprison_lock
151  *   (p) locked by pr_mtx
152  *   (c) set only during creation before the structure is shared, no mutex
153  *       required to read
154  */
155 struct prison {
156 	TAILQ_ENTRY(prison) pr_list;			/* (a) all prisons */
157 	int		 pr_id;				/* (c) prison id */
158 	int		 pr_ref;			/* (p) refcount */
159 	int		 pr_uref;			/* (p) user (alive) refcount */
160 	unsigned	 pr_flags;			/* (p) PR_* flags */
161 	LIST_HEAD(, prison) pr_children;		/* (a) list of child jails */
162 	LIST_ENTRY(prison) pr_sibling;			/* (a) next in parent's list */
163 	struct prison	*pr_parent;			/* (c) containing jail */
164 	struct mtx	 pr_mtx;
165 	struct task	 pr_task;			/* (c) destroy task */
166 	struct osd	 pr_osd;			/* (p) additional data */
167 	struct cpuset	*pr_cpuset;			/* (p) cpuset */
168 	struct vnet	*pr_vnet;			/* (c) network stack */
169 	struct vnode	*pr_root;			/* (c) vnode to rdir */
170 	int		 pr_ip4s;			/* (p) number of v4 IPs */
171 	int		 pr_ip6s;			/* (p) number of v6 IPs */
172 	struct in_addr	*pr_ip4;			/* (p) v4 IPs of jail */
173 	struct in6_addr	*pr_ip6;			/* (p) v6 IPs of jail */
174 	struct prison_racct *pr_prison_racct;		/* (c) racct jail proxy */
175 	void		*pr_sparep[3];
176 	int		 pr_childcount;			/* (a) number of child jails */
177 	int		 pr_childmax;			/* (p) maximum child jails */
178 	unsigned	 pr_allow;			/* (p) PR_ALLOW_* flags */
179 	int		 pr_securelevel;		/* (p) securelevel */
180 	int		 pr_enforce_statfs;		/* (p) statfs permission */
181 	int		 pr_devfs_rsnum;		/* (p) devfs ruleset */
182 	int		 pr_spare[3];
183 	int		 pr_osreldate;			/* (c) kern.osreldate value */
184 	unsigned long	 pr_hostid;			/* (p) jail hostid */
185 	char		 pr_name[MAXHOSTNAMELEN];	/* (p) admin jail name */
186 	char		 pr_path[MAXPATHLEN];		/* (c) chroot path */
187 	char		 pr_hostname[MAXHOSTNAMELEN];	/* (p) jail hostname */
188 	char		 pr_domainname[MAXHOSTNAMELEN];	/* (p) jail domainname */
189 	char		 pr_hostuuid[HOSTUUIDLEN];	/* (p) jail hostuuid */
190 	char		 pr_osrelease[OSRELEASELEN];	/* (c) kern.osrelease value */
191 };
192 
193 struct prison_racct {
194 	LIST_ENTRY(prison_racct) prr_next;
195 	char		prr_name[MAXHOSTNAMELEN];
196 	u_int		prr_refcount;
197 	struct racct	*prr_racct;
198 };
199 #endif /* _KERNEL || _WANT_PRISON */
200 
201 #ifdef _KERNEL
202 /* Flag bits set via options */
203 #define	PR_PERSIST	0x00000001	/* Can exist without processes */
204 #define	PR_HOST		0x00000002	/* Virtualize hostname et al */
205 #define	PR_IP4_USER	0x00000004	/* Restrict IPv4 addresses */
206 #define	PR_IP6_USER	0x00000008	/* Restrict IPv6 addresses */
207 #define	PR_VNET		0x00000010	/* Virtual network stack */
208 #define	PR_IP4_SADDRSEL	0x00000080	/* Do IPv4 src addr sel. or use the */
209 					/* primary jail address. */
210 #define	PR_IP6_SADDRSEL	0x00000100	/* Do IPv6 src addr sel. or use the */
211 					/* primary jail address. */
212 
213 /* Internal flag bits */
214 #define	PR_IP4		0x02000000	/* IPv4 restricted or disabled */
215 					/* by this jail or an ancestor */
216 #define	PR_IP6		0x04000000	/* IPv6 restricted or disabled */
217 					/* by this jail or an ancestor */
218 
219 /*
220  * Flags for pr_allow
221  * Bits not noted here may be used for dynamic allow.mount.xxxfs.
222  */
223 #define	PR_ALLOW_SET_HOSTNAME		0x00000001
224 #define	PR_ALLOW_SYSVIPC		0x00000002
225 #define	PR_ALLOW_RAW_SOCKETS		0x00000004
226 #define	PR_ALLOW_CHFLAGS		0x00000008
227 #define	PR_ALLOW_MOUNT			0x00000010
228 #define	PR_ALLOW_QUOTAS			0x00000020
229 #define	PR_ALLOW_SOCKET_AF		0x00000040
230 #define	PR_ALLOW_MLOCK			0x00000080
231 #define	PR_ALLOW_READ_MSGBUF		0x00000100
232 #define	PR_ALLOW_RESERVED_PORTS		0x00008000
233 #define	PR_ALLOW_KMEM_ACCESS		0x00010000	/* reserved, not used yet */
234 #define	PR_ALLOW_ALL_STATIC		0x000181ff
235 
236 /*
237  * OSD methods
238  */
239 #define	PR_METHOD_CREATE	0
240 #define	PR_METHOD_GET		1
241 #define	PR_METHOD_SET		2
242 #define	PR_METHOD_CHECK		3
243 #define	PR_METHOD_ATTACH	4
244 #define	PR_METHOD_REMOVE	5
245 #define	PR_MAXMETHOD		6
246 
247 /*
248  * Lock/unlock a prison.
249  * XXX These exist not so much for general convenience, but to be useable in
250  *     the FOREACH_PRISON_DESCENDANT_LOCKED macro which can't handle them in
251  *     non-function form as currently defined.
252  */
253 static __inline void
prison_lock(struct prison * pr)254 prison_lock(struct prison *pr)
255 {
256 
257 	mtx_lock(&pr->pr_mtx);
258 }
259 
260 static __inline void
prison_unlock(struct prison * pr)261 prison_unlock(struct prison *pr)
262 {
263 
264 	mtx_unlock(&pr->pr_mtx);
265 }
266 
267 /* Traverse a prison's immediate children. */
268 #define	FOREACH_PRISON_CHILD(ppr, cpr)					\
269 	LIST_FOREACH(cpr, &(ppr)->pr_children, pr_sibling)
270 
271 /*
272  * Preorder traversal of all of a prison's descendants.
273  * This ugly loop allows the macro to be followed by a single block
274  * as expected in a looping primitive.
275  */
276 #define	FOREACH_PRISON_DESCENDANT(ppr, cpr, descend)			\
277 	for ((cpr) = (ppr), (descend) = 1;				\
278 	    ((cpr) = (((descend) && !LIST_EMPTY(&(cpr)->pr_children))	\
279 	      ? LIST_FIRST(&(cpr)->pr_children)				\
280 	      : ((cpr) == (ppr)						\
281 		 ? NULL							\
282 		 : (((descend) = LIST_NEXT(cpr, pr_sibling) != NULL)	\
283 		    ? LIST_NEXT(cpr, pr_sibling)			\
284 		    : (cpr)->pr_parent))));)				\
285 		if (!(descend))						\
286 			;						\
287 		else
288 
289 /*
290  * As above, but lock descendants on the way down and unlock on the way up.
291  */
292 #define	FOREACH_PRISON_DESCENDANT_LOCKED(ppr, cpr, descend)		\
293 	for ((cpr) = (ppr), (descend) = 1;				\
294 	    ((cpr) = (((descend) && !LIST_EMPTY(&(cpr)->pr_children))	\
295 	      ? LIST_FIRST(&(cpr)->pr_children)				\
296 	      : ((cpr) == (ppr)						\
297 		 ? NULL							\
298 		 : ((prison_unlock(cpr),				\
299 		    (descend) = LIST_NEXT(cpr, pr_sibling) != NULL)	\
300 		    ? LIST_NEXT(cpr, pr_sibling)			\
301 		    : (cpr)->pr_parent))));)				\
302 		if ((descend) ? (prison_lock(cpr), 0) : 1)		\
303 			;						\
304 		else
305 
306 /*
307  * As above, but also keep track of the level descended to.
308  */
309 #define	FOREACH_PRISON_DESCENDANT_LOCKED_LEVEL(ppr, cpr, descend, level)\
310 	for ((cpr) = (ppr), (descend) = 1, (level) = 0;			\
311 	    ((cpr) = (((descend) && !LIST_EMPTY(&(cpr)->pr_children))	\
312 	      ? (level++, LIST_FIRST(&(cpr)->pr_children))		\
313 	      : ((cpr) == (ppr)						\
314 		 ? NULL							\
315 		 : ((prison_unlock(cpr),				\
316 		    (descend) = LIST_NEXT(cpr, pr_sibling) != NULL)	\
317 		    ? LIST_NEXT(cpr, pr_sibling)			\
318 		    : (level--, (cpr)->pr_parent)))));)			\
319 		if ((descend) ? (prison_lock(cpr), 0) : 1)		\
320 			;						\
321 		else
322 
323 /*
324  * Attributes of the physical system, and the root of the jail tree.
325  */
326 extern struct	prison prison0;
327 
328 TAILQ_HEAD(prisonlist, prison);
329 extern struct	prisonlist allprison;
330 extern struct	sx allprison_lock;
331 
332 /*
333  * Sysctls to describe jail parameters.
334  */
335 SYSCTL_DECL(_security_jail_param);
336 
337 #define	SYSCTL_JAIL_PARAM(module, param, type, fmt, descr)		\
338     SYSCTL_PROC(_security_jail_param ## module, OID_AUTO, param,	\
339 	(type) | CTLFLAG_MPSAFE, NULL, 0, sysctl_jail_param, fmt, descr)
340 #define	SYSCTL_JAIL_PARAM_STRING(module, param, access, len, descr)	\
341     SYSCTL_PROC(_security_jail_param ## module, OID_AUTO, param,	\
342 	CTLTYPE_STRING | CTLFLAG_MPSAFE | (access), NULL, len,		\
343 	sysctl_jail_param, "A", descr)
344 #define	SYSCTL_JAIL_PARAM_STRUCT(module, param, access, len, fmt, descr)\
345     SYSCTL_PROC(_security_jail_param ## module, OID_AUTO, param,	\
346 	CTLTYPE_STRUCT | CTLFLAG_MPSAFE | (access), NULL, len,		\
347 	sysctl_jail_param, fmt, descr)
348 #define	SYSCTL_JAIL_PARAM_NODE(module, descr)				\
349     SYSCTL_NODE(_security_jail_param, OID_AUTO, module, 0, 0, descr)
350 #define	SYSCTL_JAIL_PARAM_SUBNODE(parent, module, descr)		\
351     SYSCTL_NODE(_security_jail_param_##parent, OID_AUTO, module, 0, 0, descr)
352 #define	SYSCTL_JAIL_PARAM_SYS_NODE(module, access, descr)		\
353     SYSCTL_JAIL_PARAM_NODE(module, descr);				\
354     SYSCTL_JAIL_PARAM(_##module, , CTLTYPE_INT | (access), "E,jailsys",	\
355 	descr)
356 
357 /*
358  * Kernel support functions for jail().
359  */
360 struct ucred;
361 struct mount;
362 struct sockaddr;
363 struct statfs;
364 struct vfsconf;
365 int jailed(struct ucred *cred);
366 int jailed_without_vnet(struct ucred *);
367 void getcredhostname(struct ucred *, char *, size_t);
368 void getcreddomainname(struct ucred *, char *, size_t);
369 void getcredhostuuid(struct ucred *, char *, size_t);
370 void getcredhostid(struct ucred *, unsigned long *);
371 void prison0_init(void);
372 int prison_allow(struct ucred *, unsigned);
373 int prison_check(struct ucred *cred1, struct ucred *cred2);
374 int prison_owns_vnet(struct ucred *);
375 int prison_canseemount(struct ucred *cred, struct mount *mp);
376 void prison_enforce_statfs(struct ucred *cred, struct mount *mp,
377     struct statfs *sp);
378 struct prison *prison_find(int prid);
379 struct prison *prison_find_child(struct prison *, int);
380 struct prison *prison_find_name(struct prison *, const char *);
381 int prison_flag(struct ucred *, unsigned);
382 void prison_free(struct prison *pr);
383 void prison_free_locked(struct prison *pr);
384 void prison_hold(struct prison *pr);
385 void prison_hold_locked(struct prison *pr);
386 void prison_proc_hold(struct prison *);
387 void prison_proc_free(struct prison *);
388 int prison_ischild(struct prison *, struct prison *);
389 int prison_equal_ip4(struct prison *, struct prison *);
390 int prison_get_ip4(struct ucred *cred, struct in_addr *ia);
391 int prison_local_ip4(struct ucred *cred, struct in_addr *ia);
392 int prison_remote_ip4(struct ucred *cred, struct in_addr *ia);
393 int prison_check_ip4(const struct ucred *, const struct in_addr *);
394 int prison_check_ip4_locked(const struct prison *, const struct in_addr *);
395 int prison_saddrsel_ip4(struct ucred *, struct in_addr *);
396 int prison_restrict_ip4(struct prison *, struct in_addr *);
397 int prison_qcmp_v4(const void *, const void *);
398 #ifdef INET6
399 int prison_equal_ip6(struct prison *, struct prison *);
400 int prison_get_ip6(struct ucred *, struct in6_addr *);
401 int prison_local_ip6(struct ucred *, struct in6_addr *, int);
402 int prison_remote_ip6(struct ucred *, struct in6_addr *);
403 int prison_check_ip6(const struct ucred *, const struct in6_addr *);
404 int prison_check_ip6_locked(const struct prison *, const struct in6_addr *);
405 int prison_saddrsel_ip6(struct ucred *, struct in6_addr *);
406 int prison_restrict_ip6(struct prison *, struct in6_addr *);
407 int prison_qcmp_v6(const void *, const void *);
408 #endif
409 int prison_check_af(struct ucred *cred, int af);
410 int prison_if(struct ucred *cred, struct sockaddr *sa);
411 char *prison_name(struct prison *, struct prison *);
412 int prison_priv_check(struct ucred *cred, int priv);
413 int sysctl_jail_param(SYSCTL_HANDLER_ARGS);
414 unsigned prison_add_allow(const char *prefix, const char *name,
415     const char *prefix_descr, const char *descr);
416 void prison_add_vfs(struct vfsconf *vfsp);
417 void prison_racct_foreach(void (*callback)(struct racct *racct,
418     void *arg2, void *arg3), void (*pre)(void), void (*post)(void),
419     void *arg2, void *arg3);
420 struct prison_racct *prison_racct_find(const char *name);
421 void prison_racct_hold(struct prison_racct *prr);
422 void prison_racct_free(struct prison_racct *prr);
423 
424 #endif /* _KERNEL */
425 #endif /* !_SYS_JAIL_H_ */
426