2 * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
4 * Copyright (c) 1999 Poul-Henning Kamp.
5 * Copyright (c) 2009 James Gritton.
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
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.
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
35 #ifdef COMPAT_FREEBSD11
55 #define JAIL_API_VERSION 2
58 * For all xprison structs, always keep the pr_version an int and
59 * the first variable so userspace can easily distinguish them.
65 cpusetid_t pr_cpusetid;
66 char pr_path[MAXPATHLEN];
67 char pr_host[MAXHOSTNAMELEN];
68 char pr_name[MAXHOSTNAMELEN];
73 * sizeof(xprison) will be malloced + size needed for all
74 * IPv4 and IPv6 addesses. Offsets are based numbers of addresses.
76 struct in_addr pr_ip4[];
77 struct in6_addr pr_ip6[];
80 #define XPRISON_VERSION 3
82 #define PRISON_STATE_INVALID 0
83 #define PRISON_STATE_ALIVE 1
84 #define PRISON_STATE_DYING 2
85 #endif /* COMPAT_FREEBSD11 */
88 * Flags for jail_set and jail_get.
90 #define JAIL_CREATE 0x01 /* Create jail if it doesn't exist */
91 #define JAIL_UPDATE 0x02 /* Update parameters of existing jail */
92 #define JAIL_ATTACH 0x04 /* Attach to jail upon creation */
93 #define JAIL_DYING 0x08 /* Allow getting a dying jail */
94 #define JAIL_SET_MASK 0x0f
95 #define JAIL_GET_MASK 0x08
97 #define JAIL_SYS_DISABLE 0
98 #define JAIL_SYS_NEW 1
99 #define JAIL_SYS_INHERIT 2
105 int jail_set(struct iovec *, unsigned int, int);
106 int jail_get(struct iovec *, unsigned int, int);
107 int jail_attach(int);
108 int jail_remove(int);
112 #include <sys/queue.h>
113 #include <sys/sysctl.h>
114 #include <sys/lock.h>
115 #include <sys/mutex.h>
116 #include <sys/_task.h>
118 #define JAIL_MAX 999999
120 #ifdef MALLOC_DECLARE
121 MALLOC_DECLARE(M_PRISON);
125 #if defined(_KERNEL) || defined(_WANT_PRISON)
129 #define HOSTUUIDLEN 64
130 #define OSRELEASELEN 32
136 * This structure describes a prison. It is pointed to by all struct
137 * ucreds's of the inmates. pr_ref keeps track of them and is used to
138 * delete the struture when the last inmate is dead.
142 * (p) locked by pr_mtx
143 * (c) set only during creation before the structure is shared, no mutex
147 TAILQ_ENTRY(prison) pr_list; /* (a) all prisons */
148 int pr_id; /* (c) prison id */
149 int pr_ref; /* (p) refcount */
150 int pr_uref; /* (p) user (alive) refcount */
151 unsigned pr_flags; /* (p) PR_* flags */
152 LIST_HEAD(, prison) pr_children; /* (a) list of child jails */
153 LIST_ENTRY(prison) pr_sibling; /* (a) next in parent's list */
154 struct prison *pr_parent; /* (c) containing jail */
156 struct task pr_task; /* (c) destroy task */
157 struct osd pr_osd; /* (p) additional data */
158 struct cpuset *pr_cpuset; /* (p) cpuset */
159 struct vnet *pr_vnet; /* (c) network stack */
160 struct vnode *pr_root; /* (c) vnode to rdir */
161 int pr_ip4s; /* (p) number of v4 IPs */
162 int pr_ip6s; /* (p) number of v6 IPs */
163 struct in_addr *pr_ip4; /* (p) v4 IPs of jail */
164 struct in6_addr *pr_ip6; /* (p) v6 IPs of jail */
165 struct prison_racct *pr_prison_racct; /* (c) racct jail proxy */
167 int pr_childcount; /* (a) number of child jails */
168 int pr_childmax; /* (p) maximum child jails */
169 unsigned pr_allow; /* (p) PR_ALLOW_* flags */
170 int pr_securelevel; /* (p) securelevel */
171 int pr_enforce_statfs; /* (p) statfs permission */
172 int pr_devfs_rsnum; /* (p) devfs ruleset */
174 int pr_osreldate; /* (c) kern.osreldate value */
175 unsigned long pr_hostid; /* (p) jail hostid */
176 char pr_name[MAXHOSTNAMELEN]; /* (p) admin jail name */
177 char pr_path[MAXPATHLEN]; /* (c) chroot path */
178 char pr_hostname[MAXHOSTNAMELEN]; /* (p) jail hostname */
179 char pr_domainname[MAXHOSTNAMELEN]; /* (p) jail domainname */
180 char pr_hostuuid[HOSTUUIDLEN]; /* (p) jail hostuuid */
181 char pr_osrelease[OSRELEASELEN]; /* (c) kern.osrelease value */
184 struct prison_racct {
185 LIST_ENTRY(prison_racct) prr_next;
186 char prr_name[MAXHOSTNAMELEN];
188 struct racct *prr_racct;
190 #endif /* _KERNEL || _WANT_PRISON */
193 /* Flag bits set via options */
194 #define PR_PERSIST 0x00000001 /* Can exist without processes */
195 #define PR_HOST 0x00000002 /* Virtualize hostname et al */
196 #define PR_IP4_USER 0x00000004 /* Restrict IPv4 addresses */
197 #define PR_IP6_USER 0x00000008 /* Restrict IPv6 addresses */
198 #define PR_VNET 0x00000010 /* Virtual network stack */
199 #define PR_IP4_SADDRSEL 0x00000080 /* Do IPv4 src addr sel. or use the */
200 /* primary jail address. */
201 #define PR_IP6_SADDRSEL 0x00000100 /* Do IPv6 src addr sel. or use the */
202 /* primary jail address. */
204 /* Internal flag bits */
205 #define PR_IP4 0x02000000 /* IPv4 restricted or disabled */
206 /* by this jail or an ancestor */
207 #define PR_IP6 0x04000000 /* IPv6 restricted or disabled */
208 /* by this jail or an ancestor */
212 * Bits not noted here may be used for dynamic allow.mount.xxxfs.
214 #define PR_ALLOW_SET_HOSTNAME 0x00000001
215 #define PR_ALLOW_SYSVIPC 0x00000002
216 #define PR_ALLOW_RAW_SOCKETS 0x00000004
217 #define PR_ALLOW_CHFLAGS 0x00000008
218 #define PR_ALLOW_MOUNT 0x00000010
219 #define PR_ALLOW_QUOTAS 0x00000020
220 #define PR_ALLOW_SOCKET_AF 0x00000040
221 #define PR_ALLOW_MLOCK 0x00000080
222 #define PR_ALLOW_RESERVED_PORTS 0x00008000
223 #define PR_ALLOW_KMEM_ACCESS 0x00010000 /* reserved, not used yet */
224 #define PR_ALLOW_ALL_STATIC 0x000180ff
229 #define PR_METHOD_CREATE 0
230 #define PR_METHOD_GET 1
231 #define PR_METHOD_SET 2
232 #define PR_METHOD_CHECK 3
233 #define PR_METHOD_ATTACH 4
234 #define PR_METHOD_REMOVE 5
235 #define PR_MAXMETHOD 6
238 * Lock/unlock a prison.
239 * XXX These exist not so much for general convenience, but to be useable in
240 * the FOREACH_PRISON_DESCENDANT_LOCKED macro which can't handle them in
241 * non-function form as currently defined.
244 prison_lock(struct prison *pr)
247 mtx_lock(&pr->pr_mtx);
251 prison_unlock(struct prison *pr)
254 mtx_unlock(&pr->pr_mtx);
257 /* Traverse a prison's immediate children. */
258 #define FOREACH_PRISON_CHILD(ppr, cpr) \
259 LIST_FOREACH(cpr, &(ppr)->pr_children, pr_sibling)
262 * Preorder traversal of all of a prison's descendants.
263 * This ugly loop allows the macro to be followed by a single block
264 * as expected in a looping primitive.
266 #define FOREACH_PRISON_DESCENDANT(ppr, cpr, descend) \
267 for ((cpr) = (ppr), (descend) = 1; \
268 ((cpr) = (((descend) && !LIST_EMPTY(&(cpr)->pr_children)) \
269 ? LIST_FIRST(&(cpr)->pr_children) \
272 : (((descend) = LIST_NEXT(cpr, pr_sibling) != NULL) \
273 ? LIST_NEXT(cpr, pr_sibling) \
274 : (cpr)->pr_parent))));) \
280 * As above, but lock descendants on the way down and unlock on the way up.
282 #define FOREACH_PRISON_DESCENDANT_LOCKED(ppr, cpr, descend) \
283 for ((cpr) = (ppr), (descend) = 1; \
284 ((cpr) = (((descend) && !LIST_EMPTY(&(cpr)->pr_children)) \
285 ? LIST_FIRST(&(cpr)->pr_children) \
288 : ((prison_unlock(cpr), \
289 (descend) = LIST_NEXT(cpr, pr_sibling) != NULL) \
290 ? LIST_NEXT(cpr, pr_sibling) \
291 : (cpr)->pr_parent))));) \
292 if ((descend) ? (prison_lock(cpr), 0) : 1) \
297 * As above, but also keep track of the level descended to.
299 #define FOREACH_PRISON_DESCENDANT_LOCKED_LEVEL(ppr, cpr, descend, level)\
300 for ((cpr) = (ppr), (descend) = 1, (level) = 0; \
301 ((cpr) = (((descend) && !LIST_EMPTY(&(cpr)->pr_children)) \
302 ? (level++, LIST_FIRST(&(cpr)->pr_children)) \
305 : ((prison_unlock(cpr), \
306 (descend) = LIST_NEXT(cpr, pr_sibling) != NULL) \
307 ? LIST_NEXT(cpr, pr_sibling) \
308 : (level--, (cpr)->pr_parent)))));) \
309 if ((descend) ? (prison_lock(cpr), 0) : 1) \
314 * Attributes of the physical system, and the root of the jail tree.
316 extern struct prison prison0;
318 TAILQ_HEAD(prisonlist, prison);
319 extern struct prisonlist allprison;
320 extern struct sx allprison_lock;
323 * Sysctls to describe jail parameters.
325 SYSCTL_DECL(_security_jail_param);
327 #define SYSCTL_JAIL_PARAM(module, param, type, fmt, descr) \
328 SYSCTL_PROC(_security_jail_param ## module, OID_AUTO, param, \
329 (type) | CTLFLAG_MPSAFE, NULL, 0, sysctl_jail_param, fmt, descr)
330 #define SYSCTL_JAIL_PARAM_STRING(module, param, access, len, descr) \
331 SYSCTL_PROC(_security_jail_param ## module, OID_AUTO, param, \
332 CTLTYPE_STRING | CTLFLAG_MPSAFE | (access), NULL, len, \
333 sysctl_jail_param, "A", descr)
334 #define SYSCTL_JAIL_PARAM_STRUCT(module, param, access, len, fmt, descr)\
335 SYSCTL_PROC(_security_jail_param ## module, OID_AUTO, param, \
336 CTLTYPE_STRUCT | CTLFLAG_MPSAFE | (access), NULL, len, \
337 sysctl_jail_param, fmt, descr)
338 #define SYSCTL_JAIL_PARAM_NODE(module, descr) \
339 SYSCTL_NODE(_security_jail_param, OID_AUTO, module, 0, 0, descr)
340 #define SYSCTL_JAIL_PARAM_SUBNODE(parent, module, descr) \
341 SYSCTL_NODE(_security_jail_param_##parent, OID_AUTO, module, 0, 0, descr)
342 #define SYSCTL_JAIL_PARAM_SYS_NODE(module, access, descr) \
343 SYSCTL_JAIL_PARAM_NODE(module, descr); \
344 SYSCTL_JAIL_PARAM(_##module, , CTLTYPE_INT | (access), "E,jailsys", \
348 * Kernel support functions for jail().
355 int jailed(struct ucred *cred);
356 int jailed_without_vnet(struct ucred *);
357 void getcredhostname(struct ucred *, char *, size_t);
358 void getcreddomainname(struct ucred *, char *, size_t);
359 void getcredhostuuid(struct ucred *, char *, size_t);
360 void getcredhostid(struct ucred *, unsigned long *);
361 void prison0_init(void);
362 int prison_allow(struct ucred *, unsigned);
363 int prison_check(struct ucred *cred1, struct ucred *cred2);
364 int prison_owns_vnet(struct ucred *);
365 int prison_canseemount(struct ucred *cred, struct mount *mp);
366 void prison_enforce_statfs(struct ucred *cred, struct mount *mp,
368 struct prison *prison_find(int prid);
369 struct prison *prison_find_child(struct prison *, int);
370 struct prison *prison_find_name(struct prison *, const char *);
371 int prison_flag(struct ucred *, unsigned);
372 void prison_free(struct prison *pr);
373 void prison_free_locked(struct prison *pr);
374 void prison_hold(struct prison *pr);
375 void prison_hold_locked(struct prison *pr);
376 void prison_proc_hold(struct prison *);
377 void prison_proc_free(struct prison *);
378 int prison_ischild(struct prison *, struct prison *);
379 int prison_equal_ip4(struct prison *, struct prison *);
380 int prison_get_ip4(struct ucred *cred, struct in_addr *ia);
381 int prison_local_ip4(struct ucred *cred, struct in_addr *ia);
382 int prison_remote_ip4(struct ucred *cred, struct in_addr *ia);
383 int prison_check_ip4(const struct ucred *, const struct in_addr *);
384 int prison_check_ip4_locked(const struct prison *, const struct in_addr *);
385 int prison_saddrsel_ip4(struct ucred *, struct in_addr *);
386 int prison_restrict_ip4(struct prison *, struct in_addr *);
387 int prison_qcmp_v4(const void *, const void *);
389 int prison_equal_ip6(struct prison *, struct prison *);
390 int prison_get_ip6(struct ucred *, struct in6_addr *);
391 int prison_local_ip6(struct ucred *, struct in6_addr *, int);
392 int prison_remote_ip6(struct ucred *, struct in6_addr *);
393 int prison_check_ip6(const struct ucred *, const struct in6_addr *);
394 int prison_check_ip6_locked(const struct prison *, const struct in6_addr *);
395 int prison_saddrsel_ip6(struct ucred *, struct in6_addr *);
396 int prison_restrict_ip6(struct prison *, struct in6_addr *);
397 int prison_qcmp_v6(const void *, const void *);
399 int prison_check_af(struct ucred *cred, int af);
400 int prison_if(struct ucred *cred, struct sockaddr *sa);
401 char *prison_name(struct prison *, struct prison *);
402 int prison_priv_check(struct ucred *cred, int priv);
403 int sysctl_jail_param(SYSCTL_HANDLER_ARGS);
404 unsigned prison_add_allow(const char *prefix, const char *name,
405 const char *prefix_descr, const char *descr);
406 void prison_add_vfs(struct vfsconf *vfsp);
407 void prison_racct_foreach(void (*callback)(struct racct *racct,
408 void *arg2, void *arg3), void (*pre)(void), void (*post)(void),
409 void *arg2, void *arg3);
410 struct prison_racct *prison_racct_find(const char *name);
411 void prison_racct_hold(struct prison_racct *prr);
412 void prison_racct_free(struct prison_racct *prr);
415 #endif /* !_SYS_JAIL_H_ */