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 * ----------------------------------------------------------------------------
13 #include <sys/param.h>
14 #include <sys/types.h>
15 #include <sys/kernel.h>
16 #include <sys/systm.h>
17 #include <sys/errno.h>
18 #include <sys/sysproto.h>
19 #include <sys/malloc.h>
22 #include <sys/socket.h>
23 #include <sys/sysctl.h>
25 #include <netinet/in.h>
27 MALLOC_DEFINE(M_PRISON, "prison", "Prison structures");
29 SYSCTL_NODE(, OID_AUTO, jail, CTLFLAG_RW, 0,
32 int jail_set_hostname_allowed = 1;
33 SYSCTL_INT(_jail, OID_AUTO, set_hostname_allowed, CTLFLAG_RW,
34 &jail_set_hostname_allowed, 0,
35 "Processes in jail can set their hostnames");
37 int jail_socket_unixiproute_only = 1;
38 SYSCTL_INT(_jail, OID_AUTO, socket_unixiproute_only, CTLFLAG_RW,
39 &jail_socket_unixiproute_only, 0,
40 "Processes in jail are limited to creating UNIX/IPv4/route sockets only");
42 int jail_sysvipc_allowed = 0;
43 SYSCTL_INT(_jail, OID_AUTO, sysvipc_allowed, CTLFLAG_RW,
44 &jail_sysvipc_allowed, 0,
45 "Processes in jail can use System V IPC primitives");
51 syscallarg(struct jail *) jail;
57 struct chroot_args ca;
62 error = copyin(uap->jail, &j, sizeof j);
67 MALLOC(pr, struct prison *, sizeof *pr , M_PRISON, M_WAITOK);
68 bzero((caddr_t)pr, sizeof *pr);
69 error = copyinstr(j.hostname, &pr->pr_host, sizeof pr->pr_host, 0);
72 pr->pr_ip = j.ip_number;
75 error = chroot(p, &ca);
81 p->p_flag |= P_JAILED;
90 prison_ip(struct proc *p, int flag, u_int32_t *ip)
100 if (tmp == INADDR_ANY) {
102 *ip = p->p_prison->pr_ip;
104 *ip = htonl(p->p_prison->pr_ip);
107 if (p->p_prison->pr_ip != tmp)
113 prison_remote_ip(struct proc *p, int flag, u_int32_t *ip)
117 if (!p || !p->p_prison)
123 if (tmp == 0x7f000001) {
125 *ip = p->p_prison->pr_ip;
127 *ip = htonl(p->p_prison->pr_ip);
134 prison_if(struct proc *p, struct sockaddr *sa)
136 struct sockaddr_in *sai = (struct sockaddr_in*) sa;
139 if ((sai->sin_family != AF_INET) && jail_socket_unixiproute_only)
141 else if (sai->sin_family != AF_INET)
143 else if (p->p_prison->pr_ip != ntohl(sai->sin_addr.s_addr))