FreeBSD/Linux Kernel Cross Reference
sys/netinet/in_jail.c
1 /*-
2 * Copyright (c) 1999 Poul-Henning Kamp.
3 * Copyright (c) 2008 Bjoern A. Zeeb.
4 * Copyright (c) 2009 James Gritton.
5 * All rights reserved.
6 *
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
9 * are met:
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
15 *
16 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
17 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
20 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
22 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
24 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26 * SUCH DAMAGE.
27 */
28
29 #include <sys/cdefs.h>
30 __FBSDID("$FreeBSD$");
31
32 #include "opt_ddb.h"
33 #include "opt_inet.h"
34 #include "opt_inet6.h"
35
36 #include <sys/param.h>
37 #include <sys/types.h>
38 #include <sys/kernel.h>
39 #include <sys/systm.h>
40 #include <sys/errno.h>
41 #include <sys/sysproto.h>
42 #include <sys/malloc.h>
43 #include <sys/osd.h>
44 #include <sys/priv.h>
45 #include <sys/proc.h>
46 #include <sys/taskqueue.h>
47 #include <sys/fcntl.h>
48 #include <sys/jail.h>
49 #include <sys/lock.h>
50 #include <sys/mutex.h>
51 #include <sys/racct.h>
52 #include <sys/refcount.h>
53 #include <sys/sx.h>
54 #include <sys/sysent.h>
55 #include <sys/namei.h>
56 #include <sys/mount.h>
57 #include <sys/queue.h>
58 #include <sys/socket.h>
59 #include <sys/syscallsubr.h>
60 #include <sys/sysctl.h>
61 #include <sys/vnode.h>
62
63 #include <net/if.h>
64 #include <net/vnet.h>
65
66 #include <netinet/in.h>
67
68 int
69 prison_qcmp_v4(const void *ip1, const void *ip2)
70 {
71 in_addr_t iaa, iab;
72
73 /*
74 * We need to compare in HBO here to get the list sorted as expected
75 * by the result of the code. Sorting NBO addresses gives you
76 * interesting results. If you do not understand, do not try.
77 */
78 iaa = ntohl(((const struct in_addr *)ip1)->s_addr);
79 iab = ntohl(((const struct in_addr *)ip2)->s_addr);
80
81 /*
82 * Do not simply return the difference of the two numbers, the int is
83 * not wide enough.
84 */
85 if (iaa > iab)
86 return (1);
87 else if (iaa < iab)
88 return (-1);
89 else
90 return (0);
91 }
92
93 /*
94 * Restrict a prison's IP address list with its parent's, possibly replacing
95 * it. Return true if the replacement buffer was used (or would have been).
96 */
97 int
98 prison_restrict_ip4(struct prison *pr, struct in_addr *newip4)
99 {
100 int ii, ij, used;
101 struct prison *ppr;
102
103 ppr = pr->pr_parent;
104 if (!(pr->pr_flags & PR_IP4_USER)) {
105 /* This has no user settings, so just copy the parent's list. */
106 if (pr->pr_ip4s < ppr->pr_ip4s) {
107 /*
108 * There's no room for the parent's list. Use the
109 * new list buffer, which is assumed to be big enough
110 * (if it was passed). If there's no buffer, try to
111 * allocate one.
112 */
113 used = 1;
114 if (newip4 == NULL) {
115 newip4 = malloc(ppr->pr_ip4s * sizeof(*newip4),
116 M_PRISON, M_NOWAIT);
117 if (newip4 != NULL)
118 used = 0;
119 }
120 if (newip4 != NULL) {
121 bcopy(ppr->pr_ip4, newip4,
122 ppr->pr_ip4s * sizeof(*newip4));
123 free(pr->pr_ip4, M_PRISON);
124 pr->pr_ip4 = newip4;
125 pr->pr_ip4s = ppr->pr_ip4s;
126 }
127 return (used);
128 }
129 pr->pr_ip4s = ppr->pr_ip4s;
130 if (pr->pr_ip4s > 0)
131 bcopy(ppr->pr_ip4, pr->pr_ip4,
132 pr->pr_ip4s * sizeof(*newip4));
133 else if (pr->pr_ip4 != NULL) {
134 free(pr->pr_ip4, M_PRISON);
135 pr->pr_ip4 = NULL;
136 }
137 } else if (pr->pr_ip4s > 0) {
138 /* Remove addresses that aren't in the parent. */
139 for (ij = 0; ij < ppr->pr_ip4s; ij++)
140 if (pr->pr_ip4[0].s_addr == ppr->pr_ip4[ij].s_addr)
141 break;
142 if (ij < ppr->pr_ip4s)
143 ii = 1;
144 else {
145 bcopy(pr->pr_ip4 + 1, pr->pr_ip4,
146 --pr->pr_ip4s * sizeof(*pr->pr_ip4));
147 ii = 0;
148 }
149 for (ij = 1; ii < pr->pr_ip4s; ) {
150 if (pr->pr_ip4[ii].s_addr == ppr->pr_ip4[0].s_addr) {
151 ii++;
152 continue;
153 }
154 switch (ij >= ppr->pr_ip4s ? -1 :
155 prison_qcmp_v4(&pr->pr_ip4[ii], &ppr->pr_ip4[ij])) {
156 case -1:
157 bcopy(pr->pr_ip4 + ii + 1, pr->pr_ip4 + ii,
158 (--pr->pr_ip4s - ii) * sizeof(*pr->pr_ip4));
159 break;
160 case 0:
161 ii++;
162 ij++;
163 break;
164 case 1:
165 ij++;
166 break;
167 }
168 }
169 if (pr->pr_ip4s == 0) {
170 free(pr->pr_ip4, M_PRISON);
171 pr->pr_ip4 = NULL;
172 }
173 }
174 return (0);
175 }
176
177 /*
178 * Pass back primary IPv4 address of this jail.
179 *
180 * If not restricted return success but do not alter the address. Caller has
181 * to make sure to initialize it correctly (e.g. INADDR_ANY).
182 *
183 * Returns 0 on success, EAFNOSUPPORT if the jail doesn't allow IPv4.
184 * Address returned in NBO.
185 */
186 int
187 prison_get_ip4(struct ucred *cred, struct in_addr *ia)
188 {
189 struct prison *pr;
190
191 KASSERT(cred != NULL, ("%s: cred is NULL", __func__));
192 KASSERT(ia != NULL, ("%s: ia is NULL", __func__));
193
194 pr = cred->cr_prison;
195 if (!(pr->pr_flags & PR_IP4))
196 return (0);
197 mtx_lock(&pr->pr_mtx);
198 if (!(pr->pr_flags & PR_IP4)) {
199 mtx_unlock(&pr->pr_mtx);
200 return (0);
201 }
202 if (pr->pr_ip4 == NULL) {
203 mtx_unlock(&pr->pr_mtx);
204 return (EAFNOSUPPORT);
205 }
206
207 ia->s_addr = pr->pr_ip4[0].s_addr;
208 mtx_unlock(&pr->pr_mtx);
209 return (0);
210 }
211
212 /*
213 * Return 1 if we should do proper source address selection or are not jailed.
214 * We will return 0 if we should bypass source address selection in favour
215 * of the primary jail IPv4 address. Only in this case *ia will be updated and
216 * returned in NBO.
217 * Return EAFNOSUPPORT, in case this jail does not allow IPv4.
218 */
219 int
220 prison_saddrsel_ip4(struct ucred *cred, struct in_addr *ia)
221 {
222 struct prison *pr;
223 struct in_addr lia;
224 int error;
225
226 KASSERT(cred != NULL, ("%s: cred is NULL", __func__));
227 KASSERT(ia != NULL, ("%s: ia is NULL", __func__));
228
229 if (!jailed(cred))
230 return (1);
231
232 pr = cred->cr_prison;
233 if (pr->pr_flags & PR_IP4_SADDRSEL)
234 return (1);
235
236 lia.s_addr = INADDR_ANY;
237 error = prison_get_ip4(cred, &lia);
238 if (error)
239 return (error);
240 if (lia.s_addr == INADDR_ANY)
241 return (1);
242
243 ia->s_addr = lia.s_addr;
244 return (0);
245 }
246
247 /*
248 * Return true if pr1 and pr2 have the same IPv4 address restrictions.
249 */
250 int
251 prison_equal_ip4(struct prison *pr1, struct prison *pr2)
252 {
253
254 if (pr1 == pr2)
255 return (1);
256
257 /*
258 * No need to lock since the PR_IP4_USER flag can't be altered for
259 * existing prisons.
260 */
261 while (pr1 != &prison0 &&
262 #ifdef VIMAGE
263 !(pr1->pr_flags & PR_VNET) &&
264 #endif
265 !(pr1->pr_flags & PR_IP4_USER))
266 pr1 = pr1->pr_parent;
267 while (pr2 != &prison0 &&
268 #ifdef VIMAGE
269 !(pr2->pr_flags & PR_VNET) &&
270 #endif
271 !(pr2->pr_flags & PR_IP4_USER))
272 pr2 = pr2->pr_parent;
273 return (pr1 == pr2);
274 }
275
276 /*
277 * Make sure our (source) address is set to something meaningful to this
278 * jail.
279 *
280 * Returns 0 if jail doesn't restrict IPv4 or if address belongs to jail,
281 * EADDRNOTAVAIL if the address doesn't belong, or EAFNOSUPPORT if the jail
282 * doesn't allow IPv4. Address passed in in NBO and returned in NBO.
283 */
284 int
285 prison_local_ip4(struct ucred *cred, struct in_addr *ia)
286 {
287 struct prison *pr;
288 struct in_addr ia0;
289 int error;
290
291 KASSERT(cred != NULL, ("%s: cred is NULL", __func__));
292 KASSERT(ia != NULL, ("%s: ia is NULL", __func__));
293
294 pr = cred->cr_prison;
295 if (!(pr->pr_flags & PR_IP4))
296 return (0);
297 mtx_lock(&pr->pr_mtx);
298 if (!(pr->pr_flags & PR_IP4)) {
299 mtx_unlock(&pr->pr_mtx);
300 return (0);
301 }
302 if (pr->pr_ip4 == NULL) {
303 mtx_unlock(&pr->pr_mtx);
304 return (EAFNOSUPPORT);
305 }
306
307 ia0.s_addr = ntohl(ia->s_addr);
308
309 if (ia0.s_addr == INADDR_ANY) {
310 /*
311 * In case there is only 1 IPv4 address, bind directly.
312 */
313 if (pr->pr_ip4s == 1)
314 ia->s_addr = pr->pr_ip4[0].s_addr;
315 mtx_unlock(&pr->pr_mtx);
316 return (0);
317 }
318
319 error = prison_check_ip4_locked(pr, ia);
320 if (error == EADDRNOTAVAIL && ia0.s_addr == INADDR_LOOPBACK) {
321 ia->s_addr = pr->pr_ip4[0].s_addr;
322 error = 0;
323 }
324
325 mtx_unlock(&pr->pr_mtx);
326 return (error);
327 }
328
329 /*
330 * Rewrite destination address in case we will connect to loopback address.
331 *
332 * Returns 0 on success, EAFNOSUPPORT if the jail doesn't allow IPv4.
333 * Address passed in in NBO and returned in NBO.
334 */
335 int
336 prison_remote_ip4(struct ucred *cred, struct in_addr *ia)
337 {
338 struct prison *pr;
339
340 KASSERT(cred != NULL, ("%s: cred is NULL", __func__));
341 KASSERT(ia != NULL, ("%s: ia is NULL", __func__));
342
343 pr = cred->cr_prison;
344 if (!(pr->pr_flags & PR_IP4))
345 return (0);
346 mtx_lock(&pr->pr_mtx);
347 if (!(pr->pr_flags & PR_IP4)) {
348 mtx_unlock(&pr->pr_mtx);
349 return (0);
350 }
351 if (pr->pr_ip4 == NULL) {
352 mtx_unlock(&pr->pr_mtx);
353 return (EAFNOSUPPORT);
354 }
355
356 if (ntohl(ia->s_addr) == INADDR_LOOPBACK &&
357 prison_check_ip4_locked(pr, ia) == EADDRNOTAVAIL) {
358 ia->s_addr = pr->pr_ip4[0].s_addr;
359 mtx_unlock(&pr->pr_mtx);
360 return (0);
361 }
362
363 /*
364 * Return success because nothing had to be changed.
365 */
366 mtx_unlock(&pr->pr_mtx);
367 return (0);
368 }
369
370 /*
371 * Check if given address belongs to the jail referenced by cred/prison.
372 *
373 * Returns 0 if address belongs to jail,
374 * EADDRNOTAVAIL if the address doesn't belong to the jail.
375 */
376 int
377 prison_check_ip4_locked(const struct prison *pr, const struct in_addr *ia)
378 {
379 int i, a, z, d;
380
381 /*
382 * Check the primary IP.
383 */
384 if (pr->pr_ip4[0].s_addr == ia->s_addr)
385 return (0);
386
387 /*
388 * All the other IPs are sorted so we can do a binary search.
389 */
390 a = 0;
391 z = pr->pr_ip4s - 2;
392 while (a <= z) {
393 i = (a + z) / 2;
394 d = prison_qcmp_v4(&pr->pr_ip4[i+1], ia);
395 if (d > 0)
396 z = i - 1;
397 else if (d < 0)
398 a = i + 1;
399 else
400 return (0);
401 }
402
403 return (EADDRNOTAVAIL);
404 }
405
406 int
407 prison_check_ip4(const struct ucred *cred, const struct in_addr *ia)
408 {
409 struct prison *pr;
410 int error;
411
412 KASSERT(cred != NULL, ("%s: cred is NULL", __func__));
413 KASSERT(ia != NULL, ("%s: ia is NULL", __func__));
414
415 pr = cred->cr_prison;
416 if (!(pr->pr_flags & PR_IP4))
417 return (0);
418 mtx_lock(&pr->pr_mtx);
419 if (!(pr->pr_flags & PR_IP4)) {
420 mtx_unlock(&pr->pr_mtx);
421 return (0);
422 }
423 if (pr->pr_ip4 == NULL) {
424 mtx_unlock(&pr->pr_mtx);
425 return (EAFNOSUPPORT);
426 }
427
428 error = prison_check_ip4_locked(pr, ia);
429 mtx_unlock(&pr->pr_mtx);
430 return (error);
431 }
Cache object: 2f05bf6700fdbc63e290fe1fecea18bc
|