1 /*- 2 * ---------------------------------------------------------------------------- 3 * "THE BEER-WARE LICENSE" (Revision 42): 4 * <phk@FreeBSD.ORG> wrote this file. As long as you retain this notice you 5 * can do whatever you want with this stuff. If we meet some day, and you think 6 * this stuff is worth it, you can buy me a beer in return. Poul-Henning Kamp 7 * ---------------------------------------------------------------------------- 8 */ 9 10 #include <sys/cdefs.h> 11 __FBSDID("$FreeBSD$"); 12 13 #include "opt_mac.h" 14 15 #include <sys/param.h> 16 #include <sys/types.h> 17 #include <sys/kernel.h> 18 #include <sys/systm.h> 19 #include <sys/errno.h> 20 #include <sys/sysproto.h> 21 #include <sys/mac.h> 22 #include <sys/malloc.h> 23 #include <sys/proc.h> 24 #include <sys/taskqueue.h> 25 #include <sys/jail.h> 26 #include <sys/lock.h> 27 #include <sys/mutex.h> 28 #include <sys/namei.h> 29 #include <sys/queue.h> 30 #include <sys/socket.h> 31 #include <sys/syscallsubr.h> 32 #include <sys/sysctl.h> 33 #include <sys/vnode.h> 34 #include <net/if.h> 35 #include <netinet/in.h> 36 37 MALLOC_DEFINE(M_PRISON, "prison", "Prison structures"); 38 39 SYSCTL_DECL(_security); 40 SYSCTL_NODE(_security, OID_AUTO, jail, CTLFLAG_RW, 0, 41 "Jail rules"); 42 43 int jail_set_hostname_allowed = 1; 44 SYSCTL_INT(_security_jail, OID_AUTO, set_hostname_allowed, CTLFLAG_RW, 45 &jail_set_hostname_allowed, 0, 46 "Processes in jail can set their hostnames"); 47 48 int jail_socket_unixiproute_only = 1; 49 SYSCTL_INT(_security_jail, OID_AUTO, socket_unixiproute_only, CTLFLAG_RW, 50 &jail_socket_unixiproute_only, 0, 51 "Processes in jail are limited to creating UNIX/IPv4/route sockets only"); 52 53 int jail_sysvipc_allowed = 0; 54 SYSCTL_INT(_security_jail, OID_AUTO, sysvipc_allowed, CTLFLAG_RW, 55 &jail_sysvipc_allowed, 0, 56 "Processes in jail can use System V IPC primitives"); 57 58 int jail_getfsstatroot_only = 1; 59 SYSCTL_INT(_security_jail, OID_AUTO, getfsstatroot_only, CTLFLAG_RW, 60 &jail_getfsstatroot_only, 0, 61 "Processes see only their root file system in getfsstat()"); 62 63 int jail_allow_raw_sockets = 0; 64 SYSCTL_INT(_security_jail, OID_AUTO, allow_raw_sockets, CTLFLAG_RW, 65 &jail_allow_raw_sockets, 0, 66 "Prison root can create raw sockets"); 67 68 int jail_chflags_allowed = 0; 69 SYSCTL_INT(_security_jail, OID_AUTO, chflags_allowed, CTLFLAG_RW, 70 &jail_chflags_allowed, 0, 71 "Processes in jail can alter system file flags"); 72 73 /* allprison, lastprid, and prisoncount are protected by allprison_mtx. */ 74 struct prisonlist allprison; 75 struct mtx allprison_mtx; 76 int lastprid = 0; 77 int prisoncount = 0; 78 79 static void init_prison(void *); 80 static void prison_complete(void *context, int pending); 81 static struct prison *prison_find(int); 82 static int sysctl_jail_list(SYSCTL_HANDLER_ARGS); 83 84 static void 85 init_prison(void *data __unused) 86 { 87 88 mtx_init(&allprison_mtx, "allprison", NULL, MTX_DEF); 89 LIST_INIT(&allprison); 90 } 91 92 SYSINIT(prison, SI_SUB_INTRINSIC, SI_ORDER_ANY, init_prison, NULL); 93 94 /* 95 * MPSAFE 96 * 97 * struct jail_args { 98 * struct jail *jail; 99 * }; 100 */ 101 int 102 jail(struct thread *td, struct jail_args *uap) 103 { 104 struct nameidata nd; 105 struct prison *pr, *tpr; 106 struct jail j; 107 struct jail_attach_args jaa; 108 int error, tryprid; 109 110 error = copyin(uap->jail, &j, sizeof(j)); 111 if (error) 112 return (error); 113 if (j.version != 0) 114 return (EINVAL); 115 116 MALLOC(pr, struct prison *, sizeof(*pr), M_PRISON, M_WAITOK | M_ZERO); 117 mtx_init(&pr->pr_mtx, "jail mutex", NULL, MTX_DEF); 118 pr->pr_ref = 1; 119 error = copyinstr(j.path, &pr->pr_path, sizeof(pr->pr_path), 0); 120 if (error) 121 goto e_killmtx; 122 mtx_lock(&Giant); 123 NDINIT(&nd, LOOKUP, FOLLOW | LOCKLEAF, UIO_SYSSPACE, pr->pr_path, td); 124 error = namei(&nd); 125 if (error) { 126 mtx_unlock(&Giant); 127 goto e_killmtx; 128 } 129 pr->pr_root = nd.ni_vp; 130 VOP_UNLOCK(nd.ni_vp, 0, td); 131 NDFREE(&nd, NDF_ONLY_PNBUF); 132 mtx_unlock(&Giant); 133 error = copyinstr(j.hostname, &pr->pr_host, sizeof(pr->pr_host), 0); 134 if (error) 135 goto e_dropvnref; 136 pr->pr_ip = j.ip_number; 137 pr->pr_linux = NULL; 138 pr->pr_securelevel = securelevel; 139 140 /* Determine next pr_id and add prison to allprison list. */ 141 mtx_lock(&allprison_mtx); 142 tryprid = lastprid + 1; 143 if (tryprid == JAIL_MAX) 144 tryprid = 1; 145 next: 146 LIST_FOREACH(tpr, &allprison, pr_list) { 147 if (tpr->pr_id == tryprid) { 148 tryprid++; 149 if (tryprid == JAIL_MAX) { 150 mtx_unlock(&allprison_mtx); 151 error = EAGAIN; 152 goto e_dropvnref; 153 } 154 goto next; 155 } 156 } 157 pr->pr_id = jaa.jid = lastprid = tryprid; 158 LIST_INSERT_HEAD(&allprison, pr, pr_list); 159 prisoncount++; 160 mtx_unlock(&allprison_mtx); 161 162 error = jail_attach(td, &jaa); 163 if (error) 164 goto e_dropprref; 165 mtx_lock(&pr->pr_mtx); 166 pr->pr_ref--; 167 mtx_unlock(&pr->pr_mtx); 168 td->td_retval[0] = jaa.jid; 169 return (0); 170 e_dropprref: 171 mtx_lock(&allprison_mtx); 172 LIST_REMOVE(pr, pr_list); 173 prisoncount--; 174 mtx_unlock(&allprison_mtx); 175 e_dropvnref: 176 mtx_lock(&Giant); 177 vrele(pr->pr_root); 178 mtx_unlock(&Giant); 179 e_killmtx: 180 mtx_destroy(&pr->pr_mtx); 181 FREE(pr, M_PRISON); 182 return (error); 183 } 184 185 /* 186 * MPSAFE 187 * 188 * struct jail_attach_args { 189 * int jid; 190 * }; 191 */ 192 int 193 jail_attach(struct thread *td, struct jail_attach_args *uap) 194 { 195 struct proc *p; 196 struct ucred *newcred, *oldcred; 197 struct prison *pr; 198 int error; 199 200 /* 201 * XXX: Note that there is a slight race here if two threads 202 * in the same privileged process attempt to attach to two 203 * different jails at the same time. It is important for 204 * user processes not to do this, or they might end up with 205 * a process root from one prison, but attached to the jail 206 * of another. 207 */ 208 error = suser(td); 209 if (error) 210 return (error); 211 212 p = td->td_proc; 213 mtx_lock(&allprison_mtx); 214 pr = prison_find(uap->jid); 215 if (pr == NULL) { 216 mtx_unlock(&allprison_mtx); 217 return (EINVAL); 218 } 219 pr->pr_ref++; 220 mtx_unlock(&pr->pr_mtx); 221 mtx_unlock(&allprison_mtx); 222 223 mtx_lock(&Giant); 224 vn_lock(pr->pr_root, LK_EXCLUSIVE | LK_RETRY, td); 225 if ((error = change_dir(pr->pr_root, td)) != 0) 226 goto e_unlock; 227 #ifdef MAC 228 if ((error = mac_check_vnode_chroot(td->td_ucred, pr->pr_root))) 229 goto e_unlock; 230 #endif 231 VOP_UNLOCK(pr->pr_root, 0, td); 232 change_root(pr->pr_root, td); 233 mtx_unlock(&Giant); 234 235 newcred = crget(); 236 PROC_LOCK(p); 237 oldcred = p->p_ucred; 238 setsugid(p); 239 crcopy(newcred, oldcred); 240 newcred->cr_prison = pr; 241 p->p_ucred = newcred; 242 PROC_UNLOCK(p); 243 crfree(oldcred); 244 return (0); 245 e_unlock: 246 VOP_UNLOCK(pr->pr_root, 0, td); 247 mtx_unlock(&Giant); 248 mtx_lock(&pr->pr_mtx); 249 pr->pr_ref--; 250 mtx_unlock(&pr->pr_mtx); 251 return (error); 252 } 253 254 /* 255 * Returns a locked prison instance, or NULL on failure. 256 */ 257 static struct prison * 258 prison_find(int prid) 259 { 260 struct prison *pr; 261 262 mtx_assert(&allprison_mtx, MA_OWNED); 263 LIST_FOREACH(pr, &allprison, pr_list) { 264 if (pr->pr_id == prid) { 265 mtx_lock(&pr->pr_mtx); 266 return (pr); 267 } 268 } 269 return (NULL); 270 } 271 272 void 273 prison_free(struct prison *pr) 274 { 275 276 mtx_lock(&allprison_mtx); 277 mtx_lock(&pr->pr_mtx); 278 pr->pr_ref--; 279 if (pr->pr_ref == 0) { 280 LIST_REMOVE(pr, pr_list); 281 mtx_unlock(&pr->pr_mtx); 282 prisoncount--; 283 mtx_unlock(&allprison_mtx); 284 285 TASK_INIT(&pr->pr_task, 0, prison_complete, pr); 286 taskqueue_enqueue(taskqueue_thread, &pr->pr_task); 287 return; 288 } 289 mtx_unlock(&pr->pr_mtx); 290 mtx_unlock(&allprison_mtx); 291 } 292 293 static void 294 prison_complete(void *context, int pending) 295 { 296 struct prison *pr; 297 298 pr = (struct prison *)context; 299 300 mtx_lock(&Giant); 301 vrele(pr->pr_root); 302 mtx_unlock(&Giant); 303 304 mtx_destroy(&pr->pr_mtx); 305 if (pr->pr_linux != NULL) 306 FREE(pr->pr_linux, M_PRISON); 307 FREE(pr, M_PRISON); 308 } 309 310 void 311 prison_hold(struct prison *pr) 312 { 313 314 mtx_lock(&pr->pr_mtx); 315 pr->pr_ref++; 316 mtx_unlock(&pr->pr_mtx); 317 } 318 319 u_int32_t 320 prison_getip(struct ucred *cred) 321 { 322 323 return (cred->cr_prison->pr_ip); 324 } 325 326 int 327 prison_ip(struct ucred *cred, int flag, u_int32_t *ip) 328 { 329 u_int32_t tmp; 330 331 if (!jailed(cred)) 332 return (0); 333 if (flag) 334 tmp = *ip; 335 else 336 tmp = ntohl(*ip); 337 if (tmp == INADDR_ANY) { 338 if (flag) 339 *ip = cred->cr_prison->pr_ip; 340 else 341 *ip = htonl(cred->cr_prison->pr_ip); 342 return (0); 343 } 344 if (tmp == INADDR_LOOPBACK) { 345 if (flag) 346 *ip = cred->cr_prison->pr_ip; 347 else 348 *ip = htonl(cred->cr_prison->pr_ip); 349 return (0); 350 } 351 if (cred->cr_prison->pr_ip != tmp) 352 return (1); 353 return (0); 354 } 355 356 void 357 prison_remote_ip(struct ucred *cred, int flag, u_int32_t *ip) 358 { 359 u_int32_t tmp; 360 361 if (!jailed(cred)) 362 return; 363 if (flag) 364 tmp = *ip; 365 else 366 tmp = ntohl(*ip); 367 if (tmp == INADDR_LOOPBACK) { 368 if (flag) 369 *ip = cred->cr_prison->pr_ip; 370 else 371 *ip = htonl(cred->cr_prison->pr_ip); 372 return; 373 } 374 return; 375 } 376 377 int 378 prison_if(struct ucred *cred, struct sockaddr *sa) 379 { 380 struct sockaddr_in *sai; 381 int ok; 382 383 sai = (struct sockaddr_in *)sa; 384 if ((sai->sin_family != AF_INET) && jail_socket_unixiproute_only) 385 ok = 1; 386 else if (sai->sin_family != AF_INET) 387 ok = 0; 388 else if (cred->cr_prison->pr_ip != ntohl(sai->sin_addr.s_addr)) 389 ok = 1; 390 else 391 ok = 0; 392 return (ok); 393 } 394 395 /* 396 * Return 0 if jails permit p1 to frob p2, otherwise ESRCH. 397 */ 398 int 399 prison_check(struct ucred *cred1, struct ucred *cred2) 400 { 401 402 if (jailed(cred1)) { 403 if (!jailed(cred2)) 404 return (ESRCH); 405 if (cred2->cr_prison != cred1->cr_prison) 406 return (ESRCH); 407 } 408 409 return (0); 410 } 411 412 /* 413 * Return 1 if the passed credential is in a jail, otherwise 0. 414 */ 415 int 416 jailed(struct ucred *cred) 417 { 418 419 return (cred->cr_prison != NULL); 420 } 421 422 /* 423 * Return the correct hostname for the passed credential. 424 */ 425 void 426 getcredhostname(struct ucred *cred, char *buf, size_t size) 427 { 428 429 if (jailed(cred)) { 430 mtx_lock(&cred->cr_prison->pr_mtx); 431 strlcpy(buf, cred->cr_prison->pr_host, size); 432 mtx_unlock(&cred->cr_prison->pr_mtx); 433 } else 434 strlcpy(buf, hostname, size); 435 } 436 437 /* 438 * Return 1 if the passed credential can "see" the passed mountpoint 439 * when performing a getfsstat(); otherwise, 0. 440 */ 441 int 442 prison_check_mount(struct ucred *cred, struct mount *mp) 443 { 444 445 if (jail_getfsstatroot_only && cred->cr_prison != NULL) { 446 if (cred->cr_prison->pr_root->v_mount != mp) 447 return (0); 448 } 449 return (1); 450 } 451 452 static int 453 sysctl_jail_list(SYSCTL_HANDLER_ARGS) 454 { 455 struct xprison *xp, *sxp; 456 struct prison *pr; 457 int count, error; 458 459 mtx_assert(&Giant, MA_OWNED); 460 if (jailed(req->td->td_ucred)) 461 return (0); 462 retry: 463 mtx_lock(&allprison_mtx); 464 count = prisoncount; 465 mtx_unlock(&allprison_mtx); 466 467 if (count == 0) 468 return (0); 469 470 sxp = xp = malloc(sizeof(*xp) * count, M_TEMP, M_WAITOK | M_ZERO); 471 mtx_lock(&allprison_mtx); 472 if (count != prisoncount) { 473 mtx_unlock(&allprison_mtx); 474 free(sxp, M_TEMP); 475 goto retry; 476 } 477 478 LIST_FOREACH(pr, &allprison, pr_list) { 479 mtx_lock(&pr->pr_mtx); 480 xp->pr_version = XPRISON_VERSION; 481 xp->pr_id = pr->pr_id; 482 strlcpy(xp->pr_path, pr->pr_path, sizeof(xp->pr_path)); 483 strlcpy(xp->pr_host, pr->pr_host, sizeof(xp->pr_host)); 484 xp->pr_ip = pr->pr_ip; 485 mtx_unlock(&pr->pr_mtx); 486 xp++; 487 } 488 mtx_unlock(&allprison_mtx); 489 490 error = SYSCTL_OUT(req, sxp, sizeof(*sxp) * count); 491 free(sxp, M_TEMP); 492 if (error) 493 return (error); 494 return (0); 495 } 496 497 SYSCTL_OID(_security_jail, OID_AUTO, list, CTLTYPE_STRUCT | CTLFLAG_RD, 498 NULL, 0, sysctl_jail_list, "S", "List of active jails"); 499 500 static int 501 sysctl_jail_jailed(SYSCTL_HANDLER_ARGS) 502 { 503 int error, injail; 504 505 injail = jailed(req->td->td_ucred); 506 error = SYSCTL_OUT(req, &injail, sizeof(injail)); 507 508 return (error); 509 } 510 SYSCTL_PROC(_security_jail, OID_AUTO, jailed, CTLTYPE_INT | CTLFLAG_RD, 511 NULL, 0, sysctl_jail_jailed, "I", "Process in jail?"); 512