2 * ----------------------------------------------------------------------------
3 * "THE BEER-WARE LICENSE" (Revision 42):
4 * <phk@FreeBSD.ORG> wrote this file. As long as you retain this notice you
5 * can do whatever you want with this stuff. If we meet some day, and you think
6 * this stuff is worth it, you can buy me a beer in return. Poul-Henning Kamp
7 * ----------------------------------------------------------------------------
9 * $FreeBSD: src/sys/kern/kern_jail.c,v 1.6.2.3 2001/08/17 01:00:26 rwatson Exp $
10 * $DragonFly: src/sys/kern/kern_jail.c,v 1.8 2005/07/20 20:31:18 dillon Exp $
14 #include <sys/param.h>
15 #include <sys/types.h>
16 #include <sys/kernel.h>
17 #include <sys/kinfo.h>
18 #include <sys/systm.h>
19 #include <sys/errno.h>
20 #include <sys/sysproto.h>
21 #include <sys/malloc.h>
22 #include <sys/nlookup.h>
23 #include <sys/namecache.h>
26 #include <sys/socket.h>
27 #include <sys/sysctl.h>
28 #include <sys/kern_syscall.h>
30 #include <netinet/in.h>
32 static struct prison *prison_find(int);
34 MALLOC_DEFINE(M_PRISON, "prison", "Prison structures");
36 SYSCTL_NODE(, OID_AUTO, jail, CTLFLAG_RW, 0,
39 int jail_set_hostname_allowed = 1;
40 SYSCTL_INT(_jail, OID_AUTO, set_hostname_allowed, CTLFLAG_RW,
41 &jail_set_hostname_allowed, 0,
42 "Processes in jail can set their hostnames");
44 int jail_socket_unixiproute_only = 1;
45 SYSCTL_INT(_jail, OID_AUTO, socket_unixiproute_only, CTLFLAG_RW,
46 &jail_socket_unixiproute_only, 0,
47 "Processes in jail are limited to creating UNIX/IPv4/route sockets only");
49 int jail_sysvipc_allowed = 0;
50 SYSCTL_INT(_jail, OID_AUTO, sysvipc_allowed, CTLFLAG_RW,
51 &jail_sysvipc_allowed, 0,
52 "Processes in jail can use System V IPC primitives");
54 int jail_chflags_allowed = 0;
55 SYSCTL_INT(_jail, OID_AUTO, chflags_allowed, CTLFLAG_RW,
56 &jail_chflags_allowed, 0,
57 "Process in jail can set chflags(1)");
62 LIST_HEAD(prisonlist, prison);
63 struct prisonlist allprison = LIST_HEAD_INITIALIZER(&allprison);
66 kern_jail_attach(int jid)
68 struct proc *p = curthread->td_proc;
72 pr = prison_find(jid);
76 error = kern_chroot(pr->pr_root);
82 p->p_ucred->cr_prison = pr;
83 p->p_flag |= P_JAILED;
91 * jail_args(syscallarg(struct jail *) jail)
94 jail(struct jail_args *uap)
96 struct prison *pr, *tpr;
98 struct thread *td = curthread;
100 struct nlookupdata nd;
105 error = copyin(uap->jail, &j, sizeof j);
110 MALLOC(pr, struct prison *, sizeof *pr , M_PRISON, M_WAITOK | M_ZERO);
112 error = copyinstr(j.hostname, &pr->pr_host, sizeof pr->pr_host, 0);
115 error = nlookup_init(&nd, j.path, UIO_USERSPACE, NLC_FOLLOW);
117 goto nlookup_init_clean;
118 error = nlookup(&nd);
120 goto nlookup_init_clean;
121 pr->pr_root = cache_hold(nd.nl_ncp);
123 pr->pr_ip = j.ip_number;
124 varsymset_init(&pr->pr_varsymset, NULL);
126 tryprid = lastprid + 1;
127 if (tryprid == JAIL_MAX)
130 LIST_FOREACH(tpr, &allprison, pr_list) {
131 if (tpr->pr_id != tryprid)
134 if (tryprid == JAIL_MAX) {
140 pr->pr_id = lastprid = tryprid;
141 LIST_INSERT_HEAD(&allprison, pr, pr_list);
144 error = kern_jail_attach(pr->pr_id);
146 goto jail_attach_clean;
152 LIST_REMOVE(pr, pr_list);
154 varsymset_clean(&pr->pr_varsymset);
163 * int jail_attach(int jid);
166 jail_attach(struct jail_attach_args *uap)
168 struct thread *td = curthread;
175 return(kern_jail_attach(uap->jid));
179 prison_ip(struct thread *td, int flag, u_int32_t *ip)
184 if (td->td_proc == NULL)
186 if ((pr = td->td_proc->p_ucred->cr_prison) == NULL)
192 if (tmp == INADDR_ANY) {
196 *ip = htonl(pr->pr_ip);
199 if (tmp == INADDR_LOOPBACK) {
203 *ip = htonl(pr->pr_ip);
206 if (pr->pr_ip != tmp)
212 prison_remote_ip(struct thread *td, int flag, u_int32_t *ip)
217 if (td == NULL || td->td_proc == NULL)
219 if ((pr = td->td_proc->p_ucred->cr_prison) == NULL)
225 if (tmp == INADDR_LOOPBACK) {
229 *ip = htonl(pr->pr_ip);
235 prison_if(struct thread *td, struct sockaddr *sa)
238 struct sockaddr_in *sai = (struct sockaddr_in*) sa;
241 if (td->td_proc == NULL)
243 pr = td->td_proc->p_ucred->cr_prison;
245 if ((sai->sin_family != AF_INET) && jail_socket_unixiproute_only)
247 else if (sai->sin_family != AF_INET)
249 else if (pr->pr_ip != ntohl(sai->sin_addr.s_addr))
257 * Returns a prison instance, or NULL on failure.
259 static struct prison *
260 prison_find(int prid)
264 LIST_FOREACH(pr, &allprison, pr_list) {
265 if (pr->pr_id == prid)
272 sysctl_jail_list(SYSCTL_HANDLER_ARGS)
275 struct kinfo_prison *xp, *sxp;
279 p = curthread->td_proc;
281 if (jailed(p->p_ucred))
289 sxp = xp = malloc(sizeof(*xp) * count, M_TEMP, M_WAITOK | M_ZERO);
290 if (count < prisoncount) {
296 LIST_FOREACH(pr, &allprison, pr_list) {
297 char *fullpath, *freepath;
298 xp->pr_version = KINFO_PRISON_VERSION;
299 xp->pr_id = pr->pr_id;
300 error = cache_fullpath(p, pr->pr_root, &fullpath, &freepath);
302 strlcpy(xp->pr_path, fullpath, sizeof(xp->pr_path));
303 free(freepath, M_TEMP);
305 bzero(xp->pr_path, sizeof(xp->pr_path));
307 strlcpy(xp->pr_host, pr->pr_host, sizeof(xp->pr_host));
308 xp->pr_ip = pr->pr_ip;
312 error = SYSCTL_OUT(req, sxp, sizeof(*sxp) * count);
317 SYSCTL_OID(_jail, OID_AUTO, list, CTLTYPE_STRUCT | CTLFLAG_RD, NULL, 0,
318 sysctl_jail_list, "S", "List of active jails");
321 prison_hold(struct prison *pr)
327 prison_free(struct prison *pr)
329 KKASSERT(pr->pr_ref >= 1);
331 if (--pr->pr_ref > 0)
334 LIST_REMOVE(pr, pr_list);
337 if (pr->pr_linux != NULL)
338 free(pr->pr_linux, M_PRISON);
339 varsymset_clean(&pr->pr_varsymset);
340 cache_drop(pr->pr_root);