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 else if (!strncmp(argv[i], "ip4.addr=", 9)) {
310 for (cs = argv[i] + 9;; cs = ncs + 1) {
311 ncs = strchr(cs, ',');
314 add_param(NULL, NULL, KP_IP4_ADDR, cs);
321 else if (!strncmp(argv[i], "ip6.addr=", 9)) {
322 for (cs = argv[i] + 9;; cs = ncs + 1) {
323 ncs = strchr(cs, ',');
326 add_param(NULL, NULL, KP_IP6_ADDR, cs);
333 add_param(NULL, NULL, 0, argv[i]);
336 /* From the config file, perhaps with a specified jail */
342 /* Find out which jails will be run. */
346 for (i = 0; i < argc; i++)
347 if (start_state(argv[i], docf, op, Rflag) < 0)
350 if (start_state(argv[0], docf, op, 0) < 0)
355 if (JidFile != NULL) {
356 jfp = fopen(JidFile, "w");
358 err(1, "open %s", JidFile);
364 * The main loop: Get an available jail and perform the required
365 * operation on it. When that is done, the jail may be finished,
366 * or it may go back for the next step.
368 while ((j = next_jail()))
370 if (j->flags & JF_FAILED) {
372 if (j->comparam == NULL) {
377 if (!(j->flags & JF_PARAMS))
379 j->flags |= JF_PARAMS;
381 add_param(j, NULL, IP_ALLOW_DYING, NULL);
382 if (check_intparams(j) < 0)
384 if ((j->flags & (JF_START | JF_SET)) &&
385 import_params(j) < 0)
390 (j->flags & (JF_SET | JF_DEPEND)) == JF_SET
391 ? dflag || bool_param(j->intparams[IP_ALLOW_DYING])
393 if (finish_command(j))
396 switch (j->flags & JF_OP_MASK) {
398 * These operations just turn into a different op
399 * depending on the jail's current status.
402 j->flags = j->jid < 0 ? JF_START : JF_SET;
406 jail_quoted_warnx(j, "not found",
407 "no jail specified");
411 j->flags = rdtun_params(j, 0) ? JF_RESTART : JF_SET;
412 if (j->flags == JF_RESTART)
415 case JF_START_SET_RESTART:
416 j->flags = j->jid < 0 ? JF_START
417 : rdtun_params(j, 0) ? JF_RESTART : JF_SET;
418 if (j->flags == JF_RESTART)
422 switch (j->flags & JF_OP_MASK) {
424 if (j->comparam == NULL) {
426 !(j->flags & (JF_DEPEND | JF_WILD))) {
427 jail_quoted_warnx(j, "already exists",
435 goto jail_create_done;
436 j->comparam = startcommands;
444 print_jail(jfp, j, oldcl);
449 if (j->jid < 0 && !(j->flags & JF_DEPEND)) {
450 jail_quoted_warnx(j, "not found",
451 "no jail specified");
457 if (!(j->flags & JF_DEPEND)) {
458 if (rdtun_params(j, 1) < 0 ||
461 if (verbose >= 0 && (j->name || verbose > 0))
462 jail_note(j, "updated\n");
469 if (j->comparam == NULL) {
473 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 *(const void **)&jiov[0].iov_base = "jid";
656 jiov[0].iov_len = sizeof("jid");
657 jiov[1].iov_base = &jid;
658 jiov[1].iov_len = sizeof(jid);
659 *(const void **)&jiov[2].iov_base = "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 *(const void **)&jiov[0].iov_base = "jid";
721 jiov[0].iov_len = sizeof("jid");
722 jiov[1].iov_base = &j->jid;
723 jiov[1].iov_len = sizeof(j->jid);
724 *(const void **)&jiov[2].iov_base = "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"))
800 if (jailparam_get(rtparams, nrt,
801 bool_param(j->intparams[IP_ALLOW_DYING]) ? JAIL_DYING : 0) > 0) {
803 for (jp = j->jp, rtjp = rtparams + 1; rtjp < rtparams + nrt;
805 if (JP_RDTUN(jp) && strcmp(jp->jp_name, "jid")) {
806 if (!((jp->jp_flags & (JP_BOOL | JP_NOBOOL)) &&
807 jp->jp_valuelen == 0 &&
808 *(int *)jp->jp_value) &&
809 !(rtjp->jp_valuelen == jp->jp_valuelen &&
810 !memcmp(rtjp->jp_value, jp->jp_value,
813 jail_warnx(j, "%s cannot be "
814 "changed after creation",
826 for (rtjp = rtparams + 1; rtjp < rtparams + nrt; rtjp++)
827 rtjp->jp_name = NULL;
828 jailparam_free(rtparams, nrt);
833 * Get the jail's jid if it is running.
836 running_jid(struct cfjail *j, int dflag)
838 struct iovec jiov[2];
843 if ((pval = string_param(j->intparams[KP_JID]))) {
844 if (!(jid = strtol(pval, &ep, 10)) || *ep) {
848 *(const void **)&jiov[0].iov_base = "jid";
849 jiov[0].iov_len = sizeof("jid");
850 jiov[1].iov_base = &jid;
851 jiov[1].iov_len = sizeof(jid);
852 } else if ((pval = string_param(j->intparams[KP_NAME]))) {
853 *(const void **)&jiov[0].iov_base = "name";
854 jiov[0].iov_len = sizeof("name");
855 jiov[1].iov_len = strlen(pval) + 1;
856 jiov[1].iov_base = alloca(jiov[1].iov_len);
857 strcpy(jiov[1].iov_base, pval);
862 j->jid = jail_get(jiov, 2, dflag ? JAIL_DYING : 0);
866 jail_quoted_warnx(const struct cfjail *j, const char *name_msg,
867 const char *noname_msg)
871 if ((pval = j->name) || (pval = string_param(j->intparams[KP_JID])) ||
872 (pval = string_param(j->intparams[KP_NAME])))
873 warnx("\"%s\" %s", pval, name_msg);
875 warnx("%s", noname_msg);
879 * Set jail parameters and possible print them out.
882 jailparam_set_note(const struct cfjail *j, struct jailparam *jp, unsigned njp,
889 jid = jailparam_set(jp, njp, flags);
891 jail_note(j, "jail_set(%s%s)",
892 (flags & (JAIL_CREATE | JAIL_UPDATE)) == JAIL_CREATE
893 ? "JAIL_CREATE" : "JAIL_UPDATE",
894 (flags & JAIL_DYING) ? " | JAIL_DYING" : "");
895 for (i = 0; i < njp; i++) {
896 printf(" %s", jp[i].jp_name);
897 if (jp[i].jp_value == NULL)
900 value = jailparam_export(jp + i);
902 err(1, "jailparam_export");
903 quoted_print(stdout, value);
907 printf(": %s", strerror(errno));
914 * Print a jail record.
917 print_jail(FILE *fp, struct cfjail *j, int oldcl)
922 fprintf(fp, "%d\t", j->jid);
923 print_param(fp, j->intparams[KP_PATH], ',', 0);
925 print_param(fp, j->intparams[KP_HOST_HOSTNAME], ',', 0);
928 print_param(fp, j->intparams[KP_IP4_ADDR], ',', 0);
930 if (j->intparams[KP_IP4_ADDR] &&
931 !TAILQ_EMPTY(&j->intparams[KP_IP4_ADDR]->val) &&
932 j->intparams[KP_IP6_ADDR] &&
933 !TAILQ_EMPTY(&j->intparams[KP_IP6_ADDR]->val))
938 print_param(fp, j->intparams[KP_IP6_ADDR], ',', 0);
941 print_param(fp, j->intparams[IP_COMMAND], ' ', 0);
943 fprintf(fp, "jid=%d", j->jid);
944 TAILQ_FOREACH(p, &j->params, tq)
945 if (strcmp(p->name, "jid")) {
947 print_param(fp, p, ',', 1);
954 * Print a parameter value, or a name=value pair.
957 print_param(FILE *fp, const struct cfparam *p, int sep, int doname)
959 const struct cfstring *s, *ts;
963 if (p == NULL || TAILQ_EMPTY(&p->val))
967 TAILQ_FOREACH_SAFE(s, &p->val, tq, ts) {
968 quoted_print(fp, s->s);
975 * Print a string with quotes around spaces.
978 quoted_print(FILE *fp, char *str)
984 : strchr(p, '\'') ? '"'
985 : strchr(p, '"') ? '\''
986 : strchr(p, ' ') || strchr(p, '\t') ? '"'
991 if (c == '\\' || c == qc)
1003 (void)fprintf(stderr,
1004 "usage: jail [-dhilqv] [-J jid_file] [-u username] [-U username]\n"
1005 " -[cmr] param=value ... [command=command ...]\n"
1006 " jail [-dqv] [-f file] -[cmr] [jail]\n"
1007 " jail [-qv] [-f file] -[rR] ['*' | jail ...]\n"
1008 " jail [-dhilqv] [-J jid_file] [-u username] [-U username]\n"
1009 " [-n jailname] [-s securelevel]\n"
1010 " path hostname [ip[,...]] command ...\n");