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