2 * Copyright (c) 2011 James Gritton
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
14 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
18 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27 #include <sys/cdefs.h>
28 __FBSDID("$FreeBSD$");
30 #include <sys/types.h>
31 #include <sys/event.h>
32 #include <sys/mount.h>
34 #include <sys/sysctl.h>
42 #include <login_cap.h>
53 #define DEFAULT_STOP_TIMEOUT 10
54 #define PHASH_SIZE 256
56 LIST_HEAD(phhead, phash);
66 extern char **environ;
68 static int run_command(struct cfjail *j);
69 static int add_proc(struct cfjail *j, pid_t pid);
70 static void clear_procs(struct cfjail *j);
71 static struct cfjail *find_proc(pid_t pid);
72 static int term_procs(struct cfjail *j);
73 static int get_user_info(struct cfjail *j, const char *username,
74 const struct passwd **pwdp, login_cap_t **lcapp);
75 static int check_path(struct cfjail *j, const char *pname, const char *path,
76 int isfile, const char *umount_type);
78 static struct cfjails sleeping = TAILQ_HEAD_INITIALIZER(sleeping);
79 static struct cfjails runnable = TAILQ_HEAD_INITIALIZER(runnable);
80 static struct cfstring dummystring = { .len = 1 };
81 static struct phhead phash[PHASH_SIZE];
85 * Run the next command associated with a jail.
88 next_command(struct cfjail *j)
90 enum intparam comparam;
91 int create_failed, stopping;
94 requeue(j, &runnable);
97 create_failed = (j->flags & (JF_STOP | JF_FAILED)) == JF_FAILED;
98 stopping = (j->flags & JF_STOP) != 0;
99 comparam = *j->comparam;
101 if (j->comstring == NULL) {
102 j->comparam += create_failed ? -1 : 1;
103 switch ((comparam = *j->comparam)) {
107 if (!bool_param(j->intparams[IP_MOUNT_DEVFS]))
109 j->comstring = &dummystring;
111 case IP_MOUNT_FDESCFS:
112 if (!bool_param(j->intparams[IP_MOUNT_FDESCFS]))
114 j->comstring = &dummystring;
116 case IP_STOP_TIMEOUT:
117 j->comstring = &dummystring;
120 if (j->intparams[comparam] == NULL)
122 j->comstring = create_failed || (stopping &&
123 (j->intparams[comparam]->flags & PF_REV))
124 ? TAILQ_LAST(&j->intparams[comparam]->val,
126 : TAILQ_FIRST(&j->intparams[comparam]->val);
129 j->comstring = j->comstring == &dummystring ? NULL :
130 create_failed || (stopping &&
131 (j->intparams[comparam]->flags & PF_REV))
132 ? TAILQ_PREV(j->comstring, cfstrings, tq)
133 : TAILQ_NEXT(j->comstring, tq);
135 if (j->comstring == NULL || j->comstring->len == 0 ||
136 (create_failed && (comparam == IP_EXEC_PRESTART ||
137 comparam == IP_EXEC_START || comparam == IP_COMMAND ||
138 comparam == IP_EXEC_POSTSTART)))
140 switch (run_command(j)) {
151 * Check command exit status
154 finish_command(struct cfjail *j)
158 if (!(j->flags & JF_SLEEPQ))
160 j->flags &= ~JF_SLEEPQ;
161 if (*j->comparam == IP_STOP_TIMEOUT)
163 j->flags &= ~JF_TIMEOUT;
168 if (!TAILQ_EMPTY(&runnable))
169 requeue(TAILQ_FIRST(&runnable), &ready);
171 if (j->flags & JF_TIMEOUT) {
172 j->flags &= ~JF_TIMEOUT;
173 if (*j->comparam != IP_STOP_TIMEOUT) {
174 jail_warnx(j, "%s: timed out", j->comline);
177 } else if (verbose > 0)
178 jail_note(j, "timed out\n");
179 } else if (j->pstatus != 0) {
180 if (WIFSIGNALED(j->pstatus))
181 jail_warnx(j, "%s: exited on signal %d",
182 j->comline, WTERMSIG(j->pstatus));
184 jail_warnx(j, "%s: failed", j->comline);
195 * Check for finished processes or timeouts.
198 next_proc(int nonblock)
202 struct timespec *tsp;
205 if (!TAILQ_EMPTY(&sleeping)) {
208 if ((j = TAILQ_FIRST(&sleeping)) && j->timeout.tv_sec) {
209 clock_gettime(CLOCK_REALTIME, &ts);
210 ts.tv_sec = j->timeout.tv_sec - ts.tv_sec;
211 ts.tv_nsec = j->timeout.tv_nsec - ts.tv_nsec;
212 if (ts.tv_nsec < 0) {
214 ts.tv_nsec += 1000000000;
217 (ts.tv_sec == 0 && ts.tv_nsec == 0)) {
218 j->flags |= JF_TIMEOUT;
229 switch (kevent(kq, NULL, 0, &ke, 1, tsp)) {
236 j = TAILQ_FIRST(&sleeping);
237 j->flags |= JF_TIMEOUT;
243 (void)waitpid(ke.ident, NULL, WNOHANG);
244 if ((j = find_proc(ke.ident))) {
245 j->pstatus = ke.data;
255 * Run a single command for a jail, possible inside the jail.
258 run_command(struct cfjail *j)
260 const struct passwd *pwd;
261 const struct cfstring *comstring, *s;
264 char *cs, *comcs, *devpath;
265 const char *jidstr, *conslog, *path, *ruleset, *term, *username;
266 enum intparam comparam;
269 int argc, bg, clean, consfd, down, fib, i, injail, sjuser, timeout;
270 #if defined(INET) || defined(INET6)
274 static char *cleanenv;
276 /* Perform some operations that aren't actually commands */
277 comparam = *j->comparam;
278 down = j->flags & (JF_STOP | JF_FAILED);
280 case IP_STOP_TIMEOUT:
281 return term_procs(j);
285 if (jail_remove(j->jid) < 0 && errno == EPERM) {
286 jail_warnx(j, "jail_remove: %s",
290 if (verbose > 0 || (verbose == 0 && (j->flags & JF_STOP
291 ? note_remove : j->name != NULL)))
292 jail_note(j, "removed\n");
294 if (j->flags & JF_STOP)
295 dep_done(j, DF_LIGHT);
297 j->flags &= ~JF_PERSIST;
299 if (create_jail(j) < 0)
302 printf("%d\n", j->jid);
303 if (verbose >= 0 && (j->name || verbose > 0))
304 jail_note(j, "created\n");
305 dep_done(j, DF_LIGHT);
312 * Collect exec arguments. Internal commands for network and
313 * mounting build their own argument lists.
315 comstring = j->comstring;
320 argv = alloca(8 * sizeof(char *));
321 *(const char **)&argv[0] = _PATH_IFCONFIG;
322 if ((cs = strchr(comstring->s, '|'))) {
323 argv[1] = alloca(cs - comstring->s + 1);
324 strlcpy(argv[1], comstring->s, cs - comstring->s + 1);
327 *(const char **)&argv[1] =
328 string_param(j->intparams[IP_INTERFACE]);
331 *(const char **)&argv[2] = "inet";
332 if (!(cs = strchr(addr, '/'))) {
334 *(const char **)&argv[4] = "netmask";
335 *(const char **)&argv[5] = "255.255.255.255";
337 } else if (strchr(cs + 1, '.')) {
338 argv[3] = alloca(cs - addr + 1);
339 strlcpy(argv[3], addr, cs - addr + 1);
340 *(const char **)&argv[4] = "netmask";
341 *(const char **)&argv[5] = cs + 1;
347 *(const char **)&argv[argc] = down ? "-alias" : "alias";
348 argv[argc + 1] = NULL;
354 argv = alloca(8 * sizeof(char *));
355 *(const char **)&argv[0] = _PATH_IFCONFIG;
356 if ((cs = strchr(comstring->s, '|'))) {
357 argv[1] = alloca(cs - comstring->s + 1);
358 strlcpy(argv[1], comstring->s, cs - comstring->s + 1);
361 *(const char **)&argv[1] =
362 string_param(j->intparams[IP_INTERFACE]);
365 *(const char **)&argv[2] = "inet6";
367 if (!(cs = strchr(addr, '/'))) {
368 *(const char **)&argv[4] = "prefixlen";
369 *(const char **)&argv[5] = "128";
373 *(const char **)&argv[argc] = down ? "-alias" : "alias";
374 argv[argc + 1] = NULL;
378 case IP_VNET_INTERFACE:
379 argv = alloca(5 * sizeof(char *));
380 *(const char **)&argv[0] = _PATH_IFCONFIG;
381 argv[1] = comstring->s;
382 *(const char **)&argv[2] = down ? "-vnet" : "vnet";
383 jidstr = string_param(j->intparams[KP_JID]);
384 *(const char **)&argv[3] =
385 jidstr ? jidstr : string_param(j->intparams[KP_NAME]);
390 case IP__MOUNT_FROM_FSTAB:
391 argv = alloca(8 * sizeof(char *));
392 comcs = alloca(comstring->len + 1);
393 strcpy(comcs, comstring->s);
395 for (cs = strtok(comcs, " \t\f\v\r\n"); cs && argc < 4;
396 cs = strtok(NULL, " \t\f\v\r\n"))
401 jail_warnx(j, "%s: %s: missing information",
402 j->intparams[comparam]->name, comstring->s);
405 if (check_path(j, j->intparams[comparam]->name, argv[1], 0,
406 down ? argv[2] : NULL) < 0)
411 *(const char **)&argv[0] = "/sbin/umount";
418 *(const char **)&argv[3] = "-o";
424 *(const char **)&argv[0] = _PATH_MOUNT;
426 *(const char **)&argv[1] = "-t";
430 argv = alloca(7 * sizeof(char *));
431 path = string_param(j->intparams[KP_PATH]);
433 jail_warnx(j, "mount.devfs: no path");
436 devpath = alloca(strlen(path) + 5);
437 sprintf(devpath, "%s/dev", path);
438 if (check_path(j, "mount.devfs", devpath, 0,
439 down ? "devfs" : NULL) < 0)
442 *(const char **)&argv[0] = "/sbin/umount";
446 *(const char **)&argv[0] = _PATH_MOUNT;
447 *(const char **)&argv[1] = "-t";
448 *(const char **)&argv[2] = "devfs";
449 ruleset = string_param(j->intparams[KP_DEVFS_RULESET]);
451 ruleset = "4"; /* devfsrules_jail */
452 argv[3] = alloca(11 + strlen(ruleset));
453 sprintf(argv[3], "-oruleset=%s", ruleset);
454 *(const char **)&argv[4] = ".";
460 case IP_MOUNT_FDESCFS:
461 argv = alloca(7 * sizeof(char *));
462 path = string_param(j->intparams[KP_PATH]);
464 jail_warnx(j, "mount.fdescfs: no path");
467 devpath = alloca(strlen(path) + 8);
468 sprintf(devpath, "%s/dev/fd", path);
469 if (check_path(j, "mount.fdescfs", devpath, 0,
470 down ? "fdescfs" : NULL) < 0)
473 *(const char **)&argv[0] = "/sbin/umount";
477 *(const char **)&argv[0] = _PATH_MOUNT;
478 *(const char **)&argv[1] = "-t";
479 *(const char **)&argv[2] = "fdescfs";
480 *(const char **)&argv[3] = ".";
488 goto default_command;
490 TAILQ_FOREACH(s, &j->intparams[IP_COMMAND]->val, tq)
492 argv = alloca((argc + 1) * sizeof(char *));
494 TAILQ_FOREACH(s, &j->intparams[IP_COMMAND]->val, tq)
497 j->comstring = &dummystring;
502 if ((cs = strpbrk(comstring->s, "!\"$&'()*;<>?[\\]`{|}~")) &&
503 !(cs[0] == '&' && cs[1] == '\0')) {
504 argv = alloca(4 * sizeof(char *));
505 *(const char **)&argv[0] = _PATH_BSHELL;
506 *(const char **)&argv[1] = "-c";
507 argv[2] = comstring->s;
514 comcs = alloca(comstring->len + 1);
515 strcpy(comcs, comstring->s);
517 for (cs = strtok(comcs, " \t\f\v\r\n"); cs;
518 cs = strtok(NULL, " \t\f\v\r\n"))
520 argv = alloca((argc + 1) * sizeof(char *));
521 strcpy(comcs, comstring->s);
523 for (cs = strtok(comcs, " \t\f\v\r\n"); cs;
524 cs = strtok(NULL, " \t\f\v\r\n"))
532 if (int_param(j->intparams[IP_EXEC_TIMEOUT], &timeout) &&
534 clock_gettime(CLOCK_REALTIME, &j->timeout);
535 j->timeout.tv_sec += timeout;
537 j->timeout.tv_sec = 0;
539 injail = comparam == IP_EXEC_START || comparam == IP_COMMAND ||
540 comparam == IP_EXEC_STOP;
541 clean = bool_param(j->intparams[IP_EXEC_CLEAN]);
542 username = string_param(j->intparams[injail
543 ? IP_EXEC_JAIL_USER : IP_EXEC_SYSTEM_USER]);
544 sjuser = bool_param(j->intparams[IP_EXEC_SYSTEM_JAIL_USER]);
548 (conslog = string_param(j->intparams[IP_EXEC_CONSOLELOG]))) {
549 if (check_path(j, "exec.consolelog", conslog, 1, NULL) < 0)
552 open(conslog, O_WRONLY | O_CREAT | O_APPEND, DEFFILEMODE);
554 jail_warnx(j, "open %s: %s", conslog, strerror(errno));
560 for (i = 0; argv[i]; i++)
561 comlen += strlen(argv[i]) + 1;
562 j->comline = cs = emalloc(comlen);
563 for (i = 0; argv[i]; i++) {
566 cs += strlen(argv[i]) + 1;
571 jail_note(j, "run command%s%s%s: %s\n",
572 injail ? " in jail" : "", username ? " as " : "",
573 username ? username : "", j->comline);
579 if (bg || !add_proc(j, pid)) {
591 /* Set up the environment and run the command */
594 if ((clean || username) && injail && sjuser &&
595 get_user_info(j, username, &pwd, &lcap) < 0)
598 /* jail_attach won't chdir along with its chroot. */
599 path = string_param(j->intparams[KP_PATH]);
600 if (path && chdir(path) < 0) {
601 jail_warnx(j, "chdir %s: %s", path, strerror(errno));
604 if (int_param(j->intparams[IP_EXEC_FIB], &fib) &&
606 jail_warnx(j, "setfib: %s", strerror(errno));
609 if (jail_attach(j->jid) < 0) {
610 jail_warnx(j, "jail_attach: %s", strerror(errno));
614 if (clean || username) {
615 if (!(injail && sjuser) &&
616 get_user_info(j, username, &pwd, &lcap) < 0)
619 term = getenv("TERM");
621 setenv("PATH", "/bin:/usr/bin", 0);
623 setenv("TERM", term, 1);
625 if (setusercontext(lcap, pwd, pwd->pw_uid, username
626 ? LOGIN_SETALL & ~LOGIN_SETGROUP & ~LOGIN_SETLOGIN
627 : LOGIN_SETPATH | LOGIN_SETENV) < 0) {
628 jail_warnx(j, "setusercontext %s: %s", pwd->pw_name,
633 setenv("USER", pwd->pw_name, 1);
634 setenv("HOME", pwd->pw_dir, 1);
636 *pwd->pw_shell ? pwd->pw_shell : _PATH_BSHELL, 1);
637 if (clean && chdir(pwd->pw_dir) < 0) {
638 jail_warnx(j, "chdir %s: %s",
639 pwd->pw_dir, strerror(errno));
645 if (consfd != 0 && (dup2(consfd, 1) < 0 || dup2(consfd, 2) < 0)) {
646 jail_warnx(j, "exec.consolelog: %s", strerror(errno));
650 execvp(argv[0], argv);
651 jail_warnx(j, "exec %s: %s", argv[0], strerror(errno));
656 * Add a process to the hash, tied to a jail.
659 add_proc(struct cfjail *j, pid_t pid)
665 if (!kq && (kq = kqueue()) < 0)
667 EV_SET(&ke, pid, EVFILT_PROC, EV_ADD, NOTE_EXIT, 0, NULL);
668 if (kevent(kq, &ke, 1, NULL, 0, NULL) < 0) {
673 ph = emalloc(sizeof(struct phash));
676 LIST_INSERT_HEAD(&phash[pid % PHASH_SIZE], ph, le);
678 j->flags |= JF_SLEEPQ;
679 if (j->timeout.tv_sec == 0)
680 requeue(j, &sleeping);
682 /* File the jail in the sleep queue acording to its timeout. */
683 TAILQ_REMOVE(j->queue, j, tq);
684 TAILQ_FOREACH(tj, &sleeping, tq) {
685 if (!tj->timeout.tv_sec ||
686 j->timeout.tv_sec < tj->timeout.tv_sec ||
687 (j->timeout.tv_sec == tj->timeout.tv_sec &&
688 j->timeout.tv_nsec <= tj->timeout.tv_nsec)) {
689 TAILQ_INSERT_BEFORE(tj, j, tq);
694 TAILQ_INSERT_TAIL(&sleeping, j, tq);
695 j->queue = &sleeping;
701 * Remove any processes from the hash that correspond to a jail.
704 clear_procs(struct cfjail *j)
707 struct phash *ph, *tph;
711 for (i = 0; i < PHASH_SIZE; i++)
712 LIST_FOREACH_SAFE(ph, &phash[i], le, tph)
714 EV_SET(&ke, ph->pid, EVFILT_PROC, EV_DELETE,
716 (void)kevent(kq, &ke, 1, NULL, 0, NULL);
723 * Find the jail that corresponds to an exited process.
725 static struct cfjail *
731 LIST_FOREACH(ph, &phash[pid % PHASH_SIZE], le)
732 if (ph->pid == pid) {
736 return --j->nprocs ? NULL : j;
742 * Send SIGTERM to all processes in a jail and wait for them to die.
745 term_procs(struct cfjail *j)
747 struct kinfo_proc *ki;
748 int i, noted, pcnt, timeout;
752 if (!int_param(j->intparams[IP_STOP_TIMEOUT], &timeout))
753 timeout = DEFAULT_STOP_TIMEOUT;
754 else if (timeout == 0)
758 kd = kvm_open(NULL, NULL, NULL, O_RDONLY, NULL);
763 ki = kvm_getprocs(kd, KERN_PROC_PROC, 0, &pcnt);
767 for (i = 0; i < pcnt; i++)
768 if (ki[i].ki_jid == j->jid &&
769 kill(ki[i].ki_pid, SIGTERM) == 0) {
770 (void)add_proc(j, ki[i].ki_pid);
774 jail_note(j, "sent SIGTERM to:");
776 printf(" %d", ki[i].ki_pid);
782 clock_gettime(CLOCK_REALTIME, &j->timeout);
783 j->timeout.tv_sec += timeout;
790 * Look up a user in the passwd and login.conf files.
793 get_user_info(struct cfjail *j, const char *username,
794 const struct passwd **pwdp, login_cap_t **lcapp)
796 const struct passwd *pwd;
798 *pwdp = pwd = username ? getpwnam(username) : getpwuid(getuid());
801 jail_warnx(j, "getpwnam%s%s: %s", username ? " " : "",
802 username ? username : "", strerror(errno));
804 jail_warnx(j, "%s: no such user", username);
806 jail_warnx(j, "unknown uid %d", getuid());
809 *lcapp = login_getpwclass(pwd);
810 if (*lcapp == NULL) {
811 jail_warnx(j, "getpwclass %s: %s", pwd->pw_name,
815 /* Set the groups while the group file is still available */
816 if (initgroups(pwd->pw_name, pwd->pw_gid) < 0) {
817 jail_warnx(j, "initgroups %s: %s", pwd->pw_name,
825 * Make sure a mount or consolelog path is a valid absolute pathname
829 check_path(struct cfjail *j, const char *pname, const char *path, int isfile,
830 const char *umount_type)
832 struct stat st, mpst;
835 const char *jailpath;
838 if (path[0] != '/') {
839 jail_warnx(j, "%s: %s: not an absolute pathname",
844 * Only check for symlinks in components below the jail's path,
845 * since that's where the security risk lies.
847 jailpath = string_param(j->intparams[KP_PATH]);
848 if (jailpath == NULL)
850 jplen = strlen(jailpath);
851 if (!strncmp(path, jailpath, jplen) && path[jplen] == '/') {
852 tpath = alloca(strlen(path) + 1);
854 for (p = tpath + jplen; p != NULL; ) {
855 p = strchr(p + 1, '/');
858 if (lstat(tpath, &st) < 0) {
859 if (errno == ENOENT && isfile && !p)
861 jail_warnx(j, "%s: %s: %s", pname, tpath,
865 if (S_ISLNK(st.st_mode)) {
866 jail_warnx(j, "%s: %s is a symbolic link",
874 if (umount_type != NULL) {
875 if (stat(path, &st) < 0 || statfs(path, &stfs) < 0) {
876 jail_warnx(j, "%s: %s: %s", pname, path,
880 if (stat(stfs.f_mntonname, &mpst) < 0) {
881 jail_warnx(j, "%s: %s: %s", pname, stfs.f_mntonname,
885 if (st.st_ino != mpst.st_ino) {
886 jail_warnx(j, "%s: %s: not a mount point",
890 if (strcmp(stfs.f_fstypename, umount_type)) {
891 jail_warnx(j, "%s: %s: not a %s mount",
892 pname, path, umount_type);