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_PREPARE] = {"exec.prepare", PF_INTERNAL},
75 [IP_EXEC_PRESTART] = {"exec.prestart", PF_INTERNAL},
76 [IP_EXEC_PRESTOP] = {"exec.prestop", PF_INTERNAL},
77 [IP_EXEC_RELEASE] = {"exec.release", PF_INTERNAL},
78 [IP_EXEC_CREATED] = {"exec.created", PF_INTERNAL},
79 [IP_EXEC_START] = {"exec.start", PF_INTERNAL},
80 [IP_EXEC_STOP] = {"exec.stop", PF_INTERNAL},
81 [IP_EXEC_SYSTEM_JAIL_USER]= {"exec.system_jail_user",
82 PF_INTERNAL | PF_BOOL},
83 [IP_EXEC_SYSTEM_USER] = {"exec.system_user", PF_INTERNAL},
84 [IP_EXEC_TIMEOUT] = {"exec.timeout", PF_INTERNAL | PF_INT},
85 #if defined(INET) || defined(INET6)
86 [IP_INTERFACE] = {"interface", PF_INTERNAL},
87 [IP_IP_HOSTNAME] = {"ip_hostname", PF_INTERNAL | PF_BOOL},
89 [IP_MOUNT] = {"mount", PF_INTERNAL | PF_REV},
90 [IP_MOUNT_DEVFS] = {"mount.devfs", PF_INTERNAL | PF_BOOL},
91 [IP_MOUNT_FDESCFS] = {"mount.fdescfs", PF_INTERNAL | PF_BOOL},
92 [IP_MOUNT_PROCFS] = {"mount.procfs", PF_INTERNAL | PF_BOOL},
93 [IP_MOUNT_FSTAB] = {"mount.fstab", PF_INTERNAL},
94 [IP_STOP_TIMEOUT] = {"stop.timeout", PF_INTERNAL | PF_INT},
95 [IP_VNET_INTERFACE] = {"vnet.interface", PF_INTERNAL},
97 [IP__IP4_IFADDR] = {"ip4.addr", PF_INTERNAL | PF_CONV | PF_REV},
100 [IP__IP6_IFADDR] = {"ip6.addr", PF_INTERNAL | PF_CONV | PF_REV},
102 [IP__MOUNT_FROM_FSTAB] = {"mount.fstab", PF_INTERNAL | PF_CONV | PF_REV},
103 [IP__OP] = {NULL, PF_CONV},
104 [KP_ALLOW_CHFLAGS] = {"allow.chflags", 0},
105 [KP_ALLOW_MOUNT] = {"allow.mount", 0},
106 [KP_ALLOW_RAW_SOCKETS] = {"allow.raw_sockets", 0},
107 [KP_ALLOW_SET_HOSTNAME]= {"allow.set_hostname", 0},
108 [KP_ALLOW_SOCKET_AF] = {"allow.socket_af", 0},
109 [KP_ALLOW_SYSVIPC] = {"allow.sysvipc", 0},
110 [KP_DEVFS_RULESET] = {"devfs_ruleset", 0},
111 [KP_HOST_HOSTNAME] = {"host.hostname", 0},
113 [KP_IP4_ADDR] = {"ip4.addr", 0},
116 [KP_IP6_ADDR] = {"ip6.addr", 0},
118 [KP_JID] = {"jid", PF_IMMUTABLE},
119 [KP_NAME] = {"name", PF_IMMUTABLE},
120 [KP_PATH] = {"path", 0},
121 [KP_PERSIST] = {"persist", 0},
122 [KP_SECURELEVEL] = {"securelevel", 0},
123 [KP_VNET] = {"vnet", 0},
127 * Parse the jail configuration file.
134 struct cfjail *j, *tj, *wj;
135 struct cfparam *p, *vp, *tp;
136 struct cfstring *s, *vs, *ns;
137 struct cfvar *v, *vv;
139 int did_self, jseq, pgen;
141 if (!strcmp(cfname, "-")) {
145 yyin = fopen(cfname, "r");
147 err(1, "%s", cfname);
149 if (yyparse() || yynerrs)
152 /* Separate the wildcard jails out from the actual jails. */
155 TAILQ_FOREACH_SAFE(j, &cfjails, tq, tj) {
157 if (wild_jail_name(j->name))
161 TAILQ_FOREACH(j, &cfjails, tq) {
162 /* Set aside the jail's parameters. */
164 TAILQ_CONCAT(&opp, &j->params, tq);
166 * The jail name implies its "name" or "jid" parameter,
167 * though they may also be explicitly set later on.
170 strtol(j->name, &ep, 10) && !*ep ? KP_JID : KP_NAME,
173 * Collect parameters for the jail, global parameters/variables,
174 * and any matching wildcard jails.
177 TAILQ_FOREACH(wj, &wild, tq) {
178 if (j->seq < wj->seq && !did_self) {
179 TAILQ_FOREACH(p, &opp, tq)
180 add_param(j, p, 0, NULL);
183 if (wild_jail_match(j->name, wj->name))
184 TAILQ_FOREACH(p, &wj->params, tq)
185 add_param(j, p, 0, NULL);
188 TAILQ_FOREACH(p, &opp, tq)
189 add_param(j, p, 0, NULL);
191 /* Resolve any variable substitutions. */
193 TAILQ_FOREACH(p, &j->params, tq) {
196 TAILQ_FOREACH(s, &p->val, tq) {
197 while ((v = STAILQ_FIRST(&s->vars))) {
198 TAILQ_FOREACH(vp, &j->params, tq)
199 if (!strcmp(vp->name, v->name))
203 "%s: variable \"%s\" not found",
206 j->flags |= JF_FAILED;
207 TAILQ_FOREACH(vp, &j->params, tq)
212 if (vp->flags & PF_BAD)
214 if (vp->gen == pgen) {
215 jail_warnx(j, "%s: variable loop",
219 TAILQ_FOREACH(vs, &vp->val, tq)
220 if (!STAILQ_EMPTY(&vs->vars)) {
222 TAILQ_REMOVE(&j->params, vp,
224 TAILQ_INSERT_BEFORE(p, vp, tq);
228 vs = TAILQ_FIRST(&vp->val);
229 if (TAILQ_NEXT(vs, tq) != NULL &&
231 STAILQ_NEXT(v, tq))) {
232 jail_warnx(j, "%s: array cannot be "
233 "substituted inline",
237 s->s = erealloc(s->s, s->len + vs->len + 1);
238 memmove(s->s + v->pos + vs->len,
240 s->len - v->pos + 1);
241 memcpy(s->s + v->pos, vs->s, vs->len);
243 while ((vv = STAILQ_NEXT(vv, tq)))
246 while ((vs = TAILQ_NEXT(vs, tq))) {
247 ns = emalloc(sizeof(struct cfstring));
248 ns->s = estrdup(vs->s);
250 STAILQ_INIT(&ns->vars);
251 TAILQ_INSERT_AFTER(&p->val, s, ns, tq);
256 STAILQ_REMOVE_HEAD(&s->vars, tq);
262 /* Free the jail's original parameter list and any variables. */
263 while ((p = TAILQ_FIRST(&opp)))
265 TAILQ_FOREACH_SAFE(p, &j->params, tq, tp)
266 if (p->flags & PF_VAR)
267 free_param(&j->params, p);
269 while ((wj = TAILQ_FIRST(&wild))) {
271 while ((p = TAILQ_FIRST(&wj->params)))
272 free_param(&wj->params, p);
273 TAILQ_REMOVE(&wild, wj, tq);
278 * Create a new jail record.
285 j = emalloc(sizeof(struct cfjail));
286 memset(j, 0, sizeof(struct cfjail));
287 TAILQ_INIT(&j->params);
288 STAILQ_INIT(&j->dep[DEP_FROM]);
289 STAILQ_INIT(&j->dep[DEP_TO]);
291 TAILQ_INSERT_TAIL(&cfjails, j, tq);
296 * Add a parameter to a jail.
299 add_param(struct cfjail *j, const struct cfparam *p, enum intparam ipnum,
302 struct cfstrings nss;
303 struct cfparam *dp, *np;
304 struct cfstring *s, *ns;
305 struct cfvar *v, *nv;
311 /* Create a single anonymous jail if one doesn't yet exist. */
312 j = TAILQ_LAST(&cfjails, cfjails);
321 * Make a copy of the parameter's string list,
322 * which may be freed if it's overridden later.
324 TAILQ_FOREACH(s, &p->val, tq) {
325 ns = emalloc(sizeof(struct cfstring));
326 ns->s = estrdup(s->s);
328 STAILQ_INIT(&ns->vars);
329 STAILQ_FOREACH(v, &s->vars, tq) {
330 nv = emalloc(sizeof(struct cfvar));
331 nv->name = strdup(v->name);
333 STAILQ_INSERT_TAIL(&ns->vars, nv, tq);
335 TAILQ_INSERT_TAIL(&nss, ns, tq);
339 if (ipnum != IP__NULL) {
340 name = intparams[ipnum].name;
341 flags |= intparams[ipnum].flags;
342 } else if ((cs = strchr(value, '='))) {
343 tname = alloca(cs - value + 1);
344 strlcpy(tname, value, cs - value + 1);
352 ns = emalloc(sizeof(struct cfstring));
353 ns->s = estrdup(value);
354 ns->len = strlen(value);
355 STAILQ_INIT(&ns->vars);
356 TAILQ_INSERT_TAIL(&nss, ns, tq);
360 /* See if this parameter has already been added. */
361 if (ipnum != IP__NULL)
362 dp = j->intparams[ipnum];
364 TAILQ_FOREACH(dp, &j->params, tq)
365 if (!(dp->flags & PF_CONV) && equalopts(dp->name, name))
368 /* Found it - append or replace. */
369 if (dp->flags & PF_IMMUTABLE) {
370 jail_warnx(j, "cannot redefine variable \"%s\".",
374 if (strcmp(dp->name, name)) {
376 dp->name = estrdup(name);
378 if (!(flags & PF_APPEND) || TAILQ_EMPTY(&nss))
379 free_param_strings(dp);
380 TAILQ_CONCAT(&dp->val, &nss, tq);
383 /* Not found - add it. */
384 np = emalloc(sizeof(struct cfparam));
385 np->name = estrdup(name);
386 TAILQ_INIT(&np->val);
387 TAILQ_CONCAT(&np->val, &nss, tq);
390 TAILQ_INSERT_TAIL(&j->params, np, tq);
391 if (ipnum != IP__NULL)
392 j->intparams[ipnum] = np;
394 for (ipnum = IP__NULL + 1; ipnum < IP_NPARAM; ipnum++)
395 if (!(intparams[ipnum].flags & PF_CONV) &&
396 equalopts(name, intparams[ipnum].name)) {
397 j->intparams[ipnum] = np;
398 np->flags |= intparams[ipnum].flags;
405 * Return if a boolean parameter exists and is true.
408 bool_param(const struct cfparam *p)
414 cs = strrchr(p->name, '.');
415 return !strncmp(cs ? cs + 1 : p->name, "no", 2) ^
416 (TAILQ_EMPTY(&p->val) ||
417 !strcasecmp(TAILQ_LAST(&p->val, cfstrings)->s, "true") ||
418 (strtol(TAILQ_LAST(&p->val, cfstrings)->s, NULL, 10)));
422 * Set an integer if a parameter if it exists.
425 int_param(const struct cfparam *p, int *ip)
427 if (p == NULL || TAILQ_EMPTY(&p->val))
429 *ip = strtol(TAILQ_LAST(&p->val, cfstrings)->s, NULL, 10);
434 * Return the string value of a scalar parameter if it exists.
437 string_param(const struct cfparam *p)
439 return (p && !TAILQ_EMPTY(&p->val)
440 ? TAILQ_LAST(&p->val, cfstrings)->s : NULL);
444 * Check syntax and values of internal parameters. Set some internal
445 * parameters based on the values of others.
448 check_intparams(struct cfjail *j)
457 #if defined(INET) || defined(INET6)
458 struct addrinfo hints;
459 struct addrinfo *ai0, *ai;
460 const char *hostname;
464 struct in_addr addr4;
466 char avalue4[INET_ADDRSTRLEN];
469 struct in6_addr addr6;
471 char avalue6[INET6_ADDRSTRLEN];
475 /* Check format of boolan and integer values. */
476 TAILQ_FOREACH(p, &j->params, tq) {
477 if (!TAILQ_EMPTY(&p->val) && (p->flags & (PF_BOOL | PF_INT))) {
478 val = TAILQ_LAST(&p->val, cfstrings)->s;
479 if (p->flags & PF_BOOL) {
480 if (strcasecmp(val, "false") &&
481 strcasecmp(val, "true") &&
482 ((void)strtol(val, &ep, 10), *ep)) {
484 "%s: unknown boolean value \"%s\"",
489 (void)strtol(val, &ep, 10);
490 if (ep == val || *ep) {
492 "%s: non-integer value \"%s\"",
500 #if defined(INET) || defined(INET6)
502 * The ip_hostname parameter looks up the hostname, and adds parameters
503 * for any IP addresses it finds.
505 if (((j->flags & JF_OP_MASK) != JF_STOP ||
506 j->intparams[IP_INTERFACE] != NULL) &&
507 bool_param(j->intparams[IP_IP_HOSTNAME]) &&
508 (hostname = string_param(j->intparams[KP_HOST_HOSTNAME]))) {
509 j->intparams[IP_IP_HOSTNAME] = NULL;
511 * Silently ignore unsupported address families from
515 ip4ok = feature_present("inet");
518 ip6ok = feature_present("inet6");
521 #if defined(INET) && defined(INET6)
529 /* Look up the hostname (or get the address) */
530 memset(&hints, 0, sizeof(hints));
531 hints.ai_socktype = SOCK_STREAM;
533 #if defined(INET) && defined(INET6)
534 ip4ok ? (ip6ok ? PF_UNSPEC : PF_INET) : PF_INET6;
540 gicode = getaddrinfo(hostname, NULL, &hints, &ai0);
542 jail_warnx(j, "host.hostname %s: %s", hostname,
543 gai_strerror(gicode));
547 * Convert the addresses to ASCII so jailparam
548 * can convert them back. Errors are not
551 for (ai = ai0; ai; ai = ai->ai_next)
552 switch (ai->ai_family) {
556 &((struct sockaddr_in *)
557 (void *)ai->ai_addr)->
558 sin_addr, sizeof(addr4));
559 if (inet_ntop(AF_INET,
561 INET_ADDRSTRLEN) == NULL)
563 add_param(j, NULL, KP_IP4_ADDR,
570 &((struct sockaddr_in6 *)
571 (void *)ai->ai_addr)->
572 sin6_addr, sizeof(addr6));
573 if (inet_ntop(AF_INET6,
575 INET6_ADDRSTRLEN) == NULL)
577 add_param(j, NULL, KP_IP6_ADDR,
588 * IP addresses may include an interface to set that address on,
589 * a netmask/suffix for that address and options for ifconfig.
590 * These are copied to an internal command parameter and then stripped
591 * so they won't be passed on to jailparam_set.
593 defif = string_param(j->intparams[IP_INTERFACE]) != NULL;
595 if (j->intparams[KP_IP4_ADDR] != NULL) {
596 TAILQ_FOREACH(s, &j->intparams[KP_IP4_ADDR]->val, tq) {
597 cs = strchr(s->s, '|');
599 add_param(j, NULL, IP__IP4_IFADDR, s->s);
601 s->len -= cs + 1 - s->s;
602 memmove(s->s, cs + 1, s->len + 1);
604 if ((cs = strchr(s->s, '/')) != NULL) {
608 if ((cs = strchr(s->s, ' ')) != NULL) {
616 if (j->intparams[KP_IP6_ADDR] != NULL) {
617 TAILQ_FOREACH(s, &j->intparams[KP_IP6_ADDR]->val, tq) {
618 cs = strchr(s->s, '|');
620 add_param(j, NULL, IP__IP6_IFADDR, s->s);
622 s->len -= cs + 1 - s->s;
623 memmove(s->s, cs + 1, s->len + 1);
625 if ((cs = strchr(s->s, '/')) != NULL) {
629 if ((cs = strchr(s->s, ' ')) != NULL) {
639 * Read mount.fstab file(s), and treat each line as its own mount
642 if (j->intparams[IP_MOUNT_FSTAB] != NULL) {
643 TAILQ_FOREACH(s, &j->intparams[IP_MOUNT_FSTAB]->val, tq) {
646 f = fopen(s->s, "r");
648 jail_warnx(j, "mount.fstab: %s: %s",
649 s->s, strerror(errno));
653 while ((ln = fgetln(f, &lnlen))) {
654 if ((cs = memchr(ln, '#', lnlen - 1)))
656 if (ln[lnlen - 1] == '\n' ||
657 ln[lnlen - 1] == '#')
658 ln[lnlen - 1] = '\0';
660 cs = alloca(lnlen + 1);
661 strlcpy(cs, ln, lnlen + 1);
664 add_param(j, NULL, IP__MOUNT_FROM_FSTAB, ln);
675 * Import parameters into libjail's binary jailparam format.
678 import_params(struct cfjail *j)
681 struct cfstring *s, *ts;
682 struct jailparam *jp;
689 TAILQ_FOREACH(p, &j->params, tq)
690 if (!(p->flags & PF_INTERNAL))
692 j->jp = jp = emalloc(j->njp * sizeof(struct jailparam));
693 TAILQ_FOREACH(p, &j->params, tq) {
694 if (p->flags & PF_INTERNAL)
696 if (jailparam_init(jp, p->name) < 0) {
698 jail_warnx(j, "%s", jail_errmsg);
702 if (TAILQ_EMPTY(&p->val))
704 else if (!jp->jp_elemlen ||
705 !TAILQ_NEXT(TAILQ_FIRST(&p->val), tq)) {
707 * Scalar parameters silently discard multiple (array)
708 * values, keeping only the last value added. This
709 * lets values added from the command line append to
710 * arrays wthout pre-checking the type.
712 value = TAILQ_LAST(&p->val, cfstrings)->s;
715 * Convert arrays into comma-separated strings, which
716 * jailparam_import will then convert back into arrays.
719 TAILQ_FOREACH(s, &p->val, tq)
720 vallen += s->len + 1;
721 value = alloca(vallen);
723 TAILQ_FOREACH_SAFE(s, &p->val, tq, ts) {
724 memcpy(cs, s->s, s->len);
728 value[vallen - 1] = '\0';
730 if (jailparam_import(jp, value) < 0) {
732 jail_warnx(j, "%s", jail_errmsg);
737 jailparam_free(j->jp, j->njp);
746 * Check if options are equal (with or without the "no" prefix).
749 equalopts(const char *opt1, const char *opt2)
753 /* "opt" vs. "opt" or "noopt" vs. "noopt" */
754 if (strcmp(opt1, opt2) == 0)
756 /* "noopt" vs. "opt" */
757 if (strncmp(opt1, "no", 2) == 0 && strcmp(opt1 + 2, opt2) == 0)
759 /* "opt" vs. "noopt" */
760 if (strncmp(opt2, "no", 2) == 0 && strcmp(opt1, opt2 + 2) == 0)
762 while ((p = strchr(opt1, '.')) != NULL &&
763 !strncmp(opt1, opt2, ++p - opt1)) {
766 /* "foo.noopt" vs. "foo.opt" */
767 if (strncmp(opt1, "no", 2) == 0 && strcmp(opt1 + 2, opt2) == 0)
769 /* "foo.opt" vs. "foo.noopt" */
770 if (strncmp(opt2, "no", 2) == 0 && strcmp(opt1, opt2 + 2) == 0)
777 * See if a jail name matches a wildcard.
780 wild_jail_match(const char *jname, const char *wname)
782 const char *jc, *jd, *wc, *wd;
785 * A non-final "*" component in the wild name matches a single jail
786 * component, and a final "*" matches one or more jail components.
788 for (jc = jname, wc = wname;
789 (jd = strchr(jc, '.')) && (wd = strchr(wc, '.'));
790 jc = jd + 1, wc = wd + 1)
791 if (strncmp(jc, wc, jd - jc + 1) && strncmp(wc, "*.", 2))
793 return (!strcmp(jc, wc) || !strcmp(wc, "*"));
797 * Return if a jail name is a wildcard.
800 wild_jail_name(const char *wname)
804 for (wc = strchr(wname, '*'); wc; wc = strchr(wc + 1, '*'))
805 if ((wc == wname || wc[-1] == '.') &&
806 (wc[1] == '\0' || wc[1] == '.'))
812 * Free a parameter record and all its strings and variables.
815 free_param(struct cfparams *pp, struct cfparam *p)
818 free_param_strings(p);
819 TAILQ_REMOVE(pp, p, tq);
824 free_param_strings(struct cfparam *p)
829 while ((s = TAILQ_FIRST(&p->val))) {
831 while ((v = STAILQ_FIRST(&s->vars))) {
833 STAILQ_REMOVE_HEAD(&s->vars, tq);
836 TAILQ_REMOVE(&p->val, s, tq);