2 * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
4 * Copyright (c) 2011 James Gritton
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
16 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
17 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
20 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
22 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
24 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29 #include <sys/cdefs.h>
30 __FBSDID("$FreeBSD$");
32 #include <sys/types.h>
33 #include <sys/errno.h>
34 #include <sys/socket.h>
35 #include <sys/sysctl.h>
37 #include <arpa/inet.h>
38 #include <netinet/in.h>
57 extern int yyparse(void);
59 struct cfjails cfjails = TAILQ_HEAD_INITIALIZER(cfjails);
61 static void free_param(struct cfparams *pp, struct cfparam *p);
62 static void free_param_strings(struct cfparam *p);
64 static const struct ipspec intparams[] = {
65 [IP_ALLOW_DYING] = {"allow.dying", PF_INTERNAL | PF_BOOL},
66 [IP_COMMAND] = {"command", PF_INTERNAL},
67 [IP_DEPEND] = {"depend", PF_INTERNAL},
68 [IP_EXEC_CLEAN] = {"exec.clean", PF_INTERNAL | PF_BOOL},
69 [IP_EXEC_CONSOLELOG] = {"exec.consolelog", PF_INTERNAL},
70 [IP_EXEC_FIB] = {"exec.fib", PF_INTERNAL | PF_INT},
71 [IP_EXEC_JAIL_USER] = {"exec.jail_user", PF_INTERNAL},
72 [IP_EXEC_POSTSTART] = {"exec.poststart", PF_INTERNAL},
73 [IP_EXEC_POSTSTOP] = {"exec.poststop", PF_INTERNAL},
74 [IP_EXEC_PRESTART] = {"exec.prestart", PF_INTERNAL},
75 [IP_EXEC_PRESTOP] = {"exec.prestop", PF_INTERNAL},
76 [IP_EXEC_START] = {"exec.start", PF_INTERNAL},
77 [IP_EXEC_STOP] = {"exec.stop", PF_INTERNAL},
78 [IP_EXEC_SYSTEM_JAIL_USER]= {"exec.system_jail_user",
79 PF_INTERNAL | PF_BOOL},
80 [IP_EXEC_SYSTEM_USER] = {"exec.system_user", PF_INTERNAL},
81 [IP_EXEC_TIMEOUT] = {"exec.timeout", PF_INTERNAL | PF_INT},
82 #if defined(INET) || defined(INET6)
83 [IP_INTERFACE] = {"interface", PF_INTERNAL},
84 [IP_IP_HOSTNAME] = {"ip_hostname", PF_INTERNAL | PF_BOOL},
86 [IP_MOUNT] = {"mount", PF_INTERNAL | PF_REV},
87 [IP_MOUNT_DEVFS] = {"mount.devfs", PF_INTERNAL | PF_BOOL},
88 [IP_MOUNT_FDESCFS] = {"mount.fdescfs", PF_INTERNAL | PF_BOOL},
89 [IP_MOUNT_PROCFS] = {"mount.procfs", PF_INTERNAL | PF_BOOL},
90 [IP_MOUNT_FSTAB] = {"mount.fstab", PF_INTERNAL},
91 [IP_STOP_TIMEOUT] = {"stop.timeout", PF_INTERNAL | PF_INT},
92 [IP_VNET_INTERFACE] = {"vnet.interface", PF_INTERNAL},
94 [IP__IP4_IFADDR] = {"ip4.addr", PF_INTERNAL | PF_CONV | PF_REV},
97 [IP__IP6_IFADDR] = {"ip6.addr", PF_INTERNAL | PF_CONV | PF_REV},
99 [IP__MOUNT_FROM_FSTAB] = {"mount.fstab", PF_INTERNAL | PF_CONV | PF_REV},
100 [IP__OP] = {NULL, PF_CONV},
101 [KP_ALLOW_CHFLAGS] = {"allow.chflags", 0},
102 [KP_ALLOW_MOUNT] = {"allow.mount", 0},
103 [KP_ALLOW_RAW_SOCKETS] = {"allow.raw_sockets", 0},
104 [KP_ALLOW_SET_HOSTNAME]= {"allow.set_hostname", 0},
105 [KP_ALLOW_SOCKET_AF] = {"allow.socket_af", 0},
106 [KP_ALLOW_SYSVIPC] = {"allow.sysvipc", 0},
107 [KP_DEVFS_RULESET] = {"devfs_ruleset", 0},
108 [KP_ENFORCE_STATFS] = {"enforce_statfs", 0},
109 [KP_HOST_HOSTNAME] = {"host.hostname", 0},
111 [KP_IP4_ADDR] = {"ip4.addr", 0},
114 [KP_IP6_ADDR] = {"ip6.addr", 0},
116 [KP_JID] = {"jid", PF_IMMUTABLE},
117 [KP_NAME] = {"name", PF_IMMUTABLE},
118 [KP_PATH] = {"path", 0},
119 [KP_PERSIST] = {"persist", 0},
120 [KP_SECURELEVEL] = {"securelevel", 0},
121 [KP_VNET] = {"vnet", 0},
125 * Parse the jail configuration file.
132 struct cfjail *j, *tj, *wj;
133 struct cfparam *p, *vp, *tp;
134 struct cfstring *s, *vs, *ns;
135 struct cfvar *v, *vv;
137 int did_self, jseq, pgen;
139 if (!strcmp(cfname, "-")) {
143 yyin = fopen(cfname, "r");
145 err(1, "%s", cfname);
147 if (yyparse() || yynerrs)
150 /* Separate the wildcard jails out from the actual jails. */
153 TAILQ_FOREACH_SAFE(j, &cfjails, tq, tj) {
155 if (wild_jail_name(j->name))
159 TAILQ_FOREACH(j, &cfjails, tq) {
160 /* Set aside the jail's parameters. */
162 TAILQ_CONCAT(&opp, &j->params, tq);
164 * The jail name implies its "name" or "jid" parameter,
165 * though they may also be explicitly set later on.
168 strtol(j->name, &ep, 10) && !*ep ? KP_JID : KP_NAME,
171 * Collect parameters for the jail, global parameters/variables,
172 * and any matching wildcard jails.
175 TAILQ_FOREACH(wj, &wild, tq) {
176 if (j->seq < wj->seq && !did_self) {
177 TAILQ_FOREACH(p, &opp, tq)
178 add_param(j, p, 0, NULL);
181 if (wild_jail_match(j->name, wj->name))
182 TAILQ_FOREACH(p, &wj->params, tq)
183 add_param(j, p, 0, NULL);
186 TAILQ_FOREACH(p, &opp, tq)
187 add_param(j, p, 0, NULL);
189 /* Resolve any variable substitutions. */
191 TAILQ_FOREACH(p, &j->params, tq) {
194 TAILQ_FOREACH(s, &p->val, tq) {
195 while ((v = STAILQ_FIRST(&s->vars))) {
196 TAILQ_FOREACH(vp, &j->params, tq)
197 if (!strcmp(vp->name, v->name))
201 "%s: variable \"%s\" not found",
204 j->flags |= JF_FAILED;
205 TAILQ_FOREACH(vp, &j->params, tq)
210 if (vp->flags & PF_BAD)
212 if (vp->gen == pgen) {
213 jail_warnx(j, "%s: variable loop",
217 TAILQ_FOREACH(vs, &vp->val, tq)
218 if (!STAILQ_EMPTY(&vs->vars)) {
220 TAILQ_REMOVE(&j->params, vp,
222 TAILQ_INSERT_BEFORE(p, vp, tq);
226 vs = TAILQ_FIRST(&vp->val);
227 if (TAILQ_NEXT(vs, tq) != NULL &&
229 STAILQ_NEXT(v, tq))) {
230 jail_warnx(j, "%s: array cannot be "
231 "substituted inline",
235 s->s = erealloc(s->s, s->len + vs->len + 1);
236 memmove(s->s + v->pos + vs->len,
238 s->len - v->pos + 1);
239 memcpy(s->s + v->pos, vs->s, vs->len);
241 while ((vv = STAILQ_NEXT(vv, tq)))
244 while ((vs = TAILQ_NEXT(vs, tq))) {
245 ns = emalloc(sizeof(struct cfstring));
246 ns->s = estrdup(vs->s);
248 STAILQ_INIT(&ns->vars);
249 TAILQ_INSERT_AFTER(&p->val, s, ns, tq);
254 STAILQ_REMOVE_HEAD(&s->vars, tq);
260 /* Free the jail's original parameter list and any variables. */
261 while ((p = TAILQ_FIRST(&opp)))
263 TAILQ_FOREACH_SAFE(p, &j->params, tq, tp)
264 if (p->flags & PF_VAR)
265 free_param(&j->params, p);
267 while ((wj = TAILQ_FIRST(&wild))) {
269 while ((p = TAILQ_FIRST(&wj->params)))
270 free_param(&wj->params, p);
271 TAILQ_REMOVE(&wild, wj, tq);
276 * Create a new jail record.
283 j = emalloc(sizeof(struct cfjail));
284 memset(j, 0, sizeof(struct cfjail));
285 TAILQ_INIT(&j->params);
286 STAILQ_INIT(&j->dep[DEP_FROM]);
287 STAILQ_INIT(&j->dep[DEP_TO]);
289 TAILQ_INSERT_TAIL(&cfjails, j, tq);
294 * Add a parameter to a jail.
297 add_param(struct cfjail *j, const struct cfparam *p, enum intparam ipnum,
300 struct cfstrings nss;
301 struct cfparam *dp, *np;
302 struct cfstring *s, *ns;
303 struct cfvar *v, *nv;
309 /* Create a single anonymous jail if one doesn't yet exist. */
310 j = TAILQ_LAST(&cfjails, cfjails);
319 * Make a copy of the parameter's string list,
320 * which may be freed if it's overridden later.
322 TAILQ_FOREACH(s, &p->val, tq) {
323 ns = emalloc(sizeof(struct cfstring));
324 ns->s = estrdup(s->s);
326 STAILQ_INIT(&ns->vars);
327 STAILQ_FOREACH(v, &s->vars, tq) {
328 nv = emalloc(sizeof(struct cfvar));
329 nv->name = strdup(v->name);
331 STAILQ_INSERT_TAIL(&ns->vars, nv, tq);
333 TAILQ_INSERT_TAIL(&nss, ns, tq);
337 if (ipnum != IP__NULL) {
338 name = intparams[ipnum].name;
339 flags |= intparams[ipnum].flags;
340 } else if ((cs = strchr(value, '='))) {
341 tname = alloca(cs - value + 1);
342 strlcpy(tname, value, cs - value + 1);
350 ns = emalloc(sizeof(struct cfstring));
351 ns->s = estrdup(value);
352 ns->len = strlen(value);
353 STAILQ_INIT(&ns->vars);
354 TAILQ_INSERT_TAIL(&nss, ns, tq);
358 /* See if this parameter has already been added. */
359 if (ipnum != IP__NULL)
360 dp = j->intparams[ipnum];
362 TAILQ_FOREACH(dp, &j->params, tq)
363 if (!(dp->flags & PF_CONV) && equalopts(dp->name, name))
366 /* Found it - append or replace. */
367 if (dp->flags & PF_IMMUTABLE) {
368 jail_warnx(j, "cannot redefine variable \"%s\".",
372 if (strcmp(dp->name, name)) {
374 dp->name = estrdup(name);
376 if (!(flags & PF_APPEND) || TAILQ_EMPTY(&nss))
377 free_param_strings(dp);
378 TAILQ_CONCAT(&dp->val, &nss, tq);
381 /* Not found - add it. */
382 np = emalloc(sizeof(struct cfparam));
383 np->name = estrdup(name);
384 TAILQ_INIT(&np->val);
385 TAILQ_CONCAT(&np->val, &nss, tq);
388 TAILQ_INSERT_TAIL(&j->params, np, tq);
389 if (ipnum != IP__NULL)
390 j->intparams[ipnum] = np;
392 for (ipnum = IP__NULL + 1; ipnum < IP_NPARAM; ipnum++)
393 if (!(intparams[ipnum].flags & PF_CONV) &&
394 equalopts(name, intparams[ipnum].name)) {
395 j->intparams[ipnum] = np;
396 np->flags |= intparams[ipnum].flags;
403 * Return if a boolean parameter exists and is true.
406 bool_param(const struct cfparam *p)
412 cs = strrchr(p->name, '.');
413 return !strncmp(cs ? cs + 1 : p->name, "no", 2) ^
414 (TAILQ_EMPTY(&p->val) ||
415 !strcasecmp(TAILQ_LAST(&p->val, cfstrings)->s, "true") ||
416 (strtol(TAILQ_LAST(&p->val, cfstrings)->s, NULL, 10)));
420 * Set an integer if a parameter if it exists.
423 int_param(const struct cfparam *p, int *ip)
425 if (p == NULL || TAILQ_EMPTY(&p->val))
427 *ip = strtol(TAILQ_LAST(&p->val, cfstrings)->s, NULL, 10);
432 * Return the string value of a scalar parameter if it exists.
435 string_param(const struct cfparam *p)
437 return (p && !TAILQ_EMPTY(&p->val)
438 ? TAILQ_LAST(&p->val, cfstrings)->s : NULL);
442 * Check syntax and values of internal parameters. Set some internal
443 * parameters based on the values of others.
446 check_intparams(struct cfjail *j)
455 #if defined(INET) || defined(INET6)
456 struct addrinfo hints;
457 struct addrinfo *ai0, *ai;
458 const char *hostname;
462 struct in_addr addr4;
464 char avalue4[INET_ADDRSTRLEN];
467 struct in6_addr addr6;
469 char avalue6[INET6_ADDRSTRLEN];
473 /* Check format of boolan and integer values. */
474 TAILQ_FOREACH(p, &j->params, tq) {
475 if (!TAILQ_EMPTY(&p->val) && (p->flags & (PF_BOOL | PF_INT))) {
476 val = TAILQ_LAST(&p->val, cfstrings)->s;
477 if (p->flags & PF_BOOL) {
478 if (strcasecmp(val, "false") &&
479 strcasecmp(val, "true") &&
480 ((void)strtol(val, &ep, 10), *ep)) {
482 "%s: unknown boolean value \"%s\"",
487 (void)strtol(val, &ep, 10);
488 if (ep == val || *ep) {
490 "%s: non-integer value \"%s\"",
498 #if defined(INET) || defined(INET6)
500 * The ip_hostname parameter looks up the hostname, and adds parameters
501 * for any IP addresses it finds.
503 if (((j->flags & JF_OP_MASK) != JF_STOP ||
504 j->intparams[IP_INTERFACE] != NULL) &&
505 bool_param(j->intparams[IP_IP_HOSTNAME]) &&
506 (hostname = string_param(j->intparams[KP_HOST_HOSTNAME]))) {
507 j->intparams[IP_IP_HOSTNAME] = NULL;
509 * Silently ignore unsupported address families from
513 ip4ok = feature_present("inet");
516 ip6ok = feature_present("inet6");
519 #if defined(INET) && defined(INET6)
527 /* Look up the hostname (or get the address) */
528 memset(&hints, 0, sizeof(hints));
529 hints.ai_socktype = SOCK_STREAM;
531 #if defined(INET) && defined(INET6)
532 ip4ok ? (ip6ok ? PF_UNSPEC : PF_INET) : PF_INET6;
538 gicode = getaddrinfo(hostname, NULL, &hints, &ai0);
540 jail_warnx(j, "host.hostname %s: %s", hostname,
541 gai_strerror(gicode));
545 * Convert the addresses to ASCII so jailparam
546 * can convert them back. Errors are not
549 for (ai = ai0; ai; ai = ai->ai_next)
550 switch (ai->ai_family) {
554 &((struct sockaddr_in *)
555 (void *)ai->ai_addr)->
556 sin_addr, sizeof(addr4));
557 if (inet_ntop(AF_INET,
559 INET_ADDRSTRLEN) == NULL)
561 add_param(j, NULL, KP_IP4_ADDR,
568 &((struct sockaddr_in6 *)
569 (void *)ai->ai_addr)->
570 sin6_addr, sizeof(addr6));
571 if (inet_ntop(AF_INET6,
573 INET6_ADDRSTRLEN) == NULL)
575 add_param(j, NULL, KP_IP6_ADDR,
586 * IP addresses may include an interface to set that address on,
587 * a netmask/suffix for that address and options for ifconfig.
588 * These are copied to an internal command parameter and then stripped
589 * so they won't be passed on to jailparam_set.
591 defif = string_param(j->intparams[IP_INTERFACE]) != NULL;
593 if (j->intparams[KP_IP4_ADDR] != NULL) {
594 TAILQ_FOREACH(s, &j->intparams[KP_IP4_ADDR]->val, tq) {
595 cs = strchr(s->s, '|');
597 add_param(j, NULL, IP__IP4_IFADDR, s->s);
599 strcpy(s->s, cs + 1);
600 s->len -= cs + 1 - s->s;
602 if ((cs = strchr(s->s, '/')) != NULL) {
606 if ((cs = strchr(s->s, ' ')) != NULL) {
614 if (j->intparams[KP_IP6_ADDR] != NULL) {
615 TAILQ_FOREACH(s, &j->intparams[KP_IP6_ADDR]->val, tq) {
616 cs = strchr(s->s, '|');
618 add_param(j, NULL, IP__IP6_IFADDR, s->s);
620 strcpy(s->s, cs + 1);
621 s->len -= cs + 1 - s->s;
623 if ((cs = strchr(s->s, '/')) != NULL) {
627 if ((cs = strchr(s->s, ' ')) != NULL) {
637 * Read mount.fstab file(s), and treat each line as its own mount
640 if (j->intparams[IP_MOUNT_FSTAB] != NULL) {
641 TAILQ_FOREACH(s, &j->intparams[IP_MOUNT_FSTAB]->val, tq) {
644 f = fopen(s->s, "r");
646 jail_warnx(j, "mount.fstab: %s: %s",
647 s->s, strerror(errno));
651 while ((ln = fgetln(f, &lnlen))) {
652 if ((cs = memchr(ln, '#', lnlen - 1)))
654 if (ln[lnlen - 1] == '\n' ||
655 ln[lnlen - 1] == '#')
656 ln[lnlen - 1] = '\0';
658 cs = alloca(lnlen + 1);
659 strlcpy(cs, ln, lnlen + 1);
662 add_param(j, NULL, IP__MOUNT_FROM_FSTAB, ln);
673 * Import parameters into libjail's binary jailparam format.
676 import_params(struct cfjail *j)
679 struct cfstring *s, *ts;
680 struct jailparam *jp;
687 TAILQ_FOREACH(p, &j->params, tq)
688 if (!(p->flags & PF_INTERNAL))
690 j->jp = jp = emalloc(j->njp * sizeof(struct jailparam));
691 TAILQ_FOREACH(p, &j->params, tq) {
692 if (p->flags & PF_INTERNAL)
694 if (jailparam_init(jp, p->name) < 0) {
696 jail_warnx(j, "%s", jail_errmsg);
700 if (TAILQ_EMPTY(&p->val))
702 else if (!jp->jp_elemlen ||
703 !TAILQ_NEXT(TAILQ_FIRST(&p->val), tq)) {
705 * Scalar parameters silently discard multiple (array)
706 * values, keeping only the last value added. This
707 * lets values added from the command line append to
708 * arrays wthout pre-checking the type.
710 value = TAILQ_LAST(&p->val, cfstrings)->s;
713 * Convert arrays into comma-separated strings, which
714 * jailparam_import will then convert back into arrays.
717 TAILQ_FOREACH(s, &p->val, tq)
718 vallen += s->len + 1;
719 value = alloca(vallen);
721 TAILQ_FOREACH_SAFE(s, &p->val, tq, ts) {
722 memcpy(cs, s->s, s->len);
726 value[vallen - 1] = '\0';
728 if (jailparam_import(jp, value) < 0) {
730 jail_warnx(j, "%s", jail_errmsg);
735 jailparam_free(j->jp, j->njp);
744 * Check if options are equal (with or without the "no" prefix).
747 equalopts(const char *opt1, const char *opt2)
751 /* "opt" vs. "opt" or "noopt" vs. "noopt" */
752 if (strcmp(opt1, opt2) == 0)
754 /* "noopt" vs. "opt" */
755 if (strncmp(opt1, "no", 2) == 0 && strcmp(opt1 + 2, opt2) == 0)
757 /* "opt" vs. "noopt" */
758 if (strncmp(opt2, "no", 2) == 0 && strcmp(opt1, opt2 + 2) == 0)
760 while ((p = strchr(opt1, '.')) != NULL &&
761 !strncmp(opt1, opt2, ++p - opt1)) {
764 /* "foo.noopt" vs. "foo.opt" */
765 if (strncmp(opt1, "no", 2) == 0 && strcmp(opt1 + 2, opt2) == 0)
767 /* "foo.opt" vs. "foo.noopt" */
768 if (strncmp(opt2, "no", 2) == 0 && strcmp(opt1, opt2 + 2) == 0)
775 * See if a jail name matches a wildcard.
778 wild_jail_match(const char *jname, const char *wname)
780 const char *jc, *jd, *wc, *wd;
783 * A non-final "*" component in the wild name matches a single jail
784 * component, and a final "*" matches one or more jail components.
786 for (jc = jname, wc = wname;
787 (jd = strchr(jc, '.')) && (wd = strchr(wc, '.'));
788 jc = jd + 1, wc = wd + 1)
789 if (strncmp(jc, wc, jd - jc + 1) && strncmp(wc, "*.", 2))
791 return (!strcmp(jc, wc) || !strcmp(wc, "*"));
795 * Return if a jail name is a wildcard.
798 wild_jail_name(const char *wname)
802 for (wc = strchr(wname, '*'); wc; wc = strchr(wc + 1, '*'))
803 if ((wc == wname || wc[-1] == '.') &&
804 (wc[1] == '\0' || wc[1] == '.'))
810 * Free a parameter record and all its strings and variables.
813 free_param(struct cfparams *pp, struct cfparam *p)
816 free_param_strings(p);
817 TAILQ_REMOVE(pp, p, tq);
822 free_param_strings(struct cfparam *p)
827 while ((s = TAILQ_FIRST(&p->val))) {
829 while ((v = STAILQ_FIRST(&s->vars))) {
831 STAILQ_REMOVE_HEAD(&s->vars, tq);
834 TAILQ_REMOVE(&p->val, s, tq);