FreeBSD/Linux Kernel Cross Reference
sys/sys/jail.h
1 /*-
2 * Copyright (c) 1999 Poul-Henning Kamp.
3 * Copyright (c) 2009 James Gritton.
4 * All rights reserved.
5 *
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
8 * are met:
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.
14 *
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
25 * SUCH DAMAGE.
26 *
27 * $FreeBSD: releng/8.1/sys/sys/jail.h 202891 2010-01-23 16:40:35Z bz $
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 #define PRISON_STATE_INVALID 0
90 #define PRISON_STATE_ALIVE 1
91 #define PRISON_STATE_DYING 2
92
93 /*
94 * Flags for jail_set and jail_get.
95 */
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
102
103 #define JAIL_SYS_DISABLE 0
104 #define JAIL_SYS_NEW 1
105 #define JAIL_SYS_INHERIT 2
106
107 #ifndef _KERNEL
108
109 struct iovec;
110
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);
116
117 #else /* _KERNEL */
118
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>
124
125 #define JAIL_MAX 999999
126
127 #ifdef MALLOC_DECLARE
128 MALLOC_DECLARE(M_PRISON);
129 #endif
130 #endif /* _KERNEL */
131
132 #if defined(_KERNEL) || defined(_WANT_PRISON)
133
134 #include <sys/osd.h>
135
136 #define HOSTUUIDLEN 64
137
138 /*
139 * This structure describes a prison. It is pointed to by all struct
140 * ucreds's of the inmates. pr_ref keeps track of them and is used to
141 * delete the struture when the last inmate is dead.
142 *
143 * Lock key:
144 * (a) allprison_lock
145 * (p) locked by pr_mtx
146 * (c) set only during creation before the structure is shared, no mutex
147 * required to read
148 * (d) set only during destruction of jail, no mutex needed
149 */
150 struct prison {
151 TAILQ_ENTRY(prison) pr_list; /* (a) all prisons */
152 int pr_id; /* (c) prison id */
153 int pr_ref; /* (p) refcount */
154 int pr_uref; /* (p) user (alive) refcount */
155 unsigned pr_flags; /* (p) PR_* flags */
156 LIST_HEAD(, prison) pr_children; /* (a) list of child jails */
157 LIST_ENTRY(prison) pr_sibling; /* (a) next in parent's list */
158 struct prison *pr_parent; /* (c) containing jail */
159 struct mtx pr_mtx;
160 struct task pr_task; /* (d) destroy task */
161 struct osd pr_osd; /* (p) additional data */
162 struct cpuset *pr_cpuset; /* (p) cpuset */
163 struct vnet *pr_vnet; /* (c) network stack */
164 struct vnode *pr_root; /* (c) vnode to rdir */
165 int pr_ip4s; /* (p) number of v4 IPs */
166 int pr_ip6s; /* (p) number of v6 IPs */
167 struct in_addr *pr_ip4; /* (p) v4 IPs of jail */
168 struct in6_addr *pr_ip6; /* (p) v6 IPs of jail */
169 void *pr_sparep[4];
170 int pr_childcount; /* (a) number of child jails */
171 int pr_childmax; /* (p) maximum child jails */
172 unsigned pr_allow; /* (p) PR_ALLOW_* flags */
173 int pr_securelevel; /* (p) securelevel */
174 int pr_enforce_statfs; /* (p) statfs permission */
175 int pr_spare[5];
176 unsigned long pr_hostid; /* (p) jail hostid */
177 char pr_name[MAXHOSTNAMELEN]; /* (p) admin jail name */
178 char pr_path[MAXPATHLEN]; /* (c) chroot path */
179 char pr_hostname[MAXHOSTNAMELEN]; /* (p) jail hostname */
180 char pr_domainname[MAXHOSTNAMELEN]; /* (p) jail domainname */
181 char pr_hostuuid[HOSTUUIDLEN]; /* (p) jail hostuuid */
182 };
183 #endif /* _KERNEL || _WANT_PRISON */
184
185 #ifdef _KERNEL
186 /* Flag bits set via options */
187 #define PR_PERSIST 0x00000001 /* Can exist without processes */
188 #define PR_HOST 0x00000002 /* Virtualize hostname et al */
189 #define PR_IP4_USER 0x00000004 /* Restrict IPv4 addresses */
190 #define PR_IP6_USER 0x00000008 /* Restrict IPv6 addresses */
191 #define PR_VNET 0x00000010 /* Virtual network stack */
192 #define PR_IP4_DISABLE 0x00000020 /* Disable IPv4 */
193 #define PR_IP6_DISABLE 0x00000040 /* Disable IPv6 */
194 #define PR_IP4_SADDRSEL 0x00000080 /* Do IPv4 src addr sel. or use the */
195 /* primary jail address. */
196 #define PR_IP6_SADDRSEL 0x00000100 /* Do IPv6 src addr sel. or use the */
197 /* primary jail address. */
198
199 /* Internal flag bits */
200 #define PR_REMOVE 0x01000000 /* In process of being removed */
201 #define PR_IP4 0x02000000 /* IPv4 restricted or disabled */
202 /* by this jail or an ancestor */
203 #define PR_IP6 0x04000000 /* IPv6 restricted or disabled */
204 /* by this jail or an ancestor */
205
206 /* Flags for pr_allow */
207 #define PR_ALLOW_SET_HOSTNAME 0x0001
208 #define PR_ALLOW_SYSVIPC 0x0002
209 #define PR_ALLOW_RAW_SOCKETS 0x0004
210 #define PR_ALLOW_CHFLAGS 0x0008
211 #define PR_ALLOW_MOUNT 0x0010
212 #define PR_ALLOW_QUOTAS 0x0020
213 #define PR_ALLOW_SOCKET_AF 0x0040
214 #define PR_ALLOW_ALL 0x007f
215
216 /*
217 * OSD methods
218 */
219 #define PR_METHOD_CREATE 0
220 #define PR_METHOD_GET 1
221 #define PR_METHOD_SET 2
222 #define PR_METHOD_CHECK 3
223 #define PR_METHOD_ATTACH 4
224 #define PR_MAXMETHOD 5
225
226 /*
227 * Lock/unlock a prison.
228 * XXX These exist not so much for general convenience, but to be useable in
229 * the FOREACH_PRISON_DESCENDANT_LOCKED macro which can't handle them in
230 * non-function form as currently defined.
231 */
232 static __inline void
233 prison_lock(struct prison *pr)
234 {
235
236 mtx_lock(&pr->pr_mtx);
237 }
238
239 static __inline void
240 prison_unlock(struct prison *pr)
241 {
242
243 mtx_unlock(&pr->pr_mtx);
244 }
245
246 /* Traverse a prison's immediate children. */
247 #define FOREACH_PRISON_CHILD(ppr, cpr) \
248 LIST_FOREACH(cpr, &(ppr)->pr_children, pr_sibling)
249
250 /*
251 * Preorder traversal of all of a prison's descendants.
252 * This ugly loop allows the macro to be followed by a single block
253 * as expected in a looping primitive.
254 */
255 #define FOREACH_PRISON_DESCENDANT(ppr, cpr, descend) \
256 for ((cpr) = (ppr), (descend) = 1; \
257 ((cpr) = (((descend) && !LIST_EMPTY(&(cpr)->pr_children)) \
258 ? LIST_FIRST(&(cpr)->pr_children) \
259 : ((cpr) == (ppr) \
260 ? NULL \
261 : (((descend) = LIST_NEXT(cpr, pr_sibling) != NULL) \
262 ? LIST_NEXT(cpr, pr_sibling) \
263 : (cpr)->pr_parent))));) \
264 if (!(descend)) \
265 ; \
266 else
267
268 /*
269 * As above, but lock descendants on the way down and unlock on the way up.
270 */
271 #define FOREACH_PRISON_DESCENDANT_LOCKED(ppr, cpr, descend) \
272 for ((cpr) = (ppr), (descend) = 1; \
273 ((cpr) = (((descend) && !LIST_EMPTY(&(cpr)->pr_children)) \
274 ? LIST_FIRST(&(cpr)->pr_children) \
275 : ((cpr) == (ppr) \
276 ? NULL \
277 : ((prison_unlock(cpr), \
278 (descend) = LIST_NEXT(cpr, pr_sibling) != NULL) \
279 ? LIST_NEXT(cpr, pr_sibling) \
280 : (cpr)->pr_parent))));) \
281 if ((descend) ? (prison_lock(cpr), 0) : 1) \
282 ; \
283 else
284
285 /*
286 * As above, but also keep track of the level descended to.
287 */
288 #define FOREACH_PRISON_DESCENDANT_LOCKED_LEVEL(ppr, cpr, descend, level)\
289 for ((cpr) = (ppr), (descend) = 1, (level) = 0; \
290 ((cpr) = (((descend) && !LIST_EMPTY(&(cpr)->pr_children)) \
291 ? (level++, LIST_FIRST(&(cpr)->pr_children)) \
292 : ((cpr) == (ppr) \
293 ? NULL \
294 : ((prison_unlock(cpr), \
295 (descend) = LIST_NEXT(cpr, pr_sibling) != NULL) \
296 ? LIST_NEXT(cpr, pr_sibling) \
297 : (level--, (cpr)->pr_parent)))));) \
298 if ((descend) ? (prison_lock(cpr), 0) : 1) \
299 ; \
300 else
301
302 /*
303 * Attributes of the physical system, and the root of the jail tree.
304 */
305 extern struct prison prison0;
306
307 TAILQ_HEAD(prisonlist, prison);
308 extern struct prisonlist allprison;
309 extern struct sx allprison_lock;
310
311 /*
312 * Sysctls to describe jail parameters.
313 */
314 SYSCTL_DECL(_security_jail_param);
315
316 #define SYSCTL_JAIL_PARAM(module, param, type, fmt, descr) \
317 SYSCTL_PROC(_security_jail_param ## module, OID_AUTO, param, \
318 (type) | CTLFLAG_MPSAFE, NULL, 0, sysctl_jail_param, fmt, descr)
319 #define SYSCTL_JAIL_PARAM_STRING(module, param, access, len, descr) \
320 SYSCTL_PROC(_security_jail_param ## module, OID_AUTO, param, \
321 CTLTYPE_STRING | CTLFLAG_MPSAFE | (access), NULL, len, \
322 sysctl_jail_param, "A", descr)
323 #define SYSCTL_JAIL_PARAM_STRUCT(module, param, access, len, fmt, descr)\
324 SYSCTL_PROC(_security_jail_param ## module, OID_AUTO, param, \
325 CTLTYPE_STRUCT | CTLFLAG_MPSAFE | (access), NULL, len, \
326 sysctl_jail_param, fmt, descr)
327 #define SYSCTL_JAIL_PARAM_NODE(module, descr) \
328 SYSCTL_NODE(_security_jail_param, OID_AUTO, module, 0, 0, descr)
329 #define SYSCTL_JAIL_PARAM_SYS_NODE(module, access, descr) \
330 SYSCTL_JAIL_PARAM_NODE(module, descr); \
331 SYSCTL_JAIL_PARAM(_##module, , CTLTYPE_INT | (access), "E,jailsys", \
332 descr)
333
334 /*
335 * Kernel support functions for jail().
336 */
337 struct ucred;
338 struct mount;
339 struct sockaddr;
340 struct statfs;
341 int jailed(struct ucred *cred);
342 int jailed_without_vnet(struct ucred *);
343 void getcredhostname(struct ucred *, char *, size_t);
344 void getcreddomainname(struct ucred *, char *, size_t);
345 void getcredhostuuid(struct ucred *, char *, size_t);
346 void getcredhostid(struct ucred *, unsigned long *);
347 int prison_allow(struct ucred *, unsigned);
348 int prison_check(struct ucred *cred1, struct ucred *cred2);
349 int prison_owns_vnet(struct ucred *);
350 int prison_canseemount(struct ucred *cred, struct mount *mp);
351 void prison_enforce_statfs(struct ucred *cred, struct mount *mp,
352 struct statfs *sp);
353 struct prison *prison_find(int prid);
354 struct prison *prison_find_child(struct prison *, int);
355 struct prison *prison_find_name(struct prison *, const char *);
356 int prison_flag(struct ucred *, unsigned);
357 void prison_free(struct prison *pr);
358 void prison_free_locked(struct prison *pr);
359 void prison_hold(struct prison *pr);
360 void prison_hold_locked(struct prison *pr);
361 void prison_proc_hold(struct prison *);
362 void prison_proc_free(struct prison *);
363 int prison_ischild(struct prison *, struct prison *);
364 int prison_equal_ip4(struct prison *, struct prison *);
365 int prison_get_ip4(struct ucred *cred, struct in_addr *ia);
366 int prison_local_ip4(struct ucred *cred, struct in_addr *ia);
367 int prison_remote_ip4(struct ucred *cred, struct in_addr *ia);
368 int prison_check_ip4(struct ucred *cred, struct in_addr *ia);
369 int prison_saddrsel_ip4(struct ucred *, struct in_addr *);
370 #ifdef INET6
371 int prison_equal_ip6(struct prison *, struct prison *);
372 int prison_get_ip6(struct ucred *, struct in6_addr *);
373 int prison_local_ip6(struct ucred *, struct in6_addr *, int);
374 int prison_remote_ip6(struct ucred *, struct in6_addr *);
375 int prison_check_ip6(struct ucred *, struct in6_addr *);
376 int prison_saddrsel_ip6(struct ucred *, struct in6_addr *);
377 #endif
378 int prison_check_af(struct ucred *cred, int af);
379 int prison_if(struct ucred *cred, struct sockaddr *sa);
380 char *prison_name(struct prison *, struct prison *);
381 int prison_priv_check(struct ucred *cred, int priv);
382 int sysctl_jail_param(struct sysctl_oid *, void *, int , struct sysctl_req *);
383
384 #endif /* _KERNEL */
385 #endif /* !_SYS_JAIL_H_ */
Cache object: 4af8ad9c2eb1108d9eaca383a8650b0a
|