1 /*-
2 * SPDX-License-Identifier: BSD-2-Clause
3 *
4 * Copyright (c) 1999 Poul-Henning Kamp.
5 * Copyright (c) 2008 Bjoern A. Zeeb.
6 * Copyright (c) 2009 James Gritton.
7 * All rights reserved.
8 *
9 * Redistribution and use in source and binary forms, with or without
10 * modification, are permitted provided that the following conditions
11 * are met:
12 * 1. Redistributions of source code must retain the above copyright
13 * notice, this list of conditions and the following disclaimer.
14 * 2. Redistributions in binary form must reproduce the above copyright
15 * notice, this list of conditions and the following disclaimer in the
16 * documentation and/or other materials provided with the distribution.
17 *
18 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
19 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
22 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
23 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
24 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
25 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
26 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
27 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
28 * SUCH DAMAGE.
29 */
30
31 #include <sys/cdefs.h>
32 #include "opt_ddb.h"
33 #include "opt_inet.h"
34 #include "opt_inet6.h"
35 #include "opt_nfs.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/epoch.h>
48 #include <sys/taskqueue.h>
49 #include <sys/fcntl.h>
50 #include <sys/jail.h>
51 #include <sys/linker.h>
52 #include <sys/lock.h>
53 #include <sys/mman.h>
54 #include <sys/mutex.h>
55 #include <sys/racct.h>
56 #include <sys/rctl.h>
57 #include <sys/refcount.h>
58 #include <sys/sx.h>
59 #include <sys/sysent.h>
60 #include <sys/namei.h>
61 #include <sys/mount.h>
62 #include <sys/queue.h>
63 #include <sys/socket.h>
64 #include <sys/syscallsubr.h>
65 #include <sys/sysctl.h>
66 #include <sys/uuid.h>
67 #include <sys/vnode.h>
68
69 #include <net/if.h>
70 #include <net/vnet.h>
71
72 #include <netinet/in.h>
73
74 #ifdef DDB
75 #include <ddb/ddb.h>
76 #endif /* DDB */
77
78 #include <security/mac/mac_framework.h>
79
80 #define PRISON0_HOSTUUID_MODULE "hostuuid"
81
82 MALLOC_DEFINE(M_PRISON, "prison", "Prison structures");
83 static MALLOC_DEFINE(M_PRISON_RACCT, "prison_racct", "Prison racct structures");
84
85 /* Keep struct prison prison0 and some code in kern_jail_set() readable. */
86 #ifdef INET
87 #ifdef INET6
88 #define _PR_IP_SADDRSEL PR_IP4_SADDRSEL|PR_IP6_SADDRSEL
89 #else
90 #define _PR_IP_SADDRSEL PR_IP4_SADDRSEL
91 #endif
92 #else /* !INET */
93 #ifdef INET6
94 #define _PR_IP_SADDRSEL PR_IP6_SADDRSEL
95 #else
96 #define _PR_IP_SADDRSEL 0
97 #endif
98 #endif
99
100 /* prison0 describes what is "real" about the system. */
101 struct prison prison0 = {
102 .pr_id = 0,
103 .pr_name = "0",
104 .pr_ref = 1,
105 .pr_uref = 1,
106 .pr_path = "/",
107 .pr_securelevel = -1,
108 .pr_devfs_rsnum = 0,
109 .pr_state = PRISON_STATE_ALIVE,
110 .pr_childmax = JAIL_MAX,
111 .pr_hostuuid = DEFAULT_HOSTUUID,
112 .pr_children = LIST_HEAD_INITIALIZER(prison0.pr_children),
113 #ifdef VIMAGE
114 .pr_flags = PR_HOST|PR_VNET|_PR_IP_SADDRSEL,
115 #else
116 .pr_flags = PR_HOST|_PR_IP_SADDRSEL,
117 #endif
118 .pr_allow = PR_ALLOW_ALL_STATIC,
119 };
120 MTX_SYSINIT(prison0, &prison0.pr_mtx, "jail mutex", MTX_DEF);
121
122 struct bool_flags {
123 const char *name;
124 const char *noname;
125 volatile u_int flag;
126 };
127 struct jailsys_flags {
128 const char *name;
129 unsigned disable;
130 unsigned new;
131 };
132
133 /* allprison, allprison_racct and lastprid are protected by allprison_lock. */
134 struct sx allprison_lock;
135 SX_SYSINIT(allprison_lock, &allprison_lock, "allprison");
136 struct prisonlist allprison = TAILQ_HEAD_INITIALIZER(allprison);
137 LIST_HEAD(, prison_racct) allprison_racct;
138 int lastprid = 0;
139
140 static int get_next_prid(struct prison **insprp);
141 static int do_jail_attach(struct thread *td, struct prison *pr, int drflags);
142 static void prison_complete(void *context, int pending);
143 static void prison_deref(struct prison *pr, int flags);
144 static void prison_deref_kill(struct prison *pr, struct prisonlist *freeprison);
145 static int prison_lock_xlock(struct prison *pr, int flags);
146 static void prison_cleanup(struct prison *pr);
147 static void prison_free_not_last(struct prison *pr);
148 static void prison_proc_free_not_last(struct prison *pr);
149 static void prison_proc_relink(struct prison *opr, struct prison *npr,
150 struct proc *p);
151 static void prison_set_allow_locked(struct prison *pr, unsigned flag,
152 int enable);
153 static char *prison_path(struct prison *pr1, struct prison *pr2);
154 #ifdef RACCT
155 static void prison_racct_attach(struct prison *pr);
156 static void prison_racct_modify(struct prison *pr);
157 static void prison_racct_detach(struct prison *pr);
158 #endif
159
160 /* Flags for prison_deref */
161 #define PD_DEREF 0x01 /* Decrement pr_ref */
162 #define PD_DEUREF 0x02 /* Decrement pr_uref */
163 #define PD_KILL 0x04 /* Remove jail, kill processes, etc */
164 #define PD_LOCKED 0x10 /* pr_mtx is held */
165 #define PD_LIST_SLOCKED 0x20 /* allprison_lock is held shared */
166 #define PD_LIST_XLOCKED 0x40 /* allprison_lock is held exclusive */
167 #define PD_OP_FLAGS 0x07 /* Operation flags */
168 #define PD_LOCK_FLAGS 0x70 /* Lock status flags */
169
170 /*
171 * Parameter names corresponding to PR_* flag values. Size values are for kvm
172 * as we cannot figure out the size of a sparse array, or an array without a
173 * terminating entry.
174 */
175 static struct bool_flags pr_flag_bool[] = {
176 {"persist", "nopersist", PR_PERSIST},
177 #ifdef INET
178 {"ip4.saddrsel", "ip4.nosaddrsel", PR_IP4_SADDRSEL},
179 #endif
180 #ifdef INET6
181 {"ip6.saddrsel", "ip6.nosaddrsel", PR_IP6_SADDRSEL},
182 #endif
183 };
184 const size_t pr_flag_bool_size = sizeof(pr_flag_bool);
185
186 static struct jailsys_flags pr_flag_jailsys[] = {
187 {"host", 0, PR_HOST},
188 #ifdef VIMAGE
189 {"vnet", 0, PR_VNET},
190 #endif
191 #ifdef INET
192 {"ip4", PR_IP4_USER, PR_IP4_USER},
193 #endif
194 #ifdef INET6
195 {"ip6", PR_IP6_USER, PR_IP6_USER},
196 #endif
197 };
198 const size_t pr_flag_jailsys_size = sizeof(pr_flag_jailsys);
199
200 /*
201 * Make this array full-size so dynamic parameters can be added.
202 * It is protected by prison0.mtx, but lockless reading is allowed
203 * with an atomic check of the flag values.
204 */
205 static struct bool_flags pr_flag_allow[NBBY * NBPW] = {
206 {"allow.set_hostname", "allow.noset_hostname", PR_ALLOW_SET_HOSTNAME},
207 {"allow.sysvipc", "allow.nosysvipc", PR_ALLOW_SYSVIPC},
208 {"allow.raw_sockets", "allow.noraw_sockets", PR_ALLOW_RAW_SOCKETS},
209 {"allow.chflags", "allow.nochflags", PR_ALLOW_CHFLAGS},
210 {"allow.mount", "allow.nomount", PR_ALLOW_MOUNT},
211 {"allow.quotas", "allow.noquotas", PR_ALLOW_QUOTAS},
212 {"allow.socket_af", "allow.nosocket_af", PR_ALLOW_SOCKET_AF},
213 {"allow.mlock", "allow.nomlock", PR_ALLOW_MLOCK},
214 {"allow.reserved_ports", "allow.noreserved_ports",
215 PR_ALLOW_RESERVED_PORTS},
216 {"allow.read_msgbuf", "allow.noread_msgbuf", PR_ALLOW_READ_MSGBUF},
217 {"allow.unprivileged_proc_debug", "allow.nounprivileged_proc_debug",
218 PR_ALLOW_UNPRIV_DEBUG},
219 {"allow.suser", "allow.nosuser", PR_ALLOW_SUSER},
220 #ifdef VIMAGE
221 {"allow.nfsd", "allow.nonfsd", PR_ALLOW_NFSD},
222 #endif
223 };
224 static unsigned pr_allow_all = PR_ALLOW_ALL_STATIC;
225 const size_t pr_flag_allow_size = sizeof(pr_flag_allow);
226
227 #define JAIL_DEFAULT_ALLOW (PR_ALLOW_SET_HOSTNAME | \
228 PR_ALLOW_RESERVED_PORTS | \
229 PR_ALLOW_UNPRIV_DEBUG | \
230 PR_ALLOW_SUSER)
231 #define JAIL_DEFAULT_ENFORCE_STATFS 2
232 #define JAIL_DEFAULT_DEVFS_RSNUM 0
233 static unsigned jail_default_allow = JAIL_DEFAULT_ALLOW;
234 static int jail_default_enforce_statfs = JAIL_DEFAULT_ENFORCE_STATFS;
235 static int jail_default_devfs_rsnum = JAIL_DEFAULT_DEVFS_RSNUM;
236 #if defined(INET) || defined(INET6)
237 static unsigned jail_max_af_ips = 255;
238 #endif
239
240 /*
241 * Initialize the parts of prison0 that can't be static-initialized with
242 * constants. This is called from proc0_init() after creating thread0 cpuset.
243 */
244 void
prison0_init(void)245 prison0_init(void)
246 {
247 uint8_t *file, *data;
248 size_t size;
249 char buf[sizeof(prison0.pr_hostuuid)];
250 bool valid;
251
252 prison0.pr_cpuset = cpuset_ref(thread0.td_cpuset);
253 prison0.pr_osreldate = osreldate;
254 strlcpy(prison0.pr_osrelease, osrelease, sizeof(prison0.pr_osrelease));
255
256 /* If we have a preloaded hostuuid, use it. */
257 file = preload_search_by_type(PRISON0_HOSTUUID_MODULE);
258 if (file != NULL) {
259 data = preload_fetch_addr(file);
260 size = preload_fetch_size(file);
261 if (data != NULL) {
262 /*
263 * The preloaded data may include trailing whitespace, almost
264 * certainly a newline; skip over any whitespace or
265 * non-printable characters to be safe.
266 */
267 while (size > 0 && data[size - 1] <= 0x20) {
268 size--;
269 }
270
271 valid = false;
272
273 /*
274 * Not NUL-terminated when passed from loader, but
275 * validate_uuid requires that due to using sscanf (as
276 * does the subsequent strlcpy, since it still reads
277 * past the given size to return the true length);
278 * bounce to a temporary buffer to fix.
279 */
280 if (size >= sizeof(buf))
281 goto done;
282
283 memcpy(buf, data, size);
284 buf[size] = '\0';
285
286 if (validate_uuid(buf, size, NULL, 0) != 0)
287 goto done;
288
289 valid = true;
290 (void)strlcpy(prison0.pr_hostuuid, buf,
291 sizeof(prison0.pr_hostuuid));
292
293 done:
294 if (bootverbose && !valid) {
295 printf("hostuuid: preload data malformed: '%.*s'\n",
296 (int)size, data);
297 }
298 }
299 }
300 if (bootverbose)
301 printf("hostuuid: using %s\n", prison0.pr_hostuuid);
302 }
303
304 /*
305 * struct jail_args {
306 * struct jail *jail;
307 * };
308 */
309 int
sys_jail(struct thread * td,struct jail_args * uap)310 sys_jail(struct thread *td, struct jail_args *uap)
311 {
312 uint32_t version;
313 int error;
314 struct jail j;
315
316 error = copyin(uap->jail, &version, sizeof(uint32_t));
317 if (error)
318 return (error);
319
320 switch (version) {
321 case 0:
322 {
323 struct jail_v0 j0;
324
325 /* FreeBSD single IPv4 jails. */
326 bzero(&j, sizeof(struct jail));
327 error = copyin(uap->jail, &j0, sizeof(struct jail_v0));
328 if (error)
329 return (error);
330 j.version = j0.version;
331 j.path = j0.path;
332 j.hostname = j0.hostname;
333 j.ip4s = htonl(j0.ip_number); /* jail_v0 is host order */
334 break;
335 }
336
337 case 1:
338 /*
339 * Version 1 was used by multi-IPv4 jail implementations
340 * that never made it into the official kernel.
341 */
342 return (EINVAL);
343
344 case 2: /* JAIL_API_VERSION */
345 /* FreeBSD multi-IPv4/IPv6,noIP jails. */
346 error = copyin(uap->jail, &j, sizeof(struct jail));
347 if (error)
348 return (error);
349 break;
350
351 default:
352 /* Sci-Fi jails are not supported, sorry. */
353 return (EINVAL);
354 }
355 return (kern_jail(td, &j));
356 }
357
358 int
kern_jail(struct thread * td,struct jail * j)359 kern_jail(struct thread *td, struct jail *j)
360 {
361 struct iovec optiov[2 * (4 + nitems(pr_flag_allow)
362 #ifdef INET
363 + 1
364 #endif
365 #ifdef INET6
366 + 1
367 #endif
368 )];
369 struct uio opt;
370 char *u_path, *u_hostname, *u_name;
371 struct bool_flags *bf;
372 #ifdef INET
373 uint32_t ip4s;
374 struct in_addr *u_ip4;
375 #endif
376 #ifdef INET6
377 struct in6_addr *u_ip6;
378 #endif
379 size_t tmplen;
380 int error, enforce_statfs;
381
382 bzero(&optiov, sizeof(optiov));
383 opt.uio_iov = optiov;
384 opt.uio_iovcnt = 0;
385 opt.uio_offset = -1;
386 opt.uio_resid = -1;
387 opt.uio_segflg = UIO_SYSSPACE;
388 opt.uio_rw = UIO_READ;
389 opt.uio_td = td;
390
391 /* Set permissions for top-level jails from sysctls. */
392 if (!jailed(td->td_ucred)) {
393 for (bf = pr_flag_allow;
394 bf < pr_flag_allow + nitems(pr_flag_allow) &&
395 atomic_load_int(&bf->flag) != 0;
396 bf++) {
397 optiov[opt.uio_iovcnt].iov_base = __DECONST(char *,
398 (jail_default_allow & bf->flag)
399 ? bf->name : bf->noname);
400 optiov[opt.uio_iovcnt].iov_len =
401 strlen(optiov[opt.uio_iovcnt].iov_base) + 1;
402 opt.uio_iovcnt += 2;
403 }
404 optiov[opt.uio_iovcnt].iov_base = "enforce_statfs";
405 optiov[opt.uio_iovcnt].iov_len = sizeof("enforce_statfs");
406 opt.uio_iovcnt++;
407 enforce_statfs = jail_default_enforce_statfs;
408 optiov[opt.uio_iovcnt].iov_base = &enforce_statfs;
409 optiov[opt.uio_iovcnt].iov_len = sizeof(enforce_statfs);
410 opt.uio_iovcnt++;
411 }
412
413 tmplen = MAXPATHLEN + MAXHOSTNAMELEN + MAXHOSTNAMELEN;
414 #ifdef INET
415 ip4s = (j->version == 0) ? 1 : j->ip4s;
416 if (ip4s > jail_max_af_ips)
417 return (EINVAL);
418 tmplen += ip4s * sizeof(struct in_addr);
419 #else
420 if (j->ip4s > 0)
421 return (EINVAL);
422 #endif
423 #ifdef INET6
424 if (j->ip6s > jail_max_af_ips)
425 return (EINVAL);
426 tmplen += j->ip6s * sizeof(struct in6_addr);
427 #else
428 if (j->ip6s > 0)
429 return (EINVAL);
430 #endif
431 u_path = malloc(tmplen, M_TEMP, M_WAITOK);
432 u_hostname = u_path + MAXPATHLEN;
433 u_name = u_hostname + MAXHOSTNAMELEN;
434 #ifdef INET
435 u_ip4 = (struct in_addr *)(u_name + MAXHOSTNAMELEN);
436 #endif
437 #ifdef INET6
438 #ifdef INET
439 u_ip6 = (struct in6_addr *)(u_ip4 + ip4s);
440 #else
441 u_ip6 = (struct in6_addr *)(u_name + MAXHOSTNAMELEN);
442 #endif
443 #endif
444 optiov[opt.uio_iovcnt].iov_base = "path";
445 optiov[opt.uio_iovcnt].iov_len = sizeof("path");
446 opt.uio_iovcnt++;
447 optiov[opt.uio_iovcnt].iov_base = u_path;
448 error = copyinstr(j->path, u_path, MAXPATHLEN,
449 &optiov[opt.uio_iovcnt].iov_len);
450 if (error) {
451 free(u_path, M_TEMP);
452 return (error);
453 }
454 opt.uio_iovcnt++;
455 optiov[opt.uio_iovcnt].iov_base = "host.hostname";
456 optiov[opt.uio_iovcnt].iov_len = sizeof("host.hostname");
457 opt.uio_iovcnt++;
458 optiov[opt.uio_iovcnt].iov_base = u_hostname;
459 error = copyinstr(j->hostname, u_hostname, MAXHOSTNAMELEN,
460 &optiov[opt.uio_iovcnt].iov_len);
461 if (error) {
462 free(u_path, M_TEMP);
463 return (error);
464 }
465 opt.uio_iovcnt++;
466 if (j->jailname != NULL) {
467 optiov[opt.uio_iovcnt].iov_base = "name";
468 optiov[opt.uio_iovcnt].iov_len = sizeof("name");
469 opt.uio_iovcnt++;
470 optiov[opt.uio_iovcnt].iov_base = u_name;
471 error = copyinstr(j->jailname, u_name, MAXHOSTNAMELEN,
472 &optiov[opt.uio_iovcnt].iov_len);
473 if (error) {
474 free(u_path, M_TEMP);
475 return (error);
476 }
477 opt.uio_iovcnt++;
478 }
479 #ifdef INET
480 optiov[opt.uio_iovcnt].iov_base = "ip4.addr";
481 optiov[opt.uio_iovcnt].iov_len = sizeof("ip4.addr");
482 opt.uio_iovcnt++;
483 optiov[opt.uio_iovcnt].iov_base = u_ip4;
484 optiov[opt.uio_iovcnt].iov_len = ip4s * sizeof(struct in_addr);
485 if (j->version == 0)
486 u_ip4->s_addr = j->ip4s;
487 else {
488 error = copyin(j->ip4, u_ip4, optiov[opt.uio_iovcnt].iov_len);
489 if (error) {
490 free(u_path, M_TEMP);
491 return (error);
492 }
493 }
494 opt.uio_iovcnt++;
495 #endif
496 #ifdef INET6
497 optiov[opt.uio_iovcnt].iov_base = "ip6.addr";
498 optiov[opt.uio_iovcnt].iov_len = sizeof("ip6.addr");
499 opt.uio_iovcnt++;
500 optiov[opt.uio_iovcnt].iov_base = u_ip6;
501 optiov[opt.uio_iovcnt].iov_len = j->ip6s * sizeof(struct in6_addr);
502 error = copyin(j->ip6, u_ip6, optiov[opt.uio_iovcnt].iov_len);
503 if (error) {
504 free(u_path, M_TEMP);
505 return (error);
506 }
507 opt.uio_iovcnt++;
508 #endif
509 KASSERT(opt.uio_iovcnt <= nitems(optiov),
510 ("kern_jail: too many iovecs (%d)", opt.uio_iovcnt));
511 error = kern_jail_set(td, &opt, JAIL_CREATE | JAIL_ATTACH);
512 free(u_path, M_TEMP);
513 return (error);
514 }
515
516 /*
517 * struct jail_set_args {
518 * struct iovec *iovp;
519 * unsigned int iovcnt;
520 * int flags;
521 * };
522 */
523 int
sys_jail_set(struct thread * td,struct jail_set_args * uap)524 sys_jail_set(struct thread *td, struct jail_set_args *uap)
525 {
526 struct uio *auio;
527 int error;
528
529 /* Check that we have an even number of iovecs. */
530 if (uap->iovcnt & 1)
531 return (EINVAL);
532
533 error = copyinuio(uap->iovp, uap->iovcnt, &auio);
534 if (error)
535 return (error);
536 error = kern_jail_set(td, auio, uap->flags);
537 freeuio(auio);
538 return (error);
539 }
540
541 #if defined(INET) || defined(INET6)
542 typedef int prison_addr_cmp_t(const void *, const void *);
543 typedef bool prison_addr_valid_t(const void *);
544 static const struct pr_family {
545 size_t size;
546 prison_addr_cmp_t *cmp;
547 prison_addr_valid_t *valid;
548 int ip_flag;
549 } pr_families[PR_FAMILY_MAX] = {
550 #ifdef INET
551 [PR_INET] = {
552 .size = sizeof(struct in_addr),
553 .cmp = prison_qcmp_v4,
554 .valid = prison_valid_v4,
555 .ip_flag = PR_IP4_USER,
556 },
557 #endif
558 #ifdef INET6
559 [PR_INET6] = {
560 .size = sizeof(struct in6_addr),
561 .cmp = prison_qcmp_v6,
562 .valid = prison_valid_v6,
563 .ip_flag = PR_IP6_USER,
564 },
565 #endif
566 };
567
568 /*
569 * Network address lists (pr_addrs) allocation for jails. The addresses
570 * are accessed locklessly by the network stack, thus need to be protected by
571 * the network epoch.
572 */
573 struct prison_ip {
574 struct epoch_context ctx;
575 uint32_t ips;
576 #ifdef FUTURE_C
577 /*
578 * XXX Variable-length automatic arrays in union may be
579 * supported in future C.
580 */
581 union {
582 char pr_ip[];
583 struct in_addr pr_ip4[];
584 struct in6_addr pr_ip6[];
585 };
586 #else /* No future C :( */
587 char pr_ip[];
588 #endif
589 };
590
591 static char *
PR_IP(struct prison_ip * pip,const pr_family_t af,int idx)592 PR_IP(struct prison_ip *pip, const pr_family_t af, int idx)
593 {
594 MPASS(pip);
595 MPASS(af < PR_FAMILY_MAX);
596 MPASS(idx >= 0 && idx < pip->ips);
597
598 return (pip->pr_ip + pr_families[af].size * idx);
599 }
600
601 static struct prison_ip *
prison_ip_alloc(const pr_family_t af,uint32_t cnt,int flags)602 prison_ip_alloc(const pr_family_t af, uint32_t cnt, int flags)
603 {
604 struct prison_ip *pip;
605
606 pip = malloc(sizeof(struct prison_ip) + cnt * pr_families[af].size,
607 M_PRISON, flags);
608 if (pip != NULL)
609 pip->ips = cnt;
610 return (pip);
611 }
612
613 /*
614 * Allocate and copyin user supplied address list, sorting and validating.
615 * kern_jail_set() helper.
616 */
617 static struct prison_ip *
prison_ip_copyin(const pr_family_t af,void * op,uint32_t cnt)618 prison_ip_copyin(const pr_family_t af, void *op, uint32_t cnt)
619 {
620 prison_addr_cmp_t *const cmp = pr_families[af].cmp;
621 const size_t size = pr_families[af].size;
622 struct prison_ip *pip;
623
624 pip = prison_ip_alloc(af, cnt, M_WAITOK);
625 bcopy(op, pip->pr_ip, cnt * size);
626 /*
627 * IP addresses are all sorted but ip[0] to preserve
628 * the primary IP address as given from userland.
629 * This special IP is used for unbound outgoing
630 * connections as well for "loopback" traffic in case
631 * source address selection cannot find any more fitting
632 * address to connect from.
633 */
634 if (cnt > 1)
635 qsort(PR_IP(pip, af, 1), cnt - 1, size, cmp);
636 /*
637 * Check for duplicate addresses and do some simple
638 * zero and broadcast checks. If users give other bogus
639 * addresses it is their problem.
640 */
641 for (int i = 0; i < cnt; i++) {
642 if (!pr_families[af].valid(PR_IP(pip, af, i))) {
643 free(pip, M_PRISON);
644 return (NULL);
645 }
646 if (i + 1 < cnt &&
647 (cmp(PR_IP(pip, af, 0), PR_IP(pip, af, i + 1)) == 0 ||
648 cmp(PR_IP(pip, af, i), PR_IP(pip, af, i + 1)) == 0)) {
649 free(pip, M_PRISON);
650 return (NULL);
651 }
652 }
653
654 return (pip);
655 }
656
657 /*
658 * Allocate and dup parent prison address list.
659 * kern_jail_set() helper.
660 */
661 static void
prison_ip_dup(struct prison * ppr,struct prison * pr,const pr_family_t af)662 prison_ip_dup(struct prison *ppr, struct prison *pr, const pr_family_t af)
663 {
664 const struct prison_ip *ppip = ppr->pr_addrs[af];
665 struct prison_ip *pip;
666
667 if (ppip != NULL) {
668 pip = prison_ip_alloc(af, ppip->ips, M_WAITOK);
669 bcopy(ppip->pr_ip, pip->pr_ip, pip->ips * pr_families[af].size);
670 pr->pr_addrs[af] = pip;
671 }
672 }
673
674 /*
675 * Make sure the new set of IP addresses is a subset of the parent's list.
676 * Don't worry about the parent being unlocked, as any setting is done with
677 * allprison_lock held.
678 * kern_jail_set() helper.
679 */
680 static bool
prison_ip_parent_match(struct prison_ip * ppip,struct prison_ip * pip,const pr_family_t af)681 prison_ip_parent_match(struct prison_ip *ppip, struct prison_ip *pip,
682 const pr_family_t af)
683 {
684 prison_addr_cmp_t *const cmp = pr_families[af].cmp;
685 int i, j;
686
687 if (ppip == NULL)
688 return (false);
689
690 for (i = 0; i < ppip->ips; i++)
691 if (cmp(PR_IP(pip, af, 0), PR_IP(ppip, af, i)) == 0)
692 break;
693
694 if (i == ppip->ips)
695 /* Main address not present in parent. */
696 return (false);
697
698 if (pip->ips > 1) {
699 for (i = j = 1; i < pip->ips; i++) {
700 if (cmp(PR_IP(pip, af, i), PR_IP(ppip, af, 0)) == 0)
701 /* Equals to parent primary address. */
702 continue;
703 for (; j < ppip->ips; j++)
704 if (cmp(PR_IP(pip, af, i),
705 PR_IP(ppip, af, j)) == 0)
706 break;
707 if (j == ppip->ips)
708 break;
709 }
710 if (j == ppip->ips)
711 /* Address not present in parent. */
712 return (false);
713 }
714 return (true);
715 }
716
717 /*
718 * Check for conflicting IP addresses. We permit them if there is no more
719 * than one IP on each jail. If there is a duplicate on a jail with more
720 * than one IP stop checking and return error.
721 * kern_jail_set() helper.
722 */
723 static bool
prison_ip_conflict_check(const struct prison * ppr,const struct prison * pr,struct prison_ip * pip,pr_family_t af)724 prison_ip_conflict_check(const struct prison *ppr, const struct prison *pr,
725 struct prison_ip *pip, pr_family_t af)
726 {
727 const struct prison *tppr, *tpr;
728 int descend;
729
730 #ifdef VIMAGE
731 for (tppr = ppr; tppr != &prison0; tppr = tppr->pr_parent)
732 if (tppr->pr_flags & PR_VNET)
733 break;
734 #else
735 tppr = &prison0;
736 #endif
737 FOREACH_PRISON_DESCENDANT(tppr, tpr, descend) {
738 if (tpr == pr ||
739 #ifdef VIMAGE
740 (tpr != tppr && (tpr->pr_flags & PR_VNET)) ||
741 #endif
742 !prison_isalive(tpr)) {
743 descend = 0;
744 continue;
745 }
746 if (!(tpr->pr_flags & pr_families[af].ip_flag))
747 continue;
748 descend = 0;
749 if (tpr->pr_addrs[af] == NULL ||
750 (pip->ips == 1 && tpr->pr_addrs[af]->ips == 1))
751 continue;
752 for (int i = 0; i < pip->ips; i++)
753 if (prison_ip_check(tpr, af, PR_IP(pip, af, i)) == 0)
754 return (false);
755 }
756
757 return (true);
758 }
759
760 _Static_assert(offsetof(struct prison_ip, ctx) == 0,
761 "prison must start with epoch context");
762 static void
prison_ip_free_deferred(epoch_context_t ctx)763 prison_ip_free_deferred(epoch_context_t ctx)
764 {
765
766 free(ctx, M_PRISON);
767 }
768
769 static void
prison_ip_free(struct prison_ip * pip)770 prison_ip_free(struct prison_ip *pip)
771 {
772
773 if (pip != NULL)
774 NET_EPOCH_CALL(prison_ip_free_deferred, &pip->ctx);
775 }
776
777 static void
prison_ip_set(struct prison * pr,const pr_family_t af,struct prison_ip * new)778 prison_ip_set(struct prison *pr, const pr_family_t af, struct prison_ip *new)
779 {
780 struct prison_ip **mem, *old;
781
782 mtx_assert(&pr->pr_mtx, MA_OWNED);
783
784 mem = &pr->pr_addrs[af];
785
786 old = *mem;
787 atomic_store_ptr(mem, new);
788 prison_ip_free(old);
789 }
790
791 /*
792 * Restrict a prison's IP address list with its parent's, possibly replacing
793 * it. Return true if succeed, otherwise should redo.
794 * kern_jail_set() helper.
795 */
796 static bool
prison_ip_restrict(struct prison * pr,const pr_family_t af,struct prison_ip ** newp)797 prison_ip_restrict(struct prison *pr, const pr_family_t af,
798 struct prison_ip **newp)
799 {
800 struct prison_ip *ppip = pr->pr_parent->pr_addrs[af];
801 struct prison_ip *pip = pr->pr_addrs[af];
802 int (*const cmp)(const void *, const void *) = pr_families[af].cmp;
803 const size_t size = pr_families[af].size;
804 struct prison_ip *new = newp != NULL ? *newp : NULL;
805 uint32_t ips;
806
807 mtx_assert(&pr->pr_mtx, MA_OWNED);
808
809 /*
810 * Due to epoch-synchronized access to the IP address lists we always
811 * allocate a new list even if the old one has enough space. We could
812 * atomically update an IPv4 address inside a list, but that would
813 * screw up sorting, and in case of IPv6 we can't even atomically write
814 * one.
815 */
816 if (ppip == NULL) {
817 if (pip != NULL)
818 prison_ip_set(pr, af, NULL);
819 return (true);
820 }
821
822 if (!(pr->pr_flags & pr_families[af].ip_flag)) {
823 if (new == NULL) {
824 new = prison_ip_alloc(af, ppip->ips, M_NOWAIT);
825 if (new == NULL)
826 return (false); /* Redo */
827 }
828 /* This has no user settings, so just copy the parent's list. */
829 MPASS(new->ips == ppip->ips);
830 bcopy(ppip->pr_ip, new->pr_ip, ppip->ips * size);
831 prison_ip_set(pr, af, new);
832 if (newp != NULL)
833 *newp = NULL; /* Used */
834 } else if (pip != NULL) {
835 /* Remove addresses that aren't in the parent. */
836 int i;
837
838 i = 0; /* index in pip */
839 ips = 0; /* index in new */
840
841 if (new == NULL) {
842 new = prison_ip_alloc(af, pip->ips, M_NOWAIT);
843 if (new == NULL)
844 return (false); /* Redo */
845 }
846
847 for (int pi = 0; pi < ppip->ips; pi++)
848 if (cmp(PR_IP(pip, af, 0), PR_IP(ppip, af, pi)) == 0) {
849 /* Found our primary address in parent. */
850 bcopy(PR_IP(pip, af, i), PR_IP(new, af, ips),
851 size);
852 i++;
853 ips++;
854 break;
855 }
856 for (int pi = 1; i < pip->ips; ) {
857 /* Check against primary, which is unsorted. */
858 if (cmp(PR_IP(pip, af, i), PR_IP(ppip, af, 0)) == 0) {
859 /* Matches parent's primary address. */
860 bcopy(PR_IP(pip, af, i), PR_IP(new, af, ips),
861 size);
862 i++;
863 ips++;
864 continue;
865 }
866 /* The rest are sorted. */
867 switch (pi >= ppip->ips ? -1 :
868 cmp(PR_IP(pip, af, i), PR_IP(ppip, af, pi))) {
869 case -1:
870 i++;
871 break;
872 case 0:
873 bcopy(PR_IP(pip, af, i), PR_IP(new, af, ips),
874 size);
875 i++;
876 pi++;
877 ips++;
878 break;
879 case 1:
880 pi++;
881 break;
882 }
883 }
884 if (ips == 0) {
885 if (newp == NULL || *newp == NULL)
886 prison_ip_free(new);
887 new = NULL;
888 } else {
889 /* Shrink to real size */
890 KASSERT((new->ips >= ips),
891 ("Out-of-bounds write to prison_ip %p", new));
892 new->ips = ips;
893 }
894 prison_ip_set(pr, af, new);
895 if (newp != NULL)
896 *newp = NULL; /* Used */
897 }
898 return (true);
899 }
900
901 /*
902 * Fast-path check if an address belongs to a prison.
903 */
904 int
prison_ip_check(const struct prison * pr,const pr_family_t af,const void * addr)905 prison_ip_check(const struct prison *pr, const pr_family_t af,
906 const void *addr)
907 {
908 int (*const cmp)(const void *, const void *) = pr_families[af].cmp;
909 struct prison_ip *pip;
910 int i, a, z, d;
911
912 MPASS(mtx_owned(&pr->pr_mtx) ||
913 in_epoch(net_epoch_preempt) ||
914 sx_xlocked(&allprison_lock));
915
916 pip = atomic_load_ptr(&pr->pr_addrs[af]);
917 if (__predict_false(pip == NULL))
918 return (EAFNOSUPPORT);
919
920 /* Check the primary IP. */
921 if (cmp(PR_IP(pip, af, 0), addr) == 0)
922 return (0);
923
924 /*
925 * All the other IPs are sorted so we can do a binary search.
926 */
927 a = 0;
928 z = pip->ips - 2;
929 while (a <= z) {
930 i = (a + z) / 2;
931 d = cmp(PR_IP(pip, af, i + 1), addr);
932 if (d > 0)
933 z = i - 1;
934 else if (d < 0)
935 a = i + 1;
936 else
937 return (0);
938 }
939
940 return (EADDRNOTAVAIL);
941 }
942
943 /*
944 * Grab primary IP. Historically required mutex, but nothing prevents
945 * us to support epoch-protected access. Is it used in fast path?
946 * in{6}_jail.c helper
947 */
948 const void *
prison_ip_get0(const struct prison * pr,const pr_family_t af)949 prison_ip_get0(const struct prison *pr, const pr_family_t af)
950 {
951 const struct prison_ip *pip = pr->pr_addrs[af];
952
953 mtx_assert(&pr->pr_mtx, MA_OWNED);
954 MPASS(pip);
955
956 return (pip->pr_ip);
957 }
958
959 u_int
prison_ip_cnt(const struct prison * pr,const pr_family_t af)960 prison_ip_cnt(const struct prison *pr, const pr_family_t af)
961 {
962
963 return (pr->pr_addrs[af]->ips);
964 }
965 #endif /* defined(INET) || defined(INET6) */
966
967 int
kern_jail_set(struct thread * td,struct uio * optuio,int flags)968 kern_jail_set(struct thread *td, struct uio *optuio, int flags)
969 {
970 struct nameidata nd;
971 #ifdef INET
972 struct prison_ip *ip4;
973 #endif
974 #ifdef INET6
975 struct prison_ip *ip6;
976 #endif
977 struct vfsopt *opt;
978 struct vfsoptlist *opts;
979 struct prison *pr, *deadpr, *inspr, *mypr, *ppr, *tpr;
980 struct vnode *root;
981 char *domain, *errmsg, *host, *name, *namelc, *p, *path, *uuid;
982 char *g_path, *osrelstr;
983 struct bool_flags *bf;
984 struct jailsys_flags *jsf;
985 #if defined(INET) || defined(INET6)
986 void *op;
987 #endif
988 unsigned long hid;
989 size_t namelen, onamelen, pnamelen;
990 int born, created, cuflags, descend, drflags, enforce;
991 int error, errmsg_len, errmsg_pos;
992 int gotchildmax, gotenforce, gothid, gotrsnum, gotslevel;
993 int jid, jsys, len, level;
994 int childmax, osreldt, rsnum, slevel;
995 #ifdef INET
996 int ip4s;
997 bool redo_ip4;
998 #endif
999 #ifdef INET6
1000 int ip6s;
1001 bool redo_ip6;
1002 #endif
1003 uint64_t pr_allow, ch_allow, pr_flags, ch_flags;
1004 uint64_t pr_allow_diff;
1005 unsigned tallow;
1006 char numbuf[12];
1007
1008 error = priv_check(td, PRIV_JAIL_SET);
1009 if (!error && (flags & JAIL_ATTACH))
1010 error = priv_check(td, PRIV_JAIL_ATTACH);
1011 if (error)
1012 return (error);
1013 mypr = td->td_ucred->cr_prison;
1014 if ((flags & JAIL_CREATE) && mypr->pr_childmax == 0)
1015 return (EPERM);
1016 if (flags & ~JAIL_SET_MASK)
1017 return (EINVAL);
1018
1019 /*
1020 * Check all the parameters before committing to anything. Not all
1021 * errors can be caught early, but we may as well try. Also, this
1022 * takes care of some expensive stuff (path lookup) before getting
1023 * the allprison lock.
1024 *
1025 * XXX Jails are not filesystems, and jail parameters are not mount
1026 * options. But it makes more sense to re-use the vfsopt code
1027 * than duplicate it under a different name.
1028 */
1029 error = vfs_buildopts(optuio, &opts);
1030 if (error)
1031 return (error);
1032 #ifdef INET
1033 ip4 = NULL;
1034 #endif
1035 #ifdef INET6
1036 ip6 = NULL;
1037 #endif
1038 g_path = NULL;
1039
1040 cuflags = flags & (JAIL_CREATE | JAIL_UPDATE);
1041 if (!cuflags) {
1042 error = EINVAL;
1043 vfs_opterror(opts, "no valid operation (create or update)");
1044 goto done_errmsg;
1045 }
1046
1047 error = vfs_copyopt(opts, "jid", &jid, sizeof(jid));
1048 if (error == ENOENT)
1049 jid = 0;
1050 else if (error != 0)
1051 goto done_free;
1052
1053 error = vfs_copyopt(opts, "securelevel", &slevel, sizeof(slevel));
1054 if (error == ENOENT)
1055 gotslevel = 0;
1056 else if (error != 0)
1057 goto done_free;
1058 else
1059 gotslevel = 1;
1060
1061 error =
1062 vfs_copyopt(opts, "children.max", &childmax, sizeof(childmax));
1063 if (error == ENOENT)
1064 gotchildmax = 0;
1065 else if (error != 0)
1066 goto done_free;
1067 else
1068 gotchildmax = 1;
1069
1070 error = vfs_copyopt(opts, "enforce_statfs", &enforce, sizeof(enforce));
1071 if (error == ENOENT)
1072 gotenforce = 0;
1073 else if (error != 0)
1074 goto done_free;
1075 else if (enforce < 0 || enforce > 2) {
1076 error = EINVAL;
1077 goto done_free;
1078 } else
1079 gotenforce = 1;
1080
1081 error = vfs_copyopt(opts, "devfs_ruleset", &rsnum, sizeof(rsnum));
1082 if (error == ENOENT)
1083 gotrsnum = 0;
1084 else if (error != 0)
1085 goto done_free;
1086 else
1087 gotrsnum = 1;
1088
1089 pr_flags = ch_flags = 0;
1090 for (bf = pr_flag_bool;
1091 bf < pr_flag_bool + nitems(pr_flag_bool);
1092 bf++) {
1093 vfs_flagopt(opts, bf->name, &pr_flags, bf->flag);
1094 vfs_flagopt(opts, bf->noname, &ch_flags, bf->flag);
1095 }
1096 ch_flags |= pr_flags;
1097 for (jsf = pr_flag_jailsys;
1098 jsf < pr_flag_jailsys + nitems(pr_flag_jailsys);
1099 jsf++) {
1100 error = vfs_copyopt(opts, jsf->name, &jsys, sizeof(jsys));
1101 if (error == ENOENT)
1102 continue;
1103 if (error != 0)
1104 goto done_free;
1105 switch (jsys) {
1106 case JAIL_SYS_DISABLE:
1107 if (!jsf->disable) {
1108 error = EINVAL;
1109 goto done_free;
1110 }
1111 pr_flags |= jsf->disable;
1112 break;
1113 case JAIL_SYS_NEW:
1114 pr_flags |= jsf->new;
1115 break;
1116 case JAIL_SYS_INHERIT:
1117 break;
1118 default:
1119 error = EINVAL;
1120 goto done_free;
1121 }
1122 ch_flags |= jsf->new | jsf->disable;
1123 }
1124 if ((flags & (JAIL_CREATE | JAIL_ATTACH)) == JAIL_CREATE
1125 && !(pr_flags & PR_PERSIST)) {
1126 error = EINVAL;
1127 vfs_opterror(opts, "new jail must persist or attach");
1128 goto done_errmsg;
1129 }
1130 #ifdef VIMAGE
1131 if ((flags & JAIL_UPDATE) && (ch_flags & PR_VNET)) {
1132 error = EINVAL;
1133 vfs_opterror(opts, "vnet cannot be changed after creation");
1134 goto done_errmsg;
1135 }
1136 #endif
1137 #ifdef INET
1138 if ((flags & JAIL_UPDATE) && (ch_flags & PR_IP4_USER)) {
1139 error = EINVAL;
1140 vfs_opterror(opts, "ip4 cannot be changed after creation");
1141 goto done_errmsg;
1142 }
1143 #endif
1144 #ifdef INET6
1145 if ((flags & JAIL_UPDATE) && (ch_flags & PR_IP6_USER)) {
1146 error = EINVAL;
1147 vfs_opterror(opts, "ip6 cannot be changed after creation");
1148 goto done_errmsg;
1149 }
1150 #endif
1151
1152 pr_allow = ch_allow = 0;
1153 for (bf = pr_flag_allow;
1154 bf < pr_flag_allow + nitems(pr_flag_allow) &&
1155 atomic_load_int(&bf->flag) != 0;
1156 bf++) {
1157 vfs_flagopt(opts, bf->name, &pr_allow, bf->flag);
1158 vfs_flagopt(opts, bf->noname, &ch_allow, bf->flag);
1159 }
1160 ch_allow |= pr_allow;
1161
1162 error = vfs_getopt(opts, "name", (void **)&name, &len);
1163 if (error == ENOENT)
1164 name = NULL;
1165 else if (error != 0)
1166 goto done_free;
1167 else {
1168 if (len == 0 || name[len - 1] != '\0') {
1169 error = EINVAL;
1170 goto done_free;
1171 }
1172 if (len > MAXHOSTNAMELEN) {
1173 error = ENAMETOOLONG;
1174 goto done_free;
1175 }
1176 }
1177
1178 error = vfs_getopt(opts, "host.hostname", (void **)&host, &len);
1179 if (error == ENOENT)
1180 host = NULL;
1181 else if (error != 0)
1182 goto done_free;
1183 else {
1184 ch_flags |= PR_HOST;
1185 pr_flags |= PR_HOST;
1186 if (len == 0 || host[len - 1] != '\0') {
1187 error = EINVAL;
1188 goto done_free;
1189 }
1190 if (len > MAXHOSTNAMELEN) {
1191 error = ENAMETOOLONG;
1192 goto done_free;
1193 }
1194 }
1195
1196 error = vfs_getopt(opts, "host.domainname", (void **)&domain, &len);
1197 if (error == ENOENT)
1198 domain = NULL;
1199 else if (error != 0)
1200 goto done_free;
1201 else {
1202 ch_flags |= PR_HOST;
1203 pr_flags |= PR_HOST;
1204 if (len == 0 || domain[len - 1] != '\0') {
1205 error = EINVAL;
1206 goto done_free;
1207 }
1208 if (len > MAXHOSTNAMELEN) {
1209 error = ENAMETOOLONG;
1210 goto done_free;
1211 }
1212 }
1213
1214 error = vfs_getopt(opts, "host.hostuuid", (void **)&uuid, &len);
1215 if (error == ENOENT)
1216 uuid = NULL;
1217 else if (error != 0)
1218 goto done_free;
1219 else {
1220 ch_flags |= PR_HOST;
1221 pr_flags |= PR_HOST;
1222 if (len == 0 || uuid[len - 1] != '\0') {
1223 error = EINVAL;
1224 goto done_free;
1225 }
1226 if (len > HOSTUUIDLEN) {
1227 error = ENAMETOOLONG;
1228 goto done_free;
1229 }
1230 }
1231
1232 #ifdef COMPAT_FREEBSD32
1233 if (SV_PROC_FLAG(td->td_proc, SV_ILP32)) {
1234 uint32_t hid32;
1235
1236 error = vfs_copyopt(opts, "host.hostid", &hid32, sizeof(hid32));
1237 hid = hid32;
1238 } else
1239 #endif
1240 error = vfs_copyopt(opts, "host.hostid", &hid, sizeof(hid));
1241 if (error == ENOENT)
1242 gothid = 0;
1243 else if (error != 0)
1244 goto done_free;
1245 else {
1246 gothid = 1;
1247 ch_flags |= PR_HOST;
1248 pr_flags |= PR_HOST;
1249 }
1250
1251 #ifdef INET
1252 error = vfs_getopt(opts, "ip4.addr", &op, &ip4s);
1253 if (error == ENOENT)
1254 ip4s = 0;
1255 else if (error != 0)
1256 goto done_free;
1257 else if (ip4s & (sizeof(struct in_addr) - 1)) {
1258 error = EINVAL;
1259 goto done_free;
1260 } else {
1261 ch_flags |= PR_IP4_USER;
1262 pr_flags |= PR_IP4_USER;
1263 if (ip4s > 0) {
1264 ip4s /= sizeof(struct in_addr);
1265 if (ip4s > jail_max_af_ips) {
1266 error = EINVAL;
1267 vfs_opterror(opts, "too many IPv4 addresses");
1268 goto done_errmsg;
1269 }
1270 ip4 = prison_ip_copyin(PR_INET, op, ip4s);
1271 if (ip4 == NULL) {
1272 error = EINVAL;
1273 goto done_free;
1274 }
1275 }
1276 }
1277 #endif
1278
1279 #ifdef INET6
1280 error = vfs_getopt(opts, "ip6.addr", &op, &ip6s);
1281 if (error == ENOENT)
1282 ip6s = 0;
1283 else if (error != 0)
1284 goto done_free;
1285 else if (ip6s & (sizeof(struct in6_addr) - 1)) {
1286 error = EINVAL;
1287 goto done_free;
1288 } else {
1289 ch_flags |= PR_IP6_USER;
1290 pr_flags |= PR_IP6_USER;
1291 if (ip6s > 0) {
1292 ip6s /= sizeof(struct in6_addr);
1293 if (ip6s > jail_max_af_ips) {
1294 error = EINVAL;
1295 vfs_opterror(opts, "too many IPv6 addresses");
1296 goto done_errmsg;
1297 }
1298 ip6 = prison_ip_copyin(PR_INET6, op, ip6s);
1299 if (ip6 == NULL) {
1300 error = EINVAL;
1301 goto done_free;
1302 }
1303 }
1304 }
1305 #endif
1306
1307 #if defined(VIMAGE) && (defined(INET) || defined(INET6))
1308 if ((ch_flags & PR_VNET) && (ch_flags & (PR_IP4_USER | PR_IP6_USER))) {
1309 error = EINVAL;
1310 vfs_opterror(opts,
1311 "vnet jails cannot have IP address restrictions");
1312 goto done_errmsg;
1313 }
1314 #endif
1315
1316 error = vfs_getopt(opts, "osrelease", (void **)&osrelstr, &len);
1317 if (error == ENOENT)
1318 osrelstr = NULL;
1319 else if (error != 0)
1320 goto done_free;
1321 else {
1322 if (flags & JAIL_UPDATE) {
1323 error = EINVAL;
1324 vfs_opterror(opts,
1325 "osrelease cannot be changed after creation");
1326 goto done_errmsg;
1327 }
1328 if (len == 0 || osrelstr[len - 1] != '\0') {
1329 error = EINVAL;
1330 goto done_free;
1331 }
1332 if (len >= OSRELEASELEN) {
1333 error = ENAMETOOLONG;
1334 vfs_opterror(opts,
1335 "osrelease string must be 1-%d bytes long",
1336 OSRELEASELEN - 1);
1337 goto done_errmsg;
1338 }
1339 }
1340
1341 error = vfs_copyopt(opts, "osreldate", &osreldt, sizeof(osreldt));
1342 if (error == ENOENT)
1343 osreldt = 0;
1344 else if (error != 0)
1345 goto done_free;
1346 else {
1347 if (flags & JAIL_UPDATE) {
1348 error = EINVAL;
1349 vfs_opterror(opts,
1350 "osreldate cannot be changed after creation");
1351 goto done_errmsg;
1352 }
1353 if (osreldt == 0) {
1354 error = EINVAL;
1355 vfs_opterror(opts, "osreldate cannot be 0");
1356 goto done_errmsg;
1357 }
1358 }
1359
1360 root = NULL;
1361 error = vfs_getopt(opts, "path", (void **)&path, &len);
1362 if (error == ENOENT)
1363 path = NULL;
1364 else if (error != 0)
1365 goto done_free;
1366 else {
1367 if (flags & JAIL_UPDATE) {
1368 error = EINVAL;
1369 vfs_opterror(opts,
1370 "path cannot be changed after creation");
1371 goto done_errmsg;
1372 }
1373 if (len == 0 || path[len - 1] != '\0') {
1374 error = EINVAL;
1375 goto done_free;
1376 }
1377 NDINIT(&nd, LOOKUP, FOLLOW | LOCKLEAF, UIO_SYSSPACE, path);
1378 error = namei(&nd);
1379 if (error)
1380 goto done_free;
1381 root = nd.ni_vp;
1382 NDFREE_PNBUF(&nd);
1383 g_path = malloc(MAXPATHLEN, M_TEMP, M_WAITOK);
1384 strlcpy(g_path, path, MAXPATHLEN);
1385 error = vn_path_to_global_path(td, root, g_path, MAXPATHLEN);
1386 if (error == 0) {
1387 path = g_path;
1388 } else {
1389 /* exit on other errors */
1390 goto done_free;
1391 }
1392 if (root->v_type != VDIR) {
1393 error = ENOTDIR;
1394 vput(root);
1395 goto done_free;
1396 }
1397 VOP_UNLOCK(root);
1398 }
1399
1400 /*
1401 * Find the specified jail, or at least its parent.
1402 * This abuses the file error codes ENOENT and EEXIST.
1403 */
1404 pr = NULL;
1405 inspr = NULL;
1406 if (cuflags == JAIL_CREATE && jid == 0 && name != NULL) {
1407 namelc = strrchr(name, '.');
1408 jid = strtoul(namelc != NULL ? namelc + 1 : name, &p, 10);
1409 if (*p != '\0')
1410 jid = 0;
1411 }
1412 sx_xlock(&allprison_lock);
1413 drflags = PD_LIST_XLOCKED;
1414 ppr = mypr;
1415 if (!prison_isalive(ppr)) {
1416 /* This jail is dying. This process will surely follow. */
1417 error = EAGAIN;
1418 goto done_deref;
1419 }
1420 if (jid != 0) {
1421 if (jid < 0) {
1422 error = EINVAL;
1423 vfs_opterror(opts, "negative jid");
1424 goto done_deref;
1425 }
1426 /*
1427 * See if a requested jid already exists. Keep track of
1428 * where it can be inserted later.
1429 */
1430 TAILQ_FOREACH(inspr, &allprison, pr_list) {
1431 if (inspr->pr_id < jid)
1432 continue;
1433 if (inspr->pr_id > jid)
1434 break;
1435 pr = inspr;
1436 mtx_lock(&pr->pr_mtx);
1437 drflags |= PD_LOCKED;
1438 inspr = NULL;
1439 break;
1440 }
1441 if (pr != NULL) {
1442 /* Create: jid must not exist. */
1443 if (cuflags == JAIL_CREATE) {
1444 /*
1445 * Even creators that cannot see the jail will
1446 * get EEXIST.
1447 */
1448 error = EEXIST;
1449 vfs_opterror(opts, "jail %d already exists",
1450 jid);
1451 goto done_deref;
1452 }
1453 if (!prison_ischild(mypr, pr)) {
1454 /*
1455 * Updaters get ENOENT if they cannot see the
1456 * jail. This is true even for CREATE | UPDATE,
1457 * which normally cannot give this error.
1458 */
1459 error = ENOENT;
1460 vfs_opterror(opts, "jail %d not found", jid);
1461 goto done_deref;
1462 }
1463 ppr = pr->pr_parent;
1464 if (!prison_isalive(ppr)) {
1465 error = ENOENT;
1466 vfs_opterror(opts, "jail %d is dying",
1467 ppr->pr_id);
1468 goto done_deref;
1469 }
1470 if (!prison_isalive(pr)) {
1471 if (!(flags & JAIL_DYING)) {
1472 error = ENOENT;
1473 vfs_opterror(opts, "jail %d is dying",
1474 jid);
1475 goto done_deref;
1476 }
1477 if ((flags & JAIL_ATTACH) ||
1478 (pr_flags & PR_PERSIST)) {
1479 /*
1480 * A dying jail might be resurrected
1481 * (via attach or persist), but first
1482 * it must determine if another jail
1483 * has claimed its name. Accomplish
1484 * this by implicitly re-setting the
1485 * name.
1486 */
1487 if (name == NULL)
1488 name = prison_name(mypr, pr);
1489 }
1490 }
1491 } else {
1492 /* Update: jid must exist. */
1493 if (cuflags == JAIL_UPDATE) {
1494 error = ENOENT;
1495 vfs_opterror(opts, "jail %d not found", jid);
1496 goto done_deref;
1497 }
1498 }
1499 }
1500 /*
1501 * If the caller provided a name, look for a jail by that name.
1502 * This has different semantics for creates and updates keyed by jid
1503 * (where the name must not already exist in a different jail),
1504 * and updates keyed by the name itself (where the name must exist
1505 * because that is the jail being updated).
1506 */
1507 namelc = NULL;
1508 if (name != NULL) {
1509 namelc = strrchr(name, '.');
1510 if (namelc == NULL)
1511 namelc = name;
1512 else {
1513 /*
1514 * This is a hierarchical name. Split it into the
1515 * parent and child names, and make sure the parent
1516 * exists or matches an already found jail.
1517 */
1518 if (pr != NULL) {
1519 if (strncmp(name, ppr->pr_name, namelc - name)
1520 || ppr->pr_name[namelc - name] != '\0') {
1521 error = EINVAL;
1522 vfs_opterror(opts,
1523 "cannot change jail's parent");
1524 goto done_deref;
1525 }
1526 } else {
1527 *namelc = '\0';
1528 ppr = prison_find_name(mypr, name);
1529 if (ppr == NULL) {
1530 error = ENOENT;
1531 vfs_opterror(opts,
1532 "jail \"%s\" not found", name);
1533 goto done_deref;
1534 }
1535 mtx_unlock(&ppr->pr_mtx);
1536 if (!prison_isalive(ppr)) {
1537 error = ENOENT;
1538 vfs_opterror(opts,
1539 "jail \"%s\" is dying", name);
1540 goto done_deref;
1541 }
1542 *namelc = '.';
1543 }
1544 namelc++;
1545 }
1546 if (namelc[0] != '\0') {
1547 pnamelen =
1548 (ppr == &prison0) ? 0 : strlen(ppr->pr_name) + 1;
1549 deadpr = NULL;
1550 FOREACH_PRISON_CHILD(ppr, tpr) {
1551 if (tpr != pr &&
1552 !strcmp(tpr->pr_name + pnamelen, namelc)) {
1553 if (prison_isalive(tpr)) {
1554 if (pr == NULL &&
1555 cuflags != JAIL_CREATE) {
1556 /*
1557 * Use this jail
1558 * for updates.
1559 */
1560 pr = tpr;
1561 mtx_lock(&pr->pr_mtx);
1562 drflags |= PD_LOCKED;
1563 break;
1564 }
1565 /*
1566 * Create, or update(jid):
1567 * name must not exist in an
1568 * active sibling jail.
1569 */
1570 error = EEXIST;
1571 vfs_opterror(opts,
1572 "jail \"%s\" already exists",
1573 name);
1574 goto done_deref;
1575 }
1576 if (pr == NULL &&
1577 cuflags != JAIL_CREATE) {
1578 deadpr = tpr;
1579 }
1580 }
1581 }
1582 /* If no active jail is found, use a dying one. */
1583 if (deadpr != NULL && pr == NULL) {
1584 if (flags & JAIL_DYING) {
1585 pr = deadpr;
1586 mtx_lock(&pr->pr_mtx);
1587 drflags |= PD_LOCKED;
1588 } else if (cuflags == JAIL_UPDATE) {
1589 error = ENOENT;
1590 vfs_opterror(opts,
1591 "jail \"%s\" is dying", name);
1592 goto done_deref;
1593 }
1594 }
1595 /* Update: name must exist if no jid. */
1596 else if (cuflags == JAIL_UPDATE && pr == NULL) {
1597 error = ENOENT;
1598 vfs_opterror(opts, "jail \"%s\" not found",
1599 name);
1600 goto done_deref;
1601 }
1602 }
1603 }
1604 /* Update: must provide a jid or name. */
1605 else if (cuflags == JAIL_UPDATE && pr == NULL) {
1606 error = ENOENT;
1607 vfs_opterror(opts, "update specified no jail");
1608 goto done_deref;
1609 }
1610
1611 /* If there's no prison to update, create a new one and link it in. */
1612 created = pr == NULL;
1613 if (created) {
1614 for (tpr = mypr; tpr != NULL; tpr = tpr->pr_parent)
1615 if (tpr->pr_childcount >= tpr->pr_childmax) {
1616 error = EPERM;
1617 vfs_opterror(opts, "prison limit exceeded");
1618 goto done_deref;
1619 }
1620 if (jid == 0 && (jid = get_next_prid(&inspr)) == 0) {
1621 error = EAGAIN;
1622 vfs_opterror(opts, "no available jail IDs");
1623 goto done_deref;
1624 }
1625
1626 pr = malloc(sizeof(*pr), M_PRISON, M_WAITOK | M_ZERO);
1627 pr->pr_state = PRISON_STATE_INVALID;
1628 refcount_init(&pr->pr_ref, 1);
1629 refcount_init(&pr->pr_uref, 0);
1630 drflags |= PD_DEREF;
1631 LIST_INIT(&pr->pr_children);
1632 mtx_init(&pr->pr_mtx, "jail mutex", NULL, MTX_DEF | MTX_DUPOK);
1633 TASK_INIT(&pr->pr_task, 0, prison_complete, pr);
1634
1635 pr->pr_id = jid;
1636 if (inspr != NULL)
1637 TAILQ_INSERT_BEFORE(inspr, pr, pr_list);
1638 else
1639 TAILQ_INSERT_TAIL(&allprison, pr, pr_list);
1640
1641 pr->pr_parent = ppr;
1642 prison_hold(ppr);
1643 prison_proc_hold(ppr);
1644 LIST_INSERT_HEAD(&ppr->pr_children, pr, pr_sibling);
1645 for (tpr = ppr; tpr != NULL; tpr = tpr->pr_parent)
1646 tpr->pr_childcount++;
1647
1648 /* Set some default values, and inherit some from the parent. */
1649 if (namelc == NULL)
1650 namelc = "";
1651 if (path == NULL) {
1652 path = "/";
1653 root = mypr->pr_root;
1654 vref(root);
1655 }
1656 strlcpy(pr->pr_hostuuid, DEFAULT_HOSTUUID, HOSTUUIDLEN);
1657 pr->pr_flags |= PR_HOST;
1658 #if defined(INET) || defined(INET6)
1659 #ifdef VIMAGE
1660 if (!(pr_flags & PR_VNET))
1661 #endif
1662 {
1663 #ifdef INET
1664 if (!(ch_flags & PR_IP4_USER))
1665 pr->pr_flags |= PR_IP4 | PR_IP4_USER;
1666 else if (!(pr_flags & PR_IP4_USER)) {
1667 pr->pr_flags |= ppr->pr_flags & PR_IP4;
1668 prison_ip_dup(ppr, pr, PR_INET);
1669 }
1670 #endif
1671 #ifdef INET6
1672 if (!(ch_flags & PR_IP6_USER))
1673 pr->pr_flags |= PR_IP6 | PR_IP6_USER;
1674 else if (!(pr_flags & PR_IP6_USER)) {
1675 pr->pr_flags |= ppr->pr_flags & PR_IP6;
1676 prison_ip_dup(ppr, pr, PR_INET6);
1677 }
1678 #endif
1679 }
1680 #endif
1681 /* Source address selection is always on by default. */
1682 pr->pr_flags |= _PR_IP_SADDRSEL;
1683
1684 pr->pr_securelevel = ppr->pr_securelevel;
1685 pr->pr_allow = JAIL_DEFAULT_ALLOW & ppr->pr_allow;
1686 pr->pr_enforce_statfs = jail_default_enforce_statfs;
1687 pr->pr_devfs_rsnum = ppr->pr_devfs_rsnum;
1688
1689 pr->pr_osreldate = osreldt ? osreldt : ppr->pr_osreldate;
1690 if (osrelstr == NULL)
1691 strlcpy(pr->pr_osrelease, ppr->pr_osrelease,
1692 sizeof(pr->pr_osrelease));
1693 else
1694 strlcpy(pr->pr_osrelease, osrelstr,
1695 sizeof(pr->pr_osrelease));
1696
1697 #ifdef VIMAGE
1698 /* Allocate a new vnet if specified. */
1699 pr->pr_vnet = (pr_flags & PR_VNET)
1700 ? vnet_alloc() : ppr->pr_vnet;
1701 #endif
1702 /*
1703 * Allocate a dedicated cpuset for each jail.
1704 * Unlike other initial settings, this may return an error.
1705 */
1706 error = cpuset_create_root(ppr, &pr->pr_cpuset);
1707 if (error)
1708 goto done_deref;
1709
1710 mtx_lock(&pr->pr_mtx);
1711 drflags |= PD_LOCKED;
1712 } else {
1713 /*
1714 * Grab a reference for existing prisons, to ensure they
1715 * continue to exist for the duration of the call.
1716 */
1717 prison_hold(pr);
1718 drflags |= PD_DEREF;
1719 #if defined(VIMAGE) && (defined(INET) || defined(INET6))
1720 if ((pr->pr_flags & PR_VNET) &&
1721 (ch_flags & (PR_IP4_USER | PR_IP6_USER))) {
1722 error = EINVAL;
1723 vfs_opterror(opts,
1724 "vnet jails cannot have IP address restrictions");
1725 goto done_deref;
1726 }
1727 #endif
1728 #ifdef INET
1729 if (PR_IP4_USER & ch_flags & (pr_flags ^ pr->pr_flags)) {
1730 error = EINVAL;
1731 vfs_opterror(opts,
1732 "ip4 cannot be changed after creation");
1733 goto done_deref;
1734 }
1735 #endif
1736 #ifdef INET6
1737 if (PR_IP6_USER & ch_flags & (pr_flags ^ pr->pr_flags)) {
1738 error = EINVAL;
1739 vfs_opterror(opts,
1740 "ip6 cannot be changed after creation");
1741 goto done_deref;
1742 }
1743 #endif
1744 }
1745
1746 /* Do final error checking before setting anything. */
1747 if (gotslevel) {
1748 if (slevel < ppr->pr_securelevel) {
1749 error = EPERM;
1750 goto done_deref;
1751 }
1752 }
1753 if (gotchildmax) {
1754 if (childmax >= ppr->pr_childmax) {
1755 error = EPERM;
1756 goto done_deref;
1757 }
1758 }
1759 if (gotenforce) {
1760 if (enforce < ppr->pr_enforce_statfs) {
1761 error = EPERM;
1762 goto done_deref;
1763 }
1764 }
1765 if (gotrsnum) {
1766 /*
1767 * devfs_rsnum is a uint16_t
1768 */
1769 if (rsnum < 0 || rsnum > 65535) {
1770 error = EINVAL;
1771 goto done_deref;
1772 }
1773 /*
1774 * Nested jails always inherit parent's devfs ruleset
1775 */
1776 if (jailed(td->td_ucred)) {
1777 if (rsnum > 0 && rsnum != ppr->pr_devfs_rsnum) {
1778 error = EPERM;
1779 goto done_deref;
1780 } else
1781 rsnum = ppr->pr_devfs_rsnum;
1782 }
1783 }
1784 #ifdef INET
1785 if (ip4s > 0) {
1786 if ((ppr->pr_flags & PR_IP4) &&
1787 !prison_ip_parent_match(ppr->pr_addrs[PR_INET], ip4,
1788 PR_INET)) {
1789 error = EPERM;
1790 goto done_deref;
1791 }
1792 if (!prison_ip_conflict_check(ppr, pr, ip4, PR_INET)) {
1793 error = EADDRINUSE;
1794 vfs_opterror(opts, "IPv4 addresses clash");
1795 goto done_deref;
1796 }
1797 }
1798 #endif
1799 #ifdef INET6
1800 if (ip6s > 0) {
1801 if ((ppr->pr_flags & PR_IP6) &&
1802 !prison_ip_parent_match(ppr->pr_addrs[PR_INET6], ip6,
1803 PR_INET6)) {
1804 error = EPERM;
1805 goto done_deref;
1806 }
1807 if (!prison_ip_conflict_check(ppr, pr, ip6, PR_INET6)) {
1808 error = EADDRINUSE;
1809 vfs_opterror(opts, "IPv6 addresses clash");
1810 goto done_deref;
1811 }
1812 }
1813 #endif
1814 onamelen = namelen = 0;
1815 if (namelc != NULL) {
1816 /* Give a default name of the jid. Also allow the name to be
1817 * explicitly the jid - but not any other number, and only in
1818 * normal form (no leading zero/etc).
1819 */
1820 if (namelc[0] == '\0')
1821 snprintf(namelc = numbuf, sizeof(numbuf), "%d", jid);
1822 else if ((strtoul(namelc, &p, 10) != jid ||
1823 namelc[0] < '1' || namelc[0] > '9') && *p == '\0') {
1824 error = EINVAL;
1825 vfs_opterror(opts,
1826 "name cannot be numeric (unless it is the jid)");
1827 goto done_deref;
1828 }
1829 /*
1830 * Make sure the name isn't too long for the prison or its
1831 * children.
1832 */
1833 pnamelen = (ppr == &prison0) ? 0 : strlen(ppr->pr_name) + 1;
1834 onamelen = strlen(pr->pr_name + pnamelen);
1835 namelen = strlen(namelc);
1836 if (pnamelen + namelen + 1 > sizeof(pr->pr_name)) {
1837 error = ENAMETOOLONG;
1838 goto done_deref;
1839 }
1840 FOREACH_PRISON_DESCENDANT(pr, tpr, descend) {
1841 if (strlen(tpr->pr_name) + (namelen - onamelen) >=
1842 sizeof(pr->pr_name)) {
1843 error = ENAMETOOLONG;
1844 goto done_deref;
1845 }
1846 }
1847 }
1848 pr_allow_diff = pr_allow & ~ppr->pr_allow;
1849 if (pr_allow_diff & ~PR_ALLOW_DIFFERENCES) {
1850 error = EPERM;
1851 goto done_deref;
1852 }
1853
1854 /*
1855 * Let modules check their parameters. This requires unlocking and
1856 * then re-locking the prison, but this is still a valid state as long
1857 * as allprison_lock remains xlocked.
1858 */
1859 mtx_unlock(&pr->pr_mtx);
1860 drflags &= ~PD_LOCKED;
1861 error = osd_jail_call(pr, PR_METHOD_CHECK, opts);
1862 if (error != 0)
1863 goto done_deref;
1864 mtx_lock(&pr->pr_mtx);
1865 drflags |= PD_LOCKED;
1866
1867 /* At this point, all valid parameters should have been noted. */
1868 TAILQ_FOREACH(opt, opts, link) {
1869 if (!opt->seen && strcmp(opt->name, "errmsg")) {
1870 error = EINVAL;
1871 vfs_opterror(opts, "unknown parameter: %s", opt->name);
1872 goto done_deref;
1873 }
1874 }
1875
1876 /* Set the parameters of the prison. */
1877 #ifdef INET
1878 redo_ip4 = false;
1879 if (pr_flags & PR_IP4_USER) {
1880 pr->pr_flags |= PR_IP4;
1881 prison_ip_set(pr, PR_INET, ip4);
1882 ip4 = NULL;
1883 FOREACH_PRISON_DESCENDANT_LOCKED(pr, tpr, descend) {
1884 #ifdef VIMAGE
1885 if (tpr->pr_flags & PR_VNET) {
1886 descend = 0;
1887 continue;
1888 }
1889 #endif
1890 if (!prison_ip_restrict(tpr, PR_INET, NULL)) {
1891 redo_ip4 = true;
1892 descend = 0;
1893 }
1894 }
1895 }
1896 #endif
1897 #ifdef INET6
1898 redo_ip6 = false;
1899 if (pr_flags & PR_IP6_USER) {
1900 pr->pr_flags |= PR_IP6;
1901 prison_ip_set(pr, PR_INET6, ip6);
1902 ip6 = NULL;
1903 FOREACH_PRISON_DESCENDANT_LOCKED(pr, tpr, descend) {
1904 #ifdef VIMAGE
1905 if (tpr->pr_flags & PR_VNET) {
1906 descend = 0;
1907 continue;
1908 }
1909 #endif
1910 if (!prison_ip_restrict(tpr, PR_INET6, NULL)) {
1911 redo_ip6 = true;
1912 descend = 0;
1913 }
1914 }
1915 }
1916 #endif
1917 if (gotslevel) {
1918 pr->pr_securelevel = slevel;
1919 /* Set all child jails to be at least this level. */
1920 FOREACH_PRISON_DESCENDANT_LOCKED(pr, tpr, descend)
1921 if (tpr->pr_securelevel < slevel)
1922 tpr->pr_securelevel = slevel;
1923 }
1924 if (gotchildmax) {
1925 pr->pr_childmax = childmax;
1926 /* Set all child jails to under this limit. */
1927 FOREACH_PRISON_DESCENDANT_LOCKED_LEVEL(pr, tpr, descend, level)
1928 if (tpr->pr_childmax > childmax - level)
1929 tpr->pr_childmax = childmax > level
1930 ? childmax - level : 0;
1931 }
1932 if (gotenforce) {
1933 pr->pr_enforce_statfs = enforce;
1934 /* Pass this restriction on to the children. */
1935 FOREACH_PRISON_DESCENDANT_LOCKED(pr, tpr, descend)
1936 if (tpr->pr_enforce_statfs < enforce)
1937 tpr->pr_enforce_statfs = enforce;
1938 }
1939 if (gotrsnum) {
1940 pr->pr_devfs_rsnum = rsnum;
1941 /* Pass this restriction on to the children. */
1942 FOREACH_PRISON_DESCENDANT_LOCKED(pr, tpr, descend)
1943 tpr->pr_devfs_rsnum = rsnum;
1944 }
1945 if (namelc != NULL) {
1946 if (ppr == &prison0)
1947 strlcpy(pr->pr_name, namelc, sizeof(pr->pr_name));
1948 else
1949 snprintf(pr->pr_name, sizeof(pr->pr_name), "%s.%s",
1950 ppr->pr_name, namelc);
1951 /* Change this component of child names. */
1952 FOREACH_PRISON_DESCENDANT_LOCKED(pr, tpr, descend) {
1953 bcopy(tpr->pr_name + onamelen, tpr->pr_name + namelen,
1954 strlen(tpr->pr_name + onamelen) + 1);
1955 bcopy(pr->pr_name, tpr->pr_name, namelen);
1956 }
1957 }
1958 if (path != NULL) {
1959 /* Try to keep a real-rooted full pathname. */
1960 strlcpy(pr->pr_path, path, sizeof(pr->pr_path));
1961 pr->pr_root = root;
1962 root = NULL;
1963 }
1964 if (PR_HOST & ch_flags & ~pr_flags) {
1965 if (pr->pr_flags & PR_HOST) {
1966 /*
1967 * Copy the parent's host info. As with pr_ip4 above,
1968 * the lack of a lock on the parent is not a problem;
1969 * it is always set with allprison_lock at least
1970 * shared, and is held exclusively here.
1971 */
1972 strlcpy(pr->pr_hostname, pr->pr_parent->pr_hostname,
1973 sizeof(pr->pr_hostname));
1974 strlcpy(pr->pr_domainname, pr->pr_parent->pr_domainname,
1975 sizeof(pr->pr_domainname));
1976 strlcpy(pr->pr_hostuuid, pr->pr_parent->pr_hostuuid,
1977 sizeof(pr->pr_hostuuid));
1978 pr->pr_hostid = pr->pr_parent->pr_hostid;
1979 }
1980 } else if (host != NULL || domain != NULL || uuid != NULL || gothid) {
1981 /* Set this prison, and any descendants without PR_HOST. */
1982 if (host != NULL)
1983 strlcpy(pr->pr_hostname, host, sizeof(pr->pr_hostname));
1984 if (domain != NULL)
1985 strlcpy(pr->pr_domainname, domain,
1986 sizeof(pr->pr_domainname));
1987 if (uuid != NULL)
1988 strlcpy(pr->pr_hostuuid, uuid, sizeof(pr->pr_hostuuid));
1989 if (gothid)
1990 pr->pr_hostid = hid;
1991 FOREACH_PRISON_DESCENDANT_LOCKED(pr, tpr, descend) {
1992 if (tpr->pr_flags & PR_HOST)
1993 descend = 0;
1994 else {
1995 if (host != NULL)
1996 strlcpy(tpr->pr_hostname,
1997 pr->pr_hostname,
1998 sizeof(tpr->pr_hostname));
1999 if (domain != NULL)
2000 strlcpy(tpr->pr_domainname,
2001 pr->pr_domainname,
2002 sizeof(tpr->pr_domainname));
2003 if (uuid != NULL)
2004 strlcpy(tpr->pr_hostuuid,
2005 pr->pr_hostuuid,
2006 sizeof(tpr->pr_hostuuid));
2007 if (gothid)
2008 tpr->pr_hostid = hid;
2009 }
2010 }
2011 }
2012 pr->pr_allow = (pr->pr_allow & ~ch_allow) | pr_allow;
2013 if ((tallow = ch_allow & ~pr_allow))
2014 prison_set_allow_locked(pr, tallow, 0);
2015 /*
2016 * Persistent prisons get an extra reference, and prisons losing their
2017 * persist flag lose that reference.
2018 */
2019 born = !prison_isalive(pr);
2020 if (ch_flags & PR_PERSIST & (pr_flags ^ pr->pr_flags)) {
2021 if (pr_flags & PR_PERSIST) {
2022 prison_hold(pr);
2023 /*
2024 * This may make a dead prison alive again, but wait
2025 * to label it as such until after OSD calls have had
2026 * a chance to run (and perhaps to fail).
2027 */
2028 refcount_acquire(&pr->pr_uref);
2029 } else {
2030 drflags |= PD_DEUREF;
2031 prison_free_not_last(pr);
2032 }
2033 }
2034 pr->pr_flags = (pr->pr_flags & ~ch_flags) | pr_flags;
2035 mtx_unlock(&pr->pr_mtx);
2036 drflags &= ~PD_LOCKED;
2037 /*
2038 * Any errors past this point will need to de-persist newly created
2039 * prisons, as well as call remove methods.
2040 */
2041 if (born)
2042 drflags |= PD_KILL;
2043
2044 #ifdef RACCT
2045 if (racct_enable && created)
2046 prison_racct_attach(pr);
2047 #endif
2048
2049 /* Locks may have prevented a complete restriction of child IP
2050 * addresses. If so, allocate some more memory and try again.
2051 */
2052 #ifdef INET
2053 while (redo_ip4) {
2054 ip4s = pr->pr_addrs[PR_INET]->ips;
2055 MPASS(ip4 == NULL);
2056 ip4 = prison_ip_alloc(PR_INET, ip4s, M_WAITOK);
2057 mtx_lock(&pr->pr_mtx);
2058 redo_ip4 = false;
2059 FOREACH_PRISON_DESCENDANT_LOCKED(pr, tpr, descend) {
2060 #ifdef VIMAGE
2061 if (tpr->pr_flags & PR_VNET) {
2062 descend = 0;
2063 continue;
2064 }
2065 #endif
2066 if (!prison_ip_restrict(tpr, PR_INET, &ip4))
2067 redo_ip4 = true;
2068 }
2069 mtx_unlock(&pr->pr_mtx);
2070 }
2071 #endif
2072 #ifdef INET6
2073 while (redo_ip6) {
2074 ip6s = pr->pr_addrs[PR_INET6]->ips;
2075 MPASS(ip6 == NULL);
2076 ip6 = prison_ip_alloc(PR_INET6, ip6s, M_WAITOK);
2077 mtx_lock(&pr->pr_mtx);
2078 redo_ip6 = false;
2079 FOREACH_PRISON_DESCENDANT_LOCKED(pr, tpr, descend) {
2080 #ifdef VIMAGE
2081 if (tpr->pr_flags & PR_VNET) {
2082 descend = 0;
2083 continue;
2084 }
2085 #endif
2086 if (!prison_ip_restrict(tpr, PR_INET6, &ip6))
2087 redo_ip6 = true;
2088 }
2089 mtx_unlock(&pr->pr_mtx);
2090 }
2091 #endif
2092
2093 /* Let the modules do their work. */
2094 if (born) {
2095 error = osd_jail_call(pr, PR_METHOD_CREATE, opts);
2096 if (error)
2097 goto done_deref;
2098 }
2099 error = osd_jail_call(pr, PR_METHOD_SET, opts);
2100 if (error)
2101 goto done_deref;
2102
2103 /*
2104 * A new prison is now ready to be seen; either it has gained a user
2105 * reference via persistence, or is about to gain one via attachment.
2106 */
2107 if (born) {
2108 drflags = prison_lock_xlock(pr, drflags);
2109 pr->pr_state = PRISON_STATE_ALIVE;
2110 }
2111
2112 /* Attach this process to the prison if requested. */
2113 if (flags & JAIL_ATTACH) {
2114 error = do_jail_attach(td, pr,
2115 prison_lock_xlock(pr, drflags & PD_LOCK_FLAGS));
2116 drflags &= ~(PD_LOCKED | PD_LIST_XLOCKED);
2117 if (error) {
2118 vfs_opterror(opts, "attach failed");
2119 goto done_deref;
2120 }
2121 }
2122
2123 #ifdef RACCT
2124 if (racct_enable && !created) {
2125 if (drflags & PD_LOCKED) {
2126 mtx_unlock(&pr->pr_mtx);
2127 drflags &= ~PD_LOCKED;
2128 }
2129 if (drflags & PD_LIST_XLOCKED) {
2130 sx_xunlock(&allprison_lock);
2131 drflags &= ~PD_LIST_XLOCKED;
2132 }
2133 prison_racct_modify(pr);
2134 }
2135 #endif
2136
2137 if (born && pr != &prison0 && (pr->pr_allow & PR_ALLOW_NFSD) != 0 &&
2138 (pr->pr_root->v_vflag & VV_ROOT) == 0)
2139 printf("Warning jail jid=%d: mountd/nfsd requires a separate"
2140 " file system\n", pr->pr_id);
2141
2142 drflags &= ~PD_KILL;
2143 td->td_retval[0] = pr->pr_id;
2144
2145 done_deref:
2146 /* Release any temporary prison holds and/or locks. */
2147 if (pr != NULL)
2148 prison_deref(pr, drflags);
2149 else if (drflags & PD_LIST_SLOCKED)
2150 sx_sunlock(&allprison_lock);
2151 else if (drflags & PD_LIST_XLOCKED)
2152 sx_xunlock(&allprison_lock);
2153 if (root != NULL)
2154 vrele(root);
2155 done_errmsg:
2156 if (error) {
2157 /* Write the error message back to userspace. */
2158 if (vfs_getopt(opts, "errmsg", (void **)&errmsg,
2159 &errmsg_len) == 0 && errmsg_len > 0) {
2160 errmsg_pos = 2 * vfs_getopt_pos(opts, "errmsg") + 1;
2161 if (optuio->uio_segflg == UIO_SYSSPACE)
2162 bcopy(errmsg,
2163 optuio->uio_iov[errmsg_pos].iov_base,
2164 errmsg_len);
2165 else
2166 (void)copyout(errmsg,
2167 optuio->uio_iov[errmsg_pos].iov_base,
2168 errmsg_len);
2169 }
2170 }
2171 done_free:
2172 #ifdef INET
2173 prison_ip_free(ip4);
2174 #endif
2175 #ifdef INET6
2176 prison_ip_free(ip6);
2177 #endif
2178 if (g_path != NULL)
2179 free(g_path, M_TEMP);
2180 vfs_freeopts(opts);
2181 return (error);
2182 }
2183
2184 /*
2185 * Find the next available prison ID. Return the ID on success, or zero
2186 * on failure. Also set a pointer to the allprison list entry the prison
2187 * should be inserted before.
2188 */
2189 static int
get_next_prid(struct prison ** insprp)2190 get_next_prid(struct prison **insprp)
2191 {
2192 struct prison *inspr;
2193 int jid, maxid;
2194
2195 jid = lastprid % JAIL_MAX + 1;
2196 if (TAILQ_EMPTY(&allprison) ||
2197 TAILQ_LAST(&allprison, prisonlist)->pr_id < jid) {
2198 /*
2199 * A common case is for all jails to be implicitly numbered,
2200 * which means they'll go on the end of the list, at least
2201 * for the first JAIL_MAX times.
2202 */
2203 inspr = NULL;
2204 } else {
2205 /*
2206 * Take two passes through the allprison list: first starting
2207 * with the proposed jid, then ending with it.
2208 */
2209 for (maxid = JAIL_MAX; maxid != 0; ) {
2210 TAILQ_FOREACH(inspr, &allprison, pr_list) {
2211 if (inspr->pr_id < jid)
2212 continue;
2213 if (inspr->pr_id > jid) {
2214 /* Found an opening. */
2215 maxid = 0;
2216 break;
2217 }
2218 if (++jid > maxid) {
2219 if (lastprid == maxid || lastprid == 0)
2220 {
2221 /*
2222 * The entire legal range
2223 * has been traversed
2224 */
2225 return 0;
2226 }
2227 /* Try again from the start. */
2228 jid = 1;
2229 maxid = lastprid;
2230 break;
2231 }
2232 }
2233 if (inspr == NULL) {
2234 /* Found room at the end of the list. */
2235 break;
2236 }
2237 }
2238 }
2239 *insprp = inspr;
2240 lastprid = jid;
2241 return (jid);
2242 }
2243
2244 /*
2245 * struct jail_get_args {
2246 * struct iovec *iovp;
2247 * unsigned int iovcnt;
2248 * int flags;
2249 * };
2250 */
2251 int
sys_jail_get(struct thread * td,struct jail_get_args * uap)2252 sys_jail_get(struct thread *td, struct jail_get_args *uap)
2253 {
2254 struct uio *auio;
2255 int error;
2256
2257 /* Check that we have an even number of iovecs. */
2258 if (uap->iovcnt & 1)
2259 return (EINVAL);
2260
2261 error = copyinuio(uap->iovp, uap->iovcnt, &auio);
2262 if (error)
2263 return (error);
2264 error = kern_jail_get(td, auio, uap->flags);
2265 if (error == 0)
2266 error = copyout(auio->uio_iov, uap->iovp,
2267 uap->iovcnt * sizeof(struct iovec));
2268 freeuio(auio);
2269 return (error);
2270 }
2271
2272 int
kern_jail_get(struct thread * td,struct uio * optuio,int flags)2273 kern_jail_get(struct thread *td, struct uio *optuio, int flags)
2274 {
2275 struct bool_flags *bf;
2276 struct jailsys_flags *jsf;
2277 struct prison *pr, *mypr;
2278 struct vfsopt *opt;
2279 struct vfsoptlist *opts;
2280 char *errmsg, *name;
2281 int drflags, error, errmsg_len, errmsg_pos, i, jid, len, pos;
2282 unsigned f;
2283
2284 if (flags & ~JAIL_GET_MASK)
2285 return (EINVAL);
2286
2287 /* Get the parameter list. */
2288 error = vfs_buildopts(optuio, &opts);
2289 if (error)
2290 return (error);
2291 errmsg_pos = vfs_getopt_pos(opts, "errmsg");
2292 mypr = td->td_ucred->cr_prison;
2293 pr = NULL;
2294
2295 /*
2296 * Find the prison specified by one of: lastjid, jid, name.
2297 */
2298 sx_slock(&allprison_lock);
2299 drflags = PD_LIST_SLOCKED;
2300 error = vfs_copyopt(opts, "lastjid", &jid, sizeof(jid));
2301 if (error == 0) {
2302 TAILQ_FOREACH(pr, &allprison, pr_list) {
2303 if (pr->pr_id > jid &&
2304 ((flags & JAIL_DYING) || prison_isalive(pr)) &&
2305 prison_ischild(mypr, pr)) {
2306 mtx_lock(&pr->pr_mtx);
2307 drflags |= PD_LOCKED;
2308 goto found_prison;
2309 }
2310 }
2311 error = ENOENT;
2312 vfs_opterror(opts, "no jail after %d", jid);
2313 goto done;
2314 } else if (error != ENOENT)
2315 goto done;
2316
2317 error = vfs_copyopt(opts, "jid", &jid, sizeof(jid));
2318 if (error == 0) {
2319 if (jid != 0) {
2320 pr = prison_find_child(mypr, jid);
2321 if (pr != NULL) {
2322 drflags |= PD_LOCKED;
2323 if (!(prison_isalive(pr) ||
2324 (flags & JAIL_DYING))) {
2325 error = ENOENT;
2326 vfs_opterror(opts, "jail %d is dying",
2327 jid);
2328 goto done;
2329 }
2330 goto found_prison;
2331 }
2332 error = ENOENT;
2333 vfs_opterror(opts, "jail %d not found", jid);
2334 goto done;
2335 }
2336 } else if (error != ENOENT)
2337 goto done;
2338
2339 error = vfs_getopt(opts, "name", (void **)&name, &len);
2340 if (error == 0) {
2341 if (len == 0 || name[len - 1] != '\0') {
2342 error = EINVAL;
2343 goto done;
2344 }
2345 pr = prison_find_name(mypr, name);
2346 if (pr != NULL) {
2347 drflags |= PD_LOCKED;
2348 if (!(prison_isalive(pr) || (flags & JAIL_DYING))) {
2349 error = ENOENT;
2350 vfs_opterror(opts, "jail \"%s\" is dying",
2351 name);
2352 goto done;
2353 }
2354 goto found_prison;
2355 }
2356 error = ENOENT;
2357 vfs_opterror(opts, "jail \"%s\" not found", name);
2358 goto done;
2359 } else if (error != ENOENT)
2360 goto done;
2361
2362 vfs_opterror(opts, "no jail specified");
2363 error = ENOENT;
2364 goto done;
2365
2366 found_prison:
2367 /* Get the parameters of the prison. */
2368 prison_hold(pr);
2369 drflags |= PD_DEREF;
2370 td->td_retval[0] = pr->pr_id;
2371 error = vfs_setopt(opts, "jid", &pr->pr_id, sizeof(pr->pr_id));
2372 if (error != 0 && error != ENOENT)
2373 goto done;
2374 i = (pr->pr_parent == mypr) ? 0 : pr->pr_parent->pr_id;
2375 error = vfs_setopt(opts, "parent", &i, sizeof(i));
2376 if (error != 0 && error != ENOENT)
2377 goto done;
2378 error = vfs_setopts(opts, "name", prison_name(mypr, pr));
2379 if (error != 0 && error != ENOENT)
2380 goto done;
2381 error = vfs_setopt(opts, "cpuset.id", &pr->pr_cpuset->cs_id,
2382 sizeof(pr->pr_cpuset->cs_id));
2383 if (error != 0 && error != ENOENT)
2384 goto done;
2385 error = vfs_setopts(opts, "path", prison_path(mypr, pr));
2386 if (error != 0 && error != ENOENT)
2387 goto done;
2388 #ifdef INET
2389 error = vfs_setopt_part(opts, "ip4.addr", pr->pr_addrs[PR_INET]->pr_ip,
2390 pr->pr_addrs[PR_INET] ? pr->pr_addrs[PR_INET]->ips *
2391 pr_families[PR_INET].size : 0 );
2392 if (error != 0 && error != ENOENT)
2393 goto done;
2394 #endif
2395 #ifdef INET6
2396 error = vfs_setopt_part(opts, "ip6.addr", pr->pr_addrs[PR_INET6]->pr_ip,
2397 pr->pr_addrs[PR_INET6] ? pr->pr_addrs[PR_INET6]->ips *
2398 pr_families[PR_INET6].size : 0 );
2399 if (error != 0 && error != ENOENT)
2400 goto done;
2401 #endif
2402 error = vfs_setopt(opts, "securelevel", &pr->pr_securelevel,
2403 sizeof(pr->pr_securelevel));
2404 if (error != 0 && error != ENOENT)
2405 goto done;
2406 error = vfs_setopt(opts, "children.cur", &pr->pr_childcount,
2407 sizeof(pr->pr_childcount));
2408 if (error != 0 && error != ENOENT)
2409 goto done;
2410 error = vfs_setopt(opts, "children.max", &pr->pr_childmax,
2411 sizeof(pr->pr_childmax));
2412 if (error != 0 && error != ENOENT)
2413 goto done;
2414 error = vfs_setopts(opts, "host.hostname", pr->pr_hostname);
2415 if (error != 0 && error != ENOENT)
2416 goto done;
2417 error = vfs_setopts(opts, "host.domainname", pr->pr_domainname);
2418 if (error != 0 && error != ENOENT)
2419 goto done;
2420 error = vfs_setopts(opts, "host.hostuuid", pr->pr_hostuuid);
2421 if (error != 0 && error != ENOENT)
2422 goto done;
2423 #ifdef COMPAT_FREEBSD32
2424 if (SV_PROC_FLAG(td->td_proc, SV_ILP32)) {
2425 uint32_t hid32 = pr->pr_hostid;
2426
2427 error = vfs_setopt(opts, "host.hostid", &hid32, sizeof(hid32));
2428 } else
2429 #endif
2430 error = vfs_setopt(opts, "host.hostid", &pr->pr_hostid,
2431 sizeof(pr->pr_hostid));
2432 if (error != 0 && error != ENOENT)
2433 goto done;
2434 error = vfs_setopt(opts, "enforce_statfs", &pr->pr_enforce_statfs,
2435 sizeof(pr->pr_enforce_statfs));
2436 if (error != 0 && error != ENOENT)
2437 goto done;
2438 error = vfs_setopt(opts, "devfs_ruleset", &pr->pr_devfs_rsnum,
2439 sizeof(pr->pr_devfs_rsnum));
2440 if (error != 0 && error != ENOENT)
2441 goto done;
2442 for (bf = pr_flag_bool;
2443 bf < pr_flag_bool + nitems(pr_flag_bool);
2444 bf++) {
2445 i = (pr->pr_flags & bf->flag) ? 1 : 0;
2446 error = vfs_setopt(opts, bf->name, &i, sizeof(i));
2447 if (error != 0 && error != ENOENT)
2448 goto done;
2449 i = !i;
2450 error = vfs_setopt(opts, bf->noname, &i, sizeof(i));
2451 if (error != 0 && error != ENOENT)
2452 goto done;
2453 }
2454 for (jsf = pr_flag_jailsys;
2455 jsf < pr_flag_jailsys + nitems(pr_flag_jailsys);
2456 jsf++) {
2457 f = pr->pr_flags & (jsf->disable | jsf->new);
2458 i = (f != 0 && f == jsf->disable) ? JAIL_SYS_DISABLE
2459 : (f == jsf->new) ? JAIL_SYS_NEW
2460 : JAIL_SYS_INHERIT;
2461 error = vfs_setopt(opts, jsf->name, &i, sizeof(i));
2462 if (error != 0 && error != ENOENT)
2463 goto done;
2464 }
2465 for (bf = pr_flag_allow;
2466 bf < pr_flag_allow + nitems(pr_flag_allow) &&
2467 atomic_load_int(&bf->flag) != 0;
2468 bf++) {
2469 i = (pr->pr_allow & bf->flag) ? 1 : 0;
2470 error = vfs_setopt(opts, bf->name, &i, sizeof(i));
2471 if (error != 0 && error != ENOENT)
2472 goto done;
2473 i = !i;
2474 error = vfs_setopt(opts, bf->noname, &i, sizeof(i));
2475 if (error != 0 && error != ENOENT)
2476 goto done;
2477 }
2478 i = !prison_isalive(pr);
2479 error = vfs_setopt(opts, "dying", &i, sizeof(i));
2480 if (error != 0 && error != ENOENT)
2481 goto done;
2482 i = !i;
2483 error = vfs_setopt(opts, "nodying", &i, sizeof(i));
2484 if (error != 0 && error != ENOENT)
2485 goto done;
2486 error = vfs_setopt(opts, "osreldate", &pr->pr_osreldate,
2487 sizeof(pr->pr_osreldate));
2488 if (error != 0 && error != ENOENT)
2489 goto done;
2490 error = vfs_setopts(opts, "osrelease", pr->pr_osrelease);
2491 if (error != 0 && error != ENOENT)
2492 goto done;
2493
2494 /* Get the module parameters. */
2495 mtx_unlock(&pr->pr_mtx);
2496 drflags &= ~PD_LOCKED;
2497 error = osd_jail_call(pr, PR_METHOD_GET, opts);
2498 if (error)
2499 goto done;
2500 prison_deref(pr, drflags);
2501 pr = NULL;
2502 drflags = 0;
2503
2504 /* By now, all parameters should have been noted. */
2505 TAILQ_FOREACH(opt, opts, link) {
2506 if (!opt->seen && strcmp(opt->name, "errmsg")) {
2507 error = EINVAL;
2508 vfs_opterror(opts, "unknown parameter: %s", opt->name);
2509 goto done;
2510 }
2511 }
2512
2513 /* Write the fetched parameters back to userspace. */
2514 error = 0;
2515 TAILQ_FOREACH(opt, opts, link) {
2516 if (opt->pos >= 0 && opt->pos != errmsg_pos) {
2517 pos = 2 * opt->pos + 1;
2518 optuio->uio_iov[pos].iov_len = opt->len;
2519 if (opt->value != NULL) {
2520 if (optuio->uio_segflg == UIO_SYSSPACE) {
2521 bcopy(opt->value,
2522 optuio->uio_iov[pos].iov_base,
2523 opt->len);
2524 } else {
2525 error = copyout(opt->value,
2526 optuio->uio_iov[pos].iov_base,
2527 opt->len);
2528 if (error)
2529 break;
2530 }
2531 }
2532 }
2533 }
2534
2535 done:
2536 /* Release any temporary prison holds and/or locks. */
2537 if (pr != NULL)
2538 prison_deref(pr, drflags);
2539 else if (drflags & PD_LIST_SLOCKED)
2540 sx_sunlock(&allprison_lock);
2541 if (error && errmsg_pos >= 0) {
2542 /* Write the error message back to userspace. */
2543 vfs_getopt(opts, "errmsg", (void **)&errmsg, &errmsg_len);
2544 errmsg_pos = 2 * errmsg_pos + 1;
2545 if (errmsg_len > 0) {
2546 if (optuio->uio_segflg == UIO_SYSSPACE)
2547 bcopy(errmsg,
2548 optuio->uio_iov[errmsg_pos].iov_base,
2549 errmsg_len);
2550 else
2551 (void)copyout(errmsg,
2552 optuio->uio_iov[errmsg_pos].iov_base,
2553 errmsg_len);
2554 }
2555 }
2556 vfs_freeopts(opts);
2557 return (error);
2558 }
2559
2560 /*
2561 * struct jail_remove_args {
2562 * int jid;
2563 * };
2564 */
2565 int
sys_jail_remove(struct thread * td,struct jail_remove_args * uap)2566 sys_jail_remove(struct thread *td, struct jail_remove_args *uap)
2567 {
2568 struct prison *pr;
2569 int error;
2570
2571 error = priv_check(td, PRIV_JAIL_REMOVE);
2572 if (error)
2573 return (error);
2574
2575 sx_xlock(&allprison_lock);
2576 pr = prison_find_child(td->td_ucred->cr_prison, uap->jid);
2577 if (pr == NULL) {
2578 sx_xunlock(&allprison_lock);
2579 return (EINVAL);
2580 }
2581 if (!prison_isalive(pr)) {
2582 /* Silently ignore already-dying prisons. */
2583 mtx_unlock(&pr->pr_mtx);
2584 sx_xunlock(&allprison_lock);
2585 return (0);
2586 }
2587 prison_deref(pr, PD_KILL | PD_LOCKED | PD_LIST_XLOCKED);
2588 return (0);
2589 }
2590
2591 /*
2592 * struct jail_attach_args {
2593 * int jid;
2594 * };
2595 */
2596 int
sys_jail_attach(struct thread * td,struct jail_attach_args * uap)2597 sys_jail_attach(struct thread *td, struct jail_attach_args *uap)
2598 {
2599 struct prison *pr;
2600 int error;
2601
2602 error = priv_check(td, PRIV_JAIL_ATTACH);
2603 if (error)
2604 return (error);
2605
2606 sx_slock(&allprison_lock);
2607 pr = prison_find_child(td->td_ucred->cr_prison, uap->jid);
2608 if (pr == NULL) {
2609 sx_sunlock(&allprison_lock);
2610 return (EINVAL);
2611 }
2612
2613 /* Do not allow a process to attach to a prison that is not alive. */
2614 if (!prison_isalive(pr)) {
2615 mtx_unlock(&pr->pr_mtx);
2616 sx_sunlock(&allprison_lock);
2617 return (EINVAL);
2618 }
2619
2620 return (do_jail_attach(td, pr, PD_LOCKED | PD_LIST_SLOCKED));
2621 }
2622
2623 static int
do_jail_attach(struct thread * td,struct prison * pr,int drflags)2624 do_jail_attach(struct thread *td, struct prison *pr, int drflags)
2625 {
2626 struct proc *p;
2627 struct ucred *newcred, *oldcred;
2628 int error;
2629
2630 mtx_assert(&pr->pr_mtx, MA_OWNED);
2631 sx_assert(&allprison_lock, SX_LOCKED);
2632 drflags &= PD_LOCK_FLAGS;
2633 /*
2634 * XXX: Note that there is a slight race here if two threads
2635 * in the same privileged process attempt to attach to two
2636 * different jails at the same time. It is important for
2637 * user processes not to do this, or they might end up with
2638 * a process root from one prison, but attached to the jail
2639 * of another.
2640 */
2641 prison_hold(pr);
2642 refcount_acquire(&pr->pr_uref);
2643 drflags |= PD_DEREF | PD_DEUREF;
2644 mtx_unlock(&pr->pr_mtx);
2645 drflags &= ~PD_LOCKED;
2646
2647 /* Let modules do whatever they need to prepare for attaching. */
2648 error = osd_jail_call(pr, PR_METHOD_ATTACH, td);
2649 if (error) {
2650 prison_deref(pr, drflags);
2651 return (error);
2652 }
2653 sx_unlock(&allprison_lock);
2654 drflags &= ~(PD_LIST_SLOCKED | PD_LIST_XLOCKED);
2655
2656 /*
2657 * Reparent the newly attached process to this jail.
2658 */
2659 p = td->td_proc;
2660 error = cpuset_setproc_update_set(p, pr->pr_cpuset);
2661 if (error)
2662 goto e_revert_osd;
2663
2664 vn_lock(pr->pr_root, LK_EXCLUSIVE | LK_RETRY);
2665 if ((error = change_dir(pr->pr_root, td)) != 0)
2666 goto e_unlock;
2667 #ifdef MAC
2668 if ((error = mac_vnode_check_chroot(td->td_ucred, pr->pr_root)))
2669 goto e_unlock;
2670 #endif
2671 VOP_UNLOCK(pr->pr_root);
2672 if ((error = pwd_chroot_chdir(td, pr->pr_root)))
2673 goto e_revert_osd;
2674
2675 newcred = crget();
2676 PROC_LOCK(p);
2677 oldcred = crcopysafe(p, newcred);
2678 newcred->cr_prison = pr;
2679 proc_set_cred(p, newcred);
2680 setsugid(p);
2681 #ifdef RACCT
2682 racct_proc_ucred_changed(p, oldcred, newcred);
2683 crhold(newcred);
2684 #endif
2685 PROC_UNLOCK(p);
2686 #ifdef RCTL
2687 rctl_proc_ucred_changed(p, newcred);
2688 crfree(newcred);
2689 #endif
2690 prison_proc_relink(oldcred->cr_prison, pr, p);
2691 prison_deref(oldcred->cr_prison, drflags);
2692 crfree(oldcred);
2693
2694 /*
2695 * If the prison was killed while changing credentials, die along
2696 * with it.
2697 */
2698 if (!prison_isalive(pr)) {
2699 PROC_LOCK(p);
2700 kern_psignal(p, SIGKILL);
2701 PROC_UNLOCK(p);
2702 }
2703
2704 return (0);
2705
2706 e_unlock:
2707 VOP_UNLOCK(pr->pr_root);
2708 e_revert_osd:
2709 /* Tell modules this thread is still in its old jail after all. */
2710 sx_slock(&allprison_lock);
2711 drflags |= PD_LIST_SLOCKED;
2712 (void)osd_jail_call(td->td_ucred->cr_prison, PR_METHOD_ATTACH, td);
2713 prison_deref(pr, drflags);
2714 return (error);
2715 }
2716
2717 /*
2718 * Returns a locked prison instance, or NULL on failure.
2719 */
2720 struct prison *
prison_find(int prid)2721 prison_find(int prid)
2722 {
2723 struct prison *pr;
2724
2725 sx_assert(&allprison_lock, SX_LOCKED);
2726 TAILQ_FOREACH(pr, &allprison, pr_list) {
2727 if (pr->pr_id < prid)
2728 continue;
2729 if (pr->pr_id > prid)
2730 break;
2731 KASSERT(prison_isvalid(pr), ("Found invalid prison %p", pr));
2732 mtx_lock(&pr->pr_mtx);
2733 return (pr);
2734 }
2735 return (NULL);
2736 }
2737
2738 /*
2739 * Find a prison that is a descendant of mypr. Returns a locked prison or NULL.
2740 */
2741 struct prison *
prison_find_child(struct prison * mypr,int prid)2742 prison_find_child(struct prison *mypr, int prid)
2743 {
2744 struct prison *pr;
2745 int descend;
2746
2747 sx_assert(&allprison_lock, SX_LOCKED);
2748 FOREACH_PRISON_DESCENDANT(mypr, pr, descend) {
2749 if (pr->pr_id == prid) {
2750 KASSERT(prison_isvalid(pr),
2751 ("Found invalid prison %p", pr));
2752 mtx_lock(&pr->pr_mtx);
2753 return (pr);
2754 }
2755 }
2756 return (NULL);
2757 }
2758
2759 /*
2760 * Look for the name relative to mypr. Returns a locked prison or NULL.
2761 */
2762 struct prison *
prison_find_name(struct prison * mypr,const char * name)2763 prison_find_name(struct prison *mypr, const char *name)
2764 {
2765 struct prison *pr, *deadpr;
2766 size_t mylen;
2767 int descend;
2768
2769 sx_assert(&allprison_lock, SX_LOCKED);
2770 mylen = (mypr == &prison0) ? 0 : strlen(mypr->pr_name) + 1;
2771 deadpr = NULL;
2772 FOREACH_PRISON_DESCENDANT(mypr, pr, descend) {
2773 if (!strcmp(pr->pr_name + mylen, name)) {
2774 KASSERT(prison_isvalid(pr),
2775 ("Found invalid prison %p", pr));
2776 if (prison_isalive(pr)) {
2777 mtx_lock(&pr->pr_mtx);
2778 return (pr);
2779 }
2780 deadpr = pr;
2781 }
2782 }
2783 /* There was no valid prison - perhaps there was a dying one. */
2784 if (deadpr != NULL)
2785 mtx_lock(&deadpr->pr_mtx);
2786 return (deadpr);
2787 }
2788
2789 /*
2790 * See if a prison has the specific flag set. The prison should be locked,
2791 * unless checking for flags that are only set at jail creation (such as
2792 * PR_IP4 and PR_IP6), or only the single bit is examined, without regard
2793 * to any other prison data.
2794 */
2795 bool
prison_flag(struct ucred * cred,unsigned flag)2796 prison_flag(struct ucred *cred, unsigned flag)
2797 {
2798
2799 return ((cred->cr_prison->pr_flags & flag) != 0);
2800 }
2801
2802 /*
2803 * See if a prison has the specific allow flag set.
2804 * The prison *should* be locked, or only a single bit is examined, without
2805 * regard to any other prison data.
2806 */
2807 bool
prison_allow(struct ucred * cred,unsigned flag)2808 prison_allow(struct ucred *cred, unsigned flag)
2809 {
2810
2811 return ((cred->cr_prison->pr_allow & flag) != 0);
2812 }
2813
2814 /*
2815 * Hold a prison reference, by incrementing pr_ref. It is generally
2816 * an error to hold a prison that does not already have a reference.
2817 * A prison record will remain valid as long as it has at least one
2818 * reference, and will not be removed as long as either the prison
2819 * mutex or the allprison lock is held (allprison_lock may be shared).
2820 */
2821 void
prison_hold_locked(struct prison * pr)2822 prison_hold_locked(struct prison *pr)
2823 {
2824
2825 /* Locking is no longer required. */
2826 prison_hold(pr);
2827 }
2828
2829 void
prison_hold(struct prison * pr)2830 prison_hold(struct prison *pr)
2831 {
2832 #ifdef INVARIANTS
2833 int was_valid = refcount_acquire_if_not_zero(&pr->pr_ref);
2834
2835 KASSERT(was_valid,
2836 ("Trying to hold dead prison %p (jid=%d).", pr, pr->pr_id));
2837 #else
2838 refcount_acquire(&pr->pr_ref);
2839 #endif
2840 }
2841
2842 /*
2843 * Remove a prison reference. If that was the last reference, the
2844 * prison will be removed (at a later time).
2845 */
2846 void
prison_free_locked(struct prison * pr)2847 prison_free_locked(struct prison *pr)
2848 {
2849
2850 mtx_assert(&pr->pr_mtx, MA_OWNED);
2851 /*
2852 * Locking is no longer required, but unlock because the caller
2853 * expects it.
2854 */
2855 mtx_unlock(&pr->pr_mtx);
2856 prison_free(pr);
2857 }
2858
2859 void
prison_free(struct prison * pr)2860 prison_free(struct prison *pr)
2861 {
2862
2863 KASSERT(refcount_load(&pr->pr_ref) > 0,
2864 ("Trying to free dead prison %p (jid=%d).",
2865 pr, pr->pr_id));
2866 if (!refcount_release_if_not_last(&pr->pr_ref)) {
2867 /*
2868 * Don't remove the last reference in this context,
2869 * in case there are locks held.
2870 */
2871 taskqueue_enqueue(taskqueue_thread, &pr->pr_task);
2872 }
2873 }
2874
2875 static void
prison_free_not_last(struct prison * pr)2876 prison_free_not_last(struct prison *pr)
2877 {
2878 #ifdef INVARIANTS
2879 int lastref;
2880
2881 KASSERT(refcount_load(&pr->pr_ref) > 0,
2882 ("Trying to free dead prison %p (jid=%d).",
2883 pr, pr->pr_id));
2884 lastref = refcount_release(&pr->pr_ref);
2885 KASSERT(!lastref,
2886 ("prison_free_not_last freed last ref on prison %p (jid=%d).",
2887 pr, pr->pr_id));
2888 #else
2889 refcount_release(&pr->pr_ref);
2890 #endif
2891 }
2892
2893 /*
2894 * Hold a prison for user visibility, by incrementing pr_uref.
2895 * It is generally an error to hold a prison that isn't already
2896 * user-visible, except through the jail system calls. It is also
2897 * an error to hold an invalid prison. A prison record will remain
2898 * alive as long as it has at least one user reference, and will not
2899 * be set to the dying state until the prison mutex and allprison_lock
2900 * are both freed.
2901 */
2902 void
prison_proc_hold(struct prison * pr)2903 prison_proc_hold(struct prison *pr)
2904 {
2905 #ifdef INVARIANTS
2906 int was_alive = refcount_acquire_if_not_zero(&pr->pr_uref);
2907
2908 KASSERT(was_alive,
2909 ("Cannot add a process to a non-alive prison (jid=%d)", pr->pr_id));
2910 #else
2911 refcount_acquire(&pr->pr_uref);
2912 #endif
2913 }
2914
2915 /*
2916 * Remove a prison user reference. If it was the last reference, the
2917 * prison will be considered "dying", and may be removed once all of
2918 * its references are dropped.
2919 */
2920 void
prison_proc_free(struct prison * pr)2921 prison_proc_free(struct prison *pr)
2922 {
2923
2924 /*
2925 * Locking is only required when releasing the last reference.
2926 * This allows assurance that a locked prison will remain alive
2927 * until it is unlocked.
2928 */
2929 KASSERT(refcount_load(&pr->pr_uref) > 0,
2930 ("Trying to kill a process in a dead prison (jid=%d)", pr->pr_id));
2931 if (!refcount_release_if_not_last(&pr->pr_uref)) {
2932 /*
2933 * Don't remove the last user reference in this context,
2934 * which is expected to be a process that is not only locked,
2935 * but also half dead. Add a reference so any calls to
2936 * prison_free() won't re-submit the task.
2937 */
2938 prison_hold(pr);
2939 mtx_lock(&pr->pr_mtx);
2940 KASSERT(!(pr->pr_flags & PR_COMPLETE_PROC),
2941 ("Redundant last reference in prison_proc_free (jid=%d)",
2942 pr->pr_id));
2943 pr->pr_flags |= PR_COMPLETE_PROC;
2944 mtx_unlock(&pr->pr_mtx);
2945 taskqueue_enqueue(taskqueue_thread, &pr->pr_task);
2946 }
2947 }
2948
2949 static void
prison_proc_free_not_last(struct prison * pr)2950 prison_proc_free_not_last(struct prison *pr)
2951 {
2952 #ifdef INVARIANTS
2953 int lastref;
2954
2955 KASSERT(refcount_load(&pr->pr_uref) > 0,
2956 ("Trying to free dead prison %p (jid=%d).",
2957 pr, pr->pr_id));
2958 lastref = refcount_release(&pr->pr_uref);
2959 KASSERT(!lastref,
2960 ("prison_proc_free_not_last freed last uref on prison %p (jid=%d).",
2961 pr, pr->pr_id));
2962 #else
2963 refcount_release(&pr->pr_uref);
2964 #endif
2965 }
2966
2967 void
prison_proc_link(struct prison * pr,struct proc * p)2968 prison_proc_link(struct prison *pr, struct proc *p)
2969 {
2970
2971 sx_assert(&allproc_lock, SA_XLOCKED);
2972 LIST_INSERT_HEAD(&pr->pr_proclist, p, p_jaillist);
2973 }
2974
2975 void
prison_proc_unlink(struct prison * pr,struct proc * p)2976 prison_proc_unlink(struct prison *pr, struct proc *p)
2977 {
2978
2979 sx_assert(&allproc_lock, SA_XLOCKED);
2980 LIST_REMOVE(p, p_jaillist);
2981 }
2982
2983 static void
prison_proc_relink(struct prison * opr,struct prison * npr,struct proc * p)2984 prison_proc_relink(struct prison *opr, struct prison *npr, struct proc *p)
2985 {
2986
2987 sx_xlock(&allproc_lock);
2988 prison_proc_unlink(opr, p);
2989 prison_proc_link(npr, p);
2990 sx_xunlock(&allproc_lock);
2991 }
2992
2993 /*
2994 * Complete a call to either prison_free or prison_proc_free.
2995 */
2996 static void
prison_complete(void * context,int pending)2997 prison_complete(void *context, int pending)
2998 {
2999 struct prison *pr = context;
3000 int drflags;
3001
3002 /*
3003 * This could be called to release the last reference, or the last
3004 * user reference (plus the reference held in prison_proc_free).
3005 */
3006 drflags = prison_lock_xlock(pr, PD_DEREF);
3007 if (pr->pr_flags & PR_COMPLETE_PROC) {
3008 pr->pr_flags &= ~PR_COMPLETE_PROC;
3009 drflags |= PD_DEUREF;
3010 }
3011 prison_deref(pr, drflags);
3012 }
3013
3014 static void
prison_kill_processes_cb(struct proc * p,void * arg __unused)3015 prison_kill_processes_cb(struct proc *p, void *arg __unused)
3016 {
3017
3018 kern_psignal(p, SIGKILL);
3019 }
3020
3021 /*
3022 * Note the iteration does not guarantee acting on all processes.
3023 * Most notably there may be fork or jail_attach in progress.
3024 */
3025 void
prison_proc_iterate(struct prison * pr,void (* cb)(struct proc *,void *),void * cbarg)3026 prison_proc_iterate(struct prison *pr, void (*cb)(struct proc *, void *),
3027 void *cbarg)
3028 {
3029 struct prison *ppr;
3030 struct proc *p;
3031
3032 if (atomic_load_int(&pr->pr_childcount) == 0) {
3033 sx_slock(&allproc_lock);
3034 LIST_FOREACH(p, &pr->pr_proclist, p_jaillist) {
3035 if (p->p_state == PRS_NEW)
3036 continue;
3037 PROC_LOCK(p);
3038 cb(p, cbarg);
3039 PROC_UNLOCK(p);
3040 }
3041 sx_sunlock(&allproc_lock);
3042 if (atomic_load_int(&pr->pr_childcount) == 0)
3043 return;
3044 /*
3045 * Some jails popped up during the iteration, fall through to a
3046 * system-wide search.
3047 */
3048 }
3049
3050 sx_slock(&allproc_lock);
3051 FOREACH_PROC_IN_SYSTEM(p) {
3052 PROC_LOCK(p);
3053 if (p->p_state != PRS_NEW && p->p_ucred != NULL) {
3054 for (ppr = p->p_ucred->cr_prison;
3055 ppr != &prison0;
3056 ppr = ppr->pr_parent) {
3057 if (ppr == pr) {
3058 cb(p, cbarg);
3059 break;
3060 }
3061 }
3062 }
3063 PROC_UNLOCK(p);
3064 }
3065 sx_sunlock(&allproc_lock);
3066 }
3067
3068 /*
3069 * Remove a prison reference and/or user reference (usually).
3070 * This assumes context that allows sleeping (for allprison_lock),
3071 * with no non-sleeping locks held, except perhaps the prison itself.
3072 * If there are no more references, release and delist the prison.
3073 * On completion, the prison lock and the allprison lock are both
3074 * unlocked.
3075 */
3076 static void
prison_deref(struct prison * pr,int flags)3077 prison_deref(struct prison *pr, int flags)
3078 {
3079 struct prisonlist freeprison;
3080 struct prison *killpr, *rpr, *ppr, *tpr;
3081
3082 killpr = NULL;
3083 TAILQ_INIT(&freeprison);
3084 /*
3085 * Release this prison as requested, which may cause its parent
3086 * to be released, and then maybe its grandparent, etc.
3087 */
3088 for (;;) {
3089 if (flags & PD_KILL) {
3090 /* Kill the prison and its descendents. */
3091 KASSERT(pr != &prison0,
3092 ("prison_deref trying to kill prison0"));
3093 if (!(flags & PD_DEREF)) {
3094 prison_hold(pr);
3095 flags |= PD_DEREF;
3096 }
3097 flags = prison_lock_xlock(pr, flags);
3098 prison_deref_kill(pr, &freeprison);
3099 }
3100 if (flags & PD_DEUREF) {
3101 /* Drop a user reference. */
3102 KASSERT(refcount_load(&pr->pr_uref) > 0,
3103 ("prison_deref PD_DEUREF on a dead prison (jid=%d)",
3104 pr->pr_id));
3105 if (!refcount_release_if_not_last(&pr->pr_uref)) {
3106 if (!(flags & PD_DEREF)) {
3107 prison_hold(pr);
3108 flags |= PD_DEREF;
3109 }
3110 flags = prison_lock_xlock(pr, flags);
3111 if (refcount_release(&pr->pr_uref) &&
3112 pr->pr_state == PRISON_STATE_ALIVE) {
3113 /*
3114 * When the last user references goes,
3115 * this becomes a dying prison.
3116 */
3117 KASSERT(
3118 refcount_load(&prison0.pr_uref) > 0,
3119 ("prison0 pr_uref=0"));
3120 pr->pr_state = PRISON_STATE_DYING;
3121 mtx_unlock(&pr->pr_mtx);
3122 flags &= ~PD_LOCKED;
3123 prison_cleanup(pr);
3124 }
3125 }
3126 }
3127 if (flags & PD_KILL) {
3128 /*
3129 * Any remaining user references are probably processes
3130 * that need to be killed, either in this prison or its
3131 * descendants.
3132 */
3133 if (refcount_load(&pr->pr_uref) > 0)
3134 killpr = pr;
3135 /* Make sure the parent prison doesn't get killed. */
3136 flags &= ~PD_KILL;
3137 }
3138 if (flags & PD_DEREF) {
3139 /* Drop a reference. */
3140 KASSERT(refcount_load(&pr->pr_ref) > 0,
3141 ("prison_deref PD_DEREF on a dead prison (jid=%d)",
3142 pr->pr_id));
3143 if (!refcount_release_if_not_last(&pr->pr_ref)) {
3144 flags = prison_lock_xlock(pr, flags);
3145 if (refcount_release(&pr->pr_ref)) {
3146 /*
3147 * When the last reference goes,
3148 * unlink the prison and set it aside.
3149 */
3150 KASSERT(
3151 refcount_load(&pr->pr_uref) == 0,
3152 ("prison_deref: last ref, "
3153 "but still has %d urefs (jid=%d)",
3154 pr->pr_uref, pr->pr_id));
3155 KASSERT(
3156 refcount_load(&prison0.pr_ref) != 0,
3157 ("prison0 pr_ref=0"));
3158 pr->pr_state = PRISON_STATE_INVALID;
3159 TAILQ_REMOVE(&allprison, pr, pr_list);
3160 LIST_REMOVE(pr, pr_sibling);
3161 TAILQ_INSERT_TAIL(&freeprison, pr,
3162 pr_list);
3163 for (ppr = pr->pr_parent;
3164 ppr != NULL;
3165 ppr = ppr->pr_parent)
3166 ppr->pr_childcount--;
3167 /*
3168 * Removing a prison frees references
3169 * from its parent.
3170 */
3171 mtx_unlock(&pr->pr_mtx);
3172 flags &= ~PD_LOCKED;
3173 pr = pr->pr_parent;
3174 flags |= PD_DEREF | PD_DEUREF;
3175 continue;
3176 }
3177 }
3178 }
3179 break;
3180 }
3181
3182 /* Release all the prison locks. */
3183 if (flags & PD_LOCKED)
3184 mtx_unlock(&pr->pr_mtx);
3185 if (flags & PD_LIST_SLOCKED)
3186 sx_sunlock(&allprison_lock);
3187 else if (flags & PD_LIST_XLOCKED)
3188 sx_xunlock(&allprison_lock);
3189
3190 /* Kill any processes attached to a killed prison. */
3191 if (killpr != NULL)
3192 prison_proc_iterate(killpr, prison_kill_processes_cb, NULL);
3193
3194 /*
3195 * Finish removing any unreferenced prisons, which couldn't happen
3196 * while allprison_lock was held (to avoid a LOR on vrele).
3197 */
3198 TAILQ_FOREACH_SAFE(rpr, &freeprison, pr_list, tpr) {
3199 #ifdef VIMAGE
3200 if (rpr->pr_vnet != rpr->pr_parent->pr_vnet)
3201 vnet_destroy(rpr->pr_vnet);
3202 #endif
3203 if (rpr->pr_root != NULL)
3204 vrele(rpr->pr_root);
3205 mtx_destroy(&rpr->pr_mtx);
3206 #ifdef INET
3207 prison_ip_free(rpr->pr_addrs[PR_INET]);
3208 #endif
3209 #ifdef INET6
3210 prison_ip_free(rpr->pr_addrs[PR_INET6]);
3211 #endif
3212 if (rpr->pr_cpuset != NULL)
3213 cpuset_rel(rpr->pr_cpuset);
3214 osd_jail_exit(rpr);
3215 #ifdef RACCT
3216 if (racct_enable)
3217 prison_racct_detach(rpr);
3218 #endif
3219 TAILQ_REMOVE(&freeprison, rpr, pr_list);
3220 free(rpr, M_PRISON);
3221 }
3222 }
3223
3224 /*
3225 * Kill the prison and its descendants. Mark them as dying, clear the
3226 * persist flag, and call module remove methods.
3227 */
3228 static void
prison_deref_kill(struct prison * pr,struct prisonlist * freeprison)3229 prison_deref_kill(struct prison *pr, struct prisonlist *freeprison)
3230 {
3231 struct prison *cpr, *ppr, *rpr;
3232 bool descend;
3233
3234 /*
3235 * Unlike the descendants, the target prison can be killed
3236 * even if it is currently dying. This is useful for failed
3237 * creation in jail_set(2).
3238 */
3239 KASSERT(refcount_load(&pr->pr_ref) > 0,
3240 ("Trying to kill dead prison %p (jid=%d).",
3241 pr, pr->pr_id));
3242 refcount_acquire(&pr->pr_uref);
3243 pr->pr_state = PRISON_STATE_DYING;
3244 mtx_unlock(&pr->pr_mtx);
3245
3246 rpr = NULL;
3247 FOREACH_PRISON_DESCENDANT_PRE_POST(pr, cpr, descend) {
3248 if (descend) {
3249 if (!prison_isalive(cpr)) {
3250 descend = false;
3251 continue;
3252 }
3253 prison_hold(cpr);
3254 prison_proc_hold(cpr);
3255 mtx_lock(&cpr->pr_mtx);
3256 cpr->pr_state = PRISON_STATE_DYING;
3257 cpr->pr_flags |= PR_REMOVE;
3258 mtx_unlock(&cpr->pr_mtx);
3259 continue;
3260 }
3261 if (!(cpr->pr_flags & PR_REMOVE))
3262 continue;
3263 prison_cleanup(cpr);
3264 mtx_lock(&cpr->pr_mtx);
3265 cpr->pr_flags &= ~PR_REMOVE;
3266 if (cpr->pr_flags & PR_PERSIST) {
3267 cpr->pr_flags &= ~PR_PERSIST;
3268 prison_proc_free_not_last(cpr);
3269 prison_free_not_last(cpr);
3270 }
3271 (void)refcount_release(&cpr->pr_uref);
3272 if (refcount_release(&cpr->pr_ref)) {
3273 /*
3274 * When the last reference goes, unlink the prison
3275 * and set it aside for prison_deref() to handle.
3276 * Delay unlinking the sibling list to keep the loop
3277 * safe.
3278 */
3279 if (rpr != NULL)
3280 LIST_REMOVE(rpr, pr_sibling);
3281 rpr = cpr;
3282 rpr->pr_state = PRISON_STATE_INVALID;
3283 TAILQ_REMOVE(&allprison, rpr, pr_list);
3284 TAILQ_INSERT_TAIL(freeprison, rpr, pr_list);
3285 /*
3286 * Removing a prison frees references from its parent.
3287 */
3288 ppr = rpr->pr_parent;
3289 prison_proc_free_not_last(ppr);
3290 prison_free_not_last(ppr);
3291 for (; ppr != NULL; ppr = ppr->pr_parent)
3292 ppr->pr_childcount--;
3293 }
3294 mtx_unlock(&cpr->pr_mtx);
3295 }
3296 if (rpr != NULL)
3297 LIST_REMOVE(rpr, pr_sibling);
3298
3299 prison_cleanup(pr);
3300 mtx_lock(&pr->pr_mtx);
3301 if (pr->pr_flags & PR_PERSIST) {
3302 pr->pr_flags &= ~PR_PERSIST;
3303 prison_proc_free_not_last(pr);
3304 prison_free_not_last(pr);
3305 }
3306 (void)refcount_release(&pr->pr_uref);
3307 }
3308
3309 /*
3310 * Given the current locking state in the flags, make sure allprison_lock
3311 * is held exclusive, and the prison is locked. Return flags indicating
3312 * the new state.
3313 */
3314 static int
prison_lock_xlock(struct prison * pr,int flags)3315 prison_lock_xlock(struct prison *pr, int flags)
3316 {
3317
3318 if (!(flags & PD_LIST_XLOCKED)) {
3319 /*
3320 * Get allprison_lock, which may be an upgrade,
3321 * and may require unlocking the prison.
3322 */
3323 if (flags & PD_LOCKED) {
3324 mtx_unlock(&pr->pr_mtx);
3325 flags &= ~PD_LOCKED;
3326 }
3327 if (flags & PD_LIST_SLOCKED) {
3328 if (!sx_try_upgrade(&allprison_lock)) {
3329 sx_sunlock(&allprison_lock);
3330 sx_xlock(&allprison_lock);
3331 }
3332 flags &= ~PD_LIST_SLOCKED;
3333 } else
3334 sx_xlock(&allprison_lock);
3335 flags |= PD_LIST_XLOCKED;
3336 }
3337 if (!(flags & PD_LOCKED)) {
3338 /* Lock the prison mutex. */
3339 mtx_lock(&pr->pr_mtx);
3340 flags |= PD_LOCKED;
3341 }
3342 return flags;
3343 }
3344
3345 /*
3346 * Release a prison's resources when it starts dying (when the last user
3347 * reference is dropped, or when it is killed).
3348 */
3349 static void
prison_cleanup(struct prison * pr)3350 prison_cleanup(struct prison *pr)
3351 {
3352 sx_assert(&allprison_lock, SA_XLOCKED);
3353 mtx_assert(&pr->pr_mtx, MA_NOTOWNED);
3354 vfs_exjail_delete(pr);
3355 shm_remove_prison(pr);
3356 (void)osd_jail_call(pr, PR_METHOD_REMOVE, NULL);
3357 }
3358
3359 /*
3360 * Set or clear a permission bit in the pr_allow field, passing restrictions
3361 * (cleared permission) down to child jails.
3362 */
3363 void
prison_set_allow(struct ucred * cred,unsigned flag,int enable)3364 prison_set_allow(struct ucred *cred, unsigned flag, int enable)
3365 {
3366 struct prison *pr;
3367
3368 pr = cred->cr_prison;
3369 sx_slock(&allprison_lock);
3370 mtx_lock(&pr->pr_mtx);
3371 prison_set_allow_locked(pr, flag, enable);
3372 mtx_unlock(&pr->pr_mtx);
3373 sx_sunlock(&allprison_lock);
3374 }
3375
3376 static void
prison_set_allow_locked(struct prison * pr,unsigned flag,int enable)3377 prison_set_allow_locked(struct prison *pr, unsigned flag, int enable)
3378 {
3379 struct prison *cpr;
3380 int descend;
3381
3382 if (enable != 0)
3383 pr->pr_allow |= flag;
3384 else {
3385 pr->pr_allow &= ~flag;
3386 FOREACH_PRISON_DESCENDANT_LOCKED(pr, cpr, descend)
3387 cpr->pr_allow &= ~flag;
3388 }
3389 }
3390
3391 /*
3392 * Check if a jail supports the given address family.
3393 *
3394 * Returns 0 if not jailed or the address family is supported, EAFNOSUPPORT
3395 * if not.
3396 */
3397 int
prison_check_af(struct ucred * cred,int af)3398 prison_check_af(struct ucred *cred, int af)
3399 {
3400 struct prison *pr;
3401 int error;
3402
3403 KASSERT(cred != NULL, ("%s: cred is NULL", __func__));
3404
3405 pr = cred->cr_prison;
3406 #ifdef VIMAGE
3407 /* Prisons with their own network stack are not limited. */
3408 if (prison_owns_vnet(cred))
3409 return (0);
3410 #endif
3411
3412 error = 0;
3413 switch (af)
3414 {
3415 #ifdef INET
3416 case AF_INET:
3417 if (pr->pr_flags & PR_IP4)
3418 {
3419 mtx_lock(&pr->pr_mtx);
3420 if ((pr->pr_flags & PR_IP4) &&
3421 pr->pr_addrs[PR_INET] == NULL)
3422 error = EAFNOSUPPORT;
3423 mtx_unlock(&pr->pr_mtx);
3424 }
3425 break;
3426 #endif
3427 #ifdef INET6
3428 case AF_INET6:
3429 if (pr->pr_flags & PR_IP6)
3430 {
3431 mtx_lock(&pr->pr_mtx);
3432 if ((pr->pr_flags & PR_IP6) &&
3433 pr->pr_addrs[PR_INET6] == NULL)
3434 error = EAFNOSUPPORT;
3435 mtx_unlock(&pr->pr_mtx);
3436 }
3437 break;
3438 #endif
3439 case AF_LOCAL:
3440 case AF_ROUTE:
3441 case AF_NETLINK:
3442 break;
3443 default:
3444 if (!(pr->pr_allow & PR_ALLOW_SOCKET_AF))
3445 error = EAFNOSUPPORT;
3446 }
3447 return (error);
3448 }
3449
3450 /*
3451 * Check if given address belongs to the jail referenced by cred (wrapper to
3452 * prison_check_ip[46]).
3453 *
3454 * Returns 0 if jail doesn't restrict the address family or if address belongs
3455 * to jail, EADDRNOTAVAIL if the address doesn't belong, or EAFNOSUPPORT if
3456 * the jail doesn't allow the address family. IPv4 Address passed in in NBO.
3457 */
3458 int
prison_if(struct ucred * cred,const struct sockaddr * sa)3459 prison_if(struct ucred *cred, const struct sockaddr *sa)
3460 {
3461 #ifdef INET
3462 const struct sockaddr_in *sai;
3463 #endif
3464 #ifdef INET6
3465 const struct sockaddr_in6 *sai6;
3466 #endif
3467 int error;
3468
3469 KASSERT(cred != NULL, ("%s: cred is NULL", __func__));
3470 KASSERT(sa != NULL, ("%s: sa is NULL", __func__));
3471
3472 #ifdef VIMAGE
3473 if (prison_owns_vnet(cred))
3474 return (0);
3475 #endif
3476
3477 error = 0;
3478 switch (sa->sa_family)
3479 {
3480 #ifdef INET
3481 case AF_INET:
3482 sai = (const struct sockaddr_in *)sa;
3483 error = prison_check_ip4(cred, &sai->sin_addr);
3484 break;
3485 #endif
3486 #ifdef INET6
3487 case AF_INET6:
3488 sai6 = (const struct sockaddr_in6 *)sa;
3489 error = prison_check_ip6(cred, &sai6->sin6_addr);
3490 break;
3491 #endif
3492 default:
3493 if (!(cred->cr_prison->pr_allow & PR_ALLOW_SOCKET_AF))
3494 error = EAFNOSUPPORT;
3495 }
3496 return (error);
3497 }
3498
3499 /*
3500 * Return 0 if jails permit p1 to frob p2, otherwise ESRCH.
3501 */
3502 int
prison_check(struct ucred * cred1,struct ucred * cred2)3503 prison_check(struct ucred *cred1, struct ucred *cred2)
3504 {
3505
3506 return ((cred1->cr_prison == cred2->cr_prison ||
3507 prison_ischild(cred1->cr_prison, cred2->cr_prison)) ? 0 : ESRCH);
3508 }
3509
3510 /*
3511 * For mountd/nfsd to run within a prison, it must be:
3512 * - A vnet prison.
3513 * - PR_ALLOW_NFSD must be set on it.
3514 * - The root directory (pr_root) of the prison must be
3515 * a file system mount point, so the mountd can hang
3516 * export information on it.
3517 * - The prison's enforce_statfs cannot be 0, so that
3518 * mountd(8) can do exports.
3519 */
3520 bool
prison_check_nfsd(struct ucred * cred)3521 prison_check_nfsd(struct ucred *cred)
3522 {
3523
3524 if (jailed_without_vnet(cred))
3525 return (false);
3526 if (!prison_allow(cred, PR_ALLOW_NFSD))
3527 return (false);
3528 if ((cred->cr_prison->pr_root->v_vflag & VV_ROOT) == 0)
3529 return (false);
3530 if (cred->cr_prison->pr_enforce_statfs == 0)
3531 return (false);
3532 return (true);
3533 }
3534
3535 /*
3536 * Return true if p2 is a child of p1, otherwise false.
3537 */
3538 bool
prison_ischild(struct prison * pr1,struct prison * pr2)3539 prison_ischild(struct prison *pr1, struct prison *pr2)
3540 {
3541
3542 for (pr2 = pr2->pr_parent; pr2 != NULL; pr2 = pr2->pr_parent)
3543 if (pr1 == pr2)
3544 return (true);
3545 return (false);
3546 }
3547
3548 /*
3549 * Return true if the prison is currently alive. A prison is alive if it
3550 * holds user references and it isn't being removed.
3551 */
3552 bool
prison_isalive(const struct prison * pr)3553 prison_isalive(const struct prison *pr)
3554 {
3555
3556 if (__predict_false(pr->pr_state != PRISON_STATE_ALIVE))
3557 return (false);
3558 return (true);
3559 }
3560
3561 /*
3562 * Return true if the prison is currently valid. A prison is valid if it has
3563 * been fully created, and is not being destroyed. Note that dying prisons
3564 * are still considered valid. Invalid prisons won't be found under normal
3565 * circumstances, as they're only put in that state by functions that have
3566 * an exclusive hold on allprison_lock.
3567 */
3568 bool
prison_isvalid(struct prison * pr)3569 prison_isvalid(struct prison *pr)
3570 {
3571
3572 if (__predict_false(pr->pr_state == PRISON_STATE_INVALID))
3573 return (false);
3574 if (__predict_false(refcount_load(&pr->pr_ref) == 0))
3575 return (false);
3576 return (true);
3577 }
3578
3579 /*
3580 * Return true if the passed credential is in a jail and that jail does not
3581 * have its own virtual network stack, otherwise false.
3582 */
3583 bool
jailed_without_vnet(struct ucred * cred)3584 jailed_without_vnet(struct ucred *cred)
3585 {
3586
3587 if (!jailed(cred))
3588 return (false);
3589 #ifdef VIMAGE
3590 if (prison_owns_vnet(cred))
3591 return (false);
3592 #endif
3593
3594 return (true);
3595 }
3596
3597 /*
3598 * Return the correct hostname (domainname, et al) for the passed credential.
3599 */
3600 void
getcredhostname(struct ucred * cred,char * buf,size_t size)3601 getcredhostname(struct ucred *cred, char *buf, size_t size)
3602 {
3603 struct prison *pr;
3604
3605 /*
3606 * A NULL credential can be used to shortcut to the physical
3607 * system's hostname.
3608 */
3609 pr = (cred != NULL) ? cred->cr_prison : &prison0;
3610 mtx_lock(&pr->pr_mtx);
3611 strlcpy(buf, pr->pr_hostname, size);
3612 mtx_unlock(&pr->pr_mtx);
3613 }
3614
3615 void
getcreddomainname(struct ucred * cred,char * buf,size_t size)3616 getcreddomainname(struct ucred *cred, char *buf, size_t size)
3617 {
3618
3619 mtx_lock(&cred->cr_prison->pr_mtx);
3620 strlcpy(buf, cred->cr_prison->pr_domainname, size);
3621 mtx_unlock(&cred->cr_prison->pr_mtx);
3622 }
3623
3624 void
getcredhostuuid(struct ucred * cred,char * buf,size_t size)3625 getcredhostuuid(struct ucred *cred, char *buf, size_t size)
3626 {
3627
3628 mtx_lock(&cred->cr_prison->pr_mtx);
3629 strlcpy(buf, cred->cr_prison->pr_hostuuid, size);
3630 mtx_unlock(&cred->cr_prison->pr_mtx);
3631 }
3632
3633 void
getcredhostid(struct ucred * cred,unsigned long * hostid)3634 getcredhostid(struct ucred *cred, unsigned long *hostid)
3635 {
3636
3637 mtx_lock(&cred->cr_prison->pr_mtx);
3638 *hostid = cred->cr_prison->pr_hostid;
3639 mtx_unlock(&cred->cr_prison->pr_mtx);
3640 }
3641
3642 void
getjailname(struct ucred * cred,char * name,size_t len)3643 getjailname(struct ucred *cred, char *name, size_t len)
3644 {
3645
3646 mtx_lock(&cred->cr_prison->pr_mtx);
3647 strlcpy(name, cred->cr_prison->pr_name, len);
3648 mtx_unlock(&cred->cr_prison->pr_mtx);
3649 }
3650
3651 #ifdef VIMAGE
3652 /*
3653 * Determine whether the prison represented by cred owns
3654 * its vnet rather than having it inherited.
3655 *
3656 * Returns true in case the prison owns the vnet, false otherwise.
3657 */
3658 bool
prison_owns_vnet(struct ucred * cred)3659 prison_owns_vnet(struct ucred *cred)
3660 {
3661
3662 /*
3663 * vnets cannot be added/removed after jail creation,
3664 * so no need to lock here.
3665 */
3666 return ((cred->cr_prison->pr_flags & PR_VNET) != 0);
3667 }
3668 #endif
3669
3670 /*
3671 * Determine whether the subject represented by cred can "see"
3672 * status of a mount point.
3673 * Returns: 0 for permitted, ENOENT otherwise.
3674 * XXX: This function should be called cr_canseemount() and should be
3675 * placed in kern_prot.c.
3676 */
3677 int
prison_canseemount(struct ucred * cred,struct mount * mp)3678 prison_canseemount(struct ucred *cred, struct mount *mp)
3679 {
3680 struct prison *pr;
3681 struct statfs *sp;
3682 size_t len;
3683
3684 pr = cred->cr_prison;
3685 if (pr->pr_enforce_statfs == 0)
3686 return (0);
3687 if (pr->pr_root->v_mount == mp)
3688 return (0);
3689 if (pr->pr_enforce_statfs == 2)
3690 return (ENOENT);
3691 /*
3692 * If jail's chroot directory is set to "/" we should be able to see
3693 * all mount-points from inside a jail.
3694 * This is ugly check, but this is the only situation when jail's
3695 * directory ends with '/'.
3696 */
3697 if (strcmp(pr->pr_path, "/") == 0)
3698 return (0);
3699 len = strlen(pr->pr_path);
3700 sp = &mp->mnt_stat;
3701 if (strncmp(pr->pr_path, sp->f_mntonname, len) != 0)
3702 return (ENOENT);
3703 /*
3704 * Be sure that we don't have situation where jail's root directory
3705 * is "/some/path" and mount point is "/some/pathpath".
3706 */
3707 if (sp->f_mntonname[len] != '\0' && sp->f_mntonname[len] != '/')
3708 return (ENOENT);
3709 return (0);
3710 }
3711
3712 void
prison_enforce_statfs(struct ucred * cred,struct mount * mp,struct statfs * sp)3713 prison_enforce_statfs(struct ucred *cred, struct mount *mp, struct statfs *sp)
3714 {
3715 char jpath[MAXPATHLEN];
3716 struct prison *pr;
3717 size_t len;
3718
3719 pr = cred->cr_prison;
3720 if (pr->pr_enforce_statfs == 0)
3721 return;
3722 if (prison_canseemount(cred, mp) != 0) {
3723 bzero(sp->f_mntonname, sizeof(sp->f_mntonname));
3724 strlcpy(sp->f_mntonname, "[restricted]",
3725 sizeof(sp->f_mntonname));
3726 return;
3727 }
3728 if (pr->pr_root->v_mount == mp) {
3729 /*
3730 * Clear current buffer data, so we are sure nothing from
3731 * the valid path left there.
3732 */
3733 bzero(sp->f_mntonname, sizeof(sp->f_mntonname));
3734 *sp->f_mntonname = '/';
3735 return;
3736 }
3737 /*
3738 * If jail's chroot directory is set to "/" we should be able to see
3739 * all mount-points from inside a jail.
3740 */
3741 if (strcmp(pr->pr_path, "/") == 0)
3742 return;
3743 len = strlen(pr->pr_path);
3744 strlcpy(jpath, sp->f_mntonname + len, sizeof(jpath));
3745 /*
3746 * Clear current buffer data, so we are sure nothing from
3747 * the valid path left there.
3748 */
3749 bzero(sp->f_mntonname, sizeof(sp->f_mntonname));
3750 if (*jpath == '\0') {
3751 /* Should never happen. */
3752 *sp->f_mntonname = '/';
3753 } else {
3754 strlcpy(sp->f_mntonname, jpath, sizeof(sp->f_mntonname));
3755 }
3756 }
3757
3758 /*
3759 * Check with permission for a specific privilege is granted within jail. We
3760 * have a specific list of accepted privileges; the rest are denied.
3761 */
3762 int
prison_priv_check(struct ucred * cred,int priv)3763 prison_priv_check(struct ucred *cred, int priv)
3764 {
3765 struct prison *pr;
3766 int error;
3767
3768 /*
3769 * Some policies have custom handlers. This routine should not be
3770 * called for them. See priv_check_cred().
3771 */
3772 switch (priv) {
3773 case PRIV_VFS_LOOKUP:
3774 case PRIV_VFS_GENERATION:
3775 KASSERT(0, ("prison_priv_check instead of a custom handler "
3776 "called for %d\n", priv));
3777 }
3778
3779 if (!jailed(cred))
3780 return (0);
3781
3782 #ifdef VIMAGE
3783 /*
3784 * Privileges specific to prisons with a virtual network stack.
3785 * There might be a duplicate entry here in case the privilege
3786 * is only granted conditionally in the legacy jail case.
3787 */
3788 switch (priv) {
3789 /*
3790 * NFS-specific privileges.
3791 */
3792 case PRIV_NFS_DAEMON:
3793 case PRIV_VFS_GETFH:
3794 case PRIV_VFS_MOUNT_EXPORTED:
3795 if (!prison_check_nfsd(cred))
3796 return (EPERM);
3797 #ifdef notyet
3798 case PRIV_NFS_LOCKD:
3799 #endif
3800 /*
3801 * Network stack privileges.
3802 */
3803 case PRIV_NET_BRIDGE:
3804 case PRIV_NET_GRE:
3805 case PRIV_NET_BPF:
3806 case PRIV_NET_RAW: /* Dup, cond. in legacy jail case. */
3807 case PRIV_NET_ROUTE:
3808 case PRIV_NET_TAP:
3809 case PRIV_NET_SETIFMTU:
3810 case PRIV_NET_SETIFFLAGS:
3811 case PRIV_NET_SETIFCAP:
3812 case PRIV_NET_SETIFDESCR:
3813 case PRIV_NET_SETIFNAME :
3814 case PRIV_NET_SETIFMETRIC:
3815 case PRIV_NET_SETIFPHYS:
3816 case PRIV_NET_SETIFMAC:
3817 case PRIV_NET_SETLANPCP:
3818 case PRIV_NET_ADDMULTI:
3819 case PRIV_NET_DELMULTI:
3820 case PRIV_NET_HWIOCTL:
3821 case PRIV_NET_SETLLADDR:
3822 case PRIV_NET_ADDIFGROUP:
3823 case PRIV_NET_DELIFGROUP:
3824 case PRIV_NET_IFCREATE:
3825 case PRIV_NET_IFDESTROY:
3826 case PRIV_NET_ADDIFADDR:
3827 case PRIV_NET_DELIFADDR:
3828 case PRIV_NET_LAGG:
3829 case PRIV_NET_GIF:
3830 case PRIV_NET_SETIFVNET:
3831 case PRIV_NET_SETIFFIB:
3832 case PRIV_NET_OVPN:
3833 case PRIV_NET_ME:
3834 case PRIV_NET_WG:
3835
3836 /*
3837 * 802.11-related privileges.
3838 */
3839 case PRIV_NET80211_VAP_GETKEY:
3840 case PRIV_NET80211_VAP_MANAGE:
3841
3842 #ifdef notyet
3843 /*
3844 * ATM privileges.
3845 */
3846 case PRIV_NETATM_CFG:
3847 case PRIV_NETATM_ADD:
3848 case PRIV_NETATM_DEL:
3849 case PRIV_NETATM_SET:
3850
3851 /*
3852 * Bluetooth privileges.
3853 */
3854 case PRIV_NETBLUETOOTH_RAW:
3855 #endif
3856
3857 /*
3858 * Netgraph and netgraph module privileges.
3859 */
3860 case PRIV_NETGRAPH_CONTROL:
3861 #ifdef notyet
3862 case PRIV_NETGRAPH_TTY:
3863 #endif
3864
3865 /*
3866 * IPv4 and IPv6 privileges.
3867 */
3868 case PRIV_NETINET_IPFW:
3869 case PRIV_NETINET_DIVERT:
3870 case PRIV_NETINET_PF:
3871 case PRIV_NETINET_DUMMYNET:
3872 case PRIV_NETINET_CARP:
3873 case PRIV_NETINET_MROUTE:
3874 case PRIV_NETINET_RAW:
3875 case PRIV_NETINET_ADDRCTRL6:
3876 case PRIV_NETINET_ND6:
3877 case PRIV_NETINET_SCOPE6:
3878 case PRIV_NETINET_ALIFETIME6:
3879 case PRIV_NETINET_IPSEC:
3880 case PRIV_NETINET_BINDANY:
3881
3882 #ifdef notyet
3883 /*
3884 * NCP privileges.
3885 */
3886 case PRIV_NETNCP:
3887
3888 /*
3889 * SMB privileges.
3890 */
3891 case PRIV_NETSMB:
3892 #endif
3893
3894 /*
3895 * No default: or deny here.
3896 * In case of no permit fall through to next switch().
3897 */
3898 if (cred->cr_prison->pr_flags & PR_VNET)
3899 return (0);
3900 }
3901 #endif /* VIMAGE */
3902
3903 switch (priv) {
3904 /*
3905 * Allow ktrace privileges for root in jail.
3906 */
3907 case PRIV_KTRACE:
3908
3909 #if 0
3910 /*
3911 * Allow jailed processes to configure audit identity and
3912 * submit audit records (login, etc). In the future we may
3913 * want to further refine the relationship between audit and
3914 * jail.
3915 */
3916 case PRIV_AUDIT_GETAUDIT:
3917 case PRIV_AUDIT_SETAUDIT:
3918 case PRIV_AUDIT_SUBMIT:
3919 #endif
3920
3921 /*
3922 * Allow jailed processes to manipulate process UNIX
3923 * credentials in any way they see fit.
3924 */
3925 case PRIV_CRED_SETUID:
3926 case PRIV_CRED_SETEUID:
3927 case PRIV_CRED_SETGID:
3928 case PRIV_CRED_SETEGID:
3929 case PRIV_CRED_SETGROUPS:
3930 case PRIV_CRED_SETREUID:
3931 case PRIV_CRED_SETREGID:
3932 case PRIV_CRED_SETRESUID:
3933 case PRIV_CRED_SETRESGID:
3934
3935 /*
3936 * Jail implements visibility constraints already, so allow
3937 * jailed root to override uid/gid-based constraints.
3938 */
3939 case PRIV_SEEOTHERGIDS:
3940 case PRIV_SEEOTHERUIDS:
3941 case PRIV_SEEJAILPROC:
3942
3943 /*
3944 * Jail implements inter-process debugging limits already, so
3945 * allow jailed root various debugging privileges.
3946 */
3947 case PRIV_DEBUG_DIFFCRED:
3948 case PRIV_DEBUG_SUGID:
3949 case PRIV_DEBUG_UNPRIV:
3950
3951 /*
3952 * Allow jail to set various resource limits and login
3953 * properties, and for now, exceed process resource limits.
3954 */
3955 case PRIV_PROC_LIMIT:
3956 case PRIV_PROC_SETLOGIN:
3957 case PRIV_PROC_SETRLIMIT:
3958
3959 /*
3960 * System V and POSIX IPC privileges are granted in jail.
3961 */
3962 case PRIV_IPC_READ:
3963 case PRIV_IPC_WRITE:
3964 case PRIV_IPC_ADMIN:
3965 case PRIV_IPC_MSGSIZE:
3966 case PRIV_MQ_ADMIN:
3967
3968 /*
3969 * Jail operations within a jail work on child jails.
3970 */
3971 case PRIV_JAIL_ATTACH:
3972 case PRIV_JAIL_SET:
3973 case PRIV_JAIL_REMOVE:
3974
3975 /*
3976 * Jail implements its own inter-process limits, so allow
3977 * root processes in jail to change scheduling on other
3978 * processes in the same jail. Likewise for signalling.
3979 */
3980 case PRIV_SCHED_DIFFCRED:
3981 case PRIV_SCHED_CPUSET:
3982 case PRIV_SIGNAL_DIFFCRED:
3983 case PRIV_SIGNAL_SUGID:
3984
3985 /*
3986 * Allow jailed processes to write to sysctls marked as jail
3987 * writable.
3988 */
3989 case PRIV_SYSCTL_WRITEJAIL:
3990
3991 /*
3992 * Allow root in jail to manage a variety of quota
3993 * properties. These should likely be conditional on a
3994 * configuration option.
3995 */
3996 case PRIV_VFS_GETQUOTA:
3997 case PRIV_VFS_SETQUOTA:
3998
3999 /*
4000 * Since Jail relies on chroot() to implement file system
4001 * protections, grant many VFS privileges to root in jail.
4002 * Be careful to exclude mount-related and NFS-related
4003 * privileges.
4004 */
4005 case PRIV_VFS_READ:
4006 case PRIV_VFS_WRITE:
4007 case PRIV_VFS_ADMIN:
4008 case PRIV_VFS_EXEC:
4009 case PRIV_VFS_BLOCKRESERVE: /* XXXRW: Slightly surprising. */
4010 case PRIV_VFS_CHFLAGS_DEV:
4011 case PRIV_VFS_CHOWN:
4012 case PRIV_VFS_CHROOT:
4013 case PRIV_VFS_RETAINSUGID:
4014 case PRIV_VFS_FCHROOT:
4015 case PRIV_VFS_LINK:
4016 case PRIV_VFS_SETGID:
4017 case PRIV_VFS_STAT:
4018 case PRIV_VFS_STICKYFILE:
4019
4020 /*
4021 * As in the non-jail case, non-root users are expected to be
4022 * able to read kernel/physical memory (provided /dev/[k]mem
4023 * exists in the jail and they have permission to access it).
4024 */
4025 case PRIV_KMEM_READ:
4026 return (0);
4027
4028 /*
4029 * Depending on the global setting, allow privilege of
4030 * setting system flags.
4031 */
4032 case PRIV_VFS_SYSFLAGS:
4033 if (cred->cr_prison->pr_allow & PR_ALLOW_CHFLAGS)
4034 return (0);
4035 else
4036 return (EPERM);
4037
4038 /*
4039 * Depending on the global setting, allow privilege of
4040 * mounting/unmounting file systems.
4041 */
4042 case PRIV_VFS_MOUNT:
4043 case PRIV_VFS_UNMOUNT:
4044 case PRIV_VFS_MOUNT_NONUSER:
4045 case PRIV_VFS_MOUNT_OWNER:
4046 pr = cred->cr_prison;
4047 prison_lock(pr);
4048 if (pr->pr_allow & PR_ALLOW_MOUNT && pr->pr_enforce_statfs < 2)
4049 error = 0;
4050 else
4051 error = EPERM;
4052 prison_unlock(pr);
4053 return (error);
4054
4055 /*
4056 * Jails should hold no disposition on the PRIV_VFS_READ_DIR
4057 * policy. priv_check_cred will not specifically allow it, and
4058 * we may want a MAC policy to allow it.
4059 */
4060 case PRIV_VFS_READ_DIR:
4061 return (0);
4062
4063 /*
4064 * Conditionnaly allow locking (unlocking) physical pages
4065 * in memory.
4066 */
4067 case PRIV_VM_MLOCK:
4068 case PRIV_VM_MUNLOCK:
4069 if (cred->cr_prison->pr_allow & PR_ALLOW_MLOCK)
4070 return (0);
4071 else
4072 return (EPERM);
4073
4074 /*
4075 * Conditionally allow jailed root to bind reserved ports.
4076 */
4077 case PRIV_NETINET_RESERVEDPORT:
4078 if (cred->cr_prison->pr_allow & PR_ALLOW_RESERVED_PORTS)
4079 return (0);
4080 else
4081 return (EPERM);
4082
4083 /*
4084 * Allow jailed root to reuse in-use ports.
4085 */
4086 case PRIV_NETINET_REUSEPORT:
4087 return (0);
4088
4089 /*
4090 * Allow jailed root to set certain IPv4/6 (option) headers.
4091 */
4092 case PRIV_NETINET_SETHDROPTS:
4093 return (0);
4094
4095 /*
4096 * Conditionally allow creating raw sockets in jail.
4097 */
4098 case PRIV_NETINET_RAW:
4099 if (cred->cr_prison->pr_allow & PR_ALLOW_RAW_SOCKETS)
4100 return (0);
4101 else
4102 return (EPERM);
4103
4104 /*
4105 * Since jail implements its own visibility limits on netstat
4106 * sysctls, allow getcred. This allows identd to work in
4107 * jail.
4108 */
4109 case PRIV_NETINET_GETCRED:
4110 return (0);
4111
4112 /*
4113 * Allow jailed root to set loginclass.
4114 */
4115 case PRIV_PROC_SETLOGINCLASS:
4116 return (0);
4117
4118 /*
4119 * Do not allow a process inside a jail to read the kernel
4120 * message buffer unless explicitly permitted.
4121 */
4122 case PRIV_MSGBUF:
4123 if (cred->cr_prison->pr_allow & PR_ALLOW_READ_MSGBUF)
4124 return (0);
4125 return (EPERM);
4126
4127 default:
4128 /*
4129 * In all remaining cases, deny the privilege request. This
4130 * includes almost all network privileges, many system
4131 * configuration privileges.
4132 */
4133 return (EPERM);
4134 }
4135 }
4136
4137 /*
4138 * Return the part of pr2's name that is relative to pr1, or the whole name
4139 * if it does not directly follow.
4140 */
4141
4142 char *
prison_name(struct prison * pr1,struct prison * pr2)4143 prison_name(struct prison *pr1, struct prison *pr2)
4144 {
4145 char *name;
4146
4147 /* Jails see themselves as "0" (if they see themselves at all). */
4148 if (pr1 == pr2)
4149 return "0";
4150 name = pr2->pr_name;
4151 if (prison_ischild(pr1, pr2)) {
4152 /*
4153 * pr1 isn't locked (and allprison_lock may not be either)
4154 * so its length can't be counted on. But the number of dots
4155 * can be counted on - and counted.
4156 */
4157 for (; pr1 != &prison0; pr1 = pr1->pr_parent)
4158 name = strchr(name, '.') + 1;
4159 }
4160 return (name);
4161 }
4162
4163 /*
4164 * Return the part of pr2's path that is relative to pr1, or the whole path
4165 * if it does not directly follow.
4166 */
4167 static char *
prison_path(struct prison * pr1,struct prison * pr2)4168 prison_path(struct prison *pr1, struct prison *pr2)
4169 {
4170 char *path1, *path2;
4171 int len1;
4172
4173 path1 = pr1->pr_path;
4174 path2 = pr2->pr_path;
4175 if (!strcmp(path1, "/"))
4176 return (path2);
4177 len1 = strlen(path1);
4178 if (strncmp(path1, path2, len1))
4179 return (path2);
4180 if (path2[len1] == '\0')
4181 return "/";
4182 if (path2[len1] == '/')
4183 return (path2 + len1);
4184 return (path2);
4185 }
4186
4187 /*
4188 * Jail-related sysctls.
4189 */
4190 static SYSCTL_NODE(_security, OID_AUTO, jail, CTLFLAG_RW | CTLFLAG_MPSAFE, 0,
4191 "Jails");
4192
4193 #if defined(INET) || defined(INET6)
4194 /*
4195 * Copy address array to memory that would be then SYSCTL_OUT-ed.
4196 * sysctl_jail_list() helper.
4197 */
4198 static void
prison_ip_copyout(struct prison * pr,const pr_family_t af,void ** out,int * len)4199 prison_ip_copyout(struct prison *pr, const pr_family_t af, void **out, int *len)
4200 {
4201 const struct prison_ip *pip;
4202 const size_t size = pr_families[af].size;
4203
4204 again:
4205 mtx_assert(&pr->pr_mtx, MA_OWNED);
4206 if ((pip = pr->pr_addrs[af]) != NULL) {
4207 if (*len < pip->ips) {
4208 *len = pip->ips;
4209 mtx_unlock(&pr->pr_mtx);
4210 *out = realloc(*out, *len * size, M_TEMP, M_WAITOK);
4211 mtx_lock(&pr->pr_mtx);
4212 goto again;
4213 }
4214 bcopy(pip->pr_ip, *out, pip->ips * size);
4215 }
4216 }
4217 #endif
4218
4219 static int
sysctl_jail_list(SYSCTL_HANDLER_ARGS)4220 sysctl_jail_list(SYSCTL_HANDLER_ARGS)
4221 {
4222 struct xprison *xp;
4223 struct prison *pr, *cpr;
4224 #ifdef INET
4225 struct in_addr *ip4 = NULL;
4226 int ip4s = 0;
4227 #endif
4228 #ifdef INET6
4229 struct in6_addr *ip6 = NULL;
4230 int ip6s = 0;
4231 #endif
4232 int descend, error;
4233
4234 xp = malloc(sizeof(*xp), M_TEMP, M_WAITOK);
4235 pr = req->td->td_ucred->cr_prison;
4236 error = 0;
4237 sx_slock(&allprison_lock);
4238 FOREACH_PRISON_DESCENDANT(pr, cpr, descend) {
4239 mtx_lock(&cpr->pr_mtx);
4240 #ifdef INET
4241 prison_ip_copyout(cpr, PR_INET, (void **)&ip4, &ip4s);
4242 #endif
4243 #ifdef INET6
4244 prison_ip_copyout(cpr, PR_INET6, (void **)&ip6, &ip6s);
4245 #endif
4246 bzero(xp, sizeof(*xp));
4247 xp->pr_version = XPRISON_VERSION;
4248 xp->pr_id = cpr->pr_id;
4249 xp->pr_state = cpr->pr_state;
4250 strlcpy(xp->pr_path, prison_path(pr, cpr), sizeof(xp->pr_path));
4251 strlcpy(xp->pr_host, cpr->pr_hostname, sizeof(xp->pr_host));
4252 strlcpy(xp->pr_name, prison_name(pr, cpr), sizeof(xp->pr_name));
4253 #ifdef INET
4254 xp->pr_ip4s = ip4s;
4255 #endif
4256 #ifdef INET6
4257 xp->pr_ip6s = ip6s;
4258 #endif
4259 mtx_unlock(&cpr->pr_mtx);
4260 error = SYSCTL_OUT(req, xp, sizeof(*xp));
4261 if (error)
4262 break;
4263 #ifdef INET
4264 if (xp->pr_ip4s > 0) {
4265 error = SYSCTL_OUT(req, ip4,
4266 xp->pr_ip4s * sizeof(struct in_addr));
4267 if (error)
4268 break;
4269 }
4270 #endif
4271 #ifdef INET6
4272 if (xp->pr_ip6s > 0) {
4273 error = SYSCTL_OUT(req, ip6,
4274 xp->pr_ip6s * sizeof(struct in6_addr));
4275 if (error)
4276 break;
4277 }
4278 #endif
4279 }
4280 sx_sunlock(&allprison_lock);
4281 free(xp, M_TEMP);
4282 #ifdef INET
4283 free(ip4, M_TEMP);
4284 #endif
4285 #ifdef INET6
4286 free(ip6, M_TEMP);
4287 #endif
4288 return (error);
4289 }
4290
4291 SYSCTL_OID(_security_jail, OID_AUTO, list,
4292 CTLTYPE_STRUCT | CTLFLAG_RD | CTLFLAG_MPSAFE, NULL, 0,
4293 sysctl_jail_list, "S", "List of active jails");
4294
4295 static int
sysctl_jail_jailed(SYSCTL_HANDLER_ARGS)4296 sysctl_jail_jailed(SYSCTL_HANDLER_ARGS)
4297 {
4298 int error, injail;
4299
4300 injail = jailed(req->td->td_ucred);
4301 error = SYSCTL_OUT(req, &injail, sizeof(injail));
4302
4303 return (error);
4304 }
4305
4306 SYSCTL_PROC(_security_jail, OID_AUTO, jailed,
4307 CTLTYPE_INT | CTLFLAG_RD | CTLFLAG_MPSAFE, NULL, 0,
4308 sysctl_jail_jailed, "I", "Process in jail?");
4309
4310 static int
sysctl_jail_vnet(SYSCTL_HANDLER_ARGS)4311 sysctl_jail_vnet(SYSCTL_HANDLER_ARGS)
4312 {
4313 int error, havevnet;
4314 #ifdef VIMAGE
4315 struct ucred *cred = req->td->td_ucred;
4316
4317 havevnet = jailed(cred) && prison_owns_vnet(cred);
4318 #else
4319 havevnet = 0;
4320 #endif
4321 error = SYSCTL_OUT(req, &havevnet, sizeof(havevnet));
4322
4323 return (error);
4324 }
4325
4326 SYSCTL_PROC(_security_jail, OID_AUTO, vnet,
4327 CTLTYPE_INT | CTLFLAG_RD | CTLFLAG_MPSAFE, NULL, 0,
4328 sysctl_jail_vnet, "I", "Jail owns vnet?");
4329
4330 #if defined(INET) || defined(INET6)
4331 SYSCTL_UINT(_security_jail, OID_AUTO, jail_max_af_ips, CTLFLAG_RW,
4332 &jail_max_af_ips, 0,
4333 "Number of IP addresses a jail may have at most per address family (deprecated)");
4334 #endif
4335
4336 /*
4337 * Default parameters for jail(2) compatibility. For historical reasons,
4338 * the sysctl names have varying similarity to the parameter names. Prisons
4339 * just see their own parameters, and can't change them.
4340 */
4341 static int
sysctl_jail_default_allow(SYSCTL_HANDLER_ARGS)4342 sysctl_jail_default_allow(SYSCTL_HANDLER_ARGS)
4343 {
4344 int error, i;
4345
4346 /* Get the current flag value, and convert it to a boolean. */
4347 if (req->td->td_ucred->cr_prison == &prison0) {
4348 mtx_lock(&prison0.pr_mtx);
4349 i = (jail_default_allow & arg2) != 0;
4350 mtx_unlock(&prison0.pr_mtx);
4351 } else
4352 i = prison_allow(req->td->td_ucred, arg2);
4353
4354 if (arg1 != NULL)
4355 i = !i;
4356 error = sysctl_handle_int(oidp, &i, 0, req);
4357 if (error || !req->newptr)
4358 return (error);
4359 i = i ? arg2 : 0;
4360 if (arg1 != NULL)
4361 i ^= arg2;
4362 /*
4363 * The sysctls don't have CTLFLAGS_PRISON, so assume prison0
4364 * for writing.
4365 */
4366 mtx_lock(&prison0.pr_mtx);
4367 jail_default_allow = (jail_default_allow & ~arg2) | i;
4368 mtx_unlock(&prison0.pr_mtx);
4369 return (0);
4370 }
4371
4372 SYSCTL_PROC(_security_jail, OID_AUTO, set_hostname_allowed,
4373 CTLTYPE_INT | CTLFLAG_RW | CTLFLAG_MPSAFE,
4374 NULL, PR_ALLOW_SET_HOSTNAME, sysctl_jail_default_allow, "I",
4375 "Processes in jail can set their hostnames (deprecated)");
4376 SYSCTL_PROC(_security_jail, OID_AUTO, socket_unixiproute_only,
4377 CTLTYPE_INT | CTLFLAG_RW | CTLFLAG_MPSAFE,
4378 (void *)1, PR_ALLOW_SOCKET_AF, sysctl_jail_default_allow, "I",
4379 "Processes in jail are limited to creating UNIX/IP/route sockets only (deprecated)");
4380 SYSCTL_PROC(_security_jail, OID_AUTO, sysvipc_allowed,
4381 CTLTYPE_INT | CTLFLAG_RW | CTLFLAG_MPSAFE,
4382 NULL, PR_ALLOW_SYSVIPC, sysctl_jail_default_allow, "I",
4383 "Processes in jail can use System V IPC primitives (deprecated)");
4384 SYSCTL_PROC(_security_jail, OID_AUTO, allow_raw_sockets,
4385 CTLTYPE_INT | CTLFLAG_RW | CTLFLAG_MPSAFE,
4386 NULL, PR_ALLOW_RAW_SOCKETS, sysctl_jail_default_allow, "I",
4387 "Prison root can create raw sockets (deprecated)");
4388 SYSCTL_PROC(_security_jail, OID_AUTO, chflags_allowed,
4389 CTLTYPE_INT | CTLFLAG_RW | CTLFLAG_MPSAFE,
4390 NULL, PR_ALLOW_CHFLAGS, sysctl_jail_default_allow, "I",
4391 "Processes in jail can alter system file flags (deprecated)");
4392 SYSCTL_PROC(_security_jail, OID_AUTO, mount_allowed,
4393 CTLTYPE_INT | CTLFLAG_RW | CTLFLAG_MPSAFE,
4394 NULL, PR_ALLOW_MOUNT, sysctl_jail_default_allow, "I",
4395 "Processes in jail can mount/unmount jail-friendly file systems (deprecated)");
4396 SYSCTL_PROC(_security_jail, OID_AUTO, mlock_allowed,
4397 CTLTYPE_INT | CTLFLAG_RW | CTLFLAG_MPSAFE,
4398 NULL, PR_ALLOW_MLOCK, sysctl_jail_default_allow, "I",
4399 "Processes in jail can lock/unlock physical pages in memory");
4400
4401 static int
sysctl_jail_default_level(SYSCTL_HANDLER_ARGS)4402 sysctl_jail_default_level(SYSCTL_HANDLER_ARGS)
4403 {
4404 struct prison *pr;
4405 int level, error;
4406
4407 pr = req->td->td_ucred->cr_prison;
4408 level = (pr == &prison0) ? *(int *)arg1 : *(int *)((char *)pr + arg2);
4409 error = sysctl_handle_int(oidp, &level, 0, req);
4410 if (error || !req->newptr)
4411 return (error);
4412 *(int *)arg1 = level;
4413 return (0);
4414 }
4415
4416 SYSCTL_PROC(_security_jail, OID_AUTO, enforce_statfs,
4417 CTLTYPE_INT | CTLFLAG_RW | CTLFLAG_MPSAFE,
4418 &jail_default_enforce_statfs, offsetof(struct prison, pr_enforce_statfs),
4419 sysctl_jail_default_level, "I",
4420 "Processes in jail cannot see all mounted file systems (deprecated)");
4421
4422 SYSCTL_PROC(_security_jail, OID_AUTO, devfs_ruleset,
4423 CTLTYPE_INT | CTLFLAG_RD | CTLFLAG_MPSAFE,
4424 &jail_default_devfs_rsnum, offsetof(struct prison, pr_devfs_rsnum),
4425 sysctl_jail_default_level, "I",
4426 "Ruleset for the devfs filesystem in jail (deprecated)");
4427
4428 SYSCTL_NODE(_security_jail, OID_AUTO, children, CTLFLAG_RW | CTLFLAG_MPSAFE, 0,
4429 "Limits and stats of child jails");
4430
4431 static int
sysctl_jail_children(SYSCTL_HANDLER_ARGS)4432 sysctl_jail_children(SYSCTL_HANDLER_ARGS)
4433 {
4434 struct prison *pr;
4435 int i;
4436
4437 pr = req->td->td_ucred->cr_prison;
4438
4439 switch (oidp->oid_kind & CTLTYPE) {
4440 case CTLTYPE_INT:
4441 i = *(int *)((char *)pr + arg2);
4442 return (SYSCTL_OUT(req, &i, sizeof(i)));
4443 }
4444
4445 return (0);
4446 }
4447
4448 SYSCTL_PROC(_security_jail_children, OID_AUTO, max,
4449 CTLTYPE_INT | CTLFLAG_RD | CTLFLAG_MPSAFE,
4450 NULL, offsetof(struct prison, pr_childmax), sysctl_jail_children,
4451 "I", "Maximum number of child jails");
4452 SYSCTL_PROC(_security_jail_children, OID_AUTO, cur,
4453 CTLTYPE_INT | CTLFLAG_RD | CTLFLAG_MPSAFE,
4454 NULL, offsetof(struct prison, pr_childcount), sysctl_jail_children,
4455 "I", "Current number of child jails");
4456
4457 /*
4458 * Nodes to describe jail parameters. Maximum length of string parameters
4459 * is returned in the string itself, and the other parameters exist merely
4460 * to make themselves and their types known.
4461 */
4462 SYSCTL_NODE(_security_jail, OID_AUTO, param, CTLFLAG_RW | CTLFLAG_MPSAFE, 0,
4463 "Jail parameters");
4464
4465 int
sysctl_jail_param(SYSCTL_HANDLER_ARGS)4466 sysctl_jail_param(SYSCTL_HANDLER_ARGS)
4467 {
4468 int i;
4469 long l;
4470 size_t s;
4471 char numbuf[12];
4472
4473 switch (oidp->oid_kind & CTLTYPE)
4474 {
4475 case CTLTYPE_LONG:
4476 case CTLTYPE_ULONG:
4477 l = 0;
4478 #ifdef SCTL_MASK32
4479 if (!(req->flags & SCTL_MASK32))
4480 #endif
4481 return (SYSCTL_OUT(req, &l, sizeof(l)));
4482 case CTLTYPE_INT:
4483 case CTLTYPE_UINT:
4484 i = 0;
4485 return (SYSCTL_OUT(req, &i, sizeof(i)));
4486 case CTLTYPE_STRING:
4487 snprintf(numbuf, sizeof(numbuf), "%jd", (intmax_t)arg2);
4488 return
4489 (sysctl_handle_string(oidp, numbuf, sizeof(numbuf), req));
4490 case CTLTYPE_STRUCT:
4491 s = (size_t)arg2;
4492 return (SYSCTL_OUT(req, &s, sizeof(s)));
4493 }
4494 return (0);
4495 }
4496
4497 /*
4498 * CTLFLAG_RDTUN in the following indicates jail parameters that can be set at
4499 * jail creation time but cannot be changed in an existing jail.
4500 */
4501 SYSCTL_JAIL_PARAM(, jid, CTLTYPE_INT | CTLFLAG_RDTUN, "I", "Jail ID");
4502 SYSCTL_JAIL_PARAM(, parent, CTLTYPE_INT | CTLFLAG_RD, "I", "Jail parent ID");
4503 SYSCTL_JAIL_PARAM_STRING(, name, CTLFLAG_RW, MAXHOSTNAMELEN, "Jail name");
4504 SYSCTL_JAIL_PARAM_STRING(, path, CTLFLAG_RDTUN, MAXPATHLEN, "Jail root path");
4505 SYSCTL_JAIL_PARAM(, securelevel, CTLTYPE_INT | CTLFLAG_RW,
4506 "I", "Jail secure level");
4507 SYSCTL_JAIL_PARAM(, osreldate, CTLTYPE_INT | CTLFLAG_RDTUN, "I",
4508 "Jail value for kern.osreldate and uname -K");
4509 SYSCTL_JAIL_PARAM_STRING(, osrelease, CTLFLAG_RDTUN, OSRELEASELEN,
4510 "Jail value for kern.osrelease and uname -r");
4511 SYSCTL_JAIL_PARAM(, enforce_statfs, CTLTYPE_INT | CTLFLAG_RW,
4512 "I", "Jail cannot see all mounted file systems");
4513 SYSCTL_JAIL_PARAM(, devfs_ruleset, CTLTYPE_INT | CTLFLAG_RW,
4514 "I", "Ruleset for in-jail devfs mounts");
4515 SYSCTL_JAIL_PARAM(, persist, CTLTYPE_INT | CTLFLAG_RW,
4516 "B", "Jail persistence");
4517 #ifdef VIMAGE
4518 SYSCTL_JAIL_PARAM(, vnet, CTLTYPE_INT | CTLFLAG_RDTUN,
4519 "E,jailsys", "Virtual network stack");
4520 #endif
4521 SYSCTL_JAIL_PARAM(, dying, CTLTYPE_INT | CTLFLAG_RD,
4522 "B", "Jail is in the process of shutting down");
4523
4524 SYSCTL_JAIL_PARAM_NODE(children, "Number of child jails");
4525 SYSCTL_JAIL_PARAM(_children, cur, CTLTYPE_INT | CTLFLAG_RD,
4526 "I", "Current number of child jails");
4527 SYSCTL_JAIL_PARAM(_children, max, CTLTYPE_INT | CTLFLAG_RW,
4528 "I", "Maximum number of child jails");
4529
4530 SYSCTL_JAIL_PARAM_SYS_NODE(host, CTLFLAG_RW, "Jail host info");
4531 SYSCTL_JAIL_PARAM_STRING(_host, hostname, CTLFLAG_RW, MAXHOSTNAMELEN,
4532 "Jail hostname");
4533 SYSCTL_JAIL_PARAM_STRING(_host, domainname, CTLFLAG_RW, MAXHOSTNAMELEN,
4534 "Jail NIS domainname");
4535 SYSCTL_JAIL_PARAM_STRING(_host, hostuuid, CTLFLAG_RW, HOSTUUIDLEN,
4536 "Jail host UUID");
4537 SYSCTL_JAIL_PARAM(_host, hostid, CTLTYPE_ULONG | CTLFLAG_RW,
4538 "LU", "Jail host ID");
4539
4540 SYSCTL_JAIL_PARAM_NODE(cpuset, "Jail cpuset");
4541 SYSCTL_JAIL_PARAM(_cpuset, id, CTLTYPE_INT | CTLFLAG_RD, "I", "Jail cpuset ID");
4542
4543 #ifdef INET
4544 SYSCTL_JAIL_PARAM_SYS_NODE(ip4, CTLFLAG_RDTUN,
4545 "Jail IPv4 address virtualization");
4546 SYSCTL_JAIL_PARAM_STRUCT(_ip4, addr, CTLFLAG_RW, sizeof(struct in_addr),
4547 "S,in_addr,a", "Jail IPv4 addresses");
4548 SYSCTL_JAIL_PARAM(_ip4, saddrsel, CTLTYPE_INT | CTLFLAG_RW,
4549 "B", "Do (not) use IPv4 source address selection rather than the "
4550 "primary jail IPv4 address.");
4551 #endif
4552 #ifdef INET6
4553 SYSCTL_JAIL_PARAM_SYS_NODE(ip6, CTLFLAG_RDTUN,
4554 "Jail IPv6 address virtualization");
4555 SYSCTL_JAIL_PARAM_STRUCT(_ip6, addr, CTLFLAG_RW, sizeof(struct in6_addr),
4556 "S,in6_addr,a", "Jail IPv6 addresses");
4557 SYSCTL_JAIL_PARAM(_ip6, saddrsel, CTLTYPE_INT | CTLFLAG_RW,
4558 "B", "Do (not) use IPv6 source address selection rather than the "
4559 "primary jail IPv6 address.");
4560 #endif
4561
4562 SYSCTL_JAIL_PARAM_NODE(allow, "Jail permission flags");
4563 SYSCTL_JAIL_PARAM(_allow, set_hostname, CTLTYPE_INT | CTLFLAG_RW,
4564 "B", "Jail may set hostname");
4565 SYSCTL_JAIL_PARAM(_allow, sysvipc, CTLTYPE_INT | CTLFLAG_RW,
4566 "B", "Jail may use SYSV IPC");
4567 SYSCTL_JAIL_PARAM(_allow, raw_sockets, CTLTYPE_INT | CTLFLAG_RW,
4568 "B", "Jail may create raw sockets");
4569 SYSCTL_JAIL_PARAM(_allow, chflags, CTLTYPE_INT | CTLFLAG_RW,
4570 "B", "Jail may alter system file flags");
4571 SYSCTL_JAIL_PARAM(_allow, quotas, CTLTYPE_INT | CTLFLAG_RW,
4572 "B", "Jail may set file quotas");
4573 SYSCTL_JAIL_PARAM(_allow, socket_af, CTLTYPE_INT | CTLFLAG_RW,
4574 "B", "Jail may create sockets other than just UNIX/IPv4/IPv6/route");
4575 SYSCTL_JAIL_PARAM(_allow, mlock, CTLTYPE_INT | CTLFLAG_RW,
4576 "B", "Jail may lock (unlock) physical pages in memory");
4577 SYSCTL_JAIL_PARAM(_allow, reserved_ports, CTLTYPE_INT | CTLFLAG_RW,
4578 "B", "Jail may bind sockets to reserved ports");
4579 SYSCTL_JAIL_PARAM(_allow, read_msgbuf, CTLTYPE_INT | CTLFLAG_RW,
4580 "B", "Jail may read the kernel message buffer");
4581 SYSCTL_JAIL_PARAM(_allow, unprivileged_proc_debug, CTLTYPE_INT | CTLFLAG_RW,
4582 "B", "Unprivileged processes may use process debugging facilities");
4583 SYSCTL_JAIL_PARAM(_allow, suser, CTLTYPE_INT | CTLFLAG_RW,
4584 "B", "Processes in jail with uid 0 have privilege");
4585 #ifdef VIMAGE
4586 SYSCTL_JAIL_PARAM(_allow, nfsd, CTLTYPE_INT | CTLFLAG_RW,
4587 "B", "Mountd/nfsd may run in the jail");
4588 #endif
4589
4590 SYSCTL_JAIL_PARAM_SUBNODE(allow, mount, "Jail mount/unmount permission flags");
4591 SYSCTL_JAIL_PARAM(_allow_mount, , CTLTYPE_INT | CTLFLAG_RW,
4592 "B", "Jail may mount/unmount jail-friendly file systems in general");
4593
4594 /*
4595 * Add a dynamic parameter allow.<name>, or allow.<prefix>.<name>. Return
4596 * its associated bit in the pr_allow bitmask, or zero if the parameter was
4597 * not created.
4598 */
4599 unsigned
prison_add_allow(const char * prefix,const char * name,const char * prefix_descr,const char * descr)4600 prison_add_allow(const char *prefix, const char *name, const char *prefix_descr,
4601 const char *descr)
4602 {
4603 struct bool_flags *bf;
4604 struct sysctl_oid *parent;
4605 char *allow_name, *allow_noname, *allowed;
4606 #ifndef NO_SYSCTL_DESCR
4607 char *descr_deprecated;
4608 #endif
4609 u_int allow_flag;
4610
4611 if (prefix
4612 ? asprintf(&allow_name, M_PRISON, "allow.%s.%s", prefix, name)
4613 < 0 ||
4614 asprintf(&allow_noname, M_PRISON, "allow.%s.no%s", prefix, name)
4615 < 0
4616 : asprintf(&allow_name, M_PRISON, "allow.%s", name) < 0 ||
4617 asprintf(&allow_noname, M_PRISON, "allow.no%s", name) < 0) {
4618 free(allow_name, M_PRISON);
4619 return 0;
4620 }
4621
4622 /*
4623 * See if this parameter has already beed added, i.e. a module was
4624 * previously loaded/unloaded.
4625 */
4626 mtx_lock(&prison0.pr_mtx);
4627 for (bf = pr_flag_allow;
4628 bf < pr_flag_allow + nitems(pr_flag_allow) &&
4629 atomic_load_int(&bf->flag) != 0;
4630 bf++) {
4631 if (strcmp(bf->name, allow_name) == 0) {
4632 allow_flag = bf->flag;
4633 goto no_add;
4634 }
4635 }
4636
4637 /*
4638 * Find a free bit in pr_allow_all, failing if there are none
4639 * (which shouldn't happen as long as we keep track of how many
4640 * potential dynamic flags exist).
4641 */
4642 for (allow_flag = 1;; allow_flag <<= 1) {
4643 if (allow_flag == 0)
4644 goto no_add;
4645 if ((pr_allow_all & allow_flag) == 0)
4646 break;
4647 }
4648
4649 /* Note the parameter in the next open slot in pr_flag_allow. */
4650 for (bf = pr_flag_allow; ; bf++) {
4651 if (bf == pr_flag_allow + nitems(pr_flag_allow)) {
4652 /* This should never happen, but is not fatal. */
4653 allow_flag = 0;
4654 goto no_add;
4655 }
4656 if (atomic_load_int(&bf->flag) == 0)
4657 break;
4658 }
4659 bf->name = allow_name;
4660 bf->noname = allow_noname;
4661 pr_allow_all |= allow_flag;
4662 /*
4663 * prison0 always has permission for the new parameter.
4664 * Other jails must have it granted to them.
4665 */
4666 prison0.pr_allow |= allow_flag;
4667 /* The flag indicates a valid entry, so make sure it is set last. */
4668 atomic_store_rel_int(&bf->flag, allow_flag);
4669 mtx_unlock(&prison0.pr_mtx);
4670
4671 /*
4672 * Create sysctls for the parameter, and the back-compat global
4673 * permission.
4674 */
4675 parent = prefix
4676 ? SYSCTL_ADD_NODE(NULL,
4677 SYSCTL_CHILDREN(&sysctl___security_jail_param_allow),
4678 OID_AUTO, prefix, CTLFLAG_MPSAFE, 0, prefix_descr)
4679 : &sysctl___security_jail_param_allow;
4680 (void)SYSCTL_ADD_PROC(NULL, SYSCTL_CHILDREN(parent), OID_AUTO,
4681 name, CTLTYPE_INT | CTLFLAG_RW | CTLFLAG_MPSAFE,
4682 NULL, 0, sysctl_jail_param, "B", descr);
4683 if ((prefix
4684 ? asprintf(&allowed, M_TEMP, "%s_%s_allowed", prefix, name)
4685 : asprintf(&allowed, M_TEMP, "%s_allowed", name)) >= 0) {
4686 #ifndef NO_SYSCTL_DESCR
4687 (void)asprintf(&descr_deprecated, M_TEMP, "%s (deprecated)",
4688 descr);
4689 #endif
4690 (void)SYSCTL_ADD_PROC(NULL,
4691 SYSCTL_CHILDREN(&sysctl___security_jail), OID_AUTO, allowed,
4692 CTLTYPE_INT | CTLFLAG_RW | CTLFLAG_MPSAFE, NULL, allow_flag,
4693 sysctl_jail_default_allow, "I", descr_deprecated);
4694 #ifndef NO_SYSCTL_DESCR
4695 free(descr_deprecated, M_TEMP);
4696 #endif
4697 free(allowed, M_TEMP);
4698 }
4699 return allow_flag;
4700
4701 no_add:
4702 mtx_unlock(&prison0.pr_mtx);
4703 free(allow_name, M_PRISON);
4704 free(allow_noname, M_PRISON);
4705 return allow_flag;
4706 }
4707
4708 /*
4709 * The VFS system will register jail-aware filesystems here. They each get
4710 * a parameter allow.mount.xxxfs and a flag to check when a jailed user
4711 * attempts to mount.
4712 */
4713 void
prison_add_vfs(struct vfsconf * vfsp)4714 prison_add_vfs(struct vfsconf *vfsp)
4715 {
4716 #ifdef NO_SYSCTL_DESCR
4717
4718 vfsp->vfc_prison_flag = prison_add_allow("mount", vfsp->vfc_name,
4719 NULL, NULL);
4720 #else
4721 char *descr;
4722
4723 (void)asprintf(&descr, M_TEMP, "Jail may mount the %s file system",
4724 vfsp->vfc_name);
4725 vfsp->vfc_prison_flag = prison_add_allow("mount", vfsp->vfc_name,
4726 NULL, descr);
4727 free(descr, M_TEMP);
4728 #endif
4729 }
4730
4731 #ifdef RACCT
4732 void
prison_racct_foreach(void (* callback)(struct racct * racct,void * arg2,void * arg3),void (* pre)(void),void (* post)(void),void * arg2,void * arg3)4733 prison_racct_foreach(void (*callback)(struct racct *racct,
4734 void *arg2, void *arg3), void (*pre)(void), void (*post)(void),
4735 void *arg2, void *arg3)
4736 {
4737 struct prison_racct *prr;
4738
4739 ASSERT_RACCT_ENABLED();
4740
4741 sx_slock(&allprison_lock);
4742 if (pre != NULL)
4743 (pre)();
4744 LIST_FOREACH(prr, &allprison_racct, prr_next)
4745 (callback)(prr->prr_racct, arg2, arg3);
4746 if (post != NULL)
4747 (post)();
4748 sx_sunlock(&allprison_lock);
4749 }
4750
4751 static struct prison_racct *
prison_racct_find_locked(const char * name)4752 prison_racct_find_locked(const char *name)
4753 {
4754 struct prison_racct *prr;
4755
4756 ASSERT_RACCT_ENABLED();
4757 sx_assert(&allprison_lock, SA_XLOCKED);
4758
4759 if (name[0] == '\0' || strlen(name) >= MAXHOSTNAMELEN)
4760 return (NULL);
4761
4762 LIST_FOREACH(prr, &allprison_racct, prr_next) {
4763 if (strcmp(name, prr->prr_name) != 0)
4764 continue;
4765
4766 /* Found prison_racct with a matching name? */
4767 prison_racct_hold(prr);
4768 return (prr);
4769 }
4770
4771 /* Add new prison_racct. */
4772 prr = malloc(sizeof(*prr), M_PRISON_RACCT, M_ZERO | M_WAITOK);
4773 racct_create(&prr->prr_racct);
4774
4775 strcpy(prr->prr_name, name);
4776 refcount_init(&prr->prr_refcount, 1);
4777 LIST_INSERT_HEAD(&allprison_racct, prr, prr_next);
4778
4779 return (prr);
4780 }
4781
4782 struct prison_racct *
prison_racct_find(const char * name)4783 prison_racct_find(const char *name)
4784 {
4785 struct prison_racct *prr;
4786
4787 ASSERT_RACCT_ENABLED();
4788
4789 sx_xlock(&allprison_lock);
4790 prr = prison_racct_find_locked(name);
4791 sx_xunlock(&allprison_lock);
4792 return (prr);
4793 }
4794
4795 void
prison_racct_hold(struct prison_racct * prr)4796 prison_racct_hold(struct prison_racct *prr)
4797 {
4798
4799 ASSERT_RACCT_ENABLED();
4800
4801 refcount_acquire(&prr->prr_refcount);
4802 }
4803
4804 static void
prison_racct_free_locked(struct prison_racct * prr)4805 prison_racct_free_locked(struct prison_racct *prr)
4806 {
4807
4808 ASSERT_RACCT_ENABLED();
4809 sx_assert(&allprison_lock, SA_XLOCKED);
4810
4811 if (refcount_release(&prr->prr_refcount)) {
4812 racct_destroy(&prr->prr_racct);
4813 LIST_REMOVE(prr, prr_next);
4814 free(prr, M_PRISON_RACCT);
4815 }
4816 }
4817
4818 void
prison_racct_free(struct prison_racct * prr)4819 prison_racct_free(struct prison_racct *prr)
4820 {
4821
4822 ASSERT_RACCT_ENABLED();
4823 sx_assert(&allprison_lock, SA_UNLOCKED);
4824
4825 if (refcount_release_if_not_last(&prr->prr_refcount))
4826 return;
4827
4828 sx_xlock(&allprison_lock);
4829 prison_racct_free_locked(prr);
4830 sx_xunlock(&allprison_lock);
4831 }
4832
4833 static void
prison_racct_attach(struct prison * pr)4834 prison_racct_attach(struct prison *pr)
4835 {
4836 struct prison_racct *prr;
4837
4838 ASSERT_RACCT_ENABLED();
4839 sx_assert(&allprison_lock, SA_XLOCKED);
4840
4841 prr = prison_racct_find_locked(pr->pr_name);
4842 KASSERT(prr != NULL, ("cannot find prison_racct"));
4843
4844 pr->pr_prison_racct = prr;
4845 }
4846
4847 /*
4848 * Handle jail renaming. From the racct point of view, renaming means
4849 * moving from one prison_racct to another.
4850 */
4851 static void
prison_racct_modify(struct prison * pr)4852 prison_racct_modify(struct prison *pr)
4853 {
4854 #ifdef RCTL
4855 struct proc *p;
4856 struct ucred *cred;
4857 #endif
4858 struct prison_racct *oldprr;
4859
4860 ASSERT_RACCT_ENABLED();
4861
4862 sx_slock(&allproc_lock);
4863 sx_xlock(&allprison_lock);
4864
4865 if (strcmp(pr->pr_name, pr->pr_prison_racct->prr_name) == 0) {
4866 sx_xunlock(&allprison_lock);
4867 sx_sunlock(&allproc_lock);
4868 return;
4869 }
4870
4871 oldprr = pr->pr_prison_racct;
4872 pr->pr_prison_racct = NULL;
4873
4874 prison_racct_attach(pr);
4875
4876 /*
4877 * Move resource utilisation records.
4878 */
4879 racct_move(pr->pr_prison_racct->prr_racct, oldprr->prr_racct);
4880
4881 #ifdef RCTL
4882 /*
4883 * Force rctl to reattach rules to processes.
4884 */
4885 FOREACH_PROC_IN_SYSTEM(p) {
4886 PROC_LOCK(p);
4887 cred = crhold(p->p_ucred);
4888 PROC_UNLOCK(p);
4889 rctl_proc_ucred_changed(p, cred);
4890 crfree(cred);
4891 }
4892 #endif
4893
4894 sx_sunlock(&allproc_lock);
4895 prison_racct_free_locked(oldprr);
4896 sx_xunlock(&allprison_lock);
4897 }
4898
4899 static void
prison_racct_detach(struct prison * pr)4900 prison_racct_detach(struct prison *pr)
4901 {
4902
4903 ASSERT_RACCT_ENABLED();
4904 sx_assert(&allprison_lock, SA_UNLOCKED);
4905
4906 if (pr->pr_prison_racct == NULL)
4907 return;
4908 prison_racct_free(pr->pr_prison_racct);
4909 pr->pr_prison_racct = NULL;
4910 }
4911 #endif /* RACCT */
4912
4913 #ifdef DDB
4914
4915 static void
db_show_prison(struct prison * pr)4916 db_show_prison(struct prison *pr)
4917 {
4918 struct bool_flags *bf;
4919 struct jailsys_flags *jsf;
4920 #if defined(INET) || defined(INET6)
4921 int ii;
4922 struct prison_ip *pip;
4923 #endif
4924 unsigned f;
4925 #ifdef INET
4926 char ip4buf[INET_ADDRSTRLEN];
4927 #endif
4928 #ifdef INET6
4929 char ip6buf[INET6_ADDRSTRLEN];
4930 #endif
4931
4932 db_printf("prison %p:\n", pr);
4933 db_printf(" jid = %d\n", pr->pr_id);
4934 db_printf(" name = %s\n", pr->pr_name);
4935 db_printf(" parent = %p\n", pr->pr_parent);
4936 db_printf(" ref = %d\n", pr->pr_ref);
4937 db_printf(" uref = %d\n", pr->pr_uref);
4938 db_printf(" state = %s\n",
4939 pr->pr_state == PRISON_STATE_ALIVE ? "alive" :
4940 pr->pr_state == PRISON_STATE_DYING ? "dying" :
4941 "invalid");
4942 db_printf(" path = %s\n", pr->pr_path);
4943 db_printf(" cpuset = %d\n", pr->pr_cpuset
4944 ? pr->pr_cpuset->cs_id : -1);
4945 #ifdef VIMAGE
4946 db_printf(" vnet = %p\n", pr->pr_vnet);
4947 #endif
4948 db_printf(" root = %p\n", pr->pr_root);
4949 db_printf(" securelevel = %d\n", pr->pr_securelevel);
4950 db_printf(" devfs_rsnum = %d\n", pr->pr_devfs_rsnum);
4951 db_printf(" children.max = %d\n", pr->pr_childmax);
4952 db_printf(" children.cur = %d\n", pr->pr_childcount);
4953 db_printf(" child = %p\n", LIST_FIRST(&pr->pr_children));
4954 db_printf(" sibling = %p\n", LIST_NEXT(pr, pr_sibling));
4955 db_printf(" flags = 0x%x", pr->pr_flags);
4956 for (bf = pr_flag_bool; bf < pr_flag_bool + nitems(pr_flag_bool); bf++)
4957 if (pr->pr_flags & bf->flag)
4958 db_printf(" %s", bf->name);
4959 for (jsf = pr_flag_jailsys;
4960 jsf < pr_flag_jailsys + nitems(pr_flag_jailsys);
4961 jsf++) {
4962 f = pr->pr_flags & (jsf->disable | jsf->new);
4963 db_printf(" %-16s= %s\n", jsf->name,
4964 (f != 0 && f == jsf->disable) ? "disable"
4965 : (f == jsf->new) ? "new"
4966 : "inherit");
4967 }
4968 db_printf(" allow = 0x%x", pr->pr_allow);
4969 for (bf = pr_flag_allow;
4970 bf < pr_flag_allow + nitems(pr_flag_allow) &&
4971 atomic_load_int(&bf->flag) != 0;
4972 bf++)
4973 if (pr->pr_allow & bf->flag)
4974 db_printf(" %s", bf->name);
4975 db_printf("\n");
4976 db_printf(" enforce_statfs = %d\n", pr->pr_enforce_statfs);
4977 db_printf(" host.hostname = %s\n", pr->pr_hostname);
4978 db_printf(" host.domainname = %s\n", pr->pr_domainname);
4979 db_printf(" host.hostuuid = %s\n", pr->pr_hostuuid);
4980 db_printf(" host.hostid = %lu\n", pr->pr_hostid);
4981 #ifdef INET
4982 if ((pip = pr->pr_addrs[PR_INET]) != NULL) {
4983 db_printf(" ip4s = %d\n", pip->ips);
4984 for (ii = 0; ii < pip->ips; ii++)
4985 db_printf(" %s %s\n",
4986 ii == 0 ? "ip4.addr =" : " ",
4987 inet_ntoa_r(
4988 *(const struct in_addr *)PR_IP(pip, PR_INET, ii),
4989 ip4buf));
4990 }
4991 #endif
4992 #ifdef INET6
4993 if ((pip = pr->pr_addrs[PR_INET6]) != NULL) {
4994 db_printf(" ip6s = %d\n", pip->ips);
4995 for (ii = 0; ii < pip->ips; ii++)
4996 db_printf(" %s %s\n",
4997 ii == 0 ? "ip6.addr =" : " ",
4998 ip6_sprintf(ip6buf,
4999 (const struct in6_addr *)PR_IP(pip, PR_INET6, ii)));
5000 }
5001 #endif
5002 }
5003
DB_SHOW_COMMAND(prison,db_show_prison_command)5004 DB_SHOW_COMMAND(prison, db_show_prison_command)
5005 {
5006 struct prison *pr;
5007
5008 if (!have_addr) {
5009 /*
5010 * Show all prisons in the list, and prison0 which is not
5011 * listed.
5012 */
5013 db_show_prison(&prison0);
5014 if (!db_pager_quit) {
5015 TAILQ_FOREACH(pr, &allprison, pr_list) {
5016 db_show_prison(pr);
5017 if (db_pager_quit)
5018 break;
5019 }
5020 }
5021 return;
5022 }
5023
5024 if (addr == 0)
5025 pr = &prison0;
5026 else {
5027 /* Look for a prison with the ID and with references. */
5028 TAILQ_FOREACH(pr, &allprison, pr_list)
5029 if (pr->pr_id == addr && pr->pr_ref > 0)
5030 break;
5031 if (pr == NULL)
5032 /* Look again, without requiring a reference. */
5033 TAILQ_FOREACH(pr, &allprison, pr_list)
5034 if (pr->pr_id == addr)
5035 break;
5036 if (pr == NULL)
5037 /* Assume address points to a valid prison. */
5038 pr = (struct prison *)addr;
5039 }
5040 db_show_prison(pr);
5041 }
5042
5043 #endif /* DDB */
5044