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$");
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;
109256387Shrs				j->comstring = &dummystring;
110256387Shrs				break;
111256387Shrs			case IP_MOUNT_FDESCFS:
112256387Shrs				if (!bool_param(j->intparams[IP_MOUNT_FDESCFS]))
113256387Shrs					continue;
114256387Shrs				j->comstring = &dummystring;
115223263Sjamie			case IP__OP:
116223189Sjamie			case IP_STOP_TIMEOUT:
117223189Sjamie				j->comstring = &dummystring;
118223189Sjamie				break;
119223189Sjamie			default:
120223189Sjamie				if (j->intparams[comparam] == NULL)
121223189Sjamie					continue;
122248854Sjamie				j->comstring = create_failed || (stopping &&
123248854Sjamie				    (j->intparams[comparam]->flags & PF_REV))
124223189Sjamie				    ? TAILQ_LAST(&j->intparams[comparam]->val,
125223189Sjamie					cfstrings)
126223189Sjamie				    : TAILQ_FIRST(&j->intparams[comparam]->val);
127223189Sjamie			}
128223442Sjamie		} else {
129223442Sjamie			j->comstring = j->comstring == &dummystring ? NULL :
130248854Sjamie			    create_failed || (stopping &&
131248854Sjamie			    (j->intparams[comparam]->flags & PF_REV))
132223442Sjamie			    ? TAILQ_PREV(j->comstring, cfstrings, tq)
133223442Sjamie			    : TAILQ_NEXT(j->comstring, tq);
134223189Sjamie		}
135223442Sjamie		if (j->comstring == NULL || j->comstring->len == 0 ||
136223442Sjamie		    (create_failed && (comparam == IP_EXEC_PRESTART ||
137223442Sjamie		    comparam == IP_EXEC_START || comparam == IP_COMMAND ||
138223442Sjamie		    comparam == IP_EXEC_POSTSTART)))
139223442Sjamie			continue;
140223442Sjamie		switch (run_command(j)) {
141223442Sjamie		case -1:
142223442Sjamie			failed(j);
143223442Sjamie			/* FALLTHROUGH */
144223442Sjamie		case 1:
145223442Sjamie			return 1;
146223189Sjamie		}
147223189Sjamie	}
148223189Sjamie}
149223189Sjamie
150223189Sjamie/*
151223189Sjamie * Check command exit status
152223189Sjamie */
153223189Sjamieint
154223189Sjamiefinish_command(struct cfjail *j)
155223189Sjamie{
156223189Sjamie	int error;
157223189Sjamie
158223189Sjamie	if (!(j->flags & JF_SLEEPQ))
159223189Sjamie		return 0;
160223189Sjamie	j->flags &= ~JF_SLEEPQ;
161223827Sjamie	if (*j->comparam == IP_STOP_TIMEOUT)
162223827Sjamie	{
163223827Sjamie		j->flags &= ~JF_TIMEOUT;
164223827Sjamie		j->pstatus = 0;
165223827Sjamie		return 0;
166223189Sjamie	}
167223827Sjamie	paralimit++;
168223827Sjamie	if (!TAILQ_EMPTY(&runnable))
169223827Sjamie		requeue(TAILQ_FIRST(&runnable), &ready);
170223189Sjamie	error = 0;
171223189Sjamie	if (j->flags & JF_TIMEOUT) {
172223189Sjamie		j->flags &= ~JF_TIMEOUT;
173223189Sjamie		if (*j->comparam != IP_STOP_TIMEOUT) {
174223189Sjamie			jail_warnx(j, "%s: timed out", j->comline);
175223189Sjamie			failed(j);
176223189Sjamie			error = -1;
177223189Sjamie		} else if (verbose > 0)
178223189Sjamie			jail_note(j, "timed out\n");
179223189Sjamie	} else if (j->pstatus != 0) {
180223189Sjamie		if (WIFSIGNALED(j->pstatus))
181223189Sjamie			jail_warnx(j, "%s: exited on signal %d",
182223189Sjamie			    j->comline, WTERMSIG(j->pstatus));
183223189Sjamie		else
184223189Sjamie			jail_warnx(j, "%s: failed", j->comline);
185223189Sjamie		j->pstatus = 0;
186223189Sjamie		failed(j);
187223189Sjamie		error = -1;
188223189Sjamie	}
189223189Sjamie	free(j->comline);
190223189Sjamie	j->comline = NULL;
191223189Sjamie	return error;
192223189Sjamie}
193223189Sjamie
194223189Sjamie/*
195223442Sjamie * Check for finished processes or timeouts.
196223189Sjamie */
197223189Sjamiestruct cfjail *
198223189Sjamienext_proc(int nonblock)
199223189Sjamie{
200223189Sjamie	struct kevent ke;
201223189Sjamie	struct timespec ts;
202223189Sjamie	struct timespec *tsp;
203223189Sjamie	struct cfjail *j;
204223189Sjamie
205223189Sjamie	if (!TAILQ_EMPTY(&sleeping)) {
206223189Sjamie	again:
207223189Sjamie		tsp = NULL;
208223189Sjamie		if ((j = TAILQ_FIRST(&sleeping)) && j->timeout.tv_sec) {
209223189Sjamie			clock_gettime(CLOCK_REALTIME, &ts);
210223189Sjamie			ts.tv_sec = j->timeout.tv_sec - ts.tv_sec;
211223189Sjamie			ts.tv_nsec = j->timeout.tv_nsec - ts.tv_nsec;
212223189Sjamie			if (ts.tv_nsec < 0) {
213223189Sjamie				ts.tv_sec--;
214223189Sjamie				ts.tv_nsec += 1000000000;
215223189Sjamie			}
216223189Sjamie			if (ts.tv_sec < 0 ||
217223189Sjamie			    (ts.tv_sec == 0 && ts.tv_nsec == 0)) {
218223189Sjamie				j->flags |= JF_TIMEOUT;
219223189Sjamie				clear_procs(j);
220223189Sjamie				return j;
221223189Sjamie			}
222223189Sjamie			tsp = &ts;
223223189Sjamie		}
224223189Sjamie		if (nonblock) {
225223189Sjamie			ts.tv_sec = 0;
226223189Sjamie			ts.tv_nsec = 0;
227223189Sjamie			tsp = &ts;
228223189Sjamie		}
229223189Sjamie		switch (kevent(kq, NULL, 0, &ke, 1, tsp)) {
230223189Sjamie		case -1:
231223189Sjamie			if (errno != EINTR)
232223189Sjamie				err(1, "kevent");
233223189Sjamie			goto again;
234223189Sjamie		case 0:
235223189Sjamie			if (!nonblock) {
236223189Sjamie				j = TAILQ_FIRST(&sleeping);
237223189Sjamie				j->flags |= JF_TIMEOUT;
238223189Sjamie				clear_procs(j);
239223189Sjamie				return j;
240223189Sjamie			}
241223189Sjamie			break;
242223189Sjamie		case 1:
243223189Sjamie			(void)waitpid(ke.ident, NULL, WNOHANG);
244223189Sjamie			if ((j = find_proc(ke.ident))) {
245223189Sjamie				j->pstatus = ke.data;
246223189Sjamie				return j;
247223189Sjamie			}
248223189Sjamie			goto again;
249223189Sjamie		}
250223189Sjamie	}
251223189Sjamie	return NULL;
252223189Sjamie}
253223189Sjamie
254223189Sjamie/*
255223189Sjamie * Run a single command for a jail, possible inside the jail.
256223189Sjamie */
257236198Sjamiestatic int
258223189Sjamierun_command(struct cfjail *j)
259223189Sjamie{
260214117Sjamie	const struct passwd *pwd;
261223189Sjamie	const struct cfstring *comstring, *s;
262214117Sjamie	login_cap_t *lcap;
263214117Sjamie	char **argv;
264223351Sjamie	char *cs, *comcs, *devpath;
265214117Sjamie	const char *jidstr, *conslog, *path, *ruleset, *term, *username;
266223189Sjamie	enum intparam comparam;
267214117Sjamie	size_t comlen;
268214117Sjamie	pid_t pid;
269214117Sjamie	int argc, bg, clean, consfd, down, fib, i, injail, sjuser, timeout;
270223351Sjamie#if defined(INET) || defined(INET6)
271223351Sjamie	char *addr;
272223351Sjamie#endif
273214117Sjamie
274214117Sjamie	static char *cleanenv;
275214117Sjamie
276223263Sjamie	/* Perform some operations that aren't actually commands */
277223263Sjamie	comparam = *j->comparam;
278223263Sjamie	down = j->flags & (JF_STOP | JF_FAILED);
279223263Sjamie	switch (comparam) {
280223263Sjamie	case IP_STOP_TIMEOUT:
281223263Sjamie		return term_procs(j);
282223263Sjamie
283223263Sjamie	case IP__OP:
284223263Sjamie		if (down) {
285231238Sjamie			if (jail_remove(j->jid) < 0 && errno == EPERM) {
286231238Sjamie				jail_warnx(j, "jail_remove: %s",
287231238Sjamie					   strerror(errno));
288231238Sjamie				return -1;
289231238Sjamie			}
290223827Sjamie			if (verbose > 0 || (verbose == 0 && (j->flags & JF_STOP
291223263Sjamie			    ? note_remove : j->name != NULL)))
292223263Sjamie			    jail_note(j, "removed\n");
293223263Sjamie			j->jid = -1;
294223263Sjamie			if (j->flags & JF_STOP)
295223263Sjamie				dep_done(j, DF_LIGHT);
296223263Sjamie			else
297223263Sjamie				j->flags &= ~JF_PERSIST;
298223263Sjamie		} else {
299223442Sjamie			if (create_jail(j) < 0)
300223263Sjamie				return -1;
301236198Sjamie			if (iflag)
302236198Sjamie				printf("%d\n", j->jid);
303223263Sjamie			if (verbose >= 0 && (j->name || verbose > 0))
304223263Sjamie				jail_note(j, "created\n");
305223263Sjamie			dep_done(j, DF_LIGHT);
306223263Sjamie		}
307223442Sjamie		return 0;
308223327Sjamie
309223327Sjamie	default: ;
310223263Sjamie	}
311214117Sjamie	/*
312214117Sjamie	 * Collect exec arguments.  Internal commands for network and
313216367Sjamie	 * mounting build their own argument lists.
314214117Sjamie	 */
315223189Sjamie	comstring = j->comstring;
316223189Sjamie	bg = 0;
317216367Sjamie	switch (comparam) {
318223351Sjamie#ifdef INET
319216367Sjamie	case IP__IP4_IFADDR:
320214117Sjamie		argv = alloca(8 * sizeof(char *));
321214117Sjamie		*(const char **)&argv[0] = _PATH_IFCONFIG;
322214117Sjamie		if ((cs = strchr(comstring->s, '|'))) {
323214117Sjamie			argv[1] = alloca(cs - comstring->s + 1);
324214117Sjamie			strlcpy(argv[1], comstring->s, cs - comstring->s + 1);
325214117Sjamie			addr = cs + 1;
326214117Sjamie		} else {
327214117Sjamie			*(const char **)&argv[1] =
328214117Sjamie			    string_param(j->intparams[IP_INTERFACE]);
329214117Sjamie			addr = comstring->s;
330214117Sjamie		}
331214117Sjamie		*(const char **)&argv[2] = "inet";
332214117Sjamie		if (!(cs = strchr(addr, '/'))) {
333214117Sjamie			argv[3] = addr;
334214117Sjamie			*(const char **)&argv[4] = "netmask";
335214117Sjamie			*(const char **)&argv[5] = "255.255.255.255";
336214117Sjamie			argc = 6;
337214117Sjamie		} else if (strchr(cs + 1, '.')) {
338214117Sjamie			argv[3] = alloca(cs - addr + 1);
339214117Sjamie			strlcpy(argv[3], addr, cs - addr + 1);
340214117Sjamie			*(const char **)&argv[4] = "netmask";
341214117Sjamie			*(const char **)&argv[5] = cs + 1;
342214117Sjamie			argc = 6;
343214117Sjamie		} else {
344214117Sjamie			argv[3] = addr;
345214117Sjamie			argc = 4;
346214117Sjamie		}
347214117Sjamie		*(const char **)&argv[argc] = down ? "-alias" : "alias";
348214117Sjamie		argv[argc + 1] = NULL;
349216367Sjamie		break;
350223351Sjamie#endif
351216367Sjamie
352214117Sjamie#ifdef INET6
353216367Sjamie	case IP__IP6_IFADDR:
354214117Sjamie		argv = alloca(8 * sizeof(char *));
355214117Sjamie		*(const char **)&argv[0] = _PATH_IFCONFIG;
356214117Sjamie		if ((cs = strchr(comstring->s, '|'))) {
357214117Sjamie			argv[1] = alloca(cs - comstring->s + 1);
358214117Sjamie			strlcpy(argv[1], comstring->s, cs - comstring->s + 1);
359214117Sjamie			addr = cs + 1;
360214117Sjamie		} else {
361214117Sjamie			*(const char **)&argv[1] =
362214117Sjamie			    string_param(j->intparams[IP_INTERFACE]);
363214117Sjamie			addr = comstring->s;
364214117Sjamie		}
365214117Sjamie		*(const char **)&argv[2] = "inet6";
366214117Sjamie		argv[3] = addr;
367214117Sjamie		if (!(cs = strchr(addr, '/'))) {
368214117Sjamie			*(const char **)&argv[4] = "prefixlen";
369214117Sjamie			*(const char **)&argv[5] = "128";
370214117Sjamie			argc = 6;
371214117Sjamie		} else
372214117Sjamie			argc = 4;
373214117Sjamie		*(const char **)&argv[argc] = down ? "-alias" : "alias";
374214117Sjamie		argv[argc + 1] = NULL;
375216367Sjamie		break;
376214117Sjamie#endif
377216367Sjamie
378216367Sjamie	case IP_VNET_INTERFACE:
379214117Sjamie		argv = alloca(5 * sizeof(char *));
380214117Sjamie		*(const char **)&argv[0] = _PATH_IFCONFIG;
381214117Sjamie		argv[1] = comstring->s;
382214117Sjamie		*(const char **)&argv[2] = down ? "-vnet" : "vnet";
383214117Sjamie		jidstr = string_param(j->intparams[KP_JID]);
384214117Sjamie		*(const char **)&argv[3] =
385214117Sjamie			jidstr ? jidstr : string_param(j->intparams[KP_NAME]);
386214117Sjamie		argv[4] = NULL;
387216367Sjamie		break;
388216367Sjamie
389216367Sjamie	case IP_MOUNT:
390216367Sjamie	case IP__MOUNT_FROM_FSTAB:
391214117Sjamie		argv = alloca(8 * sizeof(char *));
392214117Sjamie		comcs = alloca(comstring->len + 1);
393214117Sjamie		strcpy(comcs, comstring->s);
394214117Sjamie		argc = 0;
395214117Sjamie		for (cs = strtok(comcs, " \t\f\v\r\n"); cs && argc < 4;
396214117Sjamie		     cs = strtok(NULL, " \t\f\v\r\n"))
397214117Sjamie			argv[argc++] = cs;
398214797Sjamie		if (argc == 0)
399223189Sjamie			return 0;
400214117Sjamie		if (argc < 3) {
401214783Sjamie			jail_warnx(j, "%s: %s: missing information",
402214783Sjamie			    j->intparams[comparam]->name, comstring->s);
403214117Sjamie			return -1;
404214117Sjamie		}
405214805Sjamie		if (check_path(j, j->intparams[comparam]->name, argv[1], 0,
406223442Sjamie		    down ? argv[2] : NULL) < 0)
407214797Sjamie			return -1;
408214117Sjamie		if (down) {
409214117Sjamie			argv[4] = NULL;
410214117Sjamie			argv[3] = argv[1];
411214117Sjamie			*(const char **)&argv[0] = "/sbin/umount";
412214117Sjamie		} else {
413214117Sjamie			if (argc == 4) {
414214117Sjamie				argv[7] = NULL;
415214117Sjamie				argv[6] = argv[1];
416214117Sjamie				argv[5] = argv[0];
417214117Sjamie				argv[4] = argv[3];
418214117Sjamie				*(const char **)&argv[3] = "-o";
419214117Sjamie			} else {
420214117Sjamie				argv[5] = NULL;
421214117Sjamie				argv[4] = argv[1];
422214117Sjamie				argv[3] = argv[0];
423214117Sjamie			}
424214117Sjamie			*(const char **)&argv[0] = _PATH_MOUNT;
425214117Sjamie		}
426214117Sjamie		*(const char **)&argv[1] = "-t";
427216367Sjamie		break;
428216367Sjamie
429216367Sjamie	case IP_MOUNT_DEVFS:
430232242Sjamie		argv = alloca(7 * sizeof(char *));
431214117Sjamie		path = string_param(j->intparams[KP_PATH]);
432214117Sjamie		if (path == NULL) {
433214117Sjamie			jail_warnx(j, "mount.devfs: no path");
434214117Sjamie			return -1;
435214117Sjamie		}
436214797Sjamie		devpath = alloca(strlen(path) + 5);
437214797Sjamie		sprintf(devpath, "%s/dev", path);
438214805Sjamie		if (check_path(j, "mount.devfs", devpath, 0,
439223442Sjamie		    down ? "devfs" : NULL) < 0)
440214797Sjamie			return -1;
441214117Sjamie		if (down) {
442214117Sjamie			*(const char **)&argv[0] = "/sbin/umount";
443214797Sjamie			argv[1] = devpath;
444214117Sjamie			argv[2] = NULL;
445214117Sjamie		} else {
446232242Sjamie			*(const char **)&argv[0] = _PATH_MOUNT;
447232242Sjamie			*(const char **)&argv[1] = "-t";
448232242Sjamie			*(const char **)&argv[2] = "devfs";
449232242Sjamie			ruleset = string_param(j->intparams[KP_DEVFS_RULESET]);
450232242Sjamie			if (!ruleset)
451232242Sjamie			    ruleset = "4";	/* devfsrules_jail */
452232242Sjamie			argv[3] = alloca(11 + strlen(ruleset));
453232242Sjamie			sprintf(argv[3], "-oruleset=%s", ruleset);
454232242Sjamie			*(const char **)&argv[4] = ".";
455232242Sjamie			argv[5] = devpath;
456232242Sjamie			argv[6] = NULL;
457214117Sjamie		}
458216367Sjamie		break;
459216367Sjamie
460256387Shrs	case IP_MOUNT_FDESCFS:
461256387Shrs		argv = alloca(7 * sizeof(char *));
462256387Shrs		path = string_param(j->intparams[KP_PATH]);
463256387Shrs		if (path == NULL) {
464256387Shrs			jail_warnx(j, "mount.fdescfs: no path");
465256387Shrs			return -1;
466256387Shrs		}
467256387Shrs		devpath = alloca(strlen(path) + 8);
468256387Shrs		sprintf(devpath, "%s/dev/fd", path);
469256387Shrs		if (check_path(j, "mount.fdescfs", devpath, 0,
470256387Shrs		    down ? "fdescfs" : NULL) < 0)
471256387Shrs			return -1;
472256387Shrs		if (down) {
473256387Shrs			*(const char **)&argv[0] = "/sbin/umount";
474256387Shrs			argv[1] = devpath;
475256387Shrs			argv[2] = NULL;
476256387Shrs		} else {
477256387Shrs			*(const char **)&argv[0] = _PATH_MOUNT;
478256387Shrs			*(const char **)&argv[1] = "-t";
479256387Shrs			*(const char **)&argv[2] = "fdescfs";
480256387Shrs			*(const char **)&argv[3] = ".";
481256387Shrs			argv[4] = devpath;
482256387Shrs			argv[5] = NULL;
483256387Shrs		}
484256387Shrs		break;
485256387Shrs
486216367Sjamie	case IP_COMMAND:
487216367Sjamie		if (j->name != NULL)
488216367Sjamie			goto default_command;
489214117Sjamie		argc = 0;
490223188Sjamie		TAILQ_FOREACH(s, &j->intparams[IP_COMMAND]->val, tq)
491214117Sjamie			argc++;
492214117Sjamie		argv = alloca((argc + 1) * sizeof(char *));
493214117Sjamie		argc = 0;
494223188Sjamie		TAILQ_FOREACH(s, &j->intparams[IP_COMMAND]->val, tq)
495214117Sjamie			argv[argc++] = s->s;
496214117Sjamie		argv[argc] = NULL;
497223368Sjamie		j->comstring = &dummystring;
498216367Sjamie		break;
499216367Sjamie
500216367Sjamie	default:
501216367Sjamie	default_command:
502216367Sjamie		if ((cs = strpbrk(comstring->s, "!\"$&'()*;<>?[\\]`{|}~")) &&
503216367Sjamie		    !(cs[0] == '&' && cs[1] == '\0')) {
504216367Sjamie			argv = alloca(4 * sizeof(char *));
505216367Sjamie			*(const char **)&argv[0] = _PATH_BSHELL;
506216367Sjamie			*(const char **)&argv[1] = "-c";
507216367Sjamie			argv[2] = comstring->s;
508216367Sjamie			argv[3] = NULL;
509216367Sjamie		} else {
510216367Sjamie			if (cs) {
511216367Sjamie				*cs = 0;
512216367Sjamie				bg = 1;
513216367Sjamie			}
514216367Sjamie			comcs = alloca(comstring->len + 1);
515216367Sjamie			strcpy(comcs, comstring->s);
516216367Sjamie			argc = 0;
517216367Sjamie			for (cs = strtok(comcs, " \t\f\v\r\n"); cs;
518216367Sjamie			     cs = strtok(NULL, " \t\f\v\r\n"))
519216367Sjamie				argc++;
520216367Sjamie			argv = alloca((argc + 1) * sizeof(char *));
521216367Sjamie			strcpy(comcs, comstring->s);
522216367Sjamie			argc = 0;
523216367Sjamie			for (cs = strtok(comcs, " \t\f\v\r\n"); cs;
524216367Sjamie			     cs = strtok(NULL, " \t\f\v\r\n"))
525216367Sjamie				argv[argc++] = cs;
526216367Sjamie			argv[argc] = NULL;
527214117Sjamie		}
528214117Sjamie	}
529223189Sjamie	if (argv[0] == NULL)
530223189Sjamie		return 0;
531216367Sjamie
532214117Sjamie	if (int_param(j->intparams[IP_EXEC_TIMEOUT], &timeout) &&
533214117Sjamie	    timeout != 0) {
534214117Sjamie		clock_gettime(CLOCK_REALTIME, &j->timeout);
535214117Sjamie		j->timeout.tv_sec += timeout;
536214117Sjamie	} else
537214117Sjamie		j->timeout.tv_sec = 0;
538214117Sjamie
539214117Sjamie	injail = comparam == IP_EXEC_START || comparam == IP_COMMAND ||
540214117Sjamie	    comparam == IP_EXEC_STOP;
541214117Sjamie	clean = bool_param(j->intparams[IP_EXEC_CLEAN]);
542214117Sjamie	username = string_param(j->intparams[injail
543214117Sjamie	    ? IP_EXEC_JAIL_USER : IP_EXEC_SYSTEM_USER]);
544214117Sjamie	sjuser = bool_param(j->intparams[IP_EXEC_SYSTEM_JAIL_USER]);
545214117Sjamie
546214117Sjamie	consfd = 0;
547214117Sjamie	if (injail &&
548214117Sjamie	    (conslog = string_param(j->intparams[IP_EXEC_CONSOLELOG]))) {
549223442Sjamie		if (check_path(j, "exec.consolelog", conslog, 1, NULL) < 0)
550214797Sjamie			return -1;
551214117Sjamie		consfd =
552214117Sjamie		    open(conslog, O_WRONLY | O_CREAT | O_APPEND, DEFFILEMODE);
553214117Sjamie		if (consfd < 0) {
554214117Sjamie			jail_warnx(j, "open %s: %s", conslog, strerror(errno));
555214117Sjamie			return -1;
556214117Sjamie		}
557214117Sjamie	}
558214117Sjamie
559214117Sjamie	comlen = 0;
560214117Sjamie	for (i = 0; argv[i]; i++)
561214117Sjamie		comlen += strlen(argv[i]) + 1;
562214117Sjamie	j->comline = cs = emalloc(comlen);
563214117Sjamie	for (i = 0; argv[i]; i++) {
564214117Sjamie		strcpy(cs, argv[i]);
565214117Sjamie		if (argv[i + 1]) {
566214117Sjamie			cs += strlen(argv[i]) + 1;
567214117Sjamie			cs[-1] = ' ';
568214117Sjamie		}
569214117Sjamie	}
570214117Sjamie	if (verbose > 0)
571214117Sjamie		jail_note(j, "run command%s%s%s: %s\n",
572214117Sjamie		    injail ? " in jail" : "", username ? " as " : "",
573214117Sjamie		    username ? username : "", j->comline);
574214117Sjamie
575214117Sjamie	pid = fork();
576214117Sjamie	if (pid < 0)
577214117Sjamie		err(1, "fork");
578214117Sjamie	if (pid > 0) {
579246804Sjamie		if (bg || !add_proc(j, pid)) {
580216367Sjamie			free(j->comline);
581216367Sjamie			j->comline = NULL;
582223442Sjamie			return 0;
583214117Sjamie		} else {
584216367Sjamie			paralimit--;
585223442Sjamie			return 1;
586214117Sjamie		}
587214117Sjamie	}
588214117Sjamie	if (bg)
589214117Sjamie		setsid();
590214117Sjamie
591223189Sjamie	/* Set up the environment and run the command */
592214117Sjamie	pwd = NULL;
593214117Sjamie	lcap = NULL;
594214117Sjamie	if ((clean || username) && injail && sjuser &&
595214117Sjamie	    get_user_info(j, username, &pwd, &lcap) < 0)
596214117Sjamie		exit(1);
597214117Sjamie	if (injail) {
598214117Sjamie		/* jail_attach won't chdir along with its chroot. */
599214117Sjamie		path = string_param(j->intparams[KP_PATH]);
600214117Sjamie		if (path && chdir(path) < 0) {
601214117Sjamie			jail_warnx(j, "chdir %s: %s", path, strerror(errno));
602214117Sjamie			exit(1);
603214117Sjamie		}
604214117Sjamie		if (int_param(j->intparams[IP_EXEC_FIB], &fib) &&
605214117Sjamie		    setfib(fib) < 0) {
606214117Sjamie			jail_warnx(j, "setfib: %s", strerror(errno));
607214117Sjamie			exit(1);
608214117Sjamie		}
609214117Sjamie		if (jail_attach(j->jid) < 0) {
610214117Sjamie			jail_warnx(j, "jail_attach: %s", strerror(errno));
611214117Sjamie			exit(1);
612214117Sjamie		}
613214117Sjamie	}
614214117Sjamie	if (clean || username) {
615214117Sjamie		if (!(injail && sjuser) &&
616214117Sjamie		    get_user_info(j, username, &pwd, &lcap) < 0)
617214117Sjamie			exit(1);
618214117Sjamie		if (clean) {
619214117Sjamie			term = getenv("TERM");
620214117Sjamie			environ = &cleanenv;
621214117Sjamie			setenv("PATH", "/bin:/usr/bin", 0);
622235949Sjamie			if (term != NULL)
623235949Sjamie				setenv("TERM", term, 1);
624214117Sjamie		}
625214117Sjamie		if (setusercontext(lcap, pwd, pwd->pw_uid, username
626214117Sjamie		    ? LOGIN_SETALL & ~LOGIN_SETGROUP & ~LOGIN_SETLOGIN
627214117Sjamie		    : LOGIN_SETPATH | LOGIN_SETENV) < 0) {
628214117Sjamie			jail_warnx(j, "setusercontext %s: %s", pwd->pw_name,
629214117Sjamie			    strerror(errno));
630214117Sjamie			exit(1);
631214117Sjamie		}
632214117Sjamie		login_close(lcap);
633214117Sjamie		setenv("USER", pwd->pw_name, 1);
634214117Sjamie		setenv("HOME", pwd->pw_dir, 1);
635214117Sjamie		setenv("SHELL",
636214117Sjamie		    *pwd->pw_shell ? pwd->pw_shell : _PATH_BSHELL, 1);
637214117Sjamie		if (clean && chdir(pwd->pw_dir) < 0) {
638214117Sjamie			jail_warnx(j, "chdir %s: %s",
639214117Sjamie			    pwd->pw_dir, strerror(errno));
640214117Sjamie			exit(1);
641214117Sjamie		}
642214117Sjamie		endpwent();
643214117Sjamie	}
644214117Sjamie
645214117Sjamie	if (consfd != 0 && (dup2(consfd, 1) < 0 || dup2(consfd, 2) < 0)) {
646214117Sjamie		jail_warnx(j, "exec.consolelog: %s", strerror(errno));
647214117Sjamie		exit(1);
648214117Sjamie	}
649214117Sjamie	closefrom(3);
650214117Sjamie	execvp(argv[0], argv);
651214117Sjamie	jail_warnx(j, "exec %s: %s", argv[0], strerror(errno));
652214117Sjamie	exit(1);
653214117Sjamie}
654214117Sjamie
655214117Sjamie/*
656214117Sjamie * Add a process to the hash, tied to a jail.
657214117Sjamie */
658246804Sjamiestatic int
659214117Sjamieadd_proc(struct cfjail *j, pid_t pid)
660214117Sjamie{
661214117Sjamie	struct kevent ke;
662214117Sjamie	struct cfjail *tj;
663214117Sjamie	struct phash *ph;
664214117Sjamie
665214117Sjamie	if (!kq && (kq = kqueue()) < 0)
666214117Sjamie		err(1, "kqueue");
667214117Sjamie	EV_SET(&ke, pid, EVFILT_PROC, EV_ADD, NOTE_EXIT, 0, NULL);
668246804Sjamie	if (kevent(kq, &ke, 1, NULL, 0, NULL) < 0) {
669246804Sjamie		if (errno == ESRCH)
670246804Sjamie			return 0;
671214117Sjamie		err(1, "kevent");
672246804Sjamie	}
673214117Sjamie	ph = emalloc(sizeof(struct phash));
674214117Sjamie	ph->j = j;
675214117Sjamie	ph->pid = pid;
676214117Sjamie	LIST_INSERT_HEAD(&phash[pid % PHASH_SIZE], ph, le);
677214117Sjamie	j->nprocs++;
678216367Sjamie	j->flags |= JF_SLEEPQ;
679216367Sjamie	if (j->timeout.tv_sec == 0)
680216367Sjamie		requeue(j, &sleeping);
681216367Sjamie	else {
682216367Sjamie		/* File the jail in the sleep queue acording to its timeout. */
683214117Sjamie		TAILQ_REMOVE(j->queue, j, tq);
684214117Sjamie		TAILQ_FOREACH(tj, &sleeping, tq) {
685214117Sjamie			if (!tj->timeout.tv_sec ||
686214117Sjamie			    j->timeout.tv_sec < tj->timeout.tv_sec ||
687214117Sjamie			    (j->timeout.tv_sec == tj->timeout.tv_sec &&
688214117Sjamie			    j->timeout.tv_nsec <= tj->timeout.tv_nsec)) {
689214117Sjamie				TAILQ_INSERT_BEFORE(tj, j, tq);
690214117Sjamie				break;
691214117Sjamie			}
692214117Sjamie		}
693214117Sjamie		if (tj == NULL)
694214117Sjamie			TAILQ_INSERT_TAIL(&sleeping, j, tq);
695214117Sjamie		j->queue = &sleeping;
696216367Sjamie	}
697246804Sjamie	return 1;
698214117Sjamie}
699214117Sjamie
700214117Sjamie/*
701214117Sjamie * Remove any processes from the hash that correspond to a jail.
702214117Sjamie */
703214117Sjamiestatic void
704214117Sjamieclear_procs(struct cfjail *j)
705214117Sjamie{
706214117Sjamie	struct kevent ke;
707214117Sjamie	struct phash *ph, *tph;
708214117Sjamie	int i;
709214117Sjamie
710214117Sjamie	j->nprocs = 0;
711214117Sjamie	for (i = 0; i < PHASH_SIZE; i++)
712214117Sjamie		LIST_FOREACH_SAFE(ph, &phash[i], le, tph)
713214117Sjamie			if (ph->j == j) {
714214117Sjamie				EV_SET(&ke, ph->pid, EVFILT_PROC, EV_DELETE,
715214117Sjamie				    NOTE_EXIT, 0, NULL);
716214117Sjamie				(void)kevent(kq, &ke, 1, NULL, 0, NULL);
717214117Sjamie				LIST_REMOVE(ph, le);
718214117Sjamie				free(ph);
719214117Sjamie			}
720214117Sjamie}
721214117Sjamie
722214117Sjamie/*
723214117Sjamie * Find the jail that corresponds to an exited process.
724214117Sjamie */
725214117Sjamiestatic struct cfjail *
726214117Sjamiefind_proc(pid_t pid)
727214117Sjamie{
728214117Sjamie	struct cfjail *j;
729214117Sjamie	struct phash *ph;
730214117Sjamie
731214117Sjamie	LIST_FOREACH(ph, &phash[pid % PHASH_SIZE], le)
732214117Sjamie		if (ph->pid == pid) {
733214117Sjamie			j = ph->j;
734214117Sjamie			LIST_REMOVE(ph, le);
735214117Sjamie			free(ph);
736214117Sjamie			return --j->nprocs ? NULL : j;
737214117Sjamie		}
738214117Sjamie	return NULL;
739214117Sjamie}
740214117Sjamie
741214117Sjamie/*
742216367Sjamie * Send SIGTERM to all processes in a jail and wait for them to die.
743216367Sjamie */
744216367Sjamiestatic int
745216367Sjamieterm_procs(struct cfjail *j)
746216367Sjamie{
747216367Sjamie	struct kinfo_proc *ki;
748216367Sjamie	int i, noted, pcnt, timeout;
749216367Sjamie
750216367Sjamie	static kvm_t *kd;
751216367Sjamie
752216367Sjamie	if (!int_param(j->intparams[IP_STOP_TIMEOUT], &timeout))
753216367Sjamie		timeout = DEFAULT_STOP_TIMEOUT;
754216367Sjamie	else if (timeout == 0)
755216367Sjamie		return 0;
756216367Sjamie
757216367Sjamie	if (kd == NULL) {
758231238Sjamie		kd = kvm_open(NULL, NULL, NULL, O_RDONLY, NULL);
759216367Sjamie		if (kd == NULL)
760231238Sjamie			return 0;
761216367Sjamie	}
762216367Sjamie
763216367Sjamie	ki = kvm_getprocs(kd, KERN_PROC_PROC, 0, &pcnt);
764216367Sjamie	if (ki == NULL)
765231238Sjamie		return 0;
766216367Sjamie	noted = 0;
767216367Sjamie	for (i = 0; i < pcnt; i++)
768216367Sjamie		if (ki[i].ki_jid == j->jid &&
769216367Sjamie		    kill(ki[i].ki_pid, SIGTERM) == 0) {
770246804Sjamie			(void)add_proc(j, ki[i].ki_pid);
771216367Sjamie			if (verbose > 0) {
772216367Sjamie				if (!noted) {
773216367Sjamie					noted = 1;
774216367Sjamie					jail_note(j, "sent SIGTERM to:");
775216367Sjamie				}
776216367Sjamie				printf(" %d", ki[i].ki_pid);
777216367Sjamie			}
778216367Sjamie		}
779216367Sjamie	if (noted)
780216367Sjamie		printf("\n");
781216367Sjamie	if (j->nprocs > 0) {
782216367Sjamie		clock_gettime(CLOCK_REALTIME, &j->timeout);
783216367Sjamie		j->timeout.tv_sec += timeout;
784216367Sjamie		return 1;
785216367Sjamie	}
786216367Sjamie	return 0;
787216367Sjamie}
788216367Sjamie
789216367Sjamie/*
790214117Sjamie * Look up a user in the passwd and login.conf files.
791214117Sjamie */
792214117Sjamiestatic int
793214117Sjamieget_user_info(struct cfjail *j, const char *username,
794214117Sjamie    const struct passwd **pwdp, login_cap_t **lcapp)
795214117Sjamie{
796214117Sjamie	const struct passwd *pwd;
797214117Sjamie
798214117Sjamie	*pwdp = pwd = username ? getpwnam(username) : getpwuid(getuid());
799214117Sjamie	if (pwd == NULL) {
800214117Sjamie		if (errno)
801214117Sjamie			jail_warnx(j, "getpwnam%s%s: %s", username ? " " : "",
802214117Sjamie			    username ? username : "", strerror(errno));
803214117Sjamie		else if (username)
804214117Sjamie			jail_warnx(j, "%s: no such user", username);
805214117Sjamie		else
806214117Sjamie			jail_warnx(j, "unknown uid %d", getuid());
807214117Sjamie		return -1;
808214117Sjamie	}
809214117Sjamie	*lcapp = login_getpwclass(pwd);
810214117Sjamie	if (*lcapp == NULL) {
811214117Sjamie		jail_warnx(j, "getpwclass %s: %s", pwd->pw_name,
812214117Sjamie		    strerror(errno));
813214117Sjamie		return -1;
814214117Sjamie	}
815214117Sjamie	/* Set the groups while the group file is still available */
816214117Sjamie	if (initgroups(pwd->pw_name, pwd->pw_gid) < 0) {
817214117Sjamie		jail_warnx(j, "initgroups %s: %s", pwd->pw_name,
818214117Sjamie		    strerror(errno));
819214117Sjamie		return -1;
820214117Sjamie	}
821214117Sjamie	return 0;
822214117Sjamie}
823214797Sjamie
824214797Sjamie/*
825214797Sjamie * Make sure a mount or consolelog path is a valid absolute pathname
826214797Sjamie * with no symlinks.
827214797Sjamie */
828214797Sjamiestatic int
829214805Sjamiecheck_path(struct cfjail *j, const char *pname, const char *path, int isfile,
830214805Sjamie    const char *umount_type)
831214797Sjamie{
832214805Sjamie	struct stat st, mpst;
833214805Sjamie	struct statfs stfs;
834214797Sjamie	char *tpath, *p;
835214797Sjamie	const char *jailpath;
836214797Sjamie	size_t jplen;
837214797Sjamie
838214797Sjamie	if (path[0] != '/') {
839214797Sjamie		jail_warnx(j, "%s: %s: not an absolute pathname",
840214797Sjamie		    pname, path);
841214797Sjamie		return -1;
842214797Sjamie	}
843214797Sjamie	/*
844214797Sjamie	 * Only check for symlinks in components below the jail's path,
845214797Sjamie	 * since that's where the security risk lies.
846214797Sjamie	 */
847214797Sjamie	jailpath = string_param(j->intparams[KP_PATH]);
848214797Sjamie	if (jailpath == NULL)
849214797Sjamie		jailpath = "";
850214797Sjamie	jplen = strlen(jailpath);
851214805Sjamie	if (!strncmp(path, jailpath, jplen) && path[jplen] == '/') {
852214805Sjamie		tpath = alloca(strlen(path) + 1);
853214805Sjamie		strcpy(tpath, path);
854214805Sjamie		for (p = tpath + jplen; p != NULL; ) {
855214805Sjamie			p = strchr(p + 1, '/');
856214805Sjamie			if (p)
857214805Sjamie				*p = '\0';
858214805Sjamie			if (lstat(tpath, &st) < 0) {
859214805Sjamie				if (errno == ENOENT && isfile && !p)
860214805Sjamie					break;
861214805Sjamie				jail_warnx(j, "%s: %s: %s", pname, tpath,
862214805Sjamie				    strerror(errno));
863214805Sjamie				return -1;
864214805Sjamie			}
865214805Sjamie			if (S_ISLNK(st.st_mode)) {
866214805Sjamie				jail_warnx(j, "%s: %s is a symbolic link",
867214805Sjamie				    pname, tpath);
868214805Sjamie				return -1;
869214805Sjamie			}
870214805Sjamie			if (p)
871214805Sjamie				*p = '/';
872214805Sjamie		}
873214805Sjamie	}
874214805Sjamie	if (umount_type != NULL) {
875214805Sjamie		if (stat(path, &st) < 0 || statfs(path, &stfs) < 0) {
876214805Sjamie			jail_warnx(j, "%s: %s: %s", pname, path,
877214797Sjamie			    strerror(errno));
878214797Sjamie			return -1;
879214797Sjamie		}
880214805Sjamie		if (stat(stfs.f_mntonname, &mpst) < 0) {
881214805Sjamie			jail_warnx(j, "%s: %s: %s", pname, stfs.f_mntonname,
882214805Sjamie			    strerror(errno));
883214797Sjamie			return -1;
884214797Sjamie		}
885214805Sjamie		if (st.st_ino != mpst.st_ino) {
886214805Sjamie			jail_warnx(j, "%s: %s: not a mount point",
887214805Sjamie			    pname, path);
888214805Sjamie			return -1;
889214805Sjamie		}
890214805Sjamie		if (strcmp(stfs.f_fstypename, umount_type)) {
891214805Sjamie			jail_warnx(j, "%s: %s: not a %s mount",
892214805Sjamie			    pname, path, umount_type);
893214805Sjamie			return -1;
894214805Sjamie		}
895214797Sjamie	}
896214797Sjamie	return 0;
897214797Sjamie}
898