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 #if defined(INET) || defined(INET6)
144 #if defined(INET) && defined(INET6)
145 struct in6_addr addr6;
154 while ((ch = getopt(argc, argv, "cdf:hiJ:lmn:p:qrRs:u:U:v")) != -1) {
166 #if defined(INET) || defined(INET6)
167 add_param(NULL, NULL, IP_IP_HOSTNAME, NULL);
179 add_param(NULL, NULL, IP_EXEC_CLEAN, NULL);
186 add_param(NULL, NULL, KP_NAME, optarg);
190 paralimit = strtol(optarg, NULL, 10);
205 add_param(NULL, NULL, KP_SECURELEVEL, optarg);
209 add_param(NULL, NULL, IP_EXEC_JAIL_USER, optarg);
210 add_param(NULL, NULL, IP_EXEC_SYSTEM_JAIL_USER, NULL);
214 add_param(NULL, NULL, IP_EXEC_JAIL_USER, optarg);
215 add_param(NULL, NULL, IP_EXEC_SYSTEM_JAIL_USER,
229 /* Find out which of the four command line styles this is. */
232 /* Old-style command line with four fixed parameters */
233 if (argc < 4 || argv[0][0] != '/')
238 add_param(NULL, NULL, KP_PATH, argv[0]);
239 add_param(NULL, NULL, KP_HOST_HOSTNAME, argv[1]);
240 #if defined(INET) || defined(INET6)
241 if (argv[2][0] != '\0') {
242 for (cs = argv[2];; cs = ncs + 1) {
243 ncs = strchr(cs, ',');
246 add_param(NULL, NULL,
247 #if defined(INET) && defined(INET6)
248 inet_pton(AF_INET6, cs, &addr6) == 1
249 ? KP_IP6_ADDR : KP_IP4_ADDR,
261 for (i = 3; i < argc; i++)
262 add_param(NULL, NULL, IP_COMMAND, argv[i]);
263 /* Emulate the defaults from security.jail.* sysctls. */
264 sysvallen = sizeof(sysval);
265 if (sysctlbyname("security.jail.jailed", &sysval, &sysvallen,
266 NULL, 0) == 0 && sysval == 0) {
267 for (pi = 0; pi < sizeof(perm_sysctl) /
268 sizeof(perm_sysctl[0]); pi++) {
269 sysvallen = sizeof(sysval);
270 if (sysctlbyname(perm_sysctl[pi].name,
271 &sysval, &sysvallen, NULL, 0) == 0)
272 add_param(NULL, NULL,
273 perm_sysctl[pi].ipnum,
279 } else if (op == JF_STOP) {
280 /* Jail remove, perhaps using the config file */
281 if (!docf || argc == 0)
284 for (i = 0; i < argc; i++)
285 if (strchr(argv[i], '='))
287 if ((docf = !Rflag &&
288 (!strcmp(cfname, "-") || stat(cfname, &st) == 0)))
290 note_remove = docf || argc > 1 || wild_jail_name(argv[0]);
291 } else if (argc > 1 || (argc == 1 && strchr(argv[0], '='))) {
292 /* Single jail specified on the command line */
296 for (i = 0; i < argc; i++) {
297 if (!strncmp(argv[i], "command", 7) &&
298 (argv[i][7] == '\0' || argv[i][7] == '=')) {
299 if (argv[i][7] == '=')
300 add_param(NULL, NULL, IP_COMMAND,
302 for (i++; i < argc; i++)
303 add_param(NULL, NULL, IP_COMMAND,
307 else if (!strncmp(argv[i], "ip4.addr=", 9)) {
308 for (cs = argv[i] + 9;; cs = ncs + 1) {
309 ncs = strchr(cs, ',');
312 add_param(NULL, NULL, KP_IP4_ADDR, cs);
319 else if (!strncmp(argv[i], "ip6.addr=", 9)) {
320 for (cs = argv[i] + 9;; cs = ncs + 1) {
321 ncs = strchr(cs, ',');
324 add_param(NULL, NULL, KP_IP6_ADDR, cs);
331 add_param(NULL, NULL, 0, argv[i]);
334 /* From the config file, perhaps with a specified jail */
340 /* Find out which jails will be run. */
344 for (i = 0; i < argc; i++)
345 if (start_state(argv[i], docf, op, Rflag) < 0)
348 if (start_state(argv[0], docf, op, 0) < 0)
353 if (JidFile != NULL) {
354 jfp = fopen(JidFile, "w");
356 err(1, "open %s", JidFile);
362 * The main loop: Get an available jail and perform the required
363 * operation on it. When that is done, the jail may be finished,
364 * or it may go back for the next step.
366 while ((j = next_jail()))
368 if (j->flags & JF_FAILED) {
370 if (j->comparam == NULL) {
375 if (!(j->flags & JF_PARAMS))
377 j->flags |= JF_PARAMS;
379 add_param(j, NULL, IP_ALLOW_DYING, NULL);
380 if (check_intparams(j) < 0)
382 if ((j->flags & (JF_START | JF_SET)) &&
383 import_params(j) < 0)
388 (j->flags & (JF_SET | JF_DEPEND)) == JF_SET
389 ? dflag || bool_param(j->intparams[IP_ALLOW_DYING])
391 if (finish_command(j))
394 switch (j->flags & JF_OP_MASK) {
396 * These operations just turn into a different op
397 * depending on the jail's current status.
400 j->flags = j->jid < 0 ? JF_START : JF_SET;
404 jail_quoted_warnx(j, "not found",
405 "no jail specified");
409 j->flags = rdtun_params(j, 0) ? JF_RESTART : JF_SET;
410 if (j->flags == JF_RESTART)
413 case JF_START_SET_RESTART:
414 j->flags = j->jid < 0 ? JF_START
415 : rdtun_params(j, 0) ? JF_RESTART : JF_SET;
416 if (j->flags == JF_RESTART)
420 switch (j->flags & JF_OP_MASK) {
422 if (j->comparam == NULL) {
424 !(j->flags & (JF_DEPEND | JF_WILD))) {
425 jail_quoted_warnx(j, "already exists",
433 goto jail_create_done;
434 j->comparam = startcommands;
442 print_jail(jfp, j, oldcl);
447 if (j->jid < 0 && !(j->flags & JF_DEPEND)) {
448 jail_quoted_warnx(j, "not found",
449 "no jail specified");
455 if (!(j->flags & JF_DEPEND)) {
456 if (rdtun_params(j, 1) < 0 ||
459 if (verbose >= 0 && (j->name || verbose > 0))
460 jail_note(j, "updated\n");
467 if (j->comparam == NULL) {
471 if (!(j->flags & (JF_DEPEND|JF_WILD))) {
477 goto jail_remove_done;
479 j->comparam = stopcommands;
481 } else if ((j->flags & JF_FAILED) && j->jid > 0)
482 goto jail_remove_done;
487 if ((j->flags & (JF_START | JF_FAILED)) == JF_START) {
489 j->flags &= ~JF_STOP;
491 requeue(j, j->ndeps ? &depend : &ready);
503 * Mark a jail's failure for future handling.
506 failed(struct cfjail *j)
508 j->flags |= JF_FAILED;
509 TAILQ_REMOVE(j->queue, j, tq);
510 TAILQ_INSERT_HEAD(&ready, j, tq);
515 * Exit slightly more gracefully when out of memory.
529 erealloc(void *ptr, size_t size)
533 p = realloc(ptr, size);
540 estrdup(const char *str)
551 * Print a message including an optional jail name.
554 jail_note(const struct cfjail *j, const char *fmt, ...)
562 len = vsnprintf(NULL, 0, fmt, tap);
564 cs = alloca(len + 1);
565 (void)vsnprintf(cs, len + 1, fmt, ap);
568 printf("%s: %s", j->name, cs);
574 * Print a warning message including an optional jail name.
577 jail_warnx(const struct cfjail *j, const char *fmt, ...)
585 len = vsnprintf(NULL, 0, fmt, tap);
587 cs = alloca(len + 1);
588 (void)vsnprintf(cs, len + 1, fmt, ap);
591 warnx("%s: %s", j->name, cs);
600 create_jail(struct cfjail *j)
602 struct iovec jiov[4];
604 struct jailparam *jp, *setparams, *setparams2, *sjp;
606 int dopersist, ns, jid, dying, didfail;
609 * Check the jail's path, with a better error message than jail_set
612 if ((path = string_param(j->intparams[KP_PATH]))) {
613 if (j->name != NULL && path[0] != '/') {
614 jail_warnx(j, "path %s: not an absolute pathname",
618 if (stat(path, &st) < 0) {
619 jail_warnx(j, "path %s: %s", path, strerror(errno));
622 if (!S_ISDIR(st.st_mode)) {
623 jail_warnx(j, "path %s: %s", path, strerror(ENOTDIR));
629 * Copy all the parameters, except that "persist" is always set when
630 * there are commands to run later.
632 dopersist = !bool_param(j->intparams[KP_PERSIST]) &&
633 (j->intparams[IP_EXEC_START] || j->intparams[IP_COMMAND] ||
634 j->intparams[IP_EXEC_POSTSTART]);
636 alloca((j->njp + dopersist) * sizeof(struct jailparam));
637 if (dopersist && jailparam_init(sjp++, "persist") < 0) {
638 jail_warnx(j, "%s", jail_errmsg);
641 for (jp = j->jp; jp < j->jp + j->njp; jp++)
642 if (!dopersist || !equalopts(jp->jp_name, "persist"))
644 ns = sjp - setparams;
647 j->jid = jailparam_set_note(j, setparams, ns, JAIL_CREATE);
648 if (j->jid < 0 && errno == EEXIST &&
649 bool_param(j->intparams[IP_ALLOW_DYING]) &&
650 int_param(j->intparams[KP_JID], &jid) && jid != 0) {
652 * The jail already exists, but may be dying.
653 * Make sure it is, in which case an update is appropriate.
655 jiov[0].iov_base = __DECONST(char *, "jid");
656 jiov[0].iov_len = sizeof("jid");
657 jiov[1].iov_base = &jid;
658 jiov[1].iov_len = sizeof(jid);
659 jiov[2].iov_base = __DECONST(char *, "dying");
660 jiov[2].iov_len = sizeof("dying");
661 jiov[3].iov_base = &dying;
662 jiov[3].iov_len = sizeof(dying);
663 if (jail_get(jiov, 4, JAIL_DYING) < 0) {
665 * It could be that the jail just barely finished
666 * dying, or it could be that the jid never existed
667 * but the name does. In either case, another try
668 * at creating the jail should do the right thing.
671 j->jid = jailparam_set_note(j, setparams, ns,
675 if (rdtun_params(j, 1) < 0) {
679 sjp = setparams2 = alloca((j->njp + dopersist) *
680 sizeof(struct jailparam));
681 for (jp = setparams; jp < setparams + ns; jp++)
683 !strcmp(jp->jp_name, "jid"))
685 j->jid = jailparam_set_note(j, setparams2,
686 sjp - setparams2, JAIL_UPDATE | JAIL_DYING);
688 * Again, perhaps the jail just finished dying.
690 if (j->jid < 0 && errno == ENOENT)
691 j->jid = jailparam_set_note(j,
692 setparams, ns, JAIL_CREATE);
696 if (j->jid < 0 && !didfail) {
697 jail_warnx(j, "%s", jail_errmsg);
701 jailparam_free(setparams, 1);
703 j->flags |= JF_PERSIST;
709 * Remove a temporarily set "persist" parameter.
712 clear_persist(struct cfjail *j)
714 struct iovec jiov[4];
717 if (!(j->flags & JF_PERSIST))
719 j->flags &= ~JF_PERSIST;
720 jiov[0].iov_base = __DECONST(char *, "jid");
721 jiov[0].iov_len = sizeof("jid");
722 jiov[1].iov_base = &j->jid;
723 jiov[1].iov_len = sizeof(j->jid);
724 jiov[2].iov_base = __DECONST(char *, "nopersist");
725 jiov[2].iov_len = sizeof("nopersist");
726 jiov[3].iov_base = NULL;
728 jid = jail_set(jiov, 4, JAIL_UPDATE);
730 jail_note(j, "jail_set(JAIL_UPDATE) jid=%d nopersist%s%s\n",
731 j->jid, jid < 0 ? ": " : "",
732 jid < 0 ? strerror(errno) : "");
736 * Set a jail's parameters.
739 update_jail(struct cfjail *j)
741 struct jailparam *jp, *setparams, *sjp;
745 for (jp = j->jp; jp < j->jp + j->njp; jp++)
750 sjp = setparams = alloca(++ns * sizeof(struct jailparam));
751 if (jailparam_init(sjp, "jid") < 0 ||
752 jailparam_import_raw(sjp, &j->jid, sizeof j->jid) < 0) {
753 jail_warnx(j, "%s", jail_errmsg);
757 for (jp = j->jp; jp < j->jp + j->njp; jp++)
761 jid = jailparam_set_note(j, setparams, ns,
762 bool_param(j->intparams[IP_ALLOW_DYING])
763 ? JAIL_UPDATE | JAIL_DYING : JAIL_UPDATE);
765 jail_warnx(j, "%s", jail_errmsg);
768 jailparam_free(setparams, 1);
773 * Return if a jail set would change any create-only parameters.
776 rdtun_params(struct cfjail *j, int dofail)
778 struct jailparam *jp, *rtparams, *rtjp;
781 if (j->flags & JF_RDTUN)
783 j->flags |= JF_RDTUN;
785 for (jp = j->jp; jp < j->jp + j->njp; jp++)
786 if (JP_RDTUN(jp) && strcmp(jp->jp_name, "jid"))
790 rtjp = rtparams = alloca(++nrt * sizeof(struct jailparam));
791 if (jailparam_init(rtjp, "jid") < 0 ||
792 jailparam_import_raw(rtjp, &j->jid, sizeof j->jid) < 0) {
793 jail_warnx(j, "%s", jail_errmsg);
796 for (jp = j->jp; jp < j->jp + j->njp; jp++)
797 if (JP_RDTUN(jp) && strcmp(jp->jp_name, "jid")) {
799 rtjp->jp_value = NULL;
802 if (jailparam_get(rtparams, nrt,
803 bool_param(j->intparams[IP_ALLOW_DYING]) ? JAIL_DYING : 0) > 0) {
805 for (jp = j->jp; rtjp < rtparams + nrt; jp++) {
806 if (JP_RDTUN(jp) && strcmp(jp->jp_name, "jid")) {
807 if (!((jp->jp_flags & (JP_BOOL | JP_NOBOOL)) &&
808 jp->jp_valuelen == 0 &&
809 *(int *)jp->jp_value) &&
810 !(rtjp->jp_valuelen == jp->jp_valuelen &&
811 !((jp->jp_ctltype & CTLTYPE) ==
812 CTLTYPE_STRING ? strncmp(rtjp->jp_value,
813 jp->jp_value, jp->jp_valuelen) :
814 memcmp(rtjp->jp_value, jp->jp_value,
815 jp->jp_valuelen)))) {
817 jail_warnx(j, "%s cannot be "
818 "changed after creation",
830 for (rtjp = rtparams + 1; rtjp < rtparams + nrt; rtjp++)
831 rtjp->jp_name = NULL;
832 jailparam_free(rtparams, nrt);
837 * Get the jail's jid if it is running.
840 running_jid(struct cfjail *j, int dflag)
842 struct iovec jiov[2];
847 if ((pval = string_param(j->intparams[KP_JID]))) {
848 if (!(jid = strtol(pval, &ep, 10)) || *ep) {
852 jiov[0].iov_base = __DECONST(char *, "jid");
853 jiov[0].iov_len = sizeof("jid");
854 jiov[1].iov_base = &jid;
855 jiov[1].iov_len = sizeof(jid);
856 } else if ((pval = string_param(j->intparams[KP_NAME]))) {
857 jiov[0].iov_base = __DECONST(char *, "name");
858 jiov[0].iov_len = sizeof("name");
859 jiov[1].iov_len = strlen(pval) + 1;
860 jiov[1].iov_base = alloca(jiov[1].iov_len);
861 strcpy(jiov[1].iov_base, pval);
866 j->jid = jail_get(jiov, 2, dflag ? JAIL_DYING : 0);
870 jail_quoted_warnx(const struct cfjail *j, const char *name_msg,
871 const char *noname_msg)
875 if ((pval = j->name) || (pval = string_param(j->intparams[KP_JID])) ||
876 (pval = string_param(j->intparams[KP_NAME])))
877 warnx("\"%s\" %s", pval, name_msg);
879 warnx("%s", noname_msg);
883 * Set jail parameters and possibly print them out.
886 jailparam_set_note(const struct cfjail *j, struct jailparam *jp, unsigned njp,
893 jid = jailparam_set(jp, njp, flags);
895 jail_note(j, "jail_set(%s%s)",
896 (flags & (JAIL_CREATE | JAIL_UPDATE)) == JAIL_CREATE
897 ? "JAIL_CREATE" : "JAIL_UPDATE",
898 (flags & JAIL_DYING) ? " | JAIL_DYING" : "");
899 for (i = 0; i < njp; i++) {
900 printf(" %s", jp[i].jp_name);
901 if (jp[i].jp_value == NULL)
904 value = jailparam_export(jp + i);
906 err(1, "jailparam_export");
907 quoted_print(stdout, value);
911 printf(": %s", strerror(errno));
918 * Print a jail record.
921 print_jail(FILE *fp, struct cfjail *j, int oldcl)
926 fprintf(fp, "%d\t", j->jid);
927 print_param(fp, j->intparams[KP_PATH], ',', 0);
929 print_param(fp, j->intparams[KP_HOST_HOSTNAME], ',', 0);
932 print_param(fp, j->intparams[KP_IP4_ADDR], ',', 0);
934 if (j->intparams[KP_IP4_ADDR] &&
935 !TAILQ_EMPTY(&j->intparams[KP_IP4_ADDR]->val) &&
936 j->intparams[KP_IP6_ADDR] &&
937 !TAILQ_EMPTY(&j->intparams[KP_IP6_ADDR]->val))
942 print_param(fp, j->intparams[KP_IP6_ADDR], ',', 0);
945 print_param(fp, j->intparams[IP_COMMAND], ' ', 0);
947 fprintf(fp, "jid=%d", j->jid);
948 TAILQ_FOREACH(p, &j->params, tq)
949 if (strcmp(p->name, "jid")) {
951 print_param(fp, p, ',', 1);
958 * Print a parameter value, or a name=value pair.
961 print_param(FILE *fp, const struct cfparam *p, int sep, int doname)
963 const struct cfstring *s, *ts;
967 if (p == NULL || TAILQ_EMPTY(&p->val))
971 TAILQ_FOREACH_SAFE(s, &p->val, tq, ts) {
972 quoted_print(fp, s->s);
979 * Print a string with quotes around spaces.
982 quoted_print(FILE *fp, char *str)
988 : strchr(p, '\'') ? '"'
989 : strchr(p, '"') ? '\''
990 : strchr(p, ' ') || strchr(p, '\t') ? '"'
995 if (c == '\\' || c == qc)
1007 (void)fprintf(stderr,
1008 "usage: jail [-dhilqv] [-J jid_file] [-u username] [-U username]\n"
1009 " -[cmr] param=value ... [command=command ...]\n"
1010 " jail [-dqv] [-f file] -[cmr] [jail]\n"
1011 " jail [-qv] [-f file] -[rR] ['*' | jail ...]\n"
1012 " jail [-dhilqv] [-J jid_file] [-u username] [-U username]\n"
1013 " [-n jailname] [-s securelevel]\n"
1014 " path hostname [ip[,...]] command ...\n");