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 * ----------------------------------------------------------------------------
11 * Copyright (c) 2006 Victor Balada Diaz <victor@bsdes.net>
12 * All rights reserved.
14 * Redistribution and use in source and binary forms, with or without
15 * modification, are permitted provided that the following conditions
17 * 1. Redistributions of source code must retain the above copyright
18 * notice, this list of conditions and the following disclaimer.
19 * 2. Redistributions in binary form must reproduce the above copyright
20 * notice, this list of conditions and the following disclaimer in the
21 * documentation and/or other materials provided with the distribution.
23 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
24 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
25 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
26 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
27 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
28 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
29 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
30 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
31 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
32 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
38 * $FreeBSD: src/sys/kern/kern_jail.c,v 1.6.2.3 2001/08/17 01:00:26 rwatson Exp $
39 * $DragonFly: src/sys/kern/kern_jail.c,v 1.19 2008/05/17 18:20:33 dillon Exp $
42 #include "opt_inet6.h"
44 #include <sys/param.h>
45 #include <sys/types.h>
46 #include <sys/kernel.h>
47 #include <sys/systm.h>
48 #include <sys/errno.h>
49 #include <sys/sysproto.h>
50 #include <sys/malloc.h>
51 #include <sys/nlookup.h>
52 #include <sys/namecache.h>
56 #include <sys/socket.h>
57 #include <sys/sysctl.h>
58 #include <sys/kern_syscall.h>
60 #include <netinet/in.h>
61 #include <netinet6/in6_var.h>
63 static struct prison *prison_find(int);
64 static void prison_ipcache_init(struct prison *);
66 MALLOC_DEFINE(M_PRISON, "prison", "Prison structures");
68 SYSCTL_NODE(, OID_AUTO, jail, CTLFLAG_RW, 0,
71 int jail_set_hostname_allowed = 1;
72 SYSCTL_INT(_jail, OID_AUTO, set_hostname_allowed, CTLFLAG_RW,
73 &jail_set_hostname_allowed, 0,
74 "Processes in jail can set their hostnames");
76 int jail_socket_unixiproute_only = 1;
77 SYSCTL_INT(_jail, OID_AUTO, socket_unixiproute_only, CTLFLAG_RW,
78 &jail_socket_unixiproute_only, 0,
79 "Processes in jail are limited to creating UNIX/IPv[46]/route sockets only");
81 int jail_sysvipc_allowed = 0;
82 SYSCTL_INT(_jail, OID_AUTO, sysvipc_allowed, CTLFLAG_RW,
83 &jail_sysvipc_allowed, 0,
84 "Processes in jail can use System V IPC primitives");
86 int jail_chflags_allowed = 0;
87 SYSCTL_INT(_jail, OID_AUTO, chflags_allowed, CTLFLAG_RW,
88 &jail_chflags_allowed, 0,
89 "Process in jail can set chflags(1)");
91 int jail_allow_raw_sockets = 0;
92 SYSCTL_INT(_jail, OID_AUTO, allow_raw_sockets, CTLFLAG_RW,
93 &jail_allow_raw_sockets, 0,
94 "Process in jail can create raw sockets");
99 LIST_HEAD(prisonlist, prison);
100 struct prisonlist allprison = LIST_HEAD_INITIALIZER(&allprison);
103 kern_jail_attach(int jid)
105 struct proc *p = curthread->td_proc;
109 pr = prison_find(jid);
113 error = kern_chroot(&pr->pr_root);
119 p->p_ucred->cr_prison = pr;
120 p->p_flag |= P_JAILED;
128 * jail_args(syscallarg(struct jail *) jail)
131 sys_jail(struct jail_args *uap)
133 struct prison *pr, *tpr;
136 struct thread *td = curthread;
137 int error, tryprid, i;
139 struct nlookupdata nd;
141 struct sockaddr_storage *uips; /* Userland ips */
142 struct sockaddr_in ip4addr;
143 struct jail_ip_storage *jip;
146 error = priv_check(td, PRIV_ROOT);
148 uap->sysmsg_result = -1;
151 error = copyin(uap->jail, &jversion, sizeof(jversion));
153 uap->sysmsg_result = -1;
156 pr = kmalloc(sizeof *pr , M_PRISON, M_WAITOK | M_ZERO);
157 SLIST_INIT(&pr->pr_ips);
161 /* Single IPv4 jails. */
163 error = copyin(uap->jail, &jv0, sizeof(jv0));
166 jip = kmalloc(sizeof(*jip), M_PRISON, M_WAITOK | M_ZERO);
167 ip4addr.sin_family = AF_INET;
168 ip4addr.sin_addr.s_addr = htonl(jv0.ip_number);
169 memcpy(&jip->ip, &ip4addr, sizeof(ip4addr));
170 SLIST_INSERT_HEAD(&pr->pr_ips, jip, entries);
174 * DragonFly multi noIP/IPv4/IPv6 jails
176 * NOTE: This version is unsupported by FreeBSD
177 * (which uses version 2 instead).
180 error = copyin(uap->jail, &j, sizeof(j));
183 uips = kmalloc((sizeof(*uips) * j.n_ips), M_PRISON,
185 error = copyin(j.ips, uips, (sizeof(*uips) * j.n_ips));
187 kfree(uips, M_PRISON);
190 for (i = 0; i < j.n_ips; i++) {
191 jip = kmalloc(sizeof(*jip), M_PRISON,
193 memcpy(&jip->ip, &uips[i], sizeof(*uips));
194 SLIST_INSERT_HEAD(&pr->pr_ips, jip, entries);
196 kfree(uips, M_PRISON);
203 error = copyinstr(j.hostname, &pr->pr_host, sizeof pr->pr_host, 0);
206 error = nlookup_init(&nd, j.path, UIO_USERSPACE, NLC_FOLLOW);
208 goto nlookup_init_clean;
209 error = nlookup(&nd);
211 goto nlookup_init_clean;
212 cache_copy(&nd.nl_nch, &pr->pr_root);
214 varsymset_init(&pr->pr_varsymset, NULL);
215 prison_ipcache_init(pr);
217 tryprid = lastprid + 1;
218 if (tryprid == JAIL_MAX)
221 LIST_FOREACH(tpr, &allprison, pr_list) {
222 if (tpr->pr_id != tryprid)
225 if (tryprid == JAIL_MAX) {
231 pr->pr_id = lastprid = tryprid;
232 LIST_INSERT_HEAD(&allprison, pr, pr_list);
235 error = kern_jail_attach(pr->pr_id);
237 goto jail_attach_clean;
240 uap->sysmsg_result = pr->pr_id;
244 LIST_REMOVE(pr, pr_list);
246 varsymset_clean(&pr->pr_varsymset);
251 while (!SLIST_EMPTY(&pr->pr_ips)) {
252 jip = SLIST_FIRST(&pr->pr_ips);
253 SLIST_REMOVE_HEAD(&pr->pr_ips, entries);
254 kfree(jip, M_PRISON);
261 * int jail_attach(int jid);
264 sys_jail_attach(struct jail_attach_args *uap)
266 struct thread *td = curthread;
269 error = priv_check(td, PRIV_ROOT);
273 return(kern_jail_attach(uap->jid));
277 prison_ipcache_init(struct prison *pr)
279 struct jail_ip_storage *jis;
280 struct sockaddr_in *ip4;
281 struct sockaddr_in6 *ip6;
283 SLIST_FOREACH(jis, &pr->pr_ips, entries) {
284 switch (jis->ip.ss_family) {
286 ip4 = (struct sockaddr_in *)&jis->ip;
287 if ((ntohl(ip4->sin_addr.s_addr) >> IN_CLASSA_NSHIFT) ==
289 /* loopback address */
290 if (pr->local_ip4 == NULL)
294 if (pr->nonlocal_ip4 == NULL)
295 pr->nonlocal_ip4 = ip4;
300 ip6 = (struct sockaddr_in6 *)&jis->ip;
301 if (IN6_IS_ADDR_LOOPBACK(&ip6->sin6_addr)) {
302 /* loopback address */
303 if (pr->local_ip6 == NULL)
307 if (pr->nonlocal_ip6 == NULL)
308 pr->nonlocal_ip6 = ip6;
316 * Changes INADDR_LOOPBACK for a valid jail address.
317 * ip is in network byte order.
318 * Returns 1 if the ip is among jail valid ips.
319 * Returns 0 if is not among jail valid ips or
320 * if couldn't replace INADDR_LOOPBACK for a valid
324 prison_replace_wildcards(struct thread *td, struct sockaddr *ip)
326 struct sockaddr_in *ip4 = (struct sockaddr_in *)ip;
327 struct sockaddr_in6 *ip6 = (struct sockaddr_in6 *)ip;
330 if (td->td_proc == NULL)
332 if ((pr = td->td_proc->p_ucred->cr_prison) == NULL)
335 if ((ip->sa_family == AF_INET &&
336 ip4->sin_addr.s_addr == htonl(INADDR_ANY)) ||
337 (ip->sa_family == AF_INET6 &&
338 IN6_IS_ADDR_UNSPECIFIED(&ip6->sin6_addr)))
340 if ((ip->sa_family == AF_INET &&
341 ip4->sin_addr.s_addr == htonl(INADDR_LOOPBACK)) ||
342 (ip->sa_family == AF_INET6 &&
343 IN6_IS_ADDR_LOOPBACK(&ip6->sin6_addr))) {
344 if (!prison_get_local(pr, ip->sa_family, ip) &&
345 !prison_get_nonlocal(pr, ip->sa_family, ip))
350 if (jailed_ip(pr, ip))
356 prison_remote_ip(struct thread *td, struct sockaddr *ip)
358 struct sockaddr_in *ip4 = (struct sockaddr_in *)ip;
359 struct sockaddr_in6 *ip6 = (struct sockaddr_in6 *)ip;
362 if (td == NULL || td->td_proc == NULL)
364 if ((pr = td->td_proc->p_ucred->cr_prison) == NULL)
366 if ((ip->sa_family == AF_INET &&
367 ip4->sin_addr.s_addr == htonl(INADDR_LOOPBACK)) ||
368 (ip->sa_family == AF_INET6 &&
369 IN6_IS_ADDR_LOOPBACK(&ip6->sin6_addr))) {
370 if (!prison_get_local(pr, ip->sa_family, ip) &&
371 !prison_get_nonlocal(pr, ip->sa_family, ip))
380 * Prison get non loopback ip:
381 * - af is the address family of the ip we want (AF_INET|AF_INET6).
382 * - If ip != NULL, put the first IP address that is not a loopback address
385 * ip is in network by order and we don't touch it unless we find a valid ip.
386 * No matter if ip == NULL or not, we return either a valid struct sockaddr *,
387 * or NULL. This struct may not be modified.
390 prison_get_nonlocal(struct prison *pr, sa_family_t af, struct sockaddr *ip)
392 struct sockaddr_in *ip4 = (struct sockaddr_in *)ip;
393 struct sockaddr_in6 *ip6 = (struct sockaddr_in6 *)ip;
395 /* Check if it is cached */
398 if (ip4 != NULL && pr->nonlocal_ip4 != NULL)
399 ip4->sin_addr.s_addr = pr->nonlocal_ip4->sin_addr.s_addr;
400 return (struct sockaddr *)pr->nonlocal_ip4;
403 if (ip6 != NULL && pr->nonlocal_ip6 != NULL)
404 ip6->sin6_addr = pr->nonlocal_ip6->sin6_addr;
405 return (struct sockaddr *)pr->nonlocal_ip6;
413 * Prison get loopback ip.
414 * - af is the address family of the ip we want (AF_INET|AF_INET6).
415 * - If ip != NULL, put the first IP address that is not a loopback address
418 * ip is in network by order and we don't touch it unless we find a valid ip.
419 * No matter if ip == NULL or not, we return either a valid struct sockaddr *,
420 * or NULL. This struct may not be modified.
423 prison_get_local(struct prison *pr, sa_family_t af, struct sockaddr *ip)
425 struct sockaddr_in *ip4 = (struct sockaddr_in *)ip;
426 struct sockaddr_in6 *ip6 = (struct sockaddr_in6 *)ip;
428 /* Check if it is cached */
431 if (ip4 != NULL && pr->local_ip4 != NULL)
432 ip4->sin_addr.s_addr = pr->local_ip4->sin_addr.s_addr;
433 return (struct sockaddr *)pr->local_ip4;
436 if (ip6 != NULL && pr->local_ip6 != NULL)
437 ip6->sin6_addr = pr->local_ip6->sin6_addr;
438 return (struct sockaddr *)pr->local_ip6;
445 /* Check if the IP is among ours, if it is return 1, else 0 */
447 jailed_ip(struct prison *pr, struct sockaddr *ip)
449 struct jail_ip_storage *jis;
450 struct sockaddr_in *jip4, *ip4;
451 struct sockaddr_in6 *jip6, *ip6;
455 ip4 = (struct sockaddr_in *)ip;
456 ip6 = (struct sockaddr_in6 *)ip;
457 SLIST_FOREACH(jis, &pr->pr_ips, entries) {
458 switch (ip->sa_family) {
460 jip4 = (struct sockaddr_in *) &jis->ip;
461 if (jip4->sin_family == AF_INET &&
462 ip4->sin_addr.s_addr == jip4->sin_addr.s_addr)
466 jip6 = (struct sockaddr_in6 *) &jis->ip;
467 if (jip6->sin6_family == AF_INET6 &&
468 IN6_ARE_ADDR_EQUAL(&ip6->sin6_addr,
479 prison_if(struct ucred *cred, struct sockaddr *sa)
482 struct sockaddr_in *sai = (struct sockaddr_in*) sa;
484 pr = cred->cr_prison;
486 if (((sai->sin_family != AF_INET) && (sai->sin_family != AF_INET6))
487 && jail_socket_unixiproute_only)
489 else if ((sai->sin_family != AF_INET) && (sai->sin_family != AF_INET6))
491 else if (jailed_ip(pr, sa))
497 * Returns a prison instance, or NULL on failure.
499 static struct prison *
500 prison_find(int prid)
504 LIST_FOREACH(pr, &allprison, pr_list) {
505 if (pr->pr_id == prid)
512 sysctl_jail_list(SYSCTL_HANDLER_ARGS)
514 struct jail_ip_storage *jip;
516 struct sockaddr_in6 *jsin6;
518 struct sockaddr_in *jsin;
521 unsigned int jlssize, jlsused;
523 char *jls; /* Jail list */
524 char *oip; /* Output ip */
525 char *fullpath, *freepath;
528 p = curthread->td_proc;
530 if (jailed(p->p_ucred))
538 jlssize = (count * 1024);
539 jls = kmalloc(jlssize + 1, M_TEMP, M_WAITOK | M_ZERO);
540 if (count < prisoncount) {
546 LIST_FOREACH(pr, &allprison, pr_list) {
547 error = cache_fullpath(p, &pr->pr_root, &fullpath, &freepath);
550 if (jlsused && jlsused < jlssize)
551 jls[jlsused++] = '\n';
552 count = ksnprintf(jls + jlsused, (jlssize - jlsused),
554 pr->pr_id, pr->pr_host, fullpath);
555 kfree(freepath, M_TEMP);
561 SLIST_FOREACH(jip, &pr->pr_ips, entries) {
562 jsin = (struct sockaddr_in *)&jip->ip;
564 switch(jsin->sin_family) {
566 oip = inet_ntoa(jsin->sin_addr);
570 jsin6 = (struct sockaddr_in6 *)&jip->ip;
571 oip = ip6_sprintf(&jsin6->sin6_addr);
579 if ((jlssize - jlsused) < (strlen(oip) + 1)) {
583 count = ksnprintf(jls + jlsused, (jlssize - jlsused),
593 * pr_id <SPC> hostname1 <SPC> PATH1 <SPC> IP1 <SPC> IP2\npr_id...
595 error = SYSCTL_OUT(req, jls, jlsused);
601 SYSCTL_OID(_jail, OID_AUTO, list, CTLTYPE_STRING | CTLFLAG_RD, NULL, 0,
602 sysctl_jail_list, "A", "List of active jails");
605 prison_hold(struct prison *pr)
611 prison_free(struct prison *pr)
613 struct jail_ip_storage *jls;
614 KKASSERT(pr->pr_ref >= 1);
616 if (--pr->pr_ref > 0)
620 while (!SLIST_EMPTY(&pr->pr_ips)) {
621 jls = SLIST_FIRST(&pr->pr_ips);
622 SLIST_REMOVE_HEAD(&pr->pr_ips, entries);
623 kfree(jls, M_PRISON);
625 LIST_REMOVE(pr, pr_list);
628 if (pr->pr_linux != NULL)
629 kfree(pr->pr_linux, M_PRISON);
630 varsymset_clean(&pr->pr_varsymset);
631 cache_drop(&pr->pr_root);