proc->thread stage 2: MAJOR revamping of system calls, ucred, jail API,
[dragonfly.git] / sys / kern / kern_jail.c
1 /*
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  * ----------------------------------------------------------------------------
8  *
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.3 2003/06/23 17:55:41 dillon Exp $
11  *
12  */
13
14 #include <sys/param.h>
15 #include <sys/types.h>
16 #include <sys/kernel.h>
17 #include <sys/systm.h>
18 #include <sys/errno.h>
19 #include <sys/sysproto.h>
20 #include <sys/malloc.h>
21 #include <sys/proc.h>
22 #include <sys/jail.h>
23 #include <sys/socket.h>
24 #include <sys/sysctl.h>
25 #include <net/if.h>
26 #include <netinet/in.h>
27
28 MALLOC_DEFINE(M_PRISON, "prison", "Prison structures");
29
30 SYSCTL_NODE(, OID_AUTO, jail, CTLFLAG_RW, 0,
31     "Jail rules");
32
33 int     jail_set_hostname_allowed = 1;
34 SYSCTL_INT(_jail, OID_AUTO, set_hostname_allowed, CTLFLAG_RW,
35     &jail_set_hostname_allowed, 0,
36     "Processes in jail can set their hostnames");
37
38 int     jail_socket_unixiproute_only = 1;
39 SYSCTL_INT(_jail, OID_AUTO, socket_unixiproute_only, CTLFLAG_RW,
40     &jail_socket_unixiproute_only, 0,
41     "Processes in jail are limited to creating UNIX/IPv4/route sockets only");
42
43 int     jail_sysvipc_allowed = 0;
44 SYSCTL_INT(_jail, OID_AUTO, sysvipc_allowed, CTLFLAG_RW,
45     &jail_sysvipc_allowed, 0,
46     "Processes in jail can use System V IPC primitives");
47
48 /*
49  * jail()
50  *
51  * jail_args(syscallarg(struct jail *) jail)
52  */
53 int
54 jail(struct jail_args *uap) 
55 {
56         int error;
57         struct prison *pr;
58         struct jail j;
59         struct chroot_args ca;
60         struct proc *p = curproc;
61
62         error = suser();
63         if (error)
64                 return (error);
65         error = copyin(uap->jail, &j, sizeof j);
66         if (error)
67                 return (error);
68         if (j.version != 0)
69                 return (EINVAL);
70         MALLOC(pr, struct prison *, sizeof *pr , M_PRISON, M_WAITOK);
71         bzero((caddr_t)pr, sizeof *pr);
72         error = copyinstr(j.hostname, &pr->pr_host, sizeof pr->pr_host, 0);
73         if (error) 
74                 goto bail;
75         pr->pr_ip = j.ip_number;
76
77         ca.path = j.path;
78         error = chroot(&ca);
79         if (error)
80                 goto bail;
81
82         pr->pr_ref++;
83         p->p_ucred = crcopy(p->p_ucred);
84         p->p_ucred->cr_prison = pr;
85         p->p_flag |= P_JAILED;
86         return (0);
87
88 bail:
89         FREE(pr, M_PRISON);
90         return (error);
91 }
92
93 int
94 prison_ip(struct proc *p, int flag, u_int32_t *ip)
95 {
96         u_int32_t tmp;
97         struct prison *pr;
98
99         if ((pr = p->p_ucred->cr_prison) == NULL)
100                 return (0);
101         if (flag) 
102                 tmp = *ip;
103         else
104                 tmp = ntohl(*ip);
105         if (tmp == INADDR_ANY) {
106                 if (flag) 
107                         *ip = pr->pr_ip;
108                 else
109                         *ip = htonl(pr->pr_ip);
110                 return (0);
111         }
112         if (tmp == INADDR_LOOPBACK) {
113                 if (flag)
114                         *ip = pr->pr_ip;
115                 else
116                         *ip = htonl(pr->pr_ip);
117                 return (0);
118         }
119         if (pr->pr_ip != tmp)
120                 return (1);
121         return (0);
122 }
123
124 void
125 prison_remote_ip(struct proc *p, int flag, u_int32_t *ip)
126 {
127         u_int32_t tmp;
128         struct prison *pr;
129
130         if (p == NULL)
131                 return;
132         if ((pr = p->p_ucred->cr_prison) == NULL)
133                 return;
134         if (flag)
135                 tmp = *ip;
136         else
137                 tmp = ntohl(*ip);
138         if (tmp == INADDR_LOOPBACK) {
139                 if (flag)
140                         *ip = pr->pr_ip;
141                 else
142                         *ip = htonl(pr->pr_ip);
143                 return;
144         }
145         return;
146 }
147
148 int
149 prison_if(struct proc *p, struct sockaddr *sa)
150 {
151         struct prison *pr;
152         struct sockaddr_in *sai = (struct sockaddr_in*) sa;
153         int ok;
154
155         pr = p->p_ucred->cr_prison;
156
157         if ((sai->sin_family != AF_INET) && jail_socket_unixiproute_only)
158                 ok = 1;
159         else if (sai->sin_family != AF_INET)
160                 ok = 0;
161         else if (pr->pr_ip != ntohl(sai->sin_addr.s_addr))
162                 ok = 1;
163         else
164                 ok = 0;
165         return (ok);
166 }