2 * Copyright (c) 1999 Poul-Henning Kamp.
3 * Copyright (c) 2009-2012 James Gritton
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
9 * 1. Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
11 * 2. Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in the
13 * documentation and/or other materials provided with the distribution.
15 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
16 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
17 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
18 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
19 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
20 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
21 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
22 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
23 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
24 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
28 #include <sys/cdefs.h>
29 __FBSDID("$FreeBSD$");
31 #include <sys/types.h>
33 #include <sys/socket.h>
34 #include <sys/sysctl.h>
36 #include <arpa/inet.h>
37 #include <netinet/in.h>
49 #define JP_RDTUN(jp) (((jp)->jp_ctltype & CTLFLAG_RDTUN) == CTLFLAG_RDTUN)
62 static void clear_persist(struct cfjail *j);
63 static int update_jail(struct cfjail *j);
64 static int rdtun_params(struct cfjail *j, int dofail);
65 static void running_jid(struct cfjail *j, int dflag);
66 static void jail_quoted_warnx(const struct cfjail *j, const char *name_msg,
67 const char *noname_msg);
68 static int jailparam_set_note(const struct cfjail *j, struct jailparam *jp,
69 unsigned njp, int flags);
70 static void print_jail(FILE *fp, struct cfjail *j, int oldcl);
71 static void print_param(FILE *fp, const struct cfparam *p, int sep, int doname);
72 static void quoted_print(FILE *fp, char *str);
73 static void usage(void);
75 static struct permspec perm_sysctl[] = {
76 { "security.jail.set_hostname_allowed", KP_ALLOW_SET_HOSTNAME, 0 },
77 { "security.jail.sysvipc_allowed", KP_ALLOW_SYSVIPC, 0 },
78 { "security.jail.allow_raw_sockets", KP_ALLOW_RAW_SOCKETS, 0 },
79 { "security.jail.chflags_allowed", KP_ALLOW_CHFLAGS, 0 },
80 { "security.jail.mount_allowed", KP_ALLOW_MOUNT, 0 },
81 { "security.jail.socket_unixiproute_only", KP_ALLOW_SOCKET_AF, 1 },
84 static const enum intparam startcommands[] = {
104 static const enum intparam stopcommands[] = {
112 IP__MOUNT_FROM_FSTAB,
124 main(int argc, char **argv)
132 int ch, docf, error, i, oldcl, sysval;
134 char enforce_statfs[4];
135 #if defined(INET) || defined(INET6)
138 #if defined(INET) && defined(INET6)
139 struct in6_addr addr6;
148 while ((ch = getopt(argc, argv, "cdf:hiJ:lmn:p:qrRs:u:U:v")) != -1) {
160 #if defined(INET) || defined(INET6)
161 add_param(NULL, NULL, IP_IP_HOSTNAME, NULL);
173 add_param(NULL, NULL, IP_EXEC_CLEAN, NULL);
180 add_param(NULL, NULL, KP_NAME, optarg);
184 paralimit = strtol(optarg, NULL, 10);
199 add_param(NULL, NULL, KP_SECURELEVEL, optarg);
203 add_param(NULL, NULL, IP_EXEC_JAIL_USER, optarg);
204 add_param(NULL, NULL, IP_EXEC_SYSTEM_JAIL_USER, NULL);
208 add_param(NULL, NULL, IP_EXEC_JAIL_USER, optarg);
209 add_param(NULL, NULL, IP_EXEC_SYSTEM_JAIL_USER,
223 /* Find out which of the four command line styles this is. */
226 /* Old-style command line with four fixed parameters */
227 if (argc < 4 || argv[0][0] != '/')
232 add_param(NULL, NULL, KP_PATH, argv[0]);
233 add_param(NULL, NULL, KP_HOST_HOSTNAME, argv[1]);
234 #if defined(INET) || defined(INET6)
235 if (argv[2][0] != '\0') {
236 for (cs = argv[2];; cs = ncs + 1) {
237 ncs = strchr(cs, ',');
240 add_param(NULL, NULL,
241 #if defined(INET) && defined(INET6)
242 inet_pton(AF_INET6, cs, &addr6) == 1
243 ? KP_IP6_ADDR : KP_IP4_ADDR,
255 for (i = 3; i < argc; i++)
256 add_param(NULL, NULL, IP_COMMAND, argv[i]);
257 /* Emulate the defaults from security.jail.* sysctls. */
258 sysvallen = sizeof(sysval);
259 if (sysctlbyname("security.jail.jailed", &sysval, &sysvallen,
260 NULL, 0) == 0 && sysval == 0) {
261 for (pi = 0; pi < sizeof(perm_sysctl) /
262 sizeof(perm_sysctl[0]); pi++) {
263 sysvallen = sizeof(sysval);
264 if (sysctlbyname(perm_sysctl[pi].name,
265 &sysval, &sysvallen, NULL, 0) == 0)
266 add_param(NULL, NULL,
267 perm_sysctl[pi].ipnum,
272 sysvallen = sizeof(sysval);
273 if (sysctlbyname("security.jail.enforce_statfs",
274 &sysval, &sysvallen, NULL, 0) == 0) {
275 snprintf(enforce_statfs,
276 sizeof(enforce_statfs), "%d", sysval);
277 add_param(NULL, NULL, KP_ENFORCE_STATFS,
281 } else if (op == JF_STOP) {
282 /* Jail remove, perhaps using the config file */
283 if (!docf || argc == 0)
286 for (i = 0; i < argc; i++)
287 if (strchr(argv[i], '='))
289 if ((docf = !Rflag &&
290 (!strcmp(cfname, "-") || stat(cfname, &st) == 0)))
292 note_remove = docf || argc > 1 || wild_jail_name(argv[0]);
293 } else if (argc > 1 || (argc == 1 && strchr(argv[0], '='))) {
294 /* Single jail specified on the command line */
298 for (i = 0; i < argc; i++) {
299 if (!strncmp(argv[i], "command", 7) &&
300 (argv[i][7] == '\0' || argv[i][7] == '=')) {
301 if (argv[i][7] == '=')
302 add_param(NULL, NULL, IP_COMMAND,
304 for (i++; i < argc; i++)
305 add_param(NULL, NULL, IP_COMMAND,
309 add_param(NULL, NULL, 0, argv[i]);
312 /* From the config file, perhaps with a specified jail */
318 /* Find out which jails will be run. */
322 for (i = 0; i < argc; i++)
323 if (start_state(argv[i], docf, op, Rflag) < 0)
326 if (start_state(argv[0], docf, op, 0) < 0)
331 if (JidFile != NULL) {
332 jfp = fopen(JidFile, "w");
334 err(1, "open %s", JidFile);
340 * The main loop: Get an available jail and perform the required
341 * operation on it. When that is done, the jail may be finished,
342 * or it may go back for the next step.
344 while ((j = next_jail()))
346 if (j->flags & JF_FAILED) {
348 if (j->comparam == NULL) {
353 if (!(j->flags & JF_PARAMS))
355 j->flags |= JF_PARAMS;
357 add_param(j, NULL, IP_ALLOW_DYING, NULL);
358 if (check_intparams(j) < 0)
360 if ((j->flags & (JF_START | JF_SET)) &&
361 import_params(j) < 0)
366 (j->flags & (JF_SET | JF_DEPEND)) == JF_SET
367 ? dflag || bool_param(j->intparams[IP_ALLOW_DYING])
369 if (finish_command(j))
372 switch (j->flags & JF_OP_MASK) {
374 * These operations just turn into a different op
375 * depending on the jail's current status.
378 j->flags = j->jid < 0 ? JF_START : JF_SET;
382 jail_quoted_warnx(j, "not found",
383 "no jail specified");
387 j->flags = rdtun_params(j, 0) ? JF_RESTART : JF_SET;
388 if (j->flags == JF_RESTART)
391 case JF_START_SET_RESTART:
392 j->flags = j->jid < 0 ? JF_START
393 : rdtun_params(j, 0) ? JF_RESTART : JF_SET;
394 if (j->flags == JF_RESTART)
398 switch (j->flags & JF_OP_MASK) {
400 if (j->comparam == NULL) {
402 !(j->flags & (JF_DEPEND | JF_WILD))) {
403 jail_quoted_warnx(j, "already exists",
411 goto jail_create_done;
412 j->comparam = startcommands;
420 print_jail(jfp, j, oldcl);
425 if (j->jid < 0 && !(j->flags & JF_DEPEND)) {
426 jail_quoted_warnx(j, "not found",
427 "no jail specified");
433 if (!(j->flags & JF_DEPEND)) {
434 if (rdtun_params(j, 1) < 0 ||
437 if (verbose >= 0 && (j->name || verbose > 0))
438 jail_note(j, "updated\n");
445 if (j->comparam == NULL) {
449 if (!(j->flags & (JF_DEPEND | JF_WILD))
453 goto jail_remove_done;
455 j->comparam = stopcommands;
457 } else if ((j->flags & JF_FAILED) && j->jid > 0)
458 goto jail_remove_done;
463 if ((j->flags & (JF_START | JF_FAILED)) == JF_START) {
465 j->flags &= ~JF_STOP;
467 requeue(j, j->ndeps ? &depend : &ready);
479 * Mark a jail's failure for future handling.
482 failed(struct cfjail *j)
484 j->flags |= JF_FAILED;
485 TAILQ_REMOVE(j->queue, j, tq);
486 TAILQ_INSERT_HEAD(&ready, j, tq);
491 * Exit slightly more gracefully when out of memory.
505 erealloc(void *ptr, size_t size)
509 p = realloc(ptr, size);
516 estrdup(const char *str)
527 * Print a message including an optional jail name.
530 jail_note(const struct cfjail *j, const char *fmt, ...)
538 len = vsnprintf(NULL, 0, fmt, tap);
540 cs = alloca(len + 1);
541 (void)vsnprintf(cs, len + 1, fmt, ap);
544 printf("%s: %s", j->name, cs);
550 * Print a warning message including an optional jail name.
553 jail_warnx(const struct cfjail *j, const char *fmt, ...)
561 len = vsnprintf(NULL, 0, fmt, tap);
563 cs = alloca(len + 1);
564 (void)vsnprintf(cs, len + 1, fmt, ap);
567 warnx("%s: %s", j->name, cs);
576 create_jail(struct cfjail *j)
578 struct iovec jiov[4];
580 struct jailparam *jp, *setparams, *setparams2, *sjp;
582 int dopersist, ns, jid, dying, didfail;
585 * Check the jail's path, with a better error message than jail_set
588 if ((path = string_param(j->intparams[KP_PATH]))) {
589 if (j->name != NULL && path[0] != '/') {
590 jail_warnx(j, "path %s: not an absolute pathname",
594 if (stat(path, &st) < 0) {
595 jail_warnx(j, "path %s: %s", path, strerror(errno));
598 if (!S_ISDIR(st.st_mode)) {
599 jail_warnx(j, "path %s: %s", path, strerror(ENOTDIR));
605 * Copy all the parameters, except that "persist" is always set when
606 * there are commands to run later.
608 dopersist = !bool_param(j->intparams[KP_PERSIST]) &&
609 (j->intparams[IP_EXEC_START] || j->intparams[IP_COMMAND] ||
610 j->intparams[IP_EXEC_POSTSTART]);
612 alloca((j->njp + dopersist) * sizeof(struct jailparam));
613 if (dopersist && jailparam_init(sjp++, "persist") < 0) {
614 jail_warnx(j, "%s", jail_errmsg);
617 for (jp = j->jp; jp < j->jp + j->njp; jp++)
618 if (!dopersist || !equalopts(jp->jp_name, "persist"))
620 ns = sjp - setparams;
623 j->jid = jailparam_set_note(j, setparams, ns, JAIL_CREATE);
624 if (j->jid < 0 && errno == EEXIST &&
625 bool_param(j->intparams[IP_ALLOW_DYING]) &&
626 int_param(j->intparams[KP_JID], &jid) && jid != 0) {
628 * The jail already exists, but may be dying.
629 * Make sure it is, in which case an update is appropriate.
631 *(const void **)&jiov[0].iov_base = "jid";
632 jiov[0].iov_len = sizeof("jid");
633 jiov[1].iov_base = &jid;
634 jiov[1].iov_len = sizeof(jid);
635 *(const void **)&jiov[2].iov_base = "dying";
636 jiov[2].iov_len = sizeof("dying");
637 jiov[3].iov_base = &dying;
638 jiov[3].iov_len = sizeof(dying);
639 if (jail_get(jiov, 4, JAIL_DYING) < 0) {
641 * It could be that the jail just barely finished
642 * dying, or it could be that the jid never existed
643 * but the name does. In either case, another try
644 * at creating the jail should do the right thing.
647 j->jid = jailparam_set_note(j, setparams, ns,
651 if (rdtun_params(j, 1) < 0) {
655 sjp = setparams2 = alloca((j->njp + dopersist) *
656 sizeof(struct jailparam));
657 for (jp = setparams; jp < setparams + ns; jp++)
659 !strcmp(jp->jp_name, "jid"))
661 j->jid = jailparam_set_note(j, setparams2,
662 sjp - setparams2, JAIL_UPDATE | JAIL_DYING);
664 * Again, perhaps the jail just finished dying.
666 if (j->jid < 0 && errno == ENOENT)
667 j->jid = jailparam_set_note(j,
668 setparams, ns, JAIL_CREATE);
672 if (j->jid < 0 && !didfail) {
673 jail_warnx(j, "%s", jail_errmsg);
677 jailparam_free(setparams, 1);
679 j->flags |= JF_PERSIST;
685 * Remove a temporarily set "persist" parameter.
688 clear_persist(struct cfjail *j)
690 struct iovec jiov[4];
693 if (!(j->flags & JF_PERSIST))
695 j->flags &= ~JF_PERSIST;
696 *(const void **)&jiov[0].iov_base = "jid";
697 jiov[0].iov_len = sizeof("jid");
698 jiov[1].iov_base = &j->jid;
699 jiov[1].iov_len = sizeof(j->jid);
700 *(const void **)&jiov[2].iov_base = "nopersist";
701 jiov[2].iov_len = sizeof("nopersist");
702 jiov[3].iov_base = NULL;
704 jid = jail_set(jiov, 4, JAIL_UPDATE);
706 jail_note(j, "jail_set(JAIL_UPDATE) jid=%d nopersist%s%s\n",
707 j->jid, jid < 0 ? ": " : "",
708 jid < 0 ? strerror(errno) : "");
712 * Set a jail's parameters.
715 update_jail(struct cfjail *j)
717 struct jailparam *jp, *setparams, *sjp;
721 for (jp = j->jp; jp < j->jp + j->njp; jp++)
726 sjp = setparams = alloca(++ns * sizeof(struct jailparam));
727 if (jailparam_init(sjp, "jid") < 0 ||
728 jailparam_import_raw(sjp, &j->jid, sizeof j->jid) < 0) {
729 jail_warnx(j, "%s", jail_errmsg);
733 for (jp = j->jp; jp < j->jp + j->njp; jp++)
737 jid = jailparam_set_note(j, setparams, ns,
738 bool_param(j->intparams[IP_ALLOW_DYING])
739 ? JAIL_UPDATE | JAIL_DYING : JAIL_UPDATE);
741 jail_warnx(j, "%s", jail_errmsg);
744 jailparam_free(setparams, 1);
749 * Return if a jail set would change any create-only parameters.
752 rdtun_params(struct cfjail *j, int dofail)
754 struct jailparam *jp, *rtparams, *rtjp;
757 if (j->flags & JF_RDTUN)
759 j->flags |= JF_RDTUN;
761 for (jp = j->jp; jp < j->jp + j->njp; jp++)
762 if (JP_RDTUN(jp) && strcmp(jp->jp_name, "jid"))
766 rtjp = rtparams = alloca(++nrt * sizeof(struct jailparam));
767 if (jailparam_init(rtjp, "jid") < 0 ||
768 jailparam_import_raw(rtjp, &j->jid, sizeof j->jid) < 0) {
769 jail_warnx(j, "%s", jail_errmsg);
772 for (jp = j->jp; jp < j->jp + j->njp; jp++)
773 if (JP_RDTUN(jp) && strcmp(jp->jp_name, "jid"))
776 if (jailparam_get(rtparams, nrt,
777 bool_param(j->intparams[IP_ALLOW_DYING]) ? JAIL_DYING : 0) > 0) {
779 for (jp = j->jp, rtjp = rtparams + 1; rtjp < rtparams + nrt;
781 if (JP_RDTUN(jp) && strcmp(jp->jp_name, "jid")) {
782 if (!((jp->jp_flags & (JP_BOOL | JP_NOBOOL)) &&
783 jp->jp_valuelen == 0 &&
784 *(int *)jp->jp_value) &&
785 !(rtjp->jp_valuelen == jp->jp_valuelen &&
786 !memcmp(rtjp->jp_value, jp->jp_value,
789 jail_warnx(j, "%s cannot be "
790 "changed after creation",
802 for (rtjp = rtparams + 1; rtjp < rtparams + nrt; rtjp++)
803 rtjp->jp_name = NULL;
804 jailparam_free(rtparams, nrt);
809 * Get the jail's jid if it is running.
812 running_jid(struct cfjail *j, int dflag)
814 struct iovec jiov[2];
819 if ((pval = string_param(j->intparams[KP_JID]))) {
820 if (!(jid = strtol(pval, &ep, 10)) || *ep) {
824 *(const void **)&jiov[0].iov_base = "jid";
825 jiov[0].iov_len = sizeof("jid");
826 jiov[1].iov_base = &jid;
827 jiov[1].iov_len = sizeof(jid);
828 } else if ((pval = string_param(j->intparams[KP_NAME]))) {
829 *(const void **)&jiov[0].iov_base = "name";
830 jiov[0].iov_len = sizeof("name");
831 jiov[1].iov_len = strlen(pval) + 1;
832 jiov[1].iov_base = alloca(jiov[1].iov_len);
833 strcpy(jiov[1].iov_base, pval);
838 j->jid = jail_get(jiov, 2, dflag ? JAIL_DYING : 0);
842 jail_quoted_warnx(const struct cfjail *j, const char *name_msg,
843 const char *noname_msg)
847 if ((pval = j->name) || (pval = string_param(j->intparams[KP_JID])) ||
848 (pval = string_param(j->intparams[KP_NAME])))
849 warnx("\"%s\" %s", pval, name_msg);
851 warnx("%s", noname_msg);
855 * Set jail parameters and possible print them out.
858 jailparam_set_note(const struct cfjail *j, struct jailparam *jp, unsigned njp,
865 jid = jailparam_set(jp, njp, flags);
867 jail_note(j, "jail_set(%s%s)",
868 (flags & (JAIL_CREATE | JAIL_UPDATE)) == JAIL_CREATE
869 ? "JAIL_CREATE" : "JAIL_UPDATE",
870 (flags & JAIL_DYING) ? " | JAIL_DYING" : "");
871 for (i = 0; i < njp; i++) {
872 printf(" %s", jp[i].jp_name);
873 if (jp[i].jp_value == NULL)
876 value = jailparam_export(jp + i);
878 err(1, "jailparam_export");
879 quoted_print(stdout, value);
883 printf(": %s", strerror(errno));
890 * Print a jail record.
893 print_jail(FILE *fp, struct cfjail *j, int oldcl)
898 fprintf(fp, "%d\t", j->jid);
899 print_param(fp, j->intparams[KP_PATH], ',', 0);
901 print_param(fp, j->intparams[KP_HOST_HOSTNAME], ',', 0);
904 print_param(fp, j->intparams[KP_IP4_ADDR], ',', 0);
906 if (j->intparams[KP_IP4_ADDR] &&
907 !TAILQ_EMPTY(&j->intparams[KP_IP4_ADDR]->val) &&
908 j->intparams[KP_IP6_ADDR] &&
909 !TAILQ_EMPTY(&j->intparams[KP_IP6_ADDR]->val))
914 print_param(fp, j->intparams[KP_IP6_ADDR], ',', 0);
917 print_param(fp, j->intparams[IP_COMMAND], ' ', 0);
919 fprintf(fp, "jid=%d", j->jid);
920 TAILQ_FOREACH(p, &j->params, tq)
921 if (strcmp(p->name, "jid")) {
923 print_param(fp, p, ',', 1);
930 * Print a parameter value, or a name=value pair.
933 print_param(FILE *fp, const struct cfparam *p, int sep, int doname)
935 const struct cfstring *s, *ts;
939 if (p == NULL || TAILQ_EMPTY(&p->val))
943 TAILQ_FOREACH_SAFE(s, &p->val, tq, ts) {
944 quoted_print(fp, s->s);
951 * Print a string with quotes around spaces.
954 quoted_print(FILE *fp, char *str)
960 : strchr(p, '\'') ? '"'
961 : strchr(p, '"') ? '\''
962 : strchr(p, ' ') || strchr(p, '\t') ? '"'
967 if (c == '\\' || c == qc)
979 (void)fprintf(stderr,
980 "usage: jail [-dhilqv] [-J jid_file] [-u username] [-U username]\n"
981 " -[cmr] param=value ... [command=command ...]\n"
982 " jail [-dqv] [-f file] -[cmr] [jail]\n"
983 " jail [-qv] [-f file] -[rR] ['*' | jail ...]\n"
984 " jail [-dhilqv] [-J jid_file] [-u username] [-U username]\n"
985 " [-n jailname] [-s securelevel]\n"
986 " path hostname [ip[,...]] command ...\n");