xref: /freebsd/sys/sys/jail.h (revision 19261079)
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 enum prison_state {
92     PRISON_STATE_INVALID = 0,	/* New prison, not ready to be seen */
93     PRISON_STATE_ALIVE,		/* Current prison, visible to all */
94     PRISON_STATE_DYING		/* Removed but holding resources, */
95 };				/* optionally visible. */
96 
97 /*
98  * Flags for jail_set and jail_get.
99  */
100 #define	JAIL_CREATE	0x01	/* Create jail if it doesn't exist */
101 #define	JAIL_UPDATE	0x02	/* Update parameters of existing jail */
102 #define	JAIL_ATTACH	0x04	/* Attach to jail upon creation */
103 #define	JAIL_DYING	0x08	/* Allow getting a dying jail */
104 #define	JAIL_SET_MASK	0x0f
105 #define	JAIL_GET_MASK	0x08
106 
107 #define	JAIL_SYS_DISABLE	0
108 #define	JAIL_SYS_NEW		1
109 #define	JAIL_SYS_INHERIT	2
110 
111 #ifndef _KERNEL
112 
113 struct iovec;
114 
115 __BEGIN_DECLS
116 int jail(struct jail *);
117 int jail_set(struct iovec *, unsigned int, int);
118 int jail_get(struct iovec *, unsigned int, int);
119 int jail_attach(int);
120 int jail_remove(int);
121 __END_DECLS
122 
123 #else /* _KERNEL */
124 
125 #include <sys/queue.h>
126 #include <sys/sysctl.h>
127 #include <sys/lock.h>
128 #include <sys/mutex.h>
129 #include <sys/_task.h>
130 
131 #define JAIL_MAX	999999
132 
133 #ifdef MALLOC_DECLARE
134 MALLOC_DECLARE(M_PRISON);
135 #endif
136 #endif /* _KERNEL */
137 
138 #if defined(_KERNEL) || defined(_WANT_PRISON)
139 
140 #include <sys/osd.h>
141 
142 #define	HOSTUUIDLEN	64
143 #define	DEFAULT_HOSTUUID	"00000000-0000-0000-0000-000000000000"
144 #define	OSRELEASELEN	32
145 
146 struct racct;
147 struct prison_racct;
148 
149 /*
150  * This structure describes a prison.  It is pointed to by all struct
151  * ucreds's of the inmates.  pr_ref keeps track of them and is used to
152  * delete the structure when the last inmate is dead.
153  *
154  * Lock key:
155  *   (a) allprison_lock
156  *   (c) set only during creation before the structure is shared, no mutex
157  *       required to read
158  *   (m) locked by pr_mtx
159  *   (p) locked by pr_mtx, and also at least shared allprison_lock required
160  *       to update
161  *   (q) locked by both pr_mtx and allprison_lock
162  *   (r) atomic via refcount(9), pr_mtx and allprison_lock required to
163  *       decrement to zero
164  */
165 struct prison {
166 	TAILQ_ENTRY(prison) pr_list;			/* (a) all prisons */
167 	int		 pr_id;				/* (c) prison id */
168 	volatile u_int	 pr_ref;			/* (r) refcount */
169 	volatile u_int	 pr_uref;			/* (r) user (alive) refcount */
170 	unsigned	 pr_flags;			/* (p) PR_* flags */
171 	LIST_HEAD(, prison) pr_children;		/* (a) list of child jails */
172 	LIST_ENTRY(prison) pr_sibling;			/* (a) next in parent's list */
173 	struct prison	*pr_parent;			/* (c) containing jail */
174 	struct mtx	 pr_mtx;
175 	struct task	 pr_task;			/* (c) destroy task */
176 	struct osd	 pr_osd;			/* (p) additional data */
177 	struct cpuset	*pr_cpuset;			/* (p) cpuset */
178 	struct vnet	*pr_vnet;			/* (c) network stack */
179 	struct vnode	*pr_root;			/* (c) vnode to rdir */
180 	int		 pr_ip4s;			/* (p) number of v4 IPs */
181 	int		 pr_ip6s;			/* (p) number of v6 IPs */
182 	struct in_addr	*pr_ip4;			/* (p) v4 IPs of jail */
183 	struct in6_addr	*pr_ip6;			/* (p) v6 IPs of jail */
184 	struct prison_racct *pr_prison_racct;		/* (c) racct jail proxy */
185 	void		*pr_sparep[3];
186 	int		 pr_childcount;			/* (a) number of child jails */
187 	int		 pr_childmax;			/* (p) maximum child jails */
188 	unsigned	 pr_allow;			/* (p) PR_ALLOW_* flags */
189 	int		 pr_securelevel;		/* (p) securelevel */
190 	int		 pr_enforce_statfs;		/* (p) statfs permission */
191 	int		 pr_devfs_rsnum;		/* (p) devfs ruleset */
192 	enum prison_state pr_state;			/* (q) state in life cycle */
193 	int		 pr_spare[2];
194 	int		 pr_osreldate;			/* (c) kern.osreldate value */
195 	unsigned long	 pr_hostid;			/* (p) jail hostid */
196 	char		 pr_name[MAXHOSTNAMELEN];	/* (p) admin jail name */
197 	char		 pr_path[MAXPATHLEN];		/* (c) chroot path */
198 	char		 pr_hostname[MAXHOSTNAMELEN];	/* (p) jail hostname */
199 	char		 pr_domainname[MAXHOSTNAMELEN];	/* (p) jail domainname */
200 	char		 pr_hostuuid[HOSTUUIDLEN];	/* (p) jail hostuuid */
201 	char		 pr_osrelease[OSRELEASELEN];	/* (c) kern.osrelease value */
202 };
203 
204 struct prison_racct {
205 	LIST_ENTRY(prison_racct) prr_next;
206 	char		prr_name[MAXHOSTNAMELEN];
207 	u_int		prr_refcount;
208 	struct racct	*prr_racct;
209 };
210 #endif /* _KERNEL || _WANT_PRISON */
211 
212 #ifdef _KERNEL
213 /* Flag bits set via options */
214 #define	PR_PERSIST	0x00000001	/* Can exist without processes */
215 #define	PR_HOST		0x00000002	/* Virtualize hostname et al */
216 #define	PR_IP4_USER	0x00000004	/* Restrict IPv4 addresses */
217 #define	PR_IP6_USER	0x00000008	/* Restrict IPv6 addresses */
218 #define	PR_VNET		0x00000010	/* Virtual network stack */
219 #define	PR_IP4_SADDRSEL	0x00000080	/* Do IPv4 src addr sel. or use the */
220 					/* primary jail address. */
221 #define	PR_IP6_SADDRSEL	0x00000100	/* Do IPv6 src addr sel. or use the */
222 					/* primary jail address. */
223 
224 /* Internal flag bits */
225 #define	PR_REMOVE	0x01000000	/* In process of being removed */
226 #define	PR_IP4		0x02000000	/* IPv4 restricted or disabled */
227 					/* by this jail or an ancestor */
228 #define	PR_IP6		0x04000000	/* IPv6 restricted or disabled */
229 					/* by this jail or an ancestor */
230 #define PR_COMPLETE_PROC 0x08000000	/* prison_complete called from */
231 					/* prison_proc_free, releases uref */
232 
233 /*
234  * Flags for pr_allow
235  * Bits not noted here may be used for dynamic allow.mount.xxxfs.
236  */
237 #define	PR_ALLOW_SET_HOSTNAME		0x00000001
238 #define	PR_ALLOW_SYSVIPC		0x00000002
239 #define	PR_ALLOW_RAW_SOCKETS		0x00000004
240 #define	PR_ALLOW_CHFLAGS		0x00000008
241 #define	PR_ALLOW_MOUNT			0x00000010
242 #define	PR_ALLOW_QUOTAS			0x00000020
243 #define	PR_ALLOW_SOCKET_AF		0x00000040
244 #define	PR_ALLOW_MLOCK			0x00000080
245 #define	PR_ALLOW_READ_MSGBUF		0x00000100
246 #define	PR_ALLOW_UNPRIV_DEBUG		0x00000200
247 #define	PR_ALLOW_SUSER			0x00000400
248 #define	PR_ALLOW_RESERVED_PORTS		0x00008000
249 #define	PR_ALLOW_KMEM_ACCESS		0x00010000	/* reserved, not used yet */
250 #define	PR_ALLOW_ALL_STATIC		0x000187ff
251 
252 /*
253  * PR_ALLOW_DIFFERENCES determines which flags are able to be
254  * different between the parent and child jail upon creation.
255  */
256 #define	PR_ALLOW_DIFFERENCES		(PR_ALLOW_UNPRIV_DEBUG)
257 
258 /*
259  * OSD methods
260  */
261 #define	PR_METHOD_CREATE	0
262 #define	PR_METHOD_GET		1
263 #define	PR_METHOD_SET		2
264 #define	PR_METHOD_CHECK		3
265 #define	PR_METHOD_ATTACH	4
266 #define	PR_METHOD_REMOVE	5
267 #define	PR_MAXMETHOD		6
268 
269 /*
270  * Lock/unlock a prison.
271  * XXX These exist not so much for general convenience, but to be useable in
272  *     the FOREACH_PRISON_DESCENDANT_LOCKED macro which can't handle them in
273  *     non-function form as currently defined.
274  */
275 static __inline void
276 prison_lock(struct prison *pr)
277 {
278 
279 	mtx_lock(&pr->pr_mtx);
280 }
281 
282 static __inline void
283 prison_unlock(struct prison *pr)
284 {
285 
286 	mtx_unlock(&pr->pr_mtx);
287 }
288 
289 /* Traverse a prison's immediate children. */
290 #define	FOREACH_PRISON_CHILD(ppr, cpr)					\
291 	LIST_FOREACH(cpr, &(ppr)->pr_children, pr_sibling)
292 
293 /*
294  * Preorder traversal of all of a prison's descendants.
295  * This ugly loop allows the macro to be followed by a single block
296  * as expected in a looping primitive.
297  */
298 #define	FOREACH_PRISON_DESCENDANT(ppr, cpr, descend)			\
299 	for ((cpr) = (ppr), (descend) = 1;				\
300 	    ((cpr) = (((descend) && !LIST_EMPTY(&(cpr)->pr_children))	\
301 	      ? LIST_FIRST(&(cpr)->pr_children)				\
302 	      : ((cpr) == (ppr)						\
303 		 ? NULL							\
304 		 : (((descend) = LIST_NEXT(cpr, pr_sibling) != NULL)	\
305 		    ? LIST_NEXT(cpr, pr_sibling)			\
306 		    : (cpr)->pr_parent))));)				\
307 		if (!(descend))						\
308 			;						\
309 		else
310 
311 /*
312  * As above, but lock descendants on the way down and unlock on the way up.
313  */
314 #define	FOREACH_PRISON_DESCENDANT_LOCKED(ppr, cpr, descend)		\
315 	for ((cpr) = (ppr), (descend) = 1;				\
316 	    ((cpr) = (((descend) && !LIST_EMPTY(&(cpr)->pr_children))	\
317 	      ? LIST_FIRST(&(cpr)->pr_children)				\
318 	      : ((cpr) == (ppr)						\
319 		 ? NULL							\
320 		 : ((prison_unlock(cpr),				\
321 		    (descend) = LIST_NEXT(cpr, pr_sibling) != NULL)	\
322 		    ? LIST_NEXT(cpr, pr_sibling)			\
323 		    : (cpr)->pr_parent))));)				\
324 		if ((descend) ? (prison_lock(cpr), 0) : 1)		\
325 			;						\
326 		else
327 
328 /*
329  * As above, but also keep track of the level descended to.
330  */
331 #define	FOREACH_PRISON_DESCENDANT_LOCKED_LEVEL(ppr, cpr, descend, level)\
332 	for ((cpr) = (ppr), (descend) = 1, (level) = 0;			\
333 	    ((cpr) = (((descend) && !LIST_EMPTY(&(cpr)->pr_children))	\
334 	      ? (level++, LIST_FIRST(&(cpr)->pr_children))		\
335 	      : ((cpr) == (ppr)						\
336 		 ? NULL							\
337 		 : ((prison_unlock(cpr),				\
338 		    (descend) = LIST_NEXT(cpr, pr_sibling) != NULL)	\
339 		    ? LIST_NEXT(cpr, pr_sibling)			\
340 		    : (level--, (cpr)->pr_parent)))));)			\
341 		if ((descend) ? (prison_lock(cpr), 0) : 1)		\
342 			;						\
343 		else
344 
345 /*
346  * Traverse a prison's descendants, visiting both preorder and postorder.
347  */
348 #define FOREACH_PRISON_DESCENDANT_PRE_POST(ppr, cpr, descend)		\
349 	for ((cpr) = (ppr), (descend) = 1;				\
350 	     ((cpr) = (descend)						\
351 	      ? ((descend) = !LIST_EMPTY(&(cpr)->pr_children))		\
352 		? LIST_FIRST(&(cpr)->pr_children)			\
353 		: (cpr)							\
354 	      : ((descend) = LIST_NEXT(cpr, pr_sibling) != NULL)	\
355 		? LIST_NEXT(cpr, pr_sibling)				\
356 		: cpr->pr_parent) != (ppr);)
357 
358 /*
359  * Attributes of the physical system, and the root of the jail tree.
360  */
361 extern struct	prison prison0;
362 
363 TAILQ_HEAD(prisonlist, prison);
364 extern struct	prisonlist allprison;
365 extern struct	sx allprison_lock;
366 
367 /*
368  * Sysctls to describe jail parameters.
369  */
370 SYSCTL_DECL(_security_jail_param);
371 
372 #define	SYSCTL_JAIL_PARAM(module, param, type, fmt, descr)		\
373     SYSCTL_PROC(_security_jail_param ## module, OID_AUTO, param,	\
374 	(type) | CTLFLAG_MPSAFE, NULL, 0, sysctl_jail_param, fmt, descr)
375 #define	SYSCTL_JAIL_PARAM_STRING(module, param, access, len, descr)	\
376     SYSCTL_PROC(_security_jail_param ## module, OID_AUTO, param,	\
377 	CTLTYPE_STRING | CTLFLAG_MPSAFE | (access), NULL, len,		\
378 	sysctl_jail_param, "A", descr)
379 #define	SYSCTL_JAIL_PARAM_STRUCT(module, param, access, len, fmt, descr)\
380     SYSCTL_PROC(_security_jail_param ## module, OID_AUTO, param,	\
381 	CTLTYPE_STRUCT | CTLFLAG_MPSAFE | (access), NULL, len,		\
382 	sysctl_jail_param, fmt, descr)
383 #define	SYSCTL_JAIL_PARAM_NODE(module, descr)				\
384     SYSCTL_NODE(_security_jail_param, OID_AUTO, module, CTLFLAG_MPSAFE,	\
385         0, descr)
386 #define	SYSCTL_JAIL_PARAM_SUBNODE(parent, module, descr)		\
387     SYSCTL_NODE(_security_jail_param_##parent, OID_AUTO, module, 	\
388         CTLFLAG_MPSAFE, 0, descr)
389 #define	SYSCTL_JAIL_PARAM_SYS_NODE(module, access, descr)		\
390     SYSCTL_JAIL_PARAM_NODE(module, descr);				\
391     SYSCTL_JAIL_PARAM(_##module, , CTLTYPE_INT | (access), "E,jailsys",	\
392 	descr)
393 
394 /*
395  * Kernel support functions for jail().
396  */
397 struct ucred;
398 struct mount;
399 struct sockaddr;
400 struct statfs;
401 struct vfsconf;
402 
403 /*
404  * Return 1 if the passed credential is in a jail, otherwise 0.
405  */
406 #define jailed(cred)	(cred->cr_prison != &prison0)
407 
408 int jailed_without_vnet(struct ucred *);
409 void getcredhostname(struct ucred *, char *, size_t);
410 void getcreddomainname(struct ucred *, char *, size_t);
411 void getcredhostuuid(struct ucred *, char *, size_t);
412 void getcredhostid(struct ucred *, unsigned long *);
413 void getjailname(struct ucred *cred, char *name, size_t len);
414 void prison0_init(void);
415 int prison_allow(struct ucred *, unsigned);
416 int prison_check(struct ucred *cred1, struct ucred *cred2);
417 int prison_owns_vnet(struct ucred *);
418 int prison_canseemount(struct ucred *cred, struct mount *mp);
419 void prison_enforce_statfs(struct ucred *cred, struct mount *mp,
420     struct statfs *sp);
421 struct prison *prison_find(int prid);
422 struct prison *prison_find_child(struct prison *, int);
423 struct prison *prison_find_name(struct prison *, const char *);
424 int prison_flag(struct ucred *, unsigned);
425 void prison_free(struct prison *pr);
426 void prison_free_locked(struct prison *pr);
427 void prison_hold(struct prison *pr);
428 void prison_hold_locked(struct prison *pr);
429 void prison_proc_hold(struct prison *);
430 void prison_proc_free(struct prison *);
431 void prison_set_allow(struct ucred *cred, unsigned flag, int enable);
432 int prison_ischild(struct prison *, struct prison *);
433 bool prison_isalive(struct prison *);
434 bool prison_isvalid(struct prison *);
435 int prison_equal_ip4(struct prison *, struct prison *);
436 int prison_get_ip4(struct ucred *cred, struct in_addr *ia);
437 int prison_local_ip4(struct ucred *cred, struct in_addr *ia);
438 int prison_remote_ip4(struct ucred *cred, struct in_addr *ia);
439 int prison_check_ip4(const struct ucred *, const struct in_addr *);
440 int prison_check_ip4_locked(const struct prison *, const struct in_addr *);
441 int prison_saddrsel_ip4(struct ucred *, struct in_addr *);
442 int prison_restrict_ip4(struct prison *, struct in_addr *);
443 int prison_qcmp_v4(const void *, const void *);
444 #ifdef INET6
445 int prison_equal_ip6(struct prison *, struct prison *);
446 int prison_get_ip6(struct ucred *, struct in6_addr *);
447 int prison_local_ip6(struct ucred *, struct in6_addr *, int);
448 int prison_remote_ip6(struct ucred *, struct in6_addr *);
449 int prison_check_ip6(const struct ucred *, const struct in6_addr *);
450 int prison_check_ip6_locked(const struct prison *, const struct in6_addr *);
451 int prison_saddrsel_ip6(struct ucred *, struct in6_addr *);
452 int prison_restrict_ip6(struct prison *, struct in6_addr *);
453 int prison_qcmp_v6(const void *, const void *);
454 #endif
455 int prison_check_af(struct ucred *cred, int af);
456 int prison_if(struct ucred *cred, const struct sockaddr *sa);
457 char *prison_name(struct prison *, struct prison *);
458 int prison_priv_check(struct ucred *cred, int priv);
459 int sysctl_jail_param(SYSCTL_HANDLER_ARGS);
460 unsigned prison_add_allow(const char *prefix, const char *name,
461     const char *prefix_descr, const char *descr);
462 void prison_add_vfs(struct vfsconf *vfsp);
463 void prison_racct_foreach(void (*callback)(struct racct *racct,
464     void *arg2, void *arg3), void (*pre)(void), void (*post)(void),
465     void *arg2, void *arg3);
466 struct prison_racct *prison_racct_find(const char *name);
467 void prison_racct_hold(struct prison_racct *prr);
468 void prison_racct_free(struct prison_racct *prr);
469 
470 #endif /* _KERNEL */
471 #endif /* !_SYS_JAIL_H_ */
472