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