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