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