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[] = {
108 static const enum intparam stopcommands[] = {
118 IP__MOUNT_FROM_FSTAB,
130 main(int argc, char **argv)
138 int ch, docf, error, i, oldcl, sysval;
140 char enforce_statfs[4];
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,
278 sysvallen = sizeof(sysval);
279 if (sysctlbyname("security.jail.enforce_statfs",
280 &sysval, &sysvallen, NULL, 0) == 0) {
281 snprintf(enforce_statfs,
282 sizeof(enforce_statfs), "%d", sysval);
283 add_param(NULL, NULL, KP_ENFORCE_STATFS,
287 } else if (op == JF_STOP) {
288 /* Jail remove, perhaps using the config file */
289 if (!docf || argc == 0)
292 for (i = 0; i < argc; i++)
293 if (strchr(argv[i], '='))
295 if ((docf = !Rflag &&
296 (!strcmp(cfname, "-") || stat(cfname, &st) == 0)))
298 note_remove = docf || argc > 1 || wild_jail_name(argv[0]);
299 } else if (argc > 1 || (argc == 1 && strchr(argv[0], '='))) {
300 /* Single jail specified on the command line */
304 for (i = 0; i < argc; i++) {
305 if (!strncmp(argv[i], "command", 7) &&
306 (argv[i][7] == '\0' || argv[i][7] == '=')) {
307 if (argv[i][7] == '=')
308 add_param(NULL, NULL, IP_COMMAND,
310 for (i++; i < argc; i++)
311 add_param(NULL, NULL, IP_COMMAND,
315 else if (!strncmp(argv[i], "ip4.addr=", 9)) {
316 for (cs = argv[i] + 9;; cs = ncs + 1) {
317 ncs = strchr(cs, ',');
320 add_param(NULL, NULL, KP_IP4_ADDR, cs);
327 else if (!strncmp(argv[i], "ip6.addr=", 9)) {
328 for (cs = argv[i] + 9;; cs = ncs + 1) {
329 ncs = strchr(cs, ',');
332 add_param(NULL, NULL, KP_IP6_ADDR, cs);
339 add_param(NULL, NULL, 0, argv[i]);
342 /* From the config file, perhaps with a specified jail */
348 /* Find out which jails will be run. */
352 for (i = 0; i < argc; i++)
353 if (start_state(argv[i], docf, op, Rflag) < 0)
356 if (start_state(argv[0], docf, op, 0) < 0)
361 if (JidFile != NULL) {
362 jfp = fopen(JidFile, "w");
364 err(1, "open %s", JidFile);
370 * The main loop: Get an available jail and perform the required
371 * operation on it. When that is done, the jail may be finished,
372 * or it may go back for the next step.
374 while ((j = next_jail()))
376 if (j->flags & JF_FAILED) {
378 if (j->comparam == NULL) {
383 if (!(j->flags & JF_PARAMS))
385 j->flags |= JF_PARAMS;
387 add_param(j, NULL, IP_ALLOW_DYING, NULL);
388 if (check_intparams(j) < 0)
390 if ((j->flags & (JF_START | JF_SET)) &&
391 import_params(j) < 0)
396 (j->flags & (JF_SET | JF_DEPEND)) == JF_SET
397 ? dflag || bool_param(j->intparams[IP_ALLOW_DYING])
399 if (finish_command(j))
402 switch (j->flags & JF_OP_MASK) {
404 * These operations just turn into a different op
405 * depending on the jail's current status.
408 j->flags = j->jid < 0 ? JF_START : JF_SET;
412 jail_quoted_warnx(j, "not found",
413 "no jail specified");
417 j->flags = rdtun_params(j, 0) ? JF_RESTART : JF_SET;
418 if (j->flags == JF_RESTART)
421 case JF_START_SET_RESTART:
422 j->flags = j->jid < 0 ? JF_START
423 : rdtun_params(j, 0) ? JF_RESTART : JF_SET;
424 if (j->flags == JF_RESTART)
428 switch (j->flags & JF_OP_MASK) {
430 if (j->comparam == NULL) {
432 !(j->flags & (JF_DEPEND | JF_WILD))) {
433 jail_quoted_warnx(j, "already exists",
441 goto jail_create_done;
442 j->comparam = startcommands;
450 print_jail(jfp, j, oldcl);
455 if (j->jid < 0 && !(j->flags & JF_DEPEND)) {
456 jail_quoted_warnx(j, "not found",
457 "no jail specified");
463 if (!(j->flags & JF_DEPEND)) {
464 if (rdtun_params(j, 1) < 0 ||
467 if (verbose >= 0 && (j->name || verbose > 0))
468 jail_note(j, "updated\n");
475 if (j->comparam == NULL) {
479 if (!(j->flags & (JF_DEPEND|JF_WILD))) {
485 goto jail_remove_done;
487 j->comparam = stopcommands;
489 } else if ((j->flags & JF_FAILED) && j->jid > 0)
490 goto jail_remove_done;
495 if ((j->flags & (JF_START | JF_FAILED)) == JF_START) {
497 j->flags &= ~JF_STOP;
499 requeue(j, j->ndeps ? &depend : &ready);
511 * Mark a jail's failure for future handling.
514 failed(struct cfjail *j)
516 j->flags |= JF_FAILED;
517 TAILQ_REMOVE(j->queue, j, tq);
518 TAILQ_INSERT_HEAD(&ready, j, tq);
523 * Exit slightly more gracefully when out of memory.
537 erealloc(void *ptr, size_t size)
541 p = realloc(ptr, size);
548 estrdup(const char *str)
559 * Print a message including an optional jail name.
562 jail_note(const struct cfjail *j, const char *fmt, ...)
570 len = vsnprintf(NULL, 0, fmt, tap);
572 cs = alloca(len + 1);
573 (void)vsnprintf(cs, len + 1, fmt, ap);
576 printf("%s: %s", j->name, cs);
582 * Print a warning message including an optional jail name.
585 jail_warnx(const struct cfjail *j, const char *fmt, ...)
593 len = vsnprintf(NULL, 0, fmt, tap);
595 cs = alloca(len + 1);
596 (void)vsnprintf(cs, len + 1, fmt, ap);
599 warnx("%s: %s", j->name, cs);
608 create_jail(struct cfjail *j)
610 struct iovec jiov[4];
612 struct jailparam *jp, *setparams, *setparams2, *sjp;
614 int dopersist, ns, jid, dying, didfail;
617 * Check the jail's path, with a better error message than jail_set
620 if ((path = string_param(j->intparams[KP_PATH]))) {
621 if (j->name != NULL && path[0] != '/') {
622 jail_warnx(j, "path %s: not an absolute pathname",
626 if (stat(path, &st) < 0) {
627 jail_warnx(j, "path %s: %s", path, strerror(errno));
630 if (!S_ISDIR(st.st_mode)) {
631 jail_warnx(j, "path %s: %s", path, strerror(ENOTDIR));
637 * Copy all the parameters, except that "persist" is always set when
638 * there are commands to run later.
640 dopersist = !bool_param(j->intparams[KP_PERSIST]) &&
641 (j->intparams[IP_EXEC_START] || j->intparams[IP_COMMAND] ||
642 j->intparams[IP_EXEC_POSTSTART]);
644 alloca((j->njp + dopersist) * sizeof(struct jailparam));
645 if (dopersist && jailparam_init(sjp++, "persist") < 0) {
646 jail_warnx(j, "%s", jail_errmsg);
649 for (jp = j->jp; jp < j->jp + j->njp; jp++)
650 if (!dopersist || !equalopts(jp->jp_name, "persist"))
652 ns = sjp - setparams;
655 j->jid = jailparam_set_note(j, setparams, ns, JAIL_CREATE);
656 if (j->jid < 0 && errno == EEXIST &&
657 bool_param(j->intparams[IP_ALLOW_DYING]) &&
658 int_param(j->intparams[KP_JID], &jid) && jid != 0) {
660 * The jail already exists, but may be dying.
661 * Make sure it is, in which case an update is appropriate.
663 jiov[0].iov_base = __DECONST(char *, "jid");
664 jiov[0].iov_len = sizeof("jid");
665 jiov[1].iov_base = &jid;
666 jiov[1].iov_len = sizeof(jid);
667 jiov[2].iov_base = __DECONST(char *, "dying");
668 jiov[2].iov_len = sizeof("dying");
669 jiov[3].iov_base = &dying;
670 jiov[3].iov_len = sizeof(dying);
671 if (jail_get(jiov, 4, JAIL_DYING) < 0) {
673 * It could be that the jail just barely finished
674 * dying, or it could be that the jid never existed
675 * but the name does. In either case, another try
676 * at creating the jail should do the right thing.
679 j->jid = jailparam_set_note(j, setparams, ns,
683 if (rdtun_params(j, 1) < 0) {
687 sjp = setparams2 = alloca((j->njp + dopersist) *
688 sizeof(struct jailparam));
689 for (jp = setparams; jp < setparams + ns; jp++)
691 !strcmp(jp->jp_name, "jid"))
693 j->jid = jailparam_set_note(j, setparams2,
694 sjp - setparams2, JAIL_UPDATE | JAIL_DYING);
696 * Again, perhaps the jail just finished dying.
698 if (j->jid < 0 && errno == ENOENT)
699 j->jid = jailparam_set_note(j,
700 setparams, ns, JAIL_CREATE);
704 if (j->jid < 0 && !didfail) {
705 jail_warnx(j, "%s", jail_errmsg);
709 jailparam_free(setparams, 1);
711 j->flags |= JF_PERSIST;
717 * Remove a temporarily set "persist" parameter.
720 clear_persist(struct cfjail *j)
722 struct iovec jiov[4];
725 if (!(j->flags & JF_PERSIST))
727 j->flags &= ~JF_PERSIST;
728 jiov[0].iov_base = __DECONST(char *, "jid");
729 jiov[0].iov_len = sizeof("jid");
730 jiov[1].iov_base = &j->jid;
731 jiov[1].iov_len = sizeof(j->jid);
732 jiov[2].iov_base = __DECONST(char *, "nopersist");
733 jiov[2].iov_len = sizeof("nopersist");
734 jiov[3].iov_base = NULL;
736 jid = jail_set(jiov, 4, JAIL_UPDATE);
738 jail_note(j, "jail_set(JAIL_UPDATE) jid=%d nopersist%s%s\n",
739 j->jid, jid < 0 ? ": " : "",
740 jid < 0 ? strerror(errno) : "");
744 * Set a jail's parameters.
747 update_jail(struct cfjail *j)
749 struct jailparam *jp, *setparams, *sjp;
753 for (jp = j->jp; jp < j->jp + j->njp; jp++)
758 sjp = setparams = alloca(++ns * sizeof(struct jailparam));
759 if (jailparam_init(sjp, "jid") < 0 ||
760 jailparam_import_raw(sjp, &j->jid, sizeof j->jid) < 0) {
761 jail_warnx(j, "%s", jail_errmsg);
765 for (jp = j->jp; jp < j->jp + j->njp; jp++)
769 jid = jailparam_set_note(j, setparams, ns,
770 bool_param(j->intparams[IP_ALLOW_DYING])
771 ? JAIL_UPDATE | JAIL_DYING : JAIL_UPDATE);
773 jail_warnx(j, "%s", jail_errmsg);
776 jailparam_free(setparams, 1);
781 * Return if a jail set would change any create-only parameters.
784 rdtun_params(struct cfjail *j, int dofail)
786 struct jailparam *jp, *rtparams, *rtjp;
789 if (j->flags & JF_RDTUN)
791 j->flags |= JF_RDTUN;
793 for (jp = j->jp; jp < j->jp + j->njp; jp++)
794 if (JP_RDTUN(jp) && strcmp(jp->jp_name, "jid"))
798 rtjp = rtparams = alloca(++nrt * sizeof(struct jailparam));
799 if (jailparam_init(rtjp, "jid") < 0 ||
800 jailparam_import_raw(rtjp, &j->jid, sizeof j->jid) < 0) {
801 jail_warnx(j, "%s", jail_errmsg);
804 for (jp = j->jp; jp < j->jp + j->njp; jp++)
805 if (JP_RDTUN(jp) && strcmp(jp->jp_name, "jid"))
808 if (jailparam_get(rtparams, nrt,
809 bool_param(j->intparams[IP_ALLOW_DYING]) ? JAIL_DYING : 0) > 0) {
811 for (jp = j->jp; rtjp < rtparams + nrt; jp++) {
812 if (JP_RDTUN(jp) && strcmp(jp->jp_name, "jid")) {
813 if (!((jp->jp_flags & (JP_BOOL | JP_NOBOOL)) &&
814 jp->jp_valuelen == 0 &&
815 *(int *)jp->jp_value) &&
816 !(rtjp->jp_valuelen == jp->jp_valuelen &&
817 !memcmp(rtjp->jp_value, jp->jp_value,
820 jail_warnx(j, "%s cannot be "
821 "changed after creation",
833 for (rtjp = rtparams + 1; rtjp < rtparams + nrt; rtjp++)
834 rtjp->jp_name = NULL;
835 jailparam_free(rtparams, nrt);
840 * Get the jail's jid if it is running.
843 running_jid(struct cfjail *j, int dflag)
845 struct iovec jiov[2];
850 if ((pval = string_param(j->intparams[KP_JID]))) {
851 if (!(jid = strtol(pval, &ep, 10)) || *ep) {
855 jiov[0].iov_base = __DECONST(char *, "jid");
856 jiov[0].iov_len = sizeof("jid");
857 jiov[1].iov_base = &jid;
858 jiov[1].iov_len = sizeof(jid);
859 } else if ((pval = string_param(j->intparams[KP_NAME]))) {
860 jiov[0].iov_base = __DECONST(char *, "name");
861 jiov[0].iov_len = sizeof("name");
862 jiov[1].iov_len = strlen(pval) + 1;
863 jiov[1].iov_base = alloca(jiov[1].iov_len);
864 strcpy(jiov[1].iov_base, pval);
869 j->jid = jail_get(jiov, 2, dflag ? JAIL_DYING : 0);
873 jail_quoted_warnx(const struct cfjail *j, const char *name_msg,
874 const char *noname_msg)
878 if ((pval = j->name) || (pval = string_param(j->intparams[KP_JID])) ||
879 (pval = string_param(j->intparams[KP_NAME])))
880 warnx("\"%s\" %s", pval, name_msg);
882 warnx("%s", noname_msg);
886 * Set jail parameters and possibly print them out.
889 jailparam_set_note(const struct cfjail *j, struct jailparam *jp, unsigned njp,
896 jid = jailparam_set(jp, njp, flags);
898 jail_note(j, "jail_set(%s%s)",
899 (flags & (JAIL_CREATE | JAIL_UPDATE)) == JAIL_CREATE
900 ? "JAIL_CREATE" : "JAIL_UPDATE",
901 (flags & JAIL_DYING) ? " | JAIL_DYING" : "");
902 for (i = 0; i < njp; i++) {
903 printf(" %s", jp[i].jp_name);
904 if (jp[i].jp_value == NULL)
907 value = jailparam_export(jp + i);
909 err(1, "jailparam_export");
910 quoted_print(stdout, value);
914 printf(": %s", strerror(errno));
921 * Print a jail record.
924 print_jail(FILE *fp, struct cfjail *j, int oldcl)
929 fprintf(fp, "%d\t", j->jid);
930 print_param(fp, j->intparams[KP_PATH], ',', 0);
932 print_param(fp, j->intparams[KP_HOST_HOSTNAME], ',', 0);
935 print_param(fp, j->intparams[KP_IP4_ADDR], ',', 0);
937 if (j->intparams[KP_IP4_ADDR] &&
938 !TAILQ_EMPTY(&j->intparams[KP_IP4_ADDR]->val) &&
939 j->intparams[KP_IP6_ADDR] &&
940 !TAILQ_EMPTY(&j->intparams[KP_IP6_ADDR]->val))
945 print_param(fp, j->intparams[KP_IP6_ADDR], ',', 0);
948 print_param(fp, j->intparams[IP_COMMAND], ' ', 0);
950 fprintf(fp, "jid=%d", j->jid);
951 TAILQ_FOREACH(p, &j->params, tq)
952 if (strcmp(p->name, "jid")) {
954 print_param(fp, p, ',', 1);
961 * Print a parameter value, or a name=value pair.
964 print_param(FILE *fp, const struct cfparam *p, int sep, int doname)
966 const struct cfstring *s, *ts;
970 if (p == NULL || TAILQ_EMPTY(&p->val))
974 TAILQ_FOREACH_SAFE(s, &p->val, tq, ts) {
975 quoted_print(fp, s->s);
982 * Print a string with quotes around spaces.
985 quoted_print(FILE *fp, char *str)
991 : strchr(p, '\'') ? '"'
992 : strchr(p, '"') ? '\''
993 : strchr(p, ' ') || strchr(p, '\t') ? '"'
998 if (c == '\\' || c == qc)
1010 (void)fprintf(stderr,
1011 "usage: jail [-dhilqv] [-J jid_file] [-u username] [-U username]\n"
1012 " -[cmr] param=value ... [command=command ...]\n"
1013 " jail [-dqv] [-f file] -[cmr] [jail]\n"
1014 " jail [-qv] [-f file] -[rR] ['*' | jail ...]\n"
1015 " jail [-dhilqv] [-J jid_file] [-u username] [-U username]\n"
1016 " [-n jailname] [-s securelevel]\n"
1017 " path hostname [ip[,...]] command ...\n");