2 * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
4 * Copyright (c) 1999 Poul-Henning Kamp.
5 * Copyright (c) 2009-2012 James Gritton
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
11 * 1. Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in the
15 * documentation and/or other materials provided with the distribution.
17 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
18 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
21 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
30 #include <sys/cdefs.h>
31 __FBSDID("$FreeBSD$");
33 #include <sys/types.h>
35 #include <sys/socket.h>
36 #include <sys/sysctl.h>
38 #include <arpa/inet.h>
39 #include <netinet/in.h>
51 #define JP_RDTUN(jp) (((jp)->jp_ctltype & CTLFLAG_RDTUN) == CTLFLAG_RDTUN)
64 static void clear_persist(struct cfjail *j);
65 static int update_jail(struct cfjail *j);
66 static int rdtun_params(struct cfjail *j, int dofail);
67 static void running_jid(struct cfjail *j, int dflag);
68 static void jail_quoted_warnx(const struct cfjail *j, const char *name_msg,
69 const char *noname_msg);
70 static int jailparam_set_note(const struct cfjail *j, struct jailparam *jp,
71 unsigned njp, int flags);
72 static void print_jail(FILE *fp, struct cfjail *j, int oldcl);
73 static void print_param(FILE *fp, const struct cfparam *p, int sep, int doname);
74 static void quoted_print(FILE *fp, char *str);
75 static void usage(void);
77 static struct permspec perm_sysctl[] = {
78 { "security.jail.set_hostname_allowed", KP_ALLOW_SET_HOSTNAME, 0 },
79 { "security.jail.sysvipc_allowed", KP_ALLOW_SYSVIPC, 0 },
80 { "security.jail.allow_raw_sockets", KP_ALLOW_RAW_SOCKETS, 0 },
81 { "security.jail.chflags_allowed", KP_ALLOW_CHFLAGS, 0 },
82 { "security.jail.mount_allowed", KP_ALLOW_MOUNT, 0 },
83 { "security.jail.socket_unixiproute_only", KP_ALLOW_SOCKET_AF, 1 },
86 static const enum intparam startcommands[] = {
109 static const enum intparam stopcommands[] = {
119 IP__MOUNT_FROM_FSTAB,
131 main(int argc, char **argv)
139 int ch, docf, error, i, oldcl, sysval;
141 char enforce_statfs[4];
142 #if defined(INET) || defined(INET6)
145 #if defined(INET) && defined(INET6)
146 struct in6_addr addr6;
155 while ((ch = getopt(argc, argv, "cdf:hiJ:lmn:p:qrRs:u:U:v")) != -1) {
167 #if defined(INET) || defined(INET6)
168 add_param(NULL, NULL, IP_IP_HOSTNAME, NULL);
180 add_param(NULL, NULL, IP_EXEC_CLEAN, NULL);
187 add_param(NULL, NULL, KP_NAME, optarg);
191 paralimit = strtol(optarg, NULL, 10);
206 add_param(NULL, NULL, KP_SECURELEVEL, optarg);
210 add_param(NULL, NULL, IP_EXEC_JAIL_USER, optarg);
211 add_param(NULL, NULL, IP_EXEC_SYSTEM_JAIL_USER, NULL);
215 add_param(NULL, NULL, IP_EXEC_JAIL_USER, optarg);
216 add_param(NULL, NULL, IP_EXEC_SYSTEM_JAIL_USER,
230 /* Find out which of the four command line styles this is. */
233 /* Old-style command line with four fixed parameters */
234 if (argc < 4 || argv[0][0] != '/')
239 add_param(NULL, NULL, KP_PATH, argv[0]);
240 add_param(NULL, NULL, KP_HOST_HOSTNAME, argv[1]);
241 #if defined(INET) || defined(INET6)
242 if (argv[2][0] != '\0') {
243 for (cs = argv[2];; cs = ncs + 1) {
244 ncs = strchr(cs, ',');
247 add_param(NULL, NULL,
248 #if defined(INET) && defined(INET6)
249 inet_pton(AF_INET6, cs, &addr6) == 1
250 ? KP_IP6_ADDR : KP_IP4_ADDR,
262 for (i = 3; i < argc; i++)
263 add_param(NULL, NULL, IP_COMMAND, argv[i]);
264 /* Emulate the defaults from security.jail.* sysctls. */
265 sysvallen = sizeof(sysval);
266 if (sysctlbyname("security.jail.jailed", &sysval, &sysvallen,
267 NULL, 0) == 0 && sysval == 0) {
268 for (pi = 0; pi < sizeof(perm_sysctl) /
269 sizeof(perm_sysctl[0]); pi++) {
270 sysvallen = sizeof(sysval);
271 if (sysctlbyname(perm_sysctl[pi].name,
272 &sysval, &sysvallen, NULL, 0) == 0)
273 add_param(NULL, NULL,
274 perm_sysctl[pi].ipnum,
279 sysvallen = sizeof(sysval);
280 if (sysctlbyname("security.jail.enforce_statfs",
281 &sysval, &sysvallen, NULL, 0) == 0) {
282 snprintf(enforce_statfs,
283 sizeof(enforce_statfs), "%d", sysval);
284 add_param(NULL, NULL, KP_ENFORCE_STATFS,
288 } else if (op == JF_STOP) {
289 /* Jail remove, perhaps using the config file */
290 if (!docf || argc == 0)
293 for (i = 0; i < argc; i++)
294 if (strchr(argv[i], '='))
296 if ((docf = !Rflag &&
297 (!strcmp(cfname, "-") || stat(cfname, &st) == 0)))
299 note_remove = docf || argc > 1 || wild_jail_name(argv[0]);
300 } else if (argc > 1 || (argc == 1 && strchr(argv[0], '='))) {
301 /* Single jail specified on the command line */
305 for (i = 0; i < argc; i++) {
306 if (!strncmp(argv[i], "command", 7) &&
307 (argv[i][7] == '\0' || argv[i][7] == '=')) {
308 if (argv[i][7] == '=')
309 add_param(NULL, NULL, IP_COMMAND,
311 for (i++; i < argc; i++)
312 add_param(NULL, NULL, IP_COMMAND,
316 else if (!strncmp(argv[i], "ip4.addr=", 9)) {
317 for (cs = argv[i] + 9;; cs = ncs + 1) {
318 ncs = strchr(cs, ',');
321 add_param(NULL, NULL, KP_IP4_ADDR, cs);
328 else if (!strncmp(argv[i], "ip6.addr=", 9)) {
329 for (cs = argv[i] + 9;; cs = ncs + 1) {
330 ncs = strchr(cs, ',');
333 add_param(NULL, NULL, KP_IP6_ADDR, cs);
340 add_param(NULL, NULL, 0, argv[i]);
343 /* From the config file, perhaps with a specified jail */
349 /* Find out which jails will be run. */
353 for (i = 0; i < argc; i++)
354 if (start_state(argv[i], docf, op, Rflag) < 0)
357 if (start_state(argv[0], docf, op, 0) < 0)
362 if (JidFile != NULL) {
363 jfp = fopen(JidFile, "w");
365 err(1, "open %s", JidFile);
371 * The main loop: Get an available jail and perform the required
372 * operation on it. When that is done, the jail may be finished,
373 * or it may go back for the next step.
375 while ((j = next_jail()))
377 if (j->flags & JF_FAILED) {
379 if (j->comparam == NULL) {
384 if (!(j->flags & JF_PARAMS))
386 j->flags |= JF_PARAMS;
388 add_param(j, NULL, IP_ALLOW_DYING, NULL);
389 if (check_intparams(j) < 0)
391 if ((j->flags & (JF_START | JF_SET)) &&
392 import_params(j) < 0)
397 (j->flags & (JF_SET | JF_DEPEND)) == JF_SET
398 ? dflag || bool_param(j->intparams[IP_ALLOW_DYING])
400 if (finish_command(j))
403 switch (j->flags & JF_OP_MASK) {
405 * These operations just turn into a different op
406 * depending on the jail's current status.
409 j->flags = j->jid < 0 ? JF_START : JF_SET;
413 jail_quoted_warnx(j, "not found",
414 "no jail specified");
418 j->flags = rdtun_params(j, 0) ? JF_RESTART : JF_SET;
419 if (j->flags == JF_RESTART)
422 case JF_START_SET_RESTART:
423 j->flags = j->jid < 0 ? JF_START
424 : rdtun_params(j, 0) ? JF_RESTART : JF_SET;
425 if (j->flags == JF_RESTART)
429 switch (j->flags & JF_OP_MASK) {
431 if (j->comparam == NULL) {
433 !(j->flags & (JF_DEPEND | JF_WILD))) {
434 jail_quoted_warnx(j, "already exists",
442 goto jail_create_done;
443 j->comparam = startcommands;
451 print_jail(jfp, j, oldcl);
456 if (j->jid < 0 && !(j->flags & JF_DEPEND)) {
457 jail_quoted_warnx(j, "not found",
458 "no jail specified");
464 if (!(j->flags & JF_DEPEND)) {
465 if (rdtun_params(j, 1) < 0 ||
468 if (verbose >= 0 && (j->name || verbose > 0))
469 jail_note(j, "updated\n");
476 if (j->comparam == NULL) {
480 if (!(j->flags & (JF_DEPEND|JF_WILD))) {
486 goto jail_remove_done;
488 j->comparam = stopcommands;
490 } else if ((j->flags & JF_FAILED) && j->jid > 0)
491 goto jail_remove_done;
496 if ((j->flags & (JF_START | JF_FAILED)) == JF_START) {
498 j->flags &= ~JF_STOP;
500 requeue(j, j->ndeps ? &depend : &ready);
512 * Mark a jail's failure for future handling.
515 failed(struct cfjail *j)
517 j->flags |= JF_FAILED;
518 TAILQ_REMOVE(j->queue, j, tq);
519 TAILQ_INSERT_HEAD(&ready, j, tq);
524 * Exit slightly more gracefully when out of memory.
538 erealloc(void *ptr, size_t size)
542 p = realloc(ptr, size);
549 estrdup(const char *str)
560 * Print a message including an optional jail name.
563 jail_note(const struct cfjail *j, const char *fmt, ...)
571 len = vsnprintf(NULL, 0, fmt, tap);
573 cs = alloca(len + 1);
574 (void)vsnprintf(cs, len + 1, fmt, ap);
577 printf("%s: %s", j->name, cs);
583 * Print a warning message including an optional jail name.
586 jail_warnx(const struct cfjail *j, const char *fmt, ...)
594 len = vsnprintf(NULL, 0, fmt, tap);
596 cs = alloca(len + 1);
597 (void)vsnprintf(cs, len + 1, fmt, ap);
600 warnx("%s: %s", j->name, cs);
609 create_jail(struct cfjail *j)
611 struct iovec jiov[4];
613 struct jailparam *jp, *setparams, *setparams2, *sjp;
615 int dopersist, ns, jid, dying, didfail;
618 * Check the jail's path, with a better error message than jail_set
621 if ((path = string_param(j->intparams[KP_PATH]))) {
622 if (j->name != NULL && path[0] != '/') {
623 jail_warnx(j, "path %s: not an absolute pathname",
627 if (stat(path, &st) < 0) {
628 jail_warnx(j, "path %s: %s", path, strerror(errno));
631 if (!S_ISDIR(st.st_mode)) {
632 jail_warnx(j, "path %s: %s", path, strerror(ENOTDIR));
638 * Copy all the parameters, except that "persist" is always set when
639 * there are commands to run later.
641 dopersist = !bool_param(j->intparams[KP_PERSIST]) &&
642 (j->intparams[IP_EXEC_START] || j->intparams[IP_COMMAND] ||
643 j->intparams[IP_EXEC_POSTSTART]);
645 alloca((j->njp + dopersist) * sizeof(struct jailparam));
646 if (dopersist && jailparam_init(sjp++, "persist") < 0) {
647 jail_warnx(j, "%s", jail_errmsg);
650 for (jp = j->jp; jp < j->jp + j->njp; jp++)
651 if (!dopersist || !equalopts(jp->jp_name, "persist"))
653 ns = sjp - setparams;
656 j->jid = jailparam_set_note(j, setparams, ns, JAIL_CREATE);
657 if (j->jid < 0 && errno == EEXIST &&
658 bool_param(j->intparams[IP_ALLOW_DYING]) &&
659 int_param(j->intparams[KP_JID], &jid) && jid != 0) {
661 * The jail already exists, but may be dying.
662 * Make sure it is, in which case an update is appropriate.
664 jiov[0].iov_base = __DECONST(char *, "jid");
665 jiov[0].iov_len = sizeof("jid");
666 jiov[1].iov_base = &jid;
667 jiov[1].iov_len = sizeof(jid);
668 jiov[2].iov_base = __DECONST(char *, "dying");
669 jiov[2].iov_len = sizeof("dying");
670 jiov[3].iov_base = &dying;
671 jiov[3].iov_len = sizeof(dying);
672 if (jail_get(jiov, 4, JAIL_DYING) < 0) {
674 * It could be that the jail just barely finished
675 * dying, or it could be that the jid never existed
676 * but the name does. In either case, another try
677 * at creating the jail should do the right thing.
680 j->jid = jailparam_set_note(j, setparams, ns,
684 if (rdtun_params(j, 1) < 0) {
688 sjp = setparams2 = alloca((j->njp + dopersist) *
689 sizeof(struct jailparam));
690 for (jp = setparams; jp < setparams + ns; jp++)
692 !strcmp(jp->jp_name, "jid"))
694 j->jid = jailparam_set_note(j, setparams2,
695 sjp - setparams2, JAIL_UPDATE | JAIL_DYING);
697 * Again, perhaps the jail just finished dying.
699 if (j->jid < 0 && errno == ENOENT)
700 j->jid = jailparam_set_note(j,
701 setparams, ns, JAIL_CREATE);
705 if (j->jid < 0 && !didfail) {
706 jail_warnx(j, "%s", jail_errmsg);
710 jailparam_free(setparams, 1);
712 j->flags |= JF_PERSIST;
718 * Remove a temporarily set "persist" parameter.
721 clear_persist(struct cfjail *j)
723 struct iovec jiov[4];
726 if (!(j->flags & JF_PERSIST))
728 j->flags &= ~JF_PERSIST;
729 jiov[0].iov_base = __DECONST(char *, "jid");
730 jiov[0].iov_len = sizeof("jid");
731 jiov[1].iov_base = &j->jid;
732 jiov[1].iov_len = sizeof(j->jid);
733 jiov[2].iov_base = __DECONST(char *, "nopersist");
734 jiov[2].iov_len = sizeof("nopersist");
735 jiov[3].iov_base = NULL;
737 jid = jail_set(jiov, 4, JAIL_UPDATE);
739 jail_note(j, "jail_set(JAIL_UPDATE) jid=%d nopersist%s%s\n",
740 j->jid, jid < 0 ? ": " : "",
741 jid < 0 ? strerror(errno) : "");
745 * Set a jail's parameters.
748 update_jail(struct cfjail *j)
750 struct jailparam *jp, *setparams, *sjp;
754 for (jp = j->jp; jp < j->jp + j->njp; jp++)
759 sjp = setparams = alloca(++ns * sizeof(struct jailparam));
760 if (jailparam_init(sjp, "jid") < 0 ||
761 jailparam_import_raw(sjp, &j->jid, sizeof j->jid) < 0) {
762 jail_warnx(j, "%s", jail_errmsg);
766 for (jp = j->jp; jp < j->jp + j->njp; jp++)
770 jid = jailparam_set_note(j, setparams, ns,
771 bool_param(j->intparams[IP_ALLOW_DYING])
772 ? JAIL_UPDATE | JAIL_DYING : JAIL_UPDATE);
774 jail_warnx(j, "%s", jail_errmsg);
777 jailparam_free(setparams, 1);
782 * Return if a jail set would change any create-only parameters.
785 rdtun_params(struct cfjail *j, int dofail)
787 struct jailparam *jp, *rtparams, *rtjp;
790 if (j->flags & JF_RDTUN)
792 j->flags |= JF_RDTUN;
794 for (jp = j->jp; jp < j->jp + j->njp; jp++)
795 if (JP_RDTUN(jp) && strcmp(jp->jp_name, "jid"))
799 rtjp = rtparams = alloca(++nrt * sizeof(struct jailparam));
800 if (jailparam_init(rtjp, "jid") < 0 ||
801 jailparam_import_raw(rtjp, &j->jid, sizeof j->jid) < 0) {
802 jail_warnx(j, "%s", jail_errmsg);
805 for (jp = j->jp; jp < j->jp + j->njp; jp++)
806 if (JP_RDTUN(jp) && strcmp(jp->jp_name, "jid")) {
808 rtjp->jp_value = NULL;
811 if (jailparam_get(rtparams, nrt,
812 bool_param(j->intparams[IP_ALLOW_DYING]) ? JAIL_DYING : 0) > 0) {
814 for (jp = j->jp; rtjp < rtparams + nrt; jp++) {
815 if (JP_RDTUN(jp) && strcmp(jp->jp_name, "jid")) {
816 if (!((jp->jp_flags & (JP_BOOL | JP_NOBOOL)) &&
817 jp->jp_valuelen == 0 &&
818 *(int *)jp->jp_value) &&
819 !(rtjp->jp_valuelen == jp->jp_valuelen &&
820 !((jp->jp_ctltype & CTLTYPE) ==
821 CTLTYPE_STRING ? strncmp(rtjp->jp_value,
822 jp->jp_value, jp->jp_valuelen) :
823 memcmp(rtjp->jp_value, jp->jp_value,
824 jp->jp_valuelen)))) {
826 jail_warnx(j, "%s cannot be "
827 "changed after creation",
839 for (rtjp = rtparams + 1; rtjp < rtparams + nrt; rtjp++)
840 rtjp->jp_name = NULL;
841 jailparam_free(rtparams, nrt);
846 * Get the jail's jid if it is running.
849 running_jid(struct cfjail *j, int dflag)
851 struct iovec jiov[2];
856 if ((pval = string_param(j->intparams[KP_JID]))) {
857 if (!(jid = strtol(pval, &ep, 10)) || *ep) {
861 jiov[0].iov_base = __DECONST(char *, "jid");
862 jiov[0].iov_len = sizeof("jid");
863 jiov[1].iov_base = &jid;
864 jiov[1].iov_len = sizeof(jid);
865 } else if ((pval = string_param(j->intparams[KP_NAME]))) {
866 jiov[0].iov_base = __DECONST(char *, "name");
867 jiov[0].iov_len = sizeof("name");
868 jiov[1].iov_len = strlen(pval) + 1;
869 jiov[1].iov_base = alloca(jiov[1].iov_len);
870 strcpy(jiov[1].iov_base, pval);
875 j->jid = jail_get(jiov, 2, dflag ? JAIL_DYING : 0);
879 jail_quoted_warnx(const struct cfjail *j, const char *name_msg,
880 const char *noname_msg)
884 if ((pval = j->name) || (pval = string_param(j->intparams[KP_JID])) ||
885 (pval = string_param(j->intparams[KP_NAME])))
886 warnx("\"%s\" %s", pval, name_msg);
888 warnx("%s", noname_msg);
892 * Set jail parameters and possibly print them out.
895 jailparam_set_note(const struct cfjail *j, struct jailparam *jp, unsigned njp,
902 jid = jailparam_set(jp, njp, flags);
904 jail_note(j, "jail_set(%s%s)",
905 (flags & (JAIL_CREATE | JAIL_UPDATE)) == JAIL_CREATE
906 ? "JAIL_CREATE" : "JAIL_UPDATE",
907 (flags & JAIL_DYING) ? " | JAIL_DYING" : "");
908 for (i = 0; i < njp; i++) {
909 printf(" %s", jp[i].jp_name);
910 if (jp[i].jp_value == NULL)
913 value = jailparam_export(jp + i);
915 err(1, "jailparam_export");
916 quoted_print(stdout, value);
920 printf(": %s", strerror(errno));
927 * Print a jail record.
930 print_jail(FILE *fp, struct cfjail *j, int oldcl)
935 fprintf(fp, "%d\t", j->jid);
936 print_param(fp, j->intparams[KP_PATH], ',', 0);
938 print_param(fp, j->intparams[KP_HOST_HOSTNAME], ',', 0);
941 print_param(fp, j->intparams[KP_IP4_ADDR], ',', 0);
943 if (j->intparams[KP_IP4_ADDR] &&
944 !TAILQ_EMPTY(&j->intparams[KP_IP4_ADDR]->val) &&
945 j->intparams[KP_IP6_ADDR] &&
946 !TAILQ_EMPTY(&j->intparams[KP_IP6_ADDR]->val))
951 print_param(fp, j->intparams[KP_IP6_ADDR], ',', 0);
954 print_param(fp, j->intparams[IP_COMMAND], ' ', 0);
956 fprintf(fp, "jid=%d", j->jid);
957 TAILQ_FOREACH(p, &j->params, tq)
958 if (strcmp(p->name, "jid")) {
960 print_param(fp, p, ',', 1);
967 * Print a parameter value, or a name=value pair.
970 print_param(FILE *fp, const struct cfparam *p, int sep, int doname)
972 const struct cfstring *s, *ts;
976 if (p == NULL || TAILQ_EMPTY(&p->val))
980 TAILQ_FOREACH_SAFE(s, &p->val, tq, ts) {
981 quoted_print(fp, s->s);
988 * Print a string with quotes around spaces.
991 quoted_print(FILE *fp, char *str)
997 : strchr(p, '\'') ? '"'
998 : strchr(p, '"') ? '\''
999 : strchr(p, ' ') || strchr(p, '\t') ? '"'
1003 while ((c = *p++)) {
1004 if (c == '\\' || c == qc)
1016 (void)fprintf(stderr,
1017 "usage: jail [-dhilqv] [-J jid_file] [-u username] [-U username]\n"
1018 " -[cmr] param=value ... [command=command ...]\n"
1019 " jail [-dqv] [-f file] -[cmr] [jail]\n"
1020 " jail [-qv] [-f file] -[rR] ['*' | jail ...]\n"
1021 " jail [-dhilqv] [-J jid_file] [-u username] [-U username]\n"
1022 " [-n jailname] [-s securelevel]\n"
1023 " path hostname [ip[,...]] command ...\n");