104 return 0; 105 case IP_MOUNT_DEVFS: 106 if (!bool_param(j->intparams[IP_MOUNT_DEVFS])) 107 continue; 108 /* FALLTHROUGH */ 109 case IP__OP: 110 case IP_STOP_TIMEOUT: 111 j->comstring = &dummystring; 112 break; 113 default: 114 if (j->intparams[comparam] == NULL) 115 continue; 116 j->comstring = create_failed 117 ? TAILQ_LAST(&j->intparams[comparam]->val, 118 cfstrings) 119 : TAILQ_FIRST(&j->intparams[comparam]->val); 120 } 121 } else { 122 j->comstring = j->comstring == &dummystring ? NULL : 123 create_failed 124 ? TAILQ_PREV(j->comstring, cfstrings, tq) 125 : TAILQ_NEXT(j->comstring, tq); 126 } 127 if (j->comstring == NULL || j->comstring->len == 0 || 128 (create_failed && (comparam == IP_EXEC_PRESTART || 129 comparam == IP_EXEC_START || comparam == IP_COMMAND || 130 comparam == IP_EXEC_POSTSTART))) 131 continue; 132 switch (run_command(j)) { 133 case -1: 134 failed(j); 135 /* FALLTHROUGH */ 136 case 1: 137 return 1; 138 } 139 } 140} 141 142/* 143 * Check command exit status 144 */ 145int 146finish_command(struct cfjail *j) 147{ 148 int error; 149 150 if (!(j->flags & JF_SLEEPQ)) 151 return 0; 152 j->flags &= ~JF_SLEEPQ; 153 if (*j->comparam == IP_STOP_TIMEOUT) 154 { 155 j->flags &= ~JF_TIMEOUT; 156 j->pstatus = 0; 157 return 0; 158 } 159 paralimit++; 160 if (!TAILQ_EMPTY(&runnable)) 161 requeue(TAILQ_FIRST(&runnable), &ready); 162 error = 0; 163 if (j->flags & JF_TIMEOUT) { 164 j->flags &= ~JF_TIMEOUT; 165 if (*j->comparam != IP_STOP_TIMEOUT) { 166 jail_warnx(j, "%s: timed out", j->comline); 167 failed(j); 168 error = -1; 169 } else if (verbose > 0) 170 jail_note(j, "timed out\n"); 171 } else if (j->pstatus != 0) { 172 if (WIFSIGNALED(j->pstatus)) 173 jail_warnx(j, "%s: exited on signal %d", 174 j->comline, WTERMSIG(j->pstatus)); 175 else 176 jail_warnx(j, "%s: failed", j->comline); 177 j->pstatus = 0; 178 failed(j); 179 error = -1; 180 } 181 free(j->comline); 182 j->comline = NULL; 183 return error; 184} 185 186/* 187 * Check for finished processes or timeouts. 188 */ 189struct cfjail * 190next_proc(int nonblock) 191{ 192 struct kevent ke; 193 struct timespec ts; 194 struct timespec *tsp; 195 struct cfjail *j; 196 197 if (!TAILQ_EMPTY(&sleeping)) { 198 again: 199 tsp = NULL; 200 if ((j = TAILQ_FIRST(&sleeping)) && j->timeout.tv_sec) { 201 clock_gettime(CLOCK_REALTIME, &ts); 202 ts.tv_sec = j->timeout.tv_sec - ts.tv_sec; 203 ts.tv_nsec = j->timeout.tv_nsec - ts.tv_nsec; 204 if (ts.tv_nsec < 0) { 205 ts.tv_sec--; 206 ts.tv_nsec += 1000000000; 207 } 208 if (ts.tv_sec < 0 || 209 (ts.tv_sec == 0 && ts.tv_nsec == 0)) { 210 j->flags |= JF_TIMEOUT; 211 clear_procs(j); 212 return j; 213 } 214 tsp = &ts; 215 } 216 if (nonblock) { 217 ts.tv_sec = 0; 218 ts.tv_nsec = 0; 219 tsp = &ts; 220 } 221 switch (kevent(kq, NULL, 0, &ke, 1, tsp)) { 222 case -1: 223 if (errno != EINTR) 224 err(1, "kevent"); 225 goto again; 226 case 0: 227 if (!nonblock) { 228 j = TAILQ_FIRST(&sleeping); 229 j->flags |= JF_TIMEOUT; 230 clear_procs(j); 231 return j; 232 } 233 break; 234 case 1: 235 (void)waitpid(ke.ident, NULL, WNOHANG); 236 if ((j = find_proc(ke.ident))) { 237 j->pstatus = ke.data; 238 return j; 239 } 240 goto again; 241 } 242 } 243 return NULL; 244} 245 246/* 247 * Run a single command for a jail, possible inside the jail. 248 */ 249int 250run_command(struct cfjail *j) 251{ 252 const struct passwd *pwd; 253 const struct cfstring *comstring, *s; 254 login_cap_t *lcap; 255 char **argv; 256 char *cs, *comcs, *devpath; 257 const char *jidstr, *conslog, *path, *ruleset, *term, *username; 258 enum intparam comparam; 259 size_t comlen; 260 pid_t pid; 261 int argc, bg, clean, consfd, down, fib, i, injail, sjuser, timeout; 262#if defined(INET) || defined(INET6) 263 char *addr; 264#endif 265 266 static char *cleanenv; 267 268 /* Perform some operations that aren't actually commands */ 269 comparam = *j->comparam; 270 down = j->flags & (JF_STOP | JF_FAILED); 271 switch (comparam) { 272 case IP_STOP_TIMEOUT: 273 return term_procs(j); 274 275 case IP__OP: 276 if (down) { 277 if (jail_remove(j->jid) < 0 && errno == EPERM) { 278 jail_warnx(j, "jail_remove: %s", 279 strerror(errno)); 280 return -1; 281 } 282 if (verbose > 0 || (verbose == 0 && (j->flags & JF_STOP 283 ? note_remove : j->name != NULL))) 284 jail_note(j, "removed\n"); 285 j->jid = -1; 286 if (j->flags & JF_STOP) 287 dep_done(j, DF_LIGHT); 288 else 289 j->flags &= ~JF_PERSIST; 290 } else { 291 if (create_jail(j) < 0) 292 return -1; 293 if (verbose >= 0 && (j->name || verbose > 0)) 294 jail_note(j, "created\n"); 295 dep_done(j, DF_LIGHT); 296 } 297 return 0; 298 299 default: ; 300 } 301 /* 302 * Collect exec arguments. Internal commands for network and 303 * mounting build their own argument lists. 304 */ 305 comstring = j->comstring; 306 bg = 0; 307 switch (comparam) { 308#ifdef INET 309 case IP__IP4_IFADDR: 310 argv = alloca(8 * sizeof(char *)); 311 *(const char **)&argv[0] = _PATH_IFCONFIG; 312 if ((cs = strchr(comstring->s, '|'))) { 313 argv[1] = alloca(cs - comstring->s + 1); 314 strlcpy(argv[1], comstring->s, cs - comstring->s + 1); 315 addr = cs + 1; 316 } else { 317 *(const char **)&argv[1] = 318 string_param(j->intparams[IP_INTERFACE]); 319 addr = comstring->s; 320 } 321 *(const char **)&argv[2] = "inet"; 322 if (!(cs = strchr(addr, '/'))) { 323 argv[3] = addr; 324 *(const char **)&argv[4] = "netmask"; 325 *(const char **)&argv[5] = "255.255.255.255"; 326 argc = 6; 327 } else if (strchr(cs + 1, '.')) { 328 argv[3] = alloca(cs - addr + 1); 329 strlcpy(argv[3], addr, cs - addr + 1); 330 *(const char **)&argv[4] = "netmask"; 331 *(const char **)&argv[5] = cs + 1; 332 argc = 6; 333 } else { 334 argv[3] = addr; 335 argc = 4; 336 } 337 *(const char **)&argv[argc] = down ? "-alias" : "alias"; 338 argv[argc + 1] = NULL; 339 break; 340#endif 341 342#ifdef INET6 343 case IP__IP6_IFADDR: 344 argv = alloca(8 * sizeof(char *)); 345 *(const char **)&argv[0] = _PATH_IFCONFIG; 346 if ((cs = strchr(comstring->s, '|'))) { 347 argv[1] = alloca(cs - comstring->s + 1); 348 strlcpy(argv[1], comstring->s, cs - comstring->s + 1); 349 addr = cs + 1; 350 } else { 351 *(const char **)&argv[1] = 352 string_param(j->intparams[IP_INTERFACE]); 353 addr = comstring->s; 354 } 355 *(const char **)&argv[2] = "inet6"; 356 argv[3] = addr; 357 if (!(cs = strchr(addr, '/'))) { 358 *(const char **)&argv[4] = "prefixlen"; 359 *(const char **)&argv[5] = "128"; 360 argc = 6; 361 } else 362 argc = 4; 363 *(const char **)&argv[argc] = down ? "-alias" : "alias"; 364 argv[argc + 1] = NULL; 365 break; 366#endif 367 368 case IP_VNET_INTERFACE: 369 argv = alloca(5 * sizeof(char *)); 370 *(const char **)&argv[0] = _PATH_IFCONFIG; 371 argv[1] = comstring->s; 372 *(const char **)&argv[2] = down ? "-vnet" : "vnet"; 373 jidstr = string_param(j->intparams[KP_JID]); 374 *(const char **)&argv[3] = 375 jidstr ? jidstr : string_param(j->intparams[KP_NAME]); 376 argv[4] = NULL; 377 break; 378 379 case IP_MOUNT: 380 case IP__MOUNT_FROM_FSTAB: 381 argv = alloca(8 * sizeof(char *)); 382 comcs = alloca(comstring->len + 1); 383 strcpy(comcs, comstring->s); 384 argc = 0; 385 for (cs = strtok(comcs, " \t\f\v\r\n"); cs && argc < 4; 386 cs = strtok(NULL, " \t\f\v\r\n")) 387 argv[argc++] = cs; 388 if (argc == 0) 389 return 0; 390 if (argc < 3) { 391 jail_warnx(j, "%s: %s: missing information", 392 j->intparams[comparam]->name, comstring->s); 393 return -1; 394 } 395 if (check_path(j, j->intparams[comparam]->name, argv[1], 0, 396 down ? argv[2] : NULL) < 0) 397 return -1; 398 if (down) { 399 argv[4] = NULL; 400 argv[3] = argv[1]; 401 *(const char **)&argv[0] = "/sbin/umount"; 402 } else { 403 if (argc == 4) { 404 argv[7] = NULL; 405 argv[6] = argv[1]; 406 argv[5] = argv[0]; 407 argv[4] = argv[3]; 408 *(const char **)&argv[3] = "-o"; 409 } else { 410 argv[5] = NULL; 411 argv[4] = argv[1]; 412 argv[3] = argv[0]; 413 } 414 *(const char **)&argv[0] = _PATH_MOUNT; 415 } 416 *(const char **)&argv[1] = "-t"; 417 break; 418 419 case IP_MOUNT_DEVFS: 420 argv = alloca(7 * sizeof(char *)); 421 path = string_param(j->intparams[KP_PATH]); 422 if (path == NULL) { 423 jail_warnx(j, "mount.devfs: no path"); 424 return -1; 425 } 426 devpath = alloca(strlen(path) + 5); 427 sprintf(devpath, "%s/dev", path); 428 if (check_path(j, "mount.devfs", devpath, 0, 429 down ? "devfs" : NULL) < 0) 430 return -1; 431 if (down) { 432 *(const char **)&argv[0] = "/sbin/umount"; 433 argv[1] = devpath; 434 argv[2] = NULL; 435 } else { 436 *(const char **)&argv[0] = _PATH_MOUNT; 437 *(const char **)&argv[1] = "-t"; 438 *(const char **)&argv[2] = "devfs"; 439 ruleset = string_param(j->intparams[KP_DEVFS_RULESET]); 440 if (!ruleset) 441 ruleset = "4"; /* devfsrules_jail */ 442 argv[3] = alloca(11 + strlen(ruleset)); 443 sprintf(argv[3], "-oruleset=%s", ruleset); 444 *(const char **)&argv[4] = "."; 445 argv[5] = devpath; 446 argv[6] = NULL; 447 } 448 break; 449 450 case IP_COMMAND: 451 if (j->name != NULL) 452 goto default_command; 453 argc = 0; 454 TAILQ_FOREACH(s, &j->intparams[IP_COMMAND]->val, tq) 455 argc++; 456 argv = alloca((argc + 1) * sizeof(char *)); 457 argc = 0; 458 TAILQ_FOREACH(s, &j->intparams[IP_COMMAND]->val, tq) 459 argv[argc++] = s->s; 460 argv[argc] = NULL; 461 j->comstring = &dummystring; 462 break; 463 464 default: 465 default_command: 466 if ((cs = strpbrk(comstring->s, "!\"$&'()*;<>?[\\]`{|}~")) && 467 !(cs[0] == '&' && cs[1] == '\0')) { 468 argv = alloca(4 * sizeof(char *)); 469 *(const char **)&argv[0] = _PATH_BSHELL; 470 *(const char **)&argv[1] = "-c"; 471 argv[2] = comstring->s; 472 argv[3] = NULL; 473 } else { 474 if (cs) { 475 *cs = 0; 476 bg = 1; 477 } 478 comcs = alloca(comstring->len + 1); 479 strcpy(comcs, comstring->s); 480 argc = 0; 481 for (cs = strtok(comcs, " \t\f\v\r\n"); cs; 482 cs = strtok(NULL, " \t\f\v\r\n")) 483 argc++; 484 argv = alloca((argc + 1) * sizeof(char *)); 485 strcpy(comcs, comstring->s); 486 argc = 0; 487 for (cs = strtok(comcs, " \t\f\v\r\n"); cs; 488 cs = strtok(NULL, " \t\f\v\r\n")) 489 argv[argc++] = cs; 490 argv[argc] = NULL; 491 } 492 } 493 if (argv[0] == NULL) 494 return 0; 495 496 if (int_param(j->intparams[IP_EXEC_TIMEOUT], &timeout) && 497 timeout != 0) { 498 clock_gettime(CLOCK_REALTIME, &j->timeout); 499 j->timeout.tv_sec += timeout; 500 } else 501 j->timeout.tv_sec = 0; 502 503 injail = comparam == IP_EXEC_START || comparam == IP_COMMAND || 504 comparam == IP_EXEC_STOP; 505 clean = bool_param(j->intparams[IP_EXEC_CLEAN]); 506 username = string_param(j->intparams[injail 507 ? IP_EXEC_JAIL_USER : IP_EXEC_SYSTEM_USER]); 508 sjuser = bool_param(j->intparams[IP_EXEC_SYSTEM_JAIL_USER]); 509 510 consfd = 0; 511 if (injail && 512 (conslog = string_param(j->intparams[IP_EXEC_CONSOLELOG]))) { 513 if (check_path(j, "exec.consolelog", conslog, 1, NULL) < 0) 514 return -1; 515 consfd = 516 open(conslog, O_WRONLY | O_CREAT | O_APPEND, DEFFILEMODE); 517 if (consfd < 0) { 518 jail_warnx(j, "open %s: %s", conslog, strerror(errno)); 519 return -1; 520 } 521 } 522 523 comlen = 0; 524 for (i = 0; argv[i]; i++) 525 comlen += strlen(argv[i]) + 1; 526 j->comline = cs = emalloc(comlen); 527 for (i = 0; argv[i]; i++) { 528 strcpy(cs, argv[i]); 529 if (argv[i + 1]) { 530 cs += strlen(argv[i]) + 1; 531 cs[-1] = ' '; 532 } 533 } 534 if (verbose > 0) 535 jail_note(j, "run command%s%s%s: %s\n", 536 injail ? " in jail" : "", username ? " as " : "", 537 username ? username : "", j->comline); 538 539 pid = fork(); 540 if (pid < 0) 541 err(1, "fork"); 542 if (pid > 0) { 543 if (bg) { 544 free(j->comline); 545 j->comline = NULL; 546 return 0; 547 } else { 548 paralimit--; 549 add_proc(j, pid); 550 return 1; 551 } 552 } 553 if (bg) 554 setsid(); 555 556 /* Set up the environment and run the command */ 557 pwd = NULL; 558 lcap = NULL; 559 if ((clean || username) && injail && sjuser && 560 get_user_info(j, username, &pwd, &lcap) < 0) 561 exit(1); 562 if (injail) { 563 /* jail_attach won't chdir along with its chroot. */ 564 path = string_param(j->intparams[KP_PATH]); 565 if (path && chdir(path) < 0) { 566 jail_warnx(j, "chdir %s: %s", path, strerror(errno)); 567 exit(1); 568 } 569 if (int_param(j->intparams[IP_EXEC_FIB], &fib) && 570 setfib(fib) < 0) { 571 jail_warnx(j, "setfib: %s", strerror(errno)); 572 exit(1); 573 } 574 if (jail_attach(j->jid) < 0) { 575 jail_warnx(j, "jail_attach: %s", strerror(errno)); 576 exit(1); 577 } 578 } 579 if (clean || username) { 580 if (!(injail && sjuser) && 581 get_user_info(j, username, &pwd, &lcap) < 0) 582 exit(1); 583 if (clean) { 584 term = getenv("TERM"); 585 environ = &cleanenv; 586 setenv("PATH", "/bin:/usr/bin", 0); 587 setenv("TERM", term, 1); 588 } 589 if (setusercontext(lcap, pwd, pwd->pw_uid, username 590 ? LOGIN_SETALL & ~LOGIN_SETGROUP & ~LOGIN_SETLOGIN 591 : LOGIN_SETPATH | LOGIN_SETENV) < 0) { 592 jail_warnx(j, "setusercontext %s: %s", pwd->pw_name, 593 strerror(errno)); 594 exit(1); 595 } 596 login_close(lcap); 597 setenv("USER", pwd->pw_name, 1); 598 setenv("HOME", pwd->pw_dir, 1); 599 setenv("SHELL", 600 *pwd->pw_shell ? pwd->pw_shell : _PATH_BSHELL, 1); 601 if (clean && chdir(pwd->pw_dir) < 0) { 602 jail_warnx(j, "chdir %s: %s", 603 pwd->pw_dir, strerror(errno)); 604 exit(1); 605 } 606 endpwent(); 607 } 608 609 if (consfd != 0 && (dup2(consfd, 1) < 0 || dup2(consfd, 2) < 0)) { 610 jail_warnx(j, "exec.consolelog: %s", strerror(errno)); 611 exit(1); 612 } 613 closefrom(3); 614 execvp(argv[0], argv); 615 jail_warnx(j, "exec %s: %s", argv[0], strerror(errno)); 616 exit(1); 617} 618 619/* 620 * Add a process to the hash, tied to a jail. 621 */ 622static void 623add_proc(struct cfjail *j, pid_t pid) 624{ 625 struct kevent ke; 626 struct cfjail *tj; 627 struct phash *ph; 628 629 if (!kq && (kq = kqueue()) < 0) 630 err(1, "kqueue"); 631 EV_SET(&ke, pid, EVFILT_PROC, EV_ADD, NOTE_EXIT, 0, NULL); 632 if (kevent(kq, &ke, 1, NULL, 0, NULL) < 0) 633 err(1, "kevent"); 634 ph = emalloc(sizeof(struct phash)); 635 ph->j = j; 636 ph->pid = pid; 637 LIST_INSERT_HEAD(&phash[pid % PHASH_SIZE], ph, le); 638 j->nprocs++; 639 j->flags |= JF_SLEEPQ; 640 if (j->timeout.tv_sec == 0) 641 requeue(j, &sleeping); 642 else { 643 /* File the jail in the sleep queue acording to its timeout. */ 644 TAILQ_REMOVE(j->queue, j, tq); 645 TAILQ_FOREACH(tj, &sleeping, tq) { 646 if (!tj->timeout.tv_sec || 647 j->timeout.tv_sec < tj->timeout.tv_sec || 648 (j->timeout.tv_sec == tj->timeout.tv_sec && 649 j->timeout.tv_nsec <= tj->timeout.tv_nsec)) { 650 TAILQ_INSERT_BEFORE(tj, j, tq); 651 break; 652 } 653 } 654 if (tj == NULL) 655 TAILQ_INSERT_TAIL(&sleeping, j, tq); 656 j->queue = &sleeping; 657 } 658} 659 660/* 661 * Remove any processes from the hash that correspond to a jail. 662 */ 663static void 664clear_procs(struct cfjail *j) 665{ 666 struct kevent ke; 667 struct phash *ph, *tph; 668 int i; 669 670 j->nprocs = 0; 671 for (i = 0; i < PHASH_SIZE; i++) 672 LIST_FOREACH_SAFE(ph, &phash[i], le, tph) 673 if (ph->j == j) { 674 EV_SET(&ke, ph->pid, EVFILT_PROC, EV_DELETE, 675 NOTE_EXIT, 0, NULL); 676 (void)kevent(kq, &ke, 1, NULL, 0, NULL); 677 LIST_REMOVE(ph, le); 678 free(ph); 679 } 680} 681 682/* 683 * Find the jail that corresponds to an exited process. 684 */ 685static struct cfjail * 686find_proc(pid_t pid) 687{ 688 struct cfjail *j; 689 struct phash *ph; 690 691 LIST_FOREACH(ph, &phash[pid % PHASH_SIZE], le) 692 if (ph->pid == pid) { 693 j = ph->j; 694 LIST_REMOVE(ph, le); 695 free(ph); 696 return --j->nprocs ? NULL : j; 697 } 698 return NULL; 699} 700 701/* 702 * Send SIGTERM to all processes in a jail and wait for them to die. 703 */ 704static int 705term_procs(struct cfjail *j) 706{ 707 struct kinfo_proc *ki; 708 int i, noted, pcnt, timeout; 709 710 static kvm_t *kd; 711 712 if (!int_param(j->intparams[IP_STOP_TIMEOUT], &timeout)) 713 timeout = DEFAULT_STOP_TIMEOUT; 714 else if (timeout == 0) 715 return 0; 716 717 if (kd == NULL) { 718 kd = kvm_open(NULL, NULL, NULL, O_RDONLY, NULL); 719 if (kd == NULL) 720 return 0; 721 } 722 723 ki = kvm_getprocs(kd, KERN_PROC_PROC, 0, &pcnt); 724 if (ki == NULL) 725 return 0; 726 noted = 0; 727 for (i = 0; i < pcnt; i++) 728 if (ki[i].ki_jid == j->jid && 729 kill(ki[i].ki_pid, SIGTERM) == 0) { 730 add_proc(j, ki[i].ki_pid); 731 if (verbose > 0) { 732 if (!noted) { 733 noted = 1; 734 jail_note(j, "sent SIGTERM to:"); 735 } 736 printf(" %d", ki[i].ki_pid); 737 } 738 } 739 if (noted) 740 printf("\n"); 741 if (j->nprocs > 0) { 742 clock_gettime(CLOCK_REALTIME, &j->timeout); 743 j->timeout.tv_sec += timeout; 744 return 1; 745 } 746 return 0; 747} 748 749/* 750 * Look up a user in the passwd and login.conf files. 751 */ 752static int 753get_user_info(struct cfjail *j, const char *username, 754 const struct passwd **pwdp, login_cap_t **lcapp) 755{ 756 const struct passwd *pwd; 757 758 *pwdp = pwd = username ? getpwnam(username) : getpwuid(getuid()); 759 if (pwd == NULL) { 760 if (errno) 761 jail_warnx(j, "getpwnam%s%s: %s", username ? " " : "", 762 username ? username : "", strerror(errno)); 763 else if (username) 764 jail_warnx(j, "%s: no such user", username); 765 else 766 jail_warnx(j, "unknown uid %d", getuid()); 767 return -1; 768 } 769 *lcapp = login_getpwclass(pwd); 770 if (*lcapp == NULL) { 771 jail_warnx(j, "getpwclass %s: %s", pwd->pw_name, 772 strerror(errno)); 773 return -1; 774 } 775 /* Set the groups while the group file is still available */ 776 if (initgroups(pwd->pw_name, pwd->pw_gid) < 0) { 777 jail_warnx(j, "initgroups %s: %s", pwd->pw_name, 778 strerror(errno)); 779 return -1; 780 } 781 return 0; 782} 783 784/* 785 * Make sure a mount or consolelog path is a valid absolute pathname 786 * with no symlinks. 787 */ 788static int 789check_path(struct cfjail *j, const char *pname, const char *path, int isfile, 790 const char *umount_type) 791{ 792 struct stat st, mpst; 793 struct statfs stfs; 794 char *tpath, *p; 795 const char *jailpath; 796 size_t jplen; 797 798 if (path[0] != '/') { 799 jail_warnx(j, "%s: %s: not an absolute pathname", 800 pname, path); 801 return -1; 802 } 803 /* 804 * Only check for symlinks in components below the jail's path, 805 * since that's where the security risk lies. 806 */ 807 jailpath = string_param(j->intparams[KP_PATH]); 808 if (jailpath == NULL) 809 jailpath = ""; 810 jplen = strlen(jailpath); 811 if (!strncmp(path, jailpath, jplen) && path[jplen] == '/') { 812 tpath = alloca(strlen(path) + 1); 813 strcpy(tpath, path); 814 for (p = tpath + jplen; p != NULL; ) { 815 p = strchr(p + 1, '/'); 816 if (p) 817 *p = '\0'; 818 if (lstat(tpath, &st) < 0) { 819 if (errno == ENOENT && isfile && !p) 820 break; 821 jail_warnx(j, "%s: %s: %s", pname, tpath, 822 strerror(errno)); 823 return -1; 824 } 825 if (S_ISLNK(st.st_mode)) { 826 jail_warnx(j, "%s: %s is a symbolic link", 827 pname, tpath); 828 return -1; 829 } 830 if (p) 831 *p = '/'; 832 } 833 } 834 if (umount_type != NULL) { 835 if (stat(path, &st) < 0 || statfs(path, &stfs) < 0) { 836 jail_warnx(j, "%s: %s: %s", pname, path, 837 strerror(errno)); 838 return -1; 839 } 840 if (stat(stfs.f_mntonname, &mpst) < 0) { 841 jail_warnx(j, "%s: %s: %s", pname, stfs.f_mntonname, 842 strerror(errno)); 843 return -1; 844 } 845 if (st.st_ino != mpst.st_ino) { 846 jail_warnx(j, "%s: %s: not a mount point", 847 pname, path); 848 return -1; 849 } 850 if (strcmp(stfs.f_fstypename, umount_type)) { 851 jail_warnx(j, "%s: %s: not a %s mount", 852 pname, path, umount_type); 853 return -1; 854 } 855 } 856 return 0; 857}
| 104 return 0; 105 case IP_MOUNT_DEVFS: 106 if (!bool_param(j->intparams[IP_MOUNT_DEVFS])) 107 continue; 108 /* FALLTHROUGH */ 109 case IP__OP: 110 case IP_STOP_TIMEOUT: 111 j->comstring = &dummystring; 112 break; 113 default: 114 if (j->intparams[comparam] == NULL) 115 continue; 116 j->comstring = create_failed 117 ? TAILQ_LAST(&j->intparams[comparam]->val, 118 cfstrings) 119 : TAILQ_FIRST(&j->intparams[comparam]->val); 120 } 121 } else { 122 j->comstring = j->comstring == &dummystring ? NULL : 123 create_failed 124 ? TAILQ_PREV(j->comstring, cfstrings, tq) 125 : TAILQ_NEXT(j->comstring, tq); 126 } 127 if (j->comstring == NULL || j->comstring->len == 0 || 128 (create_failed && (comparam == IP_EXEC_PRESTART || 129 comparam == IP_EXEC_START || comparam == IP_COMMAND || 130 comparam == IP_EXEC_POSTSTART))) 131 continue; 132 switch (run_command(j)) { 133 case -1: 134 failed(j); 135 /* FALLTHROUGH */ 136 case 1: 137 return 1; 138 } 139 } 140} 141 142/* 143 * Check command exit status 144 */ 145int 146finish_command(struct cfjail *j) 147{ 148 int error; 149 150 if (!(j->flags & JF_SLEEPQ)) 151 return 0; 152 j->flags &= ~JF_SLEEPQ; 153 if (*j->comparam == IP_STOP_TIMEOUT) 154 { 155 j->flags &= ~JF_TIMEOUT; 156 j->pstatus = 0; 157 return 0; 158 } 159 paralimit++; 160 if (!TAILQ_EMPTY(&runnable)) 161 requeue(TAILQ_FIRST(&runnable), &ready); 162 error = 0; 163 if (j->flags & JF_TIMEOUT) { 164 j->flags &= ~JF_TIMEOUT; 165 if (*j->comparam != IP_STOP_TIMEOUT) { 166 jail_warnx(j, "%s: timed out", j->comline); 167 failed(j); 168 error = -1; 169 } else if (verbose > 0) 170 jail_note(j, "timed out\n"); 171 } else if (j->pstatus != 0) { 172 if (WIFSIGNALED(j->pstatus)) 173 jail_warnx(j, "%s: exited on signal %d", 174 j->comline, WTERMSIG(j->pstatus)); 175 else 176 jail_warnx(j, "%s: failed", j->comline); 177 j->pstatus = 0; 178 failed(j); 179 error = -1; 180 } 181 free(j->comline); 182 j->comline = NULL; 183 return error; 184} 185 186/* 187 * Check for finished processes or timeouts. 188 */ 189struct cfjail * 190next_proc(int nonblock) 191{ 192 struct kevent ke; 193 struct timespec ts; 194 struct timespec *tsp; 195 struct cfjail *j; 196 197 if (!TAILQ_EMPTY(&sleeping)) { 198 again: 199 tsp = NULL; 200 if ((j = TAILQ_FIRST(&sleeping)) && j->timeout.tv_sec) { 201 clock_gettime(CLOCK_REALTIME, &ts); 202 ts.tv_sec = j->timeout.tv_sec - ts.tv_sec; 203 ts.tv_nsec = j->timeout.tv_nsec - ts.tv_nsec; 204 if (ts.tv_nsec < 0) { 205 ts.tv_sec--; 206 ts.tv_nsec += 1000000000; 207 } 208 if (ts.tv_sec < 0 || 209 (ts.tv_sec == 0 && ts.tv_nsec == 0)) { 210 j->flags |= JF_TIMEOUT; 211 clear_procs(j); 212 return j; 213 } 214 tsp = &ts; 215 } 216 if (nonblock) { 217 ts.tv_sec = 0; 218 ts.tv_nsec = 0; 219 tsp = &ts; 220 } 221 switch (kevent(kq, NULL, 0, &ke, 1, tsp)) { 222 case -1: 223 if (errno != EINTR) 224 err(1, "kevent"); 225 goto again; 226 case 0: 227 if (!nonblock) { 228 j = TAILQ_FIRST(&sleeping); 229 j->flags |= JF_TIMEOUT; 230 clear_procs(j); 231 return j; 232 } 233 break; 234 case 1: 235 (void)waitpid(ke.ident, NULL, WNOHANG); 236 if ((j = find_proc(ke.ident))) { 237 j->pstatus = ke.data; 238 return j; 239 } 240 goto again; 241 } 242 } 243 return NULL; 244} 245 246/* 247 * Run a single command for a jail, possible inside the jail. 248 */ 249int 250run_command(struct cfjail *j) 251{ 252 const struct passwd *pwd; 253 const struct cfstring *comstring, *s; 254 login_cap_t *lcap; 255 char **argv; 256 char *cs, *comcs, *devpath; 257 const char *jidstr, *conslog, *path, *ruleset, *term, *username; 258 enum intparam comparam; 259 size_t comlen; 260 pid_t pid; 261 int argc, bg, clean, consfd, down, fib, i, injail, sjuser, timeout; 262#if defined(INET) || defined(INET6) 263 char *addr; 264#endif 265 266 static char *cleanenv; 267 268 /* Perform some operations that aren't actually commands */ 269 comparam = *j->comparam; 270 down = j->flags & (JF_STOP | JF_FAILED); 271 switch (comparam) { 272 case IP_STOP_TIMEOUT: 273 return term_procs(j); 274 275 case IP__OP: 276 if (down) { 277 if (jail_remove(j->jid) < 0 && errno == EPERM) { 278 jail_warnx(j, "jail_remove: %s", 279 strerror(errno)); 280 return -1; 281 } 282 if (verbose > 0 || (verbose == 0 && (j->flags & JF_STOP 283 ? note_remove : j->name != NULL))) 284 jail_note(j, "removed\n"); 285 j->jid = -1; 286 if (j->flags & JF_STOP) 287 dep_done(j, DF_LIGHT); 288 else 289 j->flags &= ~JF_PERSIST; 290 } else { 291 if (create_jail(j) < 0) 292 return -1; 293 if (verbose >= 0 && (j->name || verbose > 0)) 294 jail_note(j, "created\n"); 295 dep_done(j, DF_LIGHT); 296 } 297 return 0; 298 299 default: ; 300 } 301 /* 302 * Collect exec arguments. Internal commands for network and 303 * mounting build their own argument lists. 304 */ 305 comstring = j->comstring; 306 bg = 0; 307 switch (comparam) { 308#ifdef INET 309 case IP__IP4_IFADDR: 310 argv = alloca(8 * sizeof(char *)); 311 *(const char **)&argv[0] = _PATH_IFCONFIG; 312 if ((cs = strchr(comstring->s, '|'))) { 313 argv[1] = alloca(cs - comstring->s + 1); 314 strlcpy(argv[1], comstring->s, cs - comstring->s + 1); 315 addr = cs + 1; 316 } else { 317 *(const char **)&argv[1] = 318 string_param(j->intparams[IP_INTERFACE]); 319 addr = comstring->s; 320 } 321 *(const char **)&argv[2] = "inet"; 322 if (!(cs = strchr(addr, '/'))) { 323 argv[3] = addr; 324 *(const char **)&argv[4] = "netmask"; 325 *(const char **)&argv[5] = "255.255.255.255"; 326 argc = 6; 327 } else if (strchr(cs + 1, '.')) { 328 argv[3] = alloca(cs - addr + 1); 329 strlcpy(argv[3], addr, cs - addr + 1); 330 *(const char **)&argv[4] = "netmask"; 331 *(const char **)&argv[5] = cs + 1; 332 argc = 6; 333 } else { 334 argv[3] = addr; 335 argc = 4; 336 } 337 *(const char **)&argv[argc] = down ? "-alias" : "alias"; 338 argv[argc + 1] = NULL; 339 break; 340#endif 341 342#ifdef INET6 343 case IP__IP6_IFADDR: 344 argv = alloca(8 * sizeof(char *)); 345 *(const char **)&argv[0] = _PATH_IFCONFIG; 346 if ((cs = strchr(comstring->s, '|'))) { 347 argv[1] = alloca(cs - comstring->s + 1); 348 strlcpy(argv[1], comstring->s, cs - comstring->s + 1); 349 addr = cs + 1; 350 } else { 351 *(const char **)&argv[1] = 352 string_param(j->intparams[IP_INTERFACE]); 353 addr = comstring->s; 354 } 355 *(const char **)&argv[2] = "inet6"; 356 argv[3] = addr; 357 if (!(cs = strchr(addr, '/'))) { 358 *(const char **)&argv[4] = "prefixlen"; 359 *(const char **)&argv[5] = "128"; 360 argc = 6; 361 } else 362 argc = 4; 363 *(const char **)&argv[argc] = down ? "-alias" : "alias"; 364 argv[argc + 1] = NULL; 365 break; 366#endif 367 368 case IP_VNET_INTERFACE: 369 argv = alloca(5 * sizeof(char *)); 370 *(const char **)&argv[0] = _PATH_IFCONFIG; 371 argv[1] = comstring->s; 372 *(const char **)&argv[2] = down ? "-vnet" : "vnet"; 373 jidstr = string_param(j->intparams[KP_JID]); 374 *(const char **)&argv[3] = 375 jidstr ? jidstr : string_param(j->intparams[KP_NAME]); 376 argv[4] = NULL; 377 break; 378 379 case IP_MOUNT: 380 case IP__MOUNT_FROM_FSTAB: 381 argv = alloca(8 * sizeof(char *)); 382 comcs = alloca(comstring->len + 1); 383 strcpy(comcs, comstring->s); 384 argc = 0; 385 for (cs = strtok(comcs, " \t\f\v\r\n"); cs && argc < 4; 386 cs = strtok(NULL, " \t\f\v\r\n")) 387 argv[argc++] = cs; 388 if (argc == 0) 389 return 0; 390 if (argc < 3) { 391 jail_warnx(j, "%s: %s: missing information", 392 j->intparams[comparam]->name, comstring->s); 393 return -1; 394 } 395 if (check_path(j, j->intparams[comparam]->name, argv[1], 0, 396 down ? argv[2] : NULL) < 0) 397 return -1; 398 if (down) { 399 argv[4] = NULL; 400 argv[3] = argv[1]; 401 *(const char **)&argv[0] = "/sbin/umount"; 402 } else { 403 if (argc == 4) { 404 argv[7] = NULL; 405 argv[6] = argv[1]; 406 argv[5] = argv[0]; 407 argv[4] = argv[3]; 408 *(const char **)&argv[3] = "-o"; 409 } else { 410 argv[5] = NULL; 411 argv[4] = argv[1]; 412 argv[3] = argv[0]; 413 } 414 *(const char **)&argv[0] = _PATH_MOUNT; 415 } 416 *(const char **)&argv[1] = "-t"; 417 break; 418 419 case IP_MOUNT_DEVFS: 420 argv = alloca(7 * sizeof(char *)); 421 path = string_param(j->intparams[KP_PATH]); 422 if (path == NULL) { 423 jail_warnx(j, "mount.devfs: no path"); 424 return -1; 425 } 426 devpath = alloca(strlen(path) + 5); 427 sprintf(devpath, "%s/dev", path); 428 if (check_path(j, "mount.devfs", devpath, 0, 429 down ? "devfs" : NULL) < 0) 430 return -1; 431 if (down) { 432 *(const char **)&argv[0] = "/sbin/umount"; 433 argv[1] = devpath; 434 argv[2] = NULL; 435 } else { 436 *(const char **)&argv[0] = _PATH_MOUNT; 437 *(const char **)&argv[1] = "-t"; 438 *(const char **)&argv[2] = "devfs"; 439 ruleset = string_param(j->intparams[KP_DEVFS_RULESET]); 440 if (!ruleset) 441 ruleset = "4"; /* devfsrules_jail */ 442 argv[3] = alloca(11 + strlen(ruleset)); 443 sprintf(argv[3], "-oruleset=%s", ruleset); 444 *(const char **)&argv[4] = "."; 445 argv[5] = devpath; 446 argv[6] = NULL; 447 } 448 break; 449 450 case IP_COMMAND: 451 if (j->name != NULL) 452 goto default_command; 453 argc = 0; 454 TAILQ_FOREACH(s, &j->intparams[IP_COMMAND]->val, tq) 455 argc++; 456 argv = alloca((argc + 1) * sizeof(char *)); 457 argc = 0; 458 TAILQ_FOREACH(s, &j->intparams[IP_COMMAND]->val, tq) 459 argv[argc++] = s->s; 460 argv[argc] = NULL; 461 j->comstring = &dummystring; 462 break; 463 464 default: 465 default_command: 466 if ((cs = strpbrk(comstring->s, "!\"$&'()*;<>?[\\]`{|}~")) && 467 !(cs[0] == '&' && cs[1] == '\0')) { 468 argv = alloca(4 * sizeof(char *)); 469 *(const char **)&argv[0] = _PATH_BSHELL; 470 *(const char **)&argv[1] = "-c"; 471 argv[2] = comstring->s; 472 argv[3] = NULL; 473 } else { 474 if (cs) { 475 *cs = 0; 476 bg = 1; 477 } 478 comcs = alloca(comstring->len + 1); 479 strcpy(comcs, comstring->s); 480 argc = 0; 481 for (cs = strtok(comcs, " \t\f\v\r\n"); cs; 482 cs = strtok(NULL, " \t\f\v\r\n")) 483 argc++; 484 argv = alloca((argc + 1) * sizeof(char *)); 485 strcpy(comcs, comstring->s); 486 argc = 0; 487 for (cs = strtok(comcs, " \t\f\v\r\n"); cs; 488 cs = strtok(NULL, " \t\f\v\r\n")) 489 argv[argc++] = cs; 490 argv[argc] = NULL; 491 } 492 } 493 if (argv[0] == NULL) 494 return 0; 495 496 if (int_param(j->intparams[IP_EXEC_TIMEOUT], &timeout) && 497 timeout != 0) { 498 clock_gettime(CLOCK_REALTIME, &j->timeout); 499 j->timeout.tv_sec += timeout; 500 } else 501 j->timeout.tv_sec = 0; 502 503 injail = comparam == IP_EXEC_START || comparam == IP_COMMAND || 504 comparam == IP_EXEC_STOP; 505 clean = bool_param(j->intparams[IP_EXEC_CLEAN]); 506 username = string_param(j->intparams[injail 507 ? IP_EXEC_JAIL_USER : IP_EXEC_SYSTEM_USER]); 508 sjuser = bool_param(j->intparams[IP_EXEC_SYSTEM_JAIL_USER]); 509 510 consfd = 0; 511 if (injail && 512 (conslog = string_param(j->intparams[IP_EXEC_CONSOLELOG]))) { 513 if (check_path(j, "exec.consolelog", conslog, 1, NULL) < 0) 514 return -1; 515 consfd = 516 open(conslog, O_WRONLY | O_CREAT | O_APPEND, DEFFILEMODE); 517 if (consfd < 0) { 518 jail_warnx(j, "open %s: %s", conslog, strerror(errno)); 519 return -1; 520 } 521 } 522 523 comlen = 0; 524 for (i = 0; argv[i]; i++) 525 comlen += strlen(argv[i]) + 1; 526 j->comline = cs = emalloc(comlen); 527 for (i = 0; argv[i]; i++) { 528 strcpy(cs, argv[i]); 529 if (argv[i + 1]) { 530 cs += strlen(argv[i]) + 1; 531 cs[-1] = ' '; 532 } 533 } 534 if (verbose > 0) 535 jail_note(j, "run command%s%s%s: %s\n", 536 injail ? " in jail" : "", username ? " as " : "", 537 username ? username : "", j->comline); 538 539 pid = fork(); 540 if (pid < 0) 541 err(1, "fork"); 542 if (pid > 0) { 543 if (bg) { 544 free(j->comline); 545 j->comline = NULL; 546 return 0; 547 } else { 548 paralimit--; 549 add_proc(j, pid); 550 return 1; 551 } 552 } 553 if (bg) 554 setsid(); 555 556 /* Set up the environment and run the command */ 557 pwd = NULL; 558 lcap = NULL; 559 if ((clean || username) && injail && sjuser && 560 get_user_info(j, username, &pwd, &lcap) < 0) 561 exit(1); 562 if (injail) { 563 /* jail_attach won't chdir along with its chroot. */ 564 path = string_param(j->intparams[KP_PATH]); 565 if (path && chdir(path) < 0) { 566 jail_warnx(j, "chdir %s: %s", path, strerror(errno)); 567 exit(1); 568 } 569 if (int_param(j->intparams[IP_EXEC_FIB], &fib) && 570 setfib(fib) < 0) { 571 jail_warnx(j, "setfib: %s", strerror(errno)); 572 exit(1); 573 } 574 if (jail_attach(j->jid) < 0) { 575 jail_warnx(j, "jail_attach: %s", strerror(errno)); 576 exit(1); 577 } 578 } 579 if (clean || username) { 580 if (!(injail && sjuser) && 581 get_user_info(j, username, &pwd, &lcap) < 0) 582 exit(1); 583 if (clean) { 584 term = getenv("TERM"); 585 environ = &cleanenv; 586 setenv("PATH", "/bin:/usr/bin", 0); 587 setenv("TERM", term, 1); 588 } 589 if (setusercontext(lcap, pwd, pwd->pw_uid, username 590 ? LOGIN_SETALL & ~LOGIN_SETGROUP & ~LOGIN_SETLOGIN 591 : LOGIN_SETPATH | LOGIN_SETENV) < 0) { 592 jail_warnx(j, "setusercontext %s: %s", pwd->pw_name, 593 strerror(errno)); 594 exit(1); 595 } 596 login_close(lcap); 597 setenv("USER", pwd->pw_name, 1); 598 setenv("HOME", pwd->pw_dir, 1); 599 setenv("SHELL", 600 *pwd->pw_shell ? pwd->pw_shell : _PATH_BSHELL, 1); 601 if (clean && chdir(pwd->pw_dir) < 0) { 602 jail_warnx(j, "chdir %s: %s", 603 pwd->pw_dir, strerror(errno)); 604 exit(1); 605 } 606 endpwent(); 607 } 608 609 if (consfd != 0 && (dup2(consfd, 1) < 0 || dup2(consfd, 2) < 0)) { 610 jail_warnx(j, "exec.consolelog: %s", strerror(errno)); 611 exit(1); 612 } 613 closefrom(3); 614 execvp(argv[0], argv); 615 jail_warnx(j, "exec %s: %s", argv[0], strerror(errno)); 616 exit(1); 617} 618 619/* 620 * Add a process to the hash, tied to a jail. 621 */ 622static void 623add_proc(struct cfjail *j, pid_t pid) 624{ 625 struct kevent ke; 626 struct cfjail *tj; 627 struct phash *ph; 628 629 if (!kq && (kq = kqueue()) < 0) 630 err(1, "kqueue"); 631 EV_SET(&ke, pid, EVFILT_PROC, EV_ADD, NOTE_EXIT, 0, NULL); 632 if (kevent(kq, &ke, 1, NULL, 0, NULL) < 0) 633 err(1, "kevent"); 634 ph = emalloc(sizeof(struct phash)); 635 ph->j = j; 636 ph->pid = pid; 637 LIST_INSERT_HEAD(&phash[pid % PHASH_SIZE], ph, le); 638 j->nprocs++; 639 j->flags |= JF_SLEEPQ; 640 if (j->timeout.tv_sec == 0) 641 requeue(j, &sleeping); 642 else { 643 /* File the jail in the sleep queue acording to its timeout. */ 644 TAILQ_REMOVE(j->queue, j, tq); 645 TAILQ_FOREACH(tj, &sleeping, tq) { 646 if (!tj->timeout.tv_sec || 647 j->timeout.tv_sec < tj->timeout.tv_sec || 648 (j->timeout.tv_sec == tj->timeout.tv_sec && 649 j->timeout.tv_nsec <= tj->timeout.tv_nsec)) { 650 TAILQ_INSERT_BEFORE(tj, j, tq); 651 break; 652 } 653 } 654 if (tj == NULL) 655 TAILQ_INSERT_TAIL(&sleeping, j, tq); 656 j->queue = &sleeping; 657 } 658} 659 660/* 661 * Remove any processes from the hash that correspond to a jail. 662 */ 663static void 664clear_procs(struct cfjail *j) 665{ 666 struct kevent ke; 667 struct phash *ph, *tph; 668 int i; 669 670 j->nprocs = 0; 671 for (i = 0; i < PHASH_SIZE; i++) 672 LIST_FOREACH_SAFE(ph, &phash[i], le, tph) 673 if (ph->j == j) { 674 EV_SET(&ke, ph->pid, EVFILT_PROC, EV_DELETE, 675 NOTE_EXIT, 0, NULL); 676 (void)kevent(kq, &ke, 1, NULL, 0, NULL); 677 LIST_REMOVE(ph, le); 678 free(ph); 679 } 680} 681 682/* 683 * Find the jail that corresponds to an exited process. 684 */ 685static struct cfjail * 686find_proc(pid_t pid) 687{ 688 struct cfjail *j; 689 struct phash *ph; 690 691 LIST_FOREACH(ph, &phash[pid % PHASH_SIZE], le) 692 if (ph->pid == pid) { 693 j = ph->j; 694 LIST_REMOVE(ph, le); 695 free(ph); 696 return --j->nprocs ? NULL : j; 697 } 698 return NULL; 699} 700 701/* 702 * Send SIGTERM to all processes in a jail and wait for them to die. 703 */ 704static int 705term_procs(struct cfjail *j) 706{ 707 struct kinfo_proc *ki; 708 int i, noted, pcnt, timeout; 709 710 static kvm_t *kd; 711 712 if (!int_param(j->intparams[IP_STOP_TIMEOUT], &timeout)) 713 timeout = DEFAULT_STOP_TIMEOUT; 714 else if (timeout == 0) 715 return 0; 716 717 if (kd == NULL) { 718 kd = kvm_open(NULL, NULL, NULL, O_RDONLY, NULL); 719 if (kd == NULL) 720 return 0; 721 } 722 723 ki = kvm_getprocs(kd, KERN_PROC_PROC, 0, &pcnt); 724 if (ki == NULL) 725 return 0; 726 noted = 0; 727 for (i = 0; i < pcnt; i++) 728 if (ki[i].ki_jid == j->jid && 729 kill(ki[i].ki_pid, SIGTERM) == 0) { 730 add_proc(j, ki[i].ki_pid); 731 if (verbose > 0) { 732 if (!noted) { 733 noted = 1; 734 jail_note(j, "sent SIGTERM to:"); 735 } 736 printf(" %d", ki[i].ki_pid); 737 } 738 } 739 if (noted) 740 printf("\n"); 741 if (j->nprocs > 0) { 742 clock_gettime(CLOCK_REALTIME, &j->timeout); 743 j->timeout.tv_sec += timeout; 744 return 1; 745 } 746 return 0; 747} 748 749/* 750 * Look up a user in the passwd and login.conf files. 751 */ 752static int 753get_user_info(struct cfjail *j, const char *username, 754 const struct passwd **pwdp, login_cap_t **lcapp) 755{ 756 const struct passwd *pwd; 757 758 *pwdp = pwd = username ? getpwnam(username) : getpwuid(getuid()); 759 if (pwd == NULL) { 760 if (errno) 761 jail_warnx(j, "getpwnam%s%s: %s", username ? " " : "", 762 username ? username : "", strerror(errno)); 763 else if (username) 764 jail_warnx(j, "%s: no such user", username); 765 else 766 jail_warnx(j, "unknown uid %d", getuid()); 767 return -1; 768 } 769 *lcapp = login_getpwclass(pwd); 770 if (*lcapp == NULL) { 771 jail_warnx(j, "getpwclass %s: %s", pwd->pw_name, 772 strerror(errno)); 773 return -1; 774 } 775 /* Set the groups while the group file is still available */ 776 if (initgroups(pwd->pw_name, pwd->pw_gid) < 0) { 777 jail_warnx(j, "initgroups %s: %s", pwd->pw_name, 778 strerror(errno)); 779 return -1; 780 } 781 return 0; 782} 783 784/* 785 * Make sure a mount or consolelog path is a valid absolute pathname 786 * with no symlinks. 787 */ 788static int 789check_path(struct cfjail *j, const char *pname, const char *path, int isfile, 790 const char *umount_type) 791{ 792 struct stat st, mpst; 793 struct statfs stfs; 794 char *tpath, *p; 795 const char *jailpath; 796 size_t jplen; 797 798 if (path[0] != '/') { 799 jail_warnx(j, "%s: %s: not an absolute pathname", 800 pname, path); 801 return -1; 802 } 803 /* 804 * Only check for symlinks in components below the jail's path, 805 * since that's where the security risk lies. 806 */ 807 jailpath = string_param(j->intparams[KP_PATH]); 808 if (jailpath == NULL) 809 jailpath = ""; 810 jplen = strlen(jailpath); 811 if (!strncmp(path, jailpath, jplen) && path[jplen] == '/') { 812 tpath = alloca(strlen(path) + 1); 813 strcpy(tpath, path); 814 for (p = tpath + jplen; p != NULL; ) { 815 p = strchr(p + 1, '/'); 816 if (p) 817 *p = '\0'; 818 if (lstat(tpath, &st) < 0) { 819 if (errno == ENOENT && isfile && !p) 820 break; 821 jail_warnx(j, "%s: %s: %s", pname, tpath, 822 strerror(errno)); 823 return -1; 824 } 825 if (S_ISLNK(st.st_mode)) { 826 jail_warnx(j, "%s: %s is a symbolic link", 827 pname, tpath); 828 return -1; 829 } 830 if (p) 831 *p = '/'; 832 } 833 } 834 if (umount_type != NULL) { 835 if (stat(path, &st) < 0 || statfs(path, &stfs) < 0) { 836 jail_warnx(j, "%s: %s: %s", pname, path, 837 strerror(errno)); 838 return -1; 839 } 840 if (stat(stfs.f_mntonname, &mpst) < 0) { 841 jail_warnx(j, "%s: %s: %s", pname, stfs.f_mntonname, 842 strerror(errno)); 843 return -1; 844 } 845 if (st.st_ino != mpst.st_ino) { 846 jail_warnx(j, "%s: %s: not a mount point", 847 pname, path); 848 return -1; 849 } 850 if (strcmp(stfs.f_fstypename, umount_type)) { 851 jail_warnx(j, "%s: %s: not a %s mount", 852 pname, path, umount_type); 853 return -1; 854 } 855 } 856 return 0; 857}
|