]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - sys/netinet/in_jail.c
vnode_pager_input: return runningbufspace back
[FreeBSD/FreeBSD.git] / 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/namei.h>
55 #include <sys/mount.h>
56 #include <sys/queue.h>
57 #include <sys/socket.h>
58 #include <sys/syscallsubr.h>
59 #include <sys/sysctl.h>
60 #include <sys/vnode.h>
61
62 #include <net/if.h>
63 #include <net/vnet.h>
64
65 #include <netinet/in.h>
66
67 int
68 prison_qcmp_v4(const void *ip1, const void *ip2)
69 {
70         in_addr_t iaa, iab;
71
72         /*
73          * We need to compare in HBO here to get the list sorted as expected
74          * by the result of the code.  Sorting NBO addresses gives you
75          * interesting results.  If you do not understand, do not try.
76          */
77         iaa = ntohl(((const struct in_addr *)ip1)->s_addr);
78         iab = ntohl(((const struct in_addr *)ip2)->s_addr);
79
80         /*
81          * Do not simply return the difference of the two numbers, the int is
82          * not wide enough.
83          */
84         if (iaa > iab)
85                 return (1);
86         else if (iaa < iab)
87                 return (-1);
88         else
89                 return (0);
90 }
91
92 /*
93  * Restrict a prison's IP address list with its parent's, possibly replacing
94  * it.  Return true if the replacement buffer was used (or would have been).
95  */
96 int
97 prison_restrict_ip4(struct prison *pr, struct in_addr *newip4)
98 {
99         int ii, ij, used;
100         struct prison *ppr;
101
102         ppr = pr->pr_parent;
103         if (!(pr->pr_flags & PR_IP4_USER)) {
104                 /* This has no user settings, so just copy the parent's list. */
105                 if (pr->pr_ip4s < ppr->pr_ip4s) {
106                         /*
107                          * There's no room for the parent's list.  Use the
108                          * new list buffer, which is assumed to be big enough
109                          * (if it was passed).  If there's no buffer, try to
110                          * allocate one.
111                          */
112                         used = 1;
113                         if (newip4 == NULL) {
114                                 newip4 = malloc(ppr->pr_ip4s * sizeof(*newip4),
115                                     M_PRISON, M_NOWAIT);
116                                 if (newip4 != NULL)
117                                         used = 0;
118                         }
119                         if (newip4 != NULL) {
120                                 bcopy(ppr->pr_ip4, newip4,
121                                     ppr->pr_ip4s * sizeof(*newip4));
122                                 free(pr->pr_ip4, M_PRISON);
123                                 pr->pr_ip4 = newip4;
124                                 pr->pr_ip4s = ppr->pr_ip4s;
125                         }
126                         return (used);
127                 }
128                 pr->pr_ip4s = ppr->pr_ip4s;
129                 if (pr->pr_ip4s > 0)
130                         bcopy(ppr->pr_ip4, pr->pr_ip4,
131                             pr->pr_ip4s * sizeof(*newip4));
132                 else if (pr->pr_ip4 != NULL) {
133                         free(pr->pr_ip4, M_PRISON);
134                         pr->pr_ip4 = NULL;
135                 }
136         } else if (pr->pr_ip4s > 0) {
137                 /* Remove addresses that aren't in the parent. */
138                 for (ij = 0; ij < ppr->pr_ip4s; ij++)
139                         if (pr->pr_ip4[0].s_addr == ppr->pr_ip4[ij].s_addr)
140                                 break;
141                 if (ij < ppr->pr_ip4s)
142                         ii = 1;
143                 else {
144                         bcopy(pr->pr_ip4 + 1, pr->pr_ip4,
145                             --pr->pr_ip4s * sizeof(*pr->pr_ip4));
146                         ii = 0;
147                 }
148                 for (ij = 1; ii < pr->pr_ip4s; ) {
149                         if (pr->pr_ip4[ii].s_addr == ppr->pr_ip4[0].s_addr) {
150                                 ii++;
151                                 continue;
152                         }
153                         switch (ij >= ppr->pr_ip4s ? -1 :
154                                 prison_qcmp_v4(&pr->pr_ip4[ii], &ppr->pr_ip4[ij])) {
155                         case -1:
156                                 bcopy(pr->pr_ip4 + ii + 1, pr->pr_ip4 + ii,
157                                     (--pr->pr_ip4s - ii) * sizeof(*pr->pr_ip4));
158                                 break;
159                         case 0:
160                                 ii++;
161                                 ij++;
162                                 break;
163                         case 1:
164                                 ij++;
165                                 break;
166                         }
167                 }
168                 if (pr->pr_ip4s == 0) {
169                         free(pr->pr_ip4, M_PRISON);
170                         pr->pr_ip4 = NULL;
171                 }
172         }
173         return (0);
174 }
175
176 /*
177  * Pass back primary IPv4 address of this jail.
178  *
179  * If not restricted return success but do not alter the address.  Caller has
180  * to make sure to initialize it correctly (e.g. INADDR_ANY).
181  *
182  * Returns 0 on success, EAFNOSUPPORT if the jail doesn't allow IPv4.
183  * Address returned in NBO.
184  */
185 int
186 prison_get_ip4(struct ucred *cred, struct in_addr *ia)
187 {
188         struct prison *pr;
189
190         KASSERT(cred != NULL, ("%s: cred is NULL", __func__));
191         KASSERT(ia != NULL, ("%s: ia is NULL", __func__));
192
193         pr = cred->cr_prison;
194         if (!(pr->pr_flags & PR_IP4))
195                 return (0);
196         mtx_lock(&pr->pr_mtx);
197         if (!(pr->pr_flags & PR_IP4)) {
198                 mtx_unlock(&pr->pr_mtx);
199                 return (0);
200         }
201         if (pr->pr_ip4 == NULL) {
202                 mtx_unlock(&pr->pr_mtx);
203                 return (EAFNOSUPPORT);
204         }
205
206         ia->s_addr = pr->pr_ip4[0].s_addr;
207         mtx_unlock(&pr->pr_mtx);
208         return (0);
209 }
210
211 /*
212  * Return 1 if we should do proper source address selection or are not jailed.
213  * We will return 0 if we should bypass source address selection in favour
214  * of the primary jail IPv4 address. Only in this case *ia will be updated and
215  * returned in NBO.
216  * Return EAFNOSUPPORT, in case this jail does not allow IPv4.
217  */
218 int
219 prison_saddrsel_ip4(struct ucred *cred, struct in_addr *ia)
220 {
221         struct prison *pr;
222         struct in_addr lia;
223         int error;
224
225         KASSERT(cred != NULL, ("%s: cred is NULL", __func__));
226         KASSERT(ia != NULL, ("%s: ia is NULL", __func__));
227
228         if (!jailed(cred))
229                 return (1);
230
231         pr = cred->cr_prison;
232         if (pr->pr_flags & PR_IP4_SADDRSEL)
233                 return (1);
234
235         lia.s_addr = INADDR_ANY;
236         error = prison_get_ip4(cred, &lia);
237         if (error)
238                 return (error);
239         if (lia.s_addr == INADDR_ANY)
240                 return (1);
241
242         ia->s_addr = lia.s_addr;
243         return (0);
244 }
245
246 /*
247  * Return true if pr1 and pr2 have the same IPv4 address restrictions.
248  */
249 int
250 prison_equal_ip4(struct prison *pr1, struct prison *pr2)
251 {
252
253         if (pr1 == pr2)
254                 return (1);
255
256         /*
257          * No need to lock since the PR_IP4_USER flag can't be altered for
258          * existing prisons.
259          */
260         while (pr1 != &prison0 &&
261 #ifdef VIMAGE
262                !(pr1->pr_flags & PR_VNET) &&
263 #endif
264                !(pr1->pr_flags & PR_IP4_USER))
265                 pr1 = pr1->pr_parent;
266         while (pr2 != &prison0 &&
267 #ifdef VIMAGE
268                !(pr2->pr_flags & PR_VNET) &&
269 #endif
270                !(pr2->pr_flags & PR_IP4_USER))
271                 pr2 = pr2->pr_parent;
272         return (pr1 == pr2);
273 }
274
275 /*
276  * Make sure our (source) address is set to something meaningful to this
277  * jail.
278  *
279  * Returns 0 if jail doesn't restrict IPv4 or if address belongs to jail,
280  * EADDRNOTAVAIL if the address doesn't belong, or EAFNOSUPPORT if the jail
281  * doesn't allow IPv4.  Address passed in in NBO and returned in NBO.
282  */
283 int
284 prison_local_ip4(struct ucred *cred, struct in_addr *ia)
285 {
286         struct prison *pr;
287         struct in_addr ia0;
288         int error;
289
290         KASSERT(cred != NULL, ("%s: cred is NULL", __func__));
291         KASSERT(ia != NULL, ("%s: ia is NULL", __func__));
292
293         pr = cred->cr_prison;
294         if (!(pr->pr_flags & PR_IP4))
295                 return (0);
296         mtx_lock(&pr->pr_mtx);
297         if (!(pr->pr_flags & PR_IP4)) {
298                 mtx_unlock(&pr->pr_mtx);
299                 return (0);
300         }
301         if (pr->pr_ip4 == NULL) {
302                 mtx_unlock(&pr->pr_mtx);
303                 return (EAFNOSUPPORT);
304         }
305
306         ia0.s_addr = ntohl(ia->s_addr);
307
308         if (ia0.s_addr == INADDR_ANY) {
309                 /*
310                  * In case there is only 1 IPv4 address, bind directly.
311                  */
312                 if (pr->pr_ip4s == 1)
313                         ia->s_addr = pr->pr_ip4[0].s_addr;
314                 mtx_unlock(&pr->pr_mtx);
315                 return (0);
316         }
317
318         error = prison_check_ip4_locked(pr, ia);
319         if (error == EADDRNOTAVAIL && ia0.s_addr == INADDR_LOOPBACK) {
320                 ia->s_addr = pr->pr_ip4[0].s_addr;
321                 error = 0;
322         }
323
324         mtx_unlock(&pr->pr_mtx);
325         return (error);
326 }
327
328 /*
329  * Rewrite destination address in case we will connect to loopback address.
330  *
331  * Returns 0 on success, EAFNOSUPPORT if the jail doesn't allow IPv4.
332  * Address passed in in NBO and returned in NBO.
333  */
334 int
335 prison_remote_ip4(struct ucred *cred, struct in_addr *ia)
336 {
337         struct prison *pr;
338
339         KASSERT(cred != NULL, ("%s: cred is NULL", __func__));
340         KASSERT(ia != NULL, ("%s: ia is NULL", __func__));
341
342         pr = cred->cr_prison;
343         if (!(pr->pr_flags & PR_IP4))
344                 return (0);
345         mtx_lock(&pr->pr_mtx);
346         if (!(pr->pr_flags & PR_IP4)) {
347                 mtx_unlock(&pr->pr_mtx);
348                 return (0);
349         }
350         if (pr->pr_ip4 == NULL) {
351                 mtx_unlock(&pr->pr_mtx);
352                 return (EAFNOSUPPORT);
353         }
354
355         if (ntohl(ia->s_addr) == INADDR_LOOPBACK &&
356             prison_check_ip4_locked(pr, ia) == EADDRNOTAVAIL) {
357                 ia->s_addr = pr->pr_ip4[0].s_addr;
358                 mtx_unlock(&pr->pr_mtx);
359                 return (0);
360         }
361
362         /*
363          * Return success because nothing had to be changed.
364          */
365         mtx_unlock(&pr->pr_mtx);
366         return (0);
367 }
368
369 /*
370  * Check if given address belongs to the jail referenced by cred/prison.
371  *
372  * Returns 0 if address belongs to jail,
373  * EADDRNOTAVAIL if the address doesn't belong to the jail.
374  */
375 int
376 prison_check_ip4_locked(const struct prison *pr, const struct in_addr *ia)
377 {
378         int i, a, z, d;
379
380         /*
381          * Check the primary IP.
382          */
383         if (pr->pr_ip4[0].s_addr == ia->s_addr)
384                 return (0);
385
386         /*
387          * All the other IPs are sorted so we can do a binary search.
388          */
389         a = 0;
390         z = pr->pr_ip4s - 2;
391         while (a <= z) {
392                 i = (a + z) / 2;
393                 d = prison_qcmp_v4(&pr->pr_ip4[i+1], ia);
394                 if (d > 0)
395                         z = i - 1;
396                 else if (d < 0)
397                         a = i + 1;
398                 else
399                         return (0);
400         }
401
402         return (EADDRNOTAVAIL);
403 }
404
405 int
406 prison_check_ip4(const struct ucred *cred, const struct in_addr *ia)
407 {
408         struct prison *pr;
409         int error;
410
411         KASSERT(cred != NULL, ("%s: cred is NULL", __func__));
412         KASSERT(ia != NULL, ("%s: ia is NULL", __func__));
413
414         pr = cred->cr_prison;
415         if (!(pr->pr_flags & PR_IP4))
416                 return (0);
417         mtx_lock(&pr->pr_mtx);
418         if (!(pr->pr_flags & PR_IP4)) {
419                 mtx_unlock(&pr->pr_mtx);
420                 return (0);
421         }
422         if (pr->pr_ip4 == NULL) {
423                 mtx_unlock(&pr->pr_mtx);
424                 return (EAFNOSUPPORT);
425         }
426
427         error = prison_check_ip4_locked(pr, ia);
428         mtx_unlock(&pr->pr_mtx);
429         return (error);
430 }