xref: /freebsd/sys/sys/jail.h (revision ed31b3f4)
160727d8bSWarner Losh /*-
24d846d26SWarner Losh  * SPDX-License-Identifier: BSD-2-Clause
3c4e20cadSPedro F. Giffuni  *
47074cfa2SJamie Gritton  * Copyright (c) 1999 Poul-Henning Kamp.
5b38ff370SJamie Gritton  * Copyright (c) 2009 James Gritton.
67074cfa2SJamie Gritton  * All rights reserved.
77074cfa2SJamie Gritton  *
87074cfa2SJamie Gritton  * Redistribution and use in source and binary forms, with or without
97074cfa2SJamie Gritton  * modification, are permitted provided that the following conditions
107074cfa2SJamie Gritton  * are met:
117074cfa2SJamie Gritton  * 1. Redistributions of source code must retain the above copyright
127074cfa2SJamie Gritton  *    notice, this list of conditions and the following disclaimer.
137074cfa2SJamie Gritton  * 2. Redistributions in binary form must reproduce the above copyright
147074cfa2SJamie Gritton  *    notice, this list of conditions and the following disclaimer in the
157074cfa2SJamie Gritton  *    documentation and/or other materials provided with the distribution.
167074cfa2SJamie Gritton  *
177074cfa2SJamie Gritton  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
187074cfa2SJamie Gritton  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
197074cfa2SJamie Gritton  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
207074cfa2SJamie Gritton  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
217074cfa2SJamie Gritton  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
227074cfa2SJamie Gritton  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
237074cfa2SJamie Gritton  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
247074cfa2SJamie Gritton  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
257074cfa2SJamie Gritton  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
267074cfa2SJamie Gritton  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
277074cfa2SJamie Gritton  * SUCH DAMAGE.
2875c13541SPoul-Henning Kamp  */
2975c13541SPoul-Henning Kamp 
3075c13541SPoul-Henning Kamp #ifndef _SYS_JAIL_H_
3175c13541SPoul-Henning Kamp #define _SYS_JAIL_H_
3275c13541SPoul-Henning Kamp 
33413628a7SBjoern A. Zeeb #ifdef _KERNEL
34413628a7SBjoern A. Zeeb struct jail_v0 {
35978f8d93SPoul-Henning Kamp 	u_int32_t	version;
3675c13541SPoul-Henning Kamp 	char		*path;
3775c13541SPoul-Henning Kamp 	char		*hostname;
3875c13541SPoul-Henning Kamp 	u_int32_t	ip_number;
3975c13541SPoul-Henning Kamp };
40413628a7SBjoern A. Zeeb #endif
4175c13541SPoul-Henning Kamp 
42413628a7SBjoern A. Zeeb struct jail {
43413628a7SBjoern A. Zeeb 	uint32_t	version;
44413628a7SBjoern A. Zeeb 	char		*path;
45413628a7SBjoern A. Zeeb 	char		*hostname;
46413628a7SBjoern A. Zeeb 	char		*jailname;
47413628a7SBjoern A. Zeeb 	uint32_t	ip4s;
48413628a7SBjoern A. Zeeb 	uint32_t	ip6s;
49413628a7SBjoern A. Zeeb 	struct in_addr	*ip4;
50413628a7SBjoern A. Zeeb 	struct in6_addr	*ip6;
51413628a7SBjoern A. Zeeb };
52413628a7SBjoern A. Zeeb #define	JAIL_API_VERSION	2
53413628a7SBjoern A. Zeeb 
54413628a7SBjoern A. Zeeb /*
55413628a7SBjoern A. Zeeb  * For all xprison structs, always keep the pr_version an int and
56413628a7SBjoern A. Zeeb  * the first variable so userspace can easily distinguish them.
57413628a7SBjoern A. Zeeb  */
58c542c43eSJamie Gritton #ifndef _KERNEL
59c542c43eSJamie Gritton struct xprison_v1 {
60c542c43eSJamie Gritton 	int		 pr_version;
61c542c43eSJamie Gritton 	int		 pr_id;
62c542c43eSJamie Gritton 	char		 pr_path[MAXPATHLEN];
63c542c43eSJamie Gritton 	char		 pr_host[MAXHOSTNAMELEN];
64c542c43eSJamie Gritton 	u_int32_t	 pr_ip;
65c542c43eSJamie Gritton };
66c542c43eSJamie Gritton #endif
67c542c43eSJamie Gritton 
68413628a7SBjoern A. Zeeb struct xprison {
69413628a7SBjoern A. Zeeb 	int		 pr_version;
70413628a7SBjoern A. Zeeb 	int		 pr_id;
71413628a7SBjoern A. Zeeb 	int		 pr_state;
72413628a7SBjoern A. Zeeb 	cpusetid_t	 pr_cpusetid;
73413628a7SBjoern A. Zeeb 	char		 pr_path[MAXPATHLEN];
74413628a7SBjoern A. Zeeb 	char		 pr_host[MAXHOSTNAMELEN];
75413628a7SBjoern A. Zeeb 	char		 pr_name[MAXHOSTNAMELEN];
76413628a7SBjoern A. Zeeb 	uint32_t	 pr_ip4s;
77413628a7SBjoern A. Zeeb 	uint32_t	 pr_ip6s;
78413628a7SBjoern A. Zeeb #if 0
79413628a7SBjoern A. Zeeb 	/*
80413628a7SBjoern A. Zeeb 	 * sizeof(xprison) will be malloced + size needed for all
81413628a7SBjoern A. Zeeb 	 * IPv4 and IPv6 addesses. Offsets are based numbers of addresses.
82413628a7SBjoern A. Zeeb 	 */
83413628a7SBjoern A. Zeeb 	struct in_addr	 pr_ip4[];
84413628a7SBjoern A. Zeeb 	struct in6_addr	 pr_ip6[];
85413628a7SBjoern A. Zeeb #endif
86413628a7SBjoern A. Zeeb };
87413628a7SBjoern A. Zeeb #define	XPRISON_VERSION		3
88413628a7SBjoern A. Zeeb 
891158508aSJamie Gritton enum prison_state {
901158508aSJamie Gritton     PRISON_STATE_INVALID = 0,	/* New prison, not ready to be seen */
911158508aSJamie Gritton     PRISON_STATE_ALIVE,		/* Current prison, visible to all */
921158508aSJamie Gritton     PRISON_STATE_DYING		/* Removed but holding resources, */
931158508aSJamie Gritton };				/* optionally visible. */
94413628a7SBjoern A. Zeeb 
95b38ff370SJamie Gritton /*
96b38ff370SJamie Gritton  * Flags for jail_set and jail_get.
97b38ff370SJamie Gritton  */
98b38ff370SJamie Gritton #define	JAIL_CREATE	0x01	/* Create jail if it doesn't exist */
99b38ff370SJamie Gritton #define	JAIL_UPDATE	0x02	/* Update parameters of existing jail */
100b38ff370SJamie Gritton #define	JAIL_ATTACH	0x04	/* Attach to jail upon creation */
101b38ff370SJamie Gritton #define	JAIL_DYING	0x08	/* Allow getting a dying jail */
102ed31b3f4SJamie Gritton #define	JAIL_SET_MASK	0x0f	/* JAIL_DYING is deprecated/ignored here */
103b38ff370SJamie Gritton #define	JAIL_GET_MASK	0x08
104fd7a8150SMike Barcroft 
1057cbf7213SJamie Gritton #define	JAIL_SYS_DISABLE	0
1067cbf7213SJamie Gritton #define	JAIL_SYS_NEW		1
1077cbf7213SJamie Gritton #define	JAIL_SYS_INHERIT	2
1087cbf7213SJamie Gritton 
109664a31e4SPeter Wemm #ifndef _KERNEL
110d8bd3ac4SPoul-Henning Kamp 
111b38ff370SJamie Gritton struct iovec;
112b38ff370SJamie Gritton 
11308204289SJamie Gritton __BEGIN_DECLS
114c542c43eSJamie Gritton int jail(struct jail *);
115b38ff370SJamie Gritton int jail_set(struct iovec *, unsigned int, int);
116b38ff370SJamie Gritton int jail_get(struct iovec *, unsigned int, int);
117fd7a8150SMike Barcroft int jail_attach(int);
118b38ff370SJamie Gritton int jail_remove(int);
11908204289SJamie Gritton __END_DECLS
120d8bd3ac4SPoul-Henning Kamp 
121664a31e4SPeter Wemm #else /* _KERNEL */
12275c13541SPoul-Henning Kamp 
123607aa34eSBruce Evans #include <sys/queue.h>
124b38ff370SJamie Gritton #include <sys/sysctl.h>
1250304c731SJamie Gritton #include <sys/lock.h>
1260304c731SJamie Gritton #include <sys/mutex.h>
127b3059e09SRobert Watson #include <sys/_task.h>
12801137630SRobert Watson 
1292110d913SXin LI #define JAIL_MAX	999999
1302110d913SXin LI 
13175c13541SPoul-Henning Kamp #ifdef MALLOC_DECLARE
13275c13541SPoul-Henning Kamp MALLOC_DECLARE(M_PRISON);
13375c13541SPoul-Henning Kamp #endif
134e69f1fa2SPawel Jakub Dawidek #endif /* _KERNEL */
13575c13541SPoul-Henning Kamp 
1361ba4a712SPawel Jakub Dawidek #if defined(_KERNEL) || defined(_WANT_PRISON)
1371ba4a712SPawel Jakub Dawidek 
1381ba4a712SPawel Jakub Dawidek #include <sys/osd.h>
1391ba4a712SPawel Jakub Dawidek 
14076ca6f88SJamie Gritton #define	HOSTUUIDLEN	64
1412d741f33SKyle Evans #define	DEFAULT_HOSTUUID	"00000000-0000-0000-0000-000000000000"
142b96bd95bSIan Lepore #define	OSRELEASELEN	32
14376ca6f88SJamie Gritton 
144097055e2SEdward Tomasz Napierala struct racct;
145a7ad07bfSEdward Tomasz Napierala struct prison_racct;
146097055e2SEdward Tomasz Napierala 
147eb8dcdeaSGleb Smirnoff typedef enum {
148eb8dcdeaSGleb Smirnoff 	PR_INET		= 0,
149eb8dcdeaSGleb Smirnoff 	PR_INET6	= 1,
150eb8dcdeaSGleb Smirnoff 	PR_FAMILY_MAX	= 2,
151eb8dcdeaSGleb Smirnoff } pr_family_t;
152eb8dcdeaSGleb Smirnoff 
15375c13541SPoul-Henning Kamp /*
15475c13541SPoul-Henning Kamp  * This structure describes a prison.  It is pointed to by all struct
15591421ba2SRobert Watson  * ucreds's of the inmates.  pr_ref keeps track of them and is used to
15617db4b52SGordon Bergling  * delete the structure when the last inmate is dead.
15791421ba2SRobert Watson  *
15801137630SRobert Watson  * Lock key:
159dc68a633SPawel Jakub Dawidek  *   (a) allprison_lock
1605ecb5444SMateusz Guzik  *   (A) allproc_lock
1616754ae25SJamie Gritton  *   (c) set only during creation before the structure is shared, no mutex
1626754ae25SJamie Gritton  *       required to read
16343c27348SJamie Gritton  *   (m) locked by pr_mtx
16443c27348SJamie Gritton  *   (p) locked by pr_mtx, and also at least shared allprison_lock required
16543c27348SJamie Gritton  *       to update
1661158508aSJamie Gritton  *   (q) locked by both pr_mtx and allprison_lock
167f7496dcaSJamie Gritton  *   (r) atomic via refcount(9), pr_mtx and allprison_lock required to
168f7496dcaSJamie Gritton  *       decrement to zero
169eb8dcdeaSGleb Smirnoff  *   (n) read access granted with the network epoch
17075c13541SPoul-Henning Kamp  */
17175c13541SPoul-Henning Kamp struct prison {
172b38ff370SJamie Gritton 	TAILQ_ENTRY(prison) pr_list;			/* (a) all prisons */
173fd7a8150SMike Barcroft 	int		 pr_id;				/* (c) prison id */
1746754ae25SJamie Gritton 	volatile u_int	 pr_ref;			/* (r) refcount */
1756754ae25SJamie Gritton 	volatile u_int	 pr_uref;			/* (r) user (alive) refcount */
176b38ff370SJamie Gritton 	unsigned	 pr_flags;			/* (p) PR_* flags */
1770304c731SJamie Gritton 	LIST_HEAD(, prison) pr_children;		/* (a) list of child jails */
1785ecb5444SMateusz Guzik 	LIST_HEAD(, proc) pr_proclist;			/* (A) list of jailed processes */
1790304c731SJamie Gritton 	LIST_ENTRY(prison) pr_sibling;			/* (a) next in parent's list */
180ca006477SJamie Gritton 	struct prison	*pr_parent;			/* (c) containing jail */
181ca006477SJamie Gritton 	struct mtx	 pr_mtx;
18273d9e52dSJamie Gritton 	struct task	 pr_task;			/* (c) destroy task */
183ca006477SJamie Gritton 	struct osd	 pr_osd;			/* (p) additional data */
184ca006477SJamie Gritton 	struct cpuset	*pr_cpuset;			/* (p) cpuset */
185ca006477SJamie Gritton 	struct vnet	*pr_vnet;			/* (c) network stack */
186ca006477SJamie Gritton 	struct vnode	*pr_root;			/* (c) vnode to rdir */
187eb8dcdeaSGleb Smirnoff 	struct prison_ip  *pr_addrs[PR_FAMILY_MAX];	/* (p,n) IPs of jail */
188a7ad07bfSEdward Tomasz Napierala 	struct prison_racct *pr_prison_racct;		/* (c) racct jail proxy */
189097055e2SEdward Tomasz Napierala 	void		*pr_sparep[3];
190b97457e2SJamie Gritton 	int		 pr_childcount;			/* (a) number of child jails */
191ca006477SJamie Gritton 	int		 pr_childmax;			/* (p) maximum child jails */
1920304c731SJamie Gritton 	unsigned	 pr_allow;			/* (p) PR_ALLOW_* flags */
193ca006477SJamie Gritton 	int		 pr_securelevel;		/* (p) securelevel */
1940304c731SJamie Gritton 	int		 pr_enforce_statfs;		/* (p) statfs permission */
1950cc207a6SMartin Matuska 	int		 pr_devfs_rsnum;		/* (p) devfs ruleset */
1961158508aSJamie Gritton 	enum prison_state pr_state;			/* (q) state in life cycle */
19788175af8SRick Macklem 	volatile int	 pr_exportcnt;			/* (r) count of mount exports */
19888175af8SRick Macklem 	int		 pr_spare;
199b96bd95bSIan Lepore 	int		 pr_osreldate;			/* (c) kern.osreldate value */
200ca006477SJamie Gritton 	unsigned long	 pr_hostid;			/* (p) jail hostid */
201ca006477SJamie Gritton 	char		 pr_name[MAXHOSTNAMELEN];	/* (p) admin jail name */
202ca006477SJamie Gritton 	char		 pr_path[MAXPATHLEN];		/* (c) chroot path */
203ca006477SJamie Gritton 	char		 pr_hostname[MAXHOSTNAMELEN];	/* (p) jail hostname */
204c1f19219SJamie Gritton 	char		 pr_domainname[MAXHOSTNAMELEN];	/* (p) jail domainname */
205c1f19219SJamie Gritton 	char		 pr_hostuuid[HOSTUUIDLEN];	/* (p) jail hostuuid */
206b96bd95bSIan Lepore 	char		 pr_osrelease[OSRELEASELEN];	/* (c) kern.osrelease value */
20775c13541SPoul-Henning Kamp };
208a7ad07bfSEdward Tomasz Napierala 
209a7ad07bfSEdward Tomasz Napierala struct prison_racct {
210a7ad07bfSEdward Tomasz Napierala 	LIST_ENTRY(prison_racct) prr_next;
211a7ad07bfSEdward Tomasz Napierala 	char		prr_name[MAXHOSTNAMELEN];
212a7ad07bfSEdward Tomasz Napierala 	u_int		prr_refcount;
213a7ad07bfSEdward Tomasz Napierala 	struct racct	*prr_racct;
214a7ad07bfSEdward Tomasz Napierala };
215e69f1fa2SPawel Jakub Dawidek #endif /* _KERNEL || _WANT_PRISON */
21675c13541SPoul-Henning Kamp 
217e69f1fa2SPawel Jakub Dawidek #ifdef _KERNEL
2180304c731SJamie Gritton /* Flag bits set via options */
219b38ff370SJamie Gritton #define	PR_PERSIST	0x00000001	/* Can exist without processes */
22076ca6f88SJamie Gritton #define	PR_HOST		0x00000002	/* Virtualize hostname et al */
2217cbf7213SJamie Gritton #define	PR_IP4_USER	0x00000004	/* Restrict IPv4 addresses */
2227cbf7213SJamie Gritton #define	PR_IP6_USER	0x00000008	/* Restrict IPv6 addresses */
223679e1390SJamie Gritton #define	PR_VNET		0x00000010	/* Virtual network stack */
224592bcae8SBjoern A. Zeeb #define	PR_IP4_SADDRSEL	0x00000080	/* Do IPv4 src addr sel. or use the */
225592bcae8SBjoern A. Zeeb 					/* primary jail address. */
226592bcae8SBjoern A. Zeeb #define	PR_IP6_SADDRSEL	0x00000100	/* Do IPv6 src addr sel. or use the */
227592bcae8SBjoern A. Zeeb 					/* primary jail address. */
2280304c731SJamie Gritton 
2290304c731SJamie Gritton /* Internal flag bits */
230cc7b7306SJamie Gritton #define	PR_REMOVE	0x01000000	/* In process of being removed */
2317cbf7213SJamie Gritton #define	PR_IP4		0x02000000	/* IPv4 restricted or disabled */
2327cbf7213SJamie Gritton 					/* by this jail or an ancestor */
2337cbf7213SJamie Gritton #define	PR_IP6		0x04000000	/* IPv6 restricted or disabled */
2347cbf7213SJamie Gritton 					/* by this jail or an ancestor */
2351158508aSJamie Gritton #define PR_COMPLETE_PROC 0x08000000	/* prison_complete called from */
2361158508aSJamie Gritton 					/* prison_proc_free, releases uref */
2370304c731SJamie Gritton 
2380e5c6bd4SJamie Gritton /*
2390e5c6bd4SJamie Gritton  * Flags for pr_allow
2400e5c6bd4SJamie Gritton  * Bits not noted here may be used for dynamic allow.mount.xxxfs.
2410e5c6bd4SJamie Gritton  */
24203af441cSAlexander Leidinger #define	PR_ALLOW_SET_HOSTNAME		0x00000001
24303af441cSAlexander Leidinger #define	PR_ALLOW_SYSVIPC		0x00000002
24403af441cSAlexander Leidinger #define	PR_ALLOW_RAW_SOCKETS		0x00000004
24503af441cSAlexander Leidinger #define	PR_ALLOW_CHFLAGS		0x00000008
24603af441cSAlexander Leidinger #define	PR_ALLOW_MOUNT			0x00000010
24703af441cSAlexander Leidinger #define	PR_ALLOW_QUOTAS			0x00000020
24803af441cSAlexander Leidinger #define	PR_ALLOW_SOCKET_AF		0x00000040
249ccd6ac9fSAntoine Brodin #define	PR_ALLOW_MLOCK			0x00000080
250b19d66fdSJamie Gritton #define	PR_ALLOW_READ_MSGBUF		0x00000100
251b3079544SJamie Gritton #define	PR_ALLOW_UNPRIV_DEBUG		0x00000200
25205e1e482SMariusz Zaborski #define	PR_ALLOW_SUSER			0x00000400
25303af441cSAlexander Leidinger #define	PR_ALLOW_RESERVED_PORTS		0x00008000
25403af441cSAlexander Leidinger #define	PR_ALLOW_KMEM_ACCESS		0x00010000	/* reserved, not used yet */
255bba7a2e8SRick Macklem #define	PR_ALLOW_NFSD			0x00020000
256cb48780dSShawn Webb #define	PR_ALLOW_EXTATTR		0x00040000
257cb48780dSShawn Webb #define	PR_ALLOW_ALL_STATIC		0x000787ff
258b3079544SJamie Gritton 
259b3079544SJamie Gritton /*
260b3079544SJamie Gritton  * PR_ALLOW_DIFFERENCES determines which flags are able to be
261b3079544SJamie Gritton  * different between the parent and child jail upon creation.
262b3079544SJamie Gritton  */
263b3079544SJamie Gritton #define	PR_ALLOW_DIFFERENCES		(PR_ALLOW_UNPRIV_DEBUG)
264b38ff370SJamie Gritton 
265b38ff370SJamie Gritton /*
266b38ff370SJamie Gritton  * OSD methods
267b38ff370SJamie Gritton  */
268b38ff370SJamie Gritton #define	PR_METHOD_CREATE	0
269b38ff370SJamie Gritton #define	PR_METHOD_GET		1
270b38ff370SJamie Gritton #define	PR_METHOD_SET		2
271b38ff370SJamie Gritton #define	PR_METHOD_CHECK		3
272b38ff370SJamie Gritton #define	PR_METHOD_ATTACH	4
273cc5fd8c7SJamie Gritton #define	PR_METHOD_REMOVE	5
274cc5fd8c7SJamie Gritton #define	PR_MAXMETHOD		6
275b38ff370SJamie Gritton 
276b38ff370SJamie Gritton /*
2770304c731SJamie Gritton  * Lock/unlock a prison.
2780304c731SJamie Gritton  * XXX These exist not so much for general convenience, but to be useable in
2790304c731SJamie Gritton  *     the FOREACH_PRISON_DESCENDANT_LOCKED macro which can't handle them in
2800304c731SJamie Gritton  *     non-function form as currently defined.
2815bdee2c5SRobert Watson  */
2820304c731SJamie Gritton static __inline void
prison_lock(struct prison * pr)2830304c731SJamie Gritton prison_lock(struct prison *pr)
2840304c731SJamie Gritton {
2850304c731SJamie Gritton 
2860304c731SJamie Gritton 	mtx_lock(&pr->pr_mtx);
2870304c731SJamie Gritton }
2880304c731SJamie Gritton 
2890304c731SJamie Gritton static __inline void
prison_unlock(struct prison * pr)2900304c731SJamie Gritton prison_unlock(struct prison *pr)
2910304c731SJamie Gritton {
2920304c731SJamie Gritton 
2930304c731SJamie Gritton 	mtx_unlock(&pr->pr_mtx);
2940304c731SJamie Gritton }
2950304c731SJamie Gritton 
2960304c731SJamie Gritton /* Traverse a prison's immediate children. */
2970304c731SJamie Gritton #define	FOREACH_PRISON_CHILD(ppr, cpr)					\
2980304c731SJamie Gritton 	LIST_FOREACH(cpr, &(ppr)->pr_children, pr_sibling)
2990304c731SJamie Gritton 
3000304c731SJamie Gritton /*
3010304c731SJamie Gritton  * Preorder traversal of all of a prison's descendants.
3020304c731SJamie Gritton  * This ugly loop allows the macro to be followed by a single block
3030304c731SJamie Gritton  * as expected in a looping primitive.
3040304c731SJamie Gritton  */
3050304c731SJamie Gritton #define	FOREACH_PRISON_DESCENDANT(ppr, cpr, descend)			\
3060304c731SJamie Gritton 	for ((cpr) = (ppr), (descend) = 1;				\
3070304c731SJamie Gritton 	    ((cpr) = (((descend) && !LIST_EMPTY(&(cpr)->pr_children))	\
3080304c731SJamie Gritton 	      ? LIST_FIRST(&(cpr)->pr_children)				\
3090304c731SJamie Gritton 	      : ((cpr) == (ppr)						\
3100304c731SJamie Gritton 		 ? NULL							\
3110304c731SJamie Gritton 		 : (((descend) = LIST_NEXT(cpr, pr_sibling) != NULL)	\
3120304c731SJamie Gritton 		    ? LIST_NEXT(cpr, pr_sibling)			\
3130304c731SJamie Gritton 		    : (cpr)->pr_parent))));)				\
3140304c731SJamie Gritton 		if (!(descend))						\
3150304c731SJamie Gritton 			;						\
3160304c731SJamie Gritton 		else
3170304c731SJamie Gritton 
3180304c731SJamie Gritton /*
3190304c731SJamie Gritton  * As above, but lock descendants on the way down and unlock on the way up.
3200304c731SJamie Gritton  */
3210304c731SJamie Gritton #define	FOREACH_PRISON_DESCENDANT_LOCKED(ppr, cpr, descend)		\
3220304c731SJamie Gritton 	for ((cpr) = (ppr), (descend) = 1;				\
3230304c731SJamie Gritton 	    ((cpr) = (((descend) && !LIST_EMPTY(&(cpr)->pr_children))	\
3240304c731SJamie Gritton 	      ? LIST_FIRST(&(cpr)->pr_children)				\
3250304c731SJamie Gritton 	      : ((cpr) == (ppr)						\
3260304c731SJamie Gritton 		 ? NULL							\
3270304c731SJamie Gritton 		 : ((prison_unlock(cpr),				\
3280304c731SJamie Gritton 		    (descend) = LIST_NEXT(cpr, pr_sibling) != NULL)	\
3290304c731SJamie Gritton 		    ? LIST_NEXT(cpr, pr_sibling)			\
3300304c731SJamie Gritton 		    : (cpr)->pr_parent))));)				\
3310304c731SJamie Gritton 		if ((descend) ? (prison_lock(cpr), 0) : 1)		\
3320304c731SJamie Gritton 			;						\
3330304c731SJamie Gritton 		else
3340304c731SJamie Gritton 
3350304c731SJamie Gritton /*
336b97457e2SJamie Gritton  * As above, but also keep track of the level descended to.
337b97457e2SJamie Gritton  */
338b97457e2SJamie Gritton #define	FOREACH_PRISON_DESCENDANT_LOCKED_LEVEL(ppr, cpr, descend, level)\
339b97457e2SJamie Gritton 	for ((cpr) = (ppr), (descend) = 1, (level) = 0;			\
340b97457e2SJamie Gritton 	    ((cpr) = (((descend) && !LIST_EMPTY(&(cpr)->pr_children))	\
341b97457e2SJamie Gritton 	      ? (level++, LIST_FIRST(&(cpr)->pr_children))		\
342b97457e2SJamie Gritton 	      : ((cpr) == (ppr)						\
343b97457e2SJamie Gritton 		 ? NULL							\
344b97457e2SJamie Gritton 		 : ((prison_unlock(cpr),				\
345b97457e2SJamie Gritton 		    (descend) = LIST_NEXT(cpr, pr_sibling) != NULL)	\
346b97457e2SJamie Gritton 		    ? LIST_NEXT(cpr, pr_sibling)			\
347b97457e2SJamie Gritton 		    : (level--, (cpr)->pr_parent)))));)			\
348b97457e2SJamie Gritton 		if ((descend) ? (prison_lock(cpr), 0) : 1)		\
349b97457e2SJamie Gritton 			;						\
350b97457e2SJamie Gritton 		else
351b97457e2SJamie Gritton 
352b97457e2SJamie Gritton /*
353811e27faSJamie Gritton  * Traverse a prison's descendants, visiting both preorder and postorder.
354811e27faSJamie Gritton  */
355811e27faSJamie Gritton #define FOREACH_PRISON_DESCENDANT_PRE_POST(ppr, cpr, descend)		\
356811e27faSJamie Gritton 	for ((cpr) = (ppr), (descend) = 1;				\
357811e27faSJamie Gritton 	     ((cpr) = (descend)						\
358811e27faSJamie Gritton 	      ? ((descend) = !LIST_EMPTY(&(cpr)->pr_children))		\
359811e27faSJamie Gritton 		? LIST_FIRST(&(cpr)->pr_children)			\
360811e27faSJamie Gritton 		: (cpr)							\
361811e27faSJamie Gritton 	      : ((descend) = LIST_NEXT(cpr, pr_sibling) != NULL)	\
362811e27faSJamie Gritton 		? LIST_NEXT(cpr, pr_sibling)				\
363811e27faSJamie Gritton 		: cpr->pr_parent) != (ppr);)
364811e27faSJamie Gritton 
365811e27faSJamie Gritton /*
3660304c731SJamie Gritton  * Attributes of the physical system, and the root of the jail tree.
3670304c731SJamie Gritton  */
3680304c731SJamie Gritton extern struct	prison prison0;
3695bdee2c5SRobert Watson 
370b38ff370SJamie Gritton TAILQ_HEAD(prisonlist, prison);
371fd7a8150SMike Barcroft extern struct	prisonlist allprison;
372dc68a633SPawel Jakub Dawidek extern struct	sx allprison_lock;
373fd7a8150SMike Barcroft 
37491421ba2SRobert Watson /*
375b38ff370SJamie Gritton  * Sysctls to describe jail parameters.
376b38ff370SJamie Gritton  */
377b38ff370SJamie Gritton SYSCTL_DECL(_security_jail_param);
378b38ff370SJamie Gritton 
379b38ff370SJamie Gritton #define	SYSCTL_JAIL_PARAM(module, param, type, fmt, descr)		\
380b38ff370SJamie Gritton     SYSCTL_PROC(_security_jail_param ## module, OID_AUTO, param,	\
381b38ff370SJamie Gritton 	(type) | CTLFLAG_MPSAFE, NULL, 0, sysctl_jail_param, fmt, descr)
382b38ff370SJamie Gritton #define	SYSCTL_JAIL_PARAM_STRING(module, param, access, len, descr)	\
383b38ff370SJamie Gritton     SYSCTL_PROC(_security_jail_param ## module, OID_AUTO, param,	\
384b38ff370SJamie Gritton 	CTLTYPE_STRING | CTLFLAG_MPSAFE | (access), NULL, len,		\
385b38ff370SJamie Gritton 	sysctl_jail_param, "A", descr)
386b38ff370SJamie Gritton #define	SYSCTL_JAIL_PARAM_STRUCT(module, param, access, len, fmt, descr)\
387b38ff370SJamie Gritton     SYSCTL_PROC(_security_jail_param ## module, OID_AUTO, param,	\
388b38ff370SJamie Gritton 	CTLTYPE_STRUCT | CTLFLAG_MPSAFE | (access), NULL, len,		\
389b38ff370SJamie Gritton 	sysctl_jail_param, fmt, descr)
390b38ff370SJamie Gritton #define	SYSCTL_JAIL_PARAM_NODE(module, descr)				\
3917029da5cSPawel Biernacki     SYSCTL_NODE(_security_jail_param, OID_AUTO, module, CTLFLAG_MPSAFE,	\
3927029da5cSPawel Biernacki         0, descr)
393bf3db8aaSMartin Matuska #define	SYSCTL_JAIL_PARAM_SUBNODE(parent, module, descr)		\
3947029da5cSPawel Biernacki     SYSCTL_NODE(_security_jail_param_##parent, OID_AUTO, module, 	\
3957029da5cSPawel Biernacki         CTLFLAG_MPSAFE, 0, descr)
3967cbf7213SJamie Gritton #define	SYSCTL_JAIL_PARAM_SYS_NODE(module, access, descr)		\
3977cbf7213SJamie Gritton     SYSCTL_JAIL_PARAM_NODE(module, descr);				\
3987cbf7213SJamie Gritton     SYSCTL_JAIL_PARAM(_##module, , CTLTYPE_INT | (access), "E,jailsys",	\
3997cbf7213SJamie Gritton 	descr)
400b38ff370SJamie Gritton 
401b38ff370SJamie Gritton /*
40291421ba2SRobert Watson  * Kernel support functions for jail().
40391421ba2SRobert Watson  */
40491421ba2SRobert Watson struct ucred;
405f08df373SRobert Watson struct mount;
40691421ba2SRobert Watson struct sockaddr;
407820a0de9SPawel Jakub Dawidek struct statfs;
4080e5c6bd4SJamie Gritton struct vfsconf;
409e6081fe8SMateusz Guzik 
410e6081fe8SMateusz Guzik /*
411e6081fe8SMateusz Guzik  * Return 1 if the passed credential is in a jail, otherwise 0.
412e6081fe8SMateusz Guzik  */
413e6081fe8SMateusz Guzik #define jailed(cred)	(cred->cr_prison != &prison0)
414e6081fe8SMateusz Guzik 
4150b0ae2e4SMina Galić bool jailed_without_vnet(struct ucred *);
4167455b100SJamie Gritton void getcredhostname(struct ucred *, char *, size_t);
4177455b100SJamie Gritton void getcreddomainname(struct ucred *, char *, size_t);
4187455b100SJamie Gritton void getcredhostuuid(struct ucred *, char *, size_t);
4197455b100SJamie Gritton void getcredhostid(struct ucred *, unsigned long *);
4203f8bc99cSKristof Provost void getjailname(struct ucred *cred, char *name, size_t len);
421b96bd95bSIan Lepore void prison0_init(void);
4220b0ae2e4SMina Galić bool prison_allow(struct ucred *, unsigned);
423789f12feSAlfred Perlstein int prison_check(struct ucred *cred1, struct ucred *cred2);
424bba7a2e8SRick Macklem bool prison_check_nfsd(struct ucred *cred);
4250b0ae2e4SMina Galić bool prison_owns_vnet(struct ucred *);
426820a0de9SPawel Jakub Dawidek int prison_canseemount(struct ucred *cred, struct mount *mp);
427820a0de9SPawel Jakub Dawidek void prison_enforce_statfs(struct ucred *cred, struct mount *mp,
428820a0de9SPawel Jakub Dawidek     struct statfs *sp);
42954b369c1SPawel Jakub Dawidek struct prison *prison_find(int prid);
4300304c731SJamie Gritton struct prison *prison_find_child(struct prison *, int);
4310304c731SJamie Gritton struct prison *prison_find_name(struct prison *, const char *);
4320b0ae2e4SMina Galić bool prison_flag(struct ucred *, unsigned);
433789f12feSAlfred Perlstein void prison_free(struct prison *pr);
4341ba4a712SPawel Jakub Dawidek void prison_free_locked(struct prison *pr);
435789f12feSAlfred Perlstein void prison_hold(struct prison *pr);
4361ba4a712SPawel Jakub Dawidek void prison_hold_locked(struct prison *pr);
437413628a7SBjoern A. Zeeb void prison_proc_hold(struct prison *);
438413628a7SBjoern A. Zeeb void prison_proc_free(struct prison *);
4395ecb5444SMateusz Guzik void prison_proc_link(struct prison *, struct proc *);
4405ecb5444SMateusz Guzik void prison_proc_unlink(struct prison *, struct proc *);
4415ecb5444SMateusz Guzik void prison_proc_iterate(struct prison *, void (*)(struct proc *, void *), void *);
4420fe74ae6SJamie Gritton void prison_set_allow(struct ucred *cred, unsigned flag, int enable);
4430b0ae2e4SMina Galić bool prison_ischild(struct prison *, struct prison *);
444eb8dcdeaSGleb Smirnoff bool prison_isalive(const struct prison *);
44576ad42abSJamie Gritton bool prison_isvalid(struct prison *);
446eb8dcdeaSGleb Smirnoff #if defined(INET) || defined(INET6)
447eb8dcdeaSGleb Smirnoff int prison_ip_check(const struct prison *, const pr_family_t, const void *);
448eb8dcdeaSGleb Smirnoff const void *prison_ip_get0(const struct prison *, const pr_family_t);
449eb8dcdeaSGleb Smirnoff u_int prison_ip_cnt(const struct prison *, const pr_family_t);
450eb8dcdeaSGleb Smirnoff #endif
451eb8dcdeaSGleb Smirnoff #ifdef INET
4520b0ae2e4SMina Galić bool prison_equal_ip4(struct prison *, struct prison *);
4531cecba0fSBjoern A. Zeeb int prison_get_ip4(struct ucred *cred, struct in_addr *ia);
454413628a7SBjoern A. Zeeb int prison_local_ip4(struct ucred *cred, struct in_addr *ia);
455413628a7SBjoern A. Zeeb int prison_remote_ip4(struct ucred *cred, struct in_addr *ia);
4560d168b8dSGleb Smirnoff int prison_check_ip4(const struct ucred *, const struct in_addr *);
4570ce1624dSStephen J. Kiernan int prison_check_ip4_locked(const struct prison *, const struct in_addr *);
4580b0ae2e4SMina Galić bool prison_saddrsel_ip4(struct ucred *, struct in_addr *);
4590ce1624dSStephen J. Kiernan int prison_qcmp_v4(const void *, const void *);
460eb8dcdeaSGleb Smirnoff bool prison_valid_v4(const void *);
461eb8dcdeaSGleb Smirnoff #endif
462413628a7SBjoern A. Zeeb #ifdef INET6
4630b0ae2e4SMina Galić bool prison_equal_ip6(struct prison *, struct prison *);
4641cecba0fSBjoern A. Zeeb int prison_get_ip6(struct ucred *, struct in6_addr *);
465413628a7SBjoern A. Zeeb int prison_local_ip6(struct ucred *, struct in6_addr *, int);
466413628a7SBjoern A. Zeeb int prison_remote_ip6(struct ucred *, struct in6_addr *);
4670ce1624dSStephen J. Kiernan int prison_check_ip6(const struct ucred *, const struct in6_addr *);
4680ce1624dSStephen J. Kiernan int prison_check_ip6_locked(const struct prison *, const struct in6_addr *);
4690b0ae2e4SMina Galić bool prison_saddrsel_ip6(struct ucred *, struct in6_addr *);
4700ce1624dSStephen J. Kiernan int prison_qcmp_v6(const void *, const void *);
471eb8dcdeaSGleb Smirnoff bool prison_valid_v6(const void *);
472413628a7SBjoern A. Zeeb #endif
473ca04ba64SJamie Gritton int prison_check_af(struct ucred *cred, int af);
474c83dda36SAlexander V. Chernikov int prison_if(struct ucred *cred, const struct sockaddr *sa);
4750304c731SJamie Gritton char *prison_name(struct prison *, struct prison *);
476800c9408SRobert Watson int prison_priv_check(struct ucred *cred, int priv);
477e4cd31ddSJeff Roberson int sysctl_jail_param(SYSCTL_HANDLER_ARGS);
4780a172404SJamie Gritton unsigned prison_add_allow(const char *prefix, const char *name,
4790a172404SJamie Gritton     const char *prefix_descr, const char *descr);
4800e5c6bd4SJamie Gritton void prison_add_vfs(struct vfsconf *vfsp);
481097055e2SEdward Tomasz Napierala void prison_racct_foreach(void (*callback)(struct racct *racct,
482a63513d7SEdward Tomasz Napierala     void *arg2, void *arg3), void (*pre)(void), void (*post)(void),
483a63513d7SEdward Tomasz Napierala     void *arg2, void *arg3);
484a7ad07bfSEdward Tomasz Napierala struct prison_racct *prison_racct_find(const char *name);
485a7ad07bfSEdward Tomasz Napierala void prison_racct_hold(struct prison_racct *prr);
486a7ad07bfSEdward Tomasz Napierala void prison_racct_free(struct prison_racct *prr);
48791421ba2SRobert Watson 
488e69f1fa2SPawel Jakub Dawidek #endif /* _KERNEL */
48975c13541SPoul-Henning Kamp #endif /* !_SYS_JAIL_H_ */
490