command.c revision 248854
1214117Sjamie/*- 2223190Sjamie * Copyright (c) 2011 James Gritton 3214117Sjamie * All rights reserved. 4214117Sjamie * 5214117Sjamie * Redistribution and use in source and binary forms, with or without 6214117Sjamie * modification, are permitted provided that the following conditions 7214117Sjamie * are met: 8214117Sjamie * 1. Redistributions of source code must retain the above copyright 9214117Sjamie * notice, this list of conditions and the following disclaimer. 10214117Sjamie * 2. Redistributions in binary form must reproduce the above copyright 11214117Sjamie * notice, this list of conditions and the following disclaimer in the 12214117Sjamie * documentation and/or other materials provided with the distribution. 13214117Sjamie * 14214117Sjamie * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 15214117Sjamie * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 16214117Sjamie * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 17214117Sjamie * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 18214117Sjamie * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 19214117Sjamie * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 20214117Sjamie * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 21214117Sjamie * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 22214117Sjamie * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 23214117Sjamie * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 24214117Sjamie * SUCH DAMAGE. 25214117Sjamie */ 26214117Sjamie 27214117Sjamie#include <sys/cdefs.h> 28214117Sjamie__FBSDID("$FreeBSD: head/usr.sbin/jail/command.c 248854 2013-03-28 21:02:49Z jamie $"); 29214117Sjamie 30214117Sjamie#include <sys/types.h> 31214117Sjamie#include <sys/event.h> 32214805Sjamie#include <sys/mount.h> 33214117Sjamie#include <sys/stat.h> 34214117Sjamie#include <sys/sysctl.h> 35214117Sjamie#include <sys/user.h> 36214117Sjamie#include <sys/wait.h> 37214117Sjamie 38214117Sjamie#include <err.h> 39214117Sjamie#include <errno.h> 40214117Sjamie#include <fcntl.h> 41214117Sjamie#include <kvm.h> 42214117Sjamie#include <login_cap.h> 43214117Sjamie#include <paths.h> 44214117Sjamie#include <pwd.h> 45214117Sjamie#include <signal.h> 46214117Sjamie#include <stdio.h> 47214117Sjamie#include <stdlib.h> 48214117Sjamie#include <string.h> 49214117Sjamie#include <unistd.h> 50214117Sjamie 51214117Sjamie#include "jailp.h" 52214117Sjamie 53214117Sjamie#define DEFAULT_STOP_TIMEOUT 10 54214117Sjamie#define PHASH_SIZE 256 55214117Sjamie 56214117SjamieLIST_HEAD(phhead, phash); 57214117Sjamie 58214117Sjamiestruct phash { 59214117Sjamie LIST_ENTRY(phash) le; 60214117Sjamie struct cfjail *j; 61214117Sjamie pid_t pid; 62214117Sjamie}; 63214117Sjamie 64216367Sjamieint paralimit = -1; 65216367Sjamie 66214117Sjamieextern char **environ; 67214117Sjamie 68223189Sjamiestatic int run_command(struct cfjail *j); 69246804Sjamiestatic int add_proc(struct cfjail *j, pid_t pid); 70214117Sjamiestatic void clear_procs(struct cfjail *j); 71214117Sjamiestatic struct cfjail *find_proc(pid_t pid); 72216367Sjamiestatic int term_procs(struct cfjail *j); 73223189Sjamiestatic int get_user_info(struct cfjail *j, const char *username, 74223189Sjamie const struct passwd **pwdp, login_cap_t **lcapp); 75214797Sjamiestatic int check_path(struct cfjail *j, const char *pname, const char *path, 76214805Sjamie int isfile, const char *umount_type); 77214117Sjamie 78214117Sjamiestatic struct cfjails sleeping = TAILQ_HEAD_INITIALIZER(sleeping); 79214117Sjamiestatic struct cfjails runnable = TAILQ_HEAD_INITIALIZER(runnable); 80223368Sjamiestatic struct cfstring dummystring = { .len = 1 }; 81214117Sjamiestatic struct phhead phash[PHASH_SIZE]; 82214117Sjamiestatic int kq; 83214117Sjamie 84214117Sjamie/* 85223189Sjamie * Run the next command associated with a jail. 86214117Sjamie */ 87214117Sjamieint 88223189Sjamienext_command(struct cfjail *j) 89214117Sjamie{ 90223189Sjamie enum intparam comparam; 91248854Sjamie int create_failed, stopping; 92223189Sjamie 93223442Sjamie if (paralimit == 0) { 94223442Sjamie requeue(j, &runnable); 95223442Sjamie return 1; 96223442Sjamie } 97223189Sjamie create_failed = (j->flags & (JF_STOP | JF_FAILED)) == JF_FAILED; 98248854Sjamie stopping = (j->flags & JF_STOP) != 0; 99223442Sjamie comparam = *j->comparam; 100223442Sjamie for (;;) { 101223189Sjamie if (j->comstring == NULL) { 102223442Sjamie j->comparam += create_failed ? -1 : 1; 103223442Sjamie switch ((comparam = *j->comparam)) { 104234988Sjamie case IP__NULL: 105223442Sjamie return 0; 106223189Sjamie case IP_MOUNT_DEVFS: 107223189Sjamie if (!bool_param(j->intparams[IP_MOUNT_DEVFS])) 108223189Sjamie continue; 109223189Sjamie /* FALLTHROUGH */ 110223263Sjamie case IP__OP: 111223189Sjamie case IP_STOP_TIMEOUT: 112223189Sjamie j->comstring = &dummystring; 113223189Sjamie break; 114223189Sjamie default: 115223189Sjamie if (j->intparams[comparam] == NULL) 116223189Sjamie continue; 117248854Sjamie j->comstring = create_failed || (stopping && 118248854Sjamie (j->intparams[comparam]->flags & PF_REV)) 119223189Sjamie ? TAILQ_LAST(&j->intparams[comparam]->val, 120223189Sjamie cfstrings) 121223189Sjamie : TAILQ_FIRST(&j->intparams[comparam]->val); 122223189Sjamie } 123223442Sjamie } else { 124223442Sjamie j->comstring = j->comstring == &dummystring ? NULL : 125248854Sjamie create_failed || (stopping && 126248854Sjamie (j->intparams[comparam]->flags & PF_REV)) 127223442Sjamie ? TAILQ_PREV(j->comstring, cfstrings, tq) 128223442Sjamie : TAILQ_NEXT(j->comstring, tq); 129223189Sjamie } 130223442Sjamie if (j->comstring == NULL || j->comstring->len == 0 || 131223442Sjamie (create_failed && (comparam == IP_EXEC_PRESTART || 132223442Sjamie comparam == IP_EXEC_START || comparam == IP_COMMAND || 133223442Sjamie comparam == IP_EXEC_POSTSTART))) 134223442Sjamie continue; 135223442Sjamie switch (run_command(j)) { 136223442Sjamie case -1: 137223442Sjamie failed(j); 138223442Sjamie /* FALLTHROUGH */ 139223442Sjamie case 1: 140223442Sjamie return 1; 141223189Sjamie } 142223189Sjamie } 143223189Sjamie} 144223189Sjamie 145223189Sjamie/* 146223189Sjamie * Check command exit status 147223189Sjamie */ 148223189Sjamieint 149223189Sjamiefinish_command(struct cfjail *j) 150223189Sjamie{ 151223189Sjamie int error; 152223189Sjamie 153223189Sjamie if (!(j->flags & JF_SLEEPQ)) 154223189Sjamie return 0; 155223189Sjamie j->flags &= ~JF_SLEEPQ; 156223827Sjamie if (*j->comparam == IP_STOP_TIMEOUT) 157223827Sjamie { 158223827Sjamie j->flags &= ~JF_TIMEOUT; 159223827Sjamie j->pstatus = 0; 160223827Sjamie return 0; 161223189Sjamie } 162223827Sjamie paralimit++; 163223827Sjamie if (!TAILQ_EMPTY(&runnable)) 164223827Sjamie requeue(TAILQ_FIRST(&runnable), &ready); 165223189Sjamie error = 0; 166223189Sjamie if (j->flags & JF_TIMEOUT) { 167223189Sjamie j->flags &= ~JF_TIMEOUT; 168223189Sjamie if (*j->comparam != IP_STOP_TIMEOUT) { 169223189Sjamie jail_warnx(j, "%s: timed out", j->comline); 170223189Sjamie failed(j); 171223189Sjamie error = -1; 172223189Sjamie } else if (verbose > 0) 173223189Sjamie jail_note(j, "timed out\n"); 174223189Sjamie } else if (j->pstatus != 0) { 175223189Sjamie if (WIFSIGNALED(j->pstatus)) 176223189Sjamie jail_warnx(j, "%s: exited on signal %d", 177223189Sjamie j->comline, WTERMSIG(j->pstatus)); 178223189Sjamie else 179223189Sjamie jail_warnx(j, "%s: failed", j->comline); 180223189Sjamie j->pstatus = 0; 181223189Sjamie failed(j); 182223189Sjamie error = -1; 183223189Sjamie } 184223189Sjamie free(j->comline); 185223189Sjamie j->comline = NULL; 186223189Sjamie return error; 187223189Sjamie} 188223189Sjamie 189223189Sjamie/* 190223442Sjamie * Check for finished processes or timeouts. 191223189Sjamie */ 192223189Sjamiestruct cfjail * 193223189Sjamienext_proc(int nonblock) 194223189Sjamie{ 195223189Sjamie struct kevent ke; 196223189Sjamie struct timespec ts; 197223189Sjamie struct timespec *tsp; 198223189Sjamie struct cfjail *j; 199223189Sjamie 200223189Sjamie if (!TAILQ_EMPTY(&sleeping)) { 201223189Sjamie again: 202223189Sjamie tsp = NULL; 203223189Sjamie if ((j = TAILQ_FIRST(&sleeping)) && j->timeout.tv_sec) { 204223189Sjamie clock_gettime(CLOCK_REALTIME, &ts); 205223189Sjamie ts.tv_sec = j->timeout.tv_sec - ts.tv_sec; 206223189Sjamie ts.tv_nsec = j->timeout.tv_nsec - ts.tv_nsec; 207223189Sjamie if (ts.tv_nsec < 0) { 208223189Sjamie ts.tv_sec--; 209223189Sjamie ts.tv_nsec += 1000000000; 210223189Sjamie } 211223189Sjamie if (ts.tv_sec < 0 || 212223189Sjamie (ts.tv_sec == 0 && ts.tv_nsec == 0)) { 213223189Sjamie j->flags |= JF_TIMEOUT; 214223189Sjamie clear_procs(j); 215223189Sjamie return j; 216223189Sjamie } 217223189Sjamie tsp = &ts; 218223189Sjamie } 219223189Sjamie if (nonblock) { 220223189Sjamie ts.tv_sec = 0; 221223189Sjamie ts.tv_nsec = 0; 222223189Sjamie tsp = &ts; 223223189Sjamie } 224223189Sjamie switch (kevent(kq, NULL, 0, &ke, 1, tsp)) { 225223189Sjamie case -1: 226223189Sjamie if (errno != EINTR) 227223189Sjamie err(1, "kevent"); 228223189Sjamie goto again; 229223189Sjamie case 0: 230223189Sjamie if (!nonblock) { 231223189Sjamie j = TAILQ_FIRST(&sleeping); 232223189Sjamie j->flags |= JF_TIMEOUT; 233223189Sjamie clear_procs(j); 234223189Sjamie return j; 235223189Sjamie } 236223189Sjamie break; 237223189Sjamie case 1: 238223189Sjamie (void)waitpid(ke.ident, NULL, WNOHANG); 239223189Sjamie if ((j = find_proc(ke.ident))) { 240223189Sjamie j->pstatus = ke.data; 241223189Sjamie return j; 242223189Sjamie } 243223189Sjamie goto again; 244223189Sjamie } 245223189Sjamie } 246223189Sjamie return NULL; 247223189Sjamie} 248223189Sjamie 249223189Sjamie/* 250223189Sjamie * Run a single command for a jail, possible inside the jail. 251223189Sjamie */ 252236198Sjamiestatic int 253223189Sjamierun_command(struct cfjail *j) 254223189Sjamie{ 255214117Sjamie const struct passwd *pwd; 256223189Sjamie const struct cfstring *comstring, *s; 257214117Sjamie login_cap_t *lcap; 258214117Sjamie char **argv; 259223351Sjamie char *cs, *comcs, *devpath; 260214117Sjamie const char *jidstr, *conslog, *path, *ruleset, *term, *username; 261223189Sjamie enum intparam comparam; 262214117Sjamie size_t comlen; 263214117Sjamie pid_t pid; 264214117Sjamie int argc, bg, clean, consfd, down, fib, i, injail, sjuser, timeout; 265223351Sjamie#if defined(INET) || defined(INET6) 266223351Sjamie char *addr; 267223351Sjamie#endif 268214117Sjamie 269214117Sjamie static char *cleanenv; 270214117Sjamie 271223263Sjamie /* Perform some operations that aren't actually commands */ 272223263Sjamie comparam = *j->comparam; 273223263Sjamie down = j->flags & (JF_STOP | JF_FAILED); 274223263Sjamie switch (comparam) { 275223263Sjamie case IP_STOP_TIMEOUT: 276223263Sjamie return term_procs(j); 277223263Sjamie 278223263Sjamie case IP__OP: 279223263Sjamie if (down) { 280231238Sjamie if (jail_remove(j->jid) < 0 && errno == EPERM) { 281231238Sjamie jail_warnx(j, "jail_remove: %s", 282231238Sjamie strerror(errno)); 283231238Sjamie return -1; 284231238Sjamie } 285223827Sjamie if (verbose > 0 || (verbose == 0 && (j->flags & JF_STOP 286223263Sjamie ? note_remove : j->name != NULL))) 287223263Sjamie jail_note(j, "removed\n"); 288223263Sjamie j->jid = -1; 289223263Sjamie if (j->flags & JF_STOP) 290223263Sjamie dep_done(j, DF_LIGHT); 291223263Sjamie else 292223263Sjamie j->flags &= ~JF_PERSIST; 293223263Sjamie } else { 294223442Sjamie if (create_jail(j) < 0) 295223263Sjamie return -1; 296236198Sjamie if (iflag) 297236198Sjamie printf("%d\n", j->jid); 298223263Sjamie if (verbose >= 0 && (j->name || verbose > 0)) 299223263Sjamie jail_note(j, "created\n"); 300223263Sjamie dep_done(j, DF_LIGHT); 301223263Sjamie } 302223442Sjamie return 0; 303223327Sjamie 304223327Sjamie default: ; 305223263Sjamie } 306214117Sjamie /* 307214117Sjamie * Collect exec arguments. Internal commands for network and 308216367Sjamie * mounting build their own argument lists. 309214117Sjamie */ 310223189Sjamie comstring = j->comstring; 311223189Sjamie bg = 0; 312216367Sjamie switch (comparam) { 313223351Sjamie#ifdef INET 314216367Sjamie case IP__IP4_IFADDR: 315214117Sjamie argv = alloca(8 * sizeof(char *)); 316214117Sjamie *(const char **)&argv[0] = _PATH_IFCONFIG; 317214117Sjamie if ((cs = strchr(comstring->s, '|'))) { 318214117Sjamie argv[1] = alloca(cs - comstring->s + 1); 319214117Sjamie strlcpy(argv[1], comstring->s, cs - comstring->s + 1); 320214117Sjamie addr = cs + 1; 321214117Sjamie } else { 322214117Sjamie *(const char **)&argv[1] = 323214117Sjamie string_param(j->intparams[IP_INTERFACE]); 324214117Sjamie addr = comstring->s; 325214117Sjamie } 326214117Sjamie *(const char **)&argv[2] = "inet"; 327214117Sjamie if (!(cs = strchr(addr, '/'))) { 328214117Sjamie argv[3] = addr; 329214117Sjamie *(const char **)&argv[4] = "netmask"; 330214117Sjamie *(const char **)&argv[5] = "255.255.255.255"; 331214117Sjamie argc = 6; 332214117Sjamie } else if (strchr(cs + 1, '.')) { 333214117Sjamie argv[3] = alloca(cs - addr + 1); 334214117Sjamie strlcpy(argv[3], addr, cs - addr + 1); 335214117Sjamie *(const char **)&argv[4] = "netmask"; 336214117Sjamie *(const char **)&argv[5] = cs + 1; 337214117Sjamie argc = 6; 338214117Sjamie } else { 339214117Sjamie argv[3] = addr; 340214117Sjamie argc = 4; 341214117Sjamie } 342214117Sjamie *(const char **)&argv[argc] = down ? "-alias" : "alias"; 343214117Sjamie argv[argc + 1] = NULL; 344216367Sjamie break; 345223351Sjamie#endif 346216367Sjamie 347214117Sjamie#ifdef INET6 348216367Sjamie case IP__IP6_IFADDR: 349214117Sjamie argv = alloca(8 * sizeof(char *)); 350214117Sjamie *(const char **)&argv[0] = _PATH_IFCONFIG; 351214117Sjamie if ((cs = strchr(comstring->s, '|'))) { 352214117Sjamie argv[1] = alloca(cs - comstring->s + 1); 353214117Sjamie strlcpy(argv[1], comstring->s, cs - comstring->s + 1); 354214117Sjamie addr = cs + 1; 355214117Sjamie } else { 356214117Sjamie *(const char **)&argv[1] = 357214117Sjamie string_param(j->intparams[IP_INTERFACE]); 358214117Sjamie addr = comstring->s; 359214117Sjamie } 360214117Sjamie *(const char **)&argv[2] = "inet6"; 361214117Sjamie argv[3] = addr; 362214117Sjamie if (!(cs = strchr(addr, '/'))) { 363214117Sjamie *(const char **)&argv[4] = "prefixlen"; 364214117Sjamie *(const char **)&argv[5] = "128"; 365214117Sjamie argc = 6; 366214117Sjamie } else 367214117Sjamie argc = 4; 368214117Sjamie *(const char **)&argv[argc] = down ? "-alias" : "alias"; 369214117Sjamie argv[argc + 1] = NULL; 370216367Sjamie break; 371214117Sjamie#endif 372216367Sjamie 373216367Sjamie case IP_VNET_INTERFACE: 374214117Sjamie argv = alloca(5 * sizeof(char *)); 375214117Sjamie *(const char **)&argv[0] = _PATH_IFCONFIG; 376214117Sjamie argv[1] = comstring->s; 377214117Sjamie *(const char **)&argv[2] = down ? "-vnet" : "vnet"; 378214117Sjamie jidstr = string_param(j->intparams[KP_JID]); 379214117Sjamie *(const char **)&argv[3] = 380214117Sjamie jidstr ? jidstr : string_param(j->intparams[KP_NAME]); 381214117Sjamie argv[4] = NULL; 382216367Sjamie break; 383216367Sjamie 384216367Sjamie case IP_MOUNT: 385216367Sjamie case IP__MOUNT_FROM_FSTAB: 386214117Sjamie argv = alloca(8 * sizeof(char *)); 387214117Sjamie comcs = alloca(comstring->len + 1); 388214117Sjamie strcpy(comcs, comstring->s); 389214117Sjamie argc = 0; 390214117Sjamie for (cs = strtok(comcs, " \t\f\v\r\n"); cs && argc < 4; 391214117Sjamie cs = strtok(NULL, " \t\f\v\r\n")) 392214117Sjamie argv[argc++] = cs; 393214797Sjamie if (argc == 0) 394223189Sjamie return 0; 395214117Sjamie if (argc < 3) { 396214783Sjamie jail_warnx(j, "%s: %s: missing information", 397214783Sjamie j->intparams[comparam]->name, comstring->s); 398214117Sjamie return -1; 399214117Sjamie } 400214805Sjamie if (check_path(j, j->intparams[comparam]->name, argv[1], 0, 401223442Sjamie down ? argv[2] : NULL) < 0) 402214797Sjamie return -1; 403214117Sjamie if (down) { 404214117Sjamie argv[4] = NULL; 405214117Sjamie argv[3] = argv[1]; 406214117Sjamie *(const char **)&argv[0] = "/sbin/umount"; 407214117Sjamie } else { 408214117Sjamie if (argc == 4) { 409214117Sjamie argv[7] = NULL; 410214117Sjamie argv[6] = argv[1]; 411214117Sjamie argv[5] = argv[0]; 412214117Sjamie argv[4] = argv[3]; 413214117Sjamie *(const char **)&argv[3] = "-o"; 414214117Sjamie } else { 415214117Sjamie argv[5] = NULL; 416214117Sjamie argv[4] = argv[1]; 417214117Sjamie argv[3] = argv[0]; 418214117Sjamie } 419214117Sjamie *(const char **)&argv[0] = _PATH_MOUNT; 420214117Sjamie } 421214117Sjamie *(const char **)&argv[1] = "-t"; 422216367Sjamie break; 423216367Sjamie 424216367Sjamie case IP_MOUNT_DEVFS: 425232242Sjamie argv = alloca(7 * sizeof(char *)); 426214117Sjamie path = string_param(j->intparams[KP_PATH]); 427214117Sjamie if (path == NULL) { 428214117Sjamie jail_warnx(j, "mount.devfs: no path"); 429214117Sjamie return -1; 430214117Sjamie } 431214797Sjamie devpath = alloca(strlen(path) + 5); 432214797Sjamie sprintf(devpath, "%s/dev", path); 433214805Sjamie if (check_path(j, "mount.devfs", devpath, 0, 434223442Sjamie down ? "devfs" : NULL) < 0) 435214797Sjamie return -1; 436214117Sjamie if (down) { 437214117Sjamie *(const char **)&argv[0] = "/sbin/umount"; 438214797Sjamie argv[1] = devpath; 439214117Sjamie argv[2] = NULL; 440214117Sjamie } else { 441232242Sjamie *(const char **)&argv[0] = _PATH_MOUNT; 442232242Sjamie *(const char **)&argv[1] = "-t"; 443232242Sjamie *(const char **)&argv[2] = "devfs"; 444232242Sjamie ruleset = string_param(j->intparams[KP_DEVFS_RULESET]); 445232242Sjamie if (!ruleset) 446232242Sjamie ruleset = "4"; /* devfsrules_jail */ 447232242Sjamie argv[3] = alloca(11 + strlen(ruleset)); 448232242Sjamie sprintf(argv[3], "-oruleset=%s", ruleset); 449232242Sjamie *(const char **)&argv[4] = "."; 450232242Sjamie argv[5] = devpath; 451232242Sjamie argv[6] = NULL; 452214117Sjamie } 453216367Sjamie break; 454216367Sjamie 455216367Sjamie case IP_COMMAND: 456216367Sjamie if (j->name != NULL) 457216367Sjamie goto default_command; 458214117Sjamie argc = 0; 459223188Sjamie TAILQ_FOREACH(s, &j->intparams[IP_COMMAND]->val, tq) 460214117Sjamie argc++; 461214117Sjamie argv = alloca((argc + 1) * sizeof(char *)); 462214117Sjamie argc = 0; 463223188Sjamie TAILQ_FOREACH(s, &j->intparams[IP_COMMAND]->val, tq) 464214117Sjamie argv[argc++] = s->s; 465214117Sjamie argv[argc] = NULL; 466223368Sjamie j->comstring = &dummystring; 467216367Sjamie break; 468216367Sjamie 469216367Sjamie default: 470216367Sjamie default_command: 471216367Sjamie if ((cs = strpbrk(comstring->s, "!\"$&'()*;<>?[\\]`{|}~")) && 472216367Sjamie !(cs[0] == '&' && cs[1] == '\0')) { 473216367Sjamie argv = alloca(4 * sizeof(char *)); 474216367Sjamie *(const char **)&argv[0] = _PATH_BSHELL; 475216367Sjamie *(const char **)&argv[1] = "-c"; 476216367Sjamie argv[2] = comstring->s; 477216367Sjamie argv[3] = NULL; 478216367Sjamie } else { 479216367Sjamie if (cs) { 480216367Sjamie *cs = 0; 481216367Sjamie bg = 1; 482216367Sjamie } 483216367Sjamie comcs = alloca(comstring->len + 1); 484216367Sjamie strcpy(comcs, comstring->s); 485216367Sjamie argc = 0; 486216367Sjamie for (cs = strtok(comcs, " \t\f\v\r\n"); cs; 487216367Sjamie cs = strtok(NULL, " \t\f\v\r\n")) 488216367Sjamie argc++; 489216367Sjamie argv = alloca((argc + 1) * sizeof(char *)); 490216367Sjamie strcpy(comcs, comstring->s); 491216367Sjamie argc = 0; 492216367Sjamie for (cs = strtok(comcs, " \t\f\v\r\n"); cs; 493216367Sjamie cs = strtok(NULL, " \t\f\v\r\n")) 494216367Sjamie argv[argc++] = cs; 495216367Sjamie argv[argc] = NULL; 496214117Sjamie } 497214117Sjamie } 498223189Sjamie if (argv[0] == NULL) 499223189Sjamie return 0; 500216367Sjamie 501214117Sjamie if (int_param(j->intparams[IP_EXEC_TIMEOUT], &timeout) && 502214117Sjamie timeout != 0) { 503214117Sjamie clock_gettime(CLOCK_REALTIME, &j->timeout); 504214117Sjamie j->timeout.tv_sec += timeout; 505214117Sjamie } else 506214117Sjamie j->timeout.tv_sec = 0; 507214117Sjamie 508214117Sjamie injail = comparam == IP_EXEC_START || comparam == IP_COMMAND || 509214117Sjamie comparam == IP_EXEC_STOP; 510214117Sjamie clean = bool_param(j->intparams[IP_EXEC_CLEAN]); 511214117Sjamie username = string_param(j->intparams[injail 512214117Sjamie ? IP_EXEC_JAIL_USER : IP_EXEC_SYSTEM_USER]); 513214117Sjamie sjuser = bool_param(j->intparams[IP_EXEC_SYSTEM_JAIL_USER]); 514214117Sjamie 515214117Sjamie consfd = 0; 516214117Sjamie if (injail && 517214117Sjamie (conslog = string_param(j->intparams[IP_EXEC_CONSOLELOG]))) { 518223442Sjamie if (check_path(j, "exec.consolelog", conslog, 1, NULL) < 0) 519214797Sjamie return -1; 520214117Sjamie consfd = 521214117Sjamie open(conslog, O_WRONLY | O_CREAT | O_APPEND, DEFFILEMODE); 522214117Sjamie if (consfd < 0) { 523214117Sjamie jail_warnx(j, "open %s: %s", conslog, strerror(errno)); 524214117Sjamie return -1; 525214117Sjamie } 526214117Sjamie } 527214117Sjamie 528214117Sjamie comlen = 0; 529214117Sjamie for (i = 0; argv[i]; i++) 530214117Sjamie comlen += strlen(argv[i]) + 1; 531214117Sjamie j->comline = cs = emalloc(comlen); 532214117Sjamie for (i = 0; argv[i]; i++) { 533214117Sjamie strcpy(cs, argv[i]); 534214117Sjamie if (argv[i + 1]) { 535214117Sjamie cs += strlen(argv[i]) + 1; 536214117Sjamie cs[-1] = ' '; 537214117Sjamie } 538214117Sjamie } 539214117Sjamie if (verbose > 0) 540214117Sjamie jail_note(j, "run command%s%s%s: %s\n", 541214117Sjamie injail ? " in jail" : "", username ? " as " : "", 542214117Sjamie username ? username : "", j->comline); 543214117Sjamie 544214117Sjamie pid = fork(); 545214117Sjamie if (pid < 0) 546214117Sjamie err(1, "fork"); 547214117Sjamie if (pid > 0) { 548246804Sjamie if (bg || !add_proc(j, pid)) { 549216367Sjamie free(j->comline); 550216367Sjamie j->comline = NULL; 551223442Sjamie return 0; 552214117Sjamie } else { 553216367Sjamie paralimit--; 554223442Sjamie return 1; 555214117Sjamie } 556214117Sjamie } 557214117Sjamie if (bg) 558214117Sjamie setsid(); 559214117Sjamie 560223189Sjamie /* Set up the environment and run the command */ 561214117Sjamie pwd = NULL; 562214117Sjamie lcap = NULL; 563214117Sjamie if ((clean || username) && injail && sjuser && 564214117Sjamie get_user_info(j, username, &pwd, &lcap) < 0) 565214117Sjamie exit(1); 566214117Sjamie if (injail) { 567214117Sjamie /* jail_attach won't chdir along with its chroot. */ 568214117Sjamie path = string_param(j->intparams[KP_PATH]); 569214117Sjamie if (path && chdir(path) < 0) { 570214117Sjamie jail_warnx(j, "chdir %s: %s", path, strerror(errno)); 571214117Sjamie exit(1); 572214117Sjamie } 573214117Sjamie if (int_param(j->intparams[IP_EXEC_FIB], &fib) && 574214117Sjamie setfib(fib) < 0) { 575214117Sjamie jail_warnx(j, "setfib: %s", strerror(errno)); 576214117Sjamie exit(1); 577214117Sjamie } 578214117Sjamie if (jail_attach(j->jid) < 0) { 579214117Sjamie jail_warnx(j, "jail_attach: %s", strerror(errno)); 580214117Sjamie exit(1); 581214117Sjamie } 582214117Sjamie } 583214117Sjamie if (clean || username) { 584214117Sjamie if (!(injail && sjuser) && 585214117Sjamie get_user_info(j, username, &pwd, &lcap) < 0) 586214117Sjamie exit(1); 587214117Sjamie if (clean) { 588214117Sjamie term = getenv("TERM"); 589214117Sjamie environ = &cleanenv; 590214117Sjamie setenv("PATH", "/bin:/usr/bin", 0); 591235949Sjamie if (term != NULL) 592235949Sjamie setenv("TERM", term, 1); 593214117Sjamie } 594214117Sjamie if (setusercontext(lcap, pwd, pwd->pw_uid, username 595214117Sjamie ? LOGIN_SETALL & ~LOGIN_SETGROUP & ~LOGIN_SETLOGIN 596214117Sjamie : LOGIN_SETPATH | LOGIN_SETENV) < 0) { 597214117Sjamie jail_warnx(j, "setusercontext %s: %s", pwd->pw_name, 598214117Sjamie strerror(errno)); 599214117Sjamie exit(1); 600214117Sjamie } 601214117Sjamie login_close(lcap); 602214117Sjamie setenv("USER", pwd->pw_name, 1); 603214117Sjamie setenv("HOME", pwd->pw_dir, 1); 604214117Sjamie setenv("SHELL", 605214117Sjamie *pwd->pw_shell ? pwd->pw_shell : _PATH_BSHELL, 1); 606214117Sjamie if (clean && chdir(pwd->pw_dir) < 0) { 607214117Sjamie jail_warnx(j, "chdir %s: %s", 608214117Sjamie pwd->pw_dir, strerror(errno)); 609214117Sjamie exit(1); 610214117Sjamie } 611214117Sjamie endpwent(); 612214117Sjamie } 613214117Sjamie 614214117Sjamie if (consfd != 0 && (dup2(consfd, 1) < 0 || dup2(consfd, 2) < 0)) { 615214117Sjamie jail_warnx(j, "exec.consolelog: %s", strerror(errno)); 616214117Sjamie exit(1); 617214117Sjamie } 618214117Sjamie closefrom(3); 619214117Sjamie execvp(argv[0], argv); 620214117Sjamie jail_warnx(j, "exec %s: %s", argv[0], strerror(errno)); 621214117Sjamie exit(1); 622214117Sjamie} 623214117Sjamie 624214117Sjamie/* 625214117Sjamie * Add a process to the hash, tied to a jail. 626214117Sjamie */ 627246804Sjamiestatic int 628214117Sjamieadd_proc(struct cfjail *j, pid_t pid) 629214117Sjamie{ 630214117Sjamie struct kevent ke; 631214117Sjamie struct cfjail *tj; 632214117Sjamie struct phash *ph; 633214117Sjamie 634214117Sjamie if (!kq && (kq = kqueue()) < 0) 635214117Sjamie err(1, "kqueue"); 636214117Sjamie EV_SET(&ke, pid, EVFILT_PROC, EV_ADD, NOTE_EXIT, 0, NULL); 637246804Sjamie if (kevent(kq, &ke, 1, NULL, 0, NULL) < 0) { 638246804Sjamie if (errno == ESRCH) 639246804Sjamie return 0; 640214117Sjamie err(1, "kevent"); 641246804Sjamie } 642214117Sjamie ph = emalloc(sizeof(struct phash)); 643214117Sjamie ph->j = j; 644214117Sjamie ph->pid = pid; 645214117Sjamie LIST_INSERT_HEAD(&phash[pid % PHASH_SIZE], ph, le); 646214117Sjamie j->nprocs++; 647216367Sjamie j->flags |= JF_SLEEPQ; 648216367Sjamie if (j->timeout.tv_sec == 0) 649216367Sjamie requeue(j, &sleeping); 650216367Sjamie else { 651216367Sjamie /* File the jail in the sleep queue acording to its timeout. */ 652214117Sjamie TAILQ_REMOVE(j->queue, j, tq); 653214117Sjamie TAILQ_FOREACH(tj, &sleeping, tq) { 654214117Sjamie if (!tj->timeout.tv_sec || 655214117Sjamie j->timeout.tv_sec < tj->timeout.tv_sec || 656214117Sjamie (j->timeout.tv_sec == tj->timeout.tv_sec && 657214117Sjamie j->timeout.tv_nsec <= tj->timeout.tv_nsec)) { 658214117Sjamie TAILQ_INSERT_BEFORE(tj, j, tq); 659214117Sjamie break; 660214117Sjamie } 661214117Sjamie } 662214117Sjamie if (tj == NULL) 663214117Sjamie TAILQ_INSERT_TAIL(&sleeping, j, tq); 664214117Sjamie j->queue = &sleeping; 665216367Sjamie } 666246804Sjamie return 1; 667214117Sjamie} 668214117Sjamie 669214117Sjamie/* 670214117Sjamie * Remove any processes from the hash that correspond to a jail. 671214117Sjamie */ 672214117Sjamiestatic void 673214117Sjamieclear_procs(struct cfjail *j) 674214117Sjamie{ 675214117Sjamie struct kevent ke; 676214117Sjamie struct phash *ph, *tph; 677214117Sjamie int i; 678214117Sjamie 679214117Sjamie j->nprocs = 0; 680214117Sjamie for (i = 0; i < PHASH_SIZE; i++) 681214117Sjamie LIST_FOREACH_SAFE(ph, &phash[i], le, tph) 682214117Sjamie if (ph->j == j) { 683214117Sjamie EV_SET(&ke, ph->pid, EVFILT_PROC, EV_DELETE, 684214117Sjamie NOTE_EXIT, 0, NULL); 685214117Sjamie (void)kevent(kq, &ke, 1, NULL, 0, NULL); 686214117Sjamie LIST_REMOVE(ph, le); 687214117Sjamie free(ph); 688214117Sjamie } 689214117Sjamie} 690214117Sjamie 691214117Sjamie/* 692214117Sjamie * Find the jail that corresponds to an exited process. 693214117Sjamie */ 694214117Sjamiestatic struct cfjail * 695214117Sjamiefind_proc(pid_t pid) 696214117Sjamie{ 697214117Sjamie struct cfjail *j; 698214117Sjamie struct phash *ph; 699214117Sjamie 700214117Sjamie LIST_FOREACH(ph, &phash[pid % PHASH_SIZE], le) 701214117Sjamie if (ph->pid == pid) { 702214117Sjamie j = ph->j; 703214117Sjamie LIST_REMOVE(ph, le); 704214117Sjamie free(ph); 705214117Sjamie return --j->nprocs ? NULL : j; 706214117Sjamie } 707214117Sjamie return NULL; 708214117Sjamie} 709214117Sjamie 710214117Sjamie/* 711216367Sjamie * Send SIGTERM to all processes in a jail and wait for them to die. 712216367Sjamie */ 713216367Sjamiestatic int 714216367Sjamieterm_procs(struct cfjail *j) 715216367Sjamie{ 716216367Sjamie struct kinfo_proc *ki; 717216367Sjamie int i, noted, pcnt, timeout; 718216367Sjamie 719216367Sjamie static kvm_t *kd; 720216367Sjamie 721216367Sjamie if (!int_param(j->intparams[IP_STOP_TIMEOUT], &timeout)) 722216367Sjamie timeout = DEFAULT_STOP_TIMEOUT; 723216367Sjamie else if (timeout == 0) 724216367Sjamie return 0; 725216367Sjamie 726216367Sjamie if (kd == NULL) { 727231238Sjamie kd = kvm_open(NULL, NULL, NULL, O_RDONLY, NULL); 728216367Sjamie if (kd == NULL) 729231238Sjamie return 0; 730216367Sjamie } 731216367Sjamie 732216367Sjamie ki = kvm_getprocs(kd, KERN_PROC_PROC, 0, &pcnt); 733216367Sjamie if (ki == NULL) 734231238Sjamie return 0; 735216367Sjamie noted = 0; 736216367Sjamie for (i = 0; i < pcnt; i++) 737216367Sjamie if (ki[i].ki_jid == j->jid && 738216367Sjamie kill(ki[i].ki_pid, SIGTERM) == 0) { 739246804Sjamie (void)add_proc(j, ki[i].ki_pid); 740216367Sjamie if (verbose > 0) { 741216367Sjamie if (!noted) { 742216367Sjamie noted = 1; 743216367Sjamie jail_note(j, "sent SIGTERM to:"); 744216367Sjamie } 745216367Sjamie printf(" %d", ki[i].ki_pid); 746216367Sjamie } 747216367Sjamie } 748216367Sjamie if (noted) 749216367Sjamie printf("\n"); 750216367Sjamie if (j->nprocs > 0) { 751216367Sjamie clock_gettime(CLOCK_REALTIME, &j->timeout); 752216367Sjamie j->timeout.tv_sec += timeout; 753216367Sjamie return 1; 754216367Sjamie } 755216367Sjamie return 0; 756216367Sjamie} 757216367Sjamie 758216367Sjamie/* 759214117Sjamie * Look up a user in the passwd and login.conf files. 760214117Sjamie */ 761214117Sjamiestatic int 762214117Sjamieget_user_info(struct cfjail *j, const char *username, 763214117Sjamie const struct passwd **pwdp, login_cap_t **lcapp) 764214117Sjamie{ 765214117Sjamie const struct passwd *pwd; 766214117Sjamie 767214117Sjamie *pwdp = pwd = username ? getpwnam(username) : getpwuid(getuid()); 768214117Sjamie if (pwd == NULL) { 769214117Sjamie if (errno) 770214117Sjamie jail_warnx(j, "getpwnam%s%s: %s", username ? " " : "", 771214117Sjamie username ? username : "", strerror(errno)); 772214117Sjamie else if (username) 773214117Sjamie jail_warnx(j, "%s: no such user", username); 774214117Sjamie else 775214117Sjamie jail_warnx(j, "unknown uid %d", getuid()); 776214117Sjamie return -1; 777214117Sjamie } 778214117Sjamie *lcapp = login_getpwclass(pwd); 779214117Sjamie if (*lcapp == NULL) { 780214117Sjamie jail_warnx(j, "getpwclass %s: %s", pwd->pw_name, 781214117Sjamie strerror(errno)); 782214117Sjamie return -1; 783214117Sjamie } 784214117Sjamie /* Set the groups while the group file is still available */ 785214117Sjamie if (initgroups(pwd->pw_name, pwd->pw_gid) < 0) { 786214117Sjamie jail_warnx(j, "initgroups %s: %s", pwd->pw_name, 787214117Sjamie strerror(errno)); 788214117Sjamie return -1; 789214117Sjamie } 790214117Sjamie return 0; 791214117Sjamie} 792214797Sjamie 793214797Sjamie/* 794214797Sjamie * Make sure a mount or consolelog path is a valid absolute pathname 795214797Sjamie * with no symlinks. 796214797Sjamie */ 797214797Sjamiestatic int 798214805Sjamiecheck_path(struct cfjail *j, const char *pname, const char *path, int isfile, 799214805Sjamie const char *umount_type) 800214797Sjamie{ 801214805Sjamie struct stat st, mpst; 802214805Sjamie struct statfs stfs; 803214797Sjamie char *tpath, *p; 804214797Sjamie const char *jailpath; 805214797Sjamie size_t jplen; 806214797Sjamie 807214797Sjamie if (path[0] != '/') { 808214797Sjamie jail_warnx(j, "%s: %s: not an absolute pathname", 809214797Sjamie pname, path); 810214797Sjamie return -1; 811214797Sjamie } 812214797Sjamie /* 813214797Sjamie * Only check for symlinks in components below the jail's path, 814214797Sjamie * since that's where the security risk lies. 815214797Sjamie */ 816214797Sjamie jailpath = string_param(j->intparams[KP_PATH]); 817214797Sjamie if (jailpath == NULL) 818214797Sjamie jailpath = ""; 819214797Sjamie jplen = strlen(jailpath); 820214805Sjamie if (!strncmp(path, jailpath, jplen) && path[jplen] == '/') { 821214805Sjamie tpath = alloca(strlen(path) + 1); 822214805Sjamie strcpy(tpath, path); 823214805Sjamie for (p = tpath + jplen; p != NULL; ) { 824214805Sjamie p = strchr(p + 1, '/'); 825214805Sjamie if (p) 826214805Sjamie *p = '\0'; 827214805Sjamie if (lstat(tpath, &st) < 0) { 828214805Sjamie if (errno == ENOENT && isfile && !p) 829214805Sjamie break; 830214805Sjamie jail_warnx(j, "%s: %s: %s", pname, tpath, 831214805Sjamie strerror(errno)); 832214805Sjamie return -1; 833214805Sjamie } 834214805Sjamie if (S_ISLNK(st.st_mode)) { 835214805Sjamie jail_warnx(j, "%s: %s is a symbolic link", 836214805Sjamie pname, tpath); 837214805Sjamie return -1; 838214805Sjamie } 839214805Sjamie if (p) 840214805Sjamie *p = '/'; 841214805Sjamie } 842214805Sjamie } 843214805Sjamie if (umount_type != NULL) { 844214805Sjamie if (stat(path, &st) < 0 || statfs(path, &stfs) < 0) { 845214805Sjamie jail_warnx(j, "%s: %s: %s", pname, path, 846214797Sjamie strerror(errno)); 847214797Sjamie return -1; 848214797Sjamie } 849214805Sjamie if (stat(stfs.f_mntonname, &mpst) < 0) { 850214805Sjamie jail_warnx(j, "%s: %s: %s", pname, stfs.f_mntonname, 851214805Sjamie strerror(errno)); 852214797Sjamie return -1; 853214797Sjamie } 854214805Sjamie if (st.st_ino != mpst.st_ino) { 855214805Sjamie jail_warnx(j, "%s: %s: not a mount point", 856214805Sjamie pname, path); 857214805Sjamie return -1; 858214805Sjamie } 859214805Sjamie if (strcmp(stfs.f_fstypename, umount_type)) { 860214805Sjamie jail_warnx(j, "%s: %s: not a %s mount", 861214805Sjamie pname, path, umount_type); 862214805Sjamie return -1; 863214805Sjamie } 864214797Sjamie } 865214797Sjamie return 0; 866214797Sjamie} 867