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)
63 const char *separator = "\t";
65 static void clear_persist(struct cfjail *j);
66 static int update_jail(struct cfjail *j);
67 static int rdtun_params(struct cfjail *j, int dofail);
68 static void running_jid(struct cfjail *j, int dflag);
69 static void jail_quoted_warnx(const struct cfjail *j, const char *name_msg,
70 const char *noname_msg);
71 static int jailparam_set_note(const struct cfjail *j, struct jailparam *jp,
72 unsigned njp, int flags);
73 static void print_jail(FILE *fp, struct cfjail *j, int oldcl, int running);
74 static void print_param(FILE *fp, const struct cfparam *p, int sep, int doname);
75 static void show_jails(void);
76 static void quoted_print(FILE *fp, char *str);
77 static void usage(void);
79 static struct permspec perm_sysctl[] = {
80 { "security.jail.set_hostname_allowed", KP_ALLOW_SET_HOSTNAME, 0 },
81 { "security.jail.sysvipc_allowed", KP_ALLOW_SYSVIPC, 0 },
82 { "security.jail.allow_raw_sockets", KP_ALLOW_RAW_SOCKETS, 0 },
83 { "security.jail.chflags_allowed", KP_ALLOW_CHFLAGS, 0 },
84 { "security.jail.mount_allowed", KP_ALLOW_MOUNT, 0 },
85 { "security.jail.socket_unixiproute_only", KP_ALLOW_SOCKET_AF, 1 },
88 static const enum intparam startcommands[] = {
110 static const enum intparam stopcommands[] = {
120 IP__MOUNT_FROM_FSTAB,
132 main(int argc, char **argv)
140 int ch, docf, error, i, oldcl, sysval;
142 char enforce_statfs[4];
143 #if defined(INET) || defined(INET6)
146 #if defined(INET) && defined(INET6)
147 struct in6_addr addr6;
156 while ((ch = getopt(argc, argv, "cde:f:hiJ:lmn:p:qrRs:u:U:v")) != -1) {
172 #if defined(INET) || defined(INET6)
173 add_param(NULL, NULL, IP_IP_HOSTNAME, NULL);
185 add_param(NULL, NULL, IP_EXEC_CLEAN, NULL);
192 add_param(NULL, NULL, KP_NAME, optarg);
196 paralimit = strtol(optarg, NULL, 10);
211 add_param(NULL, NULL, KP_SECURELEVEL, optarg);
215 add_param(NULL, NULL, IP_EXEC_JAIL_USER, optarg);
216 add_param(NULL, NULL, IP_EXEC_SYSTEM_JAIL_USER, NULL);
220 add_param(NULL, NULL, IP_EXEC_JAIL_USER, optarg);
221 add_param(NULL, NULL, IP_EXEC_SYSTEM_JAIL_USER,
235 /* Find out which of the four command line styles this is. */
238 /* Old-style command line with four fixed parameters */
239 if (argc < 4 || argv[0][0] != '/')
244 add_param(NULL, NULL, KP_PATH, argv[0]);
245 add_param(NULL, NULL, KP_HOST_HOSTNAME, argv[1]);
246 #if defined(INET) || defined(INET6)
247 if (argv[2][0] != '\0') {
248 for (cs = argv[2];; cs = ncs + 1) {
249 ncs = strchr(cs, ',');
252 add_param(NULL, NULL,
253 #if defined(INET) && defined(INET6)
254 inet_pton(AF_INET6, cs, &addr6) == 1
255 ? KP_IP6_ADDR : KP_IP4_ADDR,
267 for (i = 3; i < argc; i++)
268 add_param(NULL, NULL, IP_COMMAND, argv[i]);
269 /* Emulate the defaults from security.jail.* sysctls. */
270 sysvallen = sizeof(sysval);
271 if (sysctlbyname("security.jail.jailed", &sysval, &sysvallen,
272 NULL, 0) == 0 && sysval == 0) {
273 for (pi = 0; pi < sizeof(perm_sysctl) /
274 sizeof(perm_sysctl[0]); pi++) {
275 sysvallen = sizeof(sysval);
276 if (sysctlbyname(perm_sysctl[pi].name,
277 &sysval, &sysvallen, NULL, 0) == 0)
278 add_param(NULL, NULL,
279 perm_sysctl[pi].ipnum,
284 sysvallen = sizeof(sysval);
285 if (sysctlbyname("security.jail.enforce_statfs",
286 &sysval, &sysvallen, NULL, 0) == 0) {
287 snprintf(enforce_statfs,
288 sizeof(enforce_statfs), "%d", sysval);
289 add_param(NULL, NULL, KP_ENFORCE_STATFS,
293 } else if (op == JF_STOP || op == JF_SHOW) {
294 /* Just print list of all configured non-wildcard jails */
300 /* Jail remove, perhaps using the config file */
301 if (!docf || argc == 0)
304 for (i = 0; i < argc; i++)
305 if (strchr(argv[i], '='))
307 if ((docf = !Rflag &&
308 (!strcmp(cfname, "-") || stat(cfname, &st) == 0)))
310 note_remove = docf || argc > 1 || wild_jail_name(argv[0]);
311 } else if (argc > 1 || (argc == 1 && strchr(argv[0], '='))) {
312 /* Single jail specified on the command line */
316 for (i = 0; i < argc; i++) {
317 if (!strncmp(argv[i], "command", 7) &&
318 (argv[i][7] == '\0' || argv[i][7] == '=')) {
319 if (argv[i][7] == '=')
320 add_param(NULL, NULL, IP_COMMAND,
322 for (i++; i < argc; i++)
323 add_param(NULL, NULL, IP_COMMAND,
327 else if (!strncmp(argv[i], "ip4.addr=", 9)) {
328 for (cs = argv[i] + 9;; cs = ncs + 1) {
329 ncs = strchr(cs, ',');
332 add_param(NULL, NULL, KP_IP4_ADDR, cs);
339 else if (!strncmp(argv[i], "ip6.addr=", 9)) {
340 for (cs = argv[i] + 9;; cs = ncs + 1) {
341 ncs = strchr(cs, ',');
344 add_param(NULL, NULL, KP_IP6_ADDR, cs);
351 add_param(NULL, NULL, 0, argv[i]);
354 /* From the config file, perhaps with a specified jail */
360 /* Find out which jails will be run. */
364 for (i = 0; i < argc; i++)
365 if (start_state(argv[i], docf, op, Rflag) < 0)
368 if (start_state(argv[0], docf, op, 0) < 0)
373 if (JidFile != NULL) {
374 jfp = fopen(JidFile, "w");
376 err(1, "open %s", JidFile);
382 * The main loop: Get an available jail and perform the required
383 * operation on it. When that is done, the jail may be finished,
384 * or it may go back for the next step.
386 while ((j = next_jail()))
388 if (j->flags & JF_FAILED) {
390 if (j->comparam == NULL) {
395 if (!(j->flags & JF_PARAMS))
397 j->flags |= JF_PARAMS;
399 add_param(j, NULL, IP_ALLOW_DYING, NULL);
400 if (check_intparams(j) < 0)
402 if ((j->flags & (JF_START | JF_SET)) &&
403 import_params(j) < 0)
408 (j->flags & (JF_SET | JF_DEPEND)) == JF_SET
409 ? dflag || bool_param(j->intparams[IP_ALLOW_DYING])
411 if (finish_command(j))
414 switch (j->flags & JF_OP_MASK) {
416 * These operations just turn into a different op
417 * depending on the jail's current status.
420 j->flags = j->jid < 0 ? JF_START : JF_SET;
424 jail_quoted_warnx(j, "not found",
425 "no jail specified");
429 j->flags = rdtun_params(j, 0) ? JF_RESTART : JF_SET;
430 if (j->flags == JF_RESTART)
433 case JF_START_SET_RESTART:
434 j->flags = j->jid < 0 ? JF_START
435 : rdtun_params(j, 0) ? JF_RESTART : JF_SET;
436 if (j->flags == JF_RESTART)
440 switch (j->flags & JF_OP_MASK) {
442 if (j->comparam == NULL) {
444 !(j->flags & (JF_DEPEND | JF_WILD))) {
445 jail_quoted_warnx(j, "already exists",
453 goto jail_create_done;
454 j->comparam = startcommands;
462 print_jail(jfp, j, oldcl, 1);
467 if (j->jid < 0 && !(j->flags & JF_DEPEND)) {
468 jail_quoted_warnx(j, "not found",
469 "no jail specified");
475 if (!(j->flags & JF_DEPEND)) {
476 if (rdtun_params(j, 1) < 0 ||
479 if (verbose >= 0 && (j->name || verbose > 0))
480 jail_note(j, "updated\n");
487 if (j->comparam == NULL) {
491 if (!(j->flags & (JF_DEPEND|JF_WILD))) {
497 goto jail_remove_done;
499 j->comparam = stopcommands;
501 } else if ((j->flags & JF_FAILED) && j->jid > 0)
502 goto jail_remove_done;
507 if ((j->flags & (JF_START | JF_FAILED)) == JF_START) {
509 j->flags &= ~JF_STOP;
511 requeue(j, j->ndeps ? &depend : &ready);
523 * Mark a jail's failure for future handling.
526 failed(struct cfjail *j)
528 j->flags |= JF_FAILED;
529 TAILQ_REMOVE(j->queue, j, tq);
530 TAILQ_INSERT_HEAD(&ready, j, tq);
535 * Exit slightly more gracefully when out of memory.
549 erealloc(void *ptr, size_t size)
553 p = realloc(ptr, size);
560 estrdup(const char *str)
571 * Print a message including an optional jail name.
574 jail_note(const struct cfjail *j, const char *fmt, ...)
582 len = vsnprintf(NULL, 0, fmt, tap);
584 cs = alloca(len + 1);
585 (void)vsnprintf(cs, len + 1, fmt, ap);
588 printf("%s: %s", j->name, cs);
594 * Print a warning message including an optional jail name.
597 jail_warnx(const struct cfjail *j, const char *fmt, ...)
605 len = vsnprintf(NULL, 0, fmt, tap);
607 cs = alloca(len + 1);
608 (void)vsnprintf(cs, len + 1, fmt, ap);
611 warnx("%s: %s", j->name, cs);
620 create_jail(struct cfjail *j)
622 struct iovec jiov[4];
624 struct jailparam *jp, *setparams, *setparams2, *sjp;
626 int dopersist, ns, jid, dying, didfail;
629 * Check the jail's path, with a better error message than jail_set
632 if ((path = string_param(j->intparams[KP_PATH]))) {
633 if (j->name != NULL && path[0] != '/') {
634 jail_warnx(j, "path %s: not an absolute pathname",
638 if (stat(path, &st) < 0) {
639 jail_warnx(j, "path %s: %s", path, strerror(errno));
642 if (!S_ISDIR(st.st_mode)) {
643 jail_warnx(j, "path %s: %s", path, strerror(ENOTDIR));
649 * Copy all the parameters, except that "persist" is always set when
650 * there are commands to run later.
652 dopersist = !bool_param(j->intparams[KP_PERSIST]) &&
653 (j->intparams[IP_EXEC_START] || j->intparams[IP_COMMAND] ||
654 j->intparams[IP_EXEC_POSTSTART]);
656 alloca((j->njp + dopersist) * sizeof(struct jailparam));
657 if (dopersist && jailparam_init(sjp++, "persist") < 0) {
658 jail_warnx(j, "%s", jail_errmsg);
661 for (jp = j->jp; jp < j->jp + j->njp; jp++)
662 if (!dopersist || !equalopts(jp->jp_name, "persist"))
664 ns = sjp - setparams;
667 j->jid = jailparam_set_note(j, setparams, ns, JAIL_CREATE);
668 if (j->jid < 0 && errno == EEXIST &&
669 bool_param(j->intparams[IP_ALLOW_DYING]) &&
670 int_param(j->intparams[KP_JID], &jid) && jid != 0) {
672 * The jail already exists, but may be dying.
673 * Make sure it is, in which case an update is appropriate.
675 jiov[0].iov_base = __DECONST(char *, "jid");
676 jiov[0].iov_len = sizeof("jid");
677 jiov[1].iov_base = &jid;
678 jiov[1].iov_len = sizeof(jid);
679 jiov[2].iov_base = __DECONST(char *, "dying");
680 jiov[2].iov_len = sizeof("dying");
681 jiov[3].iov_base = &dying;
682 jiov[3].iov_len = sizeof(dying);
683 if (jail_get(jiov, 4, JAIL_DYING) < 0) {
685 * It could be that the jail just barely finished
686 * dying, or it could be that the jid never existed
687 * but the name does. In either case, another try
688 * at creating the jail should do the right thing.
691 j->jid = jailparam_set_note(j, setparams, ns,
695 if (rdtun_params(j, 1) < 0) {
699 sjp = setparams2 = alloca((j->njp + dopersist) *
700 sizeof(struct jailparam));
701 for (jp = setparams; jp < setparams + ns; jp++)
703 !strcmp(jp->jp_name, "jid"))
705 j->jid = jailparam_set_note(j, setparams2,
706 sjp - setparams2, JAIL_UPDATE | JAIL_DYING);
708 * Again, perhaps the jail just finished dying.
710 if (j->jid < 0 && errno == ENOENT)
711 j->jid = jailparam_set_note(j,
712 setparams, ns, JAIL_CREATE);
716 if (j->jid < 0 && !didfail) {
717 jail_warnx(j, "%s", jail_errmsg);
721 jailparam_free(setparams, 1);
723 j->flags |= JF_PERSIST;
729 * Remove a temporarily set "persist" parameter.
732 clear_persist(struct cfjail *j)
734 struct iovec jiov[4];
737 if (!(j->flags & JF_PERSIST))
739 j->flags &= ~JF_PERSIST;
740 jiov[0].iov_base = __DECONST(char *, "jid");
741 jiov[0].iov_len = sizeof("jid");
742 jiov[1].iov_base = &j->jid;
743 jiov[1].iov_len = sizeof(j->jid);
744 jiov[2].iov_base = __DECONST(char *, "nopersist");
745 jiov[2].iov_len = sizeof("nopersist");
746 jiov[3].iov_base = NULL;
748 jid = jail_set(jiov, 4, JAIL_UPDATE);
750 jail_note(j, "jail_set(JAIL_UPDATE) jid=%d nopersist%s%s\n",
751 j->jid, jid < 0 ? ": " : "",
752 jid < 0 ? strerror(errno) : "");
756 * Set a jail's parameters.
759 update_jail(struct cfjail *j)
761 struct jailparam *jp, *setparams, *sjp;
765 for (jp = j->jp; jp < j->jp + j->njp; jp++)
770 sjp = setparams = alloca(++ns * sizeof(struct jailparam));
771 if (jailparam_init(sjp, "jid") < 0 ||
772 jailparam_import_raw(sjp, &j->jid, sizeof j->jid) < 0) {
773 jail_warnx(j, "%s", jail_errmsg);
777 for (jp = j->jp; jp < j->jp + j->njp; jp++)
781 jid = jailparam_set_note(j, setparams, ns,
782 bool_param(j->intparams[IP_ALLOW_DYING])
783 ? JAIL_UPDATE | JAIL_DYING : JAIL_UPDATE);
785 jail_warnx(j, "%s", jail_errmsg);
788 jailparam_free(setparams, 1);
793 * Return if a jail set would change any create-only parameters.
796 rdtun_params(struct cfjail *j, int dofail)
798 struct jailparam *jp, *rtparams, *rtjp;
801 if (j->flags & JF_RDTUN)
803 j->flags |= JF_RDTUN;
805 for (jp = j->jp; jp < j->jp + j->njp; jp++)
806 if (JP_RDTUN(jp) && strcmp(jp->jp_name, "jid"))
810 rtjp = rtparams = alloca(++nrt * sizeof(struct jailparam));
811 if (jailparam_init(rtjp, "jid") < 0 ||
812 jailparam_import_raw(rtjp, &j->jid, sizeof j->jid) < 0) {
813 jail_warnx(j, "%s", jail_errmsg);
816 for (jp = j->jp; jp < j->jp + j->njp; jp++)
817 if (JP_RDTUN(jp) && strcmp(jp->jp_name, "jid")) {
819 rtjp->jp_value = NULL;
822 if (jailparam_get(rtparams, nrt,
823 bool_param(j->intparams[IP_ALLOW_DYING]) ? JAIL_DYING : 0) > 0) {
825 for (jp = j->jp; rtjp < rtparams + nrt; jp++) {
826 if (JP_RDTUN(jp) && strcmp(jp->jp_name, "jid")) {
827 if (!((jp->jp_flags & (JP_BOOL | JP_NOBOOL)) &&
828 jp->jp_valuelen == 0 &&
829 *(int *)jp->jp_value) &&
830 !(rtjp->jp_valuelen == jp->jp_valuelen &&
831 !((jp->jp_ctltype & CTLTYPE) ==
832 CTLTYPE_STRING ? strncmp(rtjp->jp_value,
833 jp->jp_value, jp->jp_valuelen) :
834 memcmp(rtjp->jp_value, jp->jp_value,
835 jp->jp_valuelen)))) {
837 jail_warnx(j, "%s cannot be "
838 "changed after creation",
850 for (rtjp = rtparams + 1; rtjp < rtparams + nrt; rtjp++)
851 rtjp->jp_name = NULL;
852 jailparam_free(rtparams, nrt);
857 * Get the jail's jid if it is running.
860 running_jid(struct cfjail *j, int dflag)
862 struct iovec jiov[2];
867 if ((pval = string_param(j->intparams[KP_JID]))) {
868 if (!(jid = strtol(pval, &ep, 10)) || *ep) {
872 jiov[0].iov_base = __DECONST(char *, "jid");
873 jiov[0].iov_len = sizeof("jid");
874 jiov[1].iov_base = &jid;
875 jiov[1].iov_len = sizeof(jid);
876 } else if ((pval = string_param(j->intparams[KP_NAME]))) {
877 jiov[0].iov_base = __DECONST(char *, "name");
878 jiov[0].iov_len = sizeof("name");
879 jiov[1].iov_len = strlen(pval) + 1;
880 jiov[1].iov_base = alloca(jiov[1].iov_len);
881 strcpy(jiov[1].iov_base, pval);
886 j->jid = jail_get(jiov, 2, dflag ? JAIL_DYING : 0);
890 jail_quoted_warnx(const struct cfjail *j, const char *name_msg,
891 const char *noname_msg)
895 if ((pval = j->name) || (pval = string_param(j->intparams[KP_JID])) ||
896 (pval = string_param(j->intparams[KP_NAME])))
897 warnx("\"%s\" %s", pval, name_msg);
899 warnx("%s", noname_msg);
903 * Set jail parameters and possibly print them out.
906 jailparam_set_note(const struct cfjail *j, struct jailparam *jp, unsigned njp,
913 jid = jailparam_set(jp, njp, flags);
915 jail_note(j, "jail_set(%s%s)",
916 (flags & (JAIL_CREATE | JAIL_UPDATE)) == JAIL_CREATE
917 ? "JAIL_CREATE" : "JAIL_UPDATE",
918 (flags & JAIL_DYING) ? " | JAIL_DYING" : "");
919 for (i = 0; i < njp; i++) {
920 printf(" %s", jp[i].jp_name);
921 if (jp[i].jp_value == NULL)
924 value = jailparam_export(jp + i);
926 err(1, "jailparam_export");
927 quoted_print(stdout, value);
931 printf(": %s", strerror(errno));
938 * Print a jail record.
941 print_jail(FILE *fp, struct cfjail *j, int oldcl, int running)
948 fprintf(fp, "%d%s", j->jid, separator);
949 print_param(fp, j->intparams[KP_PATH], ',', 0);
950 fputs(separator, fp);
951 print_param(fp, j->intparams[KP_HOST_HOSTNAME], ',', 0);
952 fputs(separator, fp);
954 print_param(fp, j->intparams[KP_IP4_ADDR], ',', 0);
956 if (j->intparams[KP_IP4_ADDR] &&
957 !TAILQ_EMPTY(&j->intparams[KP_IP4_ADDR]->val) &&
958 j->intparams[KP_IP6_ADDR] &&
959 !TAILQ_EMPTY(&j->intparams[KP_IP6_ADDR]->val))
964 print_param(fp, j->intparams[KP_IP6_ADDR], ',', 0);
966 fputs(separator, fp);
967 print_param(fp, j->intparams[IP_COMMAND], ' ', 0);
971 fprintf(fp, "jid=%d", j->jid);
974 TAILQ_FOREACH(p, &j->params, tq)
975 if (strcmp(p->name, "jid")) {
977 fputs(separator, fp);
980 print_param(fp, p, ',', 1);
987 * Exhibit list of all configured non-wildcard jails
994 TAILQ_FOREACH(j, &cfjails, tq)
995 print_jail(stdout, j, 0, 0);
999 * Print a parameter value, or a name=value pair.
1002 print_param(FILE *fp, const struct cfparam *p, int sep, int doname)
1004 const struct cfstring *s, *ts;
1008 if (p == NULL || TAILQ_EMPTY(&p->val))
1012 TAILQ_FOREACH_SAFE(s, &p->val, tq, ts) {
1013 quoted_print(fp, s->s);
1020 * Print a string with quotes around spaces.
1023 quoted_print(FILE *fp, char *str)
1029 : strchr(p, '\'') ? '"'
1030 : strchr(p, '"') ? '\''
1031 : strchr(p, ' ') || strchr(p, '\t') ? '"'
1035 while ((c = *p++)) {
1036 if (c == '\\' || c == qc)
1048 (void)fprintf(stderr,
1049 "usage: jail [-dhilqv] [-J jid_file] [-u username] [-U username]\n"
1050 " -[cmr] param=value ... [command=command ...]\n"
1051 " jail [-dqv] [-f file] [-e separator] -[cmr] [jail]\n"
1052 " jail [-qv] [-f file] -[rR] ['*' | jail ...]\n"
1053 " jail [-dhilqv] [-J jid_file] [-u username] [-U username]\n"
1054 " [-n jailname] [-s securelevel]\n"
1055 " path hostname [ip[,...]] command ...\n");