2 * Copyright (c) 1999 Poul-Henning Kamp.
3 * Copyright (c) 2009 James Gritton.
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
9 * 1. Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
11 * 2. Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in the
13 * documentation and/or other materials provided with the distribution.
15 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
16 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
17 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
18 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
19 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
20 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
21 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
22 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
23 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
24 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
52 #define JAIL_API_VERSION 2
55 * For all xprison structs, always keep the pr_version an int and
56 * the first variable so userspace can easily distinguish them.
62 char pr_path[MAXPATHLEN];
63 char pr_host[MAXHOSTNAMELEN];
72 cpusetid_t pr_cpusetid;
73 char pr_path[MAXPATHLEN];
74 char pr_host[MAXHOSTNAMELEN];
75 char pr_name[MAXHOSTNAMELEN];
80 * sizeof(xprison) will be malloced + size needed for all
81 * IPv4 and IPv6 addesses. Offsets are based numbers of addresses.
83 struct in_addr pr_ip4[];
84 struct in6_addr pr_ip6[];
87 #define XPRISON_VERSION 3
89 #define PRISON_STATE_INVALID 0
90 #define PRISON_STATE_ALIVE 1
91 #define PRISON_STATE_DYING 2
94 * Flags for jail_set and jail_get.
96 #define JAIL_CREATE 0x01 /* Create jail if it doesn't exist */
97 #define JAIL_UPDATE 0x02 /* Update parameters of existing jail */
98 #define JAIL_ATTACH 0x04 /* Attach to jail upon creation */
99 #define JAIL_DYING 0x08 /* Allow getting a dying jail */
100 #define JAIL_SET_MASK 0x0f
101 #define JAIL_GET_MASK 0x08
103 #define JAIL_SYS_DISABLE 0
104 #define JAIL_SYS_NEW 1
105 #define JAIL_SYS_INHERIT 2
111 int jail(struct jail *);
112 int jail_set(struct iovec *, unsigned int, int);
113 int jail_get(struct iovec *, unsigned int, int);
114 int jail_attach(int);
115 int jail_remove(int);
119 #include <sys/queue.h>
120 #include <sys/sysctl.h>
121 #include <sys/lock.h>
122 #include <sys/mutex.h>
123 #include <sys/_task.h>
125 #define JAIL_MAX 999999
127 #ifdef MALLOC_DECLARE
128 MALLOC_DECLARE(M_PRISON);
132 #if defined(_KERNEL) || defined(_WANT_PRISON)
136 #define HOSTUUIDLEN 64
142 * This structure describes a prison. It is pointed to by all struct
143 * ucreds's of the inmates. pr_ref keeps track of them and is used to
144 * delete the struture when the last inmate is dead.
148 * (p) locked by pr_mtx
149 * (c) set only during creation before the structure is shared, no mutex
151 * (d) set only during destruction of jail, no mutex needed
154 TAILQ_ENTRY(prison) pr_list; /* (a) all prisons */
155 int pr_id; /* (c) prison id */
156 int pr_ref; /* (p) refcount */
157 int pr_uref; /* (p) user (alive) refcount */
158 unsigned pr_flags; /* (p) PR_* flags */
159 LIST_HEAD(, prison) pr_children; /* (a) list of child jails */
160 LIST_ENTRY(prison) pr_sibling; /* (a) next in parent's list */
161 struct prison *pr_parent; /* (c) containing jail */
163 struct task pr_task; /* (d) destroy task */
164 struct osd pr_osd; /* (p) additional data */
165 struct cpuset *pr_cpuset; /* (p) cpuset */
166 struct vnet *pr_vnet; /* (c) network stack */
167 struct vnode *pr_root; /* (c) vnode to rdir */
168 int pr_ip4s; /* (p) number of v4 IPs */
169 int pr_ip6s; /* (p) number of v6 IPs */
170 struct in_addr *pr_ip4; /* (p) v4 IPs of jail */
171 struct in6_addr *pr_ip6; /* (p) v6 IPs of jail */
172 struct prison_racct *pr_prison_racct; /* (c) racct jail proxy */
174 int pr_childcount; /* (a) number of child jails */
175 int pr_childmax; /* (p) maximum child jails */
176 unsigned pr_allow; /* (p) PR_ALLOW_* flags */
177 int pr_securelevel; /* (p) securelevel */
178 int pr_enforce_statfs; /* (p) statfs permission */
179 int pr_devfs_rsnum; /* (p) devfs ruleset */
181 unsigned long pr_hostid; /* (p) jail hostid */
182 char pr_name[MAXHOSTNAMELEN]; /* (p) admin jail name */
183 char pr_path[MAXPATHLEN]; /* (c) chroot path */
184 char pr_hostname[MAXHOSTNAMELEN]; /* (p) jail hostname */
185 char pr_domainname[MAXHOSTNAMELEN]; /* (p) jail domainname */
186 char pr_hostuuid[HOSTUUIDLEN]; /* (p) jail hostuuid */
189 struct prison_racct {
190 LIST_ENTRY(prison_racct) prr_next;
191 char prr_name[MAXHOSTNAMELEN];
193 struct racct *prr_racct;
195 #endif /* _KERNEL || _WANT_PRISON */
198 /* Flag bits set via options */
199 #define PR_PERSIST 0x00000001 /* Can exist without processes */
200 #define PR_HOST 0x00000002 /* Virtualize hostname et al */
201 #define PR_IP4_USER 0x00000004 /* Restrict IPv4 addresses */
202 #define PR_IP6_USER 0x00000008 /* Restrict IPv6 addresses */
203 #define PR_VNET 0x00000010 /* Virtual network stack */
204 #define PR_IP4_DISABLE 0x00000020 /* Disable IPv4 */
205 #define PR_IP6_DISABLE 0x00000040 /* Disable IPv6 */
206 #define PR_IP4_SADDRSEL 0x00000080 /* Do IPv4 src addr sel. or use the */
207 /* primary jail address. */
208 #define PR_IP6_SADDRSEL 0x00000100 /* Do IPv6 src addr sel. or use the */
209 /* primary jail address. */
211 /* Internal flag bits */
212 #define PR_REMOVE 0x01000000 /* In process of being removed */
213 #define PR_IP4 0x02000000 /* IPv4 restricted or disabled */
214 /* by this jail or an ancestor */
215 #define PR_IP6 0x04000000 /* IPv6 restricted or disabled */
216 /* by this jail or an ancestor */
218 /* Flags for pr_allow */
219 #define PR_ALLOW_SET_HOSTNAME 0x0001
220 #define PR_ALLOW_SYSVIPC 0x0002
221 #define PR_ALLOW_RAW_SOCKETS 0x0004
222 #define PR_ALLOW_CHFLAGS 0x0008
223 #define PR_ALLOW_MOUNT 0x0010
224 #define PR_ALLOW_QUOTAS 0x0020
225 #define PR_ALLOW_SOCKET_AF 0x0040
226 #define PR_ALLOW_MOUNT_DEVFS 0x0080
227 #define PR_ALLOW_MOUNT_NULLFS 0x0100
228 #define PR_ALLOW_MOUNT_ZFS 0x0200
229 #define PR_ALLOW_MOUNT_PROCFS 0x0400
230 #define PR_ALLOW_ALL 0x07ff
235 #define PR_METHOD_CREATE 0
236 #define PR_METHOD_GET 1
237 #define PR_METHOD_SET 2
238 #define PR_METHOD_CHECK 3
239 #define PR_METHOD_ATTACH 4
240 #define PR_MAXMETHOD 5
243 * Lock/unlock a prison.
244 * XXX These exist not so much for general convenience, but to be useable in
245 * the FOREACH_PRISON_DESCENDANT_LOCKED macro which can't handle them in
246 * non-function form as currently defined.
249 prison_lock(struct prison *pr)
252 mtx_lock(&pr->pr_mtx);
256 prison_unlock(struct prison *pr)
259 mtx_unlock(&pr->pr_mtx);
262 /* Traverse a prison's immediate children. */
263 #define FOREACH_PRISON_CHILD(ppr, cpr) \
264 LIST_FOREACH(cpr, &(ppr)->pr_children, pr_sibling)
267 * Preorder traversal of all of a prison's descendants.
268 * This ugly loop allows the macro to be followed by a single block
269 * as expected in a looping primitive.
271 #define FOREACH_PRISON_DESCENDANT(ppr, cpr, descend) \
272 for ((cpr) = (ppr), (descend) = 1; \
273 ((cpr) = (((descend) && !LIST_EMPTY(&(cpr)->pr_children)) \
274 ? LIST_FIRST(&(cpr)->pr_children) \
277 : (((descend) = LIST_NEXT(cpr, pr_sibling) != NULL) \
278 ? LIST_NEXT(cpr, pr_sibling) \
279 : (cpr)->pr_parent))));) \
285 * As above, but lock descendants on the way down and unlock on the way up.
287 #define FOREACH_PRISON_DESCENDANT_LOCKED(ppr, cpr, descend) \
288 for ((cpr) = (ppr), (descend) = 1; \
289 ((cpr) = (((descend) && !LIST_EMPTY(&(cpr)->pr_children)) \
290 ? LIST_FIRST(&(cpr)->pr_children) \
293 : ((prison_unlock(cpr), \
294 (descend) = LIST_NEXT(cpr, pr_sibling) != NULL) \
295 ? LIST_NEXT(cpr, pr_sibling) \
296 : (cpr)->pr_parent))));) \
297 if ((descend) ? (prison_lock(cpr), 0) : 1) \
302 * As above, but also keep track of the level descended to.
304 #define FOREACH_PRISON_DESCENDANT_LOCKED_LEVEL(ppr, cpr, descend, level)\
305 for ((cpr) = (ppr), (descend) = 1, (level) = 0; \
306 ((cpr) = (((descend) && !LIST_EMPTY(&(cpr)->pr_children)) \
307 ? (level++, LIST_FIRST(&(cpr)->pr_children)) \
310 : ((prison_unlock(cpr), \
311 (descend) = LIST_NEXT(cpr, pr_sibling) != NULL) \
312 ? LIST_NEXT(cpr, pr_sibling) \
313 : (level--, (cpr)->pr_parent)))));) \
314 if ((descend) ? (prison_lock(cpr), 0) : 1) \
319 * Attributes of the physical system, and the root of the jail tree.
321 extern struct prison prison0;
323 TAILQ_HEAD(prisonlist, prison);
324 extern struct prisonlist allprison;
325 extern struct sx allprison_lock;
328 * Sysctls to describe jail parameters.
330 SYSCTL_DECL(_security_jail_param);
332 #define SYSCTL_JAIL_PARAM(module, param, type, fmt, descr) \
333 SYSCTL_PROC(_security_jail_param ## module, OID_AUTO, param, \
334 (type) | CTLFLAG_MPSAFE, NULL, 0, sysctl_jail_param, fmt, descr)
335 #define SYSCTL_JAIL_PARAM_STRING(module, param, access, len, descr) \
336 SYSCTL_PROC(_security_jail_param ## module, OID_AUTO, param, \
337 CTLTYPE_STRING | CTLFLAG_MPSAFE | (access), NULL, len, \
338 sysctl_jail_param, "A", descr)
339 #define SYSCTL_JAIL_PARAM_STRUCT(module, param, access, len, fmt, descr)\
340 SYSCTL_PROC(_security_jail_param ## module, OID_AUTO, param, \
341 CTLTYPE_STRUCT | CTLFLAG_MPSAFE | (access), NULL, len, \
342 sysctl_jail_param, fmt, descr)
343 #define SYSCTL_JAIL_PARAM_NODE(module, descr) \
344 SYSCTL_NODE(_security_jail_param, OID_AUTO, module, 0, 0, descr)
345 #define SYSCTL_JAIL_PARAM_SUBNODE(parent, module, descr) \
346 SYSCTL_NODE(_security_jail_param_##parent, OID_AUTO, module, 0, 0, descr)
347 #define SYSCTL_JAIL_PARAM_SYS_NODE(module, access, descr) \
348 SYSCTL_JAIL_PARAM_NODE(module, descr); \
349 SYSCTL_JAIL_PARAM(_##module, , CTLTYPE_INT | (access), "E,jailsys", \
353 * Kernel support functions for jail().
359 int jailed(struct ucred *cred);
360 int jailed_without_vnet(struct ucred *);
361 void getcredhostname(struct ucred *, char *, size_t);
362 void getcreddomainname(struct ucred *, char *, size_t);
363 void getcredhostuuid(struct ucred *, char *, size_t);
364 void getcredhostid(struct ucred *, unsigned long *);
365 int prison_allow(struct ucred *, unsigned);
366 int prison_check(struct ucred *cred1, struct ucred *cred2);
367 int prison_owns_vnet(struct ucred *);
368 int prison_canseemount(struct ucred *cred, struct mount *mp);
369 void prison_enforce_statfs(struct ucred *cred, struct mount *mp,
371 struct prison *prison_find(int prid);
372 struct prison *prison_find_child(struct prison *, int);
373 struct prison *prison_find_name(struct prison *, const char *);
374 int prison_flag(struct ucred *, unsigned);
375 void prison_free(struct prison *pr);
376 void prison_free_locked(struct prison *pr);
377 void prison_hold(struct prison *pr);
378 void prison_hold_locked(struct prison *pr);
379 void prison_proc_hold(struct prison *);
380 void prison_proc_free(struct prison *);
381 int prison_ischild(struct prison *, struct prison *);
382 int prison_equal_ip4(struct prison *, struct prison *);
383 int prison_get_ip4(struct ucred *cred, struct in_addr *ia);
384 int prison_local_ip4(struct ucred *cred, struct in_addr *ia);
385 int prison_remote_ip4(struct ucred *cred, struct in_addr *ia);
386 int prison_check_ip4(struct ucred *cred, struct in_addr *ia);
387 int prison_saddrsel_ip4(struct ucred *, struct in_addr *);
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(struct ucred *, struct in6_addr *);
394 int prison_saddrsel_ip6(struct ucred *, struct in6_addr *);
396 int prison_check_af(struct ucred *cred, int af);
397 int prison_if(struct ucred *cred, struct sockaddr *sa);
398 char *prison_name(struct prison *, struct prison *);
399 int prison_priv_check(struct ucred *cred, int priv);
400 int sysctl_jail_param(SYSCTL_HANDLER_ARGS);
401 void prison_racct_foreach(void (*callback)(struct racct *racct,
402 void *arg2, void *arg3), void *arg2, void *arg3);
403 struct prison_racct *prison_racct_find(const char *name);
404 void prison_racct_hold(struct prison_racct *prr);
405 void prison_racct_free(struct prison_racct *prr);
408 #endif /* !_SYS_JAIL_H_ */