xref: /freebsd/sys/kern/kern_jail.c (revision 6990ffd8a95caaba6858ad44ff1b3157d1efba8f)
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$
10  *
11  */
12 
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>
20 #include <sys/proc.h>
21 #include <sys/jail.h>
22 #include <sys/socket.h>
23 #include <sys/sysctl.h>
24 #include <net/if.h>
25 #include <netinet/in.h>
26 
27 MALLOC_DEFINE(M_PRISON, "prison", "Prison structures");
28 
29 SYSCTL_NODE(, OID_AUTO, jail, CTLFLAG_RW, 0,
30     "Jail rules");
31 
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");
36 
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");
41 
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");
46 
47 /*
48  * MPSAFE
49  */
50 int
51 jail(td, uap)
52 	struct thread *td;
53 	struct jail_args /* {
54 		syscallarg(struct jail *) jail;
55 	} */ *uap;
56 {
57 	struct proc *p = td->td_proc;
58 	int error;
59 	struct prison *pr;
60 	struct jail j;
61 	struct chroot_args ca;
62 
63 	mtx_lock(&Giant);
64 
65 	/* Implicitly fail if already in jail.  */
66 	error = suser(p);
67 	if (error)
68 		goto done2;
69 	error = copyin(uap->jail, &j, sizeof j);
70 	if (error)
71 		goto done2;
72 	if (j.version != 0) {
73 		error = EINVAL;
74 		goto done2;
75 	}
76 	MALLOC(pr, struct prison *, sizeof *pr , M_PRISON, M_WAITOK | M_ZERO);
77 	error = copyinstr(j.hostname, &pr->pr_host, sizeof pr->pr_host, 0);
78 	if (error)
79 		goto bail;
80 	pr->pr_ip = j.ip_number;
81 
82 	ca.path = j.path;
83 	error = chroot(td, &ca);
84 	if (error)
85 		goto bail;
86 
87 	p->p_ucred = crcopy(p->p_ucred);
88 	p->p_ucred->cr_prison = pr;
89 	pr->pr_ref = 1;
90 	mtx_unlock(&Giant);
91 	return (0);
92 
93 bail:
94 	FREE(pr, M_PRISON);
95 done2:
96 	mtx_unlock(&Giant);
97 	return (error);
98 }
99 
100 void
101 prison_free(struct prison *pr)
102 {
103 
104 	pr->pr_ref--;
105 	if (pr->pr_ref == 0) {
106 		if (pr->pr_linux != NULL)
107 			FREE(pr->pr_linux, M_PRISON);
108 		FREE(pr, M_PRISON);
109 	}
110 }
111 
112 void
113 prison_hold(struct prison *pr)
114 {
115 
116 	pr->pr_ref++;
117 }
118 
119 int
120 prison_ip(struct ucred *cred, int flag, u_int32_t *ip)
121 {
122 	u_int32_t tmp;
123 
124 	if (!jailed(cred))
125 		return (0);
126 	if (flag)
127 		tmp = *ip;
128 	else
129 		tmp = ntohl(*ip);
130 	if (tmp == INADDR_ANY) {
131 		if (flag)
132 			*ip = cred->cr_prison->pr_ip;
133 		else
134 			*ip = htonl(cred->cr_prison->pr_ip);
135 		return (0);
136 	}
137 	if (tmp == INADDR_LOOPBACK) {
138 		if (flag)
139 			*ip = cred->cr_prison->pr_ip;
140 		else
141 			*ip = htonl(cred->cr_prison->pr_ip);
142 		return (0);
143 	}
144 	if (cred->cr_prison->pr_ip != tmp)
145 		return (1);
146 	return (0);
147 }
148 
149 void
150 prison_remote_ip(struct ucred *cred, int flag, u_int32_t *ip)
151 {
152 	u_int32_t tmp;
153 
154 	if (!jailed(cred))
155 		return;
156 	if (flag)
157 		tmp = *ip;
158 	else
159 		tmp = ntohl(*ip);
160 	if (tmp == INADDR_LOOPBACK) {
161 		if (flag)
162 			*ip = cred->cr_prison->pr_ip;
163 		else
164 			*ip = htonl(cred->cr_prison->pr_ip);
165 		return;
166 	}
167 	return;
168 }
169 
170 int
171 prison_if(struct ucred *cred, struct sockaddr *sa)
172 {
173 	struct sockaddr_in *sai = (struct sockaddr_in*) sa;
174 	int ok;
175 
176 	if ((sai->sin_family != AF_INET) && jail_socket_unixiproute_only)
177 		ok = 1;
178 	else if (sai->sin_family != AF_INET)
179 		ok = 0;
180 	else if (cred->cr_prison->pr_ip != ntohl(sai->sin_addr.s_addr))
181 		ok = 1;
182 	else
183 		ok = 0;
184 	return (ok);
185 }
186 
187 /*
188  * Return 0 if jails permit p1 to frob p2, otherwise ESRCH.
189  */
190 int
191 prison_check(cred1, cred2)
192 	struct ucred *cred1, *cred2;
193 {
194 
195 	if (jailed(cred1)) {
196 		if (!jailed(cred2))
197 			return (ESRCH);
198 		if (cred2->cr_prison != cred1->cr_prison)
199 			return (ESRCH);
200 	}
201 
202 	return (0);
203 }
204 
205 /*
206  * Return 1 if the passed credential is in a jail, otherwise 0.
207  */
208 int
209 jailed(cred)
210 	struct ucred *cred;
211 {
212 
213 	return (cred->cr_prison != NULL);
214 }
215