FreeBSD/Linux Kernel Cross Reference
sys/kern/kern_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: releng/8.2/sys/kern/kern_jail.c 212510 2010-09-13 00:42:07Z jamie $");
31
32 #include "opt_compat.h"
33 #include "opt_ddb.h"
34 #include "opt_inet.h"
35 #include "opt_inet6.h"
36
37 #include <sys/param.h>
38 #include <sys/types.h>
39 #include <sys/kernel.h>
40 #include <sys/systm.h>
41 #include <sys/errno.h>
42 #include <sys/sysproto.h>
43 #include <sys/malloc.h>
44 #include <sys/osd.h>
45 #include <sys/priv.h>
46 #include <sys/proc.h>
47 #include <sys/taskqueue.h>
48 #include <sys/fcntl.h>
49 #include <sys/jail.h>
50 #include <sys/lock.h>
51 #include <sys/mutex.h>
52 #include <sys/sx.h>
53 #include <sys/sysent.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 #ifdef DDB
68 #include <ddb/ddb.h>
69 #ifdef INET6
70 #include <netinet6/in6_var.h>
71 #endif /* INET6 */
72 #endif /* DDB */
73
74 #include <security/mac/mac_framework.h>
75
76 #define DEFAULT_HOSTUUID "00000000-0000-0000-0000-000000000000"
77
78 MALLOC_DEFINE(M_PRISON, "prison", "Prison structures");
79
80 /* Keep struct prison prison0 and some code in kern_jail_set() readable. */
81 #ifdef INET
82 #ifdef INET6
83 #define _PR_IP_SADDRSEL PR_IP4_SADDRSEL|PR_IP6_SADDRSEL
84 #else
85 #define _PR_IP_SADDRSEL PR_IP4_SADDRSEL
86 #endif
87 #else /* !INET */
88 #ifdef INET6
89 #define _PR_IP_SADDRSEL PR_IP6_SADDRSEL
90 #else
91 #define _PR_IP_SADDRSEL 0
92 #endif
93 #endif
94
95 /* prison0 describes what is "real" about the system. */
96 struct prison prison0 = {
97 .pr_id = 0,
98 .pr_name = "",
99 .pr_ref = 1,
100 .pr_uref = 1,
101 .pr_path = "/",
102 .pr_securelevel = -1,
103 .pr_childmax = JAIL_MAX,
104 .pr_hostuuid = DEFAULT_HOSTUUID,
105 .pr_children = LIST_HEAD_INITIALIZER(prison0.pr_children),
106 #ifdef VIMAGE
107 .pr_flags = PR_HOST|PR_VNET|_PR_IP_SADDRSEL,
108 #else
109 .pr_flags = PR_HOST|_PR_IP_SADDRSEL,
110 #endif
111 .pr_allow = PR_ALLOW_ALL,
112 };
113 MTX_SYSINIT(prison0, &prison0.pr_mtx, "jail mutex", MTX_DEF);
114
115 /* allprison and lastprid are protected by allprison_lock. */
116 struct sx allprison_lock;
117 SX_SYSINIT(allprison_lock, &allprison_lock, "allprison");
118 struct prisonlist allprison = TAILQ_HEAD_INITIALIZER(allprison);
119 int lastprid = 0;
120
121 static int do_jail_attach(struct thread *td, struct prison *pr);
122 static void prison_complete(void *context, int pending);
123 static void prison_deref(struct prison *pr, int flags);
124 static char *prison_path(struct prison *pr1, struct prison *pr2);
125 static void prison_remove_one(struct prison *pr);
126 #ifdef INET
127 static int _prison_check_ip4(struct prison *pr, struct in_addr *ia);
128 static int prison_restrict_ip4(struct prison *pr, struct in_addr *newip4);
129 #endif
130 #ifdef INET6
131 static int _prison_check_ip6(struct prison *pr, struct in6_addr *ia6);
132 static int prison_restrict_ip6(struct prison *pr, struct in6_addr *newip6);
133 #endif
134
135 /* Flags for prison_deref */
136 #define PD_DEREF 0x01
137 #define PD_DEUREF 0x02
138 #define PD_LOCKED 0x04
139 #define PD_LIST_SLOCKED 0x08
140 #define PD_LIST_XLOCKED 0x10
141
142 /*
143 * Parameter names corresponding to PR_* flag values
144 */
145 static char *pr_flag_names[] = {
146 [0] = "persist",
147 #ifdef INET
148 [7] = "ip4.saddrsel",
149 #endif
150 #ifdef INET6
151 [8] = "ip6.saddrsel",
152 #endif
153 };
154
155 static char *pr_flag_nonames[] = {
156 [0] = "nopersist",
157 #ifdef INET
158 [7] = "ip4.nosaddrsel",
159 #endif
160 #ifdef INET6
161 [8] = "ip6.nosaddrsel",
162 #endif
163 };
164
165 struct jailsys_flags {
166 const char *name;
167 unsigned disable;
168 unsigned new;
169 } pr_flag_jailsys[] = {
170 { "host", 0, PR_HOST },
171 #ifdef VIMAGE
172 { "vnet", 0, PR_VNET },
173 #endif
174 #ifdef INET
175 { "ip4", PR_IP4_USER | PR_IP4_DISABLE, PR_IP4_USER },
176 #endif
177 #ifdef INET6
178 { "ip6", PR_IP6_USER | PR_IP6_DISABLE, PR_IP6_USER },
179 #endif
180 };
181
182 static char *pr_allow_names[] = {
183 "allow.set_hostname",
184 "allow.sysvipc",
185 "allow.raw_sockets",
186 "allow.chflags",
187 "allow.mount",
188 "allow.quotas",
189 "allow.socket_af",
190 };
191
192 static char *pr_allow_nonames[] = {
193 "allow.noset_hostname",
194 "allow.nosysvipc",
195 "allow.noraw_sockets",
196 "allow.nochflags",
197 "allow.nomount",
198 "allow.noquotas",
199 "allow.nosocket_af",
200 };
201
202 #define JAIL_DEFAULT_ALLOW PR_ALLOW_SET_HOSTNAME
203 #define JAIL_DEFAULT_ENFORCE_STATFS 2
204 static unsigned jail_default_allow = JAIL_DEFAULT_ALLOW;
205 static int jail_default_enforce_statfs = JAIL_DEFAULT_ENFORCE_STATFS;
206 #if defined(INET) || defined(INET6)
207 static unsigned jail_max_af_ips = 255;
208 #endif
209
210 #ifdef INET
211 static int
212 qcmp_v4(const void *ip1, const void *ip2)
213 {
214 in_addr_t iaa, iab;
215
216 /*
217 * We need to compare in HBO here to get the list sorted as expected
218 * by the result of the code. Sorting NBO addresses gives you
219 * interesting results. If you do not understand, do not try.
220 */
221 iaa = ntohl(((const struct in_addr *)ip1)->s_addr);
222 iab = ntohl(((const struct in_addr *)ip2)->s_addr);
223
224 /*
225 * Do not simply return the difference of the two numbers, the int is
226 * not wide enough.
227 */
228 if (iaa > iab)
229 return (1);
230 else if (iaa < iab)
231 return (-1);
232 else
233 return (0);
234 }
235 #endif
236
237 #ifdef INET6
238 static int
239 qcmp_v6(const void *ip1, const void *ip2)
240 {
241 const struct in6_addr *ia6a, *ia6b;
242 int i, rc;
243
244 ia6a = (const struct in6_addr *)ip1;
245 ia6b = (const struct in6_addr *)ip2;
246
247 rc = 0;
248 for (i = 0; rc == 0 && i < sizeof(struct in6_addr); i++) {
249 if (ia6a->s6_addr[i] > ia6b->s6_addr[i])
250 rc = 1;
251 else if (ia6a->s6_addr[i] < ia6b->s6_addr[i])
252 rc = -1;
253 }
254 return (rc);
255 }
256 #endif
257
258 /*
259 * struct jail_args {
260 * struct jail *jail;
261 * };
262 */
263 int
264 jail(struct thread *td, struct jail_args *uap)
265 {
266 uint32_t version;
267 int error;
268 struct jail j;
269
270 error = copyin(uap->jail, &version, sizeof(uint32_t));
271 if (error)
272 return (error);
273
274 switch (version) {
275 case 0:
276 {
277 struct jail_v0 j0;
278
279 /* FreeBSD single IPv4 jails. */
280 bzero(&j, sizeof(struct jail));
281 error = copyin(uap->jail, &j0, sizeof(struct jail_v0));
282 if (error)
283 return (error);
284 j.version = j0.version;
285 j.path = j0.path;
286 j.hostname = j0.hostname;
287 j.ip4s = j0.ip_number;
288 break;
289 }
290
291 case 1:
292 /*
293 * Version 1 was used by multi-IPv4 jail implementations
294 * that never made it into the official kernel.
295 */
296 return (EINVAL);
297
298 case 2: /* JAIL_API_VERSION */
299 /* FreeBSD multi-IPv4/IPv6,noIP jails. */
300 error = copyin(uap->jail, &j, sizeof(struct jail));
301 if (error)
302 return (error);
303 break;
304
305 default:
306 /* Sci-Fi jails are not supported, sorry. */
307 return (EINVAL);
308 }
309 return (kern_jail(td, &j));
310 }
311
312 int
313 kern_jail(struct thread *td, struct jail *j)
314 {
315 struct iovec optiov[2 * (4
316 + sizeof(pr_allow_names) / sizeof(pr_allow_names[0])
317 #ifdef INET
318 + 1
319 #endif
320 #ifdef INET6
321 + 1
322 #endif
323 )];
324 struct uio opt;
325 char *u_path, *u_hostname, *u_name;
326 #ifdef INET
327 uint32_t ip4s;
328 struct in_addr *u_ip4;
329 #endif
330 #ifdef INET6
331 struct in6_addr *u_ip6;
332 #endif
333 size_t tmplen;
334 int error, enforce_statfs, fi;
335
336 bzero(&optiov, sizeof(optiov));
337 opt.uio_iov = optiov;
338 opt.uio_iovcnt = 0;
339 opt.uio_offset = -1;
340 opt.uio_resid = -1;
341 opt.uio_segflg = UIO_SYSSPACE;
342 opt.uio_rw = UIO_READ;
343 opt.uio_td = td;
344
345 /* Set permissions for top-level jails from sysctls. */
346 if (!jailed(td->td_ucred)) {
347 for (fi = 0; fi < sizeof(pr_allow_names) /
348 sizeof(pr_allow_names[0]); fi++) {
349 optiov[opt.uio_iovcnt].iov_base =
350 (jail_default_allow & (1 << fi))
351 ? pr_allow_names[fi] : pr_allow_nonames[fi];
352 optiov[opt.uio_iovcnt].iov_len =
353 strlen(optiov[opt.uio_iovcnt].iov_base) + 1;
354 opt.uio_iovcnt += 2;
355 }
356 optiov[opt.uio_iovcnt].iov_base = "enforce_statfs";
357 optiov[opt.uio_iovcnt].iov_len = sizeof("enforce_statfs");
358 opt.uio_iovcnt++;
359 enforce_statfs = jail_default_enforce_statfs;
360 optiov[opt.uio_iovcnt].iov_base = &enforce_statfs;
361 optiov[opt.uio_iovcnt].iov_len = sizeof(enforce_statfs);
362 opt.uio_iovcnt++;
363 }
364
365 tmplen = MAXPATHLEN + MAXHOSTNAMELEN + MAXHOSTNAMELEN;
366 #ifdef INET
367 ip4s = (j->version == 0) ? 1 : j->ip4s;
368 if (ip4s > jail_max_af_ips)
369 return (EINVAL);
370 tmplen += ip4s * sizeof(struct in_addr);
371 #else
372 if (j->ip4s > 0)
373 return (EINVAL);
374 #endif
375 #ifdef INET6
376 if (j->ip6s > jail_max_af_ips)
377 return (EINVAL);
378 tmplen += j->ip6s * sizeof(struct in6_addr);
379 #else
380 if (j->ip6s > 0)
381 return (EINVAL);
382 #endif
383 u_path = malloc(tmplen, M_TEMP, M_WAITOK);
384 u_hostname = u_path + MAXPATHLEN;
385 u_name = u_hostname + MAXHOSTNAMELEN;
386 #ifdef INET
387 u_ip4 = (struct in_addr *)(u_name + MAXHOSTNAMELEN);
388 #endif
389 #ifdef INET6
390 #ifdef INET
391 u_ip6 = (struct in6_addr *)(u_ip4 + ip4s);
392 #else
393 u_ip6 = (struct in6_addr *)(u_name + MAXHOSTNAMELEN);
394 #endif
395 #endif
396 optiov[opt.uio_iovcnt].iov_base = "path";
397 optiov[opt.uio_iovcnt].iov_len = sizeof("path");
398 opt.uio_iovcnt++;
399 optiov[opt.uio_iovcnt].iov_base = u_path;
400 error = copyinstr(j->path, u_path, MAXPATHLEN,
401 &optiov[opt.uio_iovcnt].iov_len);
402 if (error) {
403 free(u_path, M_TEMP);
404 return (error);
405 }
406 opt.uio_iovcnt++;
407 optiov[opt.uio_iovcnt].iov_base = "host.hostname";
408 optiov[opt.uio_iovcnt].iov_len = sizeof("host.hostname");
409 opt.uio_iovcnt++;
410 optiov[opt.uio_iovcnt].iov_base = u_hostname;
411 error = copyinstr(j->hostname, u_hostname, MAXHOSTNAMELEN,
412 &optiov[opt.uio_iovcnt].iov_len);
413 if (error) {
414 free(u_path, M_TEMP);
415 return (error);
416 }
417 opt.uio_iovcnt++;
418 if (j->jailname != NULL) {
419 optiov[opt.uio_iovcnt].iov_base = "name";
420 optiov[opt.uio_iovcnt].iov_len = sizeof("name");
421 opt.uio_iovcnt++;
422 optiov[opt.uio_iovcnt].iov_base = u_name;
423 error = copyinstr(j->jailname, u_name, MAXHOSTNAMELEN,
424 &optiov[opt.uio_iovcnt].iov_len);
425 if (error) {
426 free(u_path, M_TEMP);
427 return (error);
428 }
429 opt.uio_iovcnt++;
430 }
431 #ifdef INET
432 optiov[opt.uio_iovcnt].iov_base = "ip4.addr";
433 optiov[opt.uio_iovcnt].iov_len = sizeof("ip4.addr");
434 opt.uio_iovcnt++;
435 optiov[opt.uio_iovcnt].iov_base = u_ip4;
436 optiov[opt.uio_iovcnt].iov_len = ip4s * sizeof(struct in_addr);
437 if (j->version == 0)
438 u_ip4->s_addr = j->ip4s;
439 else {
440 error = copyin(j->ip4, u_ip4, optiov[opt.uio_iovcnt].iov_len);
441 if (error) {
442 free(u_path, M_TEMP);
443 return (error);
444 }
445 }
446 opt.uio_iovcnt++;
447 #endif
448 #ifdef INET6
449 optiov[opt.uio_iovcnt].iov_base = "ip6.addr";
450 optiov[opt.uio_iovcnt].iov_len = sizeof("ip6.addr");
451 opt.uio_iovcnt++;
452 optiov[opt.uio_iovcnt].iov_base = u_ip6;
453 optiov[opt.uio_iovcnt].iov_len = j->ip6s * sizeof(struct in6_addr);
454 error = copyin(j->ip6, u_ip6, optiov[opt.uio_iovcnt].iov_len);
455 if (error) {
456 free(u_path, M_TEMP);
457 return (error);
458 }
459 opt.uio_iovcnt++;
460 #endif
461 KASSERT(opt.uio_iovcnt <= sizeof(optiov) / sizeof(optiov[0]),
462 ("kern_jail: too many iovecs (%d)", opt.uio_iovcnt));
463 error = kern_jail_set(td, &opt, JAIL_CREATE | JAIL_ATTACH);
464 free(u_path, M_TEMP);
465 return (error);
466 }
467
468
469 /*
470 * struct jail_set_args {
471 * struct iovec *iovp;
472 * unsigned int iovcnt;
473 * int flags;
474 * };
475 */
476 int
477 jail_set(struct thread *td, struct jail_set_args *uap)
478 {
479 struct uio *auio;
480 int error;
481
482 /* Check that we have an even number of iovecs. */
483 if (uap->iovcnt & 1)
484 return (EINVAL);
485
486 error = copyinuio(uap->iovp, uap->iovcnt, &auio);
487 if (error)
488 return (error);
489 error = kern_jail_set(td, auio, uap->flags);
490 free(auio, M_IOV);
491 return (error);
492 }
493
494 int
495 kern_jail_set(struct thread *td, struct uio *optuio, int flags)
496 {
497 struct nameidata nd;
498 #ifdef INET
499 struct in_addr *ip4;
500 #endif
501 #ifdef INET6
502 struct in6_addr *ip6;
503 #endif
504 struct vfsopt *opt;
505 struct vfsoptlist *opts;
506 struct prison *pr, *deadpr, *mypr, *ppr, *tpr;
507 struct vnode *root;
508 char *domain, *errmsg, *host, *name, *namelc, *p, *path, *uuid;
509 #if defined(INET) || defined(INET6)
510 struct prison *tppr;
511 void *op;
512 #endif
513 unsigned long hid;
514 size_t namelen, onamelen;
515 int created, cuflags, descend, enforce, error, errmsg_len, errmsg_pos;
516 int gotchildmax, gotenforce, gothid, gotslevel;
517 int fi, jid, jsys, len, level;
518 int childmax, slevel, vfslocked;
519 #if defined(INET) || defined(INET6)
520 int ii, ij;
521 #endif
522 #ifdef INET
523 int ip4s, redo_ip4;
524 #endif
525 #ifdef INET6
526 int ip6s, redo_ip6;
527 #endif
528 unsigned pr_flags, ch_flags;
529 unsigned pr_allow, ch_allow, tallow;
530 char numbuf[12];
531
532 error = priv_check(td, PRIV_JAIL_SET);
533 if (!error && (flags & JAIL_ATTACH))
534 error = priv_check(td, PRIV_JAIL_ATTACH);
535 if (error)
536 return (error);
537 mypr = ppr = td->td_ucred->cr_prison;
538 if ((flags & JAIL_CREATE) && mypr->pr_childmax == 0)
539 return (EPERM);
540 if (flags & ~JAIL_SET_MASK)
541 return (EINVAL);
542
543 /*
544 * Check all the parameters before committing to anything. Not all
545 * errors can be caught early, but we may as well try. Also, this
546 * takes care of some expensive stuff (path lookup) before getting
547 * the allprison lock.
548 *
549 * XXX Jails are not filesystems, and jail parameters are not mount
550 * options. But it makes more sense to re-use the vfsopt code
551 * than duplicate it under a different name.
552 */
553 error = vfs_buildopts(optuio, &opts);
554 if (error)
555 return (error);
556 #ifdef INET
557 ip4 = NULL;
558 #endif
559 #ifdef INET6
560 ip6 = NULL;
561 #endif
562
563 error = vfs_copyopt(opts, "jid", &jid, sizeof(jid));
564 if (error == ENOENT)
565 jid = 0;
566 else if (error != 0)
567 goto done_free;
568
569 error = vfs_copyopt(opts, "securelevel", &slevel, sizeof(slevel));
570 if (error == ENOENT)
571 gotslevel = 0;
572 else if (error != 0)
573 goto done_free;
574 else
575 gotslevel = 1;
576
577 error =
578 vfs_copyopt(opts, "children.max", &childmax, sizeof(childmax));
579 if (error == ENOENT)
580 gotchildmax = 0;
581 else if (error != 0)
582 goto done_free;
583 else
584 gotchildmax = 1;
585
586 error = vfs_copyopt(opts, "enforce_statfs", &enforce, sizeof(enforce));
587 if (error == ENOENT)
588 gotenforce = 0;
589 else if (error != 0)
590 goto done_free;
591 else if (enforce < 0 || enforce > 2) {
592 error = EINVAL;
593 goto done_free;
594 } else
595 gotenforce = 1;
596
597 pr_flags = ch_flags = 0;
598 for (fi = 0; fi < sizeof(pr_flag_names) / sizeof(pr_flag_names[0]);
599 fi++) {
600 if (pr_flag_names[fi] == NULL)
601 continue;
602 vfs_flagopt(opts, pr_flag_names[fi], &pr_flags, 1 << fi);
603 vfs_flagopt(opts, pr_flag_nonames[fi], &ch_flags, 1 << fi);
604 }
605 ch_flags |= pr_flags;
606 for (fi = 0; fi < sizeof(pr_flag_jailsys) / sizeof(pr_flag_jailsys[0]);
607 fi++) {
608 error = vfs_copyopt(opts, pr_flag_jailsys[fi].name, &jsys,
609 sizeof(jsys));
610 if (error == ENOENT)
611 continue;
612 if (error != 0)
613 goto done_free;
614 switch (jsys) {
615 case JAIL_SYS_DISABLE:
616 if (!pr_flag_jailsys[fi].disable) {
617 error = EINVAL;
618 goto done_free;
619 }
620 pr_flags |= pr_flag_jailsys[fi].disable;
621 break;
622 case JAIL_SYS_NEW:
623 pr_flags |= pr_flag_jailsys[fi].new;
624 break;
625 case JAIL_SYS_INHERIT:
626 break;
627 default:
628 error = EINVAL;
629 goto done_free;
630 }
631 ch_flags |=
632 pr_flag_jailsys[fi].new | pr_flag_jailsys[fi].disable;
633 }
634 if ((flags & (JAIL_CREATE | JAIL_UPDATE | JAIL_ATTACH)) == JAIL_CREATE
635 && !(pr_flags & PR_PERSIST)) {
636 error = EINVAL;
637 vfs_opterror(opts, "new jail must persist or attach");
638 goto done_errmsg;
639 }
640 #ifdef VIMAGE
641 if ((flags & JAIL_UPDATE) && (ch_flags & PR_VNET)) {
642 error = EINVAL;
643 vfs_opterror(opts, "vnet cannot be changed after creation");
644 goto done_errmsg;
645 }
646 #endif
647 #ifdef INET
648 if ((flags & JAIL_UPDATE) && (ch_flags & PR_IP4_USER)) {
649 error = EINVAL;
650 vfs_opterror(opts, "ip4 cannot be changed after creation");
651 goto done_errmsg;
652 }
653 #endif
654 #ifdef INET6
655 if ((flags & JAIL_UPDATE) && (ch_flags & PR_IP6_USER)) {
656 error = EINVAL;
657 vfs_opterror(opts, "ip6 cannot be changed after creation");
658 goto done_errmsg;
659 }
660 #endif
661
662 pr_allow = ch_allow = 0;
663 for (fi = 0; fi < sizeof(pr_allow_names) / sizeof(pr_allow_names[0]);
664 fi++) {
665 vfs_flagopt(opts, pr_allow_names[fi], &pr_allow, 1 << fi);
666 vfs_flagopt(opts, pr_allow_nonames[fi], &ch_allow, 1 << fi);
667 }
668 ch_allow |= pr_allow;
669
670 error = vfs_getopt(opts, "name", (void **)&name, &len);
671 if (error == ENOENT)
672 name = NULL;
673 else if (error != 0)
674 goto done_free;
675 else {
676 if (len == 0 || name[len - 1] != '\0') {
677 error = EINVAL;
678 goto done_free;
679 }
680 if (len > MAXHOSTNAMELEN) {
681 error = ENAMETOOLONG;
682 goto done_free;
683 }
684 }
685
686 error = vfs_getopt(opts, "host.hostname", (void **)&host, &len);
687 if (error == ENOENT)
688 host = NULL;
689 else if (error != 0)
690 goto done_free;
691 else {
692 ch_flags |= PR_HOST;
693 pr_flags |= PR_HOST;
694 if (len == 0 || host[len - 1] != '\0') {
695 error = EINVAL;
696 goto done_free;
697 }
698 if (len > MAXHOSTNAMELEN) {
699 error = ENAMETOOLONG;
700 goto done_free;
701 }
702 }
703
704 error = vfs_getopt(opts, "host.domainname", (void **)&domain, &len);
705 if (error == ENOENT)
706 domain = NULL;
707 else if (error != 0)
708 goto done_free;
709 else {
710 ch_flags |= PR_HOST;
711 pr_flags |= PR_HOST;
712 if (len == 0 || domain[len - 1] != '\0') {
713 error = EINVAL;
714 goto done_free;
715 }
716 if (len > MAXHOSTNAMELEN) {
717 error = ENAMETOOLONG;
718 goto done_free;
719 }
720 }
721
722 error = vfs_getopt(opts, "host.hostuuid", (void **)&uuid, &len);
723 if (error == ENOENT)
724 uuid = NULL;
725 else if (error != 0)
726 goto done_free;
727 else {
728 ch_flags |= PR_HOST;
729 pr_flags |= PR_HOST;
730 if (len == 0 || uuid[len - 1] != '\0') {
731 error = EINVAL;
732 goto done_free;
733 }
734 if (len > HOSTUUIDLEN) {
735 error = ENAMETOOLONG;
736 goto done_free;
737 }
738 }
739
740 #ifdef COMPAT_FREEBSD32
741 if (td->td_proc->p_sysent->sv_flags & SV_ILP32) {
742 uint32_t hid32;
743
744 error = vfs_copyopt(opts, "host.hostid", &hid32, sizeof(hid32));
745 hid = hid32;
746 } else
747 #endif
748 error = vfs_copyopt(opts, "host.hostid", &hid, sizeof(hid));
749 if (error == ENOENT)
750 gothid = 0;
751 else if (error != 0)
752 goto done_free;
753 else {
754 gothid = 1;
755 ch_flags |= PR_HOST;
756 pr_flags |= PR_HOST;
757 }
758
759 #ifdef INET
760 error = vfs_getopt(opts, "ip4.addr", &op, &ip4s);
761 if (error == ENOENT)
762 ip4s = (pr_flags & PR_IP4_DISABLE) ? 0 : -1;
763 else if (error != 0)
764 goto done_free;
765 else if (ip4s & (sizeof(*ip4) - 1)) {
766 error = EINVAL;
767 goto done_free;
768 } else {
769 ch_flags |= PR_IP4_USER | PR_IP4_DISABLE;
770 if (ip4s == 0)
771 pr_flags |= PR_IP4_USER | PR_IP4_DISABLE;
772 else {
773 pr_flags = (pr_flags & ~PR_IP4_DISABLE) | PR_IP4_USER;
774 ip4s /= sizeof(*ip4);
775 if (ip4s > jail_max_af_ips) {
776 error = EINVAL;
777 vfs_opterror(opts, "too many IPv4 addresses");
778 goto done_errmsg;
779 }
780 ip4 = malloc(ip4s * sizeof(*ip4), M_PRISON, M_WAITOK);
781 bcopy(op, ip4, ip4s * sizeof(*ip4));
782 /*
783 * IP addresses are all sorted but ip[0] to preserve
784 * the primary IP address as given from userland.
785 * This special IP is used for unbound outgoing
786 * connections as well for "loopback" traffic in case
787 * source address selection cannot find any more fitting
788 * address to connect from.
789 */
790 if (ip4s > 1)
791 qsort(ip4 + 1, ip4s - 1, sizeof(*ip4), qcmp_v4);
792 /*
793 * Check for duplicate addresses and do some simple
794 * zero and broadcast checks. If users give other bogus
795 * addresses it is their problem.
796 *
797 * We do not have to care about byte order for these
798 * checks so we will do them in NBO.
799 */
800 for (ii = 0; ii < ip4s; ii++) {
801 if (ip4[ii].s_addr == INADDR_ANY ||
802 ip4[ii].s_addr == INADDR_BROADCAST) {
803 error = EINVAL;
804 goto done_free;
805 }
806 if ((ii+1) < ip4s &&
807 (ip4[0].s_addr == ip4[ii+1].s_addr ||
808 ip4[ii].s_addr == ip4[ii+1].s_addr)) {
809 error = EINVAL;
810 goto done_free;
811 }
812 }
813 }
814 }
815 #endif
816
817 #ifdef INET6
818 error = vfs_getopt(opts, "ip6.addr", &op, &ip6s);
819 if (error == ENOENT)
820 ip6s = (pr_flags & PR_IP6_DISABLE) ? 0 : -1;
821 else if (error != 0)
822 goto done_free;
823 else if (ip6s & (sizeof(*ip6) - 1)) {
824 error = EINVAL;
825 goto done_free;
826 } else {
827 ch_flags |= PR_IP6_USER | PR_IP6_DISABLE;
828 if (ip6s == 0)
829 pr_flags |= PR_IP6_USER | PR_IP6_DISABLE;
830 else {
831 pr_flags = (pr_flags & ~PR_IP6_DISABLE) | PR_IP6_USER;
832 ip6s /= sizeof(*ip6);
833 if (ip6s > jail_max_af_ips) {
834 error = EINVAL;
835 vfs_opterror(opts, "too many IPv6 addresses");
836 goto done_errmsg;
837 }
838 ip6 = malloc(ip6s * sizeof(*ip6), M_PRISON, M_WAITOK);
839 bcopy(op, ip6, ip6s * sizeof(*ip6));
840 if (ip6s > 1)
841 qsort(ip6 + 1, ip6s - 1, sizeof(*ip6), qcmp_v6);
842 for (ii = 0; ii < ip6s; ii++) {
843 if (IN6_IS_ADDR_UNSPECIFIED(&ip6[ii])) {
844 error = EINVAL;
845 goto done_free;
846 }
847 if ((ii+1) < ip6s &&
848 (IN6_ARE_ADDR_EQUAL(&ip6[0], &ip6[ii+1]) ||
849 IN6_ARE_ADDR_EQUAL(&ip6[ii], &ip6[ii+1])))
850 {
851 error = EINVAL;
852 goto done_free;
853 }
854 }
855 }
856 }
857 #endif
858
859 #if defined(VIMAGE) && (defined(INET) || defined(INET6))
860 if ((ch_flags & PR_VNET) && (ch_flags & (PR_IP4_USER | PR_IP6_USER))) {
861 error = EINVAL;
862 vfs_opterror(opts,
863 "vnet jails cannot have IP address restrictions");
864 goto done_errmsg;
865 }
866 #endif
867
868 root = NULL;
869 error = vfs_getopt(opts, "path", (void **)&path, &len);
870 if (error == ENOENT)
871 path = NULL;
872 else if (error != 0)
873 goto done_free;
874 else {
875 if (flags & JAIL_UPDATE) {
876 error = EINVAL;
877 vfs_opterror(opts,
878 "path cannot be changed after creation");
879 goto done_errmsg;
880 }
881 if (len == 0 || path[len - 1] != '\0') {
882 error = EINVAL;
883 goto done_free;
884 }
885 if (len < 2 || (len == 2 && path[0] == '/'))
886 path = NULL;
887 else {
888 /* Leave room for a real-root full pathname. */
889 if (len + (path[0] == '/' && strcmp(mypr->pr_path, "/")
890 ? strlen(mypr->pr_path) : 0) > MAXPATHLEN) {
891 error = ENAMETOOLONG;
892 goto done_free;
893 }
894 NDINIT(&nd, LOOKUP, MPSAFE | FOLLOW, UIO_SYSSPACE,
895 path, td);
896 error = namei(&nd);
897 if (error)
898 goto done_free;
899 vfslocked = NDHASGIANT(&nd);
900 root = nd.ni_vp;
901 NDFREE(&nd, NDF_ONLY_PNBUF);
902 if (root->v_type != VDIR) {
903 error = ENOTDIR;
904 vrele(root);
905 VFS_UNLOCK_GIANT(vfslocked);
906 goto done_free;
907 }
908 VFS_UNLOCK_GIANT(vfslocked);
909 }
910 }
911
912 /*
913 * Grab the allprison lock before letting modules check their
914 * parameters. Once we have it, do not let go so we'll have a
915 * consistent view of the OSD list.
916 */
917 sx_xlock(&allprison_lock);
918 error = osd_jail_call(NULL, PR_METHOD_CHECK, opts);
919 if (error)
920 goto done_unlock_list;
921
922 /* By now, all parameters should have been noted. */
923 TAILQ_FOREACH(opt, opts, link) {
924 if (!opt->seen && strcmp(opt->name, "errmsg")) {
925 error = EINVAL;
926 vfs_opterror(opts, "unknown parameter: %s", opt->name);
927 goto done_unlock_list;
928 }
929 }
930
931 /*
932 * See if we are creating a new record or updating an existing one.
933 * This abuses the file error codes ENOENT and EEXIST.
934 */
935 cuflags = flags & (JAIL_CREATE | JAIL_UPDATE);
936 if (!cuflags) {
937 error = EINVAL;
938 vfs_opterror(opts, "no valid operation (create or update)");
939 goto done_unlock_list;
940 }
941 pr = NULL;
942 namelc = NULL;
943 if (cuflags == JAIL_CREATE && jid == 0 && name != NULL) {
944 namelc = strrchr(name, '.');
945 jid = strtoul(namelc != NULL ? namelc + 1 : name, &p, 10);
946 if (*p != '\0')
947 jid = 0;
948 }
949 if (jid != 0) {
950 /*
951 * See if a requested jid already exists. There is an
952 * information leak here if the jid exists but is not within
953 * the caller's jail hierarchy. Jail creators will get EEXIST
954 * even though they cannot see the jail, and CREATE | UPDATE
955 * will return ENOENT which is not normally a valid error.
956 */
957 if (jid < 0) {
958 error = EINVAL;
959 vfs_opterror(opts, "negative jid");
960 goto done_unlock_list;
961 }
962 pr = prison_find(jid);
963 if (pr != NULL) {
964 ppr = pr->pr_parent;
965 /* Create: jid must not exist. */
966 if (cuflags == JAIL_CREATE) {
967 mtx_unlock(&pr->pr_mtx);
968 error = EEXIST;
969 vfs_opterror(opts, "jail %d already exists",
970 jid);
971 goto done_unlock_list;
972 }
973 if (!prison_ischild(mypr, pr)) {
974 mtx_unlock(&pr->pr_mtx);
975 pr = NULL;
976 } else if (pr->pr_uref == 0) {
977 if (!(flags & JAIL_DYING)) {
978 mtx_unlock(&pr->pr_mtx);
979 error = ENOENT;
980 vfs_opterror(opts, "jail %d is dying",
981 jid);
982 goto done_unlock_list;
983 } else if ((flags & JAIL_ATTACH) ||
984 (pr_flags & PR_PERSIST)) {
985 /*
986 * A dying jail might be resurrected
987 * (via attach or persist), but first
988 * it must determine if another jail
989 * has claimed its name. Accomplish
990 * this by implicitly re-setting the
991 * name.
992 */
993 if (name == NULL)
994 name = prison_name(mypr, pr);
995 }
996 }
997 }
998 if (pr == NULL) {
999 /* Update: jid must exist. */
1000 if (cuflags == JAIL_UPDATE) {
1001 error = ENOENT;
1002 vfs_opterror(opts, "jail %d not found", jid);
1003 goto done_unlock_list;
1004 }
1005 }
1006 }
1007 /*
1008 * If the caller provided a name, look for a jail by that name.
1009 * This has different semantics for creates and updates keyed by jid
1010 * (where the name must not already exist in a different jail),
1011 * and updates keyed by the name itself (where the name must exist
1012 * because that is the jail being updated).
1013 */
1014 if (name != NULL) {
1015 namelc = strrchr(name, '.');
1016 if (namelc == NULL)
1017 namelc = name;
1018 else {
1019 /*
1020 * This is a hierarchical name. Split it into the
1021 * parent and child names, and make sure the parent
1022 * exists or matches an already found jail.
1023 */
1024 *namelc = '\0';
1025 if (pr != NULL) {
1026 if (strncmp(name, ppr->pr_name, namelc - name)
1027 || ppr->pr_name[namelc - name] != '\0') {
1028 mtx_unlock(&pr->pr_mtx);
1029 error = EINVAL;
1030 vfs_opterror(opts,
1031 "cannot change jail's parent");
1032 goto done_unlock_list;
1033 }
1034 } else {
1035 ppr = prison_find_name(mypr, name);
1036 if (ppr == NULL) {
1037 error = ENOENT;
1038 vfs_opterror(opts,
1039 "jail \"%s\" not found", name);
1040 goto done_unlock_list;
1041 }
1042 mtx_unlock(&ppr->pr_mtx);
1043 }
1044 name = ++namelc;
1045 }
1046 if (name[0] != '\0') {
1047 namelen =
1048 (ppr == &prison0) ? 0 : strlen(ppr->pr_name) + 1;
1049 name_again:
1050 deadpr = NULL;
1051 FOREACH_PRISON_CHILD(ppr, tpr) {
1052 if (tpr != pr && tpr->pr_ref > 0 &&
1053 !strcmp(tpr->pr_name + namelen, name)) {
1054 if (pr == NULL &&
1055 cuflags != JAIL_CREATE) {
1056 mtx_lock(&tpr->pr_mtx);
1057 if (tpr->pr_ref > 0) {
1058 /*
1059 * Use this jail
1060 * for updates.
1061 */
1062 if (tpr->pr_uref > 0) {
1063 pr = tpr;
1064 break;
1065 }
1066 deadpr = tpr;
1067 }
1068 mtx_unlock(&tpr->pr_mtx);
1069 } else if (tpr->pr_uref > 0) {
1070 /*
1071 * Create, or update(jid):
1072 * name must not exist in an
1073 * active sibling jail.
1074 */
1075 error = EEXIST;
1076 if (pr != NULL)
1077 mtx_unlock(&pr->pr_mtx);
1078 vfs_opterror(opts,
1079 "jail \"%s\" already exists",
1080 name);
1081 goto done_unlock_list;
1082 }
1083 }
1084 }
1085 /* If no active jail is found, use a dying one. */
1086 if (deadpr != NULL && pr == NULL) {
1087 if (flags & JAIL_DYING) {
1088 mtx_lock(&deadpr->pr_mtx);
1089 if (deadpr->pr_ref == 0) {
1090 mtx_unlock(&deadpr->pr_mtx);
1091 goto name_again;
1092 }
1093 pr = deadpr;
1094 } else if (cuflags == JAIL_UPDATE) {
1095 error = ENOENT;
1096 vfs_opterror(opts,
1097 "jail \"%s\" is dying", name);
1098 goto done_unlock_list;
1099 }
1100 }
1101 /* Update: name must exist if no jid. */
1102 else if (cuflags == JAIL_UPDATE && pr == NULL) {
1103 error = ENOENT;
1104 vfs_opterror(opts, "jail \"%s\" not found",
1105 name);
1106 goto done_unlock_list;
1107 }
1108 }
1109 }
1110 /* Update: must provide a jid or name. */
1111 else if (cuflags == JAIL_UPDATE && pr == NULL) {
1112 error = ENOENT;
1113 vfs_opterror(opts, "update specified no jail");
1114 goto done_unlock_list;
1115 }
1116
1117 /* If there's no prison to update, create a new one and link it in. */
1118 if (pr == NULL) {
1119 for (tpr = mypr; tpr != NULL; tpr = tpr->pr_parent)
1120 if (tpr->pr_childcount >= tpr->pr_childmax) {
1121 error = EPERM;
1122 vfs_opterror(opts, "prison limit exceeded");
1123 goto done_unlock_list;
1124 }
1125 created = 1;
1126 mtx_lock(&ppr->pr_mtx);
1127 if (ppr->pr_ref == 0 || (ppr->pr_flags & PR_REMOVE)) {
1128 mtx_unlock(&ppr->pr_mtx);
1129 error = ENOENT;
1130 vfs_opterror(opts, "parent jail went away!");
1131 goto done_unlock_list;
1132 }
1133 ppr->pr_ref++;
1134 ppr->pr_uref++;
1135 mtx_unlock(&ppr->pr_mtx);
1136 pr = malloc(sizeof(*pr), M_PRISON, M_WAITOK | M_ZERO);
1137 if (jid == 0) {
1138 /* Find the next free jid. */
1139 jid = lastprid + 1;
1140 findnext:
1141 if (jid == JAIL_MAX)
1142 jid = 1;
1143 TAILQ_FOREACH(tpr, &allprison, pr_list) {
1144 if (tpr->pr_id < jid)
1145 continue;
1146 if (tpr->pr_id > jid || tpr->pr_ref == 0) {
1147 TAILQ_INSERT_BEFORE(tpr, pr, pr_list);
1148 break;
1149 }
1150 if (jid == lastprid) {
1151 error = EAGAIN;
1152 vfs_opterror(opts,
1153 "no available jail IDs");
1154 free(pr, M_PRISON);
1155 prison_deref(ppr, PD_DEREF |
1156 PD_DEUREF | PD_LIST_XLOCKED);
1157 goto done_releroot;
1158 }
1159 jid++;
1160 goto findnext;
1161 }
1162 lastprid = jid;
1163 } else {
1164 /*
1165 * The jail already has a jid (that did not yet exist),
1166 * so just find where to insert it.
1167 */
1168 TAILQ_FOREACH(tpr, &allprison, pr_list)
1169 if (tpr->pr_id >= jid) {
1170 TAILQ_INSERT_BEFORE(tpr, pr, pr_list);
1171 break;
1172 }
1173 }
1174 if (tpr == NULL)
1175 TAILQ_INSERT_TAIL(&allprison, pr, pr_list);
1176 LIST_INSERT_HEAD(&ppr->pr_children, pr, pr_sibling);
1177 for (tpr = ppr; tpr != NULL; tpr = tpr->pr_parent)
1178 tpr->pr_childcount++;
1179
1180 pr->pr_parent = ppr;
1181 pr->pr_id = jid;
1182
1183 /* Set some default values, and inherit some from the parent. */
1184 if (name == NULL)
1185 name = "";
1186 if (path == NULL) {
1187 path = "/";
1188 root = mypr->pr_root;
1189 vref(root);
1190 }
1191 strlcpy(pr->pr_hostuuid, DEFAULT_HOSTUUID, HOSTUUIDLEN);
1192 pr->pr_flags |= PR_HOST;
1193 #if defined(INET) || defined(INET6)
1194 #ifdef VIMAGE
1195 if (!(pr_flags & PR_VNET))
1196 #endif
1197 {
1198 #ifdef INET
1199 if (!(ch_flags & PR_IP4_USER))
1200 pr->pr_flags |=
1201 PR_IP4 | PR_IP4_USER | PR_IP4_DISABLE;
1202 else if (!(pr_flags & PR_IP4_USER)) {
1203 pr->pr_flags |= ppr->pr_flags & PR_IP4;
1204 if (ppr->pr_ip4 != NULL) {
1205 pr->pr_ip4s = ppr->pr_ip4s;
1206 pr->pr_ip4 = malloc(pr->pr_ip4s *
1207 sizeof(struct in_addr), M_PRISON,
1208 M_WAITOK);
1209 bcopy(ppr->pr_ip4, pr->pr_ip4,
1210 pr->pr_ip4s * sizeof(*pr->pr_ip4));
1211 }
1212 }
1213 #endif
1214 #ifdef INET6
1215 if (!(ch_flags & PR_IP6_USER))
1216 pr->pr_flags |=
1217 PR_IP6 | PR_IP6_USER | PR_IP6_DISABLE;
1218 else if (!(pr_flags & PR_IP6_USER)) {
1219 pr->pr_flags |= ppr->pr_flags & PR_IP6;
1220 if (ppr->pr_ip6 != NULL) {
1221 pr->pr_ip6s = ppr->pr_ip6s;
1222 pr->pr_ip6 = malloc(pr->pr_ip6s *
1223 sizeof(struct in6_addr), M_PRISON,
1224 M_WAITOK);
1225 bcopy(ppr->pr_ip6, pr->pr_ip6,
1226 pr->pr_ip6s * sizeof(*pr->pr_ip6));
1227 }
1228 }
1229 #endif
1230 }
1231 #endif
1232 /* Source address selection is always on by default. */
1233 pr->pr_flags |= _PR_IP_SADDRSEL;
1234
1235 pr->pr_securelevel = ppr->pr_securelevel;
1236 pr->pr_allow = JAIL_DEFAULT_ALLOW & ppr->pr_allow;
1237 pr->pr_enforce_statfs = JAIL_DEFAULT_ENFORCE_STATFS;
1238
1239 LIST_INIT(&pr->pr_children);
1240 mtx_init(&pr->pr_mtx, "jail mutex", NULL, MTX_DEF | MTX_DUPOK);
1241
1242 #ifdef VIMAGE
1243 /* Allocate a new vnet if specified. */
1244 pr->pr_vnet = (pr_flags & PR_VNET)
1245 ? vnet_alloc() : ppr->pr_vnet;
1246 #endif
1247 /*
1248 * Allocate a dedicated cpuset for each jail.
1249 * Unlike other initial settings, this may return an erorr.
1250 */
1251 error = cpuset_create_root(ppr, &pr->pr_cpuset);
1252 if (error) {
1253 prison_deref(pr, PD_LIST_XLOCKED);
1254 goto done_releroot;
1255 }
1256
1257 mtx_lock(&pr->pr_mtx);
1258 /*
1259 * New prisons do not yet have a reference, because we do not
1260 * want other to see the incomplete prison once the
1261 * allprison_lock is downgraded.
1262 */
1263 } else {
1264 created = 0;
1265 /*
1266 * Grab a reference for existing prisons, to ensure they
1267 * continue to exist for the duration of the call.
1268 */
1269 pr->pr_ref++;
1270 #if defined(VIMAGE) && (defined(INET) || defined(INET6))
1271 if ((pr->pr_flags & PR_VNET) &&
1272 (ch_flags & (PR_IP4_USER | PR_IP6_USER))) {
1273 error = EINVAL;
1274 vfs_opterror(opts,
1275 "vnet jails cannot have IP address restrictions");
1276 goto done_deref_locked;
1277 }
1278 #endif
1279 #ifdef INET
1280 if (PR_IP4_USER & ch_flags & (pr_flags ^ pr->pr_flags)) {
1281 error = EINVAL;
1282 vfs_opterror(opts,
1283 "ip4 cannot be changed after creation");
1284 goto done_deref_locked;
1285 }
1286 #endif
1287 #ifdef INET6
1288 if (PR_IP6_USER & ch_flags & (pr_flags ^ pr->pr_flags)) {
1289 error = EINVAL;
1290 vfs_opterror(opts,
1291 "ip6 cannot be changed after creation");
1292 goto done_deref_locked;
1293 }
1294 #endif
1295 }
1296
1297 /* Do final error checking before setting anything. */
1298 if (gotslevel) {
1299 if (slevel < ppr->pr_securelevel) {
1300 error = EPERM;
1301 goto done_deref_locked;
1302 }
1303 }
1304 if (gotchildmax) {
1305 if (childmax >= ppr->pr_childmax) {
1306 error = EPERM;
1307 goto done_deref_locked;
1308 }
1309 }
1310 if (gotenforce) {
1311 if (enforce < ppr->pr_enforce_statfs) {
1312 error = EPERM;
1313 goto done_deref_locked;
1314 }
1315 }
1316 #ifdef INET
1317 if (ip4s > 0) {
1318 if (ppr->pr_flags & PR_IP4) {
1319 /*
1320 * Make sure the new set of IP addresses is a
1321 * subset of the parent's list. Don't worry
1322 * about the parent being unlocked, as any
1323 * setting is done with allprison_lock held.
1324 */
1325 for (ij = 0; ij < ppr->pr_ip4s; ij++)
1326 if (ip4[0].s_addr == ppr->pr_ip4[ij].s_addr)
1327 break;
1328 if (ij == ppr->pr_ip4s) {
1329 error = EPERM;
1330 goto done_deref_locked;
1331 }
1332 if (ip4s > 1) {
1333 for (ii = ij = 1; ii < ip4s; ii++) {
1334 if (ip4[ii].s_addr ==
1335 ppr->pr_ip4[0].s_addr)
1336 continue;
1337 for (; ij < ppr->pr_ip4s; ij++)
1338 if (ip4[ii].s_addr ==
1339 ppr->pr_ip4[ij].s_addr)
1340 break;
1341 if (ij == ppr->pr_ip4s)
1342 break;
1343 }
1344 if (ij == ppr->pr_ip4s) {
1345 error = EPERM;
1346 goto done_deref_locked;
1347 }
1348 }
1349 }
1350 /*
1351 * Check for conflicting IP addresses. We permit them
1352 * if there is no more than one IP on each jail. If
1353 * there is a duplicate on a jail with more than one
1354 * IP stop checking and return error.
1355 */
1356 tppr = ppr;
1357 #ifdef VIMAGE
1358 for (; tppr != &prison0; tppr = tppr->pr_parent)
1359 if (tppr->pr_flags & PR_VNET)
1360 break;
1361 #endif
1362 FOREACH_PRISON_DESCENDANT(tppr, tpr, descend) {
1363 if (tpr == pr ||
1364 #ifdef VIMAGE
1365 (tpr != tppr && (tpr->pr_flags & PR_VNET)) ||
1366 #endif
1367 tpr->pr_uref == 0) {
1368 descend = 0;
1369 continue;
1370 }
1371 if (!(tpr->pr_flags & PR_IP4_USER))
1372 continue;
1373 descend = 0;
1374 if (tpr->pr_ip4 == NULL ||
1375 (ip4s == 1 && tpr->pr_ip4s == 1))
1376 continue;
1377 for (ii = 0; ii < ip4s; ii++) {
1378 if (_prison_check_ip4(tpr, &ip4[ii]) == 0) {
1379 error = EADDRINUSE;
1380 vfs_opterror(opts,
1381 "IPv4 addresses clash");
1382 goto done_deref_locked;
1383 }
1384 }
1385 }
1386 }
1387 #endif
1388 #ifdef INET6
1389 if (ip6s > 0) {
1390 if (ppr->pr_flags & PR_IP6) {
1391 /*
1392 * Make sure the new set of IP addresses is a
1393 * subset of the parent's list.
1394 */
1395 for (ij = 0; ij < ppr->pr_ip6s; ij++)
1396 if (IN6_ARE_ADDR_EQUAL(&ip6[0],
1397 &ppr->pr_ip6[ij]))
1398 break;
1399 if (ij == ppr->pr_ip6s) {
1400 error = EPERM;
1401 goto done_deref_locked;
1402 }
1403 if (ip6s > 1) {
1404 for (ii = ij = 1; ii < ip6s; ii++) {
1405 if (IN6_ARE_ADDR_EQUAL(&ip6[ii],
1406 &ppr->pr_ip6[0]))
1407 continue;
1408 for (; ij < ppr->pr_ip6s; ij++)
1409 if (IN6_ARE_ADDR_EQUAL(
1410 &ip6[ii], &ppr->pr_ip6[ij]))
1411 break;
1412 if (ij == ppr->pr_ip6s)
1413 break;
1414 }
1415 if (ij == ppr->pr_ip6s) {
1416 error = EPERM;
1417 goto done_deref_locked;
1418 }
1419 }
1420 }
1421 /* Check for conflicting IP addresses. */
1422 tppr = ppr;
1423 #ifdef VIMAGE
1424 for (; tppr != &prison0; tppr = tppr->pr_parent)
1425 if (tppr->pr_flags & PR_VNET)
1426 break;
1427 #endif
1428 FOREACH_PRISON_DESCENDANT(tppr, tpr, descend) {
1429 if (tpr == pr ||
1430 #ifdef VIMAGE
1431 (tpr != tppr && (tpr->pr_flags & PR_VNET)) ||
1432 #endif
1433 tpr->pr_uref == 0) {
1434 descend = 0;
1435 continue;
1436 }
1437 if (!(tpr->pr_flags & PR_IP6_USER))
1438 continue;
1439 descend = 0;
1440 if (tpr->pr_ip6 == NULL ||
1441 (ip6s == 1 && tpr->pr_ip6s == 1))
1442 continue;
1443 for (ii = 0; ii < ip6s; ii++) {
1444 if (_prison_check_ip6(tpr, &ip6[ii]) == 0) {
1445 error = EADDRINUSE;
1446 vfs_opterror(opts,
1447 "IPv6 addresses clash");
1448 goto done_deref_locked;
1449 }
1450 }
1451 }
1452 }
1453 #endif
1454 onamelen = namelen = 0;
1455 if (name != NULL) {
1456 /* Give a default name of the jid. */
1457 if (name[0] == '\0')
1458 snprintf(name = numbuf, sizeof(numbuf), "%d", jid);
1459 else if (*namelc == '' || (strtoul(namelc, &p, 10) != jid &&
1460 *p == '\0')) {
1461 error = EINVAL;
1462 vfs_opterror(opts,
1463 "name cannot be numeric (unless it is the jid)");
1464 goto done_deref_locked;
1465 }
1466 /*
1467 * Make sure the name isn't too long for the prison or its
1468 * children.
1469 */
1470 onamelen = strlen(pr->pr_name);
1471 namelen = strlen(name);
1472 if (strlen(ppr->pr_name) + namelen + 2 > sizeof(pr->pr_name)) {
1473 error = ENAMETOOLONG;
1474 goto done_deref_locked;
1475 }
1476 FOREACH_PRISON_DESCENDANT(pr, tpr, descend) {
1477 if (strlen(tpr->pr_name) + (namelen - onamelen) >=
1478 sizeof(pr->pr_name)) {
1479 error = ENAMETOOLONG;
1480 goto done_deref_locked;
1481 }
1482 }
1483 }
1484 if (pr_allow & ~ppr->pr_allow) {
1485 error = EPERM;
1486 goto done_deref_locked;
1487 }
1488
1489 /* Set the parameters of the prison. */
1490 #ifdef INET
1491 redo_ip4 = 0;
1492 if (pr_flags & PR_IP4_USER) {
1493 pr->pr_flags |= PR_IP4;
1494 free(pr->pr_ip4, M_PRISON);
1495 pr->pr_ip4s = ip4s;
1496 pr->pr_ip4 = ip4;
1497 ip4 = NULL;
1498 FOREACH_PRISON_DESCENDANT_LOCKED(pr, tpr, descend) {
1499 #ifdef VIMAGE
1500 if (tpr->pr_flags & PR_VNET) {
1501 descend = 0;
1502 continue;
1503 }
1504 #endif
1505 if (prison_restrict_ip4(tpr, NULL)) {
1506 redo_ip4 = 1;
1507 descend = 0;
1508 }
1509 }
1510 }
1511 #endif
1512 #ifdef INET6
1513 redo_ip6 = 0;
1514 if (pr_flags & PR_IP6_USER) {
1515 pr->pr_flags |= PR_IP6;
1516 free(pr->pr_ip6, M_PRISON);
1517 pr->pr_ip6s = ip6s;
1518 pr->pr_ip6 = ip6;
1519 ip6 = NULL;
1520 FOREACH_PRISON_DESCENDANT_LOCKED(pr, tpr, descend) {
1521 #ifdef VIMAGE
1522 if (tpr->pr_flags & PR_VNET) {
1523 descend = 0;
1524 continue;
1525 }
1526 #endif
1527 if (prison_restrict_ip6(tpr, NULL)) {
1528 redo_ip6 = 1;
1529 descend = 0;
1530 }
1531 }
1532 }
1533 #endif
1534 if (gotslevel) {
1535 pr->pr_securelevel = slevel;
1536 /* Set all child jails to be at least this level. */
1537 FOREACH_PRISON_DESCENDANT_LOCKED(pr, tpr, descend)
1538 if (tpr->pr_securelevel < slevel)
1539 tpr->pr_securelevel = slevel;
1540 }
1541 if (gotchildmax) {
1542 pr->pr_childmax = childmax;
1543 /* Set all child jails to under this limit. */
1544 FOREACH_PRISON_DESCENDANT_LOCKED_LEVEL(pr, tpr, descend, level)
1545 if (tpr->pr_childmax > childmax - level)
1546 tpr->pr_childmax = childmax > level
1547 ? childmax - level : 0;
1548 }
1549 if (gotenforce) {
1550 pr->pr_enforce_statfs = enforce;
1551 /* Pass this restriction on to the children. */
1552 FOREACH_PRISON_DESCENDANT_LOCKED(pr, tpr, descend)
1553 if (tpr->pr_enforce_statfs < enforce)
1554 tpr->pr_enforce_statfs = enforce;
1555 }
1556 if (name != NULL) {
1557 if (ppr == &prison0)
1558 strlcpy(pr->pr_name, name, sizeof(pr->pr_name));
1559 else
1560 snprintf(pr->pr_name, sizeof(pr->pr_name), "%s.%s",
1561 ppr->pr_name, name);
1562 /* Change this component of child names. */
1563 FOREACH_PRISON_DESCENDANT_LOCKED(pr, tpr, descend) {
1564 bcopy(tpr->pr_name + onamelen, tpr->pr_name + namelen,
1565 strlen(tpr->pr_name + onamelen) + 1);
1566 bcopy(pr->pr_name, tpr->pr_name, namelen);
1567 }
1568 }
1569 if (path != NULL) {
1570 /* Try to keep a real-rooted full pathname. */
1571 if (path[0] == '/' && strcmp(mypr->pr_path, "/"))
1572 snprintf(pr->pr_path, sizeof(pr->pr_path), "%s%s",
1573 mypr->pr_path, path);
1574 else
1575 strlcpy(pr->pr_path, path, sizeof(pr->pr_path));
1576 pr->pr_root = root;
1577 }
1578 if (PR_HOST & ch_flags & ~pr_flags) {
1579 if (pr->pr_flags & PR_HOST) {
1580 /*
1581 * Copy the parent's host info. As with pr_ip4 above,
1582 * the lack of a lock on the parent is not a problem;
1583 * it is always set with allprison_lock at least
1584 * shared, and is held exclusively here.
1585 */
1586 strlcpy(pr->pr_hostname, pr->pr_parent->pr_hostname,
1587 sizeof(pr->pr_hostname));
1588 strlcpy(pr->pr_domainname, pr->pr_parent->pr_domainname,
1589 sizeof(pr->pr_domainname));
1590 strlcpy(pr->pr_hostuuid, pr->pr_parent->pr_hostuuid,
1591 sizeof(pr->pr_hostuuid));
1592 pr->pr_hostid = pr->pr_parent->pr_hostid;
1593 }
1594 } else if (host != NULL || domain != NULL || uuid != NULL || gothid) {
1595 /* Set this prison, and any descendants without PR_HOST. */
1596 if (host != NULL)
1597 strlcpy(pr->pr_hostname, host, sizeof(pr->pr_hostname));
1598 if (domain != NULL)
1599 strlcpy(pr->pr_domainname, domain,
1600 sizeof(pr->pr_domainname));
1601 if (uuid != NULL)
1602 strlcpy(pr->pr_hostuuid, uuid, sizeof(pr->pr_hostuuid));
1603 if (gothid)
1604 pr->pr_hostid = hid;
1605 FOREACH_PRISON_DESCENDANT_LOCKED(pr, tpr, descend) {
1606 if (tpr->pr_flags & PR_HOST)
1607 descend = 0;
1608 else {
1609 if (host != NULL)
1610 strlcpy(tpr->pr_hostname,
1611 pr->pr_hostname,
1612 sizeof(tpr->pr_hostname));
1613 if (domain != NULL)
1614 strlcpy(tpr->pr_domainname,
1615 pr->pr_domainname,
1616 sizeof(tpr->pr_domainname));
1617 if (uuid != NULL)
1618 strlcpy(tpr->pr_hostuuid,
1619 pr->pr_hostuuid,
1620 sizeof(tpr->pr_hostuuid));
1621 if (gothid)
1622 tpr->pr_hostid = hid;
1623 }
1624 }
1625 }
1626 if ((tallow = ch_allow & ~pr_allow)) {
1627 /* Clear allow bits in all children. */
1628 FOREACH_PRISON_DESCENDANT_LOCKED(pr, tpr, descend)
1629 tpr->pr_allow &= ~tallow;
1630 }
1631 pr->pr_allow = (pr->pr_allow & ~ch_allow) | pr_allow;
1632 /*
1633 * Persistent prisons get an extra reference, and prisons losing their
1634 * persist flag lose that reference. Only do this for existing prisons
1635 * for now, so new ones will remain unseen until after the module
1636 * handlers have completed.
1637 */
1638 if (!created && (ch_flags & PR_PERSIST & (pr_flags ^ pr->pr_flags))) {
1639 if (pr_flags & PR_PERSIST) {
1640 pr->pr_ref++;
1641 pr->pr_uref++;
1642 } else {
1643 pr->pr_ref--;
1644 pr->pr_uref--;
1645 }
1646 }
1647 pr->pr_flags = (pr->pr_flags & ~ch_flags) | pr_flags;
1648 mtx_unlock(&pr->pr_mtx);
1649
1650 /* Locks may have prevented a complete restriction of child IP
1651 * addresses. If so, allocate some more memory and try again.
1652 */
1653 #ifdef INET
1654 while (redo_ip4) {
1655 ip4s = pr->pr_ip4s;
1656 ip4 = malloc(ip4s * sizeof(*ip4), M_PRISON, M_WAITOK);
1657 mtx_lock(&pr->pr_mtx);
1658 redo_ip4 = 0;
1659 FOREACH_PRISON_DESCENDANT_LOCKED(pr, tpr, descend) {
1660 #ifdef VIMAGE
1661 if (tpr->pr_flags & PR_VNET) {
1662 descend = 0;
1663 continue;
1664 }
1665 #endif
1666 if (prison_restrict_ip4(tpr, ip4)) {
1667 if (ip4 != NULL)
1668 ip4 = NULL;
1669 else
1670 redo_ip4 = 1;
1671 }
1672 }
1673 mtx_unlock(&pr->pr_mtx);
1674 }
1675 #endif
1676 #ifdef INET6
1677 while (redo_ip6) {
1678 ip6s = pr->pr_ip6s;
1679 ip6 = malloc(ip6s * sizeof(*ip6), M_PRISON, M_WAITOK);
1680 mtx_lock(&pr->pr_mtx);
1681 redo_ip6 = 0;
1682 FOREACH_PRISON_DESCENDANT_LOCKED(pr, tpr, descend) {
1683 #ifdef VIMAGE
1684 if (tpr->pr_flags & PR_VNET) {
1685 descend = 0;
1686 continue;
1687 }
1688 #endif
1689 if (prison_restrict_ip6(tpr, ip6)) {
1690 if (ip6 != NULL)
1691 ip6 = NULL;
1692 else
1693 redo_ip6 = 1;
1694 }
1695 }
1696 mtx_unlock(&pr->pr_mtx);
1697 }
1698 #endif
1699
1700 /* Let the modules do their work. */
1701 sx_downgrade(&allprison_lock);
1702 if (created) {
1703 error = osd_jail_call(pr, PR_METHOD_CREATE, opts);
1704 if (error) {
1705 prison_deref(pr, PD_LIST_SLOCKED);
1706 goto done_errmsg;
1707 }
1708 }
1709 error = osd_jail_call(pr, PR_METHOD_SET, opts);
1710 if (error) {
1711 prison_deref(pr, created
1712 ? PD_LIST_SLOCKED
1713 : PD_DEREF | PD_LIST_SLOCKED);
1714 goto done_errmsg;
1715 }
1716
1717 /* Attach this process to the prison if requested. */
1718 if (flags & JAIL_ATTACH) {
1719 mtx_lock(&pr->pr_mtx);
1720 error = do_jail_attach(td, pr);
1721 if (error) {
1722 vfs_opterror(opts, "attach failed");
1723 if (!created)
1724 prison_deref(pr, PD_DEREF);
1725 goto done_errmsg;
1726 }
1727 }
1728
1729 /*
1730 * Now that it is all there, drop the temporary reference from existing
1731 * prisons. Or add a reference to newly created persistent prisons
1732 * (which was not done earlier so that the prison would not be publicly
1733 * visible).
1734 */
1735 if (!created) {
1736 prison_deref(pr, (flags & JAIL_ATTACH)
1737 ? PD_DEREF
1738 : PD_DEREF | PD_LIST_SLOCKED);
1739 } else {
1740 if (pr_flags & PR_PERSIST) {
1741 mtx_lock(&pr->pr_mtx);
1742 pr->pr_ref++;
1743 pr->pr_uref++;
1744 mtx_unlock(&pr->pr_mtx);
1745 }
1746 if (!(flags & JAIL_ATTACH))
1747 sx_sunlock(&allprison_lock);
1748 }
1749 td->td_retval[0] = pr->pr_id;
1750 goto done_errmsg;
1751
1752 done_deref_locked:
1753 prison_deref(pr, created
1754 ? PD_LOCKED | PD_LIST_XLOCKED
1755 : PD_DEREF | PD_LOCKED | PD_LIST_XLOCKED);
1756 goto done_releroot;
1757 done_unlock_list:
1758 sx_xunlock(&allprison_lock);
1759 done_releroot:
1760 if (root != NULL) {
1761 vfslocked = VFS_LOCK_GIANT(root->v_mount);
1762 vrele(root);
1763 VFS_UNLOCK_GIANT(vfslocked);
1764 }
1765 done_errmsg:
1766 if (error) {
1767 vfs_getopt(opts, "errmsg", (void **)&errmsg, &errmsg_len);
1768 if (errmsg_len > 0) {
1769 errmsg_pos = 2 * vfs_getopt_pos(opts, "errmsg") + 1;
1770 if (errmsg_pos > 0) {
1771 if (optuio->uio_segflg == UIO_SYSSPACE)
1772 bcopy(errmsg,
1773 optuio->uio_iov[errmsg_pos].iov_base,
1774 errmsg_len);
1775 else
1776 copyout(errmsg,
1777 optuio->uio_iov[errmsg_pos].iov_base,
1778 errmsg_len);
1779 }
1780 }
1781 }
1782 done_free:
1783 #ifdef INET
1784 free(ip4, M_PRISON);
1785 #endif
1786 #ifdef INET6
1787 free(ip6, M_PRISON);
1788 #endif
1789 vfs_freeopts(opts);
1790 return (error);
1791 }
1792
1793
1794 /*
1795 * struct jail_get_args {
1796 * struct iovec *iovp;
1797 * unsigned int iovcnt;
1798 * int flags;
1799 * };
1800 */
1801 int
1802 jail_get(struct thread *td, struct jail_get_args *uap)
1803 {
1804 struct uio *auio;
1805 int error;
1806
1807 /* Check that we have an even number of iovecs. */
1808 if (uap->iovcnt & 1)
1809 return (EINVAL);
1810
1811 error = copyinuio(uap->iovp, uap->iovcnt, &auio);
1812 if (error)
1813 return (error);
1814 error = kern_jail_get(td, auio, uap->flags);
1815 if (error == 0)
1816 error = copyout(auio->uio_iov, uap->iovp,
1817 uap->iovcnt * sizeof (struct iovec));
1818 free(auio, M_IOV);
1819 return (error);
1820 }
1821
1822 int
1823 kern_jail_get(struct thread *td, struct uio *optuio, int flags)
1824 {
1825 struct prison *pr, *mypr;
1826 struct vfsopt *opt;
1827 struct vfsoptlist *opts;
1828 char *errmsg, *name;
1829 int error, errmsg_len, errmsg_pos, fi, i, jid, len, locked, pos;
1830
1831 if (flags & ~JAIL_GET_MASK)
1832 return (EINVAL);
1833
1834 /* Get the parameter list. */
1835 error = vfs_buildopts(optuio, &opts);
1836 if (error)
1837 return (error);
1838 errmsg_pos = vfs_getopt_pos(opts, "errmsg");
1839 mypr = td->td_ucred->cr_prison;
1840
1841 /*
1842 * Find the prison specified by one of: lastjid, jid, name.
1843 */
1844 sx_slock(&allprison_lock);
1845 error = vfs_copyopt(opts, "lastjid", &jid, sizeof(jid));
1846 if (error == 0) {
1847 TAILQ_FOREACH(pr, &allprison, pr_list) {
1848 if (pr->pr_id > jid && prison_ischild(mypr, pr)) {
1849 mtx_lock(&pr->pr_mtx);
1850 if (pr->pr_ref > 0 &&
1851 (pr->pr_uref > 0 || (flags & JAIL_DYING)))
1852 break;
1853 mtx_unlock(&pr->pr_mtx);
1854 }
1855 }
1856 if (pr != NULL)
1857 goto found_prison;
1858 error = ENOENT;
1859 vfs_opterror(opts, "no jail after %d", jid);
1860 goto done_unlock_list;
1861 } else if (error != ENOENT)
1862 goto done_unlock_list;
1863
1864 error = vfs_copyopt(opts, "jid", &jid, sizeof(jid));
1865 if (error == 0) {
1866 if (jid != 0) {
1867 pr = prison_find_child(mypr, jid);
1868 if (pr != NULL) {
1869 if (pr->pr_uref == 0 && !(flags & JAIL_DYING)) {
1870 mtx_unlock(&pr->pr_mtx);
1871 error = ENOENT;
1872 vfs_opterror(opts, "jail %d is dying",
1873 jid);
1874 goto done_unlock_list;
1875 }
1876 goto found_prison;
1877 }
1878 error = ENOENT;
1879 vfs_opterror(opts, "jail %d not found", jid);
1880 goto done_unlock_list;
1881 }
1882 } else if (error != ENOENT)
1883 goto done_unlock_list;
1884
1885 error = vfs_getopt(opts, "name", (void **)&name, &len);
1886 if (error == 0) {
1887 if (len == 0 || name[len - 1] != '\0') {
1888 error = EINVAL;
1889 goto done_unlock_list;
1890 }
1891 pr = prison_find_name(mypr, name);
1892 if (pr != NULL) {
1893 if (pr->pr_uref == 0 && !(flags & JAIL_DYING)) {
1894 mtx_unlock(&pr->pr_mtx);
1895 error = ENOENT;
1896 vfs_opterror(opts, "jail \"%s\" is dying",
1897 name);
1898 goto done_unlock_list;
1899 }
1900 goto found_prison;
1901 }
1902 error = ENOENT;
1903 vfs_opterror(opts, "jail \"%s\" not found", name);
1904 goto done_unlock_list;
1905 } else if (error != ENOENT)
1906 goto done_unlock_list;
1907
1908 vfs_opterror(opts, "no jail specified");
1909 error = ENOENT;
1910 goto done_unlock_list;
1911
1912 found_prison:
1913 /* Get the parameters of the prison. */
1914 pr->pr_ref++;
1915 locked = PD_LOCKED;
1916 td->td_retval[0] = pr->pr_id;
1917 error = vfs_setopt(opts, "jid", &pr->pr_id, sizeof(pr->pr_id));
1918 if (error != 0 && error != ENOENT)
1919 goto done_deref;
1920 i = (pr->pr_parent == mypr) ? 0 : pr->pr_parent->pr_id;
1921 error = vfs_setopt(opts, "parent", &i, sizeof(i));
1922 if (error != 0 && error != ENOENT)
1923 goto done_deref;
1924 error = vfs_setopts(opts, "name", prison_name(mypr, pr));
1925 if (error != 0 && error != ENOENT)
1926 goto done_deref;
1927 error = vfs_setopt(opts, "cpuset.id", &pr->pr_cpuset->cs_id,
1928 sizeof(pr->pr_cpuset->cs_id));
1929 if (error != 0 && error != ENOENT)
1930 goto done_deref;
1931 error = vfs_setopts(opts, "path", prison_path(mypr, pr));
1932 if (error != 0 && error != ENOENT)
1933 goto done_deref;
1934 #ifdef INET
1935 error = vfs_setopt_part(opts, "ip4.addr", pr->pr_ip4,
1936 pr->pr_ip4s * sizeof(*pr->pr_ip4));
1937 if (error != 0 && error != ENOENT)
1938 goto done_deref;
1939 #endif
1940 #ifdef INET6
1941 error = vfs_setopt_part(opts, "ip6.addr", pr->pr_ip6,
1942 pr->pr_ip6s * sizeof(*pr->pr_ip6));
1943 if (error != 0 && error != ENOENT)
1944 goto done_deref;
1945 #endif
1946 error = vfs_setopt(opts, "securelevel", &pr->pr_securelevel,
1947 sizeof(pr->pr_securelevel));
1948 if (error != 0 && error != ENOENT)
1949 goto done_deref;
1950 error = vfs_setopt(opts, "children.cur", &pr->pr_childcount,
1951 sizeof(pr->pr_childcount));
1952 if (error != 0 && error != ENOENT)
1953 goto done_deref;
1954 error = vfs_setopt(opts, "children.max", &pr->pr_childmax,
1955 sizeof(pr->pr_childmax));
1956 if (error != 0 && error != ENOENT)
1957 goto done_deref;
1958 error = vfs_setopts(opts, "host.hostname", pr->pr_hostname);
1959 if (error != 0 && error != ENOENT)
1960 goto done_deref;
1961 error = vfs_setopts(opts, "host.domainname", pr->pr_domainname);
1962 if (error != 0 && error != ENOENT)
1963 goto done_deref;
1964 error = vfs_setopts(opts, "host.hostuuid", pr->pr_hostuuid);
1965 if (error != 0 && error != ENOENT)
1966 goto done_deref;
1967 #ifdef COMPAT_FREEBSD32
1968 if (td->td_proc->p_sysent->sv_flags & SV_ILP32) {
1969 uint32_t hid32 = pr->pr_hostid;
1970
1971 error = vfs_setopt(opts, "host.hostid", &hid32, sizeof(hid32));
1972 } else
1973 #endif
1974 error = vfs_setopt(opts, "host.hostid", &pr->pr_hostid,
1975 sizeof(pr->pr_hostid));
1976 if (error != 0 && error != ENOENT)
1977 goto done_deref;
1978 error = vfs_setopt(opts, "enforce_statfs", &pr->pr_enforce_statfs,
1979 sizeof(pr->pr_enforce_statfs));
1980 if (error != 0 && error != ENOENT)
1981 goto done_deref;
1982 for (fi = 0; fi < sizeof(pr_flag_names) / sizeof(pr_flag_names[0]);
1983 fi++) {
1984 if (pr_flag_names[fi] == NULL)
1985 continue;
1986 i = (pr->pr_flags & (1 << fi)) ? 1 : 0;
1987 error = vfs_setopt(opts, pr_flag_names[fi], &i, sizeof(i));
1988 if (error != 0 && error != ENOENT)
1989 goto done_deref;
1990 i = !i;
1991 error = vfs_setopt(opts, pr_flag_nonames[fi], &i, sizeof(i));
1992 if (error != 0 && error != ENOENT)
1993 goto done_deref;
1994 }
1995 for (fi = 0; fi < sizeof(pr_flag_jailsys) / sizeof(pr_flag_jailsys[0]);
1996 fi++) {
1997 i = pr->pr_flags &
1998 (pr_flag_jailsys[fi].disable | pr_flag_jailsys[fi].new);
1999 i = pr_flag_jailsys[fi].disable &&
2000 (i == pr_flag_jailsys[fi].disable) ? JAIL_SYS_DISABLE
2001 : (i == pr_flag_jailsys[fi].new) ? JAIL_SYS_NEW
2002 : JAIL_SYS_INHERIT;
2003 error =
2004 vfs_setopt(opts, pr_flag_jailsys[fi].name, &i, sizeof(i));
2005 if (error != 0 && error != ENOENT)
2006 goto done_deref;
2007 }
2008 for (fi = 0; fi < sizeof(pr_allow_names) / sizeof(pr_allow_names[0]);
2009 fi++) {
2010 if (pr_allow_names[fi] == NULL)
2011 continue;
2012 i = (pr->pr_allow & (1 << fi)) ? 1 : 0;
2013 error = vfs_setopt(opts, pr_allow_names[fi], &i, sizeof(i));
2014 if (error != 0 && error != ENOENT)
2015 goto done_deref;
2016 i = !i;
2017 error = vfs_setopt(opts, pr_allow_nonames[fi], &i, sizeof(i));
2018 if (error != 0 && error != ENOENT)
2019 goto done_deref;
2020 }
2021 i = (pr->pr_uref == 0);
2022 error = vfs_setopt(opts, "dying", &i, sizeof(i));
2023 if (error != 0 && error != ENOENT)
2024 goto done_deref;
2025 i = !i;
2026 error = vfs_setopt(opts, "nodying", &i, sizeof(i));
2027 if (error != 0 && error != ENOENT)
2028 goto done_deref;
2029
2030 /* Get the module parameters. */
2031 mtx_unlock(&pr->pr_mtx);
2032 locked = 0;
2033 error = osd_jail_call(pr, PR_METHOD_GET, opts);
2034 if (error)
2035 goto done_deref;
2036 prison_deref(pr, PD_DEREF | PD_LIST_SLOCKED);
2037
2038 /* By now, all parameters should have been noted. */
2039 TAILQ_FOREACH(opt, opts, link) {
2040 if (!opt->seen && strcmp(opt->name, "errmsg")) {
2041 error = EINVAL;
2042 vfs_opterror(opts, "unknown parameter: %s", opt->name);
2043 goto done_errmsg;
2044 }
2045 }
2046
2047 /* Write the fetched parameters back to userspace. */
2048 error = 0;
2049 TAILQ_FOREACH(opt, opts, link) {
2050 if (opt->pos >= 0 && opt->pos != errmsg_pos) {
2051 pos = 2 * opt->pos + 1;
2052 optuio->uio_iov[pos].iov_len = opt->len;
2053 if (opt->value != NULL) {
2054 if (optuio->uio_segflg == UIO_SYSSPACE) {
2055 bcopy(opt->value,
2056 optuio->uio_iov[pos].iov_base,
2057 opt->len);
2058 } else {
2059 error = copyout(opt->value,
2060 optuio->uio_iov[pos].iov_base,
2061 opt->len);
2062 if (error)
2063 break;
2064 }
2065 }
2066 }
2067 }
2068 goto done_errmsg;
2069
2070 done_deref:
2071 prison_deref(pr, locked | PD_DEREF | PD_LIST_SLOCKED);
2072 goto done_errmsg;
2073
2074 done_unlock_list:
2075 sx_sunlock(&allprison_lock);
2076 done_errmsg:
2077 if (error && errmsg_pos >= 0) {
2078 vfs_getopt(opts, "errmsg", (void **)&errmsg, &errmsg_len);
2079 errmsg_pos = 2 * errmsg_pos + 1;
2080 if (errmsg_len > 0) {
2081 if (optuio->uio_segflg == UIO_SYSSPACE)
2082 bcopy(errmsg,
2083 optuio->uio_iov[errmsg_pos].iov_base,
2084 errmsg_len);
2085 else
2086 copyout(errmsg,
2087 optuio->uio_iov[errmsg_pos].iov_base,
2088 errmsg_len);
2089 }
2090 }
2091 vfs_freeopts(opts);
2092 return (error);
2093 }
2094
2095
2096 /*
2097 * struct jail_remove_args {
2098 * int jid;
2099 * };
2100 */
2101 int
2102 jail_remove(struct thread *td, struct jail_remove_args *uap)
2103 {
2104 struct prison *pr, *cpr, *lpr, *tpr;
2105 int descend, error;
2106
2107 error = priv_check(td, PRIV_JAIL_REMOVE);
2108 if (error)
2109 return (error);
2110
2111 sx_xlock(&allprison_lock);
2112 pr = prison_find_child(td->td_ucred->cr_prison, uap->jid);
2113 if (pr == NULL) {
2114 sx_xunlock(&allprison_lock);
2115 return (EINVAL);
2116 }
2117
2118 /* Remove all descendants of this prison, then remove this prison. */
2119 pr->pr_ref++;
2120 pr->pr_flags |= PR_REMOVE;
2121 if (!LIST_EMPTY(&pr->pr_children)) {
2122 mtx_unlock(&pr->pr_mtx);
2123 lpr = NULL;
2124 FOREACH_PRISON_DESCENDANT(pr, cpr, descend) {
2125 mtx_lock(&cpr->pr_mtx);
2126 if (cpr->pr_ref > 0) {
2127 tpr = cpr;
2128 cpr->pr_ref++;
2129 cpr->pr_flags |= PR_REMOVE;
2130 } else {
2131 /* Already removed - do not do it again. */
2132 tpr = NULL;
2133 }
2134 mtx_unlock(&cpr->pr_mtx);
2135 if (lpr != NULL) {
2136 mtx_lock(&lpr->pr_mtx);
2137 prison_remove_one(lpr);
2138 sx_xlock(&allprison_lock);
2139 }
2140 lpr = tpr;
2141 }
2142 if (lpr != NULL) {
2143 mtx_lock(&lpr->pr_mtx);
2144 prison_remove_one(lpr);
2145 sx_xlock(&allprison_lock);
2146 }
2147 mtx_lock(&pr->pr_mtx);
2148 }
2149 prison_remove_one(pr);
2150 return (0);
2151 }
2152
2153 static void
2154 prison_remove_one(struct prison *pr)
2155 {
2156 struct proc *p;
2157 int deuref;
2158
2159 /* If the prison was persistent, it is not anymore. */
2160 deuref = 0;
2161 if (pr->pr_flags & PR_PERSIST) {
2162 pr->pr_ref--;
2163 deuref = PD_DEUREF;
2164 pr->pr_flags &= ~PR_PERSIST;
2165 }
2166
2167 /*
2168 * jail_remove added a reference. If that's the only one, remove
2169 * the prison now.
2170 */
2171 KASSERT(pr->pr_ref > 0,
2172 ("prison_remove_one removing a dead prison (jid=%d)", pr->pr_id));
2173 if (pr->pr_ref == 1) {
2174 prison_deref(pr,
2175 deuref | PD_DEREF | PD_LOCKED | PD_LIST_XLOCKED);
2176 return;
2177 }
2178
2179 mtx_unlock(&pr->pr_mtx);
2180 sx_xunlock(&allprison_lock);
2181 /*
2182 * Kill all processes unfortunate enough to be attached to this prison.
2183 */
2184 sx_slock(&allproc_lock);
2185 LIST_FOREACH(p, &allproc, p_list) {
2186 PROC_LOCK(p);
2187 if (p->p_state != PRS_NEW && p->p_ucred &&
2188 p->p_ucred->cr_prison == pr)
2189 psignal(p, SIGKILL);
2190 PROC_UNLOCK(p);
2191 }
2192 sx_sunlock(&allproc_lock);
2193 /* Remove the temporary reference added by jail_remove. */
2194 prison_deref(pr, deuref | PD_DEREF);
2195 }
2196
2197
2198 /*
2199 * struct jail_attach_args {
2200 * int jid;
2201 * };
2202 */
2203 int
2204 jail_attach(struct thread *td, struct jail_attach_args *uap)
2205 {
2206 struct prison *pr;
2207 int error;
2208
2209 error = priv_check(td, PRIV_JAIL_ATTACH);
2210 if (error)
2211 return (error);
2212
2213 sx_slock(&allprison_lock);
2214 pr = prison_find_child(td->td_ucred->cr_prison, uap->jid);
2215 if (pr == NULL) {
2216 sx_sunlock(&allprison_lock);
2217 return (EINVAL);
2218 }
2219
2220 /*
2221 * Do not allow a process to attach to a prison that is not
2222 * considered to be "alive".
2223 */
2224 if (pr->pr_uref == 0) {
2225 mtx_unlock(&pr->pr_mtx);
2226 sx_sunlock(&allprison_lock);
2227 return (EINVAL);
2228 }
2229
2230 return (do_jail_attach(td, pr));
2231 }
2232
2233 static int
2234 do_jail_attach(struct thread *td, struct prison *pr)
2235 {
2236 struct prison *ppr;
2237 struct proc *p;
2238 struct ucred *newcred, *oldcred;
2239 int vfslocked, error;
2240
2241 /*
2242 * XXX: Note that there is a slight race here if two threads
2243 * in the same privileged process attempt to attach to two
2244 * different jails at the same time. It is important for
2245 * user processes not to do this, or they might end up with
2246 * a process root from one prison, but attached to the jail
2247 * of another.
2248 */
2249 pr->pr_ref++;
2250 pr->pr_uref++;
2251 mtx_unlock(&pr->pr_mtx);
2252
2253 /* Let modules do whatever they need to prepare for attaching. */
2254 error = osd_jail_call(pr, PR_METHOD_ATTACH, td);
2255 if (error) {
2256 prison_deref(pr, PD_DEREF | PD_DEUREF | PD_LIST_SLOCKED);
2257 return (error);
2258 }
2259 sx_sunlock(&allprison_lock);
2260
2261 /*
2262 * Reparent the newly attached process to this jail.
2263 */
2264 ppr = td->td_ucred->cr_prison;
2265 p = td->td_proc;
2266 error = cpuset_setproc_update_set(p, pr->pr_cpuset);
2267 if (error)
2268 goto e_revert_osd;
2269
2270 vfslocked = VFS_LOCK_GIANT(pr->pr_root->v_mount);
2271 vn_lock(pr->pr_root, LK_EXCLUSIVE | LK_RETRY);
2272 if ((error = change_dir(pr->pr_root, td)) != 0)
2273 goto e_unlock;
2274 #ifdef MAC
2275 if ((error = mac_vnode_check_chroot(td->td_ucred, pr->pr_root)))
2276 goto e_unlock;
2277 #endif
2278 VOP_UNLOCK(pr->pr_root, 0);
2279 if ((error = change_root(pr->pr_root, td)))
2280 goto e_unlock_giant;
2281 VFS_UNLOCK_GIANT(vfslocked);
2282
2283 newcred = crget();
2284 PROC_LOCK(p);
2285 oldcred = p->p_ucred;
2286 setsugid(p);
2287 crcopy(newcred, oldcred);
2288 newcred->cr_prison = pr;
2289 p->p_ucred = newcred;
2290 PROC_UNLOCK(p);
2291 crfree(oldcred);
2292 prison_deref(ppr, PD_DEREF | PD_DEUREF);
2293 return (0);
2294 e_unlock:
2295 VOP_UNLOCK(pr->pr_root, 0);
2296 e_unlock_giant:
2297 VFS_UNLOCK_GIANT(vfslocked);
2298 e_revert_osd:
2299 /* Tell modules this thread is still in its old jail after all. */
2300 (void)osd_jail_call(ppr, PR_METHOD_ATTACH, td);
2301 prison_deref(pr, PD_DEREF | PD_DEUREF);
2302 return (error);
2303 }
2304
2305
2306 /*
2307 * Returns a locked prison instance, or NULL on failure.
2308 */
2309 struct prison *
2310 prison_find(int prid)
2311 {
2312 struct prison *pr;
2313
2314 sx_assert(&allprison_lock, SX_LOCKED);
2315 TAILQ_FOREACH(pr, &allprison, pr_list) {
2316 if (pr->pr_id == prid) {
2317 mtx_lock(&pr->pr_mtx);
2318 if (pr->pr_ref > 0)
2319 return (pr);
2320 mtx_unlock(&pr->pr_mtx);
2321 }
2322 }
2323 return (NULL);
2324 }
2325
2326 /*
2327 * Find a prison that is a descendant of mypr. Returns a locked prison or NULL.
2328 */
2329 struct prison *
2330 prison_find_child(struct prison *mypr, int prid)
2331 {
2332 struct prison *pr;
2333 int descend;
2334
2335 sx_assert(&allprison_lock, SX_LOCKED);
2336 FOREACH_PRISON_DESCENDANT(mypr, pr, descend) {
2337 if (pr->pr_id == prid) {
2338 mtx_lock(&pr->pr_mtx);
2339 if (pr->pr_ref > 0)
2340 return (pr);
2341 mtx_unlock(&pr->pr_mtx);
2342 }
2343 }
2344 return (NULL);
2345 }
2346
2347 /*
2348 * Look for the name relative to mypr. Returns a locked prison or NULL.
2349 */
2350 struct prison *
2351 prison_find_name(struct prison *mypr, const char *name)
2352 {
2353 struct prison *pr, *deadpr;
2354 size_t mylen;
2355 int descend;
2356
2357 sx_assert(&allprison_lock, SX_LOCKED);
2358 mylen = (mypr == &prison0) ? 0 : strlen(mypr->pr_name) + 1;
2359 again:
2360 deadpr = NULL;
2361 FOREACH_PRISON_DESCENDANT(mypr, pr, descend) {
2362 if (!strcmp(pr->pr_name + mylen, name)) {
2363 mtx_lock(&pr->pr_mtx);
2364 if (pr->pr_ref > 0) {
2365 if (pr->pr_uref > 0)
2366 return (pr);
2367 deadpr = pr;
2368 }
2369 mtx_unlock(&pr->pr_mtx);
2370 }
2371 }
2372 /* There was no valid prison - perhaps there was a dying one. */
2373 if (deadpr != NULL) {
2374 mtx_lock(&deadpr->pr_mtx);
2375 if (deadpr->pr_ref == 0) {
2376 mtx_unlock(&deadpr->pr_mtx);
2377 goto again;
2378 }
2379 }
2380 return (deadpr);
2381 }
2382
2383 /*
2384 * See if a prison has the specific flag set.
2385 */
2386 int
2387 prison_flag(struct ucred *cred, unsigned flag)
2388 {
2389
2390 /* This is an atomic read, so no locking is necessary. */
2391 return (cred->cr_prison->pr_flags & flag);
2392 }
2393
2394 int
2395 prison_allow(struct ucred *cred, unsigned flag)
2396 {
2397
2398 /* This is an atomic read, so no locking is necessary. */
2399 return (cred->cr_prison->pr_allow & flag);
2400 }
2401
2402 /*
2403 * Remove a prison reference. If that was the last reference, remove the
2404 * prison itself - but not in this context in case there are locks held.
2405 */
2406 void
2407 prison_free_locked(struct prison *pr)
2408 {
2409
2410 mtx_assert(&pr->pr_mtx, MA_OWNED);
2411 pr->pr_ref--;
2412 if (pr->pr_ref == 0) {
2413 mtx_unlock(&pr->pr_mtx);
2414 TASK_INIT(&pr->pr_task, 0, prison_complete, pr);
2415 taskqueue_enqueue(taskqueue_thread, &pr->pr_task);
2416 return;
2417 }
2418 mtx_unlock(&pr->pr_mtx);
2419 }
2420
2421 void
2422 prison_free(struct prison *pr)
2423 {
2424
2425 mtx_lock(&pr->pr_mtx);
2426 prison_free_locked(pr);
2427 }
2428
2429 static void
2430 prison_complete(void *context, int pending)
2431 {
2432
2433 prison_deref((struct prison *)context, 0);
2434 }
2435
2436 /*
2437 * Remove a prison reference (usually). This internal version assumes no
2438 * mutexes are held, except perhaps the prison itself. If there are no more
2439 * references, release and delist the prison. On completion, the prison lock
2440 * and the allprison lock are both unlocked.
2441 */
2442 static void
2443 prison_deref(struct prison *pr, int flags)
2444 {
2445 struct prison *ppr, *tpr;
2446 int vfslocked;
2447
2448 if (!(flags & PD_LOCKED))
2449 mtx_lock(&pr->pr_mtx);
2450 /* Decrement the user references in a separate loop. */
2451 if (flags & PD_DEUREF) {
2452 for (tpr = pr;; tpr = tpr->pr_parent) {
2453 if (tpr != pr)
2454 mtx_lock(&tpr->pr_mtx);
2455 if (--tpr->pr_uref > 0)
2456 break;
2457 KASSERT(tpr != &prison0, ("prison0 pr_uref=0"));
2458 mtx_unlock(&tpr->pr_mtx);
2459 }
2460 /* Done if there were only user references to remove. */
2461 if (!(flags & PD_DEREF)) {
2462 mtx_unlock(&tpr->pr_mtx);
2463 if (flags & PD_LIST_SLOCKED)
2464 sx_sunlock(&allprison_lock);
2465 else if (flags & PD_LIST_XLOCKED)
2466 sx_xunlock(&allprison_lock);
2467 return;
2468 }
2469 if (tpr != pr) {
2470 mtx_unlock(&tpr->pr_mtx);
2471 mtx_lock(&pr->pr_mtx);
2472 }
2473 }
2474
2475 for (;;) {
2476 if (flags & PD_DEREF)
2477 pr->pr_ref--;
2478 /* If the prison still has references, nothing else to do. */
2479 if (pr->pr_ref > 0) {
2480 mtx_unlock(&pr->pr_mtx);
2481 if (flags & PD_LIST_SLOCKED)
2482 sx_sunlock(&allprison_lock);
2483 else if (flags & PD_LIST_XLOCKED)
2484 sx_xunlock(&allprison_lock);
2485 return;
2486 }
2487
2488 mtx_unlock(&pr->pr_mtx);
2489 if (flags & PD_LIST_SLOCKED) {
2490 if (!sx_try_upgrade(&allprison_lock)) {
2491 sx_sunlock(&allprison_lock);
2492 sx_xlock(&allprison_lock);
2493 }
2494 } else if (!(flags & PD_LIST_XLOCKED))
2495 sx_xlock(&allprison_lock);
2496
2497 TAILQ_REMOVE(&allprison, pr, pr_list);
2498 LIST_REMOVE(pr, pr_sibling);
2499 ppr = pr->pr_parent;
2500 for (tpr = ppr; tpr != NULL; tpr = tpr->pr_parent)
2501 tpr->pr_childcount--;
2502 sx_xunlock(&allprison_lock);
2503
2504 #ifdef VIMAGE
2505 if (pr->pr_vnet != ppr->pr_vnet)
2506 vnet_destroy(pr->pr_vnet);
2507 #endif
2508 if (pr->pr_root != NULL) {
2509 vfslocked = VFS_LOCK_GIANT(pr->pr_root->v_mount);
2510 vrele(pr->pr_root);
2511 VFS_UNLOCK_GIANT(vfslocked);
2512 }
2513 mtx_destroy(&pr->pr_mtx);
2514 #ifdef INET
2515 free(pr->pr_ip4, M_PRISON);
2516 #endif
2517 #ifdef INET6
2518 free(pr->pr_ip6, M_PRISON);
2519 #endif
2520 if (pr->pr_cpuset != NULL)
2521 cpuset_rel(pr->pr_cpuset);
2522 osd_jail_exit(pr);
2523 free(pr, M_PRISON);
2524
2525 /* Removing a prison frees a reference on its parent. */
2526 pr = ppr;
2527 mtx_lock(&pr->pr_mtx);
2528 flags = PD_DEREF;
2529 }
2530 }
2531
2532 void
2533 prison_hold_locked(struct prison *pr)
2534 {
2535
2536 mtx_assert(&pr->pr_mtx, MA_OWNED);
2537 KASSERT(pr->pr_ref > 0,
2538 ("Trying to hold dead prison (jid=%d).", pr->pr_id));
2539 pr->pr_ref++;
2540 }
2541
2542 void
2543 prison_hold(struct prison *pr)
2544 {
2545
2546 mtx_lock(&pr->pr_mtx);
2547 prison_hold_locked(pr);
2548 mtx_unlock(&pr->pr_mtx);
2549 }
2550
2551 void
2552 prison_proc_hold(struct prison *pr)
2553 {
2554
2555 mtx_lock(&pr->pr_mtx);
2556 KASSERT(pr->pr_uref > 0,
2557 ("Cannot add a process to a non-alive prison (jid=%d)", pr->pr_id));
2558 pr->pr_uref++;
2559 mtx_unlock(&pr->pr_mtx);
2560 }
2561
2562 void
2563 prison_proc_free(struct prison *pr)
2564 {
2565
2566 mtx_lock(&pr->pr_mtx);
2567 KASSERT(pr->pr_uref > 0,
2568 ("Trying to kill a process in a dead prison (jid=%d)", pr->pr_id));
2569 prison_deref(pr, PD_DEUREF | PD_LOCKED);
2570 }
2571
2572
2573 #ifdef INET
2574 /*
2575 * Restrict a prison's IP address list with its parent's, possibly replacing
2576 * it. Return true if the replacement buffer was used (or would have been).
2577 */
2578 static int
2579 prison_restrict_ip4(struct prison *pr, struct in_addr *newip4)
2580 {
2581 int ii, ij, used;
2582 struct prison *ppr;
2583
2584 ppr = pr->pr_parent;
2585 if (!(pr->pr_flags & PR_IP4_USER)) {
2586 /* This has no user settings, so just copy the parent's list. */
2587 if (pr->pr_ip4s < ppr->pr_ip4s) {
2588 /*
2589 * There's no room for the parent's list. Use the
2590 * new list buffer, which is assumed to be big enough
2591 * (if it was passed). If there's no buffer, try to
2592 * allocate one.
2593 */
2594 used = 1;
2595 if (newip4 == NULL) {
2596 newip4 = malloc(ppr->pr_ip4s * sizeof(*newip4),
2597 M_PRISON, M_NOWAIT);
2598 if (newip4 != NULL)
2599 used = 0;
2600 }
2601 if (newip4 != NULL) {
2602 bcopy(ppr->pr_ip4, newip4,
2603 ppr->pr_ip4s * sizeof(*newip4));
2604 free(pr->pr_ip4, M_PRISON);
2605 pr->pr_ip4 = newip4;
2606 pr->pr_ip4s = ppr->pr_ip4s;
2607 }
2608 return (used);
2609 }
2610 pr->pr_ip4s = ppr->pr_ip4s;
2611 if (pr->pr_ip4s > 0)
2612 bcopy(ppr->pr_ip4, pr->pr_ip4,
2613 pr->pr_ip4s * sizeof(*newip4));
2614 else if (pr->pr_ip4 != NULL) {
2615 free(pr->pr_ip4, M_PRISON);
2616 pr->pr_ip4 = NULL;
2617 }
2618 } else if (pr->pr_ip4s > 0) {
2619 /* Remove addresses that aren't in the parent. */
2620 for (ij = 0; ij < ppr->pr_ip4s; ij++)
2621 if (pr->pr_ip4[0].s_addr == ppr->pr_ip4[ij].s_addr)
2622 break;
2623 if (ij < ppr->pr_ip4s)
2624 ii = 1;
2625 else {
2626 bcopy(pr->pr_ip4 + 1, pr->pr_ip4,
2627 --pr->pr_ip4s * sizeof(*pr->pr_ip4));
2628 ii = 0;
2629 }
2630 for (ij = 1; ii < pr->pr_ip4s; ) {
2631 if (pr->pr_ip4[ii].s_addr == ppr->pr_ip4[0].s_addr) {
2632 ii++;
2633 continue;
2634 }
2635 switch (ij >= ppr->pr_ip4s ? -1 :
2636 qcmp_v4(&pr->pr_ip4[ii], &ppr->pr_ip4[ij])) {
2637 case -1:
2638 bcopy(pr->pr_ip4 + ii + 1, pr->pr_ip4 + ii,
2639 (--pr->pr_ip4s - ii) * sizeof(*pr->pr_ip4));
2640 break;
2641 case 0:
2642 ii++;
2643 ij++;
2644 break;
2645 case 1:
2646 ij++;
2647 break;
2648 }
2649 }
2650 if (pr->pr_ip4s == 0) {
2651 pr->pr_flags |= PR_IP4_DISABLE;
2652 free(pr->pr_ip4, M_PRISON);
2653 pr->pr_ip4 = NULL;
2654 }
2655 }
2656 return (0);
2657 }
2658
2659 /*
2660 * Pass back primary IPv4 address of this jail.
2661 *
2662 * If not restricted return success but do not alter the address. Caller has
2663 * to make sure to initialize it correctly (e.g. INADDR_ANY).
2664 *
2665 * Returns 0 on success, EAFNOSUPPORT if the jail doesn't allow IPv4.
2666 * Address returned in NBO.
2667 */
2668 int
2669 prison_get_ip4(struct ucred *cred, struct in_addr *ia)
2670 {
2671 struct prison *pr;
2672
2673 KASSERT(cred != NULL, ("%s: cred is NULL", __func__));
2674 KASSERT(ia != NULL, ("%s: ia is NULL", __func__));
2675
2676 pr = cred->cr_prison;
2677 if (!(pr->pr_flags & PR_IP4))
2678 return (0);
2679 mtx_lock(&pr->pr_mtx);
2680 if (!(pr->pr_flags & PR_IP4)) {
2681 mtx_unlock(&pr->pr_mtx);
2682 return (0);
2683 }
2684 if (pr->pr_ip4 == NULL) {
2685 mtx_unlock(&pr->pr_mtx);
2686 return (EAFNOSUPPORT);
2687 }
2688
2689 ia->s_addr = pr->pr_ip4[0].s_addr;
2690 mtx_unlock(&pr->pr_mtx);
2691 return (0);
2692 }
2693
2694 /*
2695 * Return 1 if we should do proper source address selection or are not jailed.
2696 * We will return 0 if we should bypass source address selection in favour
2697 * of the primary jail IPv4 address. Only in this case *ia will be updated and
2698 * returned in NBO.
2699 * Return EAFNOSUPPORT, in case this jail does not allow IPv4.
2700 */
2701 int
2702 prison_saddrsel_ip4(struct ucred *cred, struct in_addr *ia)
2703 {
2704 struct prison *pr;
2705 struct in_addr lia;
2706 int error;
2707
2708 KASSERT(cred != NULL, ("%s: cred is NULL", __func__));
2709 KASSERT(ia != NULL, ("%s: ia is NULL", __func__));
2710
2711 if (!jailed(cred))
2712 return (1);
2713
2714 pr = cred->cr_prison;
2715 if (pr->pr_flags & PR_IP4_SADDRSEL)
2716 return (1);
2717
2718 lia.s_addr = INADDR_ANY;
2719 error = prison_get_ip4(cred, &lia);
2720 if (error)
2721 return (error);
2722 if (lia.s_addr == INADDR_ANY)
2723 return (1);
2724
2725 ia->s_addr = lia.s_addr;
2726 return (0);
2727 }
2728
2729 /*
2730 * Return true if pr1 and pr2 have the same IPv4 address restrictions.
2731 */
2732 int
2733 prison_equal_ip4(struct prison *pr1, struct prison *pr2)
2734 {
2735
2736 if (pr1 == pr2)
2737 return (1);
2738
2739 /*
2740 * No need to lock since the PR_IP4_USER flag can't be altered for
2741 * existing prisons.
2742 */
2743 while (pr1 != &prison0 &&
2744 #ifdef VIMAGE
2745 !(pr1->pr_flags & PR_VNET) &&
2746 #endif
2747 !(pr1->pr_flags & PR_IP4_USER))
2748 pr1 = pr1->pr_parent;
2749 while (pr2 != &prison0 &&
2750 #ifdef VIMAGE
2751 !(pr2->pr_flags & PR_VNET) &&
2752 #endif
2753 !(pr2->pr_flags & PR_IP4_USER))
2754 pr2 = pr2->pr_parent;
2755 return (pr1 == pr2);
2756 }
2757
2758 /*
2759 * Make sure our (source) address is set to something meaningful to this
2760 * jail.
2761 *
2762 * Returns 0 if jail doesn't restrict IPv4 or if address belongs to jail,
2763 * EADDRNOTAVAIL if the address doesn't belong, or EAFNOSUPPORT if the jail
2764 * doesn't allow IPv4. Address passed in in NBO and returned in NBO.
2765 */
2766 int
2767 prison_local_ip4(struct ucred *cred, struct in_addr *ia)
2768 {
2769 struct prison *pr;
2770 struct in_addr ia0;
2771 int error;
2772
2773 KASSERT(cred != NULL, ("%s: cred is NULL", __func__));
2774 KASSERT(ia != NULL, ("%s: ia is NULL", __func__));
2775
2776 pr = cred->cr_prison;
2777 if (!(pr->pr_flags & PR_IP4))
2778 return (0);
2779 mtx_lock(&pr->pr_mtx);
2780 if (!(pr->pr_flags & PR_IP4)) {
2781 mtx_unlock(&pr->pr_mtx);
2782 return (0);
2783 }
2784 if (pr->pr_ip4 == NULL) {
2785 mtx_unlock(&pr->pr_mtx);
2786 return (EAFNOSUPPORT);
2787 }
2788
2789 ia0.s_addr = ntohl(ia->s_addr);
2790 if (ia0.s_addr == INADDR_LOOPBACK) {
2791 ia->s_addr = pr->pr_ip4[0].s_addr;
2792 mtx_unlock(&pr->pr_mtx);
2793 return (0);
2794 }
2795
2796 if (ia0.s_addr == INADDR_ANY) {
2797 /*
2798 * In case there is only 1 IPv4 address, bind directly.
2799 */
2800 if (pr->pr_ip4s == 1)
2801 ia->s_addr = pr->pr_ip4[0].s_addr;
2802 mtx_unlock(&pr->pr_mtx);
2803 return (0);
2804 }
2805
2806 error = _prison_check_ip4(pr, ia);
2807 mtx_unlock(&pr->pr_mtx);
2808 return (error);
2809 }
2810
2811 /*
2812 * Rewrite destination address in case we will connect to loopback address.
2813 *
2814 * Returns 0 on success, EAFNOSUPPORT if the jail doesn't allow IPv4.
2815 * Address passed in in NBO and returned in NBO.
2816 */
2817 int
2818 prison_remote_ip4(struct ucred *cred, struct in_addr *ia)
2819 {
2820 struct prison *pr;
2821
2822 KASSERT(cred != NULL, ("%s: cred is NULL", __func__));
2823 KASSERT(ia != NULL, ("%s: ia is NULL", __func__));
2824
2825 pr = cred->cr_prison;
2826 if (!(pr->pr_flags & PR_IP4))
2827 return (0);
2828 mtx_lock(&pr->pr_mtx);
2829 if (!(pr->pr_flags & PR_IP4)) {
2830 mtx_unlock(&pr->pr_mtx);
2831 return (0);
2832 }
2833 if (pr->pr_ip4 == NULL) {
2834 mtx_unlock(&pr->pr_mtx);
2835 return (EAFNOSUPPORT);
2836 }
2837
2838 if (ntohl(ia->s_addr) == INADDR_LOOPBACK) {
2839 ia->s_addr = pr->pr_ip4[0].s_addr;
2840 mtx_unlock(&pr->pr_mtx);
2841 return (0);
2842 }
2843
2844 /*
2845 * Return success because nothing had to be changed.
2846 */
2847 mtx_unlock(&pr->pr_mtx);
2848 return (0);
2849 }
2850
2851 /*
2852 * Check if given address belongs to the jail referenced by cred/prison.
2853 *
2854 * Returns 0 if jail doesn't restrict IPv4 or if address belongs to jail,
2855 * EADDRNOTAVAIL if the address doesn't belong, or EAFNOSUPPORT if the jail
2856 * doesn't allow IPv4. Address passed in in NBO.
2857 */
2858 static int
2859 _prison_check_ip4(struct prison *pr, struct in_addr *ia)
2860 {
2861 int i, a, z, d;
2862
2863 /*
2864 * Check the primary IP.
2865 */
2866 if (pr->pr_ip4[0].s_addr == ia->s_addr)
2867 return (0);
2868
2869 /*
2870 * All the other IPs are sorted so we can do a binary search.
2871 */
2872 a = 0;
2873 z = pr->pr_ip4s - 2;
2874 while (a <= z) {
2875 i = (a + z) / 2;
2876 d = qcmp_v4(&pr->pr_ip4[i+1], ia);
2877 if (d > 0)
2878 z = i - 1;
2879 else if (d < 0)
2880 a = i + 1;
2881 else
2882 return (0);
2883 }
2884
2885 return (EADDRNOTAVAIL);
2886 }
2887
2888 int
2889 prison_check_ip4(struct ucred *cred, struct in_addr *ia)
2890 {
2891 struct prison *pr;
2892 int error;
2893
2894 KASSERT(cred != NULL, ("%s: cred is NULL", __func__));
2895 KASSERT(ia != NULL, ("%s: ia is NULL", __func__));
2896
2897 pr = cred->cr_prison;
2898 if (!(pr->pr_flags & PR_IP4))
2899 return (0);
2900 mtx_lock(&pr->pr_mtx);
2901 if (!(pr->pr_flags & PR_IP4)) {
2902 mtx_unlock(&pr->pr_mtx);
2903 return (0);
2904 }
2905 if (pr->pr_ip4 == NULL) {
2906 mtx_unlock(&pr->pr_mtx);
2907 return (EAFNOSUPPORT);
2908 }
2909
2910 error = _prison_check_ip4(pr, ia);
2911 mtx_unlock(&pr->pr_mtx);
2912 return (error);
2913 }
2914 #endif
2915
2916 #ifdef INET6
2917 static int
2918 prison_restrict_ip6(struct prison *pr, struct in6_addr *newip6)
2919 {
2920 int ii, ij, used;
2921 struct prison *ppr;
2922
2923 ppr = pr->pr_parent;
2924 if (!(pr->pr_flags & PR_IP6_USER)) {
2925 /* This has no user settings, so just copy the parent's list. */
2926 if (pr->pr_ip6s < ppr->pr_ip6s) {
2927 /*
2928 * There's no room for the parent's list. Use the
2929 * new list buffer, which is assumed to be big enough
2930 * (if it was passed). If there's no buffer, try to
2931 * allocate one.
2932 */
2933 used = 1;
2934 if (newip6 == NULL) {
2935 newip6 = malloc(ppr->pr_ip6s * sizeof(*newip6),
2936 M_PRISON, M_NOWAIT);
2937 if (newip6 != NULL)
2938 used = 0;
2939 }
2940 if (newip6 != NULL) {
2941 bcopy(ppr->pr_ip6, newip6,
2942 ppr->pr_ip6s * sizeof(*newip6));
2943 free(pr->pr_ip6, M_PRISON);
2944 pr->pr_ip6 = newip6;
2945 pr->pr_ip6s = ppr->pr_ip6s;
2946 }
2947 return (used);
2948 }
2949 pr->pr_ip6s = ppr->pr_ip6s;
2950 if (pr->pr_ip6s > 0)
2951 bcopy(ppr->pr_ip6, pr->pr_ip6,
2952 pr->pr_ip6s * sizeof(*newip6));
2953 else if (pr->pr_ip6 != NULL) {
2954 free(pr->pr_ip6, M_PRISON);
2955 pr->pr_ip6 = NULL;
2956 }
2957 } else if (pr->pr_ip6s > 0) {
2958 /* Remove addresses that aren't in the parent. */
2959 for (ij = 0; ij < ppr->pr_ip6s; ij++)
2960 if (IN6_ARE_ADDR_EQUAL(&pr->pr_ip6[0],
2961 &ppr->pr_ip6[ij]))
2962 break;
2963 if (ij < ppr->pr_ip6s)
2964 ii = 1;
2965 else {
2966 bcopy(pr->pr_ip6 + 1, pr->pr_ip6,
2967 --pr->pr_ip6s * sizeof(*pr->pr_ip6));
2968 ii = 0;
2969 }
2970 for (ij = 1; ii < pr->pr_ip6s; ) {
2971 if (IN6_ARE_ADDR_EQUAL(&pr->pr_ip6[ii],
2972 &ppr->pr_ip6[0])) {
2973 ii++;
2974 continue;
2975 }
2976 switch (ij >= ppr->pr_ip4s ? -1 :
2977 qcmp_v6(&pr->pr_ip6[ii], &ppr->pr_ip6[ij])) {
2978 case -1:
2979 bcopy(pr->pr_ip6 + ii + 1, pr->pr_ip6 + ii,
2980 (--pr->pr_ip6s - ii) * sizeof(*pr->pr_ip6));
2981 break;
2982 case 0:
2983 ii++;
2984 ij++;
2985 break;
2986 case 1:
2987 ij++;
2988 break;
2989 }
2990 }
2991 if (pr->pr_ip6s == 0) {
2992 pr->pr_flags |= PR_IP6_DISABLE;
2993 free(pr->pr_ip6, M_PRISON);
2994 pr->pr_ip6 = NULL;
2995 }
2996 }
2997 return 0;
2998 }
2999
3000 /*
3001 * Pass back primary IPv6 address for this jail.
3002 *
3003 * If not restricted return success but do not alter the address. Caller has
3004 * to make sure to initialize it correctly (e.g. IN6ADDR_ANY_INIT).
3005 *
3006 * Returns 0 on success, EAFNOSUPPORT if the jail doesn't allow IPv6.
3007 */
3008 int
3009 prison_get_ip6(struct ucred *cred, struct in6_addr *ia6)
3010 {
3011 struct prison *pr;
3012
3013 KASSERT(cred != NULL, ("%s: cred is NULL", __func__));
3014 KASSERT(ia6 != NULL, ("%s: ia6 is NULL", __func__));
3015
3016 pr = cred->cr_prison;
3017 if (!(pr->pr_flags & PR_IP6))
3018 return (0);
3019 mtx_lock(&pr->pr_mtx);
3020 if (!(pr->pr_flags & PR_IP6)) {
3021 mtx_unlock(&pr->pr_mtx);
3022 return (0);
3023 }
3024 if (pr->pr_ip6 == NULL) {
3025 mtx_unlock(&pr->pr_mtx);
3026 return (EAFNOSUPPORT);
3027 }
3028
3029 bcopy(&pr->pr_ip6[0], ia6, sizeof(struct in6_addr));
3030 mtx_unlock(&pr->pr_mtx);
3031 return (0);
3032 }
3033
3034 /*
3035 * Return 1 if we should do proper source address selection or are not jailed.
3036 * We will return 0 if we should bypass source address selection in favour
3037 * of the primary jail IPv6 address. Only in this case *ia will be updated and
3038 * returned in NBO.
3039 * Return EAFNOSUPPORT, in case this jail does not allow IPv6.
3040 */
3041 int
3042 prison_saddrsel_ip6(struct ucred *cred, struct in6_addr *ia6)
3043 {
3044 struct prison *pr;
3045 struct in6_addr lia6;
3046 int error;
3047
3048 KASSERT(cred != NULL, ("%s: cred is NULL", __func__));
3049 KASSERT(ia6 != NULL, ("%s: ia6 is NULL", __func__));
3050
3051 if (!jailed(cred))
3052 return (1);
3053
3054 pr = cred->cr_prison;
3055 if (pr->pr_flags & PR_IP6_SADDRSEL)
3056 return (1);
3057
3058 lia6 = in6addr_any;
3059 error = prison_get_ip6(cred, &lia6);
3060 if (error)
3061 return (error);
3062 if (IN6_IS_ADDR_UNSPECIFIED(&lia6))
3063 return (1);
3064
3065 bcopy(&lia6, ia6, sizeof(struct in6_addr));
3066 return (0);
3067 }
3068
3069 /*
3070 * Return true if pr1 and pr2 have the same IPv6 address restrictions.
3071 */
3072 int
3073 prison_equal_ip6(struct prison *pr1, struct prison *pr2)
3074 {
3075
3076 if (pr1 == pr2)
3077 return (1);
3078
3079 while (pr1 != &prison0 &&
3080 #ifdef VIMAGE
3081 !(pr1->pr_flags & PR_VNET) &&
3082 #endif
3083 !(pr1->pr_flags & PR_IP6_USER))
3084 pr1 = pr1->pr_parent;
3085 while (pr2 != &prison0 &&
3086 #ifdef VIMAGE
3087 !(pr2->pr_flags & PR_VNET) &&
3088 #endif
3089 !(pr2->pr_flags & PR_IP6_USER))
3090 pr2 = pr2->pr_parent;
3091 return (pr1 == pr2);
3092 }
3093
3094 /*
3095 * Make sure our (source) address is set to something meaningful to this jail.
3096 *
3097 * v6only should be set based on (inp->inp_flags & IN6P_IPV6_V6ONLY != 0)
3098 * when needed while binding.
3099 *
3100 * Returns 0 if jail doesn't restrict IPv6 or if address belongs to jail,
3101 * EADDRNOTAVAIL if the address doesn't belong, or EAFNOSUPPORT if the jail
3102 * doesn't allow IPv6.
3103 */
3104 int
3105 prison_local_ip6(struct ucred *cred, struct in6_addr *ia6, int v6only)
3106 {
3107 struct prison *pr;
3108 int error;
3109
3110 KASSERT(cred != NULL, ("%s: cred is NULL", __func__));
3111 KASSERT(ia6 != NULL, ("%s: ia6 is NULL", __func__));
3112
3113 pr = cred->cr_prison;
3114 if (!(pr->pr_flags & PR_IP6))
3115 return (0);
3116 mtx_lock(&pr->pr_mtx);
3117 if (!(pr->pr_flags & PR_IP6)) {
3118 mtx_unlock(&pr->pr_mtx);
3119 return (0);
3120 }
3121 if (pr->pr_ip6 == NULL) {
3122 mtx_unlock(&pr->pr_mtx);
3123 return (EAFNOSUPPORT);
3124 }
3125
3126 if (IN6_IS_ADDR_LOOPBACK(ia6)) {
3127 bcopy(&pr->pr_ip6[0], ia6, sizeof(struct in6_addr));
3128 mtx_unlock(&pr->pr_mtx);
3129 return (0);
3130 }
3131
3132 if (IN6_IS_ADDR_UNSPECIFIED(ia6)) {
3133 /*
3134 * In case there is only 1 IPv6 address, and v6only is true,
3135 * then bind directly.
3136 */
3137 if (v6only != 0 && pr->pr_ip6s == 1)
3138 bcopy(&pr->pr_ip6[0], ia6, sizeof(struct in6_addr));
3139 mtx_unlock(&pr->pr_mtx);
3140 return (0);
3141 }
3142
3143 error = _prison_check_ip6(pr, ia6);
3144 mtx_unlock(&pr->pr_mtx);
3145 return (error);
3146 }
3147
3148 /*
3149 * Rewrite destination address in case we will connect to loopback address.
3150 *
3151 * Returns 0 on success, EAFNOSUPPORT if the jail doesn't allow IPv6.
3152 */
3153 int
3154 prison_remote_ip6(struct ucred *cred, struct in6_addr *ia6)
3155 {
3156 struct prison *pr;
3157
3158 KASSERT(cred != NULL, ("%s: cred is NULL", __func__));
3159 KASSERT(ia6 != NULL, ("%s: ia6 is NULL", __func__));
3160
3161 pr = cred->cr_prison;
3162 if (!(pr->pr_flags & PR_IP6))
3163 return (0);
3164 mtx_lock(&pr->pr_mtx);
3165 if (!(pr->pr_flags & PR_IP6)) {
3166 mtx_unlock(&pr->pr_mtx);
3167 return (0);
3168 }
3169 if (pr->pr_ip6 == NULL) {
3170 mtx_unlock(&pr->pr_mtx);
3171 return (EAFNOSUPPORT);
3172 }
3173
3174 if (IN6_IS_ADDR_LOOPBACK(ia6)) {
3175 bcopy(&pr->pr_ip6[0], ia6, sizeof(struct in6_addr));
3176 mtx_unlock(&pr->pr_mtx);
3177 return (0);
3178 }
3179
3180 /*
3181 * Return success because nothing had to be changed.
3182 */
3183 mtx_unlock(&pr->pr_mtx);
3184 return (0);
3185 }
3186
3187 /*
3188 * Check if given address belongs to the jail referenced by cred/prison.
3189 *
3190 * Returns 0 if jail doesn't restrict IPv6 or if address belongs to jail,
3191 * EADDRNOTAVAIL if the address doesn't belong, or EAFNOSUPPORT if the jail
3192 * doesn't allow IPv6.
3193 */
3194 static int
3195 _prison_check_ip6(struct prison *pr, struct in6_addr *ia6)
3196 {
3197 int i, a, z, d;
3198
3199 /*
3200 * Check the primary IP.
3201 */
3202 if (IN6_ARE_ADDR_EQUAL(&pr->pr_ip6[0], ia6))
3203 return (0);
3204
3205 /*
3206 * All the other IPs are sorted so we can do a binary search.
3207 */
3208 a = 0;
3209 z = pr->pr_ip6s - 2;
3210 while (a <= z) {
3211 i = (a + z) / 2;
3212 d = qcmp_v6(&pr->pr_ip6[i+1], ia6);
3213 if (d > 0)
3214 z = i - 1;
3215 else if (d < 0)
3216 a = i + 1;
3217 else
3218 return (0);
3219 }
3220
3221 return (EADDRNOTAVAIL);
3222 }
3223
3224 int
3225 prison_check_ip6(struct ucred *cred, struct in6_addr *ia6)
3226 {
3227 struct prison *pr;
3228 int error;
3229
3230 KASSERT(cred != NULL, ("%s: cred is NULL", __func__));
3231 KASSERT(ia6 != NULL, ("%s: ia6 is NULL", __func__));
3232
3233 pr = cred->cr_prison;
3234 if (!(pr->pr_flags & PR_IP6))
3235 return (0);
3236 mtx_lock(&pr->pr_mtx);
3237 if (!(pr->pr_flags & PR_IP6)) {
3238 mtx_unlock(&pr->pr_mtx);
3239 return (0);
3240 }
3241 if (pr->pr_ip6 == NULL) {
3242 mtx_unlock(&pr->pr_mtx);
3243 return (EAFNOSUPPORT);
3244 }
3245
3246 error = _prison_check_ip6(pr, ia6);
3247 mtx_unlock(&pr->pr_mtx);
3248 return (error);
3249 }
3250 #endif
3251
3252 /*
3253 * Check if a jail supports the given address family.
3254 *
3255 * Returns 0 if not jailed or the address family is supported, EAFNOSUPPORT
3256 * if not.
3257 */
3258 int
3259 prison_check_af(struct ucred *cred, int af)
3260 {
3261 struct prison *pr;
3262 int error;
3263
3264 KASSERT(cred != NULL, ("%s: cred is NULL", __func__));
3265
3266 pr = cred->cr_prison;
3267 #ifdef VIMAGE
3268 /* Prisons with their own network stack are not limited. */
3269 if (prison_owns_vnet(cred))
3270 return (0);
3271 #endif
3272
3273 error = 0;
3274 switch (af)
3275 {
3276 #ifdef INET
3277 case AF_INET:
3278 if (pr->pr_flags & PR_IP4)
3279 {
3280 mtx_lock(&pr->pr_mtx);
3281 if ((pr->pr_flags & PR_IP4) && pr->pr_ip4 == NULL)
3282 error = EAFNOSUPPORT;
3283 mtx_unlock(&pr->pr_mtx);
3284 }
3285 break;
3286 #endif
3287 #ifdef INET6
3288 case AF_INET6:
3289 if (pr->pr_flags & PR_IP6)
3290 {
3291 mtx_lock(&pr->pr_mtx);
3292 if ((pr->pr_flags & PR_IP6) && pr->pr_ip6 == NULL)
3293 error = EAFNOSUPPORT;
3294 mtx_unlock(&pr->pr_mtx);
3295 }
3296 break;
3297 #endif
3298 case AF_LOCAL:
3299 case AF_ROUTE:
3300 break;
3301 default:
3302 if (!(pr->pr_allow & PR_ALLOW_SOCKET_AF))
3303 error = EAFNOSUPPORT;
3304 }
3305 return (error);
3306 }
3307
3308 /*
3309 * Check if given address belongs to the jail referenced by cred (wrapper to
3310 * prison_check_ip[46]).
3311 *
3312 * Returns 0 if jail doesn't restrict the address family or if address belongs
3313 * to jail, EADDRNOTAVAIL if the address doesn't belong, or EAFNOSUPPORT if
3314 * the jail doesn't allow the address family. IPv4 Address passed in in NBO.
3315 */
3316 int
3317 prison_if(struct ucred *cred, struct sockaddr *sa)
3318 {
3319 #ifdef INET
3320 struct sockaddr_in *sai;
3321 #endif
3322 #ifdef INET6
3323 struct sockaddr_in6 *sai6;
3324 #endif
3325 int error;
3326
3327 KASSERT(cred != NULL, ("%s: cred is NULL", __func__));
3328 KASSERT(sa != NULL, ("%s: sa is NULL", __func__));
3329
3330 #ifdef VIMAGE
3331 if (prison_owns_vnet(cred))
3332 return (0);
3333 #endif
3334
3335 error = 0;
3336 switch (sa->sa_family)
3337 {
3338 #ifdef INET
3339 case AF_INET:
3340 sai = (struct sockaddr_in *)sa;
3341 error = prison_check_ip4(cred, &sai->sin_addr);
3342 break;
3343 #endif
3344 #ifdef INET6
3345 case AF_INET6:
3346 sai6 = (struct sockaddr_in6 *)sa;
3347 error = prison_check_ip6(cred, &sai6->sin6_addr);
3348 break;
3349 #endif
3350 default:
3351 if (!(cred->cr_prison->pr_allow & PR_ALLOW_SOCKET_AF))
3352 error = EAFNOSUPPORT;
3353 }
3354 return (error);
3355 }
3356
3357 /*
3358 * Return 0 if jails permit p1 to frob p2, otherwise ESRCH.
3359 */
3360 int
3361 prison_check(struct ucred *cred1, struct ucred *cred2)
3362 {
3363
3364 return ((cred1->cr_prison == cred2->cr_prison ||
3365 prison_ischild(cred1->cr_prison, cred2->cr_prison)) ? 0 : ESRCH);
3366 }
3367
3368 /*
3369 * Return 1 if p2 is a child of p1, otherwise 0.
3370 */
3371 int
3372 prison_ischild(struct prison *pr1, struct prison *pr2)
3373 {
3374
3375 for (pr2 = pr2->pr_parent; pr2 != NULL; pr2 = pr2->pr_parent)
3376 if (pr1 == pr2)
3377 return (1);
3378 return (0);
3379 }
3380
3381 /*
3382 * Return 1 if the passed credential is in a jail, otherwise 0.
3383 */
3384 int
3385 jailed(struct ucred *cred)
3386 {
3387
3388 return (cred->cr_prison != &prison0);
3389 }
3390
3391 /*
3392 * Return 1 if the passed credential is in a jail and that jail does not
3393 * have its own virtual network stack, otherwise 0.
3394 */
3395 int
3396 jailed_without_vnet(struct ucred *cred)
3397 {
3398
3399 if (!jailed(cred))
3400 return (0);
3401 #ifdef VIMAGE
3402 if (prison_owns_vnet(cred))
3403 return (0);
3404 #endif
3405
3406 return (1);
3407 }
3408
3409 /*
3410 * Return the correct hostname (domainname, et al) for the passed credential.
3411 */
3412 void
3413 getcredhostname(struct ucred *cred, char *buf, size_t size)
3414 {
3415 struct prison *pr;
3416
3417 /*
3418 * A NULL credential can be used to shortcut to the physical
3419 * system's hostname.
3420 */
3421 pr = (cred != NULL) ? cred->cr_prison : &prison0;
3422 mtx_lock(&pr->pr_mtx);
3423 strlcpy(buf, pr->pr_hostname, size);
3424 mtx_unlock(&pr->pr_mtx);
3425 }
3426
3427 void
3428 getcreddomainname(struct ucred *cred, char *buf, size_t size)
3429 {
3430
3431 mtx_lock(&cred->cr_prison->pr_mtx);
3432 strlcpy(buf, cred->cr_prison->pr_domainname, size);
3433 mtx_unlock(&cred->cr_prison->pr_mtx);
3434 }
3435
3436 void
3437 getcredhostuuid(struct ucred *cred, char *buf, size_t size)
3438 {
3439
3440 mtx_lock(&cred->cr_prison->pr_mtx);
3441 strlcpy(buf, cred->cr_prison->pr_hostuuid, size);
3442 mtx_unlock(&cred->cr_prison->pr_mtx);
3443 }
3444
3445 void
3446 getcredhostid(struct ucred *cred, unsigned long *hostid)
3447 {
3448
3449 mtx_lock(&cred->cr_prison->pr_mtx);
3450 *hostid = cred->cr_prison->pr_hostid;
3451 mtx_unlock(&cred->cr_prison->pr_mtx);
3452 }
3453
3454 #ifdef VIMAGE
3455 /*
3456 * Determine whether the prison represented by cred owns
3457 * its vnet rather than having it inherited.
3458 *
3459 * Returns 1 in case the prison owns the vnet, 0 otherwise.
3460 */
3461 int
3462 prison_owns_vnet(struct ucred *cred)
3463 {
3464
3465 /*
3466 * vnets cannot be added/removed after jail creation,
3467 * so no need to lock here.
3468 */
3469 return (cred->cr_prison->pr_flags & PR_VNET ? 1 : 0);
3470 }
3471 #endif
3472
3473 /*
3474 * Determine whether the subject represented by cred can "see"
3475 * status of a mount point.
3476 * Returns: 0 for permitted, ENOENT otherwise.
3477 * XXX: This function should be called cr_canseemount() and should be
3478 * placed in kern_prot.c.
3479 */
3480 int
3481 prison_canseemount(struct ucred *cred, struct mount *mp)
3482 {
3483 struct prison *pr;
3484 struct statfs *sp;
3485 size_t len;
3486
3487 pr = cred->cr_prison;
3488 if (pr->pr_enforce_statfs == 0)
3489 return (0);
3490 if (pr->pr_root->v_mount == mp)
3491 return (0);
3492 if (pr->pr_enforce_statfs == 2)
3493 return (ENOENT);
3494 /*
3495 * If jail's chroot directory is set to "/" we should be able to see
3496 * all mount-points from inside a jail.
3497 * This is ugly check, but this is the only situation when jail's
3498 * directory ends with '/'.
3499 */
3500 if (strcmp(pr->pr_path, "/") == 0)
3501 return (0);
3502 len = strlen(pr->pr_path);
3503 sp = &mp->mnt_stat;
3504 if (strncmp(pr->pr_path, sp->f_mntonname, len) != 0)
3505 return (ENOENT);
3506 /*
3507 * Be sure that we don't have situation where jail's root directory
3508 * is "/some/path" and mount point is "/some/pathpath".
3509 */
3510 if (sp->f_mntonname[len] != '\0' && sp->f_mntonname[len] != '/')
3511 return (ENOENT);
3512 return (0);
3513 }
3514
3515 void
3516 prison_enforce_statfs(struct ucred *cred, struct mount *mp, struct statfs *sp)
3517 {
3518 char jpath[MAXPATHLEN];
3519 struct prison *pr;
3520 size_t len;
3521
3522 pr = cred->cr_prison;
3523 if (pr->pr_enforce_statfs == 0)
3524 return;
3525 if (prison_canseemount(cred, mp) != 0) {
3526 bzero(sp->f_mntonname, sizeof(sp->f_mntonname));
3527 strlcpy(sp->f_mntonname, "[restricted]",
3528 sizeof(sp->f_mntonname));
3529 return;
3530 }
3531 if (pr->pr_root->v_mount == mp) {
3532 /*
3533 * Clear current buffer data, so we are sure nothing from
3534 * the valid path left there.
3535 */
3536 bzero(sp->f_mntonname, sizeof(sp->f_mntonname));
3537 *sp->f_mntonname = '/';
3538 return;
3539 }
3540 /*
3541 * If jail's chroot directory is set to "/" we should be able to see
3542 * all mount-points from inside a jail.
3543 */
3544 if (strcmp(pr->pr_path, "/") == 0)
3545 return;
3546 len = strlen(pr->pr_path);
3547 strlcpy(jpath, sp->f_mntonname + len, sizeof(jpath));
3548 /*
3549 * Clear current buffer data, so we are sure nothing from
3550 * the valid path left there.
3551 */
3552 bzero(sp->f_mntonname, sizeof(sp->f_mntonname));
3553 if (*jpath == '\0') {
3554 /* Should never happen. */
3555 *sp->f_mntonname = '/';
3556 } else {
3557 strlcpy(sp->f_mntonname, jpath, sizeof(sp->f_mntonname));
3558 }
3559 }
3560
3561 /*
3562 * Check with permission for a specific privilege is granted within jail. We
3563 * have a specific list of accepted privileges; the rest are denied.
3564 */
3565 int
3566 prison_priv_check(struct ucred *cred, int priv)
3567 {
3568
3569 if (!jailed(cred))
3570 return (0);
3571
3572 #ifdef VIMAGE
3573 /*
3574 * Privileges specific to prisons with a virtual network stack.
3575 * There might be a duplicate entry here in case the privilege
3576 * is only granted conditionally in the legacy jail case.
3577 */
3578 switch (priv) {
3579 #ifdef notyet
3580 /*
3581 * NFS-specific privileges.
3582 */
3583 case PRIV_NFS_DAEMON:
3584 case PRIV_NFS_LOCKD:
3585 #endif
3586 /*
3587 * Network stack privileges.
3588 */
3589 case PRIV_NET_BRIDGE:
3590 case PRIV_NET_GRE:
3591 case PRIV_NET_BPF:
3592 case PRIV_NET_RAW: /* Dup, cond. in legacy jail case. */
3593 case PRIV_NET_ROUTE:
3594 case PRIV_NET_TAP:
3595 case PRIV_NET_SETIFMTU:
3596 case PRIV_NET_SETIFFLAGS:
3597 case PRIV_NET_SETIFCAP:
3598 case PRIV_NET_SETIFDESCR:
3599 case PRIV_NET_SETIFNAME :
3600 case PRIV_NET_SETIFMETRIC:
3601 case PRIV_NET_SETIFPHYS:
3602 case PRIV_NET_SETIFMAC:
3603 case PRIV_NET_ADDMULTI:
3604 case PRIV_NET_DELMULTI:
3605 case PRIV_NET_HWIOCTL:
3606 case PRIV_NET_SETLLADDR:
3607 case PRIV_NET_ADDIFGROUP:
3608 case PRIV_NET_DELIFGROUP:
3609 case PRIV_NET_IFCREATE:
3610 case PRIV_NET_IFDESTROY:
3611 case PRIV_NET_ADDIFADDR:
3612 case PRIV_NET_DELIFADDR:
3613 case PRIV_NET_LAGG:
3614 case PRIV_NET_GIF:
3615 case PRIV_NET_SETIFVNET:
3616
3617 /*
3618 * 802.11-related privileges.
3619 */
3620 case PRIV_NET80211_GETKEY:
3621 #ifdef notyet
3622 case PRIV_NET80211_MANAGE: /* XXX-BZ discuss with sam@ */
3623 #endif
3624
3625 #ifdef notyet
3626 /*
3627 * AppleTalk privileges.
3628 */
3629 case PRIV_NETATALK_RESERVEDPORT:
3630
3631 /*
3632 * ATM privileges.
3633 */
3634 case PRIV_NETATM_CFG:
3635 case PRIV_NETATM_ADD:
3636 case PRIV_NETATM_DEL:
3637 case PRIV_NETATM_SET:
3638
3639 /*
3640 * Bluetooth privileges.
3641 */
3642 case PRIV_NETBLUETOOTH_RAW:
3643 #endif
3644
3645 /*
3646 * Netgraph and netgraph module privileges.
3647 */
3648 case PRIV_NETGRAPH_CONTROL:
3649 #ifdef notyet
3650 case PRIV_NETGRAPH_TTY:
3651 #endif
3652
3653 /*
3654 * IPv4 and IPv6 privileges.
3655 */
3656 case PRIV_NETINET_IPFW:
3657 case PRIV_NETINET_DIVERT:
3658 case PRIV_NETINET_PF:
3659 case PRIV_NETINET_DUMMYNET:
3660 case PRIV_NETINET_CARP:
3661 case PRIV_NETINET_MROUTE:
3662 case PRIV_NETINET_RAW:
3663 case PRIV_NETINET_ADDRCTRL6:
3664 case PRIV_NETINET_ND6:
3665 case PRIV_NETINET_SCOPE6:
3666 case PRIV_NETINET_ALIFETIME6:
3667 case PRIV_NETINET_IPSEC:
3668 case PRIV_NETINET_BINDANY:
3669
3670 #ifdef notyet
3671 /*
3672 * IPX/SPX privileges.
3673 */
3674 case PRIV_NETIPX_RESERVEDPORT:
3675 case PRIV_NETIPX_RAW:
3676
3677 /*
3678 * NCP privileges.
3679 */
3680 case PRIV_NETNCP:
3681
3682 /*
3683 * SMB privileges.
3684 */
3685 case PRIV_NETSMB:
3686 #endif
3687
3688 /*
3689 * No default: or deny here.
3690 * In case of no permit fall through to next switch().
3691 */
3692 if (cred->cr_prison->pr_flags & PR_VNET)
3693 return (0);
3694 }
3695 #endif /* VIMAGE */
3696
3697 switch (priv) {
3698
3699 /*
3700 * Allow ktrace privileges for root in jail.
3701 */
3702 case PRIV_KTRACE:
3703
3704 #if 0
3705 /*
3706 * Allow jailed processes to configure audit identity and
3707 * submit audit records (login, etc). In the future we may
3708 * want to further refine the relationship between audit and
3709 * jail.
3710 */
3711 case PRIV_AUDIT_GETAUDIT:
3712 case PRIV_AUDIT_SETAUDIT:
3713 case PRIV_AUDIT_SUBMIT:
3714 #endif
3715
3716 /*
3717 * Allow jailed processes to manipulate process UNIX
3718 * credentials in any way they see fit.
3719 */
3720 case PRIV_CRED_SETUID:
3721 case PRIV_CRED_SETEUID:
3722 case PRIV_CRED_SETGID:
3723 case PRIV_CRED_SETEGID:
3724 case PRIV_CRED_SETGROUPS:
3725 case PRIV_CRED_SETREUID:
3726 case PRIV_CRED_SETREGID:
3727 case PRIV_CRED_SETRESUID:
3728 case PRIV_CRED_SETRESGID:
3729
3730 /*
3731 * Jail implements visibility constraints already, so allow
3732 * jailed root to override uid/gid-based constraints.
3733 */
3734 case PRIV_SEEOTHERGIDS:
3735 case PRIV_SEEOTHERUIDS:
3736
3737 /*
3738 * Jail implements inter-process debugging limits already, so
3739 * allow jailed root various debugging privileges.
3740 */
3741 case PRIV_DEBUG_DIFFCRED:
3742 case PRIV_DEBUG_SUGID:
3743 case PRIV_DEBUG_UNPRIV:
3744
3745 /*
3746 * Allow jail to set various resource limits and login
3747 * properties, and for now, exceed process resource limits.
3748 */
3749 case PRIV_PROC_LIMIT:
3750 case PRIV_PROC_SETLOGIN:
3751 case PRIV_PROC_SETRLIMIT:
3752
3753 /*
3754 * System V and POSIX IPC privileges are granted in jail.
3755 */
3756 case PRIV_IPC_READ:
3757 case PRIV_IPC_WRITE:
3758 case PRIV_IPC_ADMIN:
3759 case PRIV_IPC_MSGSIZE:
3760 case PRIV_MQ_ADMIN:
3761
3762 /*
3763 * Jail operations within a jail work on child jails.
3764 */
3765 case PRIV_JAIL_ATTACH:
3766 case PRIV_JAIL_SET:
3767 case PRIV_JAIL_REMOVE:
3768
3769 /*
3770 * Jail implements its own inter-process limits, so allow
3771 * root processes in jail to change scheduling on other
3772 * processes in the same jail. Likewise for signalling.
3773 */
3774 case PRIV_SCHED_DIFFCRED:
3775 case PRIV_SCHED_CPUSET:
3776 case PRIV_SIGNAL_DIFFCRED:
3777 case PRIV_SIGNAL_SUGID:
3778
3779 /*
3780 * Allow jailed processes to write to sysctls marked as jail
3781 * writable.
3782 */
3783 case PRIV_SYSCTL_WRITEJAIL:
3784
3785 /*
3786 * Allow root in jail to manage a variety of quota
3787 * properties. These should likely be conditional on a
3788 * configuration option.
3789 */
3790 case PRIV_VFS_GETQUOTA:
3791 case PRIV_VFS_SETQUOTA:
3792
3793 /*
3794 * Since Jail relies on chroot() to implement file system
3795 * protections, grant many VFS privileges to root in jail.
3796 * Be careful to exclude mount-related and NFS-related
3797 * privileges.
3798 */
3799 case PRIV_VFS_READ:
3800 case PRIV_VFS_WRITE:
3801 case PRIV_VFS_ADMIN:
3802 case PRIV_VFS_EXEC:
3803 case PRIV_VFS_LOOKUP:
3804 case PRIV_VFS_BLOCKRESERVE: /* XXXRW: Slightly surprising. */
3805 case PRIV_VFS_CHFLAGS_DEV:
3806 case PRIV_VFS_CHOWN:
3807 case PRIV_VFS_CHROOT:
3808 case PRIV_VFS_RETAINSUGID:
3809 case PRIV_VFS_FCHROOT:
3810 case PRIV_VFS_LINK:
3811 case PRIV_VFS_SETGID:
3812 case PRIV_VFS_STAT:
3813 case PRIV_VFS_STICKYFILE:
3814 return (0);
3815
3816 /*
3817 * Depending on the global setting, allow privilege of
3818 * setting system flags.
3819 */
3820 case PRIV_VFS_SYSFLAGS:
3821 if (cred->cr_prison->pr_allow & PR_ALLOW_CHFLAGS)
3822 return (0);
3823 else
3824 return (EPERM);
3825
3826 /*
3827 * Depending on the global setting, allow privilege of
3828 * mounting/unmounting file systems.
3829 */
3830 case PRIV_VFS_MOUNT:
3831 case PRIV_VFS_UNMOUNT:
3832 case PRIV_VFS_MOUNT_NONUSER:
3833 case PRIV_VFS_MOUNT_OWNER:
3834 if (cred->cr_prison->pr_allow & PR_ALLOW_MOUNT)
3835 return (0);
3836 else
3837 return (EPERM);
3838
3839 /*
3840 * Allow jailed root to bind reserved ports and reuse in-use
3841 * ports.
3842 */
3843 case PRIV_NETINET_RESERVEDPORT:
3844 case PRIV_NETINET_REUSEPORT:
3845 return (0);
3846
3847 /*
3848 * Allow jailed root to set certian IPv4/6 (option) headers.
3849 */
3850 case PRIV_NETINET_SETHDROPTS:
3851 return (0);
3852
3853 /*
3854 * Conditionally allow creating raw sockets in jail.
3855 */
3856 case PRIV_NETINET_RAW:
3857 if (cred->cr_prison->pr_allow & PR_ALLOW_RAW_SOCKETS)
3858 return (0);
3859 else
3860 return (EPERM);
3861
3862 /*
3863 * Since jail implements its own visibility limits on netstat
3864 * sysctls, allow getcred. This allows identd to work in
3865 * jail.
3866 */
3867 case PRIV_NETINET_GETCRED:
3868 return (0);
3869
3870 default:
3871 /*
3872 * In all remaining cases, deny the privilege request. This
3873 * includes almost all network privileges, many system
3874 * configuration privileges.
3875 */
3876 return (EPERM);
3877 }
3878 }
3879
3880 /*
3881 * Return the part of pr2's name that is relative to pr1, or the whole name
3882 * if it does not directly follow.
3883 */
3884
3885 char *
3886 prison_name(struct prison *pr1, struct prison *pr2)
3887 {
3888 char *name;
3889
3890 /* Jails see themselves as "" (if they see themselves at all). */
3891 if (pr1 == pr2)
3892 return "";
3893 name = pr2->pr_name;
3894 if (prison_ischild(pr1, pr2)) {
3895 /*
3896 * pr1 isn't locked (and allprison_lock may not be either)
3897 * so its length can't be counted on. But the number of dots
3898 * can be counted on - and counted.
3899 */
3900 for (; pr1 != &prison0; pr1 = pr1->pr_parent)
3901 name = strchr(name, '.') + 1;
3902 }
3903 return (name);
3904 }
3905
3906 /*
3907 * Return the part of pr2's path that is relative to pr1, or the whole path
3908 * if it does not directly follow.
3909 */
3910 static char *
3911 prison_path(struct prison *pr1, struct prison *pr2)
3912 {
3913 char *path1, *path2;
3914 int len1;
3915
3916 path1 = pr1->pr_path;
3917 path2 = pr2->pr_path;
3918 if (!strcmp(path1, "/"))
3919 return (path2);
3920 len1 = strlen(path1);
3921 if (strncmp(path1, path2, len1))
3922 return (path2);
3923 if (path2[len1] == '\0')
3924 return "/";
3925 if (path2[len1] == '/')
3926 return (path2 + len1);
3927 return (path2);
3928 }
3929
3930
3931 /*
3932 * Jail-related sysctls.
3933 */
3934 SYSCTL_NODE(_security, OID_AUTO, jail, CTLFLAG_RW, 0,
3935 "Jails");
3936
3937 static int
3938 sysctl_jail_list(SYSCTL_HANDLER_ARGS)
3939 {
3940 struct xprison *xp;
3941 struct prison *pr, *cpr;
3942 #ifdef INET
3943 struct in_addr *ip4 = NULL;
3944 int ip4s = 0;
3945 #endif
3946 #ifdef INET6
3947 struct in_addr *ip6 = NULL;
3948 int ip6s = 0;
3949 #endif
3950 int descend, error;
3951
3952 xp = malloc(sizeof(*xp), M_TEMP, M_WAITOK);
3953 pr = req->td->td_ucred->cr_prison;
3954 error = 0;
3955 sx_slock(&allprison_lock);
3956 FOREACH_PRISON_DESCENDANT(pr, cpr, descend) {
3957 #if defined(INET) || defined(INET6)
3958 again:
3959 #endif
3960 mtx_lock(&cpr->pr_mtx);
3961 #ifdef INET
3962 if (cpr->pr_ip4s > 0) {
3963 if (ip4s < cpr->pr_ip4s) {
3964 ip4s = cpr->pr_ip4s;
3965 mtx_unlock(&cpr->pr_mtx);
3966 ip4 = realloc(ip4, ip4s *
3967 sizeof(struct in_addr), M_TEMP, M_WAITOK);
3968 goto again;
3969 }
3970 bcopy(cpr->pr_ip4, ip4,
3971 cpr->pr_ip4s * sizeof(struct in_addr));
3972 }
3973 #endif
3974 #ifdef INET6
3975 if (cpr->pr_ip6s > 0) {
3976 if (ip6s < cpr->pr_ip6s) {
3977 ip6s = cpr->pr_ip6s;
3978 mtx_unlock(&cpr->pr_mtx);
3979 ip6 = realloc(ip6, ip6s *
3980 sizeof(struct in6_addr), M_TEMP, M_WAITOK);
3981 goto again;
3982 }
3983 bcopy(cpr->pr_ip6, ip6,
3984 cpr->pr_ip6s * sizeof(struct in6_addr));
3985 }
3986 #endif
3987 if (cpr->pr_ref == 0) {
3988 mtx_unlock(&cpr->pr_mtx);
3989 continue;
3990 }
3991 bzero(xp, sizeof(*xp));
3992 xp->pr_version = XPRISON_VERSION;
3993 xp->pr_id = cpr->pr_id;
3994 xp->pr_state = cpr->pr_uref > 0
3995 ? PRISON_STATE_ALIVE : PRISON_STATE_DYING;
3996 strlcpy(xp->pr_path, prison_path(pr, cpr), sizeof(xp->pr_path));
3997 strlcpy(xp->pr_host, cpr->pr_hostname, sizeof(xp->pr_host));
3998 strlcpy(xp->pr_name, prison_name(pr, cpr), sizeof(xp->pr_name));
3999 #ifdef INET
4000 xp->pr_ip4s = cpr->pr_ip4s;
4001 #endif
4002 #ifdef INET6
4003 xp->pr_ip6s = cpr->pr_ip6s;
4004 #endif
4005 mtx_unlock(&cpr->pr_mtx);
4006 error = SYSCTL_OUT(req, xp, sizeof(*xp));
4007 if (error)
4008 break;
4009 #ifdef INET
4010 if (xp->pr_ip4s > 0) {
4011 error = SYSCTL_OUT(req, ip4,
4012 xp->pr_ip4s * sizeof(struct in_addr));
4013 if (error)
4014 break;
4015 }
4016 #endif
4017 #ifdef INET6
4018 if (xp->pr_ip6s > 0) {
4019 error = SYSCTL_OUT(req, ip6,
4020 xp->pr_ip6s * sizeof(struct in6_addr));
4021 if (error)
4022 break;
4023 }
4024 #endif
4025 }
4026 sx_sunlock(&allprison_lock);
4027 free(xp, M_TEMP);
4028 #ifdef INET
4029 free(ip4, M_TEMP);
4030 #endif
4031 #ifdef INET6
4032 free(ip6, M_TEMP);
4033 #endif
4034 return (error);
4035 }
4036
4037 SYSCTL_OID(_security_jail, OID_AUTO, list,
4038 CTLTYPE_STRUCT | CTLFLAG_RD | CTLFLAG_MPSAFE, NULL, 0,
4039 sysctl_jail_list, "S", "List of active jails");
4040
4041 static int
4042 sysctl_jail_jailed(SYSCTL_HANDLER_ARGS)
4043 {
4044 int error, injail;
4045
4046 injail = jailed(req->td->td_ucred);
4047 error = SYSCTL_OUT(req, &injail, sizeof(injail));
4048
4049 return (error);
4050 }
4051
4052 SYSCTL_PROC(_security_jail, OID_AUTO, jailed,
4053 CTLTYPE_INT | CTLFLAG_RD | CTLFLAG_MPSAFE, NULL, 0,
4054 sysctl_jail_jailed, "I", "Process in jail?");
4055
4056 #if defined(INET) || defined(INET6)
4057 SYSCTL_UINT(_security_jail, OID_AUTO, jail_max_af_ips, CTLFLAG_RW,
4058 &jail_max_af_ips, 0,
4059 "Number of IP addresses a jail may have at most per address family");
4060 #endif
4061
4062 /*
4063 * Default parameters for jail(2) compatability. For historical reasons,
4064 * the sysctl names have varying similarity to the parameter names. Prisons
4065 * just see their own parameters, and can't change them.
4066 */
4067 static int
4068 sysctl_jail_default_allow(SYSCTL_HANDLER_ARGS)
4069 {
4070 struct prison *pr;
4071 int allow, error, i;
4072
4073 pr = req->td->td_ucred->cr_prison;
4074 allow = (pr == &prison0) ? jail_default_allow : pr->pr_allow;
4075
4076 /* Get the current flag value, and convert it to a boolean. */
4077 i = (allow & arg2) ? 1 : 0;
4078 if (arg1 != NULL)
4079 i = !i;
4080 error = sysctl_handle_int(oidp, &i, 0, req);
4081 if (error || !req->newptr)
4082 return (error);
4083 i = i ? arg2 : 0;
4084 if (arg1 != NULL)
4085 i ^= arg2;
4086 /*
4087 * The sysctls don't have CTLFLAGS_PRISON, so assume prison0
4088 * for writing.
4089 */
4090 mtx_lock(&prison0.pr_mtx);
4091 jail_default_allow = (jail_default_allow & ~arg2) | i;
4092 mtx_unlock(&prison0.pr_mtx);
4093 return (0);
4094 }
4095
4096 SYSCTL_PROC(_security_jail, OID_AUTO, set_hostname_allowed,
4097 CTLTYPE_INT | CTLFLAG_RW | CTLFLAG_MPSAFE,
4098 NULL, PR_ALLOW_SET_HOSTNAME, sysctl_jail_default_allow, "I",
4099 "Processes in jail can set their hostnames");
4100 SYSCTL_PROC(_security_jail, OID_AUTO, socket_unixiproute_only,
4101 CTLTYPE_INT | CTLFLAG_RW | CTLFLAG_MPSAFE,
4102 (void *)1, PR_ALLOW_SOCKET_AF, sysctl_jail_default_allow, "I",
4103 "Processes in jail are limited to creating UNIX/IP/route sockets only");
4104 SYSCTL_PROC(_security_jail, OID_AUTO, sysvipc_allowed,
4105 CTLTYPE_INT | CTLFLAG_RW | CTLFLAG_MPSAFE,
4106 NULL, PR_ALLOW_SYSVIPC, sysctl_jail_default_allow, "I",
4107 "Processes in jail can use System V IPC primitives");
4108 SYSCTL_PROC(_security_jail, OID_AUTO, allow_raw_sockets,
4109 CTLTYPE_INT | CTLFLAG_RW | CTLFLAG_MPSAFE,
4110 NULL, PR_ALLOW_RAW_SOCKETS, sysctl_jail_default_allow, "I",
4111 "Prison root can create raw sockets");
4112 SYSCTL_PROC(_security_jail, OID_AUTO, chflags_allowed,
4113 CTLTYPE_INT | CTLFLAG_RW | CTLFLAG_MPSAFE,
4114 NULL, PR_ALLOW_CHFLAGS, sysctl_jail_default_allow, "I",
4115 "Processes in jail can alter system file flags");
4116 SYSCTL_PROC(_security_jail, OID_AUTO, mount_allowed,
4117 CTLTYPE_INT | CTLFLAG_RW | CTLFLAG_MPSAFE,
4118 NULL, PR_ALLOW_MOUNT, sysctl_jail_default_allow, "I",
4119 "Processes in jail can mount/unmount jail-friendly file systems");
4120
4121 static int
4122 sysctl_jail_default_level(SYSCTL_HANDLER_ARGS)
4123 {
4124 struct prison *pr;
4125 int level, error;
4126
4127 pr = req->td->td_ucred->cr_prison;
4128 level = (pr == &prison0) ? *(int *)arg1 : *(int *)((char *)pr + arg2);
4129 error = sysctl_handle_int(oidp, &level, 0, req);
4130 if (error || !req->newptr)
4131 return (error);
4132 *(int *)arg1 = level;
4133 return (0);
4134 }
4135
4136 SYSCTL_PROC(_security_jail, OID_AUTO, enforce_statfs,
4137 CTLTYPE_INT | CTLFLAG_RW | CTLFLAG_MPSAFE,
4138 &jail_default_enforce_statfs, offsetof(struct prison, pr_enforce_statfs),
4139 sysctl_jail_default_level, "I",
4140 "Processes in jail cannot see all mounted file systems");
4141
4142 /*
4143 * Nodes to describe jail parameters. Maximum length of string parameters
4144 * is returned in the string itself, and the other parameters exist merely
4145 * to make themselves and their types known.
4146 */
4147 SYSCTL_NODE(_security_jail, OID_AUTO, param, CTLFLAG_RW, 0,
4148 "Jail parameters");
4149
4150 int
4151 sysctl_jail_param(SYSCTL_HANDLER_ARGS)
4152 {
4153 int i;
4154 long l;
4155 size_t s;
4156 char numbuf[12];
4157
4158 switch (oidp->oid_kind & CTLTYPE)
4159 {
4160 case CTLTYPE_LONG:
4161 case CTLTYPE_ULONG:
4162 l = 0;
4163 #ifdef SCTL_MASK32
4164 if (!(req->flags & SCTL_MASK32))
4165 #endif
4166 return (SYSCTL_OUT(req, &l, sizeof(l)));
4167 case CTLTYPE_INT:
4168 case CTLTYPE_UINT:
4169 i = 0;
4170 return (SYSCTL_OUT(req, &i, sizeof(i)));
4171 case CTLTYPE_STRING:
4172 snprintf(numbuf, sizeof(numbuf), "%d", arg2);
4173 return
4174 (sysctl_handle_string(oidp, numbuf, sizeof(numbuf), req));
4175 case CTLTYPE_STRUCT:
4176 s = (size_t)arg2;
4177 return (SYSCTL_OUT(req, &s, sizeof(s)));
4178 }
4179 return (0);
4180 }
4181
4182 SYSCTL_JAIL_PARAM(, jid, CTLTYPE_INT | CTLFLAG_RDTUN, "I", "Jail ID");
4183 SYSCTL_JAIL_PARAM(, parent, CTLTYPE_INT | CTLFLAG_RD, "I", "Jail parent ID");
4184 SYSCTL_JAIL_PARAM_STRING(, name, CTLFLAG_RW, MAXHOSTNAMELEN, "Jail name");
4185 SYSCTL_JAIL_PARAM_STRING(, path, CTLFLAG_RDTUN, MAXPATHLEN, "Jail root path");
4186 SYSCTL_JAIL_PARAM(, securelevel, CTLTYPE_INT | CTLFLAG_RW,
4187 "I", "Jail secure level");
4188 SYSCTL_JAIL_PARAM(, enforce_statfs, CTLTYPE_INT | CTLFLAG_RW,
4189 "I", "Jail cannot see all mounted file systems");
4190 SYSCTL_JAIL_PARAM(, persist, CTLTYPE_INT | CTLFLAG_RW,
4191 "B", "Jail persistence");
4192 #ifdef VIMAGE
4193 SYSCTL_JAIL_PARAM(, vnet, CTLTYPE_INT | CTLFLAG_RDTUN,
4194 "E,jailsys", "Virtual network stack");
4195 #endif
4196 SYSCTL_JAIL_PARAM(, dying, CTLTYPE_INT | CTLFLAG_RD,
4197 "B", "Jail is in the process of shutting down");
4198
4199 SYSCTL_JAIL_PARAM_NODE(children, "Number of child jails");
4200 SYSCTL_JAIL_PARAM(_children, cur, CTLTYPE_INT | CTLFLAG_RD,
4201 "I", "Current number of child jails");
4202 SYSCTL_JAIL_PARAM(_children, max, CTLTYPE_INT | CTLFLAG_RW,
4203 "I", "Maximum number of child jails");
4204
4205 SYSCTL_JAIL_PARAM_SYS_NODE(host, CTLFLAG_RW, "Jail host info");
4206 SYSCTL_JAIL_PARAM_STRING(_host, hostname, CTLFLAG_RW, MAXHOSTNAMELEN,
4207 "Jail hostname");
4208 SYSCTL_JAIL_PARAM_STRING(_host, domainname, CTLFLAG_RW, MAXHOSTNAMELEN,
4209 "Jail NIS domainname");
4210 SYSCTL_JAIL_PARAM_STRING(_host, hostuuid, CTLFLAG_RW, HOSTUUIDLEN,
4211 "Jail host UUID");
4212 SYSCTL_JAIL_PARAM(_host, hostid, CTLTYPE_ULONG | CTLFLAG_RW,
4213 "LU", "Jail host ID");
4214
4215 SYSCTL_JAIL_PARAM_NODE(cpuset, "Jail cpuset");
4216 SYSCTL_JAIL_PARAM(_cpuset, id, CTLTYPE_INT | CTLFLAG_RD, "I", "Jail cpuset ID");
4217
4218 #ifdef INET
4219 SYSCTL_JAIL_PARAM_SYS_NODE(ip4, CTLFLAG_RDTUN,
4220 "Jail IPv4 address virtualization");
4221 SYSCTL_JAIL_PARAM_STRUCT(_ip4, addr, CTLFLAG_RW, sizeof(struct in_addr),
4222 "S,in_addr,a", "Jail IPv4 addresses");
4223 SYSCTL_JAIL_PARAM(_ip4, saddrsel, CTLTYPE_INT | CTLFLAG_RW,
4224 "B", "Do (not) use IPv4 source address selection rather than the "
4225 "primary jail IPv4 address.");
4226 #endif
4227 #ifdef INET6
4228 SYSCTL_JAIL_PARAM_SYS_NODE(ip6, CTLFLAG_RDTUN,
4229 "Jail IPv6 address virtualization");
4230 SYSCTL_JAIL_PARAM_STRUCT(_ip6, addr, CTLFLAG_RW, sizeof(struct in6_addr),
4231 "S,in6_addr,a", "Jail IPv6 addresses");
4232 SYSCTL_JAIL_PARAM(_ip6, saddrsel, CTLTYPE_INT | CTLFLAG_RW,
4233 "B", "Do (not) use IPv6 source address selection rather than the "
4234 "primary jail IPv6 address.");
4235 #endif
4236
4237 SYSCTL_JAIL_PARAM_NODE(allow, "Jail permission flags");
4238 SYSCTL_JAIL_PARAM(_allow, set_hostname, CTLTYPE_INT | CTLFLAG_RW,
4239 "B", "Jail may set hostname");
4240 SYSCTL_JAIL_PARAM(_allow, sysvipc, CTLTYPE_INT | CTLFLAG_RW,
4241 "B", "Jail may use SYSV IPC");
4242 SYSCTL_JAIL_PARAM(_allow, raw_sockets, CTLTYPE_INT | CTLFLAG_RW,
4243 "B", "Jail may create raw sockets");
4244 SYSCTL_JAIL_PARAM(_allow, chflags, CTLTYPE_INT | CTLFLAG_RW,
4245 "B", "Jail may alter system file flags");
4246 SYSCTL_JAIL_PARAM(_allow, mount, CTLTYPE_INT | CTLFLAG_RW,
4247 "B", "Jail may mount/unmount jail-friendly file systems");
4248 SYSCTL_JAIL_PARAM(_allow, quotas, CTLTYPE_INT | CTLFLAG_RW,
4249 "B", "Jail may set file quotas");
4250 SYSCTL_JAIL_PARAM(_allow, socket_af, CTLTYPE_INT | CTLFLAG_RW,
4251 "B", "Jail may create sockets other than just UNIX/IPv4/IPv6/route");
4252
4253
4254 #ifdef DDB
4255
4256 static void
4257 db_show_prison(struct prison *pr)
4258 {
4259 int fi;
4260 #if defined(INET) || defined(INET6)
4261 int ii;
4262 #endif
4263 unsigned jsf;
4264 #ifdef INET6
4265 char ip6buf[INET6_ADDRSTRLEN];
4266 #endif
4267
4268 db_printf("prison %p:\n", pr);
4269 db_printf(" jid = %d\n", pr->pr_id);
4270 db_printf(" name = %s\n", pr->pr_name);
4271 db_printf(" parent = %p\n", pr->pr_parent);
4272 db_printf(" ref = %d\n", pr->pr_ref);
4273 db_printf(" uref = %d\n", pr->pr_uref);
4274 db_printf(" path = %s\n", pr->pr_path);
4275 db_printf(" cpuset = %d\n", pr->pr_cpuset
4276 ? pr->pr_cpuset->cs_id : -1);
4277 #ifdef VIMAGE
4278 db_printf(" vnet = %p\n", pr->pr_vnet);
4279 #endif
4280 db_printf(" root = %p\n", pr->pr_root);
4281 db_printf(" securelevel = %d\n", pr->pr_securelevel);
4282 db_printf(" children.max = %d\n", pr->pr_childmax);
4283 db_printf(" children.cur = %d\n", pr->pr_childcount);
4284 db_printf(" child = %p\n", LIST_FIRST(&pr->pr_children));
4285 db_printf(" sibling = %p\n", LIST_NEXT(pr, pr_sibling));
4286 db_printf(" flags = 0x%x", pr->pr_flags);
4287 for (fi = 0; fi < sizeof(pr_flag_names) / sizeof(pr_flag_names[0]);
4288 fi++)
4289 if (pr_flag_names[fi] != NULL && (pr->pr_flags & (1 << fi)))
4290 db_printf(" %s", pr_flag_names[fi]);
4291 for (fi = 0; fi < sizeof(pr_flag_jailsys) / sizeof(pr_flag_jailsys[0]);
4292 fi++) {
4293 jsf = pr->pr_flags &
4294 (pr_flag_jailsys[fi].disable | pr_flag_jailsys[fi].new);
4295 db_printf(" %-16s= %s\n", pr_flag_jailsys[fi].name,
4296 pr_flag_jailsys[fi].disable &&
4297 (jsf == pr_flag_jailsys[fi].disable) ? "disable"
4298 : (jsf == pr_flag_jailsys[fi].new) ? "new"
4299 : "inherit");
4300 }
4301 db_printf(" allow = 0x%x", pr->pr_allow);
4302 for (fi = 0; fi < sizeof(pr_allow_names) / sizeof(pr_allow_names[0]);
4303 fi++)
4304 if (pr_allow_names[fi] != NULL && (pr->pr_allow & (1 << fi)))
4305 db_printf(" %s", pr_allow_names[fi]);
4306 db_printf("\n");
4307 db_printf(" enforce_statfs = %d\n", pr->pr_enforce_statfs);
4308 db_printf(" host.hostname = %s\n", pr->pr_hostname);
4309 db_printf(" host.domainname = %s\n", pr->pr_domainname);
4310 db_printf(" host.hostuuid = %s\n", pr->pr_hostuuid);
4311 db_printf(" host.hostid = %lu\n", pr->pr_hostid);
4312 #ifdef INET
4313 db_printf(" ip4s = %d\n", pr->pr_ip4s);
4314 for (ii = 0; ii < pr->pr_ip4s; ii++)
4315 db_printf(" %s %s\n",
4316 ii == 0 ? "ip4.addr =" : " ",
4317 inet_ntoa(pr->pr_ip4[ii]));
4318 #endif
4319 #ifdef INET6
4320 db_printf(" ip6s = %d\n", pr->pr_ip6s);
4321 for (ii = 0; ii < pr->pr_ip6s; ii++)
4322 db_printf(" %s %s\n",
4323 ii == 0 ? "ip6.addr =" : " ",
4324 ip6_sprintf(ip6buf, &pr->pr_ip6[ii]));
4325 #endif
4326 }
4327
4328 DB_SHOW_COMMAND(prison, db_show_prison_command)
4329 {
4330 struct prison *pr;
4331
4332 if (!have_addr) {
4333 /*
4334 * Show all prisons in the list, and prison0 which is not
4335 * listed.
4336 */
4337 db_show_prison(&prison0);
4338 if (!db_pager_quit) {
4339 TAILQ_FOREACH(pr, &allprison, pr_list) {
4340 db_show_prison(pr);
4341 if (db_pager_quit)
4342 break;
4343 }
4344 }
4345 return;
4346 }
4347
4348 if (addr == 0)
4349 pr = &prison0;
4350 else {
4351 /* Look for a prison with the ID and with references. */
4352 TAILQ_FOREACH(pr, &allprison, pr_list)
4353 if (pr->pr_id == addr && pr->pr_ref > 0)
4354 break;
4355 if (pr == NULL)
4356 /* Look again, without requiring a reference. */
4357 TAILQ_FOREACH(pr, &allprison, pr_list)
4358 if (pr->pr_id == addr)
4359 break;
4360 if (pr == NULL)
4361 /* Assume address points to a valid prison. */
4362 pr = (struct prison *)addr;
4363 }
4364 db_show_prison(pr);
4365 }
4366
4367 #endif /* DDB */
Cache object: cb2f9b1497bc571fee795af35ab5dcd7
|