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