xref: /freebsd/sys/sys/jail.h (revision 1256447cbc21361ff5a6f9022b58ad6c1c43e59f)
1 /*-
2  * SPDX-License-Identifier: BSD-2-Clause
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 
30 #ifndef _SYS_JAIL_H_
31 #define _SYS_JAIL_H_
32 
33 #ifdef _KERNEL
34 struct jail_v0 {
35 	u_int32_t	version;
36 	char		*path;
37 	char		*hostname;
38 	u_int32_t	ip_number;
39 };
40 #endif
41 
42 struct jail {
43 	uint32_t	version;
44 	char		*path;
45 	char		*hostname;
46 	char		*jailname;
47 	uint32_t	ip4s;
48 	uint32_t	ip6s;
49 	struct in_addr	*ip4;
50 	struct in6_addr	*ip6;
51 };
52 #define	JAIL_API_VERSION	2
53 
54 /*
55  * For all xprison structs, always keep the pr_version an int and
56  * the first variable so userspace can easily distinguish them.
57  */
58 #ifndef _KERNEL
59 struct xprison_v1 {
60 	int		 pr_version;
61 	int		 pr_id;
62 	char		 pr_path[MAXPATHLEN];
63 	char		 pr_host[MAXHOSTNAMELEN];
64 	u_int32_t	 pr_ip;
65 };
66 #endif
67 
68 struct xprison {
69 	int		 pr_version;
70 	int		 pr_id;
71 	int		 pr_state;
72 	cpusetid_t	 pr_cpusetid;
73 	char		 pr_path[MAXPATHLEN];
74 	char		 pr_host[MAXHOSTNAMELEN];
75 	char		 pr_name[MAXHOSTNAMELEN];
76 	uint32_t	 pr_ip4s;
77 	uint32_t	 pr_ip6s;
78 #if 0
79 	/*
80 	 * sizeof(xprison) will be malloced + size needed for all
81 	 * IPv4 and IPv6 addesses. Offsets are based numbers of addresses.
82 	 */
83 	struct in_addr	 pr_ip4[];
84 	struct in6_addr	 pr_ip6[];
85 #endif
86 };
87 #define	XPRISON_VERSION		3
88 
89 enum prison_state {
90     PRISON_STATE_INVALID = 0,	/* New prison, not ready to be seen */
91     PRISON_STATE_ALIVE,		/* Current prison, visible to all */
92     PRISON_STATE_DYING		/* Removed but holding resources, */
93 };				/* optionally visible. */
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_USE_DESC	0x10	/* Get/set jail in descriptor */
103 #define JAIL_AT_DESC	0x20	/* Find/add jail under descriptor */
104 #define	JAIL_GET_DESC	0x40	/* Return a new jail descriptor */
105 #define	JAIL_OWN_DESC	0x80	/* Return a new owning jail descriptor */
106 #define	JAIL_SET_MASK	0xff	/* JAIL_DYING is deprecated/ignored here */
107 #define	JAIL_GET_MASK	0xf8
108 
109 #define	JAIL_SYS_DISABLE	0
110 #define	JAIL_SYS_NEW		1
111 #define	JAIL_SYS_INHERIT	2
112 
113 /*
114  * Names of the static jail parameters registered by the base kernel.
115  * Runtime-registered parameters (allow.mount.*, modules) are only
116  * discoverable via the security.jail.param sysctl tree.
117  */
118 #define	JAIL_PARAM_JID			"jid"
119 #define	JAIL_PARAM_LASTJID		"lastjid"
120 #define	JAIL_PARAM_NAME			"name"
121 #define	JAIL_PARAM_PARENT		"parent"
122 #define	JAIL_PARAM_PATH			"path"
123 #define	JAIL_PARAM_DESC			"desc"
124 #define	JAIL_PARAM_ERRMSG		"errmsg"
125 #define	JAIL_PARAM_PERSIST		"persist"
126 #define	JAIL_PARAM_NOPERSIST		"nopersist"
127 #define	JAIL_PARAM_DYING		"dying"
128 #define	JAIL_PARAM_NODYING		"nodying"
129 #define	JAIL_PARAM_SECURELEVEL		"securelevel"
130 #define	JAIL_PARAM_DEVFS_RULESET	"devfs_ruleset"
131 #define	JAIL_PARAM_ENFORCE_STATFS	"enforce_statfs"
132 #define	JAIL_PARAM_CHILDREN_CUR		"children.cur"
133 #define	JAIL_PARAM_CHILDREN_MAX		"children.max"
134 #define	JAIL_PARAM_CPUSET_ID		"cpuset.id"
135 #define	JAIL_PARAM_OSRELDATE		"osreldate"
136 #define	JAIL_PARAM_OSRELEASE		"osrelease"
137 
138 #define	JAIL_PARAM_HOST			"host"
139 #define	JAIL_PARAM_HOST_HOSTNAME	"host.hostname"
140 #define	JAIL_PARAM_HOST_DOMAINNAME	"host.domainname"
141 #define	JAIL_PARAM_HOST_HOSTUUID	"host.hostuuid"
142 #define	JAIL_PARAM_HOST_HOSTID		"host.hostid"
143 
144 #if !defined(_KERNEL) || defined(INET)
145 #define	JAIL_PARAM_IP4			"ip4"
146 #define	JAIL_PARAM_IP4_ADDR		"ip4.addr"
147 #define	JAIL_PARAM_IP4_SADDRSEL		"ip4.saddrsel"
148 #define	JAIL_PARAM_IP4_NOSADDRSEL	"ip4.nosaddrsel"
149 #endif
150 #if !defined(_KERNEL) || defined(INET6)
151 #define	JAIL_PARAM_IP6			"ip6"
152 #define	JAIL_PARAM_IP6_ADDR		"ip6.addr"
153 #define	JAIL_PARAM_IP6_SADDRSEL		"ip6.saddrsel"
154 #define	JAIL_PARAM_IP6_NOSADDRSEL	"ip6.nosaddrsel"
155 #endif
156 #if !defined(_KERNEL) || defined(VIMAGE)
157 #define	JAIL_PARAM_VNET			"vnet"
158 #endif
159 
160 #define	JAIL_PARAM_ALLOW_SET_HOSTNAME	"allow.set_hostname"
161 #define	JAIL_PARAM_ALLOW_NOSET_HOSTNAME	"allow.noset_hostname"
162 #define	JAIL_PARAM_ALLOW_SYSVIPC	"allow.sysvipc"
163 #define	JAIL_PARAM_ALLOW_NOSYSVIPC	"allow.nosysvipc"
164 #define	JAIL_PARAM_ALLOW_RAW_SOCKETS	"allow.raw_sockets"
165 #define	JAIL_PARAM_ALLOW_NORAW_SOCKETS	"allow.noraw_sockets"
166 #define	JAIL_PARAM_ALLOW_CHFLAGS	"allow.chflags"
167 #define	JAIL_PARAM_ALLOW_NOCHFLAGS	"allow.nochflags"
168 #define	JAIL_PARAM_ALLOW_MOUNT		"allow.mount"
169 #define	JAIL_PARAM_ALLOW_NOMOUNT	"allow.nomount"
170 #define	JAIL_PARAM_ALLOW_QUOTAS		"allow.quotas"
171 #define	JAIL_PARAM_ALLOW_NOQUOTAS	"allow.noquotas"
172 #define	JAIL_PARAM_ALLOW_SOCKET_AF	"allow.socket_af"
173 #define	JAIL_PARAM_ALLOW_NOSOCKET_AF	"allow.nosocket_af"
174 #define	JAIL_PARAM_ALLOW_MLOCK		"allow.mlock"
175 #define	JAIL_PARAM_ALLOW_NOMLOCK	"allow.nomlock"
176 #define	JAIL_PARAM_ALLOW_RESERVED_PORTS	"allow.reserved_ports"
177 #define	JAIL_PARAM_ALLOW_NORESERVED_PORTS \
178 					"allow.noreserved_ports"
179 #define	JAIL_PARAM_ALLOW_READ_MSGBUF	"allow.read_msgbuf"
180 #define	JAIL_PARAM_ALLOW_NOREAD_MSGBUF	"allow.noread_msgbuf"
181 #define	JAIL_PARAM_ALLOW_UNPRIV_DEBUG	"allow.unprivileged_proc_debug"
182 #define	JAIL_PARAM_ALLOW_NOUNPRIV_DEBUG	"allow.nounprivileged_proc_debug"
183 #define	JAIL_PARAM_ALLOW_SUSER		"allow.suser"
184 #define	JAIL_PARAM_ALLOW_NOSUSER	"allow.nosuser"
185 #if !defined(_KERNEL) || defined(VIMAGE)
186 #define	JAIL_PARAM_ALLOW_NFSD		"allow.nfsd"
187 #define	JAIL_PARAM_ALLOW_NONFSD		"allow.nonfsd"
188 #endif
189 #define	JAIL_PARAM_ALLOW_EXTATTR	"allow.extattr"
190 #define	JAIL_PARAM_ALLOW_NOEXTATTR	"allow.noextattr"
191 #define	JAIL_PARAM_ALLOW_ADJTIME	"allow.adjtime"
192 #define	JAIL_PARAM_ALLOW_NOADJTIME	"allow.noadjtime"
193 #define	JAIL_PARAM_ALLOW_SETTIME	"allow.settime"
194 #define	JAIL_PARAM_ALLOW_NOSETTIME	"allow.nosettime"
195 #define	JAIL_PARAM_ALLOW_ROUTING	"allow.routing"
196 #define	JAIL_PARAM_ALLOW_NOROUTING	"allow.norouting"
197 #define	JAIL_PARAM_ALLOW_UNPRIV_PARENT_TAMPER \
198 					"allow.unprivileged_parent_tampering"
199 #define	JAIL_PARAM_ALLOW_NOUNPRIV_PARENT_TAMPER \
200 					"allow.nounprivileged_parent_tampering"
201 #if !defined(_KERNEL) || defined(AUDIT)
202 #define	JAIL_PARAM_ALLOW_SETAUDIT	"allow.setaudit"
203 #define	JAIL_PARAM_ALLOW_NOSETAUDIT	"allow.nosetaudit"
204 #endif
205 
206 #ifndef _KERNEL
207 
208 struct iovec;
209 
210 __BEGIN_DECLS
211 int jail(struct jail *);
212 int jail_set(struct iovec *, unsigned int, int);
213 int jail_get(struct iovec *, unsigned int, int);
214 int jail_attach(int);
215 int jail_attach_jd(int);
216 int jail_remove(int);
217 int jail_remove_jd(int);
218 __END_DECLS
219 
220 #else /* _KERNEL */
221 
222 #include <sys/queue.h>
223 #include <sys/sysctl.h>
224 #include <sys/lock.h>
225 #include <sys/mutex.h>
226 #include <sys/_task.h>
227 
228 #define JAIL_MAX	999999
229 
230 #ifdef MALLOC_DECLARE
231 MALLOC_DECLARE(M_PRISON);
232 #endif
233 #endif /* _KERNEL */
234 
235 #if defined(_KERNEL) || defined(_WANT_PRISON)
236 
237 #include <sys/osd.h>
238 
239 #define	HOSTUUIDLEN	64
240 #define	DEFAULT_HOSTUUID	"00000000-0000-0000-0000-000000000000"
241 #define	OSRELEASELEN	32
242 
243 #define	JAIL_META_PRIVATE	"meta"
244 #define	JAIL_META_SHARED	"env"
245 
246 struct jaildesc;
247 struct knlist;
248 struct racct;
249 struct prison_racct;
250 
251 typedef enum {
252 	PR_INET		= 0,
253 	PR_INET6	= 1,
254 	PR_FAMILY_MAX	= 2,
255 } pr_family_t;
256 
257 /*
258  * This structure describes a prison.  It is pointed to by all struct
259  * ucreds's of the inmates.  pr_ref keeps track of them and is used to
260  * delete the structure when the last inmate is dead.
261  *
262  * Lock key:
263  *   (a) allprison_lock
264  *   (A) allproc_lock
265  *   (c) set only during creation before the structure is shared, no mutex
266  *       required to read
267  *   (m) locked by pr_mtx
268  *   (p) locked by pr_mtx, and also at least shared allprison_lock required
269  *       to update
270  *   (q) locked by both pr_mtx and allprison_lock
271  *   (r) atomic via refcount(9), pr_mtx and allprison_lock required to
272  *       decrement to zero
273  *   (n) read access granted with the network epoch
274  */
275 struct prison {
276 	TAILQ_ENTRY(prison) pr_list;			/* (a) all prisons */
277 	int		 pr_id;				/* (c) prison id */
278 	volatile u_int	 pr_ref;			/* (r) refcount */
279 	volatile u_int	 pr_uref;			/* (r) user (alive) refcount */
280 	unsigned	 pr_flags;			/* (p) PR_* flags */
281 	LIST_HEAD(, prison) pr_children;		/* (a) list of child jails */
282 	LIST_HEAD(, proc) pr_proclist;			/* (A) list of jailed processes */
283 	LIST_ENTRY(prison) pr_sibling;			/* (a) next in parent's list */
284 	struct prison	*pr_parent;			/* (c) containing jail */
285 	struct mtx	 pr_mtx;
286 	struct task	 pr_task;			/* (c) destroy task */
287 	struct osd	 pr_osd;			/* (p) additional data */
288 	struct cpuset	*pr_cpuset;			/* (p) cpuset */
289 	struct vnet	*pr_vnet;			/* (c) network stack */
290 	struct vnode	*pr_root;			/* (c) vnode to rdir */
291 	struct prison_ip  *pr_addrs[PR_FAMILY_MAX];	/* (p,n) IPs of jail */
292 	struct prison_racct *pr_prison_racct;		/* (c) racct jail proxy */
293 	struct knlist	*pr_klist;			/* (m) attached knotes */
294 	struct label	*pr_label;			/* (m) MAC label */
295 	LIST_HEAD(, jaildesc) pr_descs;			/* (a) attached descriptors */
296 	void		*pr_sparep;
297 	int		 pr_childcount;			/* (a) number of child jails */
298 	int		 pr_childmax;			/* (p) maximum child jails */
299 	unsigned	 pr_allow;			/* (p) PR_ALLOW_* flags */
300 	int		 pr_securelevel;		/* (p) securelevel */
301 	int		 pr_enforce_statfs;		/* (p) statfs permission */
302 	int		 pr_devfs_rsnum;		/* (p) devfs ruleset */
303 	enum prison_state pr_state;			/* (q) state in life cycle */
304 	volatile int	 pr_exportcnt;			/* (r) count of mount exports */
305 	int		 pr_spare;
306 	int		 pr_osreldate;			/* (c) kern.osreldate value */
307 	unsigned long	 pr_hostid;			/* (p) jail hostid */
308 	char		 pr_name[MAXHOSTNAMELEN];	/* (p) admin jail name */
309 	char		 pr_path[MAXPATHLEN];		/* (c) chroot path */
310 	char		 pr_hostname[MAXHOSTNAMELEN];	/* (p) jail hostname */
311 	char		 pr_domainname[MAXHOSTNAMELEN];	/* (p) jail domainname */
312 	char		 pr_hostuuid[HOSTUUIDLEN];	/* (p) jail hostuuid */
313 	char		 pr_osrelease[OSRELEASELEN];	/* (c) kern.osrelease value */
314 };
315 
316 struct prison_racct {
317 	LIST_ENTRY(prison_racct) prr_next;
318 	char		prr_name[MAXHOSTNAMELEN];
319 	u_int		prr_refcount;
320 	struct racct	*prr_racct;
321 };
322 #endif /* _KERNEL || _WANT_PRISON */
323 
324 #ifdef _KERNEL
325 /* Flag bits set via options */
326 #define	PR_PERSIST	0x00000001	/* Can exist without processes */
327 #define	PR_HOST		0x00000002	/* Virtualize hostname et al */
328 #define	PR_IP4_USER	0x00000004	/* Restrict IPv4 addresses */
329 #define	PR_IP6_USER	0x00000008	/* Restrict IPv6 addresses */
330 #define	PR_VNET		0x00000010	/* Virtual network stack */
331 #define	PR_IP4_SADDRSEL	0x00000080	/* Do IPv4 src addr sel. or use the */
332 					/* primary jail address. */
333 #define	PR_IP6_SADDRSEL	0x00000100	/* Do IPv6 src addr sel. or use the */
334 					/* primary jail address. */
335 
336 /* Internal flag bits */
337 #define	PR_REMOVE	0x01000000	/* In process of being removed */
338 #define	PR_IP4		0x02000000	/* IPv4 restricted or disabled */
339 					/* by this jail or an ancestor */
340 #define	PR_IP6		0x04000000	/* IPv6 restricted or disabled */
341 					/* by this jail or an ancestor */
342 #define PR_COMPLETE_PROC 0x08000000	/* prison_complete called from */
343 					/* prison_proc_free, releases uref */
344 
345 /*
346  * Flags for pr_allow
347  * Bits not noted here may be used for dynamic allow.mount.xxxfs.
348  */
349 #define	PR_ALLOW_SET_HOSTNAME		0x00000001
350 #define	PR_ALLOW_SYSVIPC		0x00000002
351 #define	PR_ALLOW_RAW_SOCKETS		0x00000004
352 #define	PR_ALLOW_CHFLAGS		0x00000008
353 #define	PR_ALLOW_MOUNT			0x00000010
354 #define	PR_ALLOW_QUOTAS			0x00000020
355 #define	PR_ALLOW_SOCKET_AF		0x00000040
356 #define	PR_ALLOW_MLOCK			0x00000080
357 #define	PR_ALLOW_READ_MSGBUF		0x00000100
358 #define	PR_ALLOW_UNPRIV_DEBUG		0x00000200
359 #define	PR_ALLOW_SUSER			0x00000400
360 #define	PR_ALLOW_RESERVED_PORTS		0x00008000
361 #define	PR_ALLOW_KMEM_ACCESS		0x00010000	/* reserved, not used yet */
362 #define	PR_ALLOW_NFSD			0x00020000
363 #define	PR_ALLOW_EXTATTR		0x00040000
364 #define	PR_ALLOW_ADJTIME		0x00080000
365 #define	PR_ALLOW_SETTIME		0x00100000
366 #define	PR_ALLOW_ROUTING		0x00200000
367 #define	PR_ALLOW_UNPRIV_PARENT_TAMPER	0x00400000
368 #define	PR_ALLOW_SETAUDIT		0x00800000
369 
370 /*
371  * PR_ALLOW_PRISON0 are the allow flags that we apply by default to prison0,
372  * while PR_ALLOW_ALL_STATIC are all of the allow bits that we have allocated at
373  * build time.  PR_ALLOW_ALL_STATIC should contain any bit above that we expect
374  * to be used on the system, while PR_ALLOW_PRISON0 will be some subset of that.
375  */
376 #define	PR_ALLOW_ALL_STATIC		\
377     (PR_ALLOW_SET_HOSTNAME | PR_ALLOW_SYSVIPC | PR_ALLOW_RAW_SOCKETS | \
378      PR_ALLOW_CHFLAGS | PR_ALLOW_MOUNT | PR_ALLOW_QUOTAS | \
379      PR_ALLOW_SOCKET_AF | PR_ALLOW_MLOCK | PR_ALLOW_READ_MSGBUF | \
380      PR_ALLOW_UNPRIV_DEBUG | PR_ALLOW_SUSER | PR_ALLOW_RESERVED_PORTS | \
381      PR_ALLOW_KMEM_ACCESS | PR_ALLOW_NFSD | PR_ALLOW_EXTATTR | \
382      PR_ALLOW_ADJTIME | PR_ALLOW_SETTIME | PR_ALLOW_ROUTING | \
383      PR_ALLOW_UNPRIV_PARENT_TAMPER | PR_ALLOW_SETAUDIT)
384 
385 #define	PR_ALLOW_PRISON0		\
386     (PR_ALLOW_ALL_STATIC & ~(PR_ALLOW_UNPRIV_PARENT_TAMPER))
387 
388 /*
389  * PR_ALLOW_DIFFERENCES determines which flags are able to be
390  * different between the parent and child jail upon creation.
391  */
392 #define	PR_ALLOW_DIFFERENCES		\
393     (PR_ALLOW_UNPRIV_DEBUG | PR_ALLOW_UNPRIV_PARENT_TAMPER)
394 
395 /*
396  * OSD methods
397  */
398 #define	PR_METHOD_CREATE	0
399 #define	PR_METHOD_GET		1
400 #define	PR_METHOD_SET		2
401 #define	PR_METHOD_CHECK		3
402 #define	PR_METHOD_ATTACH	4
403 #define	PR_METHOD_REMOVE	5
404 #define	PR_MAXMETHOD		6
405 
406 /*
407  * Lock/unlock a prison.
408  * XXX These exist not so much for general convenience, but to be useable in
409  *     the FOREACH_PRISON_DESCENDANT_LOCKED macro which can't handle them in
410  *     non-function form as currently defined.
411  */
412 static __inline void
prison_lock(struct prison * pr)413 prison_lock(struct prison *pr)
414 {
415 
416 	mtx_lock(&pr->pr_mtx);
417 }
418 
419 static __inline void
prison_unlock(struct prison * pr)420 prison_unlock(struct prison *pr)
421 {
422 
423 	mtx_unlock(&pr->pr_mtx);
424 }
425 
426 /* Traverse a prison's immediate children. */
427 #define	FOREACH_PRISON_CHILD(ppr, cpr)					\
428 	LIST_FOREACH(cpr, &(ppr)->pr_children, pr_sibling)
429 
430 /*
431  * Preorder traversal of all of a prison's descendants.
432  * This ugly loop allows the macro to be followed by a single block
433  * as expected in a looping primitive.
434  */
435 #define	FOREACH_PRISON_DESCENDANT(ppr, cpr, descend)			\
436 	for ((cpr) = (ppr), (descend) = 1;				\
437 	    ((cpr) = (((descend) && !LIST_EMPTY(&(cpr)->pr_children))	\
438 	      ? LIST_FIRST(&(cpr)->pr_children)				\
439 	      : ((cpr) == (ppr)						\
440 		 ? NULL							\
441 		 : (((descend) = LIST_NEXT(cpr, pr_sibling) != NULL)	\
442 		    ? LIST_NEXT(cpr, pr_sibling)			\
443 		    : (cpr)->pr_parent))));)				\
444 		if (!(descend))						\
445 			;						\
446 		else
447 
448 /*
449  * As above, but lock descendants on the way down and unlock on the way up.
450  */
451 #define	FOREACH_PRISON_DESCENDANT_LOCKED(ppr, cpr, descend)		\
452 	for ((cpr) = (ppr), (descend) = 1;				\
453 	    ((cpr) = (((descend) && !LIST_EMPTY(&(cpr)->pr_children))	\
454 	      ? LIST_FIRST(&(cpr)->pr_children)				\
455 	      : ((cpr) == (ppr)						\
456 		 ? NULL							\
457 		 : ((prison_unlock(cpr),				\
458 		    (descend) = LIST_NEXT(cpr, pr_sibling) != NULL)	\
459 		    ? LIST_NEXT(cpr, pr_sibling)			\
460 		    : (cpr)->pr_parent))));)				\
461 		if ((descend) ? (prison_lock(cpr), 0) : 1)		\
462 			;						\
463 		else
464 
465 /*
466  * As above, but also keep track of the level descended to.
467  */
468 #define	FOREACH_PRISON_DESCENDANT_LOCKED_LEVEL(ppr, cpr, descend, level)\
469 	for ((cpr) = (ppr), (descend) = 1, (level) = 0;			\
470 	    ((cpr) = (((descend) && !LIST_EMPTY(&(cpr)->pr_children))	\
471 	      ? (level++, LIST_FIRST(&(cpr)->pr_children))		\
472 	      : ((cpr) == (ppr)						\
473 		 ? NULL							\
474 		 : ((prison_unlock(cpr),				\
475 		    (descend) = LIST_NEXT(cpr, pr_sibling) != NULL)	\
476 		    ? LIST_NEXT(cpr, pr_sibling)			\
477 		    : (level--, (cpr)->pr_parent)))));)			\
478 		if ((descend) ? (prison_lock(cpr), 0) : 1)		\
479 			;						\
480 		else
481 
482 /*
483  * Traverse a prison's descendants, visiting both preorder and postorder.
484  */
485 #define FOREACH_PRISON_DESCENDANT_PRE_POST(ppr, cpr, descend)		\
486 	for ((cpr) = (ppr), (descend) = 1;				\
487 	     ((cpr) = (descend)						\
488 	      ? ((descend) = !LIST_EMPTY(&(cpr)->pr_children))		\
489 		? LIST_FIRST(&(cpr)->pr_children)			\
490 		: (cpr)							\
491 	      : ((descend) = LIST_NEXT(cpr, pr_sibling) != NULL)	\
492 		? LIST_NEXT(cpr, pr_sibling)				\
493 		: cpr->pr_parent) != (ppr);)
494 
495 /*
496  * Attributes of the physical system, and the root of the jail tree.
497  */
498 extern struct	prison prison0;
499 
500 TAILQ_HEAD(prisonlist, prison);
501 extern struct	prisonlist allprison;
502 extern struct	sx allprison_lock;
503 
504 /*
505  * Sysctls to describe jail parameters.
506  */
507 SYSCTL_DECL(_security_jail);
508 SYSCTL_DECL(_security_jail_param);
509 
510 #define SYSCTL_JAIL_PARAM_DECL(name)					\
511 	SYSCTL_DECL(_security_jail_param_##name)
512 #define	SYSCTL_JAIL_PARAM(module, param, type, fmt, descr)		\
513 	SYSCTL_PROC(_security_jail_param ## module, OID_AUTO, param,	\
514 	    (type) | CTLFLAG_MPSAFE, NULL, 0, sysctl_jail_param, fmt, descr)
515 #define	SYSCTL_JAIL_PARAM_STRING(module, param, access, len, descr)	\
516 	SYSCTL_PROC(_security_jail_param ## module, OID_AUTO, param,	\
517 	    CTLTYPE_STRING | CTLFLAG_MPSAFE | (access), NULL, len,	\
518 	    sysctl_jail_param, "A", descr)
519 #define	SYSCTL_JAIL_PARAM_STRUCT(module, param, access, len, fmt, descr) \
520 	SYSCTL_PROC(_security_jail_param ## module, OID_AUTO, param,	\
521 	    CTLTYPE_STRUCT | CTLFLAG_MPSAFE | (access), NULL, len,	\
522 	    sysctl_jail_param, fmt, descr)
523 #define	SYSCTL_JAIL_PARAM_NODE(module, descr)				\
524 	SYSCTL_NODE(_security_jail_param, OID_AUTO, module, CTLFLAG_MPSAFE, \
525 	    0, descr)
526 #define	SYSCTL_JAIL_PARAM_SUBNODE(parent, module, descr)		\
527 	SYSCTL_NODE(_security_jail_param_##parent, OID_AUTO, module,	\
528 	    CTLFLAG_MPSAFE, 0, descr)
529 #define	SYSCTL_JAIL_PARAM_SYS_NODE(module, access, descr)		\
530 	SYSCTL_JAIL_PARAM_NODE(module, descr);				\
531 	SYSCTL_JAIL_PARAM(_##module, , CTLTYPE_INT | (access), "E,jailsys", \
532 	    descr)
533 #define	SYSCTL_JAIL_PARAM_SYS_SUBNODE(parent, module, access, descr)	\
534 	SYSCTL_JAIL_PARAM_SUBNODE(parent, module, descr);		\
535 	SYSCTL_JAIL_PARAM(_##parent##_##module, , CTLTYPE_INT | (access), \
536 	    "E,jailsys", descr)
537 
538 /*
539  * Kernel support functions for jail().
540  */
541 struct knote;
542 struct mount;
543 struct sockaddr;
544 struct statfs;
545 struct ucred;
546 struct vfsconf;
547 
548 /*
549  * Return 1 if the passed credential is in a jail, otherwise 0.
550  */
551 #define jailed(cred)	(cred->cr_prison != &prison0)
552 
553 bool jailed_without_vnet(struct ucred *);
554 void getcredhostname(struct ucred *, char *, size_t);
555 void getcreddomainname(struct ucred *, char *, size_t);
556 void getcredhostuuid(struct ucred *, char *, size_t);
557 void getcredhostid(struct ucred *, unsigned long *);
558 void getjailname(struct ucred *cred, char *name, size_t len);
559 void prison0_init(void);
560 bool prison_allow(struct ucred *, unsigned);
561 int prison_check(struct ucred *cred1, struct ucred *cred2);
562 bool prison_check_nfsd(struct ucred *cred);
563 bool prison_owns_vnet(struct prison *pr);
564 int prison_canseemount(struct ucred *cred, struct mount *mp);
565 void prison_enforce_statfs(struct ucred *cred, struct mount *mp,
566     struct statfs *sp);
567 struct prison *prison_find(int prid);
568 struct prison *prison_find_child(struct prison *, int);
569 struct prison *prison_find_name(struct prison *, const char *);
570 bool prison_flag(struct ucred *, unsigned);
571 void prison_free(struct prison *pr);
572 void prison_free_locked(struct prison *pr);
573 void prison_hold(struct prison *pr);
574 void prison_hold_locked(struct prison *pr);
575 void prison_proc_hold(struct prison *);
576 void prison_proc_free(struct prison *);
577 void prison_proc_link(struct prison *, struct proc *);
578 void prison_proc_unlink(struct prison *, struct proc *);
579 void prison_proc_iterate(struct prison *, void (*)(struct proc *, void *), void *);
580 void prison_remove(struct prison *);
581 void prison_set_allow(struct ucred *cred, unsigned flag, int enable);
582 bool prison_ischild(struct prison *, struct prison *);
583 bool prison_isalive(const struct prison *);
584 bool prison_isvalid(struct prison *);
585 #if defined(INET) || defined(INET6)
586 int prison_ip_check(const struct prison *, const pr_family_t, const void *);
587 const void *prison_ip_get0(const struct prison *, const pr_family_t);
588 u_int prison_ip_cnt(const struct prison *, const pr_family_t);
589 #endif
590 #ifdef INET
591 bool prison_equal_ip4(struct prison *, struct prison *);
592 int prison_get_ip4(struct ucred *cred, struct in_addr *ia);
593 int prison_local_ip4(struct ucred *cred, struct in_addr *ia);
594 int prison_remote_ip4(struct ucred *cred, struct in_addr *ia);
595 int prison_check_ip4(const struct ucred *, const struct in_addr *);
596 int prison_check_ip4_locked(const struct prison *, const struct in_addr *);
597 bool prison_saddrsel_ip4(struct ucred *, struct in_addr *);
598 int prison_qcmp_v4(const void *, const void *);
599 bool prison_valid_v4(const void *);
600 #endif
601 #ifdef INET6
602 bool prison_equal_ip6(struct prison *, struct prison *);
603 int prison_get_ip6(struct ucred *, struct in6_addr *);
604 int prison_local_ip6(struct ucred *, struct in6_addr *, int);
605 int prison_remote_ip6(struct ucred *, struct in6_addr *);
606 int prison_check_ip6(const struct ucred *, const struct in6_addr *);
607 int prison_check_ip6_locked(const struct prison *, const struct in6_addr *);
608 bool prison_saddrsel_ip6(struct ucred *, struct in6_addr *);
609 int prison_qcmp_v6(const void *, const void *);
610 bool prison_valid_v6(const void *);
611 #endif
612 int prison_check_af(struct ucred *cred, int af);
613 int prison_if(struct ucred *cred, const struct sockaddr *sa);
614 char *prison_name(struct prison *, struct prison *);
615 int prison_priv_check(struct ucred *cred, int priv);
616 int sysctl_jail_param(SYSCTL_HANDLER_ARGS);
617 unsigned prison_add_allow(const char *prefix, const char *name,
618     const char *prefix_descr, const char *descr);
619 void prison_add_vfs(struct vfsconf *vfsp);
620 void prison_racct_foreach(void (*callback)(struct racct *racct,
621     void *arg2, void *arg3), void (*pre)(void), void (*post)(void),
622     void *arg2, void *arg3);
623 struct prison_racct *prison_racct_find(const char *name);
624 void prison_racct_hold(struct prison_racct *prr);
625 void prison_racct_free(struct prison_racct *prr);
626 
627 #endif /* _KERNEL */
628 #endif /* !_SYS_JAIL_H_ */
629